-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use DMD in NumberStyleCheck #88
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,70 +5,82 @@ | |
|
||
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; | ||
import std.stdio; | ||
|
||
/** | ||
* 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; | ||
|
||
if (isNumberLiteral(t.type) && !t.text.startsWith("0x") | ||
&& ((t.text.startsWith("0b") && !t.text.matchFirst(badBinaryRegex) | ||
.empty) || !t.text.matchFirst(badDecimalRegex).empty)) | ||
{ | ||
addErrorMessage(t, "dscanner.style.number_literals", | ||
"Use underscores to improve number constant readability."); | ||
} | ||
auto bytes = readFile(fileName) ~ '\0'; | ||
bytes = bytes[intExpr.loc.fileOffset .. $]; | ||
|
||
__gshared ErrorSinkNull errorSinkNull; | ||
if (!errorSinkNull) | ||
errorSinkNull = new ErrorSinkNull; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we doing all of these initializations here? Also, why is the file read here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nvm, I understand now. Isn't there any way to reuse a prior used errorSinkNull? For example, the one that was used to lex the initial source? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't that the behaviour of __gshared attribute? |
||
|
||
scope lexer = new Lexer(null, cast(char*) bytes, 0, bytes.length, 0, 0, errorSinkNull, &global.compileEnv); | ||
lexer.nextToken(); | ||
|
||
auto tokenValue = lexer.token.value; | ||
auto tokenText = to!string(lexer.token.ptr); | ||
|
||
if (!isIntegerLiteral(tokenValue)) | ||
return; | ||
|
||
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; | ||
|
||
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 = 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would also add some numbers with underscores just to make sure that does do not actually output any warnings. E.G: 1_000. Also, should 10_00 output any warnings? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will add tests for passing cases. |
||
} | ||
}c, sac); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -860,10 +860,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)); | ||
|
@@ -1350,6 +1346,12 @@ MessageSet analyzeDmd(string fileName, ASTCodegen.Module m, const char[] moduleN | |
config.redundant_storage_classes == Check.skipTests && !ut | ||
); | ||
|
||
if (moduleName.shouldRunDmd!(NumberStyleCheck!ASTCodegen)(config)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is ASTCodegen passed here since we are only using the lexer? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because the BaseAnalyzerDmd follows an inheritance chain starting with the parent class ParseTimeVisitor!ASTCodegen . Should ASTBase struct be used instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, it's kind of annoying that BaseAnalyzerDmd is hardcoded to ASTCodegen. Why not just pass AST to BaseAnalyzer? E.G: class BaseAnalyzerDmd(AST) : ParseTimeVisitor!AST {...} I suggest you look into this in a subsequent PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
visitors ~= new NumberStyleCheck!ASTCodegen( | ||
fileName, | ||
config.number_style_check == Check.skipTests && !ut | ||
); | ||
|
||
foreach (visitor; visitors) | ||
{ | ||
m.accept(visitor); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it really necessary to reread the file? Don't we already have the source in memory?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so. I'm thinking a proper solution for this would be a bigger refactor - a common lexer visitor base class, passing down a token stream to the visitors created from the initial d-scanner run, etc.