|
13 | 13 |
|
14 | 14 | import cpp
|
15 | 15 | import codingstandards.c.misra
|
| 16 | +import codingstandards.cpp.Macro |
16 | 17 | import codingstandards.cpp.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() |
| 28 | + ) |
| 29 | +} |
| 30 | + |
| 31 | +Macro getNonFunctionPrimaryMacro(CStyleCast cast) { |
| 32 | + result = getPrimaryMacro(cast) and |
| 33 | + not result instanceof FunctionLikeMacro |
| 34 | +} |
| 35 | + |
| 36 | +from |
| 37 | + Locatable primaryLocation, CStyleCast cast, Type typeFrom, Type typeTo, string message, |
| 38 | + string extraMessage, Locatable optionalPlaceholderLocation, string optionalPlaceholderMessage |
19 | 39 | where
|
20 | 40 | not isExcluded(cast, Pointers1Package::conversionBetweenPointerToObjectAndIntegerTypeQuery()) and
|
21 | 41 | typeFrom = cast.getExpr().getUnderlyingType() and
|
22 | 42 | typeTo = cast.getUnderlyingType() and
|
23 |
| - [typeFrom, typeTo] instanceof IntegralType and |
24 |
| - [typeFrom, typeTo] instanceof PointerToObjectType and |
25 |
| - not isNullPointerConstant(cast.getExpr()) |
26 |
| -select cast, "Cast performed between a pointer to object type and a pointer to an integer type." |
| 43 | + ( |
| 44 | + typeFrom instanceof PointerToObjectType and |
| 45 | + typeTo instanceof IntegralType and |
| 46 | + message = |
| 47 | + "Cast from pointer to object type '" + typeFrom + "' to integer type '" + typeTo + "'" + |
| 48 | + extraMessage + "." |
| 49 | + or |
| 50 | + typeFrom instanceof IntegralType and |
| 51 | + typeTo instanceof PointerToObjectType and |
| 52 | + message = |
| 53 | + "Cast from integer type '" + typeFrom + "' to pointer to object type '" + typeTo + "'" + |
| 54 | + extraMessage + "." |
| 55 | + ) and |
| 56 | + not isNullPointerConstant(cast.getExpr()) and |
| 57 | + // If this alert is arising through a non-function-like macro expansion, flag the macro instead, to |
| 58 | + // help make the alerts more manageable. We only do this for non-function-like macros because they |
| 59 | + // cannot be context specific. |
| 60 | + if exists(getNonFunctionPrimaryMacro(cast)) |
| 61 | + then |
| 62 | + primaryLocation = getNonFunctionPrimaryMacro(cast) and |
| 63 | + extraMessage = "" and |
| 64 | + optionalPlaceholderLocation = primaryLocation and |
| 65 | + optionalPlaceholderMessage = "" |
| 66 | + else ( |
| 67 | + primaryLocation = cast and |
| 68 | + // If the cast is in a macro expansion which is context specific, we still report the original |
| 69 | + // location, but also add a link to the most specific macro that contains the cast, to aid |
| 70 | + // validation. |
| 71 | + if exists(getPrimaryMacro(cast)) |
| 72 | + then |
| 73 | + extraMessage = " from expansion of macro $@" and |
| 74 | + exists(Macro m | |
| 75 | + m = getPrimaryMacro(cast) and |
| 76 | + optionalPlaceholderLocation = m and |
| 77 | + optionalPlaceholderMessage = m.getName() |
| 78 | + ) |
| 79 | + else ( |
| 80 | + extraMessage = "" and |
| 81 | + optionalPlaceholderLocation = cast and |
| 82 | + optionalPlaceholderMessage = "" |
| 83 | + ) |
| 84 | + ) |
| 85 | +select primaryLocation, message, optionalPlaceholderLocation, optionalPlaceholderMessage |
0 commit comments