2010-05-21 01:49:16 +08:00
|
|
|
//===-- cc1_main.cpp - Clang CC1 Compiler Frontend ------------------------===//
|
2009-11-19 15:37:51 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2009-12-12 06:20:12 +08:00
|
|
|
// This is the entry point to the clang -cc1 functionality, which implements the
|
|
|
|
// core compiler functionality along with a number of additional tools for
|
|
|
|
// demonstration and testing purposes.
|
2009-11-19 15:37:51 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-06-15 01:17:23 +08:00
|
|
|
#include "llvm/Option/Arg.h"
|
2009-12-12 06:20:12 +08:00
|
|
|
#include "clang/Driver/DriverDiagnostic.h"
|
2012-12-04 17:25:21 +08:00
|
|
|
#include "clang/Driver/Options.h"
|
2009-12-12 06:20:12 +08:00
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Frontend/CompilerInvocation.h"
|
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
|
|
|
#include "clang/Frontend/TextDiagnosticBuffer.h"
|
|
|
|
#include "clang/Frontend/TextDiagnosticPrinter.h"
|
2010-08-24 08:31:22 +08:00
|
|
|
#include "clang/FrontendTool/Utils.h"
|
2010-03-31 01:33:59 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2012-12-04 17:25:21 +08:00
|
|
|
#include "llvm/LinkAllPasses.h"
|
2013-06-15 01:17:23 +08:00
|
|
|
#include "llvm/Option/ArgList.h"
|
|
|
|
#include "llvm/Option/OptTable.h"
|
2009-12-12 06:20:12 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2012-11-13 03:39:37 +08:00
|
|
|
#include "llvm/Support/Signals.h"
|
2011-08-25 02:09:14 +08:00
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2010-03-30 13:39:52 +08:00
|
|
|
#include "llvm/Support/Timer.h"
|
2009-11-19 15:37:51 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-12-12 06:20:12 +08:00
|
|
|
#include <cstdio>
|
|
|
|
using namespace clang;
|
2013-06-15 01:17:23 +08:00
|
|
|
using namespace llvm::opt;
|
2009-12-12 06:20:12 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Main driver
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-03-28 02:28:23 +08:00
|
|
|
static void LLVMErrorHandler(void *UserData, const std::string &Message,
|
|
|
|
bool GenCrashDiag) {
|
2011-09-26 07:23:43 +08:00
|
|
|
DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
|
2009-12-12 06:20:12 +08:00
|
|
|
|
|
|
|
Diags.Report(diag::err_fe_error_backend) << Message;
|
|
|
|
|
2012-11-13 03:39:37 +08:00
|
|
|
// Run the interrupt handlers to make sure any special cleanups get done, in
|
|
|
|
// particular that we remove files registered with RemoveFileOnSignal.
|
|
|
|
llvm::sys::RunInterruptHandlers();
|
|
|
|
|
2012-11-13 05:32:24 +08:00
|
|
|
// We cannot recover from llvm errors. When reporting a fatal error, exit
|
2013-03-28 02:28:23 +08:00
|
|
|
// with status 70 to generate crash diagnostics. For BSD systems this is
|
|
|
|
// defined as an internal software error. Otherwise, exit with status 1.
|
|
|
|
exit(GenCrashDiag ? 70 : 1);
|
2009-12-12 06:20:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int cc1_main(const char **ArgBegin, const char **ArgEnd,
|
|
|
|
const char *Argv0, void *MainAddr) {
|
2012-02-13 20:32:21 +08:00
|
|
|
OwningPtr<CompilerInstance> Clang(new CompilerInstance());
|
2012-02-20 22:00:23 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
|
2010-02-16 09:54:47 +08:00
|
|
|
|
2009-12-12 06:20:12 +08:00
|
|
|
// Initialize targets first, so that --version shows registered targets.
|
|
|
|
llvm::InitializeAllTargets();
|
2011-07-23 05:59:11 +08:00
|
|
|
llvm::InitializeAllTargetMCs();
|
2009-12-12 06:20:12 +08:00
|
|
|
llvm::InitializeAllAsmPrinters();
|
2010-04-06 07:33:20 +08:00
|
|
|
llvm::InitializeAllAsmParsers();
|
2009-12-12 06:20:12 +08:00
|
|
|
|
|
|
|
// Buffer diagnostics from argument parsing so that we can output them using a
|
|
|
|
// well formed diagnostic object.
|
2012-10-24 06:26:28 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
|
2010-08-19 06:29:43 +08:00
|
|
|
TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer;
|
2012-10-24 06:26:28 +08:00
|
|
|
DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer);
|
2011-12-23 11:05:38 +08:00
|
|
|
bool Success;
|
|
|
|
Success = CompilerInvocation::CreateFromArgs(Clang->getInvocation(),
|
|
|
|
ArgBegin, ArgEnd, Diags);
|
2009-12-13 11:45:58 +08:00
|
|
|
|
|
|
|
// Infer the builtin include path if unspecified.
|
2010-03-23 13:09:16 +08:00
|
|
|
if (Clang->getHeaderSearchOpts().UseBuiltinIncludes &&
|
|
|
|
Clang->getHeaderSearchOpts().ResourceDir.empty())
|
|
|
|
Clang->getHeaderSearchOpts().ResourceDir =
|
2009-12-15 08:06:45 +08:00
|
|
|
CompilerInvocation::GetResourcesPath(Argv0, MainAddr);
|
2009-12-12 06:20:12 +08:00
|
|
|
|
2010-08-12 10:53:07 +08:00
|
|
|
// Create the actual diagnostics engine.
|
2013-01-20 09:58:28 +08:00
|
|
|
Clang->createDiagnostics();
|
2010-08-12 10:53:07 +08:00
|
|
|
if (!Clang->hasDiagnostics())
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
// Set an error handler, so that any LLVM backend diagnostics go through our
|
|
|
|
// error handler.
|
|
|
|
llvm::install_fatal_error_handler(LLVMErrorHandler,
|
|
|
|
static_cast<void*>(&Clang->getDiagnostics()));
|
|
|
|
|
2010-08-19 06:29:43 +08:00
|
|
|
DiagsBuffer->FlushDiagnostics(Clang->getDiagnostics());
|
2012-01-26 04:00:43 +08:00
|
|
|
if (!Success)
|
|
|
|
return 1;
|
2010-08-12 10:53:07 +08:00
|
|
|
|
2010-08-12 10:53:12 +08:00
|
|
|
// Execute the frontend actions.
|
2011-12-23 11:05:38 +08:00
|
|
|
Success = ExecuteCompilerInvocation(Clang.get());
|
2010-03-23 13:09:16 +08:00
|
|
|
|
2010-03-30 13:39:52 +08:00
|
|
|
// If any timers were active but haven't been destroyed yet, print their
|
|
|
|
// results now. This happens in -disable-free mode.
|
|
|
|
llvm::TimerGroup::printAll(llvm::errs());
|
2010-08-02 23:31:28 +08:00
|
|
|
|
2010-08-19 05:23:17 +08:00
|
|
|
// Our error handler depends on the Diagnostics object, which we're
|
|
|
|
// potentially about to delete. Uninstall the handler now so that any
|
|
|
|
// later errors use the default handling behavior instead.
|
|
|
|
llvm::remove_fatal_error_handler();
|
|
|
|
|
2010-03-23 13:09:16 +08:00
|
|
|
// When running with -disable-free, don't do any destruction or shutdown.
|
|
|
|
if (Clang->getFrontendOpts().DisableFree) {
|
2011-02-28 02:07:41 +08:00
|
|
|
if (llvm::AreStatisticsEnabled() || Clang->getFrontendOpts().ShowStats)
|
2010-03-31 01:33:59 +08:00
|
|
|
llvm::PrintStatistics();
|
2010-03-23 13:09:16 +08:00
|
|
|
Clang.take();
|
|
|
|
return !Success;
|
2009-12-12 06:20:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Managed static deconstruction. Useful for making things like
|
|
|
|
// -time-passes usable.
|
|
|
|
llvm::llvm_shutdown();
|
|
|
|
|
2010-01-13 08:48:06 +08:00
|
|
|
return !Success;
|
2009-12-12 06:20:12 +08:00
|
|
|
}
|