From 9d8ab72a6931ef1d5810c815ce623666236b1427 Mon Sep 17 00:00:00 2001 From: Argyrios Kyrtzidis Date: Mon, 7 Nov 2016 21:20:15 +0000 Subject: [PATCH] [index] Handle properly C++14's template variables. - Infer the right symbol kind. - Provide a templated USR, similar to how we handle class templates. rdar://28980398 llvm-svn: 286154 --- clang/lib/Index/IndexSymbol.cpp | 29 +++++++++++++++++--------- clang/lib/Index/IndexingContext.cpp | 10 +++++---- clang/lib/Index/USRGeneration.cpp | 20 ++++++++++++++++++ clang/test/Index/Core/index-source.cpp | 13 +++++++++++- 4 files changed, 57 insertions(+), 15 deletions(-) diff --git a/clang/lib/Index/IndexSymbol.cpp b/clang/lib/Index/IndexSymbol.cpp index d98d7c072439..4126bb6a971f 100644 --- a/clang/lib/Index/IndexSymbol.cpp +++ b/clang/lib/Index/IndexSymbol.cpp @@ -91,6 +91,25 @@ SymbolInfo index::getSymbolInfo(const Decl *D) { Info.SubKinds |= (unsigned)SymbolSubKind::TemplateSpecialization; } + } else if (auto *VD = dyn_cast(D)) { + Info.Kind = SymbolKind::Variable; + if (isa(D->getDeclContext())) { + Info.Kind = SymbolKind::StaticProperty; + Info.Lang = SymbolLanguage::CXX; + } + if (isa(D)) { + Info.Lang = SymbolLanguage::CXX; + Info.SubKinds |= (unsigned)SymbolSubKind::Generic; + Info.SubKinds |= (unsigned)SymbolSubKind::TemplatePartialSpecialization; + } else if (isa(D)) { + Info.Lang = SymbolLanguage::CXX; + Info.SubKinds |= (unsigned)SymbolSubKind::Generic; + Info.SubKinds |= (unsigned)SymbolSubKind::TemplateSpecialization; + } else if (VD->getDescribedVarTemplate()) { + Info.Lang = SymbolLanguage::CXX; + Info.SubKinds |= (unsigned)SymbolSubKind::Generic; + } + } else { switch (D->getKind()) { case Decl::Import: @@ -101,16 +120,6 @@ SymbolInfo index::getSymbolInfo(const Decl *D) { case Decl::Function: Info.Kind = SymbolKind::Function; break; - case Decl::ParmVar: - Info.Kind = SymbolKind::Variable; - break; - case Decl::Var: - Info.Kind = SymbolKind::Variable; - if (isa(D->getDeclContext())) { - Info.Kind = SymbolKind::StaticProperty; - Info.Lang = SymbolLanguage::CXX; - } - break; case Decl::Field: Info.Kind = SymbolKind::Field; if (const CXXRecordDecl * diff --git a/clang/lib/Index/IndexingContext.cpp b/clang/lib/Index/IndexingContext.cpp index 21869d88a570..e623a495b47b 100644 --- a/clang/lib/Index/IndexingContext.cpp +++ b/clang/lib/Index/IndexingContext.cpp @@ -130,9 +130,10 @@ bool IndexingContext::isTemplateImplicitInstantiation(const Decl *D) { if (const ClassTemplateSpecializationDecl * SD = dyn_cast(D)) { TKind = SD->getSpecializationKind(); - } - if (const FunctionDecl *FD = dyn_cast(D)) { + } else if (const FunctionDecl *FD = dyn_cast(D)) { TKind = FD->getTemplateSpecializationKind(); + } else if (auto *VD = dyn_cast(D)) { + TKind = VD->getTemplateSpecializationKind(); } switch (TKind) { case TSK_Undeclared: @@ -164,9 +165,10 @@ static const Decl *adjustTemplateImplicitInstantiation(const Decl *D) { if (const ClassTemplateSpecializationDecl * SD = dyn_cast(D)) { return SD->getTemplateInstantiationPattern(); - } - if (const FunctionDecl *FD = dyn_cast(D)) { + } else if (const FunctionDecl *FD = dyn_cast(D)) { return FD->getTemplateInstantiationPattern(); + } else if (auto *VD = dyn_cast(D)) { + return VD->getTemplateInstantiationPattern(); } return nullptr; } diff --git a/clang/lib/Index/USRGeneration.cpp b/clang/lib/Index/USRGeneration.cpp index bec777ddf4f9..58f61c3c65b7 100644 --- a/clang/lib/Index/USRGeneration.cpp +++ b/clang/lib/Index/USRGeneration.cpp @@ -286,6 +286,15 @@ void USRGenerator::VisitVarDecl(const VarDecl *D) { VisitDeclContext(D->getDeclContext()); + if (VarTemplateDecl *VarTmpl = D->getDescribedVarTemplate()) { + Out << "@VT"; + VisitTemplateParameterList(VarTmpl->getTemplateParameters()); + } else if (const VarTemplatePartialSpecializationDecl *PartialSpec + = dyn_cast(D)) { + Out << "@VP"; + VisitTemplateParameterList(PartialSpec->getTemplateParameters()); + } + // Variables always have simple names. StringRef s = D->getName(); @@ -297,6 +306,17 @@ void USRGenerator::VisitVarDecl(const VarDecl *D) { IgnoreResults = true; else Out << '@' << s; + + // For a template specialization, mangle the template arguments. + if (const VarTemplateSpecializationDecl *Spec + = dyn_cast(D)) { + const TemplateArgumentList &Args = Spec->getTemplateInstantiationArgs(); + Out << '>'; + for (unsigned I = 0, N = Args.size(); I != N; ++I) { + Out << '#'; + VisitTemplateArgument(Args.get(I)); + } + } } void USRGenerator::VisitNonTypeTemplateParmDecl( diff --git a/clang/test/Index/Core/index-source.cpp b/clang/test/Index/Core/index-source.cpp index 61b9675240c6..7db5d531f43d 100644 --- a/clang/test/Index/Core/index-source.cpp +++ b/clang/test/Index/Core/index-source.cpp @@ -1,4 +1,4 @@ -// RUN: c-index-test core -print-source-symbols -- %s -target x86_64-apple-macosx10.7 | FileCheck %s +// RUN: c-index-test core -print-source-symbols -- %s -std=c++14 -target x86_64-apple-macosx10.7 | FileCheck %s template class TemplCls { @@ -29,3 +29,14 @@ typedef unsigned long size_t; // CHECK: [[@LINE+2]]:7 | function/C | operator new | c:@F@operator new#l# | __Znwm | // CHECK: [[@LINE+1]]:20 | type-alias/C | size_t | {{.*}} | Ref | void* operator new(size_t sz); + +// CHECK: [[@LINE+1]]:37 | variable(Gen)/C++ | tmplVar | c:index-source.cpp@VT>1#T@tmplVar | __ZL7tmplVar | Def | rel: 0 +template static const T tmplVar = T(0); +// CHECK: [[@LINE+1]]:29 | variable(Gen,TS)/C++ | tmplVar | c:index-source.cpp@tmplVar>#I | __ZL7tmplVarIiE | Def | rel: 0 +template<> static const int tmplVar = 0; +// CHECK: [[@LINE+2]]:5 | variable/C | gvi | c:@gvi | _gvi | Def | rel: 0 +// CHECK: [[@LINE+1]]:11 | variable(Gen,TS)/C++ | tmplVar | c:index-source.cpp@tmplVar>#I | __ZL7tmplVarIiE | Ref,Read | rel: 0 +int gvi = tmplVar; +// CHECK: [[@LINE+2]]:5 | variable/C | gvf | c:@gvf | _gvf | Def | rel: 0 +// CHECK: [[@LINE+1]]:11 | variable(Gen)/C++ | tmplVar | c:index-source.cpp@VT>1#T@tmplVar | __ZL7tmplVar | Ref,Read | rel: 0 +int gvf = tmplVar; -- 2.34.1