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   // Buffer diagnostics from argument parsing so that we can output them using a
148   // well formed diagnostic object.
149   IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
150   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts =
151       CreateAndPopulateDiagOpts(ClangArgv);
152   TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer;
153   DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer);
154 
155   driver::Driver Driver(/*MainBinaryName=*/ClangArgv[0],
156                         llvm::sys::getProcessTriple(), Diags);
157   Driver.setCheckInputsExist(false); // the input comes from mem buffers
158   llvm::ArrayRef<const char *> RF = llvm::makeArrayRef(ClangArgv);
159   std::unique_ptr<driver::Compilation> Compilation(Driver.BuildCompilation(RF));
160 
161   if (Compilation->getArgs().hasArg(driver::options::OPT_v))
162     Compilation->getJobs().Print(llvm::errs(), "\n", /*Quote=*/false);
163 
164   auto ErrOrCC1Args = GetCC1Arguments(&Diags, Compilation.get());
165   if (auto Err = ErrOrCC1Args.takeError())
166     return std::move(Err);
167 
168   return CreateCI(**ErrOrCC1Args);
169 }
170 
171 Interpreter::Interpreter(std::unique_ptr<CompilerInstance> CI,
172                          llvm::Error &Err) {
173   llvm::ErrorAsOutParameter EAO(&Err);
174   auto LLVMCtx = std::make_unique<llvm::LLVMContext>();
175   TSCtx = std::make_unique<llvm::orc::ThreadSafeContext>(std::move(LLVMCtx));
176   IncrParser = std::make_unique<IncrementalParser>(std::move(CI),
177                                                    *TSCtx->getContext(), Err);
178 }
179 
180 Interpreter::~Interpreter() {}
181 
182 llvm::Expected<std::unique_ptr<Interpreter>>
183 Interpreter::create(std::unique_ptr<CompilerInstance> CI) {
184   llvm::Error Err = llvm::Error::success();
185   auto Interp =
186       std::unique_ptr<Interpreter>(new Interpreter(std::move(CI), Err));
187   if (Err)
188     return std::move(Err);
189   return std::move(Interp);
190 }
191 
192 const CompilerInstance *Interpreter::getCompilerInstance() const {
193   return IncrParser->getCI();
194 }
195 
196 llvm::Expected<PartialTranslationUnit &>
197 Interpreter::Parse(llvm::StringRef Code) {
198   return IncrParser->Parse(Code);
199 }
200 
201 llvm::Error Interpreter::Execute(PartialTranslationUnit &T) {
202   assert(T.TheModule);
203   if (!IncrExecutor) {
204     const llvm::Triple &Triple =
205         getCompilerInstance()->getASTContext().getTargetInfo().getTriple();
206     llvm::Error Err = llvm::Error::success();
207     IncrExecutor = std::make_unique<IncrementalExecutor>(*TSCtx, Err, Triple);
208 
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