@@ -61,6 +61,8 @@ class TestCppcheck : public TestFixture {
6161 TEST_CASE (getDumpFileContentsRawTokens);
6262 TEST_CASE (getDumpFileContentsLibrary);
6363 TEST_CASE (getClangFlagsIncludeFile);
64+ TEST_CASE (invalidSyntaxUnknownMacro1); // #13378
65+ TEST_CASE (invalidSyntaxUnknownMacro2); // #13378
6466 }
6567
6668 void getErrorMessages () const {
@@ -251,6 +253,46 @@ class TestCppcheck : public TestFixture {
251253 ASSERT_EQUALS (" -x c --include 1.h " , cppcheck.getClangFlags (Standards::Language::C));
252254 }
253255
256+ void invalidSyntaxUnknownMacro1 () { // #13378
257+ ScopedFile file (" test.c" ,
258+ " void foo(void) {\n "
259+ " #ifdef START\n "
260+ " START();\n "
261+ " #endif\n "
262+ " }\n " );
263+ ErrorLogger2 errorLogger;
264+ CppCheck cppcheck (errorLogger, false , {});
265+
266+ ASSERT_EQUALS (1 , cppcheck.check (FileWithDetails (file.path ())));
267+ errorLogger.errmsgs .erase (std::remove_if (errorLogger.errmsgs .begin (), errorLogger.errmsgs .end (), [](const ErrorMessage& errmsg) {
268+ return errmsg.id == " logChecker" ;
269+ }), errorLogger.errmsgs .end ());
270+ ASSERT_EQUALS (1 , errorLogger.errmsgs .size ());
271+ ASSERT_EQUALS (" literal used as function. Macro 'START' expands to '1'. Use -DSTART=... to specify a value, or -USTART to undefine it." ,
272+ errorLogger.errmsgs .cbegin ()->shortMessage ());
273+ ASSERT_EQUALS (" unknownMacro" , errorLogger.errmsgs .cbegin ()->id );
274+ }
275+
276+ void invalidSyntaxUnknownMacro2 () { // #13378
277+ ScopedFile file (" test.c" ,
278+ " void foo(void) {\n "
279+ " #ifdef START\n "
280+ " START();\n "
281+ " #endif\n "
282+ " }\n " );
283+ ErrorLogger2 errorLogger;
284+ CppCheck cppcheck (errorLogger, false , {});
285+ cppcheck.settings ().userDefines = " START=1" ; // cppcheck -DSTART ...
286+
287+ ASSERT_EQUALS (1 , cppcheck.check (FileWithDetails (file.path ())));
288+ errorLogger.errmsgs .erase (std::remove_if (errorLogger.errmsgs .begin (), errorLogger.errmsgs .end (), [](const ErrorMessage& errmsg) {
289+ return errmsg.id == " logChecker" ;
290+ }), errorLogger.errmsgs .end ());
291+ ASSERT_EQUALS (1 , errorLogger.errmsgs .size ());
292+ ASSERT_EQUALS (" syntax error" , errorLogger.errmsgs .cbegin ()->shortMessage ());
293+ ASSERT_EQUALS (" syntaxError" , errorLogger.errmsgs .cbegin ()->id );
294+ }
295+
254296 // TODO: test suppressions
255297 // TODO: test all with FS
256298};
0 commit comments