Skip to content

Commit 5ad86ef

Browse files
committed
RULE-11-4: Compress macro results
Where results arise from macro expansions, where there's no possibility that the cast was passed in through a macro argument, we compress the results by reporting the macro location once instead of each use.
1 parent aab3a88 commit 5ad86ef

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

c/misra/src/rules/RULE-11-4/ConversionBetweenPointerToObjectAndIntegerType.ql

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,34 @@
1313

1414
import cpp
1515
import codingstandards.c.misra
16+
import codingstandards.cpp.Macro
1617
import codingstandards.c.Pointers
1718

18-
from CStyleCast cast, Type typeFrom, Type typeTo
19+
MacroInvocation getAMacroInvocation(CStyleCast cast) { result.getAnExpandedElement() = cast }
20+
21+
Macro getPrimaryMacro(CStyleCast cast) {
22+
exists(MacroInvocation mi |
23+
mi = getAMacroInvocation(cast) and
24+
not exists(MacroInvocation otherMi |
25+
otherMi = getAMacroInvocation(cast) and otherMi.getParentInvocation() = mi
26+
) and
27+
result = mi.getMacro() and
28+
not result instanceof FunctionLikeMacro
29+
)
30+
}
31+
32+
from Locatable primaryLocation, CStyleCast cast, Type typeFrom, Type typeTo
1933
where
2034
not isExcluded(cast, Pointers1Package::castBetweenObjectPointerAndDifferentObjectTypeQuery()) and
2135
typeFrom = cast.getExpr().getUnderlyingType() and
2236
typeTo = cast.getUnderlyingType() and
2337
[typeFrom, typeTo] instanceof IntegralType and
2438
[typeFrom, typeTo] instanceof PointerToObjectType and
25-
not isNullPointerConstant(cast.getExpr())
26-
select cast,
39+
not isNullPointerConstant(cast.getExpr()) and
40+
// If this alert is arising through a macro expansion, flag the macro instead, to
41+
// help make the alerts more manageable
42+
if exists(getPrimaryMacro(cast))
43+
then primaryLocation = getPrimaryMacro(cast)
44+
else primaryLocation = cast
45+
select primaryLocation,
2746
"Cast performed between a pointer to object type and a pointer to an integer type."

0 commit comments

Comments
 (0)