Skip to content

Commit f432f1a

Browse files
committed
CPP: Autoformat CommentedOutCode.qll.
1 parent 9224113 commit f432f1a

File tree

1 file changed

+83
-79
lines changed

1 file changed

+83
-79
lines changed

cpp/ql/src/Documentation/CommentedOutCode.qll

Lines changed: 83 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,33 @@ import cpp
1717
*/
1818
bindingset[line]
1919
private predicate looksLikeCode(string line) {
20-
exists(string trimmed |
21-
trimmed = line.regexpReplaceAll("(?i)(^\\s+|&#?[a-z0-9]{1,31};|\\s+$)", "") |
22-
trimmed.regexpMatch(".*[{};]")
23-
and (
24-
// If this line looks like code because it ends with a closing
25-
// brace that's preceded by something other than whitespace ...
26-
trimmed.regexpMatch(".*.\\}")
27-
implies
28-
// ... then there has to be ") {" (or some variation)
29-
// on the line, suggesting it's a statement like `if`
30-
// or a function declaration. Otherwise it's likely to be a
31-
// benign use of braces such as a JSON example or explanatory
32-
// pseudocode.
33-
trimmed.regexpMatch(".*(\\)|const|volatile|override|final|noexcept|&)\\s*\\{.*")
34-
)
35-
and not trimmed.regexpMatch("(>.*|.*[\\\\@][{}].*|(optional|repeated) .*;|.*(\\{\\{\\{|\\}\\}\\}).*|\\{[-0-9a-zA-Z]+\\})"))
20+
exists(string trimmed |
21+
trimmed = line.regexpReplaceAll("(?i)(^\\s+|&#?[a-z0-9]{1,31};|\\s+$)", "")
22+
|
23+
trimmed.regexpMatch(".*[{};]") and
24+
(
25+
// If this line looks like code because it ends with a closing
26+
// brace that's preceded by something other than whitespace ...
27+
trimmed.regexpMatch(".*.\\}")
28+
implies
29+
// ... then there has to be ") {" (or some variation)
30+
// on the line, suggesting it's a statement like `if`
31+
// or a function declaration. Otherwise it's likely to be a
32+
// benign use of braces such as a JSON example or explanatory
33+
// pseudocode.
34+
trimmed.regexpMatch(".*(\\)|const|volatile|override|final|noexcept|&)\\s*\\{.*")
35+
) and
36+
not trimmed
37+
.regexpMatch("(>.*|.*[\\\\@][{}].*|(optional|repeated) .*;|.*(\\{\\{\\{|\\}\\}\\}).*|\\{[-0-9a-zA-Z]+\\})")
38+
)
3639
}
3740

3841
/**
3942
* The line of a C++-style comment within its file `f`.
4043
*/
4144
private int lineInFile(CppStyleComment c, File f) {
42-
f = c.getFile() and
43-
result = c.getLocation().getStartLine()
45+
f = c.getFile() and
46+
result = c.getLocation().getStartLine()
4447
}
4548

