Skip to content

Commit 4f5b8f7

Browse files
authored
Merge pull request github#3430 from MathiasVP/comments-about-comments
C++: Add QLDoc to CaptionedComments.qll and CommentedOutCode.qll
2 parents 91229f8 + 715fa9e commit 4f5b8f7

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

cpp/ql/src/Documentation/CaptionedComments.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
import cpp
66

7+
/**
8+
* Gets a string representation of the comment `c` containing the caption 'TODO' or 'FIXME'.
9+
* If `c` spans multiple lines, all lines after the first are abbreviated as [...].
10+
*/
711
string getCommentTextCaptioned(Comment c, string caption) {
812
(caption = "TODO" or caption = "FIXME") and
913
exists(

cpp/ql/src/Documentation/CommentedOutCode.qll

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* Provides classes and predicates for identifying C/C++ comments that look like code.
3+
*/
4+
15
import cpp
26

37
/**
@@ -137,30 +141,52 @@ class CommentBlock extends Comment {
137141
)
138142
}
139143

144+
/**
145+
* Gets the last comment associated with this comment block.
146+
*/
140147
Comment lastComment() { result = this.getComment(max(int i | exists(this.getComment(i)))) }
141148

149+
/**
150+
* Gets the contents of the `i`'th comment associated with this comment block.
151+
*/
142152
string getLine(int i) {
143153
this instanceof CStyleComment and
144154
result = this.getContents().regexpCapture("(?s)/\\*+(.*)\\*+/", 1).splitAt("\n", i)
145155
or
146156
this instanceof CppStyleComment and result = this.getComment(i).getContents().suffix(2)
147157
}
148158

159+
/**
160+
* Gets the number of lines in the comments associated with this comment block.
161+
*/
149162
int numLines() {
150163
result = strictcount(int i, string line | line = this.getLine(i) and line.trim() != "")
151164
}
152165

166+
/**
167+
* Gets the number of lines that look like code in the comments associated with this comment block.
168+
*/
153169
int numCodeLines() {
154170
result = strictcount(int i, string line | line = this.getLine(i) and looksLikeCode(line))
155171
}
156172

173+
/**
174+
* Holds if the comment block is a C-style comment, and each
175+
* comment line starts with a *.
176+
*/
157177
predicate isDocumentation() {
158178
// If a C-style comment starts each line with a *, then it's
159179
// probably documentation rather than code.
160180
this instanceof CStyleComment and
161181
forex(int i | i in [1 .. this.numLines() - 1] | this.getLine(i).trim().matches("*%"))
162182
}
163183

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+
*/
164190
predicate isCommentedOutCode() {
165191
not this.isDocumentation() and
166192
not this.getFile().(HeaderFile).noTopLevelCode() and

0 commit comments

Comments
 (0)