[clang-tools-extra] b2eaac3 - [clangd] Replace shortenNamespace with getQualification
Kadir Cetinkaya via cfe-commits
cfe-commits at lists.llvm.org
Fri Jan 3 00:08:44 PST 2020
Author: Kadir Cetinkaya
Date: 2020-01-03T09:05:30+01:00
New Revision: b2eaac3e3e0a6177f16b3e5c2a4c7c6a85104ff5
URL: https://github.com/llvm/llvm-project/commit/b2eaac3e3e0a6177f16b3e5c2a4c7c6a85104ff5
DIFF: https://github.com/llvm/llvm-project/commit/b2eaac3e3e0a6177f16b3e5c2a4c7c6a85104ff5.diff
LOG: [clangd] Replace shortenNamespace with getQualification
Reviewers: ilya-biryukov
Subscribers: MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D71652
Added:
Modified:
clang-tools-extra/clangd/AST.cpp
clang-tools-extra/clangd/AST.h
clang-tools-extra/clangd/unittests/ASTTests.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clangd/AST.cpp b/clang-tools-extra/clangd/AST.cpp
index d06245a53977..c800ee870dc9 100644
--- a/clang-tools-extra/clangd/AST.cpp
+++ b/clang-tools-extra/clangd/AST.cpp
@@ -8,8 +8,10 @@
#include "AST.h"
+#include "FindTarget.h"
#include "SourceCode.h"
#include "clang/AST/ASTContext.h"
+#include "clang/AST/ASTTypeTraits.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclBase.h"
#include "clang/AST/DeclCXX.h"
@@ -299,32 +301,21 @@ llvm::Optional<SymbolID> getSymbolID(const llvm::StringRef MacroName,
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));
+// FIXME: This should be handled while printing underlying decls instead.
+std::string printType(const QualType QT, const DeclContext &CurContext) {
+ std::string Result;
+ llvm::raw_string_ostream OS(Result);
+ auto Decls = explicitReferenceTargets(
+ ast_type_traits::DynTypedNode::create(QT), DeclRelation::Alias);
+ if (!Decls.empty())
+ OS << getQualification(CurContext.getParentASTContext(), &CurContext,
+ Decls.front(),
+ /*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 +455,7 @@ std::string getQualification(ASTContext &Context,
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("::"));
diff --git a/clang-tools-extra/clangd/AST.h b/clang-tools-extra/clangd/AST.h
index a823365e7053..f65c42869b74 100644
--- a/clang-tools-extra/clangd/AST.h
+++ b/clang-tools-extra/clangd/AST.h
@@ -80,19 +80,7 @@ llvm::Optional<SymbolID> getSymbolID(const llvm::StringRef MacroName,
/// 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 @@ std::string getQualification(ASTContext &Context,
/// 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
diff --git a/clang-tools-extra/clangd/unittests/ASTTests.cpp b/clang-tools-extra/clangd/unittests/ASTTests.cpp
index 90ded128d577..80495883a47b 100644
--- a/clang-tools-extra/clangd/unittests/ASTTests.cpp
+++ b/clang-tools-extra/clangd/unittests/ASTTests.cpp
@@ -15,6 +15,7 @@
#include "clang/AST/DeclBase.h"
#include "clang/Basic/SourceManager.h"
#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/Casting.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <cstddef>
@@ -25,29 +26,6 @@ namespace clang {
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,14 +144,70 @@ TEST(ClangdAST, GetQualification) {
Case.Qualifications[I]);
} else {
EXPECT_EQ(getQualification(AST.getASTContext(),
- D->getLexicalDeclContext(), D->getBeginLoc(),
- TargetDecl, Case.VisibleNamespaces),
+ D->getLexicalDeclContext(), TargetDecl,
+ Case.VisibleNamespaces),
Case.Qualifications[I]);
}
}
}
}
+TEST(ClangdAST, PrintType) {
+ const struct {
+ llvm::StringRef Test;
+ std::vector<llvm::StringRef> Types;
+ } Cases[] = {
+ {
+ R"cpp(
+ namespace ns1 { namespace ns2 { class Foo {}; } }
+ void insert(); // ns1::ns2::Foo
+ namespace ns1 {
+ void insert(); // ns2::Foo
+ namespace ns2 {
+ void insert(); // Foo
+ }
+ }
+ )cpp",
+ {"ns1::ns2::Foo", "ns2::Foo", "Foo"},
+ },
+ {
+ R"cpp(
+ namespace ns1 {
+ typedef int Foo;
+ }
+ void insert(); // ns1::Foo
+ namespace ns1 {
+ void insert(); // Foo
+ }
+ )cpp",
+ {"ns1::Foo", "Foo"},
+ },
+ };
+ for (const auto &Case : Cases) {
+ Annotations Test(Case.Test);
+ TestTU TU = TestTU::withCode(Test.code());
+ ParsedAST AST = TU.build();
+ std::vector<const DeclContext *> InsertionPoints;
+ const TypeDecl *TargetDecl = nullptr;
+ findDecl(AST, [&](const NamedDecl &ND) {
+ if (ND.getNameAsString() == "Foo") {
+ if (const auto *TD = llvm::dyn_cast<TypeDecl>(&ND)) {
+ TargetDecl = TD;
+ return true;
+ }
+ } else if (ND.getNameAsString() == "insert")
+ InsertionPoints.push_back(ND.getDeclContext());
+ return false;
+ });
+
+ ASSERT_EQ(InsertionPoints.size(), Case.Types.size());
+ for (size_t I = 0, E = InsertionPoints.size(); I != E; ++I) {
+ const auto *DC = InsertionPoints[I];
+ EXPECT_EQ(printType(AST.getASTContext().getTypeDeclType(TargetDecl), *DC),
+ Case.Types[I]);
+ }
+ }
+}
} // namespace
} // namespace clangd
} // namespace clang
More information about the cfe-commits
mailing list