From 20176bc7dd3f431db4c3d59b51a9f53d52190c82 Mon Sep 17 00:00:00 2001 From: Nathan Ridge Date: Tue, 6 Jul 2021 01:40:24 -0400 Subject: [clang] Refactor AST printing tests to share more infrastructure Differential Revision: https://reviews.llvm.org/D105457 --- clang/unittests/AST/DeclPrinterTest.cpp | 76 ++++++--------------------------- 1 file changed, 12 insertions(+), 64 deletions(-) (limited to 'clang/unittests/AST/DeclPrinterTest.cpp') diff --git a/clang/unittests/AST/DeclPrinterTest.cpp b/clang/unittests/AST/DeclPrinterTest.cpp index e70d2be..bdc23f3 100644 --- a/clang/unittests/AST/DeclPrinterTest.cpp +++ b/clang/unittests/AST/DeclPrinterTest.cpp @@ -18,6 +18,7 @@ // //===----------------------------------------------------------------------===// +#include "ASTPrint.h" #include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/ASTMatchers/ASTMatchers.h" @@ -32,10 +33,8 @@ using namespace tooling; namespace { -using PrintingPolicyModifier = void (*)(PrintingPolicy &policy); - void PrintDecl(raw_ostream &Out, const ASTContext *Context, const Decl *D, - PrintingPolicyModifier PolicyModifier) { + PrintingPolicyAdjuster PolicyModifier) { PrintingPolicy Policy = Context->getPrintingPolicy(); Policy.TerseOutput = true; Policy.Indentation = 0; @@ -44,74 +43,23 @@ void PrintDecl(raw_ostream &Out, const ASTContext *Context, const Decl *D, D->print(Out, Policy, /*Indentation*/ 0, /*PrintInstantiation*/ false); } -class PrintMatch : public MatchFinder::MatchCallback { - SmallString<1024> Printed; - unsigned NumFoundDecls; - PrintingPolicyModifier PolicyModifier; - -public: - PrintMatch(PrintingPolicyModifier PolicyModifier) - : NumFoundDecls(0), PolicyModifier(PolicyModifier) {} - - void run(const MatchFinder::MatchResult &Result) override { - const Decl *D = Result.Nodes.getNodeAs("id"); - if (!D || D->isImplicit()) - return; - NumFoundDecls++; - if (NumFoundDecls > 1) - return; - - llvm::raw_svector_ostream Out(Printed); - PrintDecl(Out, Result.Context, D, PolicyModifier); - } - - StringRef getPrinted() const { - return Printed; - } - - unsigned getNumFoundDecls() const { - return NumFoundDecls; - } -}; - ::testing::AssertionResult PrintedDeclMatches(StringRef Code, const std::vector &Args, const DeclarationMatcher &NodeMatch, StringRef ExpectedPrinted, StringRef FileName, - PrintingPolicyModifier PolicyModifier = nullptr, + PrintingPolicyAdjuster PolicyModifier = nullptr, bool AllowError = false) { - PrintMatch Printer(PolicyModifier); - MatchFinder Finder; - Finder.addMatcher(NodeMatch, &Printer); - std::unique_ptr Factory( - newFrontendActionFactory(&Finder)); - - if (!runToolOnCodeWithArgs(Factory->create(), Code, Args, FileName) && - !AllowError) - return testing::AssertionFailure() - << "Parsing error in \"" << Code.str() << "\""; - - if (Printer.getNumFoundDecls() == 0) - return testing::AssertionFailure() - << "Matcher didn't find any declarations"; - - if (Printer.getNumFoundDecls() > 1) - return testing::AssertionFailure() - << "Matcher should match only one declaration " - "(found " << Printer.getNumFoundDecls() << ")"; - - if (Printer.getPrinted() != ExpectedPrinted) - return ::testing::AssertionFailure() - << "Expected \"" << ExpectedPrinted.str() << "\", " - "got \"" << Printer.getPrinted().str() << "\""; - - return ::testing::AssertionSuccess(); + return PrintedNodeMatches( + Code, Args, NodeMatch, ExpectedPrinted, FileName, PrintDecl, + PolicyModifier, AllowError, + // Filter out implicit decls + [](const Decl *D) { return !D->isImplicit(); }); } ::testing::AssertionResult PrintedDeclCXX98Matches(StringRef Code, StringRef DeclName, StringRef ExpectedPrinted, - PrintingPolicyModifier PolicyModifier = nullptr) { + PrintingPolicyAdjuster PolicyModifier = nullptr) { std::vector Args(1, "-std=c++98"); return PrintedDeclMatches(Code, Args, namedDecl(hasName(DeclName)).bind("id"), ExpectedPrinted, "input.cc", PolicyModifier); @@ -120,7 +68,7 @@ PrintedDeclCXX98Matches(StringRef Code, StringRef DeclName, ::testing::AssertionResult PrintedDeclCXX98Matches(StringRef Code, const DeclarationMatcher &NodeMatch, StringRef ExpectedPrinted, - PrintingPolicyModifier PolicyModifier = nullptr) { + PrintingPolicyAdjuster PolicyModifier = nullptr) { std::vector Args(1, "-std=c++98"); return PrintedDeclMatches(Code, Args, @@ -165,7 +113,7 @@ PrintedDeclCXX98Matches(StringRef Code, const DeclarationMatcher &NodeMatch, ::testing::AssertionResult PrintedDeclCXX17Matches(StringRef Code, const DeclarationMatcher &NodeMatch, StringRef ExpectedPrinted, - PrintingPolicyModifier PolicyModifier = nullptr) { + PrintingPolicyAdjuster PolicyModifier = nullptr) { std::vector Args{"-std=c++17", "-fno-delayed-template-parsing"}; return PrintedDeclMatches(Code, Args, NodeMatch, ExpectedPrinted, "input.cc", PolicyModifier); @@ -174,7 +122,7 @@ PrintedDeclCXX17Matches(StringRef Code, const DeclarationMatcher &NodeMatch, ::testing::AssertionResult PrintedDeclC11Matches(StringRef Code, const DeclarationMatcher &NodeMatch, StringRef ExpectedPrinted, - PrintingPolicyModifier PolicyModifier = nullptr) { + PrintingPolicyAdjuster PolicyModifier = nullptr) { std::vector Args(1, "-std=c11"); return PrintedDeclMatches(Code, Args, NodeMatch, ExpectedPrinted, "input.c", PolicyModifier); -- cgit v1.1