Skip to content

Commit e3056ca

Browse files
committed
CPP: Improve accuracy of AutogeneratedFile.qll.
1 parent f474fdd commit e3056ca

File tree

1 file changed

+39
-11
lines changed

1 file changed

+39
-11
lines changed

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

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,30 @@ import semmle.code.cpp.File
33
import semmle.code.cpp.Preprocessor
44

55
/**
6-
* Holds if `c` is a comment which is usually seen in autogenerated files.
7-
* For example, comments containing 'autogenerated' or 'generated by'.
6+
* Holds if comment `c` indicates that it might be in an auto-generated file, for
7+
* example because it contains the text "auto-generated by".
88
*/
9-
predicate isAutogeneratedComment(Comment c) {
10-
c.getContents().regexpMatch("(?si).*(?:auto[ -]?generated|generated (?:by|file)|changes made in this file will be lost).*")
9+
private predicate autogeneratedComment(Comment c) {
10+
// ?s = include newlines in anything (`.`)
11+
// ?i = ignore case
12+
13+
// auto-generated, automatically generated etc.
14+
c.getContents().regexpMatch("(?si).*(auto[\\w-]*\\s*?generated).*") or
15+
16+
// generated by (not used mid-sentence)
17+
c.getContents().regexpMatch("(?si).*[^a-zA-Z\\s\\*\\r\\n][\\s\\*\\r\\n]*(generated by).*") or
18+
19+
// generated file
20+
c.getContents().regexpMatch("(?si).*(generated file).*") or
21+
22+
// file [is] generated
23+
c.getContents().regexpMatch("(?si).*(file( is)? generated).*") or
24+
25+
// changes made in this file will be lost
26+
c.getContents().regexpMatch("(?si).*(changes made in this file will be lost).*") or
27+
28+
// do not edit/modify
29+
c.getContents().regexpMatch("(?si).*(do(n't|nt| not) (edit|modify)).*")
1130
}
1231

1332
/**
@@ -25,6 +44,17 @@ predicate hasPragmaDifferentFile(File f) {
2544
)
2645
}
2746

47+
/**
48+
* The maximum line number we consider to be "near the beginning" of file `f`.
49+
*/
50+
private int fileHeaderLimit(File f) {
51+
result = max(int head, int line |
52+
head <= 5 and
53+
locations_default(_, underlyingElement(f), head, _, line, _) |
54+
line
55+
) + 5
56+
}
57+
2858
/**
2959
* Holds if the file is probably an autogenerated file.
3060
*
@@ -36,12 +66,10 @@ predicate hasPragmaDifferentFile(File f) {
3666
*/
3767
class AutogeneratedFile extends File {
3868
cached AutogeneratedFile() {
39-
exists(int limit, int head |
40-
head <= 5 and
41-
limit = max(int line | locations_default(_, underlyingElement(this), head, _, line, _)) + 5
42-
|
43-
exists (Comment c | c.getFile() = this and c.getLocation().getStartLine() <= limit and isAutogeneratedComment(c))
44-
)
45-
or hasPragmaDifferentFile(this)
69+
exists(Comment c |
70+
c.getFile() = this and
71+
c.getLocation().getStartLine() <= fileHeaderLimit(this) and
72+
autogeneratedComment(c)
73+
) or hasPragmaDifferentFile(this)
4674
}
4775
}

0 commit comments

Comments
 (0)