|
| 1 | +import cpp |
| 2 | + |
| 3 | +private string getATgMathMacroName(boolean allowComplex) { |
| 4 | + allowComplex = true and |
| 5 | + result = |
| 6 | + [ |
| 7 | + "acos", "acosh", "asin", "asinh", "atan", "atanh", "carg", "cimag", "conj", "cos", "cosh", |
| 8 | + "cproj", "creal", "exp", "fabs", "log", "pow", "sin", "sinh", "sqrt", "tan", "tanh" |
| 9 | + ] |
| 10 | + or |
| 11 | + allowComplex = false and |
| 12 | + result = |
| 13 | + [ |
| 14 | + "atan2", "cbrt", "ceil", "copysign", "erf", "erfc", "exp2", "expm1", "fdim", "floor", "fma", |
| 15 | + "fmax", "fmin", "fmod", "frexp", "hypot", "ilogb", "ldexp", "lgamma", "llrint", "llround", |
| 16 | + "log10", "log1p", "log2", "logb", "lrint", "lround", "nearbyint", "nextafter", "nexttoward", |
| 17 | + "remainder", "remquo", "rint", "round", "scalbn", "scalbln", "tgamma", "trunc", |
| 18 | + ] |
| 19 | +} |
| 20 | + |
| 21 | +private predicate hasOutputArgument(string macroName, int index) { |
| 22 | + macroName = "frexp" and index = 1 |
| 23 | + or |
| 24 | + macroName = "remquo" and index = 2 |
| 25 | +} |
| 26 | + |
| 27 | +class TgMathInvocation extends MacroInvocation { |
| 28 | + Call call; |
| 29 | + boolean allowComplex; |
| 30 | + |
| 31 | + TgMathInvocation() { |
| 32 | + this.getMacro().getName() = getATgMathMacroName(allowComplex) and |
| 33 | + call = getBestCallInExpansion(this) |
| 34 | + } |
| 35 | + |
| 36 | + Expr getOperandArgument(int i) { |
| 37 | + result = call.getArgument(i) and |
| 38 | + not hasOutputArgument(call.getTarget().getName(), i) |
| 39 | + } |
| 40 | + |
| 41 | + int getNumberOfOperandArguments() { |
| 42 | + result = call.getNumberOfArguments() - count(int i | hasOutputArgument(getMacroName(), i)) |
| 43 | + } |
| 44 | + |
| 45 | + Expr getAnOperandArgument() { result = getOperandArgument(_) } |
| 46 | + |
| 47 | + predicate allowsComplex() { allowComplex = true } |
| 48 | +} |
| 49 | + |
| 50 | +private Call getACallInExpansion(MacroInvocation mi) { result = mi.getAnExpandedElement() } |
| 51 | + |
| 52 | +private Call getNameMatchedCallInExpansion(MacroInvocation mi) { |
| 53 | + result = getACallInExpansion(mi) and result.getTarget().getName() = mi.getMacroName() |
| 54 | +} |
| 55 | + |
| 56 | +private Call getBestCallInExpansion(MacroInvocation mi) { |
| 57 | + count(getACallInExpansion(mi)) = 1 and result = getACallInExpansion(mi) |
| 58 | + or |
| 59 | + count(getNameMatchedCallInExpansion(mi)) = 1 and result = getNameMatchedCallInExpansion(mi) |
| 60 | + or |
| 61 | + count(getNameMatchedCallInExpansion(mi)) > 1 and |
| 62 | + result = rank[1](Call c | c = getACallInExpansion(mi) | c order by c.getTarget().getName()) |
| 63 | +} |
0 commit comments