r260866 - [AST/index] Introduce an option 'SuppressTemplateArgsInCXXConstructors' in printing policy.

Argyrios Kyrtzidis via cfe-commits cfe-commits at lists.llvm.org
Sun Feb 14 17:32:36 PST 2016


Author: akirtzidis
Date: Sun Feb 14 19:32:36 2016
New Revision: 260866

URL: http://llvm.org/viewvc/llvm-project?rev=260866&view=rev
Log:
[AST/index] Introduce an option 'SuppressTemplateArgsInCXXConstructors' in printing policy.

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.

Added:
    cfe/trunk/test/Index/Core/index-source.cpp
Modified:
    cfe/trunk/include/clang/AST/PrettyPrinter.h
    cfe/trunk/include/clang/Index/IndexSymbol.h
    cfe/trunk/lib/AST/DeclarationName.cpp
    cfe/trunk/lib/Index/IndexSymbol.cpp
    cfe/trunk/lib/Index/USRGeneration.cpp
    cfe/trunk/tools/c-index-test/core_main.cpp

Modified: cfe/trunk/include/clang/AST/PrettyPrinter.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/PrettyPrinter.h?rev=260866&r1=260865&r2=260866&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/PrettyPrinter.h (original)
+++ cfe/trunk/include/clang/AST/PrettyPrinter.h Sun Feb 14 19:32:36 2016
@@ -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;

Modified: cfe/trunk/include/clang/Index/IndexSymbol.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Index/IndexSymbol.h?rev=260866&r1=260865&r2=260866&view=diff
==============================================================================
--- cfe/trunk/include/clang/Index/IndexSymbol.h (original)
+++ cfe/trunk/include/clang/Index/IndexSymbol.h Sun Feb 14 19:32:36 2016
@@ -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);

Modified: cfe/trunk/lib/AST/DeclarationName.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclarationName.cpp?rev=260866&r1=260865&r2=260866&view=diff
==============================================================================
--- cfe/trunk/lib/AST/DeclarationName.cpp (original)
+++ cfe/trunk/lib/AST/DeclarationName.cpp Sun Feb 14 19:32:36 2016
@@ -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 printCXXConstructorDestructo
     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;

Modified: cfe/trunk/lib/Index/IndexSymbol.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Index/IndexSymbol.cpp?rev=260866&r1=260865&r2=260866&view=diff
==============================================================================
--- cfe/trunk/lib/Index/IndexSymbol.cpp (original)
+++ cfe/trunk/lib/Index/IndexSymbol.cpp Sun Feb 14 19:32:36 2016
@@ -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(SymbolRoleS
   });
 }
 
+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>";

Modified: cfe/trunk/lib/Index/USRGeneration.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Index/USRGeneration.cpp?rev=260866&r1=260865&r2=260866&view=diff
==============================================================================
--- cfe/trunk/lib/Index/USRGeneration.cpp (original)
+++ cfe/trunk/lib/Index/USRGeneration.cpp Sun Feb 14 19:32:36 2016
@@ -210,7 +210,12 @@ void USRGenerator::VisitFunctionDecl(con
     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())

Added: cfe/trunk/test/Index/Core/index-source.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/Core/index-source.cpp?rev=260866&view=auto
==============================================================================
--- cfe/trunk/test/Index/Core/index-source.cpp (added)
+++ cfe/trunk/test/Index/Core/index-source.cpp Sun Feb 14 19:32:36 2016
@@ -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 at TemplCls | <no-cgname> | Def | rel: 0
+  TemplCls(int x);
+  // CHECK: [[@LINE-1]]:3 | constructor/C++ | TemplCls | c:@ST>1#T at TemplCls@F at TemplCls#I# | <no-cgname> | Decl/RelChild | rel: 1
+  // CHECK-NEXT: RelChild | TemplCls | c:@ST>1#T at TemplCls
+};

Modified: cfe/trunk/tools/c-index-test/core_main.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/c-index-test/core_main.cpp?rev=260866&r1=260865&r2=260866&view=diff
==============================================================================
--- cfe/trunk/tools/c-index-test/core_main.cpp (original)
+++ cfe/trunk/tools/c-index-test/core_main.cpp Sun Feb 14 19:32:36 2016
@@ -156,10 +156,7 @@ static void printSymbolInfo(SymbolInfo S
 
 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 << " | ";




More information about the cfe-commits mailing list