Skip to content

Commit 026d8f6

Browse files
IOBYTEamai2012
authored andcommitted
fix #8284: False positive: "Label 'class' is not used." for anonymous… (danmar#1011)
* fix #8284: False positive: "Label 'class' is not used." for anonymous C++ class Add support for annonymous derived structures and classes. * Fix travis build (use findsimplematch). * Fix bug in simplifyLabelsCaseDefault which was inserting ; in wrong place.
1 parent 0d4744c commit 026d8f6

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

lib/tokenize.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2178,6 +2178,7 @@ const Token * Tokenizer::startOfExecutableScope(const Token * tok)
21782178

21792179
void Tokenizer::simplifyLabelsCaseDefault()
21802180
{
2181+
const bool cpp = isCPP();
21812182
bool executablescope = false;
21822183
unsigned int indentLevel = 0;
21832184
for (Token *tok = list.front(); tok; tok = tok->next()) {
@@ -2233,8 +2234,10 @@ void Tokenizer::simplifyLabelsCaseDefault()
22332234
syntaxError(tok);
22342235
}
22352236
} else if (Token::Match(tok, "[;{}] %name% : !!;")) {
2236-
tok = tok->tokAt(2);
2237-
tok->insertToken(";");
2237+
if (!cpp || !Token::Match(tok->next(), "class|struct")) {
2238+
tok = tok->tokAt(2);
2239+
tok->insertToken(";");
2240+
}
22382241
}
22392242
}
22402243
}
@@ -8506,6 +8509,8 @@ void Tokenizer::simplifyFuncInWhile()
85068509

85078510
void Tokenizer::simplifyStructDecl()
85088511
{
8512+
const bool cpp = isCPP();
8513+
85098514
// A counter that is used when giving unique names for anonymous structs.
85108515
unsigned int count = 0;
85118516

@@ -8523,6 +8528,13 @@ void Tokenizer::simplifyStructDecl()
85238528
tok->insertToken("Anonymous" + MathLib::toString(count++));
85248529
}
85258530
}
8531+
// check for derived anonymous class/struct
8532+
else if (cpp && Token::Match(tok, "class|struct :")) {
8533+
const Token *tok1 = Token::findsimplematch(tok, "{");
8534+
if (tok1 && Token::Match(tok1->link(), "} *|&| %type% ,|;|[|(|{")) {
8535+
tok->insertToken("Anonymous" + MathLib::toString(count++));
8536+
}
8537+
}
85268538
// check for anonymous enum
85278539
else if ((Token::simpleMatch(tok, "enum {") && Token::Match(tok->next()->link(), "} %type%| ,|;|[|(|{")) ||
85288540
(Token::Match(tok, "enum : %type% {") && Token::Match(tok->linkAt(3), "} %type%| ,|;|[|(|{"))) {

test/testsimplifytokens.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3067,6 +3067,13 @@ class TestSimplifyTokens : public TestFixture {
30673067
const char expected[] = "void f ( ) { int A ; A = 1 ; int B ; B = 2 ; int C ; C = 3 ; int D ; int E ; E = 5 ; int F ; F = 6 ; }";
30683068
ASSERT_EQUALS(expected, tok(code, false));
30693069
}
3070+
3071+
// ticket #8284
3072+
{
3073+
const char code[] = "void f() { class : foo<int> { } abc; }";
3074+
const char expected[] = "void f ( ) { class Anonymous0 : foo < int > { } ; Anonymous0 abc ; }";
3075+
ASSERT_EQUALS(expected, tok(code, false));
3076+
}
30703077
}
30713078

30723079
void simplifyStructDecl2() { // ticket #2479 (segmentation fault)

0 commit comments

Comments
 (0)