15#include "llvm/ADT/ScopeExit.h"
16#include "llvm/Support/Error.h"
17#include "llvm/Support/VirtualFileSystem.h"
19#include "gtest/gtest.h"
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;
57 if (!Clang.hasVirtualFileSystem())
58 Clang.createVirtualFileSystem();
59 if (!Clang.hasDiagnostics())
60 Clang.createDiagnostics();
61 if (!Clang.hasFileManager())
62 Clang.createFileManager();
63 if (!Clang.hasSourceManager())
64 Clang.createSourceManager(Clang.getFileManager());
65 if (!Clang.hasTarget())
67 if (!Clang.hasPreprocessor())
69 if (!Clang.hasASTConsumer())
70 Clang.setASTConsumer(std::make_unique<ASTConsumer>());
71 if (!Clang.hasASTContext())
72 Clang.createASTContext();
80 Clang = std::make_unique<CompilerInstance>();
83 auto RecoverFromEarlyExit =
84 llvm::make_scope_exit([&] { createMissingComponents(*Clang); });
86 std::string Filename = In.FileName;
91 auto VFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
92 if (
auto Err = VFS->setCurrentWorkingDirectory(In.WorkingDir))
93 ADD_FAILURE() <<
"Failed to setWD: " << Err.message();
94 VFS->addFile(Filename, 0,
95 llvm::MemoryBuffer::getMemBufferCopy(In.Code, Filename));
96 for (
const auto &
Extra : In.ExtraFiles)
99 llvm::MemoryBuffer::getMemBufferCopy(
Extra.getValue(),
Extra.getKey()));
102 bool ErrorOK = In.ErrorOK || llvm::StringRef(In.Code).contains(
"error-ok");
103 auto DiagConsumer =
new StoreDiagnostics(Diagnostics, !ErrorOK);
104 Clang->createVirtualFileSystem(std::move(VFS), DiagConsumer);
105 Clang->createDiagnostics(DiagConsumer);
108 std::vector<const char *> Argv;
110 for (
const auto &S : LangArgs)
111 Argv.push_back(S.c_str());
112 for (
const auto &S : In.ExtraArgs)
113 Argv.push_back(S.c_str());
114 Argv.push_back(Filename.c_str());
116 Clang->getDiagnostics(),
"clang")) {
117 ADD_FAILURE() <<
"Failed to create invocation";
120 assert(!Clang->getInvocation().getFrontendOpts().DisableFree);
122 Clang->createFileManager();
126 EXPECT_TRUE(Clang->createTarget());
128 In.MakeAction ? In.MakeAction() : std::make_unique<SyntaxOnlyAction>();
130 if (!Action->BeginSourceFile(*Clang, Main)) {
131 ADD_FAILURE() <<
"Failed to BeginSourceFile()";
135 if (
auto Err = Action->Execute())
136 ADD_FAILURE() <<
"Failed to Execute(): " << llvm::toString(std::move(Err));
140 Clang->getPreprocessor().EndSourceFile();
142 Clang->getDiagnosticClient().EndSourceFile();
147void TestAST::clear() {
151 auto PP = Clang->getPreprocessorPtr();
152 Clang->setPreprocessor(
nullptr);
153 Action->EndSourceFile();
163 Action = std::move(M.Action);
164 Clang = std::move(M.Clang);
165 Diagnostics = std::move(M.Diagnostics);
Defines the Diagnostic-related interfaces.
Defines the clang::LangOptions interface.
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
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.
TestAST(const TestInputs &)
Constructing a TestAST parses the virtual file.
TestAST & operator=(TestAST &&)
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
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.