1 //===------ Interpreter.cpp - Incremental Compilation and Execution -------===//
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 // This file implements the component which performs incremental code
10 // compilation and execution.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/Interpreter/Interpreter.h"
15 
16 #include "IncrementalExecutor.h"
17 #include "IncrementalParser.h"
18 
19 #include "clang/AST/ASTContext.h"
20 #include "clang/Basic/TargetInfo.h"
21 #include "clang/CodeGen/ModuleBuilder.h"
22 #include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
23 #include "clang/Driver/Compilation.h"
24 #include "clang/Driver/Driver.h"
25 #include "clang/Driver/Job.h"
26 #include "clang/Driver/Options.h"
27 #include "clang/Driver/Tool.h"
28 #include "clang/Frontend/CompilerInstance.h"
29 #include "clang/Frontend/TextDiagnosticBuffer.h"
30 #include "clang/Lex/PreprocessorOptions.h"
31 
32 #include "llvm/IR/Module.h"
33 #include "llvm/Support/Host.h"
34 
35 using namespace clang;
36 
37 // FIXME: Figure out how to unify with namespace init_convenience from
38 //        tools/clang-import-test/clang-import-test.cpp
39 namespace {
40 /// Retrieves the clang CC1 specific flags out of the compilation's jobs.
41 /// \returns NULL on error.
42 static llvm::Expected<const llvm::opt::ArgStringList *>
43 GetCC1Arguments(DiagnosticsEngine *Diagnostics,
44                 driver::Compilation *Compilation) {
45   // We expect to get back exactly one Command job, if we didn't something
46   // failed. Extract that job from the Compilation.
47   const driver::JobList &Jobs = Compilation->getJobs();
48   if (!Jobs.size() || !isa<driver::Command>(*Jobs.begin()))
49     return llvm::createStringError(std::errc::state_not_recoverable,
50                                    "Driver initialization failed. "
51                                    "Unable to create a driver job");
52 
53   // The one job we find should be to invoke clang again.
54   const driver::Command *Cmd = cast<driver::Command>(&(*Jobs.begin()));
55   if (llvm::StringRef(Cmd->getCreator().getName()) != "clang")
56     return llvm::createStringError(std::errc::state_not_recoverable,
57                                    "Driver initialization failed");
58 
59   return &Cmd->getArguments();
60 }
61 
62 static llvm::Expected<std::unique_ptr<CompilerInstance>>
63 CreateCI(const llvm::opt::ArgStringList &Argv) {
64   std::unique_ptr<CompilerInstance> Clang(new CompilerInstance());
65   IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
66 
67   // Register the support for object-file-wrapped Clang modules.
68   // FIXME: Clang should register these container operations automatically.
69   auto PCHOps = Clang->getPCHContainerOperations();
70   PCHOps->registerWriter(std::make_unique<ObjectFilePCHContainerWriter>());
71   PCHOps->registerReader(std::make_unique<ObjectFilePCHContainerReader>());
72 
73   // Buffer diagnostics from argument parsing so that we can output them using
74   // a well formed diagnostic object.
75   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
76   TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer;
77   DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer);
78   bool Success = CompilerInvocation::CreateFromArgs(
79       Clang->getInvocation(), llvm::makeArrayRef(Argv.begin(), Argv.size()),
80       Diags);
81 
82   // Infer the builtin include path if unspecified.
83   if (Clang->getHeaderSearchOpts().UseBuiltinIncludes &&
84       Clang->getHeaderSearchOpts().ResourceDir.empty())
85     Clang->getHeaderSearchOpts().ResourceDir =
86         CompilerInvocation::GetResourcesPath(Argv[0], nullptr);
87 
88   // Create the actual diagnostics engine.
89   Clang->createDiagnostics();
90   if (!Clang->hasDiagnostics())
91     return llvm::createStringError(std::errc::state_not_recoverable,
92                                    "Initialization failed. "
93                                    "Unable to create diagnostics engine");
94 
95   DiagsBuffer->FlushDiagnostics(Clang->getDiagnostics());
96   if (!Success)
97     return llvm::createStringError(std::errc::state_not_recoverable,
98                                    "Initialization failed. "
99                                    "Unable to flush diagnostics");
100 
101   // FIXME: Merge with CompilerInstance::ExecuteAction.
102   llvm::MemoryBuffer *MB = llvm::MemoryBuffer::getMemBuffer("").release();
103   Clang->getPreprocessorOpts().addRemappedFile("<<< inputs >>>", MB);
104 
105   Clang->setTarget(TargetInfo::CreateTargetInfo(
106       Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
107   if (!Clang->hasTarget())
108     return llvm::createStringError(std::errc::state_not_recoverable,
109                                    "Initialization failed. "
110                                    "Target is missing");
111 
112   Clang->getTarget().adjust(Clang->getDiagnostics(), Clang->getLangOpts());
113 
114   return std::move(Clang);
115 }
116 
117 } // anonymous namespace
118 
119 llvm::Expected<std::unique_ptr<CompilerInstance>>
120 IncrementalCompilerBuilder::create(std::vector<const char *> &ClangArgv) {
121 
122   // If we don't know ClangArgv0 or the address of main() at this point, try
123   // to guess it anyway (it's possible on some platforms).
124   std::string MainExecutableName =
125       llvm::sys::fs::getMainExecutable(nullptr, nullptr);
126 
127   ClangArgv.insert(ClangArgv.begin(), MainExecutableName.c_str());
128 
129   // Prepending -c to force the driver to do something if no action was
130   // specified. By prepending we allow users to override the default
131   // action and use other actions in incremental mode.
132   // FIXME: Print proper driver diagnostics if the driver flags are wrong.
133   ClangArgv.insert(ClangArgv.begin() + 1, "-c");
134 
135   if (!llvm::is_contained(ClangArgv, " -x")) {
136     // We do C++ by default; append right after argv[0] if no "-x" given
137     ClangArgv.push_back("-x");
138     ClangArgv.push_back("c++");
139   }
140 
141   // Put a dummy C++ file on to ensure there's at least one compile job for the
142   // driver to construct.
143   ClangArgv.push_back("<<< inputs >>>");
144 
145   CompilerInvocation Invocation;
146   // Buffer diagnostics from argument parsing so that we can output them using a
147   // well formed diagnostic object.
148   IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
149   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts =
150       CreateAndPopulateDiagOpts(ClangArgv);
151   TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer;
152   DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer);
153 
154   driver::Driver Driver(/*MainBinaryName=*/ClangArgv[0],
155                         llvm::sys::getProcessTriple(), Diags);
156   Driver.setCheckInputsExist(false); // the input comes from mem buffers
157   llvm::ArrayRef<const char *> RF = llvm::makeArrayRef(ClangArgv);
158   std::unique_ptr<driver::Compilation> Compilation(Driver.BuildCompilation(RF));
159 
160   if (Compilation->getArgs().hasArg(driver::options::OPT_v))
161     Compilation->getJobs().Print(llvm::errs(), "\n", /*Quote=*/false);
162 
163   auto ErrOrCC1Args = GetCC1Arguments(&Diags, Compilation.get());
164   if (auto Err = ErrOrCC1Args.takeError())
165     return std::move(Err);
166 
167   return CreateCI(**ErrOrCC1Args);
168 }
169 
170 Interpreter::Interpreter(std::unique_ptr<CompilerInstance> CI,
171                          llvm::Error &Err) {
172   llvm::ErrorAsOutParameter EAO(&Err);
173   auto LLVMCtx = std::make_unique<llvm::LLVMContext>();
174   TSCtx = std::make_unique<llvm::orc::ThreadSafeContext>(std::move(LLVMCtx));
175   IncrParser = std::make_unique<IncrementalParser>(std::move(CI),
176                                                    *TSCtx->getContext(), Err);
177 }
178 
179 Interpreter::~Interpreter() {}
180 
181 llvm::Expected<std::unique_ptr<Interpreter>>
182 Interpreter::create(std::unique_ptr<CompilerInstance> CI) {
183   llvm::Error Err = llvm::Error::success();
184   auto Interp =
185       std::unique_ptr<Interpreter>(new Interpreter(std::move(CI), Err));
186   if (Err)
187     return std::move(Err);
188   return std::move(Interp);
189 }
190 
191 const CompilerInstance *Interpreter::getCompilerInstance() const {
192   return IncrParser->getCI();
193 }
194 
195 llvm::Expected<PartialTranslationUnit &>
196 Interpreter::Parse(llvm::StringRef Code) {
197   return IncrParser->Parse(Code);
198 }
199 
200 llvm::Error Interpreter::Execute(PartialTranslationUnit &T) {
201   assert(T.TheModule);
202   if (!IncrExecutor) {
203     const llvm::Triple &Triple =
204         getCompilerInstance()->getASTContext().getTargetInfo().getTriple();
205     llvm::Error Err = llvm::Error::success();
206     IncrExecutor = std::make_unique<IncrementalExecutor>(*TSCtx, Err, Triple);
207 
208     if (Err)
209       return Err;
210   }
211   // FIXME: Add a callback to retain the llvm::Module once the JIT is done.
212   if (auto Err = IncrExecutor->addModule(std::move(T.TheModule)))
213     return Err;
214 
215   if (auto Err = IncrExecutor->runCtors())
216     return Err;
217 
218   return llvm::Error::success();
219 }
220 
221 llvm::Expected<llvm::JITTargetAddress>
222 Interpreter::getSymbolAddress(llvm::StringRef UnmangledName) const {
223   if (!IncrExecutor)
224     return llvm::make_error<llvm::StringError>("Operation failed. "
225                                                "No execution engine",
226                                                std::error_code());
227 
228   return IncrExecutor->getSymbolAddress(UnmangledName);
229 }
230