From: Argyrios Kyrtzidis Date: Mon, 15 Feb 2016 01:32:36 +0000 (+0000) Subject: [AST/index] Introduce an option 'SuppressTemplateArgsInCXXConstructors' in printing... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1c8c47123f17c4ae9a4de07ee453782b9dae8757;p=clang [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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@260866 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/PrettyPrinter.h b/include/clang/AST/PrettyPrinter.h index 8ab3f61703..57495efa96 100644 --- a/include/clang/AST/PrettyPrinter.h +++ b/include/clang/AST/PrettyPrinter.h @@ -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; diff --git a/include/clang/Index/IndexSymbol.h b/include/clang/Index/IndexSymbol.h index feee13cdbf..506540b0e6 100644 --- a/include/clang/Index/IndexSymbol.h +++ b/include/clang/Index/IndexSymbol.h @@ -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 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); diff --git a/lib/AST/DeclarationName.cpp b/lib/AST/DeclarationName.cpp index f6d045b2da..344a238922 100644 --- a/lib/AST/DeclarationName.cpp +++ b/lib/AST/DeclarationName.cpp @@ -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()) { + OS << *InjTy->getDecl(); + return; + } + } if (!Policy.LangOpts.CPlusPlus) { // Passed policy is the default one from operator <<, use a C++ policy. LangOptions LO; diff --git a/lib/Index/IndexSymbol.cpp b/lib/Index/IndexSymbol.cpp index 95ae977682..010ccd42a4 100644 --- a/lib/Index/IndexSymbol.cpp +++ b/lib/Index/IndexSymbol.cpp @@ -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(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 ""; diff --git a/lib/Index/USRGeneration.cpp b/lib/Index/USRGeneration.cpp index 3b2954f977..c15838c76c 100644 --- a/lib/Index/USRGeneration.cpp +++ b/lib/Index/USRGeneration.cpp @@ -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 index 0000000000..7544646826 --- /dev/null +++ b/test/Index/Core/index-source.cpp @@ -0,0 +1,9 @@ +// RUN: c-index-test core -print-source-symbols -- %s -target x86_64-apple-macosx10.7 | FileCheck %s + +template +class TemplCls { +// CHECK: [[@LINE-1]]:7 | c++-class/C++ | TemplCls | c:@ST>1#T@TemplCls | | Def | rel: 0 + TemplCls(int x); + // CHECK: [[@LINE-1]]:3 | constructor/C++ | TemplCls | c:@ST>1#T@TemplCls@F@TemplCls#I# | | Decl/RelChild | rel: 1 + // CHECK-NEXT: RelChild | TemplCls | c:@ST>1#T@TemplCls +}; diff --git a/tools/c-index-test/core_main.cpp b/tools/c-index-test/core_main.cpp index e37b3422ac..e72b9f93ef 100644 --- a/tools/c-index-test/core_main.cpp +++ b/tools/c-index-test/core_main.cpp @@ -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(D)) { - PrintingPolicy PrintPolicy(Ctx.getLangOpts()); - ND->getDeclName().print(OS, PrintPolicy); - } else { + if (printSymbolName(D, Ctx.getLangOpts(), OS)) { OS << ""; } OS << " | ";