Relaxed rules on number formatting for #84. Implemented #86

This commit is contained in:
Hackerpilot 2014-01-26 23:29:45 -08:00
parent 6bed0a287a
commit 3df1324e9b
5 changed files with 87 additions and 15 deletions

View File

@ -18,6 +18,18 @@ public:
protected:
bool inAggregate = false;
template visitTemplate(T)
{
override void visit(T structDec)
{
inAggregate = true;
structDec.accept(this);
inAggregate = false;
}
}
import core.vararg;
void addErrorMessage(size_t line, size_t column, string message)

View File

@ -20,19 +20,8 @@ class EnumArrayLiteralCheck : BaseAnalyzer
super(fileName);
}
bool inAggregate = false;
bool looking = false;
template visitTemplate(T)
{
override void visit(T structDec)
{
inAggregate = true;
structDec.accept(this);
inAggregate = false;
}
}
mixin visitTemplate!ClassDeclaration;
mixin visitTemplate!InterfaceDeclaration;
mixin visitTemplate!UnionDeclaration;

View File

@ -24,7 +24,9 @@ class NumberStyleCheck : BaseAnalyzer
override void visit(Token t)
{
if (isNumberLiteral(t.type) && (t.text.matchFirst(badBinaryRegex)
import std.algorithm;
if (isNumberLiteral(t.type) && !t.text.startsWith("0x")
&& ((t.text.startsWith("0b") && t.text.matchFirst(badBinaryRegex))
|| t.text.matchFirst(badDecimalRegex)))
{
addErrorMessage(t.line, t.column,
@ -33,5 +35,5 @@ class NumberStyleCheck : BaseAnalyzer
}
auto badBinaryRegex = ctRegex!(`0b[01]{9,}`);
auto badDecimalRegex = ctRegex!(`\d{4,}`);
auto badDecimalRegex = ctRegex!(`\d{5,}`);
}

65
analysis/objectconst.d Normal file
View File

@ -0,0 +1,65 @@
// Copyright Brian Schott (Sir Alaran) 2014.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
module analysis.objectconst;
import std.regex;
import stdx.d.ast;
import stdx.d.lexer;
import analysis.base;
/**
* Checks for use of the deprecated "delete" keyword
*/
class ObjectConstCheck : BaseAnalyzer
{
alias visit = BaseAnalyzer.visit;
this(string fileName)
{
super(fileName);
}
mixin visitTemplate!ClassDeclaration;
mixin visitTemplate!InterfaceDeclaration;
mixin visitTemplate!UnionDeclaration;
mixin visitTemplate!StructDeclaration;
override void visit(Declaration d)
{
if (inAggregate && d.functionDeclaration !is null
&& isInteresting(d.functionDeclaration.name.text)
&& (!hasConst(d.attributes)
&& !hasConst(d.functionDeclaration.memberFunctionAttributes)))
{
addErrorMessage(d.functionDeclaration.name.line,
d.functionDeclaration.name.column, "opCmp, ToHash, opEquals,"
~ " and toString should be declared const");
}
d.accept(this);
}
private static bool hasConst(Attribute[] attributes)
{
import std.algorithm;
return attributes.any!(a => a.attribute == tok!"const"
|| (a.storageClass !is null && a.storageClass.token == tok!"const"));
}
private static bool hasConst(MemberFunctionAttribute[] attributes)
{
import std.algorithm;
return attributes.any!(a => a.tokenType == tok!"const");
}
private static bool isInteresting(string name)
{
return name == "opCmp" || name == "toHash" || name == "opEquals"
|| name == "toString";
}
private bool looking = false;
}

View File

@ -18,6 +18,7 @@ import analysis.pokemon;
import analysis.del;
import analysis.fish;
import analysis.numbers;
import analysis.objectconst;
void messageFunction(string fileName, size_t line, size_t column, string message,
bool isError)
@ -75,9 +76,12 @@ void analyze(File output, string[] fileNames, bool staticAnalyze = true)
auto numbers = new NumberStyleCheck(fileName);
numbers.visit(m);
auto objConst = new ObjectConstCheck(fileName);
objConst.visit(m);
foreach (message; sort(chain(enums.messages, style.messages,
pokemon.messages, del.messages, fish.messages, numbers.messages
).array))
pokemon.messages, del.messages, fish.messages, numbers.messages,
objConst.messages).array))
{
writeln(message);
}