Skip to content

Commit 1e0e319

Browse files
committed
CPP: Restrict to #elif, #else, #endif.
1 parent 2dad62a commit 1e0e319

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

cpp/ql/src/Documentation/CommentedOutCode.qll

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,21 @@ private predicate looksLikeCode(string line) {
4949

5050
/**
5151
* Holds if there is a preprocessor directive on the line indicated by
52-
* `f` and `line`.
52+
* `f` and `line` that we permit code comments besides. For example this
53+
* is considered acceptable:
54+
* ```
55+
* #ifdef MYMACRO
56+
* ...
57+
* #endif // #ifdef MYMACRO
58+
* ```
5359
*/
5460
private predicate preprocLine(File f, int line) {
5561
exists(PreprocessorDirective pd, Location l |
62+
(
63+
pd instanceof PreprocessorElse or
64+
pd instanceof PreprocessorElif or
65+
pd instanceof PreprocessorEndif
66+
) and
5667
pd.getLocation() = l and
5768
l.getFile() = f and
5869
l.getStartLine() = line

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
| test2.cpp:65:1:65:17 | // # pragma once | This comment appears to contain commented-out code |
1010
| test2.cpp:67:1:67:19 | /*#error"myerror"*/ | This comment appears to contain commented-out code |
1111
| test2.cpp:91:1:95:2 | /*\n#ifdef MYMACRO\n\t// ...\n#endif // #ifdef MYMACRO\n*/ | This comment appears to contain commented-out code |
12+
| test2.cpp:107:21:107:43 | // #include "config2.h" | This comment appears to contain commented-out code |
13+
| test2.cpp:115:16:115:35 | /* #ifdef MYMACRO */ | This comment appears to contain commented-out code |
1214
| test.c:2:1:2:22 | // commented out code; | This comment appears to contain commented-out code |
1315
| test.c:4:1:7:8 | // some; | This comment appears to contain commented-out code |
1416
| test.c:9:1:13:8 | // also; | This comment appears to contain commented-out code |

0 commit comments

Comments
 (0)