Merge pull request #801 from jbj/mergeback-20190121

C++: Merge rc/1.19 and #777
This commit is contained in:
Geoffrey White
2019-01-22 08:54:26 +00:00
committed by GitHub
3 changed files with 91 additions and 14 deletions

View File

@@ -3,11 +3,45 @@ import semmle.code.cpp.File
import semmle.code.cpp.Preprocessor
/**
* Holds if `c` is a comment which is usually seen in autogenerated files.
* For example, comments containing 'autogenerated' or 'generated by'.
* Holds if comment `c` indicates that it might be in an auto-generated file, for
* example because it contains the text "auto-generated by".
*/
predicate isAutogeneratedComment(Comment c) {
c.getContents().regexpMatch("(?si).*(?:auto[ -]?generated|generated (?:by|file)|changes made in this file will be lost).*")
private bindingset[comment] predicate autogeneratedComment(string comment) {
// ?s = include newlines in anything (`.`)
// ?i = ignore case
exists(string cond |
cond =
// generated by (not mid-sentence)
"(^ generated by[^a-z])|" +
"(! generated by[^a-z])|" +
// generated file
"(generated file)|" +
// file [is/was/has been] generated
"(file( is| was| has been)? generated)|" +
// changes made in this file will be lost
"(changes made in this file will be lost)|" +
// do not edit/modify
"(^ do(n't|nt| not) (hand-?)?(edit|modify))|" +
"(! do(n't|nt| not) (hand-?)?(edit|modify))" and
comment.regexpMatch("(?si).*(" +
// replace `generated` with a regexp that also catches things like
// `auto-generated`.
cond.replaceAll("generated", "(auto[\\w-]*[\\s/\\*\\r\\n]*)?generated")
// replace `!` with a regexp for end-of-sentence / separator characters.
.replaceAll("!", "[\\.\\?\\!\\-\\;\\,]")
// replace ` ` with a regexp for one or more whitespace characters
// (including newlines and `/*`).
.replaceAll(" ", "[\\s/\\*\\r\\n]+") +
").*"
)
)
}
/**
@@ -25,6 +59,48 @@ predicate hasPragmaDifferentFile(File f) {
)
}
/**
* The line where the first comment in file `f` begins (maximum of 5). This allows
* us to skip past any preprocessor logic or similar code before the first comment.
*/
private int fileFirstComment(File f) {
result = min(int line |
exists(Comment c |
c.getFile() = f and
c.getLocation().getStartLine() = line and
line < 5
)
).minimum(5)
}
/**
* The line where the initial comments of file `f` end. This is just before the
* first bit of code, excluding anything skipped over by `fileFirstComment`.
*/
private int fileHeaderLimit(File f) {
exists(int fc |
fc = fileFirstComment(f) and
result = min(int line |
exists(DeclarationEntry de, Location l |
l = de.getLocation() and
l.getFile() = f and
line = l.getStartLine() - 1 and
line > fc
) or exists(PreprocessorDirective pd, Location l |
l = pd.getLocation() and
l.getFile() = f and
line = l.getStartLine() - 1 and
line > fc
) or exists(NamespaceDeclarationEntry nde, Location l |
l = nde.getLocation() and
l.getFile() = f and
line = l.getStartLine() - 1 and
line > fc
) or line = f.getMetrics().getNumberOfLines()
)
)
}
/**
* Holds if the file is probably an autogenerated file.
*
@@ -36,12 +112,13 @@ predicate hasPragmaDifferentFile(File f) {
*/
class AutogeneratedFile extends File {
cached AutogeneratedFile() {
exists(int limit, int head |
head <= 5 and
limit = max(int line | locations_default(_, underlyingElement(this), head, _, line, _)) + 5
|
exists (Comment c | c.getFile() = this and c.getLocation().getStartLine() <= limit and isAutogeneratedComment(c))
)
or hasPragmaDifferentFile(this)
autogeneratedComment(
strictconcat(Comment c |
c.getFile() = this and
c.getLocation().getStartLine() <= fileHeaderLimit(this) |
c.getContents() order by c.getLocation().getStartLine()
)
) or
hasPragmaDifferentFile(this)
}
}

View File

@@ -959,7 +959,7 @@ class TranslatedFunctionAccess extends TranslatedNonConstantExpr {
/**
* IR translation of an expression whose value is not known at compile time.
*/
abstract class TranslatedNonConstantExpr extends TranslatedCoreExpr {
abstract class TranslatedNonConstantExpr extends TranslatedCoreExpr, TTranslatedValueExpr {
TranslatedNonConstantExpr() {
this = TTranslatedValueExpr(expr) and
not expr.isConstant()
@@ -971,7 +971,7 @@ abstract class TranslatedNonConstantExpr extends TranslatedCoreExpr {
* includes not only literals, but also "integral constant expressions" (e.g.
* `1 + 2`).
*/
abstract class TranslatedConstantExpr extends TranslatedCoreExpr {
abstract class TranslatedConstantExpr extends TranslatedCoreExpr, TTranslatedValueExpr {
TranslatedConstantExpr() {
this = TTranslatedValueExpr(expr) and
expr.isConstant()