|
| 1 | +/** |
| 2 | + * Provides classes and predicates for identifying C/C++ comments that look like code. |
| 3 | + */ |
| 4 | + |
1 | 5 | import cpp
|
2 | 6 |
|
3 | 7 | /**
|
@@ -137,30 +141,52 @@ class CommentBlock extends Comment {
|
137 | 141 | )
|
138 | 142 | }
|
139 | 143 |
|
| 144 | + /** |
| 145 | + * Gets the last comment associated with this comment block. |
| 146 | + */ |
140 | 147 | Comment lastComment() { result = this.getComment(max(int i | exists(this.getComment(i)))) }
|
141 | 148 |
|
| 149 | + /** |
| 150 | + * Gets the contents of the `i`'th comment associated with this comment block. |
| 151 | + */ |
142 | 152 | string getLine(int i) {
|
143 | 153 | this instanceof CStyleComment and
|
144 | 154 | result = this.getContents().regexpCapture("(?s)/\\*+(.*)\\*+/", 1).splitAt("\n", i)
|
145 | 155 | or
|
146 | 156 | this instanceof CppStyleComment and result = this.getComment(i).getContents().suffix(2)
|
147 | 157 | }
|
148 | 158 |
|
| 159 | + /** |
| 160 | + * Gets the number of lines in the comments associated with this comment block. |
| 161 | + */ |
149 | 162 | int numLines() {
|
150 | 163 | result = strictcount(int i, string line | line = this.getLine(i) and line.trim() != "")
|
151 | 164 | }
|
152 | 165 |
|
| 166 | + /** |
| 167 | + * Gets the number of lines that look like code in the comments associated with this comment block. |
| 168 | + */ |
153 | 169 | int numCodeLines() {
|
154 | 170 | result = strictcount(int i, string line | line = this.getLine(i) and looksLikeCode(line))
|
155 | 171 | }
|
156 | 172 |
|
| 173 | + /** |
| 174 | + * Holds if the comment block is a C-style comment, and each |
| 175 | + * comment line starts with a *. |
| 176 | + */ |
157 | 177 | predicate isDocumentation() {
|
158 | 178 | // If a C-style comment starts each line with a *, then it's
|
159 | 179 | // probably documentation rather than code.
|
160 | 180 | this instanceof CStyleComment and
|
161 | 181 | forex(int i | i in [1 .. this.numLines() - 1] | this.getLine(i).trim().matches("*%"))
|
162 | 182 | }
|
163 | 183 |
|
| 184 | + /** |
| 185 | + * Holds if this comment block looks like code that has been commented out. Specifically: |
| 186 | + * 1. It does not look like documentation (see `isDocumentation`). |
| 187 | + * 2. It is not in a header file without any declaration entries or top level declarations. |
| 188 | + * 3. More than half of the lines in the comment block look like code. |
| 189 | + */ |
164 | 190 | predicate isCommentedOutCode() {
|
165 | 191 | not this.isDocumentation() and
|
166 | 192 | not this.getFile().(HeaderFile).noTopLevelCode() and
|
|
0 commit comments