Fix #11770 cppcheckError for unknown macro after else (#5163)

This commit is contained in:
chrchr-github 2023-06-17 18:39:23 +02:00 committed by GitHub
parent 8f6b0b41e7
commit 082331c210
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 7 deletions

View File

@ -7444,9 +7444,18 @@ Token * Tokenizer::initVar(Token * tok)
void Tokenizer::elseif() void Tokenizer::elseif()
{ {
for (Token *tok = list.front(); tok; tok = tok->next()) { for (Token *tok = list.front(); tok; tok = tok->next()) {
if (!Token::simpleMatch(tok, "else if")) if (tok->str() != "else")
continue; continue;
if (!Token::Match(tok->previous(), ";|}"))
syntaxError(tok->previous());
if (!Token::Match(tok->next(), "%name%"))
continue;
if (tok->strAt(1) != "if")
unknownMacroError(tok->next());
for (Token *tok2 = tok; tok2; tok2 = tok2->next()) { for (Token *tok2 = tok; tok2; tok2 = tok2->next()) {
if (Token::Match(tok2, "(|{|[")) if (Token::Match(tok2, "(|{|["))
tok2 = tok2->link(); tok2 = tok2->link();
@ -10258,8 +10267,15 @@ void Tokenizer::simplifyNamespaceAliases()
Token * tokNameStart = tok->tokAt(3); Token * tokNameStart = tok->tokAt(3);
Token * tokNameEnd = tokNameStart; Token * tokNameEnd = tokNameStart;
while (tokNameEnd && tokNameEnd->next() && tokNameEnd->next()->str() != ";") while (tokNameEnd && tokNameEnd->next() && tokNameEnd->next()->str() != ";") {
if (tokNameEnd->str() == "(") {
if (tokNameEnd->previous()->isName())
unknownMacroError(tokNameEnd->previous());
else
syntaxError(tokNameEnd);
}
tokNameEnd = tokNameEnd->next(); tokNameEnd = tokNameEnd->next();
}
if (!tokNameEnd) if (!tokNameEnd)
return; // syntax error return; // syntax error

View File

@ -548,11 +548,12 @@ private:
void garbageCode23() { void garbageCode23() {
//garbage code : don't crash (#3481) //garbage code : don't crash (#3481)
checkCode("{\n" ASSERT_THROW_EQUALS(checkCode("{\n"
" if (1) = x\n" " if (1) = x\n"
" else abort s[2]\n" " else abort s[2]\n"
"}"); "}"),
ASSERT_EQUALS("", errout.str()); InternalError,
"syntax error");
} }
void garbageCode24() { void garbageCode24() {

View File

@ -6938,6 +6938,23 @@ private:
InternalError, InternalError,
"There is an unknown macro here somewhere. Configuration is required. If MACRO is a macro then please configure it."); "There is an unknown macro here somewhere. Configuration is required. If MACRO is a macro then please configure it.");
ASSERT_THROW_EQUALS(tokenizeAndStringify("void f(int i) {\n" // #11770
" if (i == 0) {}\n"
" else if (i == 1) {}\n"
" else\n"
" MACRO(i)\n"
"}\n"
"void g() {}\n"),
InternalError,
"There is an unknown macro here somewhere. Configuration is required. If MACRO is a macro then please configure it.");
ASSERT_NO_THROW(tokenizeAndStringify("void f(int i) {\n"
" if (i == 0) {}\n"
" else if (i == 1) {}\n"
" else\n"
" MACRO(i);\n"
"}\n"
"void g() {}\n"));
ASSERT_THROW_EQUALS(tokenizeAndStringify("class C : public QObject {\n" // #11770 ASSERT_THROW_EQUALS(tokenizeAndStringify("class C : public QObject {\n" // #11770
" struct S { static void g() {} };\n" " struct S { static void g() {} };\n"
"private Q_SLOTS:\n" "private Q_SLOTS:\n"
@ -6952,6 +6969,15 @@ private:
"};\n"), "};\n"),
InternalError, InternalError,
"There is an unknown macro here somewhere. Configuration is required. If slots is a macro then please configure it."); "There is an unknown macro here somewhere. Configuration is required. If slots is a macro then please configure it.");
ASSERT_THROW_EQUALS(tokenizeAndStringify("namespace U_ICU_ENTRY_POINT_RENAME(icu) { }\n"
"namespace icu = U_ICU_ENTRY_POINT_RENAME(icu);\n"
"namespace U_ICU_ENTRY_POINT_RENAME(icu) {\n"
" class BreakIterator;\n"
"}\n"
"typedef int UStringCaseMapper(icu::BreakIterator* iter);\n"),
InternalError,
"There is an unknown macro here somewhere. Configuration is required. If U_ICU_ENTRY_POINT_RENAME is a macro then please configure it.");
} }