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   // Don't clear the AST before backend codegen since we do codegen multiple
117   // times, reusing the same AST.
118   Clang->getCodeGenOpts().ClearASTBeforeBackend = false;
119 
120   return std::move(Clang);
121 }
122 
123 } // anonymous namespace
124 
125 llvm::Expected<std::unique_ptr<CompilerInstance>>
126 IncrementalCompilerBuilder::create(std::vector<const char *> &ClangArgv) {
127 
128   // If we don't know ClangArgv0 or the address of main() at this point, try
129   // to guess it anyway (it's possible on some platforms).
130   std::string MainExecutableName =
131       llvm::sys::fs::getMainExecutable(nullptr, nullptr);
132 
133   ClangArgv.insert(ClangArgv.begin(), MainExecutableName.c_str());
134 
135   // Prepending -c to force the driver to do something if no action was
136   // specified. By prepending we allow users to override the default
137   // action and use other actions in incremental mode.
138   // FIXME: Print proper driver diagnostics if the driver flags are wrong.
139   ClangArgv.insert(ClangArgv.begin() + 1, "-c");
140 
141   if (!llvm::is_contained(ClangArgv, " -x")) {
142     // We do C++ by default; append right after argv[0] if no "-x" given
143     ClangArgv.push_back("-x");
144     ClangArgv.push_back("c++");
145   }
146 
147   // Put a dummy C++ file on to ensure there's at least one compile job for the
148   // driver to construct.
149   ClangArgv.push_back("<<< inputs >>>");
150 
151   // Buffer diagnostics from argument parsing so that we can output them using a
152   // well formed diagnostic object.
153   IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
154   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts =
155       CreateAndPopulateDiagOpts(ClangArgv);
156   TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer;
157   DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer);
158 
159   driver::Driver Driver(/*MainBinaryName=*/ClangArgv[0],
160                         llvm::sys::getProcessTriple(), 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<PartialTranslationUnit &>
201 Interpreter::Parse(llvm::StringRef Code) {
202   return IncrParser->Parse(Code);
203 }
204 
205 llvm::Error Interpreter::Execute(PartialTranslationUnit &T) {
206   assert(T.TheModule);
207   if (!IncrExecutor) {
208     const llvm::Triple &Triple =
209         getCompilerInstance()->getASTContext().getTargetInfo().getTriple();
210     llvm::Error Err = llvm::Error::success();
211     IncrExecutor = std::make_unique<IncrementalExecutor>(*TSCtx, Err, Triple);
212 
213     if (Err)
214       return Err;
215   }
216   // FIXME: Add a callback to retain the llvm::Module once the JIT is done.
217   if (auto Err = IncrExecutor->addModule(std::move(T.TheModule)))
218     return Err;
219 
220   if (auto Err = IncrExecutor->runCtors())
221     return Err;
222 
223   return llvm::Error::success();
224 }
225