Fixed #9354 (Unknown macro is not reported and then Cppcheck is silent about issues)

This commit is contained in:
Daniel Marjamäki 2019-09-13 13:05:26 +02:00
parent 246ba265ff
commit 742c437953
5 changed files with 12 additions and 62 deletions

View File

@ -5037,6 +5037,9 @@ void Tokenizer::removeMacrosInGlobalScope()
if (tok2 && tok2->str() == "(") if (tok2 && tok2->str() == "(")
tok2 = tok2->link()->next(); tok2 = tok2->link()->next();
if (Token::Match(tok, "%name% (") && Token::Match(tok2, "%name% *|&|::|<| %name%") && !Token::Match(tok2, "namespace|class|struct|union"))
unknownMacroError(tok);
if (Token::Match(tok, "%type% (") && Token::Match(tok2, "%type% (") && !Token::Match(tok2, "noexcept|throw") && isFunctionHead(tok2->next(), ":;{")) if (Token::Match(tok, "%type% (") && Token::Match(tok2, "%type% (") && !Token::Match(tok2, "noexcept|throw") && isFunctionHead(tok2->next(), ":;{"))
unknownMacroError(tok); unknownMacroError(tok);

View File

@ -75,7 +75,6 @@ private:
TEST_CASE(noConstructor7); // ticket #4391 TEST_CASE(noConstructor7); // ticket #4391
TEST_CASE(noConstructor8); // ticket #4404 TEST_CASE(noConstructor8); // ticket #4404
TEST_CASE(noConstructor9); // ticket #4419 TEST_CASE(noConstructor9); // ticket #4419
TEST_CASE(noConstructor10); // ticket #6614
TEST_CASE(noConstructor11); // ticket #3552 TEST_CASE(noConstructor11); // ticket #3552
TEST_CASE(noConstructor12); // #8951 - member initialization TEST_CASE(noConstructor12); // #8951 - member initialization
@ -554,26 +553,6 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void noConstructor10() {
// ticket #6614
check("class A : public wxDialog\n"
"{\n"
"private:\n"
" DECLARE_EVENT_TABLE()\n"
"public:\n"
" A(wxWindow *parent,\n"
" wxWindowID id = 1,\n"
" const wxString &title = wxT(""),\n"
" const wxPoint& pos = wxDefaultPosition,\n"
" const wxSize& size = wxDefaultSize,\n"
" long style = wxDIALOG_NO_PARENT | wxMINIMIZE_BOX | wxMAXIMIZE_BOX | wxCLOSE_BOX);\n"
" virtual ~A();\n"
"private:\n"
" wxTimer *WxTimer1;\n"
"};\n");
ASSERT_EQUALS("", errout.str());
}
void noConstructor11() { // #3552 void noConstructor11() { // #3552
check("class Fred { int x; };\n" check("class Fred { int x; };\n"
"union U { int y; Fred fred; };"); "union U { int y; Fred fred; };");

View File

@ -6304,7 +6304,6 @@ private:
ASSERT_EQUALS("signed int *", typeOf("; auto data = new (nothrow) int[100];", "data")); ASSERT_EQUALS("signed int *", typeOf("; auto data = new (nothrow) int[100];", "data"));
ASSERT_EQUALS("signed int *", typeOf("; auto data = new (std::nothrow) int[100];", "data")); ASSERT_EQUALS("signed int *", typeOf("; auto data = new (std::nothrow) int[100];", "data"));
ASSERT_EQUALS("const signed short", typeOf("short values[10]; void f() { for (const auto *x : values); }", "x")); ASSERT_EQUALS("const signed short", typeOf("short values[10]; void f() { for (const auto *x : values); }", "x"));
ASSERT_EQUALS("signed int *", typeOf("MACRO(test) void test() { auto x = (int*)y; }", "x")); // #7931 (garbage?)
ASSERT_EQUALS("const signed int", typeOf("; const auto x = 3;", "x")); ASSERT_EQUALS("const signed int", typeOf("; const auto x = 3;", "x"));
// Variable declaration // Variable declaration

View File

@ -152,7 +152,6 @@ private:
TEST_CASE(simplifyKnownVariables9); TEST_CASE(simplifyKnownVariables9);
TEST_CASE(simplifyKnownVariables10); TEST_CASE(simplifyKnownVariables10);
TEST_CASE(simplifyKnownVariables11); TEST_CASE(simplifyKnownVariables11);
TEST_CASE(simplifyKnownVariables12);
TEST_CASE(simplifyKnownVariables13); TEST_CASE(simplifyKnownVariables13);
TEST_CASE(simplifyKnownVariables14); TEST_CASE(simplifyKnownVariables14);
TEST_CASE(simplifyKnownVariables15); TEST_CASE(simplifyKnownVariables15);
@ -1715,15 +1714,6 @@ private:
simplifyKnownVariables(code)); simplifyKnownVariables(code));
} }
void simplifyKnownVariables12() {
const char code[] = "ENTER_NAMESPACE(project_namespace)\n"
"const double pi = 3.14;\n"
"int main(){}\n";
ASSERT_EQUALS(
"ENTER_NAMESPACE ( project_namespace ) const double pi = 3.14 ; int main ( ) { }",
simplifyKnownVariables(code));
}
void simplifyKnownVariables13() { void simplifyKnownVariables13() {
const char code[] = "void f()\n" const char code[] = "void f()\n"
"{\n" "{\n"
@ -3151,19 +3141,6 @@ private:
"}"; "}";
ASSERT_EQUALS("void foo ( ) { if ( x ) { } { } }", tokenizeAndStringify(code, true)); ASSERT_EQUALS("void foo ( ) { if ( x ) { } { } }", tokenizeAndStringify(code, true));
} }
// #3770 - Don't segfault and don't change macro argument as if it's a K&R function argument
{
const char code[] = "MACRO(a)"
""
"void f()"
"{"
" SetLanguage();"
" {"
" }"
"}";
ASSERT_EQUALS("MACRO ( a ) void f ( ) { SetLanguage ( ) ; { } }", tokenizeAndStringify(code));
}
} }
void simplifyFunctionParameters1() { // ticket #3721 void simplifyFunctionParameters1() { // ticket #3721
@ -6424,8 +6401,7 @@ private:
ASSERT_EQUALS("namespace { }", tokenizeAndStringify("ABA() namespace { }")); ASSERT_EQUALS("namespace { }", tokenizeAndStringify("ABA() namespace { }"));
// #3750 // #3750
ASSERT_EQUALS("; foo :: foo ( ) { }", ASSERT_THROW(tokenizeAndStringify("; AB(foo*) foo::foo() { }"), InternalError);
tokenizeAndStringify("; AB(foo*) foo::foo() { }"));
// #4834 - syntax error // #4834 - syntax error
ASSERT_THROW(tokenizeAndStringify("A(B) foo() {}"), InternalError); ASSERT_THROW(tokenizeAndStringify("A(B) foo() {}"), InternalError);
@ -6435,6 +6411,14 @@ private:
tokenizeAndStringify("; AB class foo { }")); tokenizeAndStringify("; AB class foo { }"));
ASSERT_EQUALS("; CONST struct ABC abc ;", ASSERT_EQUALS("; CONST struct ABC abc ;",
tokenizeAndStringify("; CONST struct ABC abc ;")); tokenizeAndStringify("; CONST struct ABC abc ;"));
ASSERT_THROW(tokenizeAndStringify("class A {\n"
" UNKNOWN_MACRO(A)\n"
"private:\n"
" int x;\n"
"};"), InternalError);
ASSERT_THROW(tokenizeAndStringify("MACRO(test) void test() { }"), InternalError); // #7931
} }
void removeMacroInVarDecl() { // #4304 void removeMacroInVarDecl() { // #4304

View File

@ -115,7 +115,6 @@ private:
TEST_CASE(varid_in_class5); // #3584 - std::vector<::FOO::B> b; TEST_CASE(varid_in_class5); // #3584 - std::vector<::FOO::B> b;
TEST_CASE(varid_in_class6); // #3755 TEST_CASE(varid_in_class6); // #3755
TEST_CASE(varid_in_class7); // set variable id for struct members TEST_CASE(varid_in_class7); // set variable id for struct members
TEST_CASE(varid_in_class8); // unknown macro in class
TEST_CASE(varid_in_class9); // #4291 - id for variables accessed through 'this' TEST_CASE(varid_in_class9); // #4291 - id for variables accessed through 'this'
TEST_CASE(varid_in_class10); TEST_CASE(varid_in_class10);
TEST_CASE(varid_in_class11); // #4277 - anonymous union TEST_CASE(varid_in_class11); // #4277 - anonymous union
@ -1508,20 +1507,6 @@ private:
tokenize(code)); tokenize(code));
} }
void varid_in_class8() { // #3776 - unknown macro
const char code[] = "class A {\n"
" UNKNOWN_MACRO(A)\n"
"private:\n"
" int x;\n"
"};";
ASSERT_EQUALS("1: class A {\n"
"2: UNKNOWN_MACRO ( A )\n"
"3: private:\n"
"4: int x@1 ;\n"
"5: } ;\n",
tokenize(code));
}
void varid_in_class9() { // #4291 - id for variables accessed through 'this' void varid_in_class9() { // #4291 - id for variables accessed through 'this'
const char code1[] = "class A {\n" const char code1[] = "class A {\n"
" int var;\n" " int var;\n"