Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 821a7bf

Browse files
authored
Merge pull request #779 from jbj/commented-out-code-braces
C++: Relax commented-out code heuristic for `}`
2 parents 9561fda + 9c42b5f commit 821a7bf

3 files changed

Lines changed: 50 additions & 0 deletions

File tree

cpp/ql/src/Documentation/CommentedOutCode.qll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ private predicate looksLikeCode(string line) {
2020
exists(string trimmed |
2121
trimmed = line.regexpReplaceAll("(?i)(^\\s+|&#?[a-z0-9]{1,31};|\\s+$)", "") |
2222
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 ") {" (with some variation of
29+
// whitespace) 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(".*\\)\\s*\\{.*")
34+
)
2335
and not trimmed.regexpMatch("(>.*|.*[\\\\@][{}].*|(optional|repeated) .*;|.*(\\{\\{\\{|\\}\\}\\}).*|\\{[-0-9a-zA-Z]+\\})"))
2436
}
2537

cpp/ql/test/query-tests/Documentation/CommentedOutCode/CommentedOutCode.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
| test2.cpp:37:1:37:39 | // int myFunction() { return myValue; } | This comment appears to contain commented-out code |
12
| test.c:2:1:2:22 | // commented out code; | This comment appears to contain commented-out code |
23
| test.c:4:1:7:8 | // some; | This comment appears to contain commented-out code |
34
| test.c:9:1:13:8 | // also; | This comment appears to contain commented-out code |
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* This sentence contains a semicolon;
3+
* however, this doesn't make it code.
4+
*/
5+
6+
// This sentence contains a semicolon;
7+
// however, this doesn't make it code.
8+
9+
/* Mention a ';' */
10+
11+
/* Mention a '{' */
12+
13+
/* JSON example: {"foo":"bar"} */
14+
15+
/* JSON example in backticks: `{"foo":"bar"}` */
16+
17+
/* JSON example in quotes: '{"foo":"bar"}' */
18+
19+
/*
20+
* Code example: `return 0;`.
21+
*/
22+
23+
// Code example:
24+
//
25+
// return 0;
26+
27+
// Code example:
28+
//
29+
// ```
30+
// return 0;
31+
// ```
32+
33+
// { 1, 2, 3, 4 }
34+
35+
// Example: { 1, 2, 3, 4 }
36+
37+
// int myFunction() { return myValue; }

0 commit comments

Comments
 (0)