diff --git a/clang-tools-extra/clangd/AST.h b/clang-tools-extra/clangd/AST.h --- a/clang-tools-extra/clangd/AST.h +++ b/clang-tools-extra/clangd/AST.h @@ -80,19 +80,7 @@ /// Returns a QualType as string. The result doesn't contain unwritten scopes /// like annoymous/inline namespace. -std::string printType(const QualType QT, const DeclContext &Context); - -/// Try to shorten the OriginalName by removing namespaces from the left of -/// the string that are redundant in the CurrentNamespace. This way the type -/// idenfier become shorter and easier to read. -/// Limitation: It only handles the qualifier of the type itself, not that of -/// templates. -/// FIXME: change type of parameter CurrentNamespace to DeclContext , -/// take in to account using directives etc -/// Example: shortenNamespace("ns1::MyClass", "ns1") -/// --> "MyClass" -std::string shortenNamespace(const llvm::StringRef OriginalName, - const llvm::StringRef CurrentNamespace); +std::string printType(const QualType QT, const DeclContext &CurContext); /// Indicates if \p D is a template instantiation implicitly generated by the /// compiler, e.g. @@ -157,7 +145,7 @@ /// present in \p VisibleNamespaces, no matter whether it is from ns1:: or ns2:: std::string getQualification(ASTContext &Context, const DeclContext *DestContext, - SourceLocation InsertionPoint, const NamedDecl *ND, + const NamedDecl *ND, llvm::ArrayRef VisibleNamespaces); } // namespace clangd diff --git a/clang-tools-extra/clangd/AST.cpp b/clang-tools-extra/clangd/AST.cpp --- a/clang-tools-extra/clangd/AST.cpp +++ b/clang-tools-extra/clangd/AST.cpp @@ -299,32 +299,17 @@ return SymbolID(USR); } -std::string shortenNamespace(const llvm::StringRef OriginalName, - const llvm::StringRef CurrentNamespace) { - llvm::SmallVector OriginalParts; - llvm::SmallVector CurrentParts; - llvm::SmallVector Result; - OriginalName.split(OriginalParts, "::"); - CurrentNamespace.split(CurrentParts, "::"); - auto MinLength = std::min(CurrentParts.size(), OriginalParts.size()); - - unsigned DifferentAt = 0; - while (DifferentAt < MinLength && - CurrentParts[DifferentAt] == OriginalParts[DifferentAt]) { - DifferentAt++; - } - - for (unsigned i = DifferentAt; i < OriginalParts.size(); ++i) { - Result.push_back(OriginalParts[i]); - } - return join(Result, "::"); -} - -std::string printType(const QualType QT, const DeclContext &Context) { - PrintingPolicy PP(Context.getParentASTContext().getPrintingPolicy()); - PP.SuppressUnwrittenScope = 1; - PP.SuppressTagKeyword = 1; - return shortenNamespace(QT.getAsString(PP), printNamespaceScope(Context)); +std::string printType(const QualType QT, const DeclContext &CurContext) { + std::string Result; + llvm::raw_string_ostream OS(Result); + if (auto *TD = QT->getAsTagDecl()) + OS << getQualification(CurContext.getParentASTContext(), &CurContext, TD, + /*VisibleNamespaces=*/llvm::ArrayRef{}); + PrintingPolicy PP(CurContext.getParentASTContext().getPrintingPolicy()); + PP.SuppressScope = true; + PP.SuppressTagKeyword = true; + QT.print(OS, PP); + return OS.str(); } QualType declaredType(const TypeDecl *D) { @@ -464,7 +449,7 @@ std::string getQualification(ASTContext &Context, const DeclContext *DestContext, - SourceLocation InsertionPoint, const NamedDecl *ND, + const NamedDecl *ND, llvm::ArrayRef VisibleNamespaces) { for (llvm::StringRef NS : VisibleNamespaces) { assert(NS.endswith("::")); diff --git a/clang-tools-extra/clangd/unittests/ASTTests.cpp b/clang-tools-extra/clangd/unittests/ASTTests.cpp --- a/clang-tools-extra/clangd/unittests/ASTTests.cpp +++ b/clang-tools-extra/clangd/unittests/ASTTests.cpp @@ -25,29 +25,6 @@ namespace clangd { namespace { -TEST(ShortenNamespace, All) { - ASSERT_EQ("TestClass", shortenNamespace("TestClass", "")); - - ASSERT_EQ("TestClass", shortenNamespace( - "testnamespace::TestClass", "testnamespace")); - - ASSERT_EQ( - "namespace1::TestClass", - shortenNamespace("namespace1::TestClass", "namespace2")); - - ASSERT_EQ("TestClass", - shortenNamespace("testns1::testns2::TestClass", - "testns1::testns2")); - - ASSERT_EQ( - "testns2::TestClass", - shortenNamespace("testns1::testns2::TestClass", "testns1")); - - ASSERT_EQ("TestClass", - shortenNamespace( - "testns1::TestClass", "testns1")); -} - TEST(GetDeducedType, KwAutoExpansion) { struct Test { StringRef AnnotatedCode; @@ -166,8 +143,8 @@ Case.Qualifications[I]); } else { EXPECT_EQ(getQualification(AST.getASTContext(), - D->getLexicalDeclContext(), D->getBeginLoc(), - TargetDecl, Case.VisibleNamespaces), + D->getLexicalDeclContext(), TargetDecl, + Case.VisibleNamespaces), Case.Qualifications[I]); } }