Skip to content

Commit b271c9b

Browse files
committed
fix
1 parent 7fc09d8 commit b271c9b

4 files changed

Lines changed: 25 additions & 15 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 isSimplifedIfInitStmt(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 (x) f(); => if (x) { f(); }
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 & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6218,8 +6218,8 @@ void Tokenizer::dump(std::ostream &out) const
62186218
}
62196219
if (tok->isRemovedVoidParameter())
62206220
outs += " isRemovedVoidParameter=\"true\"";
6221-
if (tok->isSimplifiedScope())
6222-
outs += " isSimplifiedScope=\"true\"";
6221+
if (tok->isInsertedBrace())
6222+
outs += " isInsertedBrace=\"true\"";
62236223
if (tok->isSplittedVarDeclComma())
62246224
outs += " isSplittedVarDeclComma=\"true\"";
62256225
if (tok->isSplittedVarDeclEq())
@@ -7006,12 +7006,12 @@ Token *Tokenizer::simplifyAddBracesPair(Token *tok, bool commandWithCondition)
70067006
tokAfterCondition->previous()->insertToken("{");
70077007
Token * tokOpenBrace=tokAfterCondition->previous();
70087008
tokOpenBrace->column(tokAfterCondition->column());
7009-
tokOpenBrace->isSimplifiedScope(true);
7009+
tokOpenBrace->isInsertedBrace(true);
70107010

70117011
tokEnd->insertToken("}");
70127012
Token * tokCloseBrace=tokEnd->next();
70137013
tokCloseBrace->column(tokEnd->column());
7014-
tokCloseBrace->isSimplifiedScope(true);
7014+
tokCloseBrace->isInsertedBrace(true);
70157015

70167016
Token::createMutualLinks(tokOpenBrace,tokCloseBrace);
70177017
tokBracesEnd=tokCloseBrace;
@@ -8042,8 +8042,8 @@ void Tokenizer::elseif()
80428042

80438043
if (Token::Match(tok2, "}|;")) {
80448044
if (tok2->next() && tok2->strAt(1) != "else") {
8045-
tok->insertToken("{")->isSimplifiedScope(true);
8046-
tok2->insertToken("}")->isSimplifiedScope(true);
8045+
tok->insertToken("{")->isInsertedBrace(true);
8046+
tok2->insertToken("}")->isInsertedBrace(true);
80478047
Token::createMutualLinks(tok->next(), tok2->next());
80488048
break;
80498049
}
@@ -8109,7 +8109,8 @@ void Tokenizer::simplifyIfSwitchForInit()
81098109
tok->str("{");
81108110
endscope->insertToken("}");
81118111
Token::createMutualLinks(tok, endscope->next());
8112-
tok->isSimplifiedScope(true);
8112+
tok->isInsertedBrace(true);
8113+
tok->isSimplifedIfInitStmt(true);
81138114
}
81148115
}
81158116

test/testtokenize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1541,7 +1541,7 @@ class TestTokenizer : public TestFixture {
15411541
std::ostringstream ostr;
15421542
tokenizer.dump(ostr);
15431543
const std::string dump = ostr.str();
1544-
ASSERT(dump.find("isSimplifiedScope=\"true\"") != std::string::npos);
1544+
ASSERT(dump.find("isInsertedBrace=\"true\"") != std::string::npos);
15451545
}
15461546

15471547
void doWhileAddBraces() {

0 commit comments

Comments
 (0)