2010-05-27 11:17:06 -04:00
|
|
|
//===-- cc1_main.cpp - Clang CC1 Compiler Frontend ------------------------===//
|
2009-11-19 04:00:00 -05:00
|
|
|
//
|
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
|
//
|
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
//
|
2009-12-15 13:49:47 -05: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 04:00:00 -05:00
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2009-12-15 13:49:47 -05:00
|
|
|
#include "clang/Driver/Arg.h"
|
|
|
|
|
#include "clang/Driver/ArgList.h"
|
2012-08-15 16:02:54 -04:00
|
|
|
#include "clang/Driver/Options.h"
|
2009-12-15 13:49:47 -05:00
|
|
|
#include "clang/Driver/DriverDiagnostic.h"
|
|
|
|
|
#include "clang/Driver/OptTable.h"
|
|
|
|
|
#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-09-17 11:54:40 -04:00
|
|
|
#include "clang/FrontendTool/Utils.h"
|
2010-04-02 04:55:10 -04:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2009-12-15 13:49:47 -05:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2011-10-20 17:14:49 -04:00
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2010-04-02 04:55:10 -04:00
|
|
|
#include "llvm/Support/Timer.h"
|
2009-11-19 04:00:00 -05:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-04-14 10:01:31 -04:00
|
|
|
#include "llvm/LinkAllPasses.h"
|
2009-12-15 13:49:47 -05:00
|
|
|
#include <cstdio>
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
// Main driver
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2010-05-27 11:17:06 -04:00
|
|
|
static void LLVMErrorHandler(void *UserData, const std::string &Message) {
|
2011-10-20 17:14:49 -04:00
|
|
|
DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
|
2009-12-15 13:49:47 -05:00
|
|
|
|
|
|
|
|
Diags.Report(diag::err_fe_error_backend) << Message;
|
|
|
|
|
|
|
|
|
|
// We cannot recover from llvm errors.
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int cc1_main(const char **ArgBegin, const char **ArgEnd,
|
|
|
|
|
const char *Argv0, void *MainAddr) {
|
2012-04-14 10:01:31 -04:00
|
|
|
OwningPtr<CompilerInstance> Clang(new CompilerInstance());
|
|
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
|
2009-12-15 13:49:47 -05:00
|
|
|
|
|
|
|
|
// Initialize targets first, so that --version shows registered targets.
|
|
|
|
|
llvm::InitializeAllTargets();
|
2011-10-20 17:14:49 -04:00
|
|
|
llvm::InitializeAllTargetMCs();
|
2009-12-15 13:49:47 -05:00
|
|
|
llvm::InitializeAllAsmPrinters();
|
2010-04-06 11:53:59 -04:00
|
|
|
llvm::InitializeAllAsmParsers();
|
2009-12-15 13:49:47 -05:00
|
|
|
|
|
|
|
|
// Buffer diagnostics from argument parsing so that we can output them using a
|
|
|
|
|
// well formed diagnostic object.
|
2012-12-02 08:20:44 -05:00
|
|
|
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
|
2010-09-17 11:54:40 -04:00
|
|
|
TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer;
|
2012-12-02 08:20:44 -05:00
|
|
|
DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer);
|
2012-04-14 10:01:31 -04:00
|
|
|
bool Success;
|
|
|
|
|
Success = CompilerInvocation::CreateFromArgs(Clang->getInvocation(),
|
|
|
|
|
ArgBegin, ArgEnd, Diags);
|
2009-12-15 13:49:47 -05:00
|
|
|
|
|
|
|
|
// Infer the builtin include path if unspecified.
|
2010-04-02 04:55:10 -04:00
|
|
|
if (Clang->getHeaderSearchOpts().UseBuiltinIncludes &&
|
|
|
|
|
Clang->getHeaderSearchOpts().ResourceDir.empty())
|
|
|
|
|
Clang->getHeaderSearchOpts().ResourceDir =
|
2009-12-15 13:49:47 -05:00
|
|
|
CompilerInvocation::GetResourcesPath(Argv0, MainAddr);
|
|
|
|
|
|
|
|
|
|
// Create the actual diagnostics engine.
|
2010-04-02 04:55:10 -04:00
|
|
|
Clang->createDiagnostics(ArgEnd - ArgBegin, const_cast<char**>(ArgBegin));
|
|
|
|
|
if (!Clang->hasDiagnostics())
|
2009-12-15 13:49:47 -05:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
// Set an error handler, so that any LLVM backend diagnostics go through our
|
|
|
|
|
// error handler.
|
2010-05-04 12:12:48 -04:00
|
|
|
llvm::install_fatal_error_handler(LLVMErrorHandler,
|
2010-04-02 04:55:10 -04:00
|
|
|
static_cast<void*>(&Clang->getDiagnostics()));
|
2009-12-15 13:49:47 -05:00
|
|
|
|
2010-09-17 11:54:40 -04:00
|
|
|
DiagsBuffer->FlushDiagnostics(Clang->getDiagnostics());
|
2012-04-14 10:01:31 -04:00
|
|
|
if (!Success)
|
|
|
|
|
return 1;
|
2009-12-15 13:49:47 -05:00
|
|
|
|
2010-09-17 11:54:40 -04:00
|
|
|
// Execute the frontend actions.
|
2012-04-14 10:01:31 -04:00
|
|
|
Success = ExecuteCompilerInvocation(Clang.get());
|
2010-04-02 04:55:10 -04: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-09-17 11:54:40 -04: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-04-02 04:55:10 -04:00
|
|
|
// When running with -disable-free, don't do any destruction or shutdown.
|
|
|
|
|
if (Clang->getFrontendOpts().DisableFree) {
|
2011-05-02 15:39:53 -04:00
|
|
|
if (llvm::AreStatisticsEnabled() || Clang->getFrontendOpts().ShowStats)
|
2010-04-02 04:55:10 -04:00
|
|
|
llvm::PrintStatistics();
|
|
|
|
|
Clang.take();
|
|
|
|
|
return !Success;
|
2009-12-15 13:49:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Managed static deconstruction. Useful for making things like
|
|
|
|
|
// -time-passes usable.
|
|
|
|
|
llvm::llvm_shutdown();
|
|
|
|
|
|
2010-01-15 10:39:40 -05:00
|
|
|
return !Success;
|
2009-12-15 13:49:47 -05:00
|
|
|
}
|