Skip to content

Commit

Permalink
Fix root cause of segfault
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladiwostok committed Dec 14, 2023
1 parent 3f7f128 commit 0160413
Showing 1 changed file with 33 additions and 15 deletions.
48 changes: 33 additions & 15 deletions src/dscanner/analysis/unused_label.d
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ extern (C++) class UnusedLabelCheck(AST) : BaseAnalyzerDmd

override void visit(AST.BreakStatement bs)
{
import std.stdio : writeln;

if (bs.ident)
labelUsed(bs.ident.toString());
}
Expand Down Expand Up @@ -95,13 +93,8 @@ extern (C++) class UnusedLabelCheck(AST) : BaseAnalyzerDmd

// Look for jump instructions
bool jmp;

if (as.tokens[0].ident)
{
auto asmInstr = as.tokens[0].ident.toString();
if (asmInstr.ptr !is null && asmInstr[0] == 'j')
jmp = true;
}
if (getFirstLetterOf(cast(char*) as.tokens[0].ptr) == 'j')
jmp = true;

// Last argument of the jmp instruction will be the label
Token* label;
Expand All @@ -113,6 +106,20 @@ extern (C++) class UnusedLabelCheck(AST) : BaseAnalyzerDmd
labelUsed(label.ident.toString());
}

private char getFirstLetterOf(char* str)
{
import std.ascii : isAlpha;

if (str is null)
return '\0';

for (; str && !isAlpha(*str); str++)
{
}

return *str;
}

private:

static struct Label
Expand Down Expand Up @@ -166,13 +173,13 @@ private:

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

StaticAnalysisConfig sac = disabledConfig();
sac.unused_label_check = Check.enabled;
assertAnalyzerWarnings(q{
assertAnalyzerWarningsDMD(q{
int testUnusedLabel()
{
int x = 0;
Expand Down Expand Up @@ -211,15 +218,15 @@ unittest
}
}c, sac);

assertAnalyzerWarnings(q{
assertAnalyzerWarningsDMD(q{
void testAsm()
{
asm { jmp lbl;}
lbl:
}
}c, sac);

assertAnalyzerWarnings(q{
assertAnalyzerWarningsDMD(q{
void testAsm()
{
asm { mov RAX,1;}
Expand All @@ -228,7 +235,7 @@ unittest
}c, sac);

// from std.math
assertAnalyzerWarnings(q{
assertAnalyzerWarningsDMD(q{
real polyImpl() {
asm {
jecxz return_ST;
Expand All @@ -237,7 +244,7 @@ unittest
}c, sac);

// a label might be hard to find, e.g. in a mixin
assertAnalyzerWarnings(q{
assertAnalyzerWarningsDMD(q{
real polyImpl() {
mixin("return_ST: return 1;");
asm {
Expand All @@ -246,5 +253,16 @@ unittest
}
}c, sac);

assertAnalyzerWarningsDMD(q{
void testAsm()
{
asm nothrow @nogc
{
"movgr2fcsr $r0,%0" :
: "r" (newState & (roundingMask | allExceptions));
}
}
}c, sac);

stderr.writeln("Unittest for UnusedLabelCheck passed.");
}

0 comments on commit 0160413

Please sign in to comment.