@@ -47,12 +47,38 @@ 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` that we permit code comments besides. For example this
53+ * is considered acceptable:
54+ * ```
55+ * #ifdef MYMACRO
56+ * ...
57+ * #endif // #ifdef MYMACRO
58+ * ```
59+ */
60+ private predicate preprocLine ( File f , int line ) {
61+ exists ( PreprocessorDirective pd , Location l |
62+ (
63+ pd instanceof PreprocessorElse or
64+ pd instanceof PreprocessorElif or
65+ pd instanceof PreprocessorEndif
66+ ) and
67+ pd .getLocation ( ) = l and
68+ l .getFile ( ) = f and
69+ l .getStartLine ( ) = line
70+ )
71+ }
72+
5073/**
5174 * The line of a C++-style comment within its file `f`.
5275 */
5376private int lineInFile ( CppStyleComment c , File f ) {
5477 f = c .getFile ( ) and
55- result = c .getLocation ( ) .getStartLine ( )
78+ result = c .getLocation ( ) .getStartLine ( ) and
79+
80+ // Ignore comments on the same line as a preprocessor directive.
81+ not preprocLine ( f , result )
5682}
5783
5884/**
@@ -89,9 +115,17 @@ private int commentId(CppStyleComment c, File f, int line) {
89115 */
90116class CommentBlock extends Comment {
91117 CommentBlock ( ) {
92- this instanceof CppStyleComment
93- implies
94- not exists ( CppStyleComment pred , File f | lineInFile ( pred , f ) + 1 = lineInFile ( this , f ) )
118+ (
119+ this instanceof CppStyleComment
120+ implies
121+ not exists ( CppStyleComment pred , File f | lineInFile ( pred , f ) + 1 = lineInFile ( this , f ) )
122+ ) and (
123+ // Ignore comments on the same line as a preprocessor directive.
124+ not exists ( Location l |
125+ l = this .getLocation ( ) and
126+ preprocLine ( l .getFile ( ) , l .getStartLine ( ) )
127+ )
128+ )
95129 }
96130
97131 /**
@@ -133,6 +167,7 @@ class CommentBlock extends Comment {
133167
134168 predicate isCommentedOutCode ( ) {
135169 not this .isDocumentation ( ) and
170+ not this .getFile ( ) .( HeaderFile ) .noTopLevelCode ( ) and
136171 this .numCodeLines ( ) .( float ) / this .numLines ( ) .( float ) > 0.5
137172 }
138173
0 commit comments