-
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
Replace libdparse with DMD in IfConstraintsIndentCheck #128
Merged
RazvanN7
merged 4 commits into
Dlang-UPB:replace_libdparse
from
Vladiwostok:if-constraints
Nov 8, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,194 +4,132 @@ | |
|
||
module dscanner.analysis.if_constraints_indent; | ||
|
||
import dparse.lexer; | ||
import dparse.ast; | ||
import dscanner.analysis.base; | ||
import dsymbol.scope_ : Scope; | ||
|
||
import std.algorithm.iteration : filter; | ||
import std.range; | ||
import dmd.tokens : Token, TOK; | ||
|
||
/** | ||
Checks whether all if constraints have the same indention as their declaration. | ||
*/ | ||
final class IfConstraintsIndentCheck : BaseAnalyzer | ||
extern (C++) class IfConstraintsIndentCheck(AST) : BaseAnalyzerDmd | ||
{ | ||
alias visit = BaseAnalyzerDmd.visit; | ||
mixin AnalyzerInfo!"if_constraints_indent"; | ||
|
||
/// | ||
this(BaseAnalyzerArguments args) | ||
{ | ||
super(args); | ||
private enum string KEY = "dscanner.style.if_constraints_indent"; | ||
private enum string MSG = "If constraints should have the same indentation as the function"; | ||
|
||
// convert tokens to a list of token starting positions per line | ||
private Token[] tokens; | ||
|
||
// libdparse columns start at 1 | ||
foreach (t; tokens) | ||
{ | ||
// pad empty positions if we skip empty token-less lines | ||
// t.line (unsigned) may be 0 if the token is uninitialized/broken, so don't subtract from it | ||
// equivalent to: firstSymbolAtLine.length < t.line - 1 | ||
while (firstSymbolAtLine.length + 1 < t.line) | ||
firstSymbolAtLine ~= Pos(1, t.index); | ||
|
||
// insert a new line with positions if new line is reached | ||
// (previous while pads skipped lines) | ||
if (firstSymbolAtLine.length < t.line) | ||
firstSymbolAtLine ~= Pos(t.column, t.index, t.type == tok!"if"); | ||
} | ||
} | ||
|
||
override void visit(const FunctionDeclaration decl) | ||
extern (D) this(string fileName, bool skipTests = false) | ||
{ | ||
if (decl.constraint !is null) | ||
checkConstraintSpace(decl.constraint, decl.name); | ||
super(fileName, skipTests); | ||
lexFile(); | ||
} | ||
|
||
override void visit(const InterfaceDeclaration decl) | ||
private void lexFile() | ||
{ | ||
if (decl.constraint !is null) | ||
checkConstraintSpace(decl.constraint, decl.name); | ||
} | ||
import dscanner.utils : readFile; | ||
import dmd.errorsink : ErrorSinkNull; | ||
import dmd.globals : global; | ||
import dmd.lexer : Lexer; | ||
|
||
auto bytes = readFile(fileName) ~ '\0'; | ||
|
||
override void visit(const ClassDeclaration decl) | ||
{ | ||
if (decl.constraint !is null) | ||
checkConstraintSpace(decl.constraint, decl.name); | ||
} | ||
__gshared ErrorSinkNull errorSinkNull; | ||
if (!errorSinkNull) | ||
errorSinkNull = new ErrorSinkNull; | ||
|
||
override void visit(const TemplateDeclaration decl) | ||
{ | ||
if (decl.constraint !is null) | ||
checkConstraintSpace(decl.constraint, decl.name); | ||
} | ||
|
||
override void visit(const UnionDeclaration decl) | ||
{ | ||
if (decl.constraint !is null) | ||
checkConstraintSpace(decl.constraint, decl.name); | ||
} | ||
scope lexer = new Lexer(null, cast(char*) bytes, 0, bytes.length, 0, 0, errorSinkNull, &global.compileEnv); | ||
|
||
override void visit(const StructDeclaration decl) | ||
{ | ||
if (decl.constraint !is null) | ||
checkConstraintSpace(decl.constraint, decl.name); | ||
} | ||
|
||
override void visit(const Constructor decl) | ||
{ | ||
if (decl.constraint !is null) | ||
checkConstraintSpace(decl.constraint, decl.line); | ||
} | ||
|
||
alias visit = ASTVisitor.visit; | ||
|
||
private: | ||
|
||
enum string KEY = "dscanner.style.if_constraints_indent"; | ||
enum string MESSAGE = "If constraints should have the same indentation as the function"; | ||
|
||
Pos[] firstSymbolAtLine; | ||
static struct Pos | ||
{ | ||
size_t column; | ||
size_t index; | ||
bool isIf; | ||
} | ||
|
||
/** | ||
Check indentation of constraints | ||
*/ | ||
void checkConstraintSpace(const Constraint constraint, const Token token) | ||
{ | ||
checkConstraintSpace(constraint, token.line); | ||
do | ||
{ | ||
lexer.nextToken(); | ||
tokens ~= lexer.token; | ||
} | ||
while (lexer.token.value != TOK.endOfFile); | ||
} | ||
|
||
void checkConstraintSpace(const Constraint constraint, size_t line) | ||
override void visit(AST.TemplateDeclaration templateDecl) | ||
{ | ||
import std.algorithm : min; | ||
|
||
// dscanner lines start at 1 | ||
auto pDecl = firstSymbolAtLine[line - 1]; | ||
|
||
// search for constraint if (might not be on the same line as the expression) | ||
auto r = firstSymbolAtLine[line .. constraint.expression.line].retro.filter!(s => s.isIf); | ||
|
||
auto if_ = constraint.tokens.findTokenForDisplay(tok!"if")[0]; | ||
|
||
// no hit = constraint is on the same line | ||
if (r.empty) | ||
addErrorMessage(if_, KEY, MESSAGE); | ||
else if (pDecl.column != r.front.column) | ||
addErrorMessage([min(if_.index, pDecl.index), if_.index + 2], if_.line, [min(if_.column, pDecl.column), if_.column + 2], KEY, MESSAGE); | ||
import std.array : array; | ||
import std.algorithm : filter; | ||
import std.range : front, retro; | ||
|
||
super.visit(templateDecl); | ||
|
||
if (templateDecl.constraint is null || templateDecl.members is null) | ||
return; | ||
|
||
auto firstTemplateToken = tokens.filter!(t => t.loc.linnum == templateDecl.loc.linnum) | ||
.filter!(t => t.value != TOK.whitespace) | ||
.front; | ||
uint templateLine = firstTemplateToken.loc.linnum; | ||
uint templateCol = firstTemplateToken.loc.charnum; | ||
|
||
auto constraintToken = tokens.filter!(t => t.loc.fileOffset <= templateDecl.constraint.loc.fileOffset) | ||
.array() | ||
.retro() | ||
.filter!(t => t.value == TOK.if_) | ||
.front; | ||
uint constraintLine = constraintToken.loc.linnum; | ||
uint constraintCol = constraintToken.loc.charnum; | ||
|
||
if (templateLine == constraintLine || templateCol != constraintCol) | ||
addErrorMessage(cast(ulong) constraintLine, cast(ulong) constraintCol, KEY, MSG); | ||
} | ||
} | ||
|
||
unittest | ||
{ | ||
import dscanner.analysis.config : StaticAnalysisConfig, Check, disabledConfig; | ||
import dscanner.analysis.helpers : assertAnalyzerWarnings; | ||
import dscanner.analysis.helpers : assertAnalyzerWarningsDMD; | ||
import std.format : format; | ||
import std.stdio : stderr; | ||
|
||
StaticAnalysisConfig sac = disabledConfig(); | ||
sac.if_constraints_indent = Check.enabled; | ||
enum MSG = "If constraints should have the same indentation as the function"; | ||
|
||
assertAnalyzerWarnings(q{ | ||
assertAnalyzerWarningsDMD(q{ | ||
void foo(R)(R r) | ||
if (R == null) | ||
{} | ||
|
||
// note: since we are using tabs, the ^ look a bit off here. Use 1-wide tab stops to view tests. | ||
void foo(R)(R r) | ||
if (R == null) /+ | ||
^^^ [warn]: %s +/ | ||
if (R == null) // [warn]: %s | ||
{} | ||
}c.format( | ||
IfConstraintsIndentCheck.MESSAGE, | ||
), sac); | ||
}c.format(MSG), sac); | ||
|
||
assertAnalyzerWarnings(q{ | ||
assertAnalyzerWarningsDMD(q{ | ||
void foo(R)(R r) | ||
if (R == null) | ||
{} | ||
|
||
void foo(R)(R r) | ||
if (R == null) /+ | ||
^^ [warn]: %s +/ | ||
if (R == null) // [warn]: %s | ||
{} | ||
|
||
void foo(R)(R r) | ||
if (R == null) /+ | ||
^^^ [warn]: %s +/ | ||
if (R == null) // [warn]: %s | ||
{} | ||
}c.format( | ||
IfConstraintsIndentCheck.MESSAGE, | ||
IfConstraintsIndentCheck.MESSAGE, | ||
), sac); | ||
}c.format(MSG, MSG), sac); | ||
|
||
assertAnalyzerWarnings(q{ | ||
assertAnalyzerWarningsDMD(q{ | ||
struct Foo(R) | ||
if (R == null) | ||
{} | ||
|
||
struct Foo(R) | ||
if (R == null) /+ | ||
^^ [warn]: %s +/ | ||
if (R == null) // [warn]: %s | ||
{} | ||
|
||
struct Foo(R) | ||
if (R == null) /+ | ||
^^^ [warn]: %s +/ | ||
if (R == null) // [warn]: %s | ||
{} | ||
}c.format( | ||
IfConstraintsIndentCheck.MESSAGE, | ||
IfConstraintsIndentCheck.MESSAGE, | ||
), sac); | ||
}c.format(MSG, MSG), sac); | ||
|
||
// test example from Phobos | ||
assertAnalyzerWarnings(q{ | ||
assertAnalyzerWarningsDMD(q{ | ||
Num abs(Num)(Num x) @safe pure nothrow | ||
if (is(typeof(Num.init >= 0)) && is(typeof(-Num.init)) && | ||
!(is(Num* : const(ifloat*)) || is(Num* : const(idouble*)) | ||
|
@@ -205,7 +143,7 @@ if (is(typeof(Num.init >= 0)) && is(typeof(-Num.init)) && | |
}, sac); | ||
|
||
// weird constraint formatting | ||
assertAnalyzerWarnings(q{ | ||
assertAnalyzerWarningsDMD(q{ | ||
struct Foo(R) | ||
if | ||
(R == null) | ||
|
@@ -217,8 +155,7 @@ if (is(typeof(Num.init >= 0)) && is(typeof(-Num.init)) && | |
{} | ||
|
||
struct Foo(R) | ||
if /+ | ||
^^ [warn]: %s +/ | ||
if // [warn]: %s | ||
(R == null) | ||
{} | ||
|
||
|
@@ -234,39 +171,61 @@ if /+ | |
{} | ||
|
||
struct Foo(R) | ||
if ( /+ | ||
^^^ [warn]: %s +/ | ||
if ( // [warn]: %s | ||
R == null | ||
) {} | ||
}c.format( | ||
IfConstraintsIndentCheck.MESSAGE, | ||
IfConstraintsIndentCheck.MESSAGE, | ||
), sac); | ||
}c.format(MSG, MSG), sac); | ||
|
||
// constraint on the same line | ||
assertAnalyzerWarnings(q{ | ||
struct CRC(uint N, ulong P) if (N == 32 || N == 64) /+ | ||
^^ [warn]: %s +/ | ||
assertAnalyzerWarningsDMD(q{ | ||
struct CRC(uint N, ulong P) if (N == 32 || N == 64) // [warn]: %s | ||
{} | ||
}c.format( | ||
IfConstraintsIndentCheck.MESSAGE, | ||
), sac); | ||
}c.format(MSG), sac); | ||
|
||
stderr.writeln("Unittest for IfConstraintsIndentCheck passed."); | ||
assertAnalyzerWarningsDMD(q{ | ||
private template sharedToString(alias field) | ||
if (is(typeof(field) == shared)) | ||
{ | ||
static immutable sharedToString = typeof(field).stringof; | ||
} | ||
}c, sac); | ||
|
||
@("issue #829") | ||
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. Removed as this no longer applies with current code |
||
unittest | ||
assertAnalyzerWarningsDMD(q{ | ||
private union EndianSwapper(T) | ||
if (canSwapEndianness!T) | ||
{ | ||
import dscanner.analysis.config : StaticAnalysisConfig, Check, disabledConfig; | ||
import dscanner.analysis.helpers : assertAnalyzerWarnings; | ||
import std.format : format; | ||
import std.stdio : stderr; | ||
T value; | ||
} | ||
}c, sac); | ||
|
||
StaticAnalysisConfig sac = disabledConfig(); | ||
sac.if_constraints_indent = Check.enabled; | ||
assertAnalyzerWarningsDMD(q{ | ||
void test(alias matchFn)() | ||
{ | ||
auto baz(Cap)(Cap m) | ||
if (is(Cap == Captures!(Cap.String))) | ||
{ | ||
return toUpper(m.hit); | ||
} | ||
} | ||
}c, sac); | ||
|
||
assertAnalyzerWarnings(`void foo() { | ||
f; | ||
}`, sac); | ||
assertAnalyzerWarningsDMD(q{ | ||
ElementType!(A) pop (A) (ref A a) | ||
if (isDynamicArray!(A) && !isNarrowString!(A) && isMutable!(A) && !is(A == void[])) | ||
{ | ||
auto e = a.back; | ||
a.popBack(); | ||
return e; | ||
} | ||
}c, sac); | ||
|
||
assertAnalyzerWarningsDMD(q{ | ||
template HMAC(H) | ||
if (isDigest!H && hasBlockSize!H) | ||
{ | ||
alias HMAC = HMAC!(H, H.blockSize); | ||
} | ||
}, sac); | ||
|
||
stderr.writeln("Unittest for IfConstraintsIndentCheck passed."); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 have seen this method before. Maybe it should be made public and let all visitors use it.