|
| 1 | +import cpp |
| 2 | +import codingstandards.cpp.Macro |
| 3 | +import codingstandards.cpp.MatchingParenthesis |
| 4 | + |
| 5 | +string genericRegexp() { result = "\\b_Generic\\s*\\(\\s*(.+),.*" } |
| 6 | + |
| 7 | +bindingset[input] |
| 8 | +string deparenthesize(string input) { |
| 9 | + input = "(" + result + ")" and |
| 10 | + result = input.substring(1, input.length() - 1) |
| 11 | +} |
| 12 | + |
| 13 | +class GenericMacro extends Macro { |
| 14 | + string ctrlExpr; |
| 15 | + |
| 16 | + GenericMacro() { ctrlExpr = getBody().regexpCapture(genericRegexp(), 1).trim() } |
| 17 | + |
| 18 | + string getAParameter() { result = this.(FunctionLikeMacro).getAParameter() } |
| 19 | + |
| 20 | + string getControllingExprString() { |
| 21 | + if exists(string s | s = deparenthesize(ctrlExpr)) |
| 22 | + then result = deparenthesize(ctrlExpr).trim() |
| 23 | + else result = ctrlExpr |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Whether the controlling expression of the `_Generic` expr in this macro's controlling |
| 28 | + * expression refers to one of this macro's parameters. |
| 29 | + */ |
| 30 | + predicate hasControllingExprFromMacroParameter() { |
| 31 | + getControllingExprString().matches(getAParameter()) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +class GenericMacroString extends string { |
| 36 | + GenericMacroString() { this = any(Macro m).getBody() and this.matches("%_Generic%") } |
| 37 | +} |
| 38 | + |
| 39 | +import MatchingParenthesis<GenericMacroString> |
| 40 | + |
| 41 | +class ParsedGenericMacro extends Macro { |
| 42 | + ParsedRoot macroBody; |
| 43 | + Parsed genericBody; |
| 44 | + string beforeGenericBody; |
| 45 | + string afterGenericBody; |
| 46 | + |
| 47 | + ParsedGenericMacro() { |
| 48 | + macroBody.getInputString() = this.getBody() and |
| 49 | + exists(ParsedText genericText | |
| 50 | + genericText.getText().matches("%_Generic%") and |
| 51 | + genericBody = genericText.getParent().getChild(genericText.getChildIdx() + 1) and |
| 52 | + genericBody.getRoot() = macroBody |
| 53 | + ) and |
| 54 | + beforeGenericBody = |
| 55 | + textFrom(macroBody.getStartToken(), genericBody.getStartToken().getPrevious()) and |
| 56 | + ( |
| 57 | + if exists(genericBody.getEndToken().getNext()) |
| 58 | + then afterGenericBody = textFrom(genericBody.getEndToken().getNext(), macroBody.getEndToken()) |
| 59 | + else afterGenericBody = "" |
| 60 | + ) |
| 61 | + } |
| 62 | + |
| 63 | + string getAParameter() { result = this.(FunctionLikeMacro).getAParameter() } |
| 64 | + |
| 65 | + int getAParsedGenericCommaSeparatorOffset() { |
| 66 | + exists(ParsedText text | |
| 67 | + text.getParent() = genericBody and |
| 68 | + result = text.getStartToken().getStartPos() + text.getText().indexOf(",") |
| 69 | + ) |
| 70 | + } |
| 71 | + |
| 72 | + int getAParsedGenericColonSeparatorOffset() { |
| 73 | + exists(ParsedText text | |
| 74 | + text.getParent() = genericBody and |
| 75 | + result = text.getStartToken().getStartPos() + text.getText().indexOf(":") |
| 76 | + ) |
| 77 | + } |
| 78 | + |
| 79 | + int getParsedGenericCommaSeparatorOffset(int i) { |
| 80 | + result = rank[i](int index | index = getAParsedGenericCommaSeparatorOffset()) |
| 81 | + } |
| 82 | + |
| 83 | + bindingset[start, end] |
| 84 | + int getParsedGenericColon(int start, int end) { |
| 85 | + result = |
| 86 | + min(int offset | |
| 87 | + offset = getAParsedGenericColonSeparatorOffset() and |
| 88 | + offset >= start and |
| 89 | + offset <= end |
| 90 | + ) |
| 91 | + } |
| 92 | + |
| 93 | + predicate hasParsedFullSelectionRange(int idx, int start, int end) { |
| 94 | + idx = 1 and |
| 95 | + start = genericBody.getStartToken().getEndPos() and |
| 96 | + end = getParsedGenericCommaSeparatorOffset(idx) |
| 97 | + or |
| 98 | + not exists(getParsedGenericCommaSeparatorOffset(idx)) and |
| 99 | + start = getParsedGenericCommaSeparatorOffset(idx - 1) and |
| 100 | + end = genericBody.getEndToken().getStartPos() |
| 101 | + or |
| 102 | + start = getParsedGenericCommaSeparatorOffset(idx - 1) and |
| 103 | + end = getParsedGenericCommaSeparatorOffset(idx) |
| 104 | + } |
| 105 | + |
| 106 | + string getSelectionString(int idx) { |
| 107 | + exists(int start, int rawStart, int end | |
| 108 | + hasParsedFullSelectionRange(idx, rawStart, end) and |
| 109 | + ( |
| 110 | + if exists(getParsedGenericColon(rawStart, end)) |
| 111 | + then start = getParsedGenericColon(rawStart, end) |
| 112 | + else start = rawStart |
| 113 | + ) and |
| 114 | + result = genericBody.getInputString().substring(start, end) |
| 115 | + ) |
| 116 | + } |
| 117 | + |
| 118 | + string getControllingExprString() { result = getSelectionString(1).trim() } |
| 119 | + |
| 120 | + bindingset[str, word] |
| 121 | + private int countWordInString(string word, string str) { |
| 122 | + result = |
| 123 | + max(int occurrence | |
| 124 | + exists(str.regexpFind("\\b" + word + "\\b", occurrence, _)) or occurrence = -1 |
| 125 | + | |
| 126 | + occurrence + 1 |
| 127 | + ) |
| 128 | + } |
| 129 | + |
| 130 | + int expansionsOutsideExpr(string parameter) { |
| 131 | + parameter = getAParameter() and |
| 132 | + result = |
| 133 | + countWordInString(parameter, beforeGenericBody) + |
| 134 | + countWordInString(parameter, afterGenericBody) |
| 135 | + } |
| 136 | + |
| 137 | + int expansionsInsideSelection(string parameter, int idx) { |
| 138 | + parameter = getAParameter() and |
| 139 | + result = countWordInString(parameter, getSelectionString(idx)) |
| 140 | + } |
| 141 | + |
| 142 | + int expansionsInsideControllingExpr(string parameter) { |
| 143 | + result = expansionsInsideSelection(parameter, 1) |
| 144 | + } |
| 145 | + |
| 146 | + int expansionsInsideAssociation(string parameter, int idx) { |
| 147 | + not idx = 0 and |
| 148 | + result = expansionsInsideSelection(parameter, idx + 1) |
| 149 | + } |
| 150 | +} |
0 commit comments