diff --git a/cpp/ql/lib/definitions.qll b/cpp/ql/lib/definitions.qll index 662cb5ed877..1eee3804359 100644 --- a/cpp/ql/lib/definitions.qll +++ b/cpp/ql/lib/definitions.qll @@ -123,6 +123,13 @@ private predicate constructorCallTypeMention(ConstructorCall cc, TypeMention tm) ) } +/** Holds if `loc` has the container `container` and is on the line starting at `startLine`. */ +pragma[nomagic] +private predicate hasContainerAndStartLine(Location loc, Container container, int startLine) { + loc.getStartLine() = startLine and + loc.getContainer() = container +} + /** * Gets an element, of kind `kind`, that element `e` uses, if any. * Attention: This predicate yields multiple definitions for a single location. @@ -184,11 +191,9 @@ Top definitionOf(Top e, string kind) { kind = "I" and result = e.(Include).getIncludedFile() and // exclude `#include` directives containing macros - not exists(MacroInvocation mi, Location l1, Location l2 | - l1 = e.(Include).getLocation() and - l2 = mi.getLocation() and - l1.getContainer() = l2.getContainer() and - l1.getStartLine() = l2.getStartLine() + not exists(MacroInvocation mi, Container container, int startLine | + hasContainerAndStartLine(e.(Include).getLocation(), container, startLine) and + hasContainerAndStartLine(mi.getLocation(), container, startLine) // (an #include directive must be always on it's own line) ) ) and diff --git a/cpp/ql/lib/semmle/code/cpp/Location.qll b/cpp/ql/lib/semmle/code/cpp/Location.qll index 2b2a06ac474..fd5d2d34c5d 100644 --- a/cpp/ql/lib/semmle/code/cpp/Location.qll +++ b/cpp/ql/lib/semmle/code/cpp/Location.qll @@ -10,12 +10,14 @@ import semmle.code.cpp.File */ class Location extends @location { /** Gets the container corresponding to this location. */ + pragma[nomagic] Container getContainer() { this.fullLocationInfo(result, _, _, _, _) } /** Gets the file corresponding to this location, if any. */ File getFile() { result = this.getContainer() } /** Gets the 1-based line number (inclusive) where this location starts. */ + pragma[nomagic] int getStartLine() { this.fullLocationInfo(_, result, _, _, _) } /** Gets the 1-based column number (inclusive) where this location starts. */