|
| 1 | +/** |
| 2 | + * A module for considering whether a result occurs in all copies of the code at a given location. |
| 3 | + * |
| 4 | + * Multiple copies of an element at the same location can occur for two main reasons: |
| 5 | + * 1. Instantiations of a template |
| 6 | + * 2. Re-compilation of a file under a different context |
| 7 | + * This module helps ensure that a particular condition holds for all copies of a particular logical |
| 8 | + * element. For example, this can be used to determine whether a line of code is dead in all copies |
| 9 | + * of a piece of code. |
| 10 | + * |
| 11 | + * This module is parameterized by a set of _candidate_ elements in the program. For each candidate |
| 12 | + * element, we determine whether all other elements in the same element set that occur at the same |
| 13 | + * location in the program are also part of the same set, ignoring any results generated by macros. |
| 14 | + * |
| 15 | + * We do so by reporting a new type of result, `LogicalResultElement`, which represents a logical result |
| 16 | + * where all instances of a element at a given location are considered to be part of the same set. |
| 17 | + */ |
| 18 | + |
| 19 | +import cpp |
| 20 | + |
| 21 | +/** |
| 22 | + * Holds if the `Element` `e` is not within a macro expansion, i.e. generated by a macro, but not |
| 23 | + * the outermost `Element` or `Expr` generated by the macro. |
| 24 | + */ |
| 25 | +predicate isNotWithinMacroExpansion(Element e) { |
| 26 | + not e.isInMacroExpansion() |
| 27 | + or |
| 28 | + exists(MacroInvocation mi | |
| 29 | + mi.getStmt() = e |
| 30 | + or |
| 31 | + mi.getExpr() = e |
| 32 | + or |
| 33 | + mi.getStmt().(ExprStmt).getExpr() = e |
| 34 | + | |
| 35 | + not exists(mi.getParentInvocation()) |
| 36 | + ) |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * A type representing a set of Element's in the program that satisfy some condition. |
| 41 | + * |
| 42 | + * `HoldsForAllCopies<T>::LogicalResultElement` will represent an element in this set |
| 43 | + * iff all copies of that element satisfy the condition. |
| 44 | + */ |
| 45 | +signature class CandidateElementSig extends Element; |
| 46 | + |
| 47 | +/** The super set of relevant elements. */ |
| 48 | +signature class ElementSetSig extends Element; |
| 49 | + |
| 50 | +/** |
| 51 | + * A module for considering whether a result occurs in all copies of the code at a given location. |
| 52 | + */ |
| 53 | +module HoldsForAllCopies<CandidateElementSig CandidateElement, ElementSetSig ElementSet> { |
| 54 | + private predicate hasLocation( |
| 55 | + ElementSet s, string filepath, int startline, int startcolumn, int endline, int endcolumn |
| 56 | + ) { |
| 57 | + s.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) |
| 58 | + } |
| 59 | + |
| 60 | + final private class MyElement = ElementSet; |
| 61 | + |
| 62 | + /** |
| 63 | + * A `Element` that appears at the same location as a candidate element. |
| 64 | + */ |
| 65 | + private class RelevantElement extends MyElement { |
| 66 | + CandidateElement e; |
| 67 | + |
| 68 | + RelevantElement() { |
| 69 | + exists(string filepath, int startline, int startcolumn, int endline, int endcolumn | |
| 70 | + hasLocation(this, filepath, startline, startcolumn, endline, endcolumn) and |
| 71 | + hasLocation(e, filepath, startline, startcolumn, endline, endcolumn) |
| 72 | + ) and |
| 73 | + // Not within a macro expansion, as we cannot match up instances by location in that |
| 74 | + // case |
| 75 | + isNotWithinMacroExpansion(this) and |
| 76 | + // Ignore catch handlers, as they occur at the same location as the catch block |
| 77 | + not this instanceof Handler |
| 78 | + } |
| 79 | + |
| 80 | + CandidateElement getCandidateElement() { result = e } |
| 81 | + } |
| 82 | + |
| 83 | + newtype TResultElements = |
| 84 | + TLogicalResultElement( |
| 85 | + string filepath, int startline, int startcolumn, int endline, int endcolumn |
| 86 | + ) { |
| 87 | + exists(CandidateElement s | |
| 88 | + // Only consider candidates where we can match up the location |
| 89 | + isNotWithinMacroExpansion(s) and |
| 90 | + hasLocation(s, filepath, startline, startcolumn, endline, endcolumn) and |
| 91 | + // All relevant elements that occur at the same location are candidates |
| 92 | + forex(RelevantElement relevantElement | s = relevantElement.getCandidateElement() | |
| 93 | + relevantElement instanceof CandidateElement |
| 94 | + ) |
| 95 | + ) |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * A logical result element representing all copies of an element that occur at the same |
| 100 | + * location, iff they all belong to the `CandidateElement` set. |
| 101 | + */ |
| 102 | + class LogicalResultElement extends TLogicalResultElement { |
| 103 | + predicate hasLocationInfo( |
| 104 | + string filepath, int startline, int startcolumn, int endline, int endcolumn |
| 105 | + ) { |
| 106 | + this = TLogicalResultElement(filepath, startline, startcolumn, endline, endcolumn) |
| 107 | + } |
| 108 | + |
| 109 | + /** Gets a copy instance of this logical result element. */ |
| 110 | + CandidateElement getAnElementInstance() { |
| 111 | + exists(string filepath, int startline, int startcolumn, int endline, int endcolumn | |
| 112 | + this = TLogicalResultElement(filepath, startline, startcolumn, endline, endcolumn) and |
| 113 | + hasLocation(result, filepath, startline, startcolumn, endline, endcolumn) |
| 114 | + ) |
| 115 | + } |
| 116 | + |
| 117 | + string toString() { result = getAnElementInstance().toString() } |
| 118 | + } |
| 119 | +} |
0 commit comments