Skip to content

Commit

Permalink
Use DMD in NumberStyleCheck (#88)
Browse files Browse the repository at this point in the history
* Replace libdparse with DMD in NumberStyleCheck

* Fix re-lexing for windows (CRLF terminators) files

* Improve unit test
  • Loading branch information
Vladiwostok authored Mar 5, 2024
1 parent 24f48ed commit a805955
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 36 deletions.
90 changes: 58 additions & 32 deletions src/dscanner/analysis/numbers.d
Original file line number Diff line number Diff line change
Expand Up @@ -5,70 +5,96 @@

module dscanner.analysis.numbers;

import std.stdio;
import std.regex;
import dparse.ast;
import dparse.lexer;
import dscanner.analysis.base;
import dscanner.analysis.helpers;
import dsymbol.scope_ : Scope;
import dmd.tokens : TOK;
import std.conv;
import std.regex;

/**
* Checks for long and hard-to-read number literals
*/
final class NumberStyleCheck : BaseAnalyzer
extern (C++) class NumberStyleCheck(AST) : BaseAnalyzerDmd
{
public:
alias visit = BaseAnalyzer.visit;

alias visit = BaseAnalyzerDmd.visit;
mixin AnalyzerInfo!"number_style_check";

/**
* Constructs the style checker with the given file name.
*/
this(BaseAnalyzerArguments args)
private enum KEY = "dscanner.style.number_literals";
private enum string MSG = "Use underscores to improve number constant readability.";

private auto badBinaryRegex = ctRegex!(`^0b[01]{9,}`);
private auto badDecimalRegex = ctRegex!(`^\d{5,}`);

extern (D) this(string fileName, bool skipTests = false)
{
super(args);
super(fileName, skipTests);
}

override void visit(const Token t)
override void visit(AST.IntegerExp intExpr)
{
import std.algorithm : startsWith;
import dscanner.utils : readFile;
import dmd.errorsink : ErrorSinkNull;
import dmd.globals : global;
import dmd.lexer : Lexer;

auto bytes = readFile(fileName) ~ '\0';
bytes = bytes[intExpr.loc.fileOffset .. $];

__gshared ErrorSinkNull errorSinkNull;
if (!errorSinkNull)
errorSinkNull = new ErrorSinkNull;

if (isNumberLiteral(t.type) && !t.text.startsWith("0x")
&& ((t.text.startsWith("0b") && !t.text.matchFirst(badBinaryRegex)
.empty) || !t.text.matchFirst(badDecimalRegex).empty))
scope lexer = new Lexer(null, cast(char*) bytes, 0, bytes.length, 0, 0, errorSinkNull, &global.compileEnv);
auto tokenValue = lexer.nextToken();
bool isInt = false;

while (tokenValue != TOK.semicolon && tokenValue != TOK.endOfFile)
{
addErrorMessage(t, "dscanner.style.number_literals",
"Use underscores to improve number constant readability.");
if (isIntegerLiteral(tokenValue))
{
isInt = true;
break;
}

tokenValue = lexer.nextToken();
}

if (!isInt)
return;

auto tokenText = to!string(lexer.token.ptr);

if (!matchFirst(tokenText, badDecimalRegex).empty || !matchFirst(tokenText, badBinaryRegex).empty)
addErrorMessage(intExpr.loc.linnum, intExpr.loc.charnum, KEY, MSG);
}

private:
auto badBinaryRegex = ctRegex!(`^0b[01]{9,}`);
auto badDecimalRegex = ctRegex!(`^\d{5,}`);
private bool isIntegerLiteral(TOK token)
{
return token >= TOK.int32Literal && token <= TOK.uns128Literal;
}
}

unittest
{
import dscanner.analysis.config : StaticAnalysisConfig, Check, disabledConfig;
import dscanner.analysis.helpers : assertAnalyzerWarningsDMD;
import std.stdio : stderr;

StaticAnalysisConfig sac = disabledConfig();
sac.number_style_check = Check.enabled;
assertAnalyzerWarnings(q{
assertAnalyzerWarningsDMD(q{
void testNumbers()
{
int a;
a = 1; // ok
a = 10; // ok
a = 100; // ok
a = 1000; // ok
a = 10000; /+
^^^^^ [warn]: Use underscores to improve number constant readability. +/
a = 100000; /+
^^^^^^ [warn]: Use underscores to improve number constant readability. +/
a = 1000000; /+
^^^^^^^ [warn]: Use underscores to improve number constant readability. +/
a = 10_00; // ok
a = 10_000; // ok
a = 100_000; // ok
a = 10000; // [warn]: Use underscores to improve number constant readability.
a = 100000; // [warn]: Use underscores to improve number constant readability.
a = 1000000; // [warn]: Use underscores to improve number constant readability.
}
}c, sac);

Expand Down
10 changes: 6 additions & 4 deletions src/dscanner/analysis/run.d
Original file line number Diff line number Diff line change
Expand Up @@ -855,10 +855,6 @@ private BaseAnalyzer[] getAnalyzersForModuleAndConfig(string fileName,
checks ~= new MismatchedArgumentCheck(args.setSkipTests(
analysisConfig.mismatched_args_check == Check.skipTests && !ut));

if (moduleName.shouldRun!NumberStyleCheck(analysisConfig))
checks ~= new NumberStyleCheck(args.setSkipTests(
analysisConfig.number_style_check == Check.skipTests && !ut));

if (moduleName.shouldRun!StyleChecker(analysisConfig))
checks ~= new StyleChecker(args.setSkipTests(
analysisConfig.style_check == Check.skipTests && !ut));
Expand Down Expand Up @@ -1345,6 +1341,12 @@ MessageSet analyzeDmd(string fileName, ASTCodegen.Module m, const char[] moduleN
config.redundant_storage_classes == Check.skipTests && !ut
);

if (moduleName.shouldRunDmd!(NumberStyleCheck!ASTCodegen)(config))
visitors ~= new NumberStyleCheck!ASTCodegen(
fileName,
config.number_style_check == Check.skipTests && !ut
);

foreach (visitor; visitors)
{
m.accept(visitor);
Expand Down

0 comments on commit a805955

Please sign in to comment.