Skip to content

Commit 3be9d35

Browse files
authored
Fix #14863 (tokenizer: add token flag when braces are inserted during simplifications and publish in dumpfile) (#8664)
1 parent 93bc251 commit 3be9d35

4 files changed

Lines changed: 36 additions & 10 deletions

File tree

lib/checkother.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,7 @@ void CheckOtherImpl::checkVariableScope()
12891289
tok = tok->link();
12901290

12911291
// parse else if blocks..
1292-
} else if (Token::simpleMatch(tok, "else { if (") && tok->next()->isSimplifiedScope() && Token::simpleMatch(tok->linkAt(3), ") {")) {
1292+
} else if (Token::simpleMatch(tok, "else { if (") && tok->next()->isInsertedBrace() && Token::simpleMatch(tok->linkAt(3), ") {")) {
12931293
tok = tok->next();
12941294
} else if (tok->varId() == var->declarationId() || tok->str() == "goto") {
12951295
reduce = false;
@@ -1415,7 +1415,7 @@ bool CheckOtherImpl::checkInnerScope(const Token *tok, const Variable* var, bool
14151415
if (scope->type == ScopeType::eSwitch)
14161416
return false; // Used in outer switch scope - unsafe or impossible to reduce scope
14171417

1418-
if (scope->bodyStart && scope->bodyStart->isSimplifiedScope())
1418+
if (scope->bodyStart && scope->bodyStart->isSimplifiedIfInitStmt())
14191419
return false; // simplified if/for/switch init statement
14201420
}
14211421
if (var->isArrayOrPointer()) {

lib/token.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -732,11 +732,11 @@ class CPPCHECKLIB Token {
732732
setFlag(fIsTemplate, b);
733733
}
734734

735-
bool isSimplifiedScope() const {
736-
return getFlag(fIsSimplifedScope);
735+
bool isSimplifiedIfInitStmt() const {
736+
return getFlag(fIsSimplifiedIfInitStmt);
737737
}
738-
void isSimplifiedScope(bool b) {
739-
setFlag(fIsSimplifedScope, b);
738+
void isSimplifiedIfInitStmt(bool b) {
739+
setFlag(fIsSimplifiedIfInitStmt, b);
740740
}
741741

742742
bool isFinalType() const {
@@ -767,6 +767,14 @@ class CPPCHECKLIB Token {
767767
setFlag(fIsAnonymous, b);
768768
}
769769

770+
bool isInsertedBrace() const {
771+
return getFlag(fIsInsertedBrace);
772+
}
773+
Token* isInsertedBrace(bool b) {
774+
setFlag(fIsInsertedBrace, b);
775+
return this;
776+
}
777+
770778
// cppcheck-suppress unusedFunction
771779
bool isBitfield() const {
772780
return mImpl->mBits >= 0;
@@ -1498,7 +1506,7 @@ class CPPCHECKLIB Token {
14981506
fIsImplicitInt = (1ULL << 33), // Is "int" token implicitly added?
14991507
fIsInline = (1ULL << 34), // Is this a inline type
15001508
fIsTemplate = (1ULL << 35),
1501-
fIsSimplifedScope = (1ULL << 36), // scope added when simplifying e.g. if (int i = ...; ...)
1509+
fIsSimplifiedIfInitStmt = (1ULL << 36), // simplified if/switch/while init statement e.g. if (int i = ...; ...) => { int i = ...; if (..) .. }
15021510
fIsRemovedVoidParameter = (1ULL << 37), // A void function parameter has been removed
15031511
fIsIncompleteConstant = (1ULL << 38),
15041512
fIsRestrict = (1ULL << 39), // Is this a restrict pointer type
@@ -1508,6 +1516,7 @@ class CPPCHECKLIB Token {
15081516
fIsInitComma = (1ULL << 43), // Is this comma located inside some {..}. i.e: {1,2,3,4}
15091517
fIsInitBracket = (1ULL << 44), // Is this bracket used as a part of variable initialization i.e: int a{5}, b(2);
15101518
fIsAnonymous = (1ULL << 45), // Is this a token added for an unnamed member
1519+
fIsInsertedBrace = (1ULL << 46), // brace added when simplifying e.g. if (x) f(); => if (x) { f(); }
15111520
};
15121521

15131522
enum : std::uint8_t {

lib/tokenize.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6218,6 +6218,8 @@ void Tokenizer::dump(std::ostream &out) const
62186218
}
62196219
if (tok->isRemovedVoidParameter())
62206220
outs += " isRemovedVoidParameter=\"true\"";
6221+
if (tok->isInsertedBrace())
6222+
outs += " isInsertedBrace=\"true\"";
62216223
if (tok->isSplittedVarDeclComma())
62226224
outs += " isSplittedVarDeclComma=\"true\"";
62236225
if (tok->isSplittedVarDeclEq())
@@ -7004,10 +7006,12 @@ Token *Tokenizer::simplifyAddBracesPair(Token *tok, bool commandWithCondition)
70047006
tokAfterCondition->previous()->insertToken("{");
70057007
Token * tokOpenBrace=tokAfterCondition->previous();
70067008
tokOpenBrace->column(tokAfterCondition->column());
7009+
tokOpenBrace->isInsertedBrace(true);
70077010

70087011
tokEnd->insertToken("}");
70097012
Token * tokCloseBrace=tokEnd->next();
70107013
tokCloseBrace->column(tokEnd->column());
7014+
tokCloseBrace->isInsertedBrace(true);
70117015

70127016
Token::createMutualLinks(tokOpenBrace,tokCloseBrace);
70137017
tokBracesEnd=tokCloseBrace;
@@ -8038,8 +8042,8 @@ void Tokenizer::elseif()
80388042

80398043
if (Token::Match(tok2, "}|;")) {
80408044
if (tok2->next() && tok2->strAt(1) != "else") {
8041-
tok->insertToken("{")->isSimplifiedScope(true);
8042-
tok2->insertToken("}")->isSimplifiedScope(true);
8045+
tok->insertToken("{")->isInsertedBrace(true);
8046+
tok2->insertToken("}")->isInsertedBrace(true);
80438047
Token::createMutualLinks(tok->next(), tok2->next());
80448048
break;
80458049
}
@@ -8105,7 +8109,8 @@ void Tokenizer::simplifyIfSwitchForInit()
81058109
tok->str("{");
81068110
endscope->insertToken("}");
81078111
Token::createMutualLinks(tok, endscope->next());
8108-
tok->isSimplifiedScope(true);
8112+
tok->isInsertedBrace(true);
8113+
tok->isSimplifiedIfInitStmt(true);
81098114
}
81108115
}
81118116

test/testtokenize.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ class TestTokenizer : public TestFixture {
140140

141141
TEST_CASE(whileAddBraces);
142142
TEST_CASE(whileAddBracesLabels);
143+
TEST_CASE(whileAddBracesDump);
143144

144145
TEST_CASE(doWhileAddBraces);
145146
TEST_CASE(doWhileAddBracesLabels);
@@ -1532,6 +1533,17 @@ class TestTokenizer : public TestFixture {
15321533
ASSERT_EQUALS("", filter_valueflow(errout_str()));
15331534
}
15341535

1536+
void whileAddBracesDump() {
1537+
const char code[] = "void f(){while(a);}";
1538+
SimpleTokenizer tokenizer(settingsDefault, *this, false);
1539+
ASSERT(tokenizer.tokenize(code));
1540+
ASSERT(Token::simpleMatch(tokenizer.tokens(), "void f ( ) { while ( a ) { ; } }"));
1541+
std::ostringstream ostr;
1542+
tokenizer.dump(ostr);
1543+
const std::string dump = ostr.str();
1544+
ASSERT(dump.find("isInsertedBrace=\"true\"") != std::string::npos);
1545+
}
1546+
15351547
void doWhileAddBraces() {
15361548
{
15371549
const char code[] = "{do ; while (0);}";

0 commit comments

Comments
 (0)