diff options
author | Ilya Biryukov <ibiryukov@google.com> | 2019-09-25 13:09:10 +0000 |
---|---|---|
committer | Ilya Biryukov <ibiryukov@google.com> | 2019-09-25 13:09:10 +0000 |
commit | 73bc9c1440b57a5bfc1f3879ffd7002fdf5877ee (patch) | |
tree | b91c866d3dcaeb8051ec48775da95637f07af7e6 /unittests | |
parent | c80478b976273e88f936ed14303f2e0a00792bae (diff) | |
download | clang-73bc9c1440b57a5bfc1f3879ffd7002fdf5877ee.tar.gz |
[AST] Extract Decl::printNestedNameSpecifier helper from Decl::printQualifiedName
Summary:
To be used in clangd, e.g. in D66647.
Currently the alternative to this function is doing string manipulation on results of `printQualifiedName`, which is
hard-to-impossible to get right in presence of template arguments.
Reviewers: kadircet, aaron.ballman
Reviewed By: kadircet, aaron.ballman
Subscribers: aaron.ballman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D67825
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@372863 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests')
-rw-r--r-- | unittests/AST/NamedDeclPrinterTest.cpp | 70 |
1 files changed, 58 insertions, 12 deletions
diff --git a/unittests/AST/NamedDeclPrinterTest.cpp b/unittests/AST/NamedDeclPrinterTest.cpp index a50626517f..a5c3e19055 100644 --- a/unittests/AST/NamedDeclPrinterTest.cpp +++ b/unittests/AST/NamedDeclPrinterTest.cpp @@ -16,9 +16,12 @@ //===----------------------------------------------------------------------===// #include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/PrettyPrinter.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/Tooling/Tooling.h" #include "llvm/ADT/SmallString.h" +#include "llvm/Support/raw_ostream.h" #include "gtest/gtest.h" using namespace clang; @@ -30,11 +33,12 @@ namespace { class PrintMatch : public MatchFinder::MatchCallback { SmallString<1024> Printed; unsigned NumFoundDecls; - bool SuppressUnwrittenScope; + std::function<void(llvm::raw_ostream &OS, const NamedDecl *)> Printer; public: - explicit PrintMatch(bool suppressUnwrittenScope) - : NumFoundDecls(0), SuppressUnwrittenScope(suppressUnwrittenScope) {} + explicit PrintMatch( + std::function<void(llvm::raw_ostream &OS, const NamedDecl *)> Printer) + : NumFoundDecls(0), Printer(std::move(Printer)) {} void run(const MatchFinder::MatchResult &Result) override { const NamedDecl *ND = Result.Nodes.getNodeAs<NamedDecl>("id"); @@ -45,9 +49,7 @@ public: return; llvm::raw_svector_ostream Out(Printed); - PrintingPolicy Policy = Result.Context->getPrintingPolicy(); - Policy.SuppressUnwrittenScope = SuppressUnwrittenScope; - ND->printQualifiedName(Out, Policy); + Printer(Out, ND); } StringRef getPrinted() const { @@ -59,12 +61,12 @@ public: } }; -::testing::AssertionResult -PrintedNamedDeclMatches(StringRef Code, const std::vector<std::string> &Args, - bool SuppressUnwrittenScope, - const DeclarationMatcher &NodeMatch, - StringRef ExpectedPrinted, StringRef FileName) { - PrintMatch Printer(SuppressUnwrittenScope); +::testing::AssertionResult PrintedDeclMatches( + StringRef Code, const std::vector<std::string> &Args, + const DeclarationMatcher &NodeMatch, StringRef ExpectedPrinted, + StringRef FileName, + std::function<void(llvm::raw_ostream &, const NamedDecl *)> Print) { + PrintMatch Printer(std::move(Print)); MatchFinder Finder; Finder.addMatcher(NodeMatch, &Printer); std::unique_ptr<FrontendActionFactory> Factory = @@ -92,6 +94,21 @@ PrintedNamedDeclMatches(StringRef Code, const std::vector<std::string> &Args, } ::testing::AssertionResult +PrintedNamedDeclMatches(StringRef Code, const std::vector<std::string> &Args, + bool SuppressUnwrittenScope, + const DeclarationMatcher &NodeMatch, + StringRef ExpectedPrinted, StringRef FileName) { + return PrintedDeclMatches(Code, Args, NodeMatch, ExpectedPrinted, FileName, + [=](llvm::raw_ostream &Out, const NamedDecl *ND) { + auto Policy = + ND->getASTContext().getPrintingPolicy(); + Policy.SuppressUnwrittenScope = + SuppressUnwrittenScope; + ND->printQualifiedName(Out, Policy); + }); +} + +::testing::AssertionResult PrintedNamedDeclCXX98Matches(StringRef Code, StringRef DeclName, StringRef ExpectedPrinted) { std::vector<std::string> Args(1, "-std=c++98"); @@ -127,6 +144,17 @@ PrintedWrittenPropertyDeclObjCMatches(StringRef Code, StringRef DeclName, "input.m"); } +::testing::AssertionResult +PrintedNestedNameSpecifierMatches(StringRef Code, StringRef DeclName, + StringRef ExpectedPrinted) { + std::vector<std::string> Args{"-std=c++11"}; + return PrintedDeclMatches(Code, Args, namedDecl(hasName(DeclName)).bind("id"), + ExpectedPrinted, "input.cc", + [](llvm::raw_ostream &Out, const NamedDecl *D) { + D->printNestedNameSpecifier(Out); + }); +} + } // unnamed namespace TEST(NamedDeclPrinter, TestNamespace1) { @@ -223,3 +251,21 @@ R"( "property", "Obj::property")); } + +TEST(NamedDeclPrinter, NestedNameSpecifierSimple) { + const char *Code = + R"( + namespace foo { namespace bar { void func(); } } +)"; + ASSERT_TRUE(PrintedNestedNameSpecifierMatches(Code, "func", "foo::bar::")); +} + +TEST(NamedDeclPrinter, NestedNameSpecifierTemplateArgs) { + const char *Code = + R"( + template <class T> struct vector; + template <> struct vector<int> { int method(); }; +)"; + ASSERT_TRUE( + PrintedNestedNameSpecifierMatches(Code, "method", "vector<int>::")); +} |