kadircet created this revision.
kadircet added a reviewer: ilya-biryukov.
Herald added subscribers: cfe-commits, usaxena95, arphaman, jkorous, MaskRay.
Herald added a project: clang.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D71652

Files:
  clang-tools-extra/clangd/AST.cpp
  clang-tools-extra/clangd/AST.h
  clang-tools-extra/clangd/unittests/ASTTests.cpp

Index: clang-tools-extra/clangd/unittests/ASTTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/ASTTests.cpp
+++ 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<testns1::OtherClass>",
-            shortenNamespace(
-                "testns1::TestClass<testns1::OtherClass>", "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]);
       }
     }
Index: clang-tools-extra/clangd/AST.h
===================================================================
--- clang-tools-extra/clangd/AST.h
+++ 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::OtherClass>", "ns1")
-///    --> "MyClass<ns1::OtherClass>"
-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<std::string> VisibleNamespaces);
 
 } // namespace clangd
Index: clang-tools-extra/clangd/AST.cpp
===================================================================
--- clang-tools-extra/clangd/AST.cpp
+++ 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<llvm::StringRef, 8> OriginalParts;
-  llvm::SmallVector<llvm::StringRef, 8> CurrentParts;
-  llvm::SmallVector<llvm::StringRef, 8> 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<std::string>{});
+  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<std::string> VisibleNamespaces) {
   for (llvm::StringRef NS : VisibleNamespaces) {
     assert(NS.endswith("::"));
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to