Skip to content

Commit 9e6b178

Browse files
committed
CPP: Resolve #endif FPs.
1 parent 4beb775 commit 9e6b178

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

cpp/ql/src/Documentation/CommentedOutCode.qll

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,27 @@ private predicate looksLikeCode(string line) {
4747
)
4848
}
4949

50+
/**
51+
* Holds if there is a preprocessor directive on the line indicated by
52+
* `f` and `line`.
53+
*/
54+
private predicate preprocLine(File f, int line) {
55+
exists(PreprocessorDirective pd, Location l |
56+
pd.getLocation() = l and
57+
l.getFile() = f and
58+
l.getStartLine() = line
59+
)
60+
}
61+
5062
/**
5163
* The line of a C++-style comment within its file `f`.
5264
*/
5365
private int lineInFile(CppStyleComment c, File f) {
5466
f = c.getFile() and
55-
result = c.getLocation().getStartLine()
67+
result = c.getLocation().getStartLine() and
68+
69+
// Ignore comments on the same line as a preprocessor directive.
70+
not preprocLine(f, result)
5671
}
5772

5873
/**
@@ -89,9 +104,17 @@ private int commentId(CppStyleComment c, File f, int line) {
89104
*/
90105
class CommentBlock extends Comment {
91106
CommentBlock() {
92-
this instanceof CppStyleComment
93-
implies
94-
not exists(CppStyleComment pred, File f | lineInFile(pred, f) + 1 = lineInFile(this, f))
107+
(
108+
this instanceof CppStyleComment
109+
implies
110+
not exists(CppStyleComment pred, File f | lineInFile(pred, f) + 1 = lineInFile(this, f))
111+
) and (
112+
// Ignore comments on the same line as a preprocessor directive.
113+
not exists(Location l |
114+
l = this.getLocation() and
115+
preprocLine(l.getFile(), l.getStartLine())
116+
)
117+
)
95118
}
96119

97120
/**

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,7 @@
1111
| test2.cpp:63:1:63:15 | // #pragma once | This comment appears to contain commented-out code |
1212
| test2.cpp:65:1:65:17 | // # pragma once | This comment appears to contain commented-out code |
1313
| test2.cpp:67:1:67:19 | /*#error"myerror"*/ | This comment appears to contain commented-out code |
14-
| test2.cpp:73:8:73:24 | // #ifdef MYMACRO | This comment appears to contain commented-out code |
15-
| test2.cpp:79:7:79:30 | // #if !defined(MYMACRO) | This comment appears to contain commented-out code |
16-
| test2.cpp:83:8:83:37 | // #else #if !defined(MYMACRO) | This comment appears to contain commented-out code |
17-
| test2.cpp:89:10:89:45 | // #ifdef MYMACRO (comment) | This comment appears to contain commented-out code |
1814
| test2.cpp:91:1:95:2 | /*\n#ifdef MYMACRO\n\t// ...\n#endif // #ifdef MYMACRO\n*/ | This comment appears to contain commented-out code |
19-
| test2.cpp:103:3:105:25 | // comment at end of block | This comment appears to contain commented-out code |
2015
| test.c:2:1:2:22 | // commented out code; | This comment appears to contain commented-out code |
2116
| test.c:4:1:7:8 | // some; | This comment appears to contain commented-out code |
2217
| test.c:9:1:13:8 | // also; | This comment appears to contain commented-out code |

0 commit comments

Comments
 (0)