Skip to content

Commit 45cd47e

Browse files
authored
Merge pull request #4535 from criemen/jump-to-def
C++: Extend jump-to-def support to template instantiations.
2 parents 12233e5 + 07452c0 commit 45cd47e

File tree

5 files changed

+14
-14
lines changed

5 files changed

+14
-14
lines changed

cpp/ql/src/definitions.ql

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
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 LGTM.
55
* @kind definitions
66
* @id cpp/jump-to-definition
77
*/
88

99
import definitions
1010

1111
from Top e, Top def, string kind
12-
where def = definitionOf(e, kind)
12+
where
13+
def = definitionOf(e, kind) and
14+
// We need to exclude definitions for elements inside template instantiations,
15+
// as these often lead to multiple links to definitions from the same source location.
16+
// LGTM does not support this bevaviour.
17+
not e.isFromTemplateInstantiation(_)
1318
select e, def, kind

cpp/ql/src/definitions.qll

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +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+
* Attention: This predicate yields multiple definitions for a single location.
127128
*
128129
* The `kind` is a string representing what kind of use it is:
129130
* - `"M"` for function and method calls
@@ -196,15 +197,7 @@ Top definitionOf(Top e, string kind) {
196197
not e.(Element).isInMacroExpansion() and
197198
// exclude nested macro invocations, as they will overlap with
198199
// the top macro invocation.
199-
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(_)
200+
not exists(e.(MacroAccess).getParentInvocation())
208201
) and
209202
// Some entities have many locations. This can arise for an external
210203
// function that is frequently declared but not defined, or perhaps

cpp/ql/src/localDefinitions.ql

Lines changed: 1 addition & 1 deletion
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

cpp/ql/src/localReferences.ql

Lines changed: 1 addition & 1 deletion
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

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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)