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