4649
/**
@@ -53,11 +56,12 @@ private int lineInFile(CppStyleComment c, File f) {
5356
* without line comments would increase the line number without increasing
5457
* the rank and thus force a change of block ID.
5558
*/
56-
private pragma[nomagic] int commentLineBlockID(File f, int line) {
57-
exists(int lineRank |
58-
line = rank[lineRank](lineInFile(_, f)) and
59-
result = line - lineRank
60-
)
59+
pragma[nomagic]
60+
private int commentLineBlockID(File f, int line) {
61+
exists(int lineRank |
62+
line = rank[lineRank](lineInFile(_, f)) and
63+
result = line - lineRank
64+
)
6165
}
6266

6367
/**
@@ -67,80 +71,80 @@ private pragma[nomagic] int commentLineBlockID(File f, int line) {
6771
* for separate runs.
6872
*/
6973
private int commentId(CppStyleComment c, File f, int line) {
70-
result = commentLineBlockID(f, line) and
71-
line = lineInFile(c, f)
74+
result = commentLineBlockID(f, line) and
75+
line = lineInFile(c, f)
7276
}
7377

7478
/**
7579
* A contiguous block of comments.
7680
*/
7781
class CommentBlock extends Comment {
78-
CommentBlock() {
79-
this instanceof CppStyleComment implies
80-
not exists(CppStyleComment pred, File f | lineInFile(pred, f) + 1 = lineInFile(this, f))
81-
}
82+
CommentBlock() {
83+
this instanceof CppStyleComment
84+
implies
85+
not exists(CppStyleComment pred, File f | lineInFile(pred, f) + 1 = lineInFile(this, f))
86+
}
8287

83-
/**
84-
* Gets the `i`th comment associated with this comment block.
85-
*/
86-
Comment getComment(int i) {
87-
(i = 0 and result = this) or
88-
exists(File f, int thisLine, int resultLine |
89-
commentId(this, f, thisLine) = commentId(result, f, resultLine) |
90-
i = resultLine - thisLine
91-
)
92-
}
88+
/**
89+
* Gets the `i`th comment associated with this comment block.
90+
*/
91+
Comment getComment(int i) {
92+
i = 0 and result = this
93+
or
94+
exists(File f, int thisLine, int resultLine |
95+
commentId(this, f, thisLine) = commentId(result, f, resultLine)
96+
|
97+
i = resultLine - thisLine
98+
)
99+
}
93100

94-
Comment lastComment() {
95-
result = this.getComment(max(int i | exists(this.getComment(i))))
96-
}
101+
Comment lastComment() { result = this.getComment(max(int i | exists(this.getComment(i)))) }
97102

98-
string getLine(int i) {
99-
this instanceof CStyleComment and result = this.getContents().regexpCapture("(?s)/\\*+(.*)\\*+/", 1).splitAt("\n", i)
100-
or
101-
this instanceof CppStyleComment and result = this.getComment(i).getContents().suffix(2)
102-
}
103+
string getLine(int i) {
104+
this instanceof CStyleComment and
105+
result = this.getContents().regexpCapture("(?s)/\\*+(.*)\\*+/", 1).splitAt("\n", i)
106+
or
107+
this instanceof CppStyleComment and result = this.getComment(i).getContents().suffix(2)
108+
}
103109

104-
int numLines() {
105-
result = strictcount(int i, string line | line = this.getLine(i) and line.trim() != "")
106-
}
110+
int numLines() {
111+
result = strictcount(int i, string line | line = this.getLine(i) and line.trim() != "")
112+
}
107113

108-
int numCodeLines() {
109-
result = strictcount(int i, string line | line = this.getLine(i) and looksLikeCode(line))
110-
}
114+
int numCodeLines() {
115+
result = strictcount(int i, string line | line = this.getLine(i) and looksLikeCode(line))
116+
}
111117

112-
predicate isDocumentation() {
113-
// If a C-style comment starts each line with a *, then it's
114-
// probably documentation rather than code.
115-
this instanceof CStyleComment and
116-
forex(int i | i in [1 .. this.numLines() - 1]
117-
| this.getLine(i).trim().matches("*%"))
118-
}
118+
predicate isDocumentation() {
119+
// If a C-style comment starts each line with a *, then it's
120+
// probably documentation rather than code.
121+
this instanceof CStyleComment and
122+
forex(int i | i in [1 .. this.numLines() - 1] | this.getLine(i).trim().matches("*%"))
123+
}
119124

120-
predicate isCommentedOutCode() {
121-
not this.isDocumentation() and
122-
this.numCodeLines().(float) / this.numLines().(float) > 0.5
123-
}
125+
predicate isCommentedOutCode() {
126+
not this.isDocumentation() and
127+
this.numCodeLines().(float) / this.numLines().(float) > 0.5
128+
}
124129

125-
/**
126-
* Holds if this element is at the specified location.
127-
* The location spans column `startcolumn` of line `startline` to
128-
* column `endcolumn` of line `endline` in file `filepath`.
129-
* For more information, see
130-
* [Locations](https://help.semmle.com/QL/learn-ql/ql/locations.html).
131-
*/
132-
predicate hasLocationInfo(string filepath, int startline, int startcolumn, int endline, int endcolumn) {
133-
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, _, _) and
134-
this.lastComment().getLocation().hasLocationInfo(_, _, _, endline, endcolumn)
135-
}
130+
/**
131+
* Holds if this element is at the specified location.
132+
* The location spans column `startcolumn` of line `startline` to
133+
* column `endcolumn` of line `endline` in file `filepath`.
134+
* For more information, see
135+
* [Locations](https://help.semmle.com/QL/learn-ql/ql/locations.html).
136+
*/
137+
predicate hasLocationInfo(
138+
string filepath, int startline, int startcolumn, int endline, int endcolumn
139+
) {
140+
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, _, _) and
141+
this.lastComment().getLocation().hasLocationInfo(_, _, _, endline, endcolumn)
142+
}
136143
}
137144

138145
/**
139146
* A piece of commented-out code, identified using heuristics
140147
*/
141148
class CommentedOutCode extends CommentBlock {
142-
CommentedOutCode() {
143-
this.isCommentedOutCode()
144-
}
149+
CommentedOutCode() { this.isCommentedOutCode() }
145150
}
146-

0 commit comments

Comments
 (0)