mirror of
https://github.com/dlang-community/D-Scanner.git
synced 2025-04-28 06:10:01 +03:00
replace libdparse in alias style visitor (#38)
This commit is contained in:
parent
65720ab41f
commit
07f97fb4c5
2 changed files with 51 additions and 22 deletions
|
@ -5,50 +5,80 @@
|
||||||
|
|
||||||
module dscanner.analysis.alias_syntax_check;
|
module dscanner.analysis.alias_syntax_check;
|
||||||
|
|
||||||
import dparse.ast;
|
|
||||||
import dparse.lexer;
|
|
||||||
import dscanner.analysis.base;
|
import dscanner.analysis.base;
|
||||||
|
import dmd.tokens;
|
||||||
|
import dmd.lexer : Lexer;
|
||||||
|
import dmd.location : Loc;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks for uses of the old alias syntax.
|
* Checks for uses of the old alias syntax.
|
||||||
*/
|
*/
|
||||||
final class AliasSyntaxCheck : BaseAnalyzer
|
extern(C++) class AliasSyntaxCheck(AST) : BaseAnalyzerDmd
|
||||||
{
|
{
|
||||||
alias visit = BaseAnalyzer.visit;
|
|
||||||
|
|
||||||
mixin AnalyzerInfo!"alias_syntax_check";
|
mixin AnalyzerInfo!"alias_syntax_check";
|
||||||
|
alias visit = BaseAnalyzerDmd.visit;
|
||||||
|
|
||||||
this(BaseAnalyzerArguments args)
|
extern(D) this(string fileName)
|
||||||
{
|
{
|
||||||
super(args);
|
super(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
override void visit(const AliasDeclaration ad)
|
override void visit(AST.AliasDeclaration ad)
|
||||||
{
|
{
|
||||||
if (ad.declaratorIdentifierList is null)
|
import dscanner.utils: readFile;
|
||||||
return;
|
import dmd.errorsink : ErrorSinkNull;
|
||||||
assert(ad.declaratorIdentifierList.identifiers.length > 0,
|
import dmd.globals : global;
|
||||||
"Identifier list length is zero, libdparse has a bug");
|
|
||||||
addErrorMessage(ad, KEY,
|
__gshared ErrorSinkNull errorSinkNull;
|
||||||
"Prefer the new \"'alias' identifier '=' type ';'\" syntax"
|
if (!errorSinkNull)
|
||||||
~ " to the old \"'alias' type identifier ';'\" syntax.");
|
errorSinkNull = new ErrorSinkNull;
|
||||||
|
|
||||||
|
auto bytes = readFile(fileName);
|
||||||
|
bool foundEq = false;
|
||||||
|
Loc idLoc;
|
||||||
|
|
||||||
|
bytes ~= '\0';
|
||||||
|
bytes = bytes[ad.loc.fileOffset .. $];
|
||||||
|
|
||||||
|
scope lexer = new Lexer(null, cast(char*) bytes, 0, bytes.length, 0, 0, errorSinkNull, &global.compileEnv);
|
||||||
|
TOK nextTok;
|
||||||
|
lexer.nextToken();
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
if (lexer.token.value == TOK.assign)
|
||||||
|
foundEq = true;
|
||||||
|
|
||||||
|
if (lexer.token.value == TOK.identifier)
|
||||||
|
idLoc = lexer.token.loc;
|
||||||
|
|
||||||
|
nextTok = lexer.nextToken;
|
||||||
|
}
|
||||||
|
while(nextTok != TOK.semicolon && nextTok != TOK.endOfFile);
|
||||||
|
|
||||||
|
if (!foundEq)
|
||||||
|
// Re-lexing is done based on offsets, so the alias appears to be at line 1.
|
||||||
|
// Fix this by computing the initial location.
|
||||||
|
addErrorMessage(cast(ulong) (ad.loc.linnum + idLoc.linnum - 1), cast(ulong) idLoc.charnum, KEY,
|
||||||
|
"Prefer the new \"'alias' identifier '=' type ';'\" syntax"
|
||||||
|
~ " to the old \"'alias' type identifier ';'\" syntax.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
enum KEY = "dscanner.style.alias_syntax";
|
enum KEY = "dscanner.style.alias_syntax";
|
||||||
}
|
}
|
||||||
|
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
import dscanner.analysis.helpers : assertAnalyzerWarnings;
|
import dscanner.analysis.helpers : assertAnalyzerWarnings = assertAnalyzerWarningsDMD;
|
||||||
import dscanner.analysis.config : StaticAnalysisConfig, Check, disabledConfig;
|
import dscanner.analysis.config : StaticAnalysisConfig, Check, disabledConfig;
|
||||||
import std.stdio : stderr;
|
import std.stdio : stderr;
|
||||||
|
|
||||||
StaticAnalysisConfig sac = disabledConfig();
|
StaticAnalysisConfig sac = disabledConfig();
|
||||||
sac.alias_syntax_check = Check.enabled;
|
sac.alias_syntax_check = Check.enabled;
|
||||||
assertAnalyzerWarnings(q{
|
assertAnalyzerWarnings(q{
|
||||||
alias int abcde; /+
|
alias int abcde; // [warn]: Prefer the new "'alias' identifier '=' type ';'" syntax to the old "'alias' type identifier ';'" syntax.
|
||||||
^^^^^^^^^^^^^^^^ [warn]: Prefer the new "'alias' identifier '=' type ';'" syntax to the old "'alias' type identifier ';'" syntax.+/
|
|
||||||
alias abcde = int;
|
alias abcde = int;
|
||||||
}c, sac);
|
}c, sac);
|
||||||
|
|
||||||
|
|
|
@ -929,10 +929,6 @@ private BaseAnalyzer[] getAnalyzersForModuleAndConfig(string fileName,
|
||||||
checks ~= new UselessAssertCheck(args.setSkipTests(
|
checks ~= new UselessAssertCheck(args.setSkipTests(
|
||||||
analysisConfig.useless_assert_check == Check.skipTests && !ut));
|
analysisConfig.useless_assert_check == Check.skipTests && !ut));
|
||||||
|
|
||||||
if (moduleName.shouldRun!AliasSyntaxCheck(analysisConfig))
|
|
||||||
checks ~= new AliasSyntaxCheck(args.setSkipTests(
|
|
||||||
analysisConfig.alias_syntax_check == Check.skipTests && !ut));
|
|
||||||
|
|
||||||
if (moduleName.shouldRun!StaticIfElse(analysisConfig))
|
if (moduleName.shouldRun!StaticIfElse(analysisConfig))
|
||||||
checks ~= new StaticIfElse(args.setSkipTests(
|
checks ~= new StaticIfElse(args.setSkipTests(
|
||||||
analysisConfig.static_if_else_check == Check.skipTests && !ut));
|
analysisConfig.static_if_else_check == Check.skipTests && !ut));
|
||||||
|
@ -1322,6 +1318,9 @@ MessageSet analyzeDmd(string fileName, ASTBase.Module m, const char[] moduleName
|
||||||
if (moduleName.shouldRunDmd!(LengthSubtractionCheck!ASTBase)(config))
|
if (moduleName.shouldRunDmd!(LengthSubtractionCheck!ASTBase)(config))
|
||||||
visitors ~= new LengthSubtractionCheck!ASTBase(fileName);
|
visitors ~= new LengthSubtractionCheck!ASTBase(fileName);
|
||||||
|
|
||||||
|
if (moduleName.shouldRunDmd!(AliasSyntaxCheck!ASTBase)(config))
|
||||||
|
visitors ~= new AliasSyntaxCheck!ASTBase(fileName);
|
||||||
|
|
||||||
if (moduleName.shouldRunDmd!(ExplicitlyAnnotatedUnittestCheck!ASTBase)(config))
|
if (moduleName.shouldRunDmd!(ExplicitlyAnnotatedUnittestCheck!ASTBase)(config))
|
||||||
visitors ~= new ExplicitlyAnnotatedUnittestCheck!ASTBase(fileName);
|
visitors ~= new ExplicitlyAnnotatedUnittestCheck!ASTBase(fileName);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue