15#include "llvm/ADT/ScopeExit.h"
16#include "llvm/Support/Error.h"
17#include "llvm/Support/VirtualFileSystem.h"
19#include "gtest/gtest.h"
26class StoreDiagnostics :
public DiagnosticConsumer {
27 std::vector<StoredDiagnostic> &Out;
32 StoreDiagnostics(std::vector<StoredDiagnostic> &Out,
bool ReportErrors)
33 : Out(Out), ReportErrors(ReportErrors) {}
35 void BeginSourceFile(
const LangOptions &LangOpts,
36 const Preprocessor *)
override {
37 this->LangOpts = LangOpts;
41 const Diagnostic &Info)
override {
42 Out.emplace_back(DiagLevel, Info);
45 llvm::raw_string_ostream OS(
Text);
46 TextDiagnostic Renderer(OS, LangOpts,
47 &Info.getDiags()->getDiagnosticOptions());
48 Renderer.emitStoredDiagnostic(Out.back());
49 ADD_FAILURE() <<
Text;
56void createMissingComponents(CompilerInstance &Clang) {
57 if (!Clang.hasDiagnostics())
58 Clang.createDiagnostics();
59 if (!Clang.hasFileManager())
60 Clang.createFileManager();
61 if (!Clang.hasSourceManager())
62 Clang.createSourceManager(Clang.getFileManager());
63 if (!Clang.hasTarget())
65 if (!Clang.hasPreprocessor())
67 if (!Clang.hasASTConsumer())
68 Clang.setASTConsumer(std::make_unique<ASTConsumer>());
69 if (!Clang.hasASTContext())
70 Clang.createASTContext();
78 Clang = std::make_unique<CompilerInstance>(
79 std::make_shared<PCHContainerOperations>());
82 auto RecoverFromEarlyExit =
83 llvm::make_scope_exit([&] { createMissingComponents(*Clang); });
86 bool ErrorOK = In.ErrorOK || llvm::StringRef(In.Code).contains(
"error-ok");
87 Clang->createDiagnostics(
new StoreDiagnostics(Diagnostics, !ErrorOK));
90 std::vector<const char *> Argv;
92 for (
const auto &S : LangArgs)
93 Argv.push_back(S.c_str());
94 for (
const auto &S : In.ExtraArgs)
95 Argv.push_back(S.c_str());
100 Clang->setInvocation(std::make_unique<CompilerInvocation>());
102 Clang->getDiagnostics(),
"clang")) {
103 ADD_FAILURE() <<
"Failed to create invocation";
106 assert(!Clang->getInvocation().getFrontendOpts().DisableFree);
109 auto VFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
110 if (
auto Err = VFS->setCurrentWorkingDirectory(In.WorkingDir))
111 ADD_FAILURE() <<
"Failed to setWD: " << Err.message();
113 llvm::MemoryBuffer::getMemBufferCopy(In.Code,
Filename));
114 for (
const auto &Extra : In.ExtraFiles)
117 llvm::MemoryBuffer::getMemBufferCopy(Extra.getValue(), Extra.getKey()));
118 Clang->createFileManager(VFS);
122 EXPECT_TRUE(Clang->createTarget());
124 In.MakeAction ? In.MakeAction() : std::make_unique<SyntaxOnlyAction>();
126 if (!Action->BeginSourceFile(*Clang, Main)) {
127 ADD_FAILURE() <<
"Failed to BeginSourceFile()";
131 if (
auto Err = Action->Execute())
132 ADD_FAILURE() <<
"Failed to Execute(): " << llvm::toString(std::move(Err));
136 Clang->getPreprocessor().EndSourceFile();
138 Clang->getDiagnosticClient().EndSourceFile();
143void TestAST::clear() {
147 auto PP = Clang->getPreprocessorPtr();
148 Clang->setPreprocessor(
nullptr);
149 Action->EndSourceFile();
159 Action = std::move(M.Action);
160 Clang = std::move(M.Clang);
161 Diagnostics = std::move(M.Diagnostics);
Defines the Diagnostic-related interfaces.
Defines the clang::LangOptions interface.
static bool CreateFromArgs(CompilerInvocation &Res, ArrayRef< const char * > CommandLineArgs, DiagnosticsEngine &Diags, const char *Argv0=nullptr)
Create a compiler invocation from a list of input options.
Abstract interface, implemented by clients of the front-end, which formats and prints fully processed...
Level
The level of the diagnostic, after it has been through mapping.
The result of parsing a file specified by TestInputs.
TestAST(const TestInputs &)
Constructing a TestAST parses the virtual file.
TestAST & operator=(TestAST &&)
The JSON file list parser is used to communicate input to InstallAPI.
std::vector< std::string > getCC1ArgsForTesting(TestLanguage Lang)
StringRef getFilenameForTesting(TestLanguage Lang)
@ TU_Complete
The translation unit is a complete translation unit.