|
15 | 15 |
|
16 | 16 | import cpp
|
17 | 17 | import codingstandards.c.misra
|
| 18 | +import codingstandards.cpp.Macro |
| 19 | +import codingstandards.cpp.Includes |
| 20 | +import codingstandards.cpp.PreprocessorDirective |
| 21 | + |
| 22 | +/** |
| 23 | + * Gets a link target that this macro is expanded in. |
| 24 | + */ |
| 25 | +LinkTarget getALinkTarget(Macro m) { |
| 26 | + exists(Element e | e = m.getAnInvocation().getAnAffectedElement() | |
| 27 | + result = e.(Expr).getEnclosingFunction().getALinkTarget() |
| 28 | + or |
| 29 | + result = e.(Stmt).getEnclosingFunction().getALinkTarget() |
| 30 | + or |
| 31 | + exists(GlobalOrNamespaceVariable g | |
| 32 | + result = g.getALinkTarget() and |
| 33 | + g.getInitializer().getExpr().getAChild*() = e |
| 34 | + ) |
| 35 | + ) |
| 36 | +} |
18 | 37 |
|
19 | 38 | from Macro m, Macro m2
|
20 | 39 | where
|
|
30 | 49 | else m.getName() = m2.getName()
|
31 | 50 | ) and
|
32 | 51 | //reduce double report since both macros are in alert, arbitrary ordering
|
33 |
| - m.getLocation().getStartLine() >= m2.getLocation().getStartLine() |
| 52 | + m.getLocation().getStartLine() >= m2.getLocation().getStartLine() and |
| 53 | + // Not within an #ifndef MACRO_NAME |
| 54 | + not exists(PreprocessorIfndef ifBranch | |
| 55 | + m.getAGuard() = ifBranch or |
| 56 | + m2.getAGuard() = ifBranch |
| 57 | + | |
| 58 | + ifBranch.getHead() = m.getName() |
| 59 | + ) and |
| 60 | + // Must be included unconditionally from the same file, otherwise m1 may not be defined |
| 61 | + // when m2 is defined |
| 62 | + exists(File f | |
| 63 | + getAnUnconditionallyIncludedFile*(f) = m.getFile() and |
| 64 | + getAnUnconditionallyIncludedFile*(f) = m2.getFile() |
| 65 | + ) and |
| 66 | + // Macros can't be mutually exclusive |
| 67 | + not mutuallyExclusiveMacros(m, m2) and |
| 68 | + not mutuallyExclusiveMacros(m2, m) and |
| 69 | + // If at least one invocation exists for at least one of the macros, then they must share a link |
| 70 | + // target - i.e. must both be expanded in the same context |
| 71 | + ( |
| 72 | + (exists(m.getAnInvocation()) and exists(m2.getAnInvocation())) |
| 73 | + implies |
| 74 | + // Must share a link target - e.g. must both be expanded in the same context |
| 75 | + getALinkTarget(m) = getALinkTarget(m2) |
| 76 | + ) |
34 | 77 | select m,
|
35 | 78 | "Macro identifer " + m.getName() + " is nondistinct in first 63 characters, compared to $@.", m2,
|
36 | 79 | m2.getName()
|
0 commit comments