From 5a1352c44ba6c931afc6a55a8ccf289fd2e58eef Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Wed, 6 Apr 2022 10:35:53 +0200 Subject: [PATCH] Don't use cast with C++ linkage --- make.rb | 2 +- src/cogito/visitor.d | 9 ++++++--- tests/app.d | 4 +++- tests/cogito/functions.d | 4 ++-- tests/cogito/visitor.d | 22 ++++++++++++++++++++++ 5 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 tests/cogito/visitor.d diff --git a/make.rb b/make.rb index 3f51c1c..1c5459a 100755 --- a/make.rb +++ b/make.rb @@ -97,7 +97,7 @@ def clean build argument when 'run' build 'debug' - system 'build/cogito', 'tools/sample.d' + system 'build/cogito', '--format', 'verbose', 'tools/sample.d' when 'test' build 'unittest' system 'build/test', '-s' diff --git a/src/cogito/visitor.d b/src/cogito/visitor.d index 11d5dfc..a3987b5 100644 --- a/src/cogito/visitor.d +++ b/src/cogito/visitor.d @@ -1,5 +1,6 @@ module cogito.visitor; +import core.stdc.string; import dmd.ast_node; import dmd.astcodegen; import dmd.parsetimevisitor; @@ -495,10 +496,10 @@ extern(C++) final class CognitiveVisitor : SemanticTimeTransitiveVisitor } foreach (elseDeclaration; *declaration) { - auto elseIf = cast(AST.StaticIfDeclaration) elseDeclaration; - if (elseIf !is null) + if (strcmp(elseDeclaration.kind, "static if") == 0) { - if (elseIf.decl) + auto elseIf = cast(AST.StaticIfDeclaration) elseDeclaration; + if (elseIf.decl !is null) { increase; @@ -513,6 +514,8 @@ extern(C++) final class CognitiveVisitor : SemanticTimeTransitiveVisitor } else { + increase; + ++this.depth; elseDeclaration.accept(this); --this.depth; diff --git a/tests/app.d b/tests/app.d index c250a75..537185e 100644 --- a/tests/app.d +++ b/tests/app.d @@ -4,6 +4,7 @@ import cogito.tests.expressions; import cogito.tests.functions; import cogito.tests.meta; import cogito.tests.statements; +import cogito.tests.visitor; int main(string[] args) { @@ -13,6 +14,7 @@ int main(string[] args) cogito.tests.expressions, cogito.tests.functions, cogito.tests.meta, - cogito.tests.statements + cogito.tests.statements, + cogito.tests.visitor )(args); } diff --git a/tests/cogito/functions.d b/tests/cogito/functions.d index 5efc44a..2aba274 100644 --- a/tests/cogito/functions.d +++ b/tests/cogito/functions.d @@ -83,7 +83,7 @@ interface C } }); - assert(meter.tryMatch!((Source source) => source.inner[].front.name.toString) == "C"); + assert(meter.tryMatch!((Source source) => source.inner[].front.name) == "C"); } @("Union") @@ -95,7 +95,7 @@ union U } }); - assert(meter.tryMatch!((Source source) => source.inner[].front.name.toString) == "U"); + assert(meter.tryMatch!((Source source) => source.inner[].front.name) == "U"); } @("Constructor") diff --git a/tests/cogito/visitor.d b/tests/cogito/visitor.d new file mode 100644 index 0000000..bbef667 --- /dev/null +++ b/tests/cogito/visitor.d @@ -0,0 +1,22 @@ +module cogito.tests.visitor; + +import cogito; +import std.sumtype; + +unittest +{ + auto meter = runOnCode(q{ +struct S +{ + static if (true) + { + } + else + { + ubyte[T.sizeof] data; + } +} + }); + + assert(meter.tryMatch!((Source source) => source.score) == 2); +}