Home | History | Annotate | Download | only in driver
      1 //===-- cc1_main.cpp - Clang CC1 Compiler Frontend ------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This is the entry point to the clang -cc1 functionality, which implements the
     11 // core compiler functionality along with a number of additional tools for
     12 // demonstration and testing purposes.
     13 //
     14 //===----------------------------------------------------------------------===//
     15 
     16 #include "clang/Driver/Arg.h"
     17 #include "clang/Driver/ArgList.h"
     18 #include "clang/Driver/DriverDiagnostic.h"
     19 #include "clang/Driver/OptTable.h"
     20 #include "clang/Driver/Options.h"
     21 #include "clang/Frontend/CompilerInstance.h"
     22 #include "clang/Frontend/CompilerInvocation.h"
     23 #include "clang/Frontend/FrontendDiagnostic.h"
     24 #include "clang/Frontend/TextDiagnosticBuffer.h"
     25 #include "clang/Frontend/TextDiagnosticPrinter.h"
     26 #include "clang/FrontendTool/Utils.h"
     27 #include "llvm/ADT/Statistic.h"
     28 #include "llvm/LinkAllPasses.h"
     29 #include "llvm/Support/ErrorHandling.h"
     30 #include "llvm/Support/ManagedStatic.h"
     31 #include "llvm/Support/Signals.h"
     32 #include "llvm/Support/TargetSelect.h"
     33 #include "llvm/Support/Timer.h"
     34 #include "llvm/Support/raw_ostream.h"
     35 #include <cstdio>
     36 using namespace clang;
     37 
     38 //===----------------------------------------------------------------------===//
     39 // Main driver
     40 //===----------------------------------------------------------------------===//
     41 
     42 static void LLVMErrorHandler(void *UserData, const std::string &Message) {
     43   DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
     44 
     45   Diags.Report(diag::err_fe_error_backend) << Message;
     46 
     47   // Run the interrupt handlers to make sure any special cleanups get done, in
     48   // particular that we remove files registered with RemoveFileOnSignal.
     49   llvm::sys::RunInterruptHandlers();
     50 
     51   // We cannot recover from llvm errors.  When reporting a fatal error, exit
     52   // with status 70.  For BSD systems this is defined as an internal software
     53   // error.  This notifies the driver to report diagnostics information.
     54   exit(70);
     55 }
     56 
     57 int cc1_main(const char **ArgBegin, const char **ArgEnd,
     58              const char *Argv0, void *MainAddr) {
     59   OwningPtr<CompilerInstance> Clang(new CompilerInstance());
     60   IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
     61 
     62   // Initialize targets first, so that --version shows registered targets.
     63   llvm::InitializeAllTargets();
     64   llvm::InitializeAllTargetMCs();
     65   llvm::InitializeAllAsmPrinters();
     66   llvm::InitializeAllAsmParsers();
     67 
     68   // Buffer diagnostics from argument parsing so that we can output them using a
     69   // well formed diagnostic object.
     70   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
     71   TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer;
     72   DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer);
     73   bool Success;
     74   Success = CompilerInvocation::CreateFromArgs(Clang->getInvocation(),
     75                                                ArgBegin, ArgEnd, Diags);
     76 
     77   // Infer the builtin include path if unspecified.
     78   if (Clang->getHeaderSearchOpts().UseBuiltinIncludes &&
     79       Clang->getHeaderSearchOpts().ResourceDir.empty())
     80     Clang->getHeaderSearchOpts().ResourceDir =
     81       CompilerInvocation::GetResourcesPath(Argv0, MainAddr);
     82 
     83   // Create the actual diagnostics engine.
     84   Clang->createDiagnostics();
     85   if (!Clang->hasDiagnostics())
     86     return 1;
     87 
     88   // Set an error handler, so that any LLVM backend diagnostics go through our
     89   // error handler.
     90   llvm::install_fatal_error_handler(LLVMErrorHandler,
     91                                   static_cast<void*>(&Clang->getDiagnostics()));
     92 
     93   DiagsBuffer->FlushDiagnostics(Clang->getDiagnostics());
     94   if (!Success)
     95     return 1;
     96 
     97   // Execute the frontend actions.
     98   Success = ExecuteCompilerInvocation(Clang.get());
     99 
    100   // If any timers were active but haven't been destroyed yet, print their
    101   // results now.  This happens in -disable-free mode.
    102   llvm::TimerGroup::printAll(llvm::errs());
    103 
    104   // Our error handler depends on the Diagnostics object, which we're
    105   // potentially about to delete. Uninstall the handler now so that any
    106   // later errors use the default handling behavior instead.
    107   llvm::remove_fatal_error_handler();
    108 
    109   // When running with -disable-free, don't do any destruction or shutdown.
    110   if (Clang->getFrontendOpts().DisableFree) {
    111     if (llvm::AreStatisticsEnabled() || Clang->getFrontendOpts().ShowStats)
    112       llvm::PrintStatistics();
    113     Clang.take();
    114     return !Success;
    115   }
    116 
    117   // Managed static deconstruction. Useful for making things like
    118   // -time-passes usable.
    119   llvm::llvm_shutdown();
    120 
    121   return !Success;
    122 }
    123