]> granicus.if.org Git - clang/commitdiff
[AST/index] Introduce an option 'SuppressTemplateArgsInCXXConstructors' in printing...
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Mon, 15 Feb 2016 01:32:36 +0000 (01:32 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Mon, 15 Feb 2016 01:32:36 +0000 (01:32 +0000)
Enable it for USRs and names when indexing.
Forward references can have different template argument names; including them
makes USRs and names unstable, since the name depends on whether we saw a forward reference or not.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@260866 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/PrettyPrinter.h
include/clang/Index/IndexSymbol.h
lib/AST/DeclarationName.cpp
lib/Index/IndexSymbol.cpp
lib/Index/USRGeneration.cpp
test/Index/Core/index-source.cpp [new file with mode: 0644]
tools/c-index-test/core_main.cpp

index 8ab3f61703a3c75fb2938f98a12325b6ae97cbc5..57495efa96639b0bb551430f07ba44e17433cd2b 100644 (file)
@@ -40,6 +40,7 @@ struct PrintingPolicy {
       SuppressUnwrittenScope(false), SuppressInitializers(false),
       ConstantArraySizeAsWritten(false), AnonymousTagLocations(true),
       SuppressStrongLifetime(false), SuppressLifetimeQualifiers(false),
+      SuppressTemplateArgsInCXXConstructors(false),
       Bool(LO.Bool), TerseOutput(false), PolishForDeclaration(false),
       Half(LO.Half), MSWChar(LO.MicrosoftExt && !LO.WChar),
       IncludeNewlines(true), MSVCFormatting(false) { }
@@ -136,7 +137,11 @@ struct PrintingPolicy {
   /// \brief When true, suppress printing of lifetime qualifier in
   /// ARC.
   unsigned SuppressLifetimeQualifiers : 1;
-  
+
+  /// When true, suppresses printing template arguments in names of C++
+  /// constructors.
+  unsigned SuppressTemplateArgsInCXXConstructors : 1;
+
   /// \brief Whether we can use 'bool' rather than '_Bool', even if the language
   /// doesn't actually have 'bool' (because, e.g., it is defined as a macro).
   unsigned Bool : 1;
index feee13cdbf5757d40bb914a5d5ef15a0e9f1cddd..506540b0e6e642b75990198fc3d70ab9e39dc15c 100644 (file)
@@ -16,6 +16,7 @@
 
 namespace clang {
   class Decl;
+  class LangOptions;
 
 namespace index {
 
@@ -111,6 +112,10 @@ SymbolInfo getSymbolInfo(const Decl *D);
 void applyForEachSymbolRole(SymbolRoleSet Roles,
                             llvm::function_ref<void(SymbolRole)> Fn);
 void printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS);
+
+/// \returns true if no name was printed, false otherwise.
+bool printSymbolName(const Decl *D, const LangOptions &LO, raw_ostream &OS);
+
 StringRef getSymbolKindString(SymbolKind K);
 StringRef getTemplateKindStr(SymbolCXXTemplateKind TK);
 StringRef getSymbolLanguageString(SymbolLanguage K);
index f6d045b2dab20262bd779524b10f08105f06eda7..344a23892281ce879ea3a91cdb88b94c6ba74a64 100644 (file)
@@ -12,7 +12,7 @@
 //
 //===----------------------------------------------------------------------===//
 #include "clang/AST/ASTContext.h"
-#include "clang/AST/Decl.h"
+#include "clang/AST/DeclCXX.h"
 #include "clang/AST/DeclarationName.h"
 #include "clang/AST/Type.h"
 #include "clang/AST/TypeLoc.h"
@@ -140,6 +140,12 @@ static void printCXXConstructorDestructorName(QualType ClassType,
     OS << *ClassRec->getDecl();
     return;
   }
+  if (Policy.SuppressTemplateArgsInCXXConstructors) {
+    if (auto *InjTy = ClassType->getAs<InjectedClassNameType>()) {
+      OS << *InjTy->getDecl();
+      return;
+    }
+  }
   if (!Policy.LangOpts.CPlusPlus) {
     // Passed policy is the default one from operator <<, use a C++ policy.
     LangOptions LO;
index 95ae9776826ffaa15aa323ca534313c8968e3283..010ccd42a4a184a9ef3a9c9eb92cccae4e6c842f 100644 (file)
@@ -11,6 +11,7 @@
 #include "clang/AST/DeclCXX.h"
 #include "clang/AST/DeclObjC.h"
 #include "clang/AST/DeclTemplate.h"
+#include "clang/AST/PrettyPrinter.h"
 
 using namespace clang;
 using namespace clang::index;
@@ -234,6 +235,24 @@ void index::printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS) {
   });
 }
 
+bool index::printSymbolName(const Decl *D, const LangOptions &LO,
+                            raw_ostream &OS) {
+  if (auto *ND = dyn_cast<NamedDecl>(D)) {
+    PrintingPolicy Policy(LO);
+    // Forward references can have different template argument names. Suppress
+    // the template argument names in constructors to make their name more
+    // stable.
+    Policy.SuppressTemplateArgsInCXXConstructors = true;
+    DeclarationName DeclName = ND->getDeclName();
+    if (DeclName.isEmpty())
+      return true;
+    DeclName.print(OS, Policy);
+    return false;
+  } else {
+    return true;
+  }
+}
+
 StringRef index::getSymbolKindString(SymbolKind K) {
   switch (K) {
   case SymbolKind::Unknown: return "<unknown>";
index 3b2954f977026bebea3e99b2644afecc7b26bbf1..c15838c76c6310423287b6c866fde3ce0bca5510 100644 (file)
@@ -210,7 +210,12 @@ void USRGenerator::VisitFunctionDecl(const FunctionDecl *D) {
     VisitTemplateParameterList(FunTmpl->getTemplateParameters());
   } else
     Out << "@F@";
-  D->printName(Out);
+
+  PrintingPolicy Policy(Context->getLangOpts());
+  // Forward references can have different template argument names. Suppress the
+  // template argument names in constructors to make their USR more stable.
+  Policy.SuppressTemplateArgsInCXXConstructors = true;
+  D->getDeclName().print(Out, Policy);
 
   ASTContext &Ctx = *Context;
   if (!Ctx.getLangOpts().CPlusPlus || D->isExternC())
diff --git a/test/Index/Core/index-source.cpp b/test/Index/Core/index-source.cpp
new file mode 100644 (file)
index 0000000..7544646
--- /dev/null
@@ -0,0 +1,9 @@
+// RUN: c-index-test core -print-source-symbols -- %s -target x86_64-apple-macosx10.7 | FileCheck %s
+
+template <typename TemplArg>
+class TemplCls {
+// CHECK: [[@LINE-1]]:7 | c++-class/C++ | TemplCls | c:@ST>1#T@TemplCls | <no-cgname> | Def | rel: 0
+  TemplCls(int x);
+  // CHECK: [[@LINE-1]]:3 | constructor/C++ | TemplCls | c:@ST>1#T@TemplCls@F@TemplCls#I# | <no-cgname> | Decl/RelChild | rel: 1
+  // CHECK-NEXT: RelChild | TemplCls | c:@ST>1#T@TemplCls
+};
index e37b3422ac6a17fd2e3f32e5fa629cc1c48397e8..e72b9f93efd269127efeeb9f0cacf45ff757257b 100644 (file)
@@ -156,10 +156,7 @@ static void printSymbolInfo(SymbolInfo SymInfo, raw_ostream &OS) {
 
 static void printSymbolNameAndUSR(const Decl *D, ASTContext &Ctx,
                                   raw_ostream &OS) {
-  if (auto *ND = dyn_cast<NamedDecl>(D)) {
-    PrintingPolicy PrintPolicy(Ctx.getLangOpts());
-    ND->getDeclName().print(OS, PrintPolicy);
-  } else {
+  if (printSymbolName(D, Ctx.getLangOpts(), OS)) {
     OS << "<no-name>";
   }
   OS << " | ";