From 80cb549c2fb973ffa84276b6144e0aa65ef690c9 Mon Sep 17 00:00:00 2001 From: Alex Lorenz Date: Mon, 22 May 2017 16:50:54 +0000 Subject: [PATCH] [index] Index the default template parameter values rdar://32323724 llvm-svn: 303568 --- clang/lib/Index/IndexDecl.cpp | 46 ++++++++++++++++++++++++++ clang/test/Index/Core/index-source.cpp | 59 ++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) diff --git a/clang/lib/Index/IndexDecl.cpp b/clang/lib/Index/IndexDecl.cpp index 7f940ef..870b4e4 100644 --- a/clang/lib/Index/IndexDecl.cpp +++ b/clang/lib/Index/IndexDecl.cpp @@ -63,6 +63,17 @@ public: case TemplateArgument::Type: IndexCtx.indexTypeSourceInfo(LocInfo.getAsTypeSourceInfo(), Parent, DC); break; + case TemplateArgument::Template: + case TemplateArgument::TemplateExpansion: + IndexCtx.indexNestedNameSpecifierLoc(TALoc.getTemplateQualifierLoc(), + Parent, DC); + if (const TemplateDecl *TD = TALoc.getArgument() + .getAsTemplateOrTemplatePattern() + .getAsTemplateDecl()) { + if (const NamedDecl *TTD = TD->getTemplatedDecl()) + IndexCtx.handleReference(TTD, TALoc.getTemplateNameLoc(), Parent, DC); + } + break; default: break; } @@ -610,8 +621,43 @@ public: return true; } + static bool shouldIndexTemplateParameterDefaultValue(const NamedDecl *D) { + if (!D) + return false; + // We want to index the template parameters only once when indexing the + // canonical declaration. + if (const auto *FD = dyn_cast(D)) + return FD->getCanonicalDecl() == FD; + else if (const auto *TD = dyn_cast(D)) + return TD->getCanonicalDecl() == TD; + else if (const auto *VD = dyn_cast(D)) + return VD->getCanonicalDecl() == VD; + return true; + } + bool VisitTemplateDecl(const TemplateDecl *D) { // FIXME: Template parameters. + + // Index the default values for the template parameters. + const NamedDecl *Parent = D->getTemplatedDecl(); + if (D->getTemplateParameters() && + shouldIndexTemplateParameterDefaultValue(Parent)) { + const TemplateParameterList *Params = D->getTemplateParameters(); + for (const NamedDecl *TP : *Params) { + if (const auto *TTP = dyn_cast(TP)) { + if (TTP->hasDefaultArgument()) + IndexCtx.indexTypeSourceInfo(TTP->getDefaultArgumentInfo(), Parent); + } else if (const auto *NTTP = dyn_cast(TP)) { + if (NTTP->hasDefaultArgument()) + IndexCtx.indexBody(NTTP->getDefaultArgument(), Parent); + } else if (const auto *TTPD = dyn_cast(TP)) { + if (TTPD->hasDefaultArgument()) + handleTemplateArgumentLoc(TTPD->getDefaultArgument(), Parent, + /*DC=*/nullptr); + } + } + } + return Visit(D->getTemplatedDecl()); } diff --git a/clang/test/Index/Core/index-source.cpp b/clang/test/Index/Core/index-source.cpp index e5f58d1..8b04931 100644 --- a/clang/test/Index/Core/index-source.cpp +++ b/clang/test/Index/Core/index-source.cpp @@ -374,3 +374,62 @@ struct DeletedMethods { // CHECK: [[@LINE-3]]:24 | struct/C++ | DeletedMethods | c:@S@DeletedMethods | | Ref,RelCont | rel: 1 // CHECK: [[@LINE-4]]:3 | struct/C++ | DeletedMethods | c:@S@DeletedMethods | | Ref,RelCont | rel: 1 }; + +namespace ns2 { +template struct ACollectionDecl { }; +} + +template | Ref,RelCont | rel: 1 +// CHECK-NEXT: RelCont | TemplateDefaultValues | c:@ST>3#T#NI#t>1#T@TemplateDefaultValues + int x = Record::C, +// CHECK: [[@LINE-1]]:26 | static-property/C++ | C | c:@S@Record@C | __ZN6Record1CE | Ref,Read,RelCont | rel: 1 +// CHECK-NEXT: RelCont | TemplateDefaultValues | c:@ST>3#T#NI#t>1#T@TemplateDefaultValues +// CHECK: [[@LINE-3]]:18 | struct/C++ | Record | c:@S@Record | | Ref,RelCont | rel: 1 + template class Collection = ns2::ACollectionDecl> +// CHECK: [[@LINE-1]]:49 | namespace/C++ | ns2 | c:@N@ns2 | | Ref,RelCont | rel: 1 +// CHECK-NEXT: RelCont | TemplateDefaultValues | c:@ST>3#T#NI#t>1#T@TemplateDefaultValues +// CHECK: [[@LINE-3]]:54 | struct(Gen)/C++ | ACollectionDecl | c:@N@ns2@ST>1#T@ACollectionDecl | | Ref,RelCont | rel: 1 +// CHECK-NEXT: RelCont | TemplateDefaultValues | c:@ST>3#T#NI#t>1#T@TemplateDefaultValues +struct TemplateDefaultValues { }; + +template | Ref,RelCont | rel: 1 + int x = sizeof(Cls)> +// CHECK: [[@LINE-1]]:25 | class/C++ | Cls | c:@S@Cls | | Ref,RelCont | rel: 1 +void functionTemplateDefaultValues() { } + +namespace ensureDefaultTemplateParamsAreRecordedOnce { + +template +// CHECK: [[@LINE-1]]:23 | class/C++ | Cls | c:@S@Cls | | Ref,RelCont | rel: 1 +// CHECK-NOT: [[@LINE-2]]:23 +void functionDecl(); + +template +void functionDecl() { } + +template +// CHECK: [[@LINE-1]]:23 | class/C++ | Cls | c:@S@Cls | | Ref,RelCont | rel: 1 +// CHECK-NOT: [[@LINE-2]]:23 +class TagDecl; + +template +class TagDecl; + +template +class TagDecl { }; + +template +// CHECK: [[@LINE-1]]:23 | class/C++ | Cls | c:@S@Cls | | Ref,RelCont | rel: 1 +using TypeAlias = TagDecl; + +template +// CHECK: [[@LINE-1]]:23 | class/C++ | Cls | c:@S@Cls | | Ref,RelCont | rel: 1 +// CHECK-NOT: [[@LINE-2]]:23 +extern T varDecl; + +template +T varDecl = T(); + +} // end namespace ensureDefaultTemplateParamsAreRecordedOnce -- 2.7.4