2010-02-25 16:49:05 +08:00
|
|
|
//===-- examples/clang-interpreter/main.cpp - Clang C Interpreter Example -===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-06-16 01:48:49 +08:00
|
|
|
#include "clang/CodeGen/CodeGenAction.h"
|
2012-12-04 17:37:22 +08:00
|
|
|
#include "clang/Basic/DiagnosticOptions.h"
|
2010-02-25 16:49:05 +08:00
|
|
|
#include "clang/Driver/Compilation.h"
|
|
|
|
#include "clang/Driver/Driver.h"
|
|
|
|
#include "clang/Driver/Tool.h"
|
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
2012-12-04 17:37:22 +08:00
|
|
|
#include "clang/Frontend/CompilerInvocation.h"
|
2010-02-25 16:49:05 +08:00
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
|
|
|
#include "clang/Frontend/TextDiagnosticPrinter.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ExecutionEngine/ExecutionEngine.h"
|
2014-07-25 01:13:09 +08:00
|
|
|
#include "llvm/ExecutionEngine/MCJIT.h"
|
2013-01-02 19:45:17 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2013-06-26 13:03:40 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2010-11-30 02:12:39 +08:00
|
|
|
#include "llvm/Support/Host.h"
|
2012-12-04 17:37:22 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2010-11-30 02:12:39 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2011-08-25 02:09:14 +08:00
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2012-12-04 17:37:22 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-03-09 19:36:40 +08:00
|
|
|
#include <memory>
|
2010-02-25 16:49:05 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace clang::driver;
|
|
|
|
|
2010-11-18 01:23:53 +08:00
|
|
|
// This function isn't referenced outside its translation unit, but it
|
|
|
|
// can't use the "static" keyword because its address is used for
|
|
|
|
// GetMainExecutable (since some platforms don't support taking the
|
|
|
|
// address of main, and some platforms can't implement GetMainExecutable
|
|
|
|
// without being given the address of a function in the main executable).
|
2013-06-26 13:03:40 +08:00
|
|
|
std::string GetExecutablePath(const char *Argv0) {
|
2010-02-25 16:49:05 +08:00
|
|
|
// This just needs to be some symbol in the binary; C++ doesn't
|
|
|
|
// allow taking the address of ::main however.
|
|
|
|
void *MainAddr = (void*) (intptr_t) GetExecutablePath;
|
2013-06-26 13:03:40 +08:00
|
|
|
return llvm::sys::fs::getMainExecutable(Argv0, MainAddr);
|
2010-02-25 16:49:05 +08:00
|
|
|
}
|
|
|
|
|
2014-08-19 12:04:30 +08:00
|
|
|
static llvm::ExecutionEngine *
|
|
|
|
createExecutionEngine(std::unique_ptr<llvm::Module> M, std::string *ErrorStr) {
|
|
|
|
return llvm::EngineBuilder(std::move(M))
|
|
|
|
.setEngineKind(llvm::EngineKind::Either)
|
|
|
|
.setErrorStr(ErrorStr)
|
|
|
|
.create();
|
2014-07-24 23:54:23 +08:00
|
|
|
}
|
|
|
|
|
2014-08-19 12:04:30 +08:00
|
|
|
static int Execute(std::unique_ptr<llvm::Module> Mod, char *const *envp) {
|
2014-07-01 11:19:50 +08:00
|
|
|
llvm::InitializeNativeTarget();
|
2014-07-25 01:13:09 +08:00
|
|
|
llvm::InitializeNativeTargetAsmPrinter();
|
2010-02-25 16:49:05 +08:00
|
|
|
|
2014-08-19 12:04:30 +08:00
|
|
|
llvm::Module &M = *Mod;
|
2010-02-25 16:49:05 +08:00
|
|
|
std::string Error;
|
2014-08-19 12:04:30 +08:00
|
|
|
std::unique_ptr<llvm::ExecutionEngine> EE(
|
|
|
|
createExecutionEngine(std::move(Mod), &Error));
|
2010-02-25 16:49:05 +08:00
|
|
|
if (!EE) {
|
|
|
|
llvm::errs() << "unable to make execution engine: " << Error << "\n";
|
|
|
|
return 255;
|
|
|
|
}
|
|
|
|
|
2014-08-19 12:04:30 +08:00
|
|
|
llvm::Function *EntryFn = M.getFunction("main");
|
2010-02-25 16:49:05 +08:00
|
|
|
if (!EntryFn) {
|
|
|
|
llvm::errs() << "'main' function not found in module.\n";
|
|
|
|
return 255;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Support passing arguments.
|
|
|
|
std::vector<std::string> Args;
|
2014-08-19 12:04:30 +08:00
|
|
|
Args.push_back(M.getModuleIdentifier());
|
2010-02-25 16:49:05 +08:00
|
|
|
|
2014-07-25 01:13:09 +08:00
|
|
|
EE->finalizeObject();
|
2010-02-25 16:49:05 +08:00
|
|
|
return EE->runFunctionAsMain(EntryFn, Args, envp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, const char **argv, char * const *envp) {
|
|
|
|
void *MainAddr = (void*) (intptr_t) GetExecutablePath;
|
2013-06-26 13:03:40 +08:00
|
|
|
std::string Path = GetExecutablePath(argv[0]);
|
2012-10-24 06:36:49 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
|
2010-08-26 21:48:56 +08:00
|
|
|
TextDiagnosticPrinter *DiagClient =
|
2012-10-24 06:36:49 +08:00
|
|
|
new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
|
2010-02-25 16:49:05 +08:00
|
|
|
|
2012-02-20 22:00:23 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
|
2012-10-24 06:36:49 +08:00
|
|
|
DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagClient);
|
2014-07-25 04:47:42 +08:00
|
|
|
|
|
|
|
// Use ELF on windows for now.
|
|
|
|
std::string TripleStr = llvm::sys::getProcessTriple();
|
|
|
|
llvm::Triple T(TripleStr);
|
|
|
|
if (T.isOSBinFormatCOFF())
|
|
|
|
T.setObjectFormat(llvm::Triple::ELF);
|
|
|
|
|
|
|
|
Driver TheDriver(Path, T.str(), Diags);
|
2010-02-25 16:49:05 +08:00
|
|
|
TheDriver.setTitle("clang interpreter");
|
2014-07-09 09:37:36 +08:00
|
|
|
TheDriver.setCheckInputsExist(false);
|
2010-02-25 16:49:05 +08:00
|
|
|
|
|
|
|
// FIXME: This is a hack to try to force the driver to do something we can
|
|
|
|
// recognize. We need to extend the driver library to support this use model
|
|
|
|
// (basically, exactly one input, and the operation mode is hard wired).
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallVector<const char *, 16> Args(argv, argv + argc);
|
2010-02-25 16:49:05 +08:00
|
|
|
Args.push_back("-fsyntax-only");
|
2014-03-09 19:34:25 +08:00
|
|
|
std::unique_ptr<Compilation> C(TheDriver.BuildCompilation(Args));
|
2010-02-25 16:49:05 +08:00
|
|
|
if (!C)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// FIXME: This is copied from ASTUnit.cpp; simplify and eliminate.
|
|
|
|
|
|
|
|
// We expect to get back exactly one command job, if we didn't something
|
|
|
|
// failed. Extract that job from the compilation.
|
|
|
|
const driver::JobList &Jobs = C->getJobs();
|
2014-10-03 09:08:27 +08:00
|
|
|
if (Jobs.size() != 1 || !isa<driver::Command>(*Jobs.begin())) {
|
2012-02-13 20:32:26 +08:00
|
|
|
SmallString<256> Msg;
|
2010-02-25 16:49:05 +08:00
|
|
|
llvm::raw_svector_ostream OS(Msg);
|
2013-09-13 02:23:34 +08:00
|
|
|
Jobs.Print(OS, "; ", true);
|
2010-02-25 16:49:05 +08:00
|
|
|
Diags.Report(diag::err_fe_expected_compiler_job) << OS.str();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-10-03 09:08:27 +08:00
|
|
|
const driver::Command &Cmd = cast<driver::Command>(*Jobs.begin());
|
2014-09-05 00:04:28 +08:00
|
|
|
if (llvm::StringRef(Cmd.getCreator().getName()) != "clang") {
|
2010-02-25 16:49:05 +08:00
|
|
|
Diags.Report(diag::err_fe_expected_clang_command);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize a compiler invocation object from the clang (-cc1) arguments.
|
2014-09-05 00:04:28 +08:00
|
|
|
const driver::ArgStringList &CCArgs = Cmd.getArguments();
|
2014-03-09 19:34:25 +08:00
|
|
|
std::unique_ptr<CompilerInvocation> CI(new CompilerInvocation);
|
2010-04-20 19:50:39 +08:00
|
|
|
CompilerInvocation::CreateFromArgs(*CI,
|
|
|
|
const_cast<const char **>(CCArgs.data()),
|
|
|
|
const_cast<const char **>(CCArgs.data()) +
|
|
|
|
CCArgs.size(),
|
2010-04-20 19:55:38 +08:00
|
|
|
Diags);
|
2010-02-25 16:49:05 +08:00
|
|
|
|
|
|
|
// Show the invocation, with -v.
|
|
|
|
if (CI->getHeaderSearchOpts().Verbose) {
|
|
|
|
llvm::errs() << "clang invocation:\n";
|
2013-09-13 02:23:34 +08:00
|
|
|
Jobs.Print(llvm::errs(), "\n", true);
|
2010-02-25 16:49:05 +08:00
|
|
|
llvm::errs() << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: This is copied from cc1_main.cpp; simplify and eliminate.
|
|
|
|
|
|
|
|
// Create a compiler instance to handle the actual work.
|
|
|
|
CompilerInstance Clang;
|
2014-03-09 19:46:32 +08:00
|
|
|
Clang.setInvocation(CI.release());
|
2010-02-25 16:49:05 +08:00
|
|
|
|
|
|
|
// Create the compilers actual diagnostics engine.
|
2013-01-20 09:58:28 +08:00
|
|
|
Clang.createDiagnostics();
|
2010-02-25 16:49:05 +08:00
|
|
|
if (!Clang.hasDiagnostics())
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
// Infer the builtin include path if unspecified.
|
|
|
|
if (Clang.getHeaderSearchOpts().UseBuiltinIncludes &&
|
|
|
|
Clang.getHeaderSearchOpts().ResourceDir.empty())
|
|
|
|
Clang.getHeaderSearchOpts().ResourceDir =
|
|
|
|
CompilerInvocation::GetResourcesPath(argv[0], MainAddr);
|
|
|
|
|
|
|
|
// Create and execute the frontend to generate an LLVM bitcode module.
|
2014-03-09 19:34:25 +08:00
|
|
|
std::unique_ptr<CodeGenAction> Act(new EmitLLVMOnlyAction());
|
2010-02-25 16:49:05 +08:00
|
|
|
if (!Clang.ExecuteAction(*Act))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
int Res = 255;
|
2014-08-19 22:32:16 +08:00
|
|
|
if (std::unique_ptr<llvm::Module> Module = Act->takeModule())
|
2014-08-19 12:04:30 +08:00
|
|
|
Res = Execute(std::move(Module), envp);
|
2010-02-25 16:49:05 +08:00
|
|
|
|
|
|
|
// Shutdown.
|
|
|
|
|
|
|
|
llvm::llvm_shutdown();
|
|
|
|
|
|
|
|
return Res;
|
|
|
|
}
|