Skip to content

Commit 9388448

Browse files
committed
C++: Extend jump-to-def support to template instantiations.
This commit extends developers ability to use jump-to-def in C/C++ files opened in the VSCode extension. Before, jump-to-def starting with code in a template instantiation did not work. Furthermore, this fixes a bug, as the list of all references of a location did not include template instantiations.
1 parent a92a701 commit 9388448

File tree

5 files changed

+20
-18
lines changed

5 files changed

+20
-18
lines changed

cpp/ql/src/definitions.ql

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

1111
from Top e, Top def, string kind
12-
where def = definitionOf(e, kind)
12+
where def = definitionOf(e, kind, false)
1313
select e, def, kind

cpp/ql/src/definitions.qll

Lines changed: 13 additions & 11 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, _))
80+
exists(definitionOf(e, _, true))
8181
or
82-
e = definitionOf(_, _)
82+
e = definitionOf(_, _, true)
8383
}
8484

8585
/**
@@ -124,6 +124,10 @@ 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.
127131
*
128132
* The `kind` is a string representing what kind of use it is:
129133
* - `"M"` for function and method calls
@@ -133,7 +137,7 @@ private predicate constructorCallTypeMention(ConstructorCall cc, TypeMention tm)
133137
* - `"I"` for import / include directives
134138
*/
135139
cached
136-
Top definitionOf(Top e, string kind) {
140+
Top definitionOf(Top e, string kind, boolean includeTemplateInstantiations) {
137141
(
138142
// call -> function called
139143
kind = "M" and
@@ -197,14 +201,12 @@ Top definitionOf(Top e, string kind) {
197201
// exclude nested macro invocations, as they will overlap with
198202
// the top macro invocation.
199203
not exists(e.(MacroAccess).getParentInvocation()) and
200-
// exclude results from template instantiations, as:
201-
// (1) these dependencies will often be caused by a choice of
202-
// template parameter, which is non-local to this part of code; and
203-
// (2) overlapping results pointing to different locations will
204-
// be very common.
205-
// It's possible we could allow a subset of these dependencies
206-
// in future, if we're careful to ensure the above don't apply.
207-
not e.isFromTemplateInstantiation(_)
204+
(
205+
includeTemplateInstantiations = true
206+
or
207+
includeTemplateInstantiations = false and
208+
not e.isFromTemplateInstantiation(_)
209+
)
208210
) and
209211
// Some entities have many locations. This can arise for an external
210212
// 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
@@ -1,7 +1,7 @@
11
/**
22
* @name Jump-to-definition links
33
* @description Generates use-definition pairs that provide the data
4-
* for jump-to-definition in the code viewer.
4+
* for jump-to-definition in the code viewer of VSCode.
55
* @kind definitions
66
* @id cpp/ide-jump-to-definition
77
* @tags ide-contextual-queries/local-definitions
@@ -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) and e.getFile() = getEncodedFile(selectedSourceFile())
15+
where def = definitionOf(e, kind, true) and e.getFile() = getEncodedFile(selectedSourceFile())
1616
select e, def, kind

cpp/ql/src/localReferences.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @name Find-references links
33
* @description Generates use-definition pairs that provide the data
4-
* for find-references in the code viewer.
4+
* for find-references in the code viewer of VSCode.
55
* @kind definitions
66
* @id cpp/ide-find-references
77
* @tags ide-contextual-queries/local-references
@@ -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) and def.getFile() = getEncodedFile(selectedSourceFile())
15+
where def = definitionOf(e, kind, true) and def.getFile() = getEncodedFile(selectedSourceFile())
1616
select e, def, kind

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

Lines changed: 2 additions & 2 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, _)) }
7+
Link() { exists(definitionOf(this, _, false)) }
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, _)) > 1 and
34+
strictcount(Top def | def = definitionOf(e, _, false)) > 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 |

0 commit comments

Comments
 (0)