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.
This commit is contained in:
Cornelius Riemenschneider
2020-10-21 21:35:38 +02:00
parent a92a701c35
commit 9388448053
5 changed files with 20 additions and 18 deletions

View File

@@ -9,5 +9,5 @@
import definitions
from Top e, Top def, string kind
where def = definitionOf(e, kind)
where def = definitionOf(e, kind, false)
select e, def, kind

View File

@@ -77,9 +77,9 @@ predicate hasLocationInfo_Include(Include i, string path, int sl, int sc, int el
/** Holds if `e` is a source or a target of jump-to-definition. */
predicate interestingElement(Element e) {
exists(definitionOf(e, _))
exists(definitionOf(e, _, true))
or
e = definitionOf(_, _)
e = definitionOf(_, _, true)
}
/**
@@ -124,6 +124,10 @@ private predicate constructorCallTypeMention(ConstructorCall cc, TypeMention tm)
/**
* Gets an element, of kind `kind`, that element `e` uses, if any.
* If `includeTemplateInstantiations` is set, include information for
* elements `e` that are inside of template instantiations.
* Doing so yields multiple definitions for a single location, which can be
* undesirably. For example, lgtm.com does not support that.
*
* The `kind` is a string representing what kind of use it is:
* - `"M"` for function and method calls
@@ -133,7 +137,7 @@ private predicate constructorCallTypeMention(ConstructorCall cc, TypeMention tm)
* - `"I"` for import / include directives
*/
cached
Top definitionOf(Top e, string kind) {
Top definitionOf(Top e, string kind, boolean includeTemplateInstantiations) {
(
// call -> function called
kind = "M" and
@@ -197,14 +201,12 @@ Top definitionOf(Top e, string kind) {
// exclude nested macro invocations, as they will overlap with
// the top macro invocation.
not exists(e.(MacroAccess).getParentInvocation()) and
// exclude results from template instantiations, as:
// (1) these dependencies will often be caused by a choice of
// template parameter, which is non-local to this part of code; and
// (2) overlapping results pointing to different locations will
// be very common.
// It's possible we could allow a subset of these dependencies
// in future, if we're careful to ensure the above don't apply.
not e.isFromTemplateInstantiation(_)
(
includeTemplateInstantiations = true
or
includeTemplateInstantiations = false and
not e.isFromTemplateInstantiation(_)
)
) and
// Some entities have many locations. This can arise for an external
// function that is frequently declared but not defined, or perhaps

View File

@@ -1,7 +1,7 @@
/**
* @name Jump-to-definition links
* @description Generates use-definition pairs that provide the data
* for jump-to-definition in the code viewer.
* for jump-to-definition in the code viewer of VSCode.
* @kind definitions
* @id cpp/ide-jump-to-definition
* @tags ide-contextual-queries/local-definitions
@@ -12,5 +12,5 @@ import definitions
external string selectedSourceFile();
from Top e, Top def, string kind
where def = definitionOf(e, kind) and e.getFile() = getEncodedFile(selectedSourceFile())
where def = definitionOf(e, kind, true) and e.getFile() = getEncodedFile(selectedSourceFile())
select e, def, kind

View File

@@ -1,7 +1,7 @@
/**
* @name Find-references links
* @description Generates use-definition pairs that provide the data
* for find-references in the code viewer.
* for find-references in the code viewer of VSCode.
* @kind definitions
* @id cpp/ide-find-references
* @tags ide-contextual-queries/local-references
@@ -12,5 +12,5 @@ import definitions
external string selectedSourceFile();
from Top e, Top def, string kind
where def = definitionOf(e, kind) and def.getFile() = getEncodedFile(selectedSourceFile())
where def = definitionOf(e, kind, true) and def.getFile() = getEncodedFile(selectedSourceFile())
select e, def, kind

View File

@@ -4,7 +4,7 @@ import definitions
* An element that is the source of a jump-to-definition link.
*/
class Link extends Top {
Link() { exists(definitionOf(this, _)) }
Link() { exists(definitionOf(this, _, false)) }
}
/**
@@ -31,7 +31,7 @@ predicate linkLocationInfo(Link e, string filepath, int begin, int end) {
* Gets a string describing a problem with a `Link`.
*/
string issues(Link e) {
strictcount(Top def | def = definitionOf(e, _)) > 1 and
strictcount(Top def | def = definitionOf(e, _, false)) > 1 and
result = "has more than one definition"
or
exists(string filepath1, int begin1, int end1, Link e2, string filepath2, int begin2, int end2 |