Skip to content

Commit a82cf74

Browse files
committed
C++: Improve performance of definitions.qll.
1 parent 9388448 commit a82cf74

File tree

5 files changed

+16
-21
lines changed

5 files changed

+16
-21
lines changed

cpp/ql/src/definitions.ql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@
99
import definitions
1010

1111
from Top e, Top def, string kind
12-
where def = definitionOf(e, kind, false)
12+
where
13+
def = definitionOf(e, kind) and
14+
not e.isFromTemplateInstantiation(_)
1315
select e, def, kind

cpp/ql/src/definitions.qll

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ predicate hasLocationInfo_Include(Include i, string path, int sl, int sc, int el
7777

7878
/** Holds if `e` is a source or a target of jump-to-definition. */
7979
predicate interestingElement(Element e) {
80-
exists(definitionOf(e, _, true))
80+
exists(definitionOf(e, _))
8181
or
82-
e = definitionOf(_, _, true)
82+
e = definitionOf(_, _)
8383
}
8484

8585
/**
@@ -124,10 +124,7 @@ private predicate constructorCallTypeMention(ConstructorCall cc, TypeMention tm)
124124

125125
/**
126126
* Gets an element, of kind `kind`, that element `e` uses, if any.
127-
* If `includeTemplateInstantiations` is set, include information for
128-
* elements `e` that are inside of template instantiations.
129-
* Doing so yields multiple definitions for a single location, which can be
130-
* undesirably. For example, lgtm.com does not support that.
127+
* Attention: This predicate yields multiple definitions for a single location.
131128
*
132129
* The `kind` is a string representing what kind of use it is:
133130
* - `"M"` for function and method calls
@@ -137,7 +134,7 @@ private predicate constructorCallTypeMention(ConstructorCall cc, TypeMention tm)
137134
* - `"I"` for import / include directives
138135
*/
139136
cached
140-
Top definitionOf(Top e, string kind, boolean includeTemplateInstantiations) {
137+
Top definitionOf(Top e, string kind) {
141138
(
142139
// call -> function called
143140
kind = "M" and
@@ -200,13 +197,7 @@ Top definitionOf(Top e, string kind, boolean includeTemplateInstantiations) {
200197
not e.(Element).isInMacroExpansion() and
201198
// exclude nested macro invocations, as they will overlap with
202199
// the top macro invocation.
203-
not exists(e.(MacroAccess).getParentInvocation()) and
204-
(
205-
includeTemplateInstantiations = true
206-
or
207-
includeTemplateInstantiations = false and
208-
not e.isFromTemplateInstantiation(_)
209-
)
200+
not exists(e.(MacroAccess).getParentInvocation())
210201
) and
211202
// Some entities have many locations. This can arise for an external
212203
// function that is frequently declared but not defined, or perhaps

cpp/ql/src/localDefinitions.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ import definitions
1212
external string selectedSourceFile();
1313

1414
from Top e, Top def, string kind
15-
where def = definitionOf(e, kind, true) and e.getFile() = getEncodedFile(selectedSourceFile())
16-
select e, def, kind
15+
where def = definitionOf(e, kind) and e.getFile() = getEncodedFile(selectedSourceFile())
16+
select e.getLocation(), def.getLocation(), kind

cpp/ql/src/localReferences.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ import definitions
1212
external string selectedSourceFile();
1313

1414
from Top e, Top def, string kind
15-
where def = definitionOf(e, kind, true) and def.getFile() = getEncodedFile(selectedSourceFile())
15+
where def = definitionOf(e, kind) and def.getFile() = getEncodedFile(selectedSourceFile())
1616
select e, def, kind

cpp/ql/test/query-tests/definitions/locationInfo.ql

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import definitions
44
* An element that is the source of a jump-to-definition link.
55
*/
66
class Link extends Top {
7-
Link() { exists(definitionOf(this, _, false)) }
7+
Link() { exists(definitionOf(this, _)) }
88
}
99

1010
/**
@@ -31,7 +31,7 @@ predicate linkLocationInfo(Link e, string filepath, int begin, int end) {
3131
* Gets a string describing a problem with a `Link`.
3232
*/
3333
string issues(Link e) {
34-
strictcount(Top def | def = definitionOf(e, _, false)) > 1 and
34+
strictcount(Top def | def = definitionOf(e, _)) > 1 and
3535
result = "has more than one definition"
3636
or
3737
exists(string filepath1, int begin1, int end1, Link e2, string filepath2, int begin2, int end2 |
@@ -40,7 +40,9 @@ string issues(Link e) {
4040
filepath1 = filepath2 and
4141
not end1 < begin2 and
4242
not begin1 > end2 and
43-
e != e2
43+
e != e2 and
44+
not e.isFromTemplateInstantiation(_) and
45+
not e2.isFromTemplateInstantiation(_)
4446
) and
4547
result = "overlaps another link"
4648
}

0 commit comments

Comments
 (0)