aboutsummaryrefslogtreecommitdiff
path: root/clang/unittests/AST/DeclPrinterTest.cpp
diff options
context:
space:
mode:
authorNathan Ridge <zeratul976@hotmail.com>2021-07-06 01:40:24 -0400
committerNathan Ridge <zeratul976@hotmail.com>2021-07-14 19:44:18 -0400
commit9cfec72ffeec242783b70e792c50bd163dcf9dbb (patch)
tree29743727e0420b99d5363c4b6655ab2be4cefe07 /clang/unittests/AST/DeclPrinterTest.cpp
parent4a4229f70f815a0a83e8e226ec1718af693faf4d (diff)
downloadllvm-9cfec72ffeec242783b70e792c50bd163dcf9dbb.zip
llvm-9cfec72ffeec242783b70e792c50bd163dcf9dbb.tar.gz
llvm-9cfec72ffeec242783b70e792c50bd163dcf9dbb.tar.bz2
[clang] Refactor AST printing tests to share more infrastructure
Differential Revision: https://reviews.llvm.org/D105457
Diffstat (limited to 'clang/unittests/AST/DeclPrinterTest.cpp')
-rw-r--r--clang/unittests/AST/DeclPrinterTest.cpp76
1 files changed, 12 insertions, 64 deletions
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<Decl>("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<std::string> &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<FrontendActionFactory> 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<Decl>(
+ 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<std::string> 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<std::string> 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<std::string> 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<std::string> Args(1, "-std=c11");
return PrintedDeclMatches(Code, Args, NodeMatch, ExpectedPrinted, "input.c",
PolicyModifier);