//===- unittests/AST/DeclPrinterTest.cpp --- Declaration printer tests ----===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file contains tests for Decl::print() and related methods.
//
// Search this file for WRONG to see test cases that are producing something
// completely wrong, invalid C++ or just misleading.
//
// These tests have a coding convention:
// * declaration to be printed is named 'A' unless it should have some special
// name (e.g., 'operator+');
// * additional helper declarations are 'Z', 'Y', 'X' and so on.
//
//===----------------------------------------------------------------------===//
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Tooling/Tooling.h"
#include "gtest/gtest.h"
using namespace clang;
using namespace ast_matchers;
using namespace tooling;
namespace {
void PrintDecl(raw_ostream &Out, const ASTContext *Context, const Decl *D) {
PrintingPolicy Policy = Context->getPrintingPolicy();
Policy.TerseOutput = true;
D->print(Out, Policy, /*Indentation*/ 0, /*PrintInstantiation*/ false);
}
class PrintMatch : public MatchFinder::MatchCallback {
SmallString<1024> Printed;
unsigned NumFoundDecls;
public:
PrintMatch() : NumFoundDecls(0) {}
virtual void run(const MatchFinder::MatchResult &Result) {
const Decl *D = Result.Nodes.getDeclAs<Decl>("id");
if (!D || D->isImplicit())
return;
NumFoundDecls++;
if (NumFoundDecls > 1)
return;
llvm::raw_svector_ostream Out(Printed);
PrintDecl(Out, Result.Context, D);
}
StringRef getPrinted() const {
return Printed;
}
unsigned getNumFoundDecls() const {
return NumFoundDecls;
}
};
bool runToolOnCode(clang::FrontendAction *ToolAction, const Twine &Code,
ArrayRef<const char *> ClangArgs) {
SmallString<16> FileNameStorage;
StringRef FileNameRef = "input.cc";
std::vector<std::string> ArgVector;
ArgVector.push_back("clang-tool");
ArgVector.push_back("-fsyntax-only");
ArgVector.push_back(FileNameRef.data());
for (unsigned i = 0, e = ClangArgs.size(); i != e; ++i)
ArgVector.push_back(ClangArgs[i]);
FileManager Files((FileSystemOptions()));
ToolInvocation Invocation(ArgVector, ToolAction, &Files);
SmallString<1024> CodeStorage;
Invocation.mapVirtualFile(FileNameRef,
Code.toNullTerminatedStringRef(CodeStorage));
return Invocation.run();
}
::testing::AssertionResult PrintedDeclMatches(
StringRef Code,
ArrayRef<const char *> ClangArgs,
const DeclarationMatcher &NodeMatch,
StringRef ExpectedPrinted) {
PrintMatch Printer;
MatchFinder Finder;
Finder.addMatcher(NodeMatch, &Printer);
OwningPtr<FrontendActionFactory