Fixed #8263 (check-library incorrectly reports missing configuration for case when value is in parentheses)

This commit is contained in:
Daniel Marjamäki 2017-11-17 22:10:39 +01:00
parent cf05b722a9
commit b57dd4359b
4 changed files with 28 additions and 23 deletions

View File

@ -427,10 +427,10 @@ void CheckFunctions::checkLibraryMatchFunctions()
else if (New) else if (New)
continue; continue;
if (!Token::Match(tok, "%name% (") || Token::Match(tok, "for|if|while|switch|sizeof|catch|asm|return")) if (!Token::Match(tok, "%name% (") || Token::Match(tok, "asm|sizeof|catch"))
continue; continue;
if (tok->varId() != 0 || tok->type() || tok->isStandardType()) if (tok->varId() != 0 || tok->type() || tok->isStandardType() || tok->isControlFlowKeyword())
continue; continue;
if (tok->linkAt(1)->strAt(1) == "(") if (tok->linkAt(1)->strAt(1) == "(")

View File

@ -65,8 +65,23 @@ Token::~Token()
delete _values; delete _values;
} }
static const std::set<std::string> controlFlowKeywords = make_container< std::set<std::string> > () <<
"goto" <<
"do" <<
"if" <<
"else" <<
"for" <<
"while" <<
"switch" <<
"case" <<
"break" <<
"continue" <<
"return";
void Token::update_property_info() void Token::update_property_info()
{ {
setFlag(fIsControlFlowKeyword, controlFlowKeywords.find(_str) != controlFlowKeywords.end());
if (!_str.empty()) { if (!_str.empty()) {
if (_str == "true" || _str == "false") if (_str == "true" || _str == "false")
tokType(eBoolean); tokType(eBoolean);

View File

@ -401,6 +401,9 @@ public:
void isAttributePacked(bool value) { void isAttributePacked(bool value) {
setFlag(fIsAttributePacked, value); setFlag(fIsAttributePacked, value);
} }
bool isControlFlowKeyword() const {
return getFlag(fIsControlFlowKeyword);
}
bool isOperatorKeyword() const { bool isOperatorKeyword() const {
return getFlag(fIsOperatorKeyword); return getFlag(fIsOperatorKeyword);
} }
@ -889,12 +892,12 @@ private:
fIsAttributeNothrow = (1 << 13), // __attribute__((nothrow)), __declspec(nothrow) fIsAttributeNothrow = (1 << 13), // __attribute__((nothrow)), __declspec(nothrow)
fIsAttributeUsed = (1 << 14), // __attribute__((used)) fIsAttributeUsed = (1 << 14), // __attribute__((used))
fIsAttributePacked = (1 << 15), // __attribute__((packed)) fIsAttributePacked = (1 << 15), // __attribute__((packed))
fIsOperatorKeyword = (1 << 16), // operator=, etc fIsControlFlowKeyword = (1 << 16), // if/switch/while/...
fIsComplex = (1 << 17), // complex/_Complex type fIsOperatorKeyword = (1 << 17), // operator=, etc
fIsEnumType = (1 << 18), // enumeration type fIsComplex = (1 << 18), // complex/_Complex type
fIsEnumType = (1 << 19), // enumeration type
fIsName = (1 << 19), fIsName = (1 << 20),
fIsLiteral = (1 << 20), fIsLiteral = (1 << 21),
}; };
unsigned int _flags; unsigned int _flags;

View File

@ -8258,19 +8258,6 @@ void Tokenizer::validate() const
cppcheckError(lastTok); cppcheckError(lastTok);
} }
static const std::set<std::string> controlFlowKeywords = make_container< std::set<std::string> > () <<
"goto" <<
"do" <<
"if" <<
"else" <<
"for" <<
"while" <<
"switch" <<
"case" <<
"break" <<
"continue" <<
"return";
const Token * Tokenizer::findGarbageCode() const const Token * Tokenizer::findGarbageCode() const
{ {
for (const Token *tok = tokens(); tok; tok = tok->next()) { for (const Token *tok = tokens(); tok; tok = tok->next()) {
@ -8336,7 +8323,7 @@ const Token * Tokenizer::findGarbageCode() const
return list.back(); return list.back();
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")) 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"))
return list.back(); return list.back();
if ((list.back()->str()==")"||list.back()->str()=="}") && list.back()->previous() && controlFlowKeywords.find(list.back()->previous()->str()) != controlFlowKeywords.end()) if ((list.back()->str()==")" || list.back()->str()=="}") && list.back()->previous() && list.back()->previous()->isControlFlowKeyword())
return list.back()->previous(); return list.back()->previous();
return nullptr; return nullptr;
@ -8346,7 +8333,7 @@ const Token * Tokenizer::findGarbageCode() const
bool Tokenizer::isGarbageExpr(const Token *start, const Token *end) bool Tokenizer::isGarbageExpr(const Token *start, const Token *end)
{ {
for (const Token *tok = start; tok != end; tok = tok->next()) { for (const Token *tok = start; tok != end; tok = tok->next()) {
if (controlFlowKeywords.find(tok->str()) != controlFlowKeywords.end()) if (tok->isControlFlowKeyword())
return true; return true;
if (tok->str() == ";") if (tok->str() == ";")
return true; return true;