diff --git a/cpp/ql/src/Documentation/CommentedOutCode.qll b/cpp/ql/src/Documentation/CommentedOutCode.qll index 5bb22623564..9155e1613a5 100644 --- a/cpp/ql/src/Documentation/CommentedOutCode.qll +++ b/cpp/ql/src/Documentation/CommentedOutCode.qll @@ -47,12 +47,27 @@ private predicate looksLikeCode(string line) { ) } +/** + * Holds if there is a preprocessor directive on the line indicated by + * `f` and `line`. + */ +private predicate preprocLine(File f, int line) { + exists(PreprocessorDirective pd, Location l | + pd.getLocation() = l and + l.getFile() = f and + l.getStartLine() = line + ) +} + /** * The line of a C++-style comment within its file `f`. */ private int lineInFile(CppStyleComment c, File f) { f = c.getFile() and - result = c.getLocation().getStartLine() + result = c.getLocation().getStartLine() and + + // Ignore comments on the same line as a preprocessor directive. + not preprocLine(f, result) } /** @@ -89,9 +104,17 @@ private int commentId(CppStyleComment c, File f, int line) { */ class CommentBlock extends Comment { CommentBlock() { - this instanceof CppStyleComment - implies - not exists(CppStyleComment pred, File f | lineInFile(pred, f) + 1 = lineInFile(this, f)) + ( + this instanceof CppStyleComment + implies + not exists(CppStyleComment pred, File f | lineInFile(pred, f) + 1 = lineInFile(this, f)) + ) and ( + // Ignore comments on the same line as a preprocessor directive. + not exists(Location l | + l = this.getLocation() and + preprocLine(l.getFile(), l.getStartLine()) + ) + ) } /** diff --git a/cpp/ql/test/query-tests/Documentation/CommentedOutCode/CommentedOutCode.expected b/cpp/ql/test/query-tests/Documentation/CommentedOutCode/CommentedOutCode.expected index 1280bd0b3c7..a406bf53f8d 100644 --- a/cpp/ql/test/query-tests/Documentation/CommentedOutCode/CommentedOutCode.expected +++ b/cpp/ql/test/query-tests/Documentation/CommentedOutCode/CommentedOutCode.expected @@ -11,12 +11,7 @@ | test2.cpp:63:1:63:15 | // #pragma once | This comment appears to contain commented-out code | | test2.cpp:65:1:65:17 | // # pragma once | This comment appears to contain commented-out code | | test2.cpp:67:1:67:19 | /*#error"myerror"*/ | This comment appears to contain commented-out code | -| test2.cpp:73:8:73:24 | // #ifdef MYMACRO | This comment appears to contain commented-out code | -| test2.cpp:79:7:79:30 | // #if !defined(MYMACRO) | This comment appears to contain commented-out code | -| test2.cpp:83:8:83:37 | // #else #if !defined(MYMACRO) | This comment appears to contain commented-out code | -| test2.cpp:89:10:89:45 | // #ifdef MYMACRO (comment) | This comment appears to contain commented-out code | | test2.cpp:91:1:95:2 | /*\n#ifdef MYMACRO\n\t// ...\n#endif // #ifdef MYMACRO\n*/ | This comment appears to contain commented-out code | -| test2.cpp:103:3:105:25 | // comment at end of block | This comment appears to contain commented-out code | | test.c:2:1:2:22 | // commented out code; | This comment appears to contain commented-out code | | test.c:4:1:7:8 | // some; | This comment appears to contain commented-out code | | test.c:9:1:13:8 | // also; | This comment appears to contain commented-out code |