r286154 - [index] Handle properly C++14's template variables.

Argyrios Kyrtzidis via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 7 13:20:15 PST 2016


Author: akirtzidis
Date: Mon Nov  7 15:20:15 2016
New Revision: 286154

URL: http://llvm.org/viewvc/llvm-project?rev=286154&view=rev
Log:
[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

Modified:
    cfe/trunk/lib/Index/IndexSymbol.cpp
    cfe/trunk/lib/Index/IndexingContext.cpp
    cfe/trunk/lib/Index/USRGeneration.cpp
    cfe/trunk/test/Index/Core/index-source.cpp

Modified: cfe/trunk/lib/Index/IndexSymbol.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Index/IndexSymbol.cpp?rev=286154&r1=286153&r2=286154&view=diff
==============================================================================
--- cfe/trunk/lib/Index/IndexSymbol.cpp (original)
+++ cfe/trunk/lib/Index/IndexSymbol.cpp Mon Nov  7 15:20:15 2016
@@ -91,6 +91,25 @@ SymbolInfo index::getSymbolInfo(const De
       Info.SubKinds |= (unsigned)SymbolSubKind::TemplateSpecialization;
     }
 
+  } else if (auto *VD = dyn_cast<VarDecl>(D)) {
+    Info.Kind = SymbolKind::Variable;
+    if (isa<CXXRecordDecl>(D->getDeclContext())) {
+      Info.Kind = SymbolKind::StaticProperty;
+      Info.Lang = SymbolLanguage::CXX;
+    }
+    if (isa<VarTemplatePartialSpecializationDecl>(D)) {
+      Info.Lang = SymbolLanguage::CXX;
+      Info.SubKinds |= (unsigned)SymbolSubKind::Generic;
+      Info.SubKinds |= (unsigned)SymbolSubKind::TemplatePartialSpecialization;
+    } else if (isa<VarTemplateSpecializationDecl>(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 De
     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<CXXRecordDecl>(D->getDeclContext())) {
-        Info.Kind = SymbolKind::StaticProperty;
-        Info.Lang = SymbolLanguage::CXX;
-      }
-      break;
     case Decl::Field:
       Info.Kind = SymbolKind::Field;
       if (const CXXRecordDecl *

Modified: cfe/trunk/lib/Index/IndexingContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Index/IndexingContext.cpp?rev=286154&r1=286153&r2=286154&view=diff
==============================================================================
--- cfe/trunk/lib/Index/IndexingContext.cpp (original)
+++ cfe/trunk/lib/Index/IndexingContext.cpp Mon Nov  7 15:20:15 2016
@@ -130,9 +130,10 @@ bool IndexingContext::isTemplateImplicit
   if (const ClassTemplateSpecializationDecl *
       SD = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
     TKind = SD->getSpecializationKind();
-  }
-  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
+  } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
     TKind = FD->getTemplateSpecializationKind();
+  } else if (auto *VD = dyn_cast<VarDecl>(D)) {
+    TKind = VD->getTemplateSpecializationKind();
   }
   switch (TKind) {
     case TSK_Undeclared:
@@ -164,9 +165,10 @@ static const Decl *adjustTemplateImplici
   if (const ClassTemplateSpecializationDecl *
       SD = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
     return SD->getTemplateInstantiationPattern();
-  }
-  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
+  } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
     return FD->getTemplateInstantiationPattern();
+  } else if (auto *VD = dyn_cast<VarDecl>(D)) {
+    return VD->getTemplateInstantiationPattern();
   }
   return nullptr;
 }

Modified: cfe/trunk/lib/Index/USRGeneration.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Index/USRGeneration.cpp?rev=286154&r1=286153&r2=286154&view=diff
==============================================================================
--- cfe/trunk/lib/Index/USRGeneration.cpp (original)
+++ cfe/trunk/lib/Index/USRGeneration.cpp Mon Nov  7 15:20:15 2016
@@ -286,6 +286,15 @@ void USRGenerator::VisitVarDecl(const Va
 
   VisitDeclContext(D->getDeclContext());
 
+  if (VarTemplateDecl *VarTmpl = D->getDescribedVarTemplate()) {
+    Out << "@VT";
+    VisitTemplateParameterList(VarTmpl->getTemplateParameters());
+  } else if (const VarTemplatePartialSpecializationDecl *PartialSpec
+             = dyn_cast<VarTemplatePartialSpecializationDecl>(D)) {
+    Out << "@VP";
+    VisitTemplateParameterList(PartialSpec->getTemplateParameters());
+  }
+
   // Variables always have simple names.
   StringRef s = D->getName();
 
@@ -297,6 +306,17 @@ void USRGenerator::VisitVarDecl(const Va
     IgnoreResults = true;
   else
     Out << '@' << s;
+
+  // For a template specialization, mangle the template arguments.
+  if (const VarTemplateSpecializationDecl *Spec
+                              = dyn_cast<VarTemplateSpecializationDecl>(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(

Modified: cfe/trunk/test/Index/Core/index-source.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/Core/index-source.cpp?rev=286154&r1=286153&r2=286154&view=diff
==============================================================================
--- cfe/trunk/test/Index/Core/index-source.cpp (original)
+++ cfe/trunk/test/Index/Core/index-source.cpp Mon Nov  7 15:20:15 2016
@@ -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 <typename TemplArg>
 class TemplCls {
@@ -29,3 +29,14 @@ typedef unsigned long size_t;
 // CHECK: [[@LINE+2]]:7 | function/C | operator new | c:@F at 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 at VT>1#T at tmplVar | __ZL7tmplVar | Def | rel: 0
+template<typename T> static const T tmplVar = T(0);
+// CHECK: [[@LINE+1]]:29 | variable(Gen,TS)/C++ | tmplVar | c:index-source.cpp at tmplVar>#I | __ZL7tmplVarIiE | Def | rel: 0
+template<> static const int tmplVar<int> = 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 at tmplVar>#I | __ZL7tmplVarIiE | Ref,Read | rel: 0
+int gvi = tmplVar<int>;
+// 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 at VT>1#T at tmplVar | __ZL7tmplVar | Ref,Read | rel: 0
+int gvf = tmplVar<float>;




More information about the cfe-commits mailing list