Skip to content

Commit 48fff33

Browse files
committed
CPP: Detect commented preprocessor code.
1 parent 4d67bd3 commit 48fff33

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

cpp/ql/src/Documentation/CommentedOutCode.qll

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,26 @@ private predicate looksLikeCode(string line) {
1212
// * HTML entities in hexadecimal notation (e.g. 灟)
1313
trimmed = line.regexpReplaceAll("(?i)(^\\s+|&#?[a-z0-9]{1,31};|\\s+$)", "")
1414
|
15-
// Match comment lines ending with '{', '}' or ';'
16-
trimmed.regexpMatch(".*[{};]") and
1715
(
18-
// If this line looks like code because it ends with a closing
19-
// brace that's preceded by something other than whitespace ...
20-
trimmed.regexpMatch(".*.\\}")
21-
implies
22-
// ... then there has to be ") {" (or some variation)
23-
// on the line, suggesting it's a statement like `if`
24-
// or a function declaration. Otherwise it's likely to be a
25-
// benign use of braces such as a JSON example or explanatory
26-
// pseudocode.
27-
trimmed.regexpMatch(".*(\\)|const|volatile|override|final|noexcept|&)\\s*\\{.*")
16+
(
17+
// Match comment lines ending with '{', '}' or ';'
18+
trimmed.regexpMatch(".*[{};]") and
19+
(
20+
// If this line looks like code because it ends with a closing
21+
// brace that's preceded by something other than whitespace ...
22+
trimmed.regexpMatch(".*.\\}")
23+
implies
24+
// ... then there has to be ") {" (or some variation)
25+
// on the line, suggesting it's a statement like `if`
26+
// or a function declaration. Otherwise it's likely to be a
27+
// benign use of braces such as a JSON example or explanatory
28+
// pseudocode.
29+
trimmed.regexpMatch(".*(\\)|const|volatile|override|final|noexcept|&)\\s*\\{.*")
30+
)
31+
) or (
32+
// Match comment lines that look like preprocessor code
33+
trimmed.regexpMatch("#(include|define|undef|if|ifdef|ifndef|elif|else|endif|error)(\\s.*|)")
34+
)
2835
) and (
2936
// Exclude lines that start with '>' or contain '@{' or '@}'.
3037
// To account for the code generated by protobuf, we also insist that the comment

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
| test2.cpp:37:1:37:39 | // int myFunction() { return myValue; } | This comment appears to contain commented-out code |
22
| test2.cpp:39:1:39:45 | // int myFunction() const { return myValue; } | This comment appears to contain commented-out code |
33
| test2.cpp:41:1:41:54 | // int myFunction() const noexcept { return myValue; } | This comment appears to contain commented-out code |
4+
| test2.cpp:43:1:43:18 | // #define MYMACRO | This comment appears to contain commented-out code |
5+
| test2.cpp:45:1:45:23 | // #include "include.h" | This comment appears to contain commented-out code |
6+
| test2.cpp:47:1:51:2 | /*\n#ifdef\nvoid myFunction();\n#endif\n*/ | This comment appears to contain commented-out code |
47
| test.c:2:1:2:22 | // commented out code; | This comment appears to contain commented-out code |
58
| test.c:4:1:7:8 | // some; | This comment appears to contain commented-out code |
69
| test.c:9:1:13:8 | // also; | This comment appears to contain commented-out code |

0 commit comments

Comments
 (0)