|
15 | 15 |
|
16 | 16 | import cpp |
17 | 17 | import codingstandards.c.misra |
18 | | -import codingstandards.cpp.Naming |
19 | | -import codingstandards.cpp.Macro |
20 | | - |
21 | | -abstract class IrreplaceableFunctionLikeMacro extends FunctionLikeMacro { } |
22 | | - |
23 | | -/** A standard library function like macro that contains the use of a stringize or tokenize operator should not be replaced by a function. */ |
24 | | -private class StringizeOrTokenizeMacro extends IrreplaceableFunctionLikeMacro { |
25 | | - StringizeOrTokenizeMacro() { |
26 | | - exists(TokenPastingOperator t | t.getMacro() = this) or |
27 | | - exists(StringizingOperator s | s.getMacro() = this) |
28 | | - } |
29 | | -} |
30 | | - |
31 | | -/** A standard library function like macro that should not be replaced by a function. */ |
32 | | -private class StandardLibraryFunctionLikeMacro extends IrreplaceableFunctionLikeMacro { |
33 | | - StandardLibraryFunctionLikeMacro() { Naming::Cpp14::hasStandardLibraryMacroName(this.getName()) } |
34 | | -} |
35 | | - |
36 | | -/** A function like macro invocation as an `asm` argument cannot be replaced by a function. */ |
37 | | -private class AsmArgumentInvoked extends IrreplaceableFunctionLikeMacro { |
38 | | - AsmArgumentInvoked() { |
39 | | - any(AsmStmt s).getLocation().subsumes(this.getAnInvocation().getLocation()) |
40 | | - } |
41 | | -} |
42 | | - |
43 | | -/** A macro that is only invoked with constant arguments is more likely to be compile-time evaluated than a function call so do not suggest replacement. */ |
44 | | -private class OnlyConstantArgsInvoked extends IrreplaceableFunctionLikeMacro { |
45 | | - OnlyConstantArgsInvoked() { |
46 | | - forex(MacroInvocation mi | mi = this.getAnInvocation() | |
47 | | - //int/float literals |
48 | | - mi.getUnexpandedArgument(_).regexpMatch("\\d+") |
49 | | - or |
50 | | - //char literal or string literal, which is a literal surrounded by single quotes or double quotes |
51 | | - mi.getUnexpandedArgument(_).regexpMatch("('[^']*'|\"[^\"]*\")") |
52 | | - ) |
53 | | - } |
54 | | -} |
55 | | - |
56 | | -/** A function like macro invoked to initialize an object with static storage that cannot be replaced with a function call. */ |
57 | | -private class UsedToStaticInitialize extends IrreplaceableFunctionLikeMacro { |
58 | | - UsedToStaticInitialize() { |
59 | | - any(StaticStorageDurationVariable v).getInitializer().getExpr() = |
60 | | - this.getAnInvocation().getExpr() |
61 | | - } |
62 | | -} |
63 | | - |
64 | | -/** A function like macro that is called with an argument that is an operator that cannot be replaced with a function call. */ |
65 | | -private class FunctionLikeMacroWithOperatorArgument extends IrreplaceableFunctionLikeMacro { |
66 | | - FunctionLikeMacroWithOperatorArgument() { |
67 | | - exists(MacroInvocation mi | mi.getMacro() = this | |
68 | | - mi.getUnexpandedArgument(_) = any(Operation op).getOperator() |
69 | | - ) |
70 | | - } |
71 | | -} |
| 18 | +import IrreplaceableFunctionLikeMacro |
72 | 19 |
|
73 | 20 | predicate partOfConstantExpr(MacroInvocation i) { |
74 | 21 | exists(Expr e | |
|
86 | 33 | not m.getBody().length() = 0 and |
87 | 34 | //function call not allowed in a constant expression (where constant expr is parent) |
88 | 35 | forall(MacroInvocation i | i = m.getAnInvocation() | not partOfConstantExpr(i)) |
89 | | -select m, "Macro used when function call would be preferred.", m.getBody().length() |
| 36 | +select m, "Macro used instead of a function." |
0 commit comments