This commit is contained in:
Hackerpilot 2014-01-26 23:00:57 -08:00
parent d13d680b74
commit 6bed0a287a
3 changed files with 60 additions and 18 deletions

37
analysis/numbers.d Normal file
View File

@ -0,0 +1,37 @@
// 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.numbers;
import std.regex;
import stdx.d.ast;
import stdx.d.lexer;
import analysis.base;
/**
* Checks for use of the deprecated "delete" keyword
*/
class NumberStyleCheck : BaseAnalyzer
{
alias visit = BaseAnalyzer.visit;
this(string fileName)
{
super(fileName);
}
override void visit(Token t)
{
if (isNumberLiteral(t.type) && (t.text.matchFirst(badBinaryRegex)
|| t.text.matchFirst(badDecimalRegex)))
{
addErrorMessage(t.line, t.column,
"Use underscores to improve number constant readability");
}
}
auto badBinaryRegex = ctRegex!(`0b[01]{9,}`);
auto badDecimalRegex = ctRegex!(`\d{4,}`);
}

View File

@ -17,6 +17,7 @@ import analysis.enumarrayliteral;
import analysis.pokemon;
import analysis.del;
import analysis.fish;
import analysis.numbers;
void messageFunction(string fileName, size_t line, size_t column, string message,
bool isError)
@ -71,8 +72,12 @@ void analyze(File output, string[] fileNames, bool staticAnalyze = true)
auto fish = new FloatOperatorCheck(fileName);
fish.visit(m);
auto numbers = new NumberStyleCheck(fileName);
numbers.visit(m);
foreach (message; sort(chain(enums.messages, style.messages,
pokemon.messages, del.messages, fish.messages).array))
pokemon.messages, del.messages, fish.messages, numbers.messages
).array))
{
writeln(message);
}

View File

@ -59,12 +59,12 @@ class XMLPrinter : ASTVisitor
output.writeln("<andAndExpression>");
output.writeln("<left>");
andAndExpression.left.accept(this);
output.writeln("<left>");
output.writeln("</left>");
if (andAndExpression.right !is null)
{
output.writeln("<right>");
andAndExpression.right.accept(this);
output.writeln("<right>");
output.writeln("</right>");
}
output.writeln("</andAndExpression>");
}
@ -74,12 +74,12 @@ class XMLPrinter : ASTVisitor
output.writeln("<andExpression>");
output.writeln("<left>");
andExpression.left.accept(this);
output.writeln("<left>");
output.writeln("</left>");
if (andExpression.right !is null)
{
output.writeln("<right>");
andExpression.right.accept(this);
output.writeln("<right>");
output.writeln("</right>");
}
output.writeln("</andExpression>");
}
@ -429,14 +429,14 @@ class XMLPrinter : ASTVisitor
override void visit(EqualExpression equalExpression)
{
output.writeln("<enumMember operator=\"", str(equalExpression.operator), "\">");
output.writeln("<equalExpression operator=\"", str(equalExpression.operator), "\">");
output.writeln("<left>");
visit(equalExpression.left);
equalExpression.left.accept(this);
output.writeln("</left>");
output.writeln("<right>");
visit(equalExpression.right);
equalExpression.right.accept(this);
output.writeln("</right>");
output.writeln("</enumMember>");
output.writeln("</equalExpression>");
}
override void visit(Expression expression)
@ -637,17 +637,17 @@ class XMLPrinter : ASTVisitor
visit(ifStatement.type);
visit(ifStatement.identifier);
}
visit(ifStatement.expression);
ifStatement.expression.accept(this);
output.writeln("</condition>");
output.writeln("<then>");
visit(ifStatement.thenStatement);
ifStatement.thenStatement.accept(this);
output.writeln("</then>");
if (ifStatement.elseStatement !is null)
{
output.writeln("<else>");
visit(ifStatement.elseStatement);
ifStatement.elseStatement.accept(this);
output.writeln("</else>");
}
output.writeln("</ifStatement>");
@ -983,10 +983,10 @@ class XMLPrinter : ASTVisitor
output.writeln("<relExpression operator=\"",
xmlEscape(str(relExpression.operator)), "\">");
output.writeln("<left>");
visit(relExpression.left);
relExpression.left.accept(this);
output.writeln("</left>");
output.writeln("<right>");
visit(relExpression.right);
relExpression.right.accept(this);
output.writeln("</right>");
output.writeln("</relExpression>");
}
@ -1023,10 +1023,10 @@ class XMLPrinter : ASTVisitor
output.writeln("<shiftExpression operator=\"",
xmlEscape(str(shiftExpression.operator)), "\">");
output.writeln("<left>");
visit(shiftExpression.left);
shiftExpression.left.accept(this);
output.writeln("</left>");
output.writeln("<right>");
visit(shiftExpression.right);
shiftExpression.right.accept(this);
output.writeln("</right>");
output.writeln("</shiftExpression>");
}
@ -1404,11 +1404,11 @@ class XMLPrinter : ASTVisitor
{
output.writeln("<prefix>", xmlEscape(unaryExpression.prefix.text),
"</prefix>");
visit(unaryExpression.unaryExpression);
unaryExpression.unaryExpression.accept(this);
}
if (unaryExpression.suffix != tok!"")
{
visit(unaryExpression.unaryExpression);
unaryExpression.unaryExpression.accept(this);
output.writeln("<suffix>", unaryExpression.suffix.text,
"</suffix>");
}