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(*llvm::vfs::getRealFileSystem());
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); });
90 auto VFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
91 if (
auto Err = VFS->setCurrentWorkingDirectory(In.WorkingDir))
92 ADD_FAILURE() <<
"Failed to setWD: " << Err.message();
94 llvm::MemoryBuffer::getMemBufferCopy(In.Code,
Filename));
95 for (
const auto &Extra : In.ExtraFiles)
98 llvm::MemoryBuffer::getMemBufferCopy(Extra.getValue(), Extra.getKey()));
101 bool ErrorOK = In.ErrorOK || llvm::StringRef(In.Code).contains(
"error-ok");
102 Clang->createDiagnostics(*VFS,
new StoreDiagnostics(Diagnostics, !ErrorOK));
105 std::vector<const char *> Argv;
107 for (
const auto &S : LangArgs)
108 Argv.push_back(S.c_str());
109 for (
const auto &S : In.ExtraArgs)
110 Argv.push_back(S.c_str());
112 Clang->setInvocation(std::make_unique<CompilerInvocation>());
114 Clang->getDiagnostics(),
"clang")) {
115 ADD_FAILURE() <<
"Failed to create invocation";
118 assert(!Clang->getInvocation().getFrontendOpts().DisableFree);
120 Clang->createFileManager(VFS);
124 EXPECT_TRUE(Clang->createTarget());
126 In.MakeAction ? In.MakeAction() : std::make_unique<SyntaxOnlyAction>();
128 if (!Action->BeginSourceFile(*Clang, Main)) {
129 ADD_FAILURE() <<
"Failed to BeginSourceFile()";
133 if (
auto Err = Action->Execute())
134 ADD_FAILURE() <<
"Failed to Execute(): " << llvm::toString(std::move(Err));
138 Clang->getPreprocessor().EndSourceFile();
140 Clang->getDiagnosticClient().EndSourceFile();
145void TestAST::clear() {
149 auto PP = Clang->getPreprocessorPtr();
150 Clang->setPreprocessor(
nullptr);
151 Action->EndSourceFile();
161 Action = std::move(M.Action);
162 Clang = std::move(M.Clang);
163 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.