|
| 1 | +import cpp |
| 2 | +import codingstandards.cpp.Macro |
| 3 | +import codingstandards.cpp.Naming |
| 4 | + |
| 5 | +/** |
| 6 | + * Macros that cannot be replaced by functions |
| 7 | + */ |
| 8 | +abstract class IrreplaceableFunctionLikeMacro extends FunctionLikeMacro { } |
| 9 | + |
| 10 | +/** A function like macro that contains the use of a stringize or tokenize operator should not be replaced by a function. */ |
| 11 | +private class StringizeOrTokenizeMacro extends IrreplaceableFunctionLikeMacro { |
| 12 | + StringizeOrTokenizeMacro() { |
| 13 | + exists(TokenPastingOperator t | t.getMacro() = this) or |
| 14 | + exists(StringizingOperator s | s.getMacro() = this) |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +/** A standard library function like macro that should not be replaced by a function. */ |
| 19 | +private class StandardLibraryFunctionLikeMacro extends IrreplaceableFunctionLikeMacro { |
| 20 | + StandardLibraryFunctionLikeMacro() { Naming::Cpp14::hasStandardLibraryMacroName(this.getName()) } |
| 21 | +} |
| 22 | + |
| 23 | +/** A function like macro invocation as an `asm` argument cannot be replaced by a function. */ |
| 24 | +private class AsmArgumentInvoked extends IrreplaceableFunctionLikeMacro { |
| 25 | + AsmArgumentInvoked() { |
| 26 | + any(AsmStmt s).getLocation().subsumes(this.getAnInvocation().getLocation()) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +/** 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. */ |
| 31 | +private class OnlyConstantArgsInvoked extends IrreplaceableFunctionLikeMacro { |
| 32 | + OnlyConstantArgsInvoked() { |
| 33 | + forex(MacroInvocation mi | mi = this.getAnInvocation() | |
| 34 | + //int/float literals |
| 35 | + mi.getUnexpandedArgument(_).regexpMatch("\\d+") |
| 36 | + or |
| 37 | + //char literal or string literal, which is a literal surrounded by single quotes or double quotes |
| 38 | + mi.getUnexpandedArgument(_).regexpMatch("('[^']*'|\"[^\"]*\")") |
| 39 | + ) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +/** A function like macro invoked to initialize an object with static storage that cannot be replaced with a function call. */ |
| 44 | +private class UsedToStaticInitialize extends IrreplaceableFunctionLikeMacro { |
| 45 | + UsedToStaticInitialize() { |
| 46 | + any(StaticStorageDurationVariable v).getInitializer().getExpr() = |
| 47 | + this.getAnInvocation().getExpr() |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/** A function like macro that is called with an argument that is an operator that cannot be replaced with a function call. */ |
| 52 | +private class FunctionLikeMacroWithOperatorArgument extends IrreplaceableFunctionLikeMacro { |
| 53 | + FunctionLikeMacroWithOperatorArgument() { |
| 54 | + exists(MacroInvocation mi | mi.getMacro() = this | |
| 55 | + mi.getUnexpandedArgument(_) = any(Operation op).getOperator() |
| 56 | + ) |
| 57 | + } |
| 58 | +} |
0 commit comments