Skip to content

Commit 49cfa43

Browse files
committed
CPP: Improve accuracy further.
1 parent 105f8dd commit 49cfa43

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

cpp/ql/src/semmle/code/cpp/AutogeneratedFile.qll

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,43 @@ import semmle.code.cpp.Preprocessor
66
* Holds if comment `c` indicates that it might be in an auto-generated file, for
77
* example because it contains the text "auto-generated by".
88
*/
9-
private predicate autogeneratedComment(Comment c) {
9+
private bindingset[comment] predicate autogeneratedComment(string comment) {
1010
// ?s = include newlines in anything (`.`)
1111
// ?i = ignore case
12-
c.getContents().regexpMatch("(?si).*(" +
12+
exists(string cond |
13+
cond =
14+
// generated by (not mid-sentence)
15+
//"([^a-z\\s/\\*\\r\\n] generated by )|" +
16+
"(^ generated by[^a-z])|" +
17+
"(! generated by[^a-z])|" +
1318

14-
// auto-generated, automatically generated etc.
15-
"(auto[\\w-]*\\s*?generated)|" +
19+
// generated file
20+
"(generated file)|" +
1621

17-
// generated by (at beginning of sentence)
18-
"([^a-z\\s\\*\\r\\n][\\s\\*\\r\\n]*(generated by)[^a-z])|" +
22+
// file [is/was/has been] generated
23+
"(file( is| was| has been)? generated)|" +
1924

20-
// generated file
21-
"(generated file)|" +
22-
23-
// file [is] generated
24-
"(file( is)? generated)|" +
25-
26-
// changes made in this file will be lost
27-
"(changes made in this file will be lost)|" +
25+
// changes made in this file will be lost
26+
"(changes made in this file will be lost)|" +
27+
28+
// do not edit/modify
29+
"(^ do(n't|nt| not) (hand-?)?(edit|modify))|" +
30+
"(! do(n't|nt| not) (hand-?)?(edit|modify))" and
2831

29-
// do not edit/modify
30-
"(do(n't|nt| not) (edit|modify))" +
32+
comment.regexpMatch("(?si).*(" +
33+
// replace `generated` with a regexp that also catches things like
34+
// `auto-generated`.
35+
cond.replaceAll("generated", "(auto[\\w-]*[\\s/\\*\\r\\n]*)?generated")
3136

32-
").*")
37+
// replace `!` with a regexp for end-of-sentence / separator characters.
38+
.replaceAll("!", "[\\.\\?\\!\\-\\;\\,]")
39+
40+
// replace ` ` with a regexp for one or more whitespace characters
41+
// (including newlines and `/*`).
42+
.replaceAll(" ", "[\\s/\\*\\r\\n]+") +
43+
").*"
44+
)
45+
)
3346
}
3447

3548
/**

0 commit comments

Comments
 (0)