Skip to content

Commit b57dd43

Browse files
committed
Fixed #8263 (check-library incorrectly reports missing configuration for case when value is in parentheses)
1 parent cf05b72 commit b57dd43

4 files changed

Lines changed: 28 additions & 23 deletions

File tree

lib/checkfunctions.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,10 +427,10 @@ void CheckFunctions::checkLibraryMatchFunctions()
427427
else if (New)
428428
continue;
429429

430-
if (!Token::Match(tok, "%name% (") || Token::Match(tok, "for|if|while|switch|sizeof|catch|asm|return"))
430+
if (!Token::Match(tok, "%name% (") || Token::Match(tok, "asm|sizeof|catch"))
431431
continue;
432432

433-
if (tok->varId() != 0 || tok->type() || tok->isStandardType())
433+
if (tok->varId() != 0 || tok->type() || tok->isStandardType() || tok->isControlFlowKeyword())
434434
continue;
435435

436436
if (tok->linkAt(1)->strAt(1) == "(")

lib/token.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,23 @@ Token::~Token()
6565
delete _values;
6666
}
6767

68+
static const std::set<std::string> controlFlowKeywords = make_container< std::set<std::string> > () <<
69+
"goto" <<
70+
"do" <<
71+
"if" <<
72+
"else" <<
73+
"for" <<
74+
"while" <<
75+
"switch" <<
76+
"case" <<
77+
"break" <<
78+
"continue" <<
79+
"return";
80+
6881
void Token::update_property_info()
6982
{
83+
setFlag(fIsControlFlowKeyword, controlFlowKeywords.find(_str) != controlFlowKeywords.end());
84+
7085
if (!_str.empty()) {
7186
if (_str == "true" || _str == "false")
7287
tokType(eBoolean);

lib/token.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,9 @@ class CPPCHECKLIB Token {
401401
void isAttributePacked(bool value) {
402402
setFlag(fIsAttributePacked, value);
403403
}
404+
bool isControlFlowKeyword() const {
405+
return getFlag(fIsControlFlowKeyword);
406+
}
404407
bool isOperatorKeyword() const {
405408
return getFlag(fIsOperatorKeyword);
406409
}
@@ -889,12 +892,12 @@ class CPPCHECKLIB Token {
889892
fIsAttributeNothrow = (1 << 13), // __attribute__((nothrow)), __declspec(nothrow)
890893
fIsAttributeUsed = (1 << 14), // __attribute__((used))
891894
fIsAttributePacked = (1 << 15), // __attribute__((packed))
892-
fIsOperatorKeyword = (1 << 16), // operator=, etc
893-
fIsComplex = (1 << 17), // complex/_Complex type
894-
fIsEnumType = (1 << 18), // enumeration type
895-
896-
fIsName = (1 << 19),
897-
fIsLiteral = (1 << 20),
895+
fIsControlFlowKeyword = (1 << 16), // if/switch/while/...
896+
fIsOperatorKeyword = (1 << 17), // operator=, etc
897+
fIsComplex = (1 << 18), // complex/_Complex type
898+
fIsEnumType = (1 << 19), // enumeration type
899+
fIsName = (1 << 20),
900+
fIsLiteral = (1 << 21),
898901
};
899902

900903
unsigned int _flags;

lib/tokenize.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8258,19 +8258,6 @@ void Tokenizer::validate() const
82588258
cppcheckError(lastTok);
82598259
}
82608260

8261-
static const std::set<std::string> controlFlowKeywords = make_container< std::set<std::string> > () <<
8262-
"goto" <<
8263-
"do" <<
8264-
"if" <<
8265-
"else" <<
8266-
"for" <<
8267-
"while" <<
8268-
"switch" <<
8269-
"case" <<
8270-
"break" <<
8271-
"continue" <<
8272-
"return";
8273-
82748261
const Token * Tokenizer::findGarbageCode() const
82758262
{
82768263
for (const Token *tok = tokens(); tok; tok = tok->next()) {
@@ -8336,7 +8323,7 @@ const Token * Tokenizer::findGarbageCode() const
83368323
return list.back();
83378324
if (Token::Match(list.back(), "void|char|short|int|long|float|double|const|volatile|static|inline|struct|class|enum|union|template|sizeof|case|break|continue|typedef"))
83388325
return list.back();
8339-
if ((list.back()->str()==")"||list.back()->str()=="}") && list.back()->previous() && controlFlowKeywords.find(list.back()->previous()->str()) != controlFlowKeywords.end())
8326+
if ((list.back()->str()==")" || list.back()->str()=="}") && list.back()->previous() && list.back()->previous()->isControlFlowKeyword())
83408327
return list.back()->previous();
83418328

83428329
return nullptr;
@@ -8346,7 +8333,7 @@ const Token * Tokenizer::findGarbageCode() const
83468333
bool Tokenizer::isGarbageExpr(const Token *start, const Token *end)
83478334
{
83488335
for (const Token *tok = start; tok != end; tok = tok->next()) {
8349-
if (controlFlowKeywords.find(tok->str()) != controlFlowKeywords.end())
8336+
if (tok->isControlFlowKeyword())
83508337
return true;
83518338
if (tok->str() == ";")
83528339
return true;

0 commit comments

Comments
 (0)