From 11361d956016d65e5917c25318291e7600af7841 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 8 Sep 2026 18:24:48 +0200 Subject: [PATCH 1/2] Fix #15017 (simplecpp DIRECTIVE_AS_MACRO_PARAMETER, separate error id and misra 20.6) --- lib/checkersidmapping.cpp | 27 +++++++++++++------------ lib/preprocessor.cpp | 3 ++- lib/settings.cpp | 42 ++++++++++++++++++++++++++++++++++++--- test/cli/other_test.py | 23 +++++++++++++++++++++ 4 files changed, 78 insertions(+), 17 deletions(-) diff --git a/lib/checkersidmapping.cpp b/lib/checkersidmapping.cpp index e9636843853..7021a5c12ce 100644 --- a/lib/checkersidmapping.cpp +++ b/lib/checkersidmapping.cpp @@ -22,7 +22,7 @@ std::vector checkers::idMappingAutosar{ {"m0-1-1", "unreachableCode,duplicateBreak"}, - {"m0-1-2", "unsignedLessThanZero"}, + {"m0-1-2", "unsignedLessThanZero,compareValueOutOfTypeRangeError"}, {"m0-1-3", "unusedVariable,unusedStructMember"}, {"a0-1-1", "unreadVariable,redundantAssignment"}, {"m0-1-9", "redundantAssignment,redundantInitialization"}, @@ -95,6 +95,8 @@ std::vector checkers::idMappingCertC{ }; std::vector checkers::idMappingCertCpp{ + {"STR52", "invalidContainer"}, + {"STR51", "nullPointer"}, {"CTR51", "eraseDereference"}, {"CTR54", "comparePointers"}, {"CTR55", "containerOutOfBounds"}, @@ -104,8 +106,8 @@ std::vector checkers::idMappingCertCpp{ {"EXP52", "sizeofCalculation"}, {"EXP53", "uninitvar,uninitdata,uninitStructMember"}, {"EXP54", "uninitvar,danglingLifetime,danglingReference,danglingTemporaryLifetime,danglingTempReference,returnDanglingLifetime"}, - {"EXP61", "danglingLifetime,danglingReference,danglingTemporaryLifetime,danglingTempReference,returnDanglingLifetime,deallocuse,deallocret"}, - {"EXP63", "accessMoved"}, + {"EXP61", "danglingLifetime,danglingReference,danglingTemporaryLifetime,danglingTempReference,deallocuse,deallocret,returnDanglingLifetime"}, + {"EXP63", "accessMoved,uselessCallsRemove"}, {"FIO50", "IOWithoutPositioning"}, {"MEM50", "deallocuse"}, {"MEM51", "mismatchAllocDealloc"}, @@ -115,8 +117,6 @@ std::vector checkers::idMappingCertCpp{ {"OOP52", "virtualDestructor"}, {"OOP53", "initializerList"}, {"OOP54", "operatorEqToSelf"}, - {"STR51", "nullPointer"}, - {"STR52", "invalidContainer"}, }; std::vector checkers::idMappingMisraC{ @@ -128,22 +128,23 @@ std::vector checkers::idMappingMisraC{ {"2.8", "unusedVariable"}, {"5.3", "shadowVariable"}, {"8.3", "funcArgNamesDifferent"}, - {"8.13", "constParameterPointer"}, - {"9.1", "uninitvar"}, + {"8.13", "constParameter,constParameterPointer,constVariablePointer,constParameterCallback"}, + {"9.1", "uninitvar,legacyUninitvar"}, + {"12.2", "shiftNegative"}, {"12.5", "sizeofwithsilentarraypointer"}, {"13.2", "unknownEvaluationOrder"}, {"13.6", "sizeofCalculation"}, - {"14.3", "compareValueOutOfTypeRangeError,knownConditionTrueFalse"}, + {"14.3", "compareValueOutOfTypeRangeError,knownConditionTrueFalse,incorrectLogicOperator"}, {"17.4", "missingReturn"}, {"17.5", "argumentSize"}, - {"18.1", "pointerOutOfBounds"}, + {"18.1", "arrayIndexOutOfBounds,arrayIndexOutOfBoundsCond,pointerOutOfBounds,negativeIndex"}, {"18.2", "comparePointers"}, {"18.3", "comparePointers"}, - {"18.6", "danglingLifetime,danglingTemporaryLifetime,returnDanglingLifetime"}, + {"18.6", "autoVariables,danglingLifetime,danglingTemporaryLifetime,returnDanglingLifetime"}, {"19.1", "overlappingWriteUnion,overlappingWriteFunction"}, - {"20.6", "preprocessorErrorDirective"}, + {"20.6", "directiveAsMacroParameter"}, {"21.13", "invalidFunctionArg"}, - {"21.17", "bufferAccessOutOfBounds"}, + {"21.17", "invalidFunctionArgStr"}, {"21.18", "bufferAccessOutOfBounds"}, {"22.1", "memleak,resourceLeak,memleakOnRealloc,leakReturnValNotUsed,leakNoVarFunctionCall"}, {"22.2", "autovarInvalidDeallocation"}, @@ -154,7 +155,7 @@ std::vector checkers::idMappingMisraC{ std::vector checkers::idMappingMisraCpp2008{ {"0-1-1", "unreachableCode,duplicateBreak"}, - {"0-1-2", "unsignedLessThanZero"}, + {"0-1-2", "unsignedLessThanZero,compareValueOutOfTypeRangeError"}, {"0-1-3", "unusedVariable,unusedStructMember"}, {"0-1-6", "redundantAssignment,unreadVariable,variableScope"}, {"0-1-9", "redundantAssignment,redundantInitialization"}, diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 325ee4c5c55..d18a1fae4f2 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -981,8 +981,9 @@ static std::string simplecppErrToId(simplecpp::Output::Type type) case simplecpp::Output::ERROR: return "preprocessorErrorDirective"; case simplecpp::Output::SYNTAX_ERROR: - case simplecpp::Output::DIRECTIVE_AS_MACRO_PARAMETER: return "syntaxError"; + case simplecpp::Output::DIRECTIVE_AS_MACRO_PARAMETER: + return "directiveAsMacroParameter"; case simplecpp::Output::UNHANDLED_CHAR_ERROR: return "unhandledChar"; case simplecpp::Output::INCLUDE_NESTED_TOO_DEEPLY: diff --git a/lib/settings.cpp b/lib/settings.cpp index 479346208e3..410f3d5107d 100644 --- a/lib/settings.cpp +++ b/lib/settings.cpp @@ -369,6 +369,7 @@ static const std::set autosarCheckers{ "arrayIndexThenCheck", "bufferAccessOutOfBounds", "comparePointers", + "compareValueOutOfTypeRangeError", "constParameter", "ctuOneDefinitionRuleViolation", "doubleFree", @@ -528,6 +529,7 @@ static const std::set certCppCheckers{ "uninitdata", "uninitvar", "useClosedFile", + "uselessCallsRemove", "virtualCallInConstructor", "virtualDestructor", "wrongPrintfScanfArgNum", @@ -536,34 +538,45 @@ static const std::set certCppCheckers{ static const std::set misrac2012Checkers{ "argumentSize", + "arrayIndexOutOfBounds", + "arrayIndexOutOfBoundsCond", + "autoVariables", "autovarInvalidDeallocation", "bufferAccessOutOfBounds", "comparePointers", "compareValueOutOfTypeRangeError", + "constParameter", + "constParameterCallback", "constParameterPointer", "constStatement", + "constVariablePointer", "danglingLifetime", "danglingTemporaryLifetime", + "directiveAsMacroParameter", "duplicateBreak", "funcArgNamesDifferent", "incompatibleFileOpen", + "incorrectLogicOperator", "invalidFunctionArg", + "invalidFunctionArgStr", "knownConditionTrueFalse", "leakNoVarFunctionCall", "leakReturnValNotUsed", + "legacyUninitvar", "memleak", "memleakOnRealloc", "missingReturn", + "negativeIndex", "overlappingWriteFunction", "overlappingWriteUnion", "pointerOutOfBounds", - "preprocessorErrorDirective", "redundantAssignInSwitch", "redundantAssignment", "redundantCondition", "resourceLeak", "returnDanglingLifetime", "shadowVariable", + "shiftNegative", "sizeofCalculation", "sizeofwithsilentarraypointer", "syntaxError", @@ -580,34 +593,45 @@ static const std::set misrac2012Checkers{ static const std::set misrac2023Checkers{ "argumentSize", + "arrayIndexOutOfBounds", + "arrayIndexOutOfBoundsCond", + "autoVariables", "autovarInvalidDeallocation", "bufferAccessOutOfBounds", "comparePointers", "compareValueOutOfTypeRangeError", + "constParameter", + "constParameterCallback", "constParameterPointer", "constStatement", + "constVariablePointer", "danglingLifetime", "danglingTemporaryLifetime", + "directiveAsMacroParameter", "duplicateBreak", "funcArgNamesDifferent", "incompatibleFileOpen", + "incorrectLogicOperator", "invalidFunctionArg", + "invalidFunctionArgStr", "knownConditionTrueFalse", "leakNoVarFunctionCall", "leakReturnValNotUsed", + "legacyUninitvar", "memleak", "memleakOnRealloc", "missingReturn", + "negativeIndex", "overlappingWriteFunction", "overlappingWriteUnion", "pointerOutOfBounds", - "preprocessorErrorDirective", "redundantAssignInSwitch", "redundantAssignment", "redundantCondition", "resourceLeak", "returnDanglingLifetime", "shadowVariable", + "shiftNegative", "sizeofCalculation", "sizeofwithsilentarraypointer", "syntaxError", @@ -624,34 +648,45 @@ static const std::set misrac2023Checkers{ static const std::set misrac2025Checkers{ "argumentSize", + "arrayIndexOutOfBounds", + "arrayIndexOutOfBoundsCond", + "autoVariables", "autovarInvalidDeallocation", "bufferAccessOutOfBounds", "comparePointers", "compareValueOutOfTypeRangeError", + "constParameter", + "constParameterCallback", "constParameterPointer", "constStatement", + "constVariablePointer", "danglingLifetime", "danglingTemporaryLifetime", + "directiveAsMacroParameter", "duplicateBreak", "funcArgNamesDifferent", "incompatibleFileOpen", + "incorrectLogicOperator", "invalidFunctionArg", + "invalidFunctionArgStr", "knownConditionTrueFalse", "leakNoVarFunctionCall", "leakReturnValNotUsed", + "legacyUninitvar", "memleak", "memleakOnRealloc", "missingReturn", + "negativeIndex", "overlappingWriteFunction", "overlappingWriteUnion", "pointerOutOfBounds", - "preprocessorErrorDirective", "redundantAssignInSwitch", "redundantAssignment", "redundantCondition", "resourceLeak", "returnDanglingLifetime", "shadowVariable", + "shiftNegative", "sizeofCalculation", "sizeofwithsilentarraypointer", "syntaxError", @@ -669,6 +704,7 @@ static const std::set misrac2025Checkers{ static const std::set misracpp2008Checkers{ "autoVariables", "comparePointers", + "compareValueOutOfTypeRangeError", "constParameter", "constVariable", "cstyleCast", diff --git a/test/cli/other_test.py b/test/cli/other_test.py index ace82b9ac32..3979f2dd8b8 100644 --- a/test/cli/other_test.py +++ b/test/cli/other_test.py @@ -432,6 +432,29 @@ def test_addon_misra(tmpdir): assert stderr == '{}:2:13: style: misra violation (use --rule-texts= to get proper output) [misra-c2012-2.3]\ntypedef int MISRA_5_6_VIOLATION;\n ^\n'.format(test_file) +def test_report_type_misra_c_2025_20_6(tmpdir): # #15017 + """ using a preprocessor directive as a macro parameter is a MISRA C 20.6 violation """ + test_file = os.path.join(tmpdir, 'test.c') + with open(test_file, 'wt') as f: + f.write(""" +#define A(X) X + +A( +#ifdef __GNUC__ + 1 +#else + 0 +#endif +) +""") + + args = ['--template=simple', '--report-type=misra-c-2025', test_file] + + exitcode, stdout, stderr = cppcheck(args) + assert exitcode == 0, stdout if stdout else stderr + assert stderr == "{}:5:1: Required: failed to expand 'A', it is invalid to use a preprocessor directive as macro parameter [20.6]\n".format(test_file) + + def test_addon_y2038(tmpdir): test_file = os.path.join(tmpdir, 'test.cpp') # TODO: trigger warning From a51ad9df87d5564a9f97d1a29e87e301d2d6db69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 8 Sep 2026 18:26:59 +0200 Subject: [PATCH 2/2] critical error --- lib/errorlogger.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/errorlogger.cpp b/lib/errorlogger.cpp index 1199223c822..30097a4f21c 100644 --- a/lib/errorlogger.cpp +++ b/lib/errorlogger.cpp @@ -47,6 +47,7 @@ const std::set ErrorLogger::mCriticalErrorIds{ "cppcheckError", "cppcheckLimit", + "directiveAsMacroParameter", "includeNestedTooDeeply", "internalAstError", "instantiationError",