Skip to content

Commit

Permalink
Fix code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladiwostok committed Nov 7, 2023
1 parent 511db85 commit 4418921
Showing 1 changed file with 46 additions and 45 deletions.
91 changes: 46 additions & 45 deletions src/dscanner/analysis/vcall_in_ctor.d
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,41 @@
//
module dscanner.analysis.vcall_in_ctor;

import dmd.astenums : STC;
import dscanner.analysis.base;
import dscanner.utils;
import dmd.astenums : STC;

/**
* Checks virtual calls from the constructor to methods defined in the same class.
*
* When not used carefully, virtual calls from constructors can lead to a call
* in a derived instance that's not yet constructed.
*/
extern(C++) class VcallCtorChecker(AST) : BaseAnalyzerDmd
extern (C++) class VcallCtorChecker(AST) : BaseAnalyzerDmd
{
alias visit = BaseAnalyzerDmd.visit;
mixin AnalyzerInfo!"vcall_in_ctor";

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

override void visit(AST.ClassDeclaration d)
{
if (d.members)
{
foreach (s; *d.members)
{
if (s.isCtorDeclaration())
inClassCtor = true;

s.accept(this);

if (s.isCtorDeclaration())
inClassCtor = false;
}
}
}

override void visit(AST.CallExp e)
Expand All @@ -44,37 +48,35 @@ extern(C++) class VcallCtorChecker(AST) : BaseAnalyzerDmd
}

bool inClassCtor;
private enum MSG = "a virtual call inside a constructor may lead to"
~ " unexpected results in the derived classes";
private enum MSG = "a virtual call inside a constructor may lead to unexpected results in the derived classes";
private enum string KEY = "dscanner.vcall_ctor";
}

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

StaticAnalysisConfig sac = disabledConfig();
sac.vcall_in_ctor = Check.enabled;

enum MSG = "a virtual call inside a constructor may lead to"
~ " unexpected results in the derived classes";
enum MSG = "a virtual call inside a constructor may lead to unexpected results in the derived classes";

// fails
assertAnalyzerWarnings(q{
assertAnalyzerWarningsDMD(q{
class Bar
{
this(){foo();} // [warn]: %s
this() { foo(); } // [warn]: %s
private:
public
void foo(){}
void foo() {}

}
}c.format(MSG), sac, true);

assertAnalyzerWarnings(q{
assertAnalyzerWarningsDMD(q{
class Bar
{
this()
Expand All @@ -84,74 +86,74 @@ unittest
bar();
}
private: void bar();
public{void foo(){}}
public { void foo() {} }
}
}c.format(MSG,MSG), sac, true);
}c.format(MSG, MSG), sac, true);

// passes
assertAnalyzerWarnings(q{
assertAnalyzerWarningsDMD(q{
class Bar
{
this(){foo();}
private void foo(){}
this() { foo(); }
private void foo() {}
}
}, sac, true);
}c, sac, true);

assertAnalyzerWarnings(q{
assertAnalyzerWarningsDMD(q{
class Bar
{
this(){foo();}
private {void foo(){}}
this() { foo(); }
private { void foo() {} }
}
}, sac, true);
}c, sac, true);

assertAnalyzerWarnings(q{
assertAnalyzerWarningsDMD(q{
interface I
{
final void foo() {}
final void foo() {}
}

class C : I
{
this() {foo();}
this() { foo(); }
}
}, sac, true);
}c, sac, true);

assertAnalyzerWarnings(q{
assertAnalyzerWarningsDMD(q{
final class Bar
{
public:
this(){foo();}
void foo(){}
this() { foo(); }
void foo() {}
}
}, sac, true);
}c, sac, true);

assertAnalyzerWarnings(q{
assertAnalyzerWarningsDMD(q{
class Bar
{
public:
this(){foo!int();}
void foo(T)(){}
this() { foo!int(); }
void foo(T)() {}
}
}, sac, true);
}c, sac, true);

assertAnalyzerWarnings(q{
assertAnalyzerWarningsDMD(q{
class Foo
{
static void nonVirtual();
this(){nonVirtual();}
this() { nonVirtual(); }
}
}, sac, true);
}c, sac, true);

assertAnalyzerWarnings(q{
assertAnalyzerWarningsDMD(q{
class Foo
{
package void nonVirtual();
this(){nonVirtual();}
this() { nonVirtual(); }
}
}, sac, true);
}c, sac, true);

assertAnalyzerWarnings(q{
assertAnalyzerWarningsDMD(q{
class C {
static struct S {
public:
Expand All @@ -161,8 +163,7 @@ unittest
void foo() {}
}
}
}, sac, true);
}c, sac, true);

import std.stdio: writeln;
writeln("Unittest for VcallCtorChecker passed");
}
stderr.writeln("Unittest for VcallCtorChecker passed");
}

0 comments on commit 4418921

Please sign in to comment.