|
16 | 16 | import cpp |
17 | 17 | import codingstandards.cpp.misra |
18 | 18 | import codingstandards.cpp.AlertReporting |
| 19 | +import codingstandards.cpp.BannedFunctions |
19 | 20 |
|
20 | | -predicate isTerminatingFunction(Function f, string functionName) { |
21 | | - functionName = f.getName() and |
22 | | - ( |
23 | | - functionName in ["abort", "exit", "_Exit", "quick_exit"] and |
24 | | - (f.hasQualifiedName("", functionName) or f.hasQualifiedName("std", functionName)) |
| 21 | +class TerminatingFunction extends Function { |
| 22 | + TerminatingFunction() { |
| 23 | + this.hasQualifiedName(["", "std"], ["abort", "exit", "_Exit", "quick_exit"]) |
25 | 24 | or |
26 | 25 | // std::terminate does not occur in the global namespace. |
27 | | - functionName = "terminate" and f.hasQualifiedName("std", functionName) |
28 | | - ) |
29 | | -} |
30 | | - |
31 | | -class TerminatingFunctionUse extends Expr { |
32 | | - string action; |
33 | | - string functionName; |
34 | | - |
35 | | - TerminatingFunctionUse() { |
36 | | - exists(Function f | isTerminatingFunction(f, functionName) | |
37 | | - this.(FunctionCall).getTarget() = f and |
38 | | - action = "Call to" |
39 | | - or |
40 | | - this.(FunctionAccess).getTarget() = f and |
41 | | - action = "Address taken for" |
42 | | - ) |
43 | | - } |
44 | | - |
45 | | - string getFunctionName() { result = functionName } |
46 | | - |
47 | | - string getAction() { result = action } |
48 | | - |
49 | | - Element getPrimaryElement() { |
50 | | - // If this is defined in a macro in the users source location, then report the macro |
51 | | - // expansion, otherwise report the element itself. This ensures that we always report |
52 | | - // the use of the terminating function, but combine usages when the macro is defined |
53 | | - // by the user. |
54 | | - exists(Element e | e = MacroUnwrapper<TerminatingFunctionUse>::unwrapElement(this) | |
55 | | - if exists(e.getFile().getRelativePath()) then result = e else result = this |
56 | | - ) |
| 26 | + this.hasQualifiedName("std", "terminate") |
57 | 27 | } |
58 | 28 | } |
59 | 29 |
|
60 | | -predicate isInAssertMacroInvocation(TerminatingFunctionUse use) { |
| 30 | +predicate isInAssertMacroInvocation(BannedFunctions<TerminatingFunction>::UseExpr use) { |
61 | 31 | exists(MacroInvocation mi | |
62 | 32 | mi.getMacroName() = "assert" and |
63 | 33 | mi.getAnExpandedElement() = use |
64 | 34 | ) |
65 | 35 | } |
66 | 36 |
|
67 | | -from TerminatingFunctionUse use |
| 37 | +from BannedFunctions<TerminatingFunction>::UseExpr use |
68 | 38 | where |
69 | 39 | not isExcluded(use, BannedAPIsPackage::avoidProgramTerminatingFunctionsQuery()) and |
70 | 40 | // Exclude the uses in the assert macro |
|
0 commit comments