Skip to content

Commit f9d4a64

Browse files
authored
Fix #15017 (simplecpp DIRECTIVE_AS_MACRO_PARAMETER, separate error id and misra 20.6) (#8839)
1 parent f81f6f0 commit f9d4a64

5 files changed

Lines changed: 79 additions & 17 deletions

File tree

lib/checkersidmapping.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
std::vector<checkers::IdMapping> checkers::idMappingAutosar{
2424
{"m0-1-1", "unreachableCode,duplicateBreak"},
25-
{"m0-1-2", "unsignedLessThanZero"},
25+
{"m0-1-2", "unsignedLessThanZero,compareValueOutOfTypeRangeError"},
2626
{"m0-1-3", "unusedVariable,unusedStructMember"},
2727
{"a0-1-1", "unreadVariable,redundantAssignment"},
2828
{"m0-1-9", "redundantAssignment,redundantInitialization"},
@@ -95,6 +95,8 @@ std::vector<checkers::IdMapping> checkers::idMappingCertC{
9595
};
9696

9797
std::vector<checkers::IdMapping> checkers::idMappingCertCpp{
98+
{"STR52", "invalidContainer"},
99+
{"STR51", "nullPointer"},
98100
{"CTR51", "eraseDereference"},
99101
{"CTR54", "comparePointers"},
100102
{"CTR55", "containerOutOfBounds"},
@@ -104,8 +106,8 @@ std::vector<checkers::IdMapping> checkers::idMappingCertCpp{
104106
{"EXP52", "sizeofCalculation"},
105107
{"EXP53", "uninitvar,uninitdata,uninitStructMember"},
106108
{"EXP54", "uninitvar,danglingLifetime,danglingReference,danglingTemporaryLifetime,danglingTempReference,returnDanglingLifetime"},
107-
{"EXP61", "danglingLifetime,danglingReference,danglingTemporaryLifetime,danglingTempReference,returnDanglingLifetime,deallocuse,deallocret"},
108-
{"EXP63", "accessMoved"},
109+
{"EXP61", "danglingLifetime,danglingReference,danglingTemporaryLifetime,danglingTempReference,deallocuse,deallocret,returnDanglingLifetime"},
110+
{"EXP63", "accessMoved,uselessCallsRemove"},
109111
{"FIO50", "IOWithoutPositioning"},
110112
{"MEM50", "deallocuse"},
111113
{"MEM51", "mismatchAllocDealloc"},
@@ -115,8 +117,6 @@ std::vector<checkers::IdMapping> checkers::idMappingCertCpp{
115117
{"OOP52", "virtualDestructor"},
116118
{"OOP53", "initializerList"},
117119
{"OOP54", "operatorEqToSelf"},
118-
{"STR51", "nullPointer"},
119-
{"STR52", "invalidContainer"},
120120
};
121121

122122
std::vector<checkers::IdMapping> checkers::idMappingMisraC{
@@ -128,22 +128,23 @@ std::vector<checkers::IdMapping> checkers::idMappingMisraC{
128128
{"2.8", "unusedVariable"},
129129
{"5.3", "shadowVariable"},
130130
{"8.3", "funcArgNamesDifferent"},
131-
{"8.13", "constParameterPointer"},
132-
{"9.1", "uninitvar"},
131+
{"8.13", "constParameter,constParameterPointer,constVariablePointer,constParameterCallback"},
132+
{"9.1", "uninitvar,legacyUninitvar"},
133+
{"12.2", "shiftNegative"},
133134
{"12.5", "sizeofwithsilentarraypointer"},
134135
{"13.2", "unknownEvaluationOrder"},
135136
{"13.6", "sizeofCalculation"},
136-
{"14.3", "compareValueOutOfTypeRangeError,knownConditionTrueFalse"},
137+
{"14.3", "compareValueOutOfTypeRangeError,knownConditionTrueFalse,incorrectLogicOperator"},
137138
{"17.4", "missingReturn"},
138139
{"17.5", "argumentSize"},
139-
{"18.1", "pointerOutOfBounds"},
140+
{"18.1", "arrayIndexOutOfBounds,arrayIndexOutOfBoundsCond,pointerOutOfBounds,negativeIndex"},
140141
{"18.2", "comparePointers"},
141142
{"18.3", "comparePointers"},
142-
{"18.6", "danglingLifetime,danglingTemporaryLifetime,returnDanglingLifetime"},
143+
{"18.6", "autoVariables,danglingLifetime,danglingTemporaryLifetime,returnDanglingLifetime"},
143144
{"19.1", "overlappingWriteUnion,overlappingWriteFunction"},
144-
{"20.6", "preprocessorErrorDirective"},
145+
{"20.6", "directiveAsMacroParameter"},
145146
{"21.13", "invalidFunctionArg"},
146-
{"21.17", "bufferAccessOutOfBounds"},
147+
{"21.17", "invalidFunctionArgStr"},
147148
{"21.18", "bufferAccessOutOfBounds"},
148149
{"22.1", "memleak,resourceLeak,memleakOnRealloc,leakReturnValNotUsed,leakNoVarFunctionCall"},
149150
{"22.2", "autovarInvalidDeallocation"},
@@ -154,7 +155,7 @@ std::vector<checkers::IdMapping> checkers::idMappingMisraC{
154155

155156
std::vector<checkers::IdMapping> checkers::idMappingMisraCpp2008{
156157
{"0-1-1", "unreachableCode,duplicateBreak"},
157-
{"0-1-2", "unsignedLessThanZero"},
158+
{"0-1-2", "unsignedLessThanZero,compareValueOutOfTypeRangeError"},
158159
{"0-1-3", "unusedVariable,unusedStructMember"},
159160
{"0-1-6", "redundantAssignment,unreadVariable,variableScope"},
160161
{"0-1-9", "redundantAssignment,redundantInitialization"},

lib/errorlogger.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
const std::set<std::string> ErrorLogger::mCriticalErrorIds{
4848
"cppcheckError",
4949
"cppcheckLimit",
50+
"directiveAsMacroParameter",
5051
"includeNestedTooDeeply",
5152
"internalAstError",
5253
"instantiationError",

lib/preprocessor.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -981,8 +981,9 @@ static std::string simplecppErrToId(simplecpp::Output::Type type)
981981
case simplecpp::Output::ERROR:
982982
return "preprocessorErrorDirective";
983983
case simplecpp::Output::SYNTAX_ERROR:
984-
case simplecpp::Output::DIRECTIVE_AS_MACRO_PARAMETER:
985984
return "syntaxError";
985+
case simplecpp::Output::DIRECTIVE_AS_MACRO_PARAMETER:
986+
return "directiveAsMacroParameter";
986987
case simplecpp::Output::UNHANDLED_CHAR_ERROR:
987988
return "unhandledChar";
988989
case simplecpp::Output::INCLUDE_NESTED_TOO_DEEPLY:

lib/settings.cpp

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ static const std::set<std::string> autosarCheckers{
369369
"arrayIndexThenCheck",
370370
"bufferAccessOutOfBounds",
371371
"comparePointers",
372+
"compareValueOutOfTypeRangeError",
372373
"constParameter",
373374
"ctuOneDefinitionRuleViolation",
374375
"doubleFree",
@@ -528,6 +529,7 @@ static const std::set<std::string> certCppCheckers{
528529
"uninitdata",
529530
"uninitvar",
530531
"useClosedFile",
532+
"uselessCallsRemove",
531533
"virtualCallInConstructor",
532534
"virtualDestructor",
533535
"wrongPrintfScanfArgNum",
@@ -536,34 +538,45 @@ static const std::set<std::string> certCppCheckers{
536538

537539
static const std::set<std::string> misrac2012Checkers{
538540
"argumentSize",
541+
"arrayIndexOutOfBounds",
542+
"arrayIndexOutOfBoundsCond",
543+
"autoVariables",
539544
"autovarInvalidDeallocation",
540545
"bufferAccessOutOfBounds",
541546
"comparePointers",
542547
"compareValueOutOfTypeRangeError",
548+
"constParameter",
549+
"constParameterCallback",
543550
"constParameterPointer",
544551
"constStatement",
552+
"constVariablePointer",
545553
"danglingLifetime",
546554
"danglingTemporaryLifetime",
555+
"directiveAsMacroParameter",
547556
"duplicateBreak",
548557
"funcArgNamesDifferent",
549558
"incompatibleFileOpen",
559+
"incorrectLogicOperator",
550560
"invalidFunctionArg",
561+
"invalidFunctionArgStr",
551562
"knownConditionTrueFalse",
552563
"leakNoVarFunctionCall",
553564
"leakReturnValNotUsed",
565+
"legacyUninitvar",
554566
"memleak",
555567
"memleakOnRealloc",
556568
"missingReturn",
569+
"negativeIndex",
557570
"overlappingWriteFunction",
558571
"overlappingWriteUnion",
559572
"pointerOutOfBounds",
560-
"preprocessorErrorDirective",
561573
"redundantAssignInSwitch",
562574
"redundantAssignment",
563575
"redundantCondition",
564576
"resourceLeak",
565577
"returnDanglingLifetime",
566578
"shadowVariable",
579+
"shiftNegative",
567580
"sizeofCalculation",
568581
"sizeofwithsilentarraypointer",
569582
"syntaxError",
@@ -580,34 +593,45 @@ static const std::set<std::string> misrac2012Checkers{
580593

581594
static const std::set<std::string> misrac2023Checkers{
582595
"argumentSize",
596+
"arrayIndexOutOfBounds",
597+
"arrayIndexOutOfBoundsCond",
598+
"autoVariables",
583599
"autovarInvalidDeallocation",
584600
"bufferAccessOutOfBounds",
585601
"comparePointers",
586602
"compareValueOutOfTypeRangeError",
603+
"constParameter",
604+
"constParameterCallback",
587605
"constParameterPointer",
588606
"constStatement",
607+
"constVariablePointer",
589608
"danglingLifetime",
590609
"danglingTemporaryLifetime",
610+
"directiveAsMacroParameter",
591611
"duplicateBreak",
592612
"funcArgNamesDifferent",
593613
"incompatibleFileOpen",
614+
"incorrectLogicOperator",
594615
"invalidFunctionArg",
616+
"invalidFunctionArgStr",
595617
"knownConditionTrueFalse",
596618
"leakNoVarFunctionCall",
597619
"leakReturnValNotUsed",
620+
"legacyUninitvar",
598621
"memleak",
599622
"memleakOnRealloc",
600623
"missingReturn",
624+
"negativeIndex",
601625
"overlappingWriteFunction",
602626
"overlappingWriteUnion",
603627
"pointerOutOfBounds",
604-
"preprocessorErrorDirective",
605628
"redundantAssignInSwitch",
606629
"redundantAssignment",
607630
"redundantCondition",
608631
"resourceLeak",
609632
"returnDanglingLifetime",
610633
"shadowVariable",
634+
"shiftNegative",
611635
"sizeofCalculation",
612636
"sizeofwithsilentarraypointer",
613637
"syntaxError",
@@ -624,34 +648,45 @@ static const std::set<std::string> misrac2023Checkers{
624648

625649
static const std::set<std::string> misrac2025Checkers{
626650
"argumentSize",
651+
"arrayIndexOutOfBounds",
652+
"arrayIndexOutOfBoundsCond",
653+
"autoVariables",
627654
"autovarInvalidDeallocation",
628655
"bufferAccessOutOfBounds",
629656
"comparePointers",
630657
"compareValueOutOfTypeRangeError",
658+
"constParameter",
659+
"constParameterCallback",
631660
"constParameterPointer",
632661
"constStatement",
662+
"constVariablePointer",
633663
"danglingLifetime",
634664
"danglingTemporaryLifetime",
665+
"directiveAsMacroParameter",
635666
"duplicateBreak",
636667
"funcArgNamesDifferent",
637668
"incompatibleFileOpen",
669+
"incorrectLogicOperator",
638670
"invalidFunctionArg",
671+
"invalidFunctionArgStr",
639672
"knownConditionTrueFalse",
640673
"leakNoVarFunctionCall",
641674
"leakReturnValNotUsed",
675+
"legacyUninitvar",
642676
"memleak",
643677
"memleakOnRealloc",
644678
"missingReturn",
679+
"negativeIndex",
645680
"overlappingWriteFunction",
646681
"overlappingWriteUnion",
647682
"pointerOutOfBounds",
648-
"preprocessorErrorDirective",
649683
"redundantAssignInSwitch",
650684
"redundantAssignment",
651685
"redundantCondition",
652686
"resourceLeak",
653687
"returnDanglingLifetime",
654688
"shadowVariable",
689+
"shiftNegative",
655690
"sizeofCalculation",
656691
"sizeofwithsilentarraypointer",
657692
"syntaxError",
@@ -669,6 +704,7 @@ static const std::set<std::string> misrac2025Checkers{
669704
static const std::set<std::string> misracpp2008Checkers{
670705
"autoVariables",
671706
"comparePointers",
707+
"compareValueOutOfTypeRangeError",
672708
"constParameter",
673709
"constVariable",
674710
"cstyleCast",

test/cli/other_test.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,29 @@ def test_addon_misra(tmpdir):
432432
assert stderr == '{}:2:13: style: misra violation (use --rule-texts=<file> to get proper output) [misra-c2012-2.3]\ntypedef int MISRA_5_6_VIOLATION;\n ^\n'.format(test_file)
433433

434434

435+
def test_report_type_misra_c_2025_20_6(tmpdir): # #15017
436+
""" using a preprocessor directive as a macro parameter is a MISRA C 20.6 violation """
437+
test_file = os.path.join(tmpdir, 'test.c')
438+
with open(test_file, 'wt') as f:
439+
f.write("""
440+
#define A(X) X
441+
442+
A(
443+
#ifdef __GNUC__
444+
1
445+
#else
446+
0
447+
#endif
448+
)
449+
""")
450+
451+
args = ['--template=simple', '--report-type=misra-c-2025', test_file]
452+
453+
exitcode, stdout, stderr = cppcheck(args)
454+
assert exitcode == 0, stdout if stdout else stderr
455+
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)
456+
457+
435458
def test_addon_y2038(tmpdir):
436459
test_file = os.path.join(tmpdir, 'test.cpp')
437460
# TODO: trigger warning

0 commit comments

Comments
 (0)