|
13 | 13 |
|
14 | 14 | import cpp
|
15 | 15 | import codingstandards.c.misra
|
| 16 | +import codingstandards.cpp.Macro |
16 | 17 | import codingstandards.c.Pointers
|
17 | 18 |
|
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 |
19 | 33 | where
|
20 | 34 | not isExcluded(cast, Pointers1Package::castBetweenObjectPointerAndDifferentObjectTypeQuery()) and
|
21 | 35 | typeFrom = cast.getExpr().getUnderlyingType() and
|
22 | 36 | typeTo = cast.getUnderlyingType() and
|
23 | 37 | [typeFrom, typeTo] instanceof IntegralType and
|
24 | 38 | [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, |
27 | 46 | "Cast performed between a pointer to object type and a pointer to an integer type."
|
0 commit comments