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/Basic/TargetInfo.h"
20 #include "clang/CodeGen/ModuleBuilder.h"
21 #include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
22 #include "clang/Driver/Compilation.h"
23 #include "clang/Driver/Driver.h"
24 #include "clang/Driver/Job.h"
25 #include "clang/Driver/Options.h"
26 #include "clang/Driver/Tool.h"
27 #include "clang/Frontend/CompilerInstance.h"
28 #include "clang/Frontend/TextDiagnosticBuffer.h"
29 #include "clang/Lex/PreprocessorOptions.h"
30 
31 #include "llvm/IR/Module.h"
32 #include "llvm/Support/Host.h"
33 
34 using namespace clang;
35 
36 // FIXME: Figure out how to unify with namespace init_convenience from
37 //        tools/clang-import-test/clang-import-test.cpp and
38 //        examples/clang-interpreter/main.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->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 = new DiagnosticOptions();
150   TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer;
151   DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer);
152   unsigned MissingArgIndex, MissingArgCount;
153   const llvm::opt::OptTable &Opts = driver::getDriverOptTable();
154   llvm::opt::InputArgList ParsedArgs =
155       Opts.ParseArgs(ArrayRef<const char *>(ClangArgv).slice(1),
156                      MissingArgIndex, MissingArgCount);
157   ParseDiagnosticArgs(*DiagOpts, ParsedArgs, &Diags);
158 
159   driver::Driver Driver(/*MainBinaryName=*/ClangArgv[0],
160                         llvm::sys::getDefaultTargetTriple(), Diags);
161   Driver.setCheckInputsExist(false); // the input comes from mem buffers
162   llvm::ArrayRef<const char *> RF = llvm::makeArrayRef(ClangArgv);
163   std::unique_ptr<driver::Compilation> Compilation(Driver.BuildCompilation(RF));
164 
165   if (Compilation->getArgs().hasArg(driver::options::OPT_v))
166     Compilation->getJobs().Print(llvm::errs(), "\n", /*Quote=*/false);
167 
168   auto ErrOrCC1Args = GetCC1Arguments(&Diags, Compilation.get());
169   if (auto Err = ErrOrCC1Args.takeError())
170     return std::move(Err);
171 
172   return CreateCI(**ErrOrCC1Args);
173 }
174 
175 Interpreter::Interpreter(std::unique_ptr<CompilerInstance> CI,
176                          llvm::Error &Err) {
177   llvm::ErrorAsOutParameter EAO(&Err);
178   auto LLVMCtx = std::make_unique<llvm::LLVMContext>();
179   TSCtx = std::make_unique<llvm::orc::ThreadSafeContext>(std::move(LLVMCtx));
180   IncrParser = std::make_unique<IncrementalParser>(std::move(CI),
181                                                    *TSCtx->getContext(), Err);
182 }
183 
184 Interpreter::~Interpreter() {}
185 
186 llvm::Expected<std::unique_ptr<Interpreter>>
187 Interpreter::create(std::unique_ptr<CompilerInstance> CI) {
188   llvm::Error Err = llvm::Error::success();
189   auto Interp =
190       std::unique_ptr<Interpreter>(new Interpreter(std::move(CI), Err));
191   if (Err)
192     return std::move(Err);
193   return std::move(Interp);
194 }
195 
196 const CompilerInstance *Interpreter::getCompilerInstance() const {
197   return IncrParser->getCI();
198 }
199 
200 llvm::Expected<Transaction &> Interpreter::Parse(llvm::StringRef Code) {
201   return IncrParser->Parse(Code);
202 }
203 
204 llvm::Error Interpreter::Execute(Transaction &T) {
205   assert(T.TheModule);
206   if (!IncrExecutor) {
207     llvm::Error Err = llvm::Error::success();
208     IncrExecutor = std::make_unique<IncrementalExecutor>(*TSCtx, Err);
209     if (Err)
210       return Err;
211   }
212   // FIXME: Add a callback to retain the llvm::Module once the JIT is done.
213   if (auto Err = IncrExecutor->addModule(std::move(T.TheModule)))
214     return Err;
215 
216   if (auto Err = IncrExecutor->runCtors())
217     return Err;
218 
219   return llvm::Error::success();
220 }
221