//===- unittests/AST/ASTPrint.h ------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // Helpers to simplify testing of printing of AST constructs provided in the/ // form of the source code. // //===----------------------------------------------------------------------===// #include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/Tooling/Tooling.h" #include "llvm/ADT/SmallString.h" #include "gtest/gtest.h" namespace clang { using PrintingPolicyAdjuster = llvm::function_ref; template using NodePrinter = std::function; template using NodeFilter = std::function; template class PrintMatch : public ast_matchers::MatchFinder::MatchCallback { using PrinterT = NodePrinter; using FilterT = NodeFilter; SmallString<1024> Printed; unsigned NumFoundNodes; PrinterT Printer; FilterT Filter; PrintingPolicyAdjuster PolicyAdjuster; public: PrintMatch(PrinterT Printer, PrintingPolicyAdjuster PolicyAdjuster, FilterT Filter) : NumFoundNodes(0), Printer(std::move(Printer)), Filter(std::move(Filter)), PolicyAdjuster(PolicyAdjuster) {} void run(const ast_matchers::MatchFinder::MatchResult &Result) override { const NodeType *N = Result.Nodes.getNodeAs("id"); if (!N || !Filter(N)) return; NumFoundNodes++; if (NumFoundNodes > 1) return; llvm::raw_svector_ostream Out(Printed); Printer(Out, Result.Context, N, PolicyAdjuster); } StringRef getPrinted() const { return Printed; } unsigned getNumFoundNodes() const { return NumFoundNodes; } }; template bool NoNodeFilter(const NodeType *) { return true; } template ::testing::AssertionResult PrintedNodeMatches(StringRef Code, const std::vector &Args, const Matcher &NodeMatch, StringRef ExpectedPrinted, StringRef FileName, NodePrinter Printer, PrintingPolicyAdjuster PolicyAdjuster = nullptr, bool AllowError = false, // Would like to use a lambda for the default value, but that // trips gcc 7 up. NodeFilter Filter = &NoNodeFilter) { PrintMatch Callback(Printer, PolicyAdjuster, Filter); ast_matchers::MatchFinder Finder; Finder.addMatcher(NodeMatch, &Callback); std::unique_ptr Factory( tooling::newFrontendActionFactory(&Finder)); bool ToolResult; if (FileName.empty()) { ToolResult = tooling::runToolOnCodeWithArgs(Factory->create(), Code, Args); } else { ToolResult = tooling::runToolOnCodeWithArgs(Factory->create(), Code, Args, FileName); } if (!ToolResult && !AllowError) return testing::AssertionFailure() << "Parsing error in \"" << Code.str() << "\""; if (Callback.getNumFoundNodes() == 0) return testing::AssertionFailure() << "Matcher didn't find any nodes"; if (Callback.getNumFoundNodes() > 1) return testing::AssertionFailure() << "Matcher should match only one node (found " << Callback.getNumFoundNodes() << ")"; if (Callback.getPrinted() != ExpectedPrinted) return ::testing::AssertionFailure() << "Expected \"" << ExpectedPrinted.str() << "\", got \"" << Callback.getPrinted().str() << "\""; return ::testing::AssertionSuccess(); } } // namespace clang