1//===--- TestAST.cpp ------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "clang/Testing/TestAST.h"
10#include "clang/Basic/Diagnostic.h"
11#include "clang/Basic/LangOptions.h"
12#include "clang/Frontend/FrontendActions.h"
13#include "clang/Frontend/TextDiagnostic.h"
14#include "clang/Testing/CommandLineArgs.h"
15#include "llvm/ADT/ScopeExit.h"
16#include "llvm/Support/Error.h"
17#include "llvm/Support/VirtualFileSystem.h"
18
19#include "gtest/gtest.h"
20#include <string>
21
22namespace clang {
23namespace {
24
25// Captures diagnostics into a vector, optionally reporting errors to gtest.
26class StoreDiagnostics : public DiagnosticConsumer {
27 std::vector<StoredDiagnostic> &Out;
28 bool ReportErrors;
29 LangOptions LangOpts;
30
31public:
32 StoreDiagnostics(std::vector<StoredDiagnostic> &Out, bool ReportErrors)
33 : Out(Out), ReportErrors(ReportErrors) {}
34
35 void BeginSourceFile(const LangOptions &LangOpts,
36 const Preprocessor *) override {
37 this->LangOpts = LangOpts;
38 }
39
40 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
41 const Diagnostic &Info) override {
42 Out.emplace_back(args&: DiagLevel, args: Info);
43 if (ReportErrors && DiagLevel >= DiagnosticsEngine::Error) {
44 std::string Text;
45 llvm::raw_string_ostream OS(Text);
46 TextDiagnostic Renderer(OS, LangOpts,
47 Info.getDiags()->getDiagnosticOptions());
48 Renderer.emitStoredDiagnostic(Diag&: Out.back());
49 ADD_FAILURE() << Text;
50 }
51 }
52};
53
54// Fills in the bits of a CompilerInstance that weren't initialized yet.
55// Provides "empty" ASTContext etc if we fail before parsing gets started.
56void createMissingComponents(CompilerInstance &Clang) {
57 if (!Clang.hasDiagnostics())
58 Clang.createDiagnostics(VFS&: *llvm::vfs::getRealFileSystem());
59 if (!Clang.hasFileManager())
60 Clang.createFileManager();
61 if (!Clang.hasSourceManager())
62 Clang.createSourceManager(FileMgr&: Clang.getFileManager());
63 if (!Clang.hasTarget())
64 Clang.createTarget();
65 if (!Clang.hasPreprocessor())
66 Clang.createPreprocessor(TUKind: TU_Complete);
67 if (!Clang.hasASTConsumer())
68 Clang.setASTConsumer(std::make_unique<ASTConsumer>());
69 if (!Clang.hasASTContext())
70 Clang.createASTContext();
71 if (!Clang.hasSema())
72 Clang.createSema(TUKind: TU_Complete, /*CodeCompleteConsumer=*/CompletionConsumer: nullptr);
73}
74
75} // namespace
76
77TestAST::TestAST(const TestInputs &In) {
78 Clang = std::make_unique<CompilerInstance>();
79 // If we don't manage to finish parsing, create CompilerInstance components
80 // anyway so that the test will see an empty AST instead of crashing.
81 auto RecoverFromEarlyExit =
82 llvm::make_scope_exit(F: [&] { createMissingComponents(Clang&: *Clang); });
83
84 std::string Filename = In.FileName;
85 if (Filename.empty())
86 Filename = getFilenameForTesting(Lang: In.Language).str();
87
88 // Set up a VFS with only the virtual file visible.
89 auto VFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
90 if (auto Err = VFS->setCurrentWorkingDirectory(In.WorkingDir))
91 ADD_FAILURE() << "Failed to setWD: " << Err.message();
92 VFS->addFile(Path: Filename, /*ModificationTime=*/0,
93 Buffer: llvm::MemoryBuffer::getMemBufferCopy(InputData: In.Code, BufferName: Filename));
94 for (const auto &Extra : In.ExtraFiles)
95 VFS->addFile(
96 Path: Extra.getKey(), /*ModificationTime=*/0,
97 Buffer: llvm::MemoryBuffer::getMemBufferCopy(InputData: Extra.getValue(), BufferName: Extra.getKey()));
98
99 // Extra error conditions are reported through diagnostics, set that up first.
100 bool ErrorOK = In.ErrorOK || llvm::StringRef(In.Code).contains(Other: "error-ok");
101 Clang->createDiagnostics(VFS&: *VFS, Client: new StoreDiagnostics(Diagnostics, !ErrorOK));
102
103 // Parse cc1 argv, (typically [-std=c++20 input.cc]) into CompilerInvocation.
104 std::vector<const char *> Argv;
105 std::vector<std::string> LangArgs = getCC1ArgsForTesting(Lang: In.Language);
106 for (const auto &S : LangArgs)
107 Argv.push_back(x: S.c_str());
108 for (const auto &S : In.ExtraArgs)
109 Argv.push_back(x: S.c_str());
110 Argv.push_back(x: Filename.c_str());
111 if (!CompilerInvocation::CreateFromArgs(Res&: Clang->getInvocation(), CommandLineArgs: Argv,
112 Diags&: Clang->getDiagnostics(), Argv0: "clang")) {
113 ADD_FAILURE() << "Failed to create invocation";
114 return;
115 }
116 assert(!Clang->getInvocation().getFrontendOpts().DisableFree);
117
118 Clang->createFileManager(VFS);
119
120 // Running the FrontendAction creates the other components: SourceManager,
121 // Preprocessor, ASTContext, Sema. Preprocessor needs TargetInfo to be set.
122 EXPECT_TRUE(Clang->createTarget());
123 Action =
124 In.MakeAction ? In.MakeAction() : std::make_unique<SyntaxOnlyAction>();
125 const FrontendInputFile &Main = Clang->getFrontendOpts().Inputs.front();
126 if (!Action->BeginSourceFile(CI&: *Clang, Input: Main)) {
127 ADD_FAILURE() << "Failed to BeginSourceFile()";
128 Action.reset(); // Don't call EndSourceFile if BeginSourceFile failed.
129 return;
130 }
131 if (auto Err = Action->Execute())
132 ADD_FAILURE() << "Failed to Execute(): " << llvm::toString(E: std::move(Err));
133
134 // Action->EndSourceFile() would destroy the ASTContext, we want to keep it.
135 // But notify the preprocessor we're done now.
136 Clang->getPreprocessor().EndSourceFile();
137 // We're done gathering diagnostics, detach the consumer so we can destroy it.
138 Clang->getDiagnosticClient().EndSourceFile();
139 Clang->getDiagnostics().setClient(client: new DiagnosticConsumer(),
140 /*ShouldOwnClient=*/true);
141}
142
143void TestAST::clear() {
144 if (Action) {
145 // We notified the preprocessor of EOF already, so detach it first.
146 // Sema needs the PP alive until after EndSourceFile() though.
147 auto PP = Clang->getPreprocessorPtr(); // Keep PP alive for now.
148 Clang->setPreprocessor(nullptr); // Detach so we don't send EOF twice.
149 Action->EndSourceFile(); // Destroy ASTContext and Sema.
150 // Now Sema is gone, PP can safely be destroyed.
151 }
152 Action.reset();
153 Clang.reset();
154 Diagnostics.clear();
155}
156
157TestAST &TestAST::operator=(TestAST &&M) {
158 clear();
159 Action = std::move(M.Action);
160 Clang = std::move(M.Clang);
161 Diagnostics = std::move(M.Diagnostics);
162 return *this;
163}
164
165TestAST::TestAST(TestAST &&M) { *this = std::move(M); }
166
167TestAST::~TestAST() { clear(); }
168
169} // end namespace clang
170

source code of clang/lib/Testing/TestAST.cpp