zcov: / tools/driver/driver.cpp


Files: 1 Branches Taken: 73.3% 63 / 86
Generated: 2010-02-10 01:31 Branches Executed: 97.7% 84 / 86
Line Coverage: 90.5% 105 / 116


Programs: 1 Runs 2897


       1                 : //===-- driver.cpp - Clang GCC-Compatible Driver --------------------------===//
       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 driver; it is a thin wrapper
      11                 : // for functionality in the Driver clang library.
      12                 : //
      13                 : //===----------------------------------------------------------------------===//
      14                 : 
      15                 : #include "clang/Driver/Compilation.h"
      16                 : #include "clang/Driver/Driver.h"
      17                 : #include "clang/Driver/Option.h"
      18                 : 
      19                 : #include "llvm/ADT/SmallString.h"
      20                 : #include "llvm/ADT/OwningPtr.h"
      21                 : #include "llvm/Config/config.h"
      22                 : #include "llvm/Support/ManagedStatic.h"
      23                 : #include "llvm/Support/PrettyStackTrace.h"
      24                 : #include "llvm/Support/raw_ostream.h"
      25                 : #include "llvm/System/Host.h"
      26                 : #include "llvm/System/Path.h"
      27                 : #include "llvm/System/Signals.h"
      28                 : using namespace clang;
      29                 : using namespace clang::driver;
      30                 : 
                        0: branch 2 not taken
                        0: branch 3 not taken
                        0: branch 7 not taken
                      239: branch 8 taken
      31              239: class DriverDiagnosticPrinter : public DiagnosticClient {
      32                 :   std::string ProgName;
      33                 :   llvm::raw_ostream &OS;
      34                 : 
      35                 : public:
      36                 :   DriverDiagnosticPrinter(const std::string _ProgName,
      37              239:                           llvm::raw_ostream &_OS)
      38                 :     : ProgName(_ProgName),
      39              239:       OS(_OS) {}
      40                 : 
      41                 :   virtual void HandleDiagnostic(Diagnostic::Level DiagLevel,
      42                 :                                 const DiagnosticInfo &Info);
      43                 : };
      44                 : 
      45                 : void DriverDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level,
      46               40:                                                const DiagnosticInfo &Info) {
      47               40:   OS << ProgName << ": ";
      48                 : 
                        0: branch 0 not taken
                        0: branch 1 not taken
                       29: branch 2 taken
                       11: branch 3 taken
                        0: branch 4 not taken
                        0: branch 5 not taken
      49               40:   switch (Level) {
      50                0:   case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type");
      51                0:   case Diagnostic::Note:    OS << "note: "; break;
      52               29:   case Diagnostic::Warning: OS << "warning: "; break;
      53               11:   case Diagnostic::Error:   OS << "error: "; break;
      54                0:   case Diagnostic::Fatal:   OS << "fatal error: "; break;
      55                 :   }
      56                 : 
      57               40:   llvm::SmallString<100> OutStr;
      58               40:   Info.FormatDiagnostic(OutStr);
      59               40:   OS.write(OutStr.begin(), OutStr.size());
      60               40:   OS << '\n';
      61               40: }
      62                 : 
      63              239: llvm::sys::Path GetExecutablePath(const char *Argv0, bool CanonicalPrefixes) {
                        0: branch 0 not taken
                      239: branch 1 taken
      64              239:   if (!CanonicalPrefixes)
      65                0:     return llvm::sys::Path(Argv0);
      66                 : 
      67                 :   // This just needs to be some symbol in the binary; C++ doesn't
      68                 :   // allow taking the address of ::main however.
      69              239:   void *P = (void*) (intptr_t) GetExecutablePath;
      70              239:   return llvm::sys::Path::GetMainExecutable(Argv0, P);
      71                 : }
      72                 : 
      73                 : static const char *SaveStringInSet(std::set<std::string> &SavedStrings,
      74               14:                                    const std::string &S) {
      75               14:   return SavedStrings.insert(S).first->c_str();
      76                 : }
      77                 : 
      78                 : /// ApplyQAOverride - Apply a list of edits to the input argument lists.
      79                 : ///
      80                 : /// The input string is a space separate list of edits to perform,
      81                 : /// they are applied in order to the input argument lists. Edits
      82                 : /// should be one of the following forms:
      83                 : ///
      84                 : ///  '#': Silence information about the changes to the command line arguments.
      85                 : ///
      86                 : ///  '^': Add FOO as a new argument at the beginning of the command line.
      87                 : ///
      88                 : ///  '+': Add FOO as a new argument at the end of the command line.
      89                 : ///
      90                 : ///  's/XXX/YYY/': Replace the literal argument XXX by YYY in the
      91                 : ///  command line.
      92                 : ///
      93                 : ///  'xOPTION': Removes all instances of the literal argument OPTION.
      94                 : ///
      95                 : ///  'XOPTION': Removes all instances of the literal argument OPTION,
      96                 : ///  and the following argument.
      97                 : ///
      98                 : ///  'Ox': Removes all flags matching 'O' or 'O[sz0-9]' and adds 'Ox'
      99                 : ///  at the end of the command line.
     100                 : ///
     101                 : /// \param OS - The stream to write edit information to.
     102                 : /// \param Args - The vector of command line arguments.
     103                 : /// \param Edit - The override command to perform.
     104                 : /// \param SavedStrings - Set to use for storing string representations.
     105                 : void ApplyOneQAOverride(llvm::raw_ostream &OS,
     106                 :                         std::vector<const char*> &Args,
     107                 :                         const std::string &Edit,
     108               12:                         std::set<std::string> &SavedStrings) {
     109                 :   // This does not need to be efficient.
     110                 : 
                        1: branch 1 taken
                       11: branch 2 taken
     111               12:   if (Edit[0] == '^') {
     112                 :     const char *Str =
     113                1:       SaveStringInSet(SavedStrings, Edit.substr(1, std::string::npos));
     114                1:     OS << "### Adding argument " << Str << " at beginning\n";
     115                1:     Args.insert(Args.begin() + 1, Str);
                        8: branch 1 taken
                        3: branch 2 taken
     116               11:   } else if (Edit[0] == '+') {
     117                 :     const char *Str =
     118                8:       SaveStringInSet(SavedStrings, Edit.substr(1, std::string::npos));
     119                8:     OS << "### Adding argument " << Str << " at end\n";
     120                8:     Args.push_back(Str);
                        2: branch 1 taken
                        1: branch 2 taken
                        1: branch 4 taken
                        1: branch 5 taken
                        2: branch 6 taken
                        1: branch 7 taken
     121                3:   } else if (Edit[0] == 'x' || Edit[0] == 'X') {
     122                2:     std::string Option = Edit.substr(1, std::string::npos);
                       21: branch 1 taken
                        2: branch 2 taken
     123               25:     for (unsigned i = 1; i < Args.size();) {
                        3: branch 2 taken
                       18: branch 3 taken
     124               21:       if (Option == Args[i]) {
     125                3:         OS << "### Deleting argument " << Args[i] << '\n';
     126                3:         Args.erase(Args.begin() + i);
                        1: branch 1 taken
                        2: branch 2 taken
     127                3:         if (Edit[0] == 'X') {
                        1: branch 1 taken
                        0: branch 2 not taken
     128                1:           if (i < Args.size()) {
     129                1:             OS << "### Deleting argument " << Args[i] << '\n';
     130                1:             Args.erase(Args.begin() + i);
     131                 :           } else
     132                0:             OS << "### Invalid X edit, end of command line!\n";
     133                 :         }
     134                 :       } else
     135               18:         ++i;
     136                2:     }
                        1: branch 1 taken
                        0: branch 2 not taken
     137                1:   } else if (Edit[0] == 'O') {
                        8: branch 1 taken
                        1: branch 2 taken
     138               10:     for (unsigned i = 1; i < Args.size();) {
     139                8:       const char *A = Args[i];
                        7: branch 0 taken
                        1: branch 1 taken
                        7: branch 2 taken
                        0: branch 3 not taken
                        6: branch 4 taken
                        1: branch 5 taken
                        5: branch 6 taken
                        1: branch 7 taken
                        4: branch 8 taken
                        1: branch 9 taken
                        3: branch 10 taken
                        1: branch 11 taken
                        3: branch 12 taken
                        0: branch 13 not taken
                        3: branch 14 taken
                        0: branch 15 not taken
     140               14:       if (A[0] == '-' && A[1] == 'O' &&
     141                 :           (A[2] == '\0' ||
     142                 :            (A[3] == '\0' && (A[2] == 's' || A[2] == 'z' ||
     143                 :                              ('0' <= A[2] && A[2] <= '9'))))) {
     144                6:         OS << "### Deleting argument " << Args[i] << '\n';
     145                6:         Args.erase(Args.begin() + i);
     146                 :       } else
     147                2:         ++i;
     148                 :     }
     149                1:     OS << "### Adding argument " << Edit << " at end\n";
     150                1:     Args.push_back(SaveStringInSet(SavedStrings, '-' + Edit));
     151                 :   } else {
     152                0:     OS << "### Unrecognized edit: " << Edit << "\n";
     153                 :   }
     154               12: }
     155                 : 
     156                 : /// ApplyQAOverride - Apply a comma separate list of edits to the
     157                 : /// input argument lists. See ApplyOneQAOverride.
     158                 : void ApplyQAOverride(std::vector<const char*> &Args, const char *OverrideStr,
     159                1:                      std::set<std::string> &SavedStrings) {
     160                1:   llvm::raw_ostream *OS = &llvm::errs();
     161                 : 
                        1: branch 0 taken
                        0: branch 1 not taken
     162                1:   if (OverrideStr[0] == '#') {
     163                1:     ++OverrideStr;
     164                1:     OS = &llvm::nulls();
     165                 :   }
     166                 : 
     167                1:   *OS << "### QA_OVERRIDE_GCC3_OPTIONS: " << OverrideStr << "\n";
     168                 : 
     169                 :   // This does not need to be efficient.
     170                 : 
     171                1:   const char *S = OverrideStr;
                       13: branch 0 taken
                        1: branch 1 taken
     172               15:   while (*S) {
     173               13:     const char *End = ::strchr(S, ' ');
                        0: branch 0 not taken
                       13: branch 1 taken
     174               13:     if (!End)
     175                0:       End = S + strlen(S);
                       12: branch 0 taken
                        1: branch 1 taken
     176               13:     if (End != S)
     177               12:       ApplyOneQAOverride(*OS, Args, std::string(S, End), SavedStrings);
     178               13:     S = End;
                       13: branch 0 taken
                        0: branch 1 not taken
     179               13:     if (*S != '\0')
     180               13:       ++S;
     181                 :   }
     182                1: }
     183                 : 
     184                 : extern int cc1_main(const char **ArgBegin, const char **ArgEnd,
     185                 :                     const char *Argv0, void *MainAddr);
     186                 : 
     187             2753: int main(int argc, const char **argv) {
     188             2753:   llvm::sys::PrintStackTraceOnErrorSignal();
     189             2753:   llvm::PrettyStackTraceProgram X(argc, argv);
     190                 : 
     191                 :   // Dispatch to cc1_main if appropriate.
                     2753: branch 0 taken
                        0: branch 1 not taken
                     2514: branch 5 taken
                      239: branch 6 taken
                     2514: branch 7 taken
                      239: branch 8 taken
     192             2753:   if (argc > 1 && llvm::StringRef(argv[1]) == "-cc1")
     193                 :     return cc1_main(argv+2, argv+argc, argv[0],
     194             2514:                     (void*) (intptr_t) GetExecutablePath);
     195                 : 
     196              239:   bool CanonicalPrefixes = true;
                     1953: branch 0 taken
                      239: branch 1 taken
     197             2192:   for (int i = 1; i < argc; ++i) {
                        0: branch 3 not taken
                     1953: branch 4 taken
     198             1953:     if (llvm::StringRef(argv[i]) == "-no-canonical-prefixes") {
     199                0:       CanonicalPrefixes = false;
     200                0:       break;
     201                 :     }
     202                 :   }
     203                 : 
     204              239:   llvm::sys::Path Path = GetExecutablePath(argv[0], CanonicalPrefixes);
     205                 : 
     206              239:   DriverDiagnosticPrinter DiagClient(Path.getBasename(), llvm::errs());
     207                 : 
     208              239:   Diagnostic Diags(&DiagClient);
     209                 : 
     210                 : #ifdef CLANG_IS_PRODUCTION
     211                 :   bool IsProduction = true;
     212                 : #else
     213              239:   bool IsProduction = false;
     214                 : #endif
     215                 :   Driver TheDriver(Path.getBasename(), Path.getDirname(),
     216                 :                    llvm::sys::getHostTriple(),
     217              239:                    "a.out", IsProduction, Diags);
     218                 : 
     219                 :   // Check for ".*++" or ".*++-[^-]*" to determine if we are a C++
     220                 :   // compiler. This matches things like "c++", "clang++", and "clang++-1.1".
     221                 :   //
     222                 :   // Note that we intentionally want to use argv[0] here, to support "clang++"
     223                 :   // being a symlink.
     224                 :   //
     225                 :   // We use *argv instead of argv[0] to work around a bogus g++ warning.
     226              239:   std::string ProgName(llvm::sys::Path(*argv).getBasename());
                      239: branch 3 taken
                        0: branch 4 not taken
                        0: branch 9 not taken
                      239: branch 10 taken
                        0: branch 11 not taken
                      239: branch 12 taken
     227              239:   if (llvm::StringRef(ProgName).endswith("++") ||
     228                 :       llvm::StringRef(ProgName).rsplit('-').first.endswith("++")) {
     229                0:     TheDriver.CCCIsCXX = true;
     230                0:     TheDriver.CCCGenericGCCName = "g++";
     231                 :   }
     232                 : 
     233              239:   llvm::OwningPtr<Compilation> C;
     234                 : 
     235                 :   // Handle QA_OVERRIDE_GCC3_OPTIONS and CCC_ADD_ARGS, used for editing a
     236                 :   // command line behind the scenes.
     237              239:   std::set<std::string> SavedStrings;
                        1: branch 1 taken
                      238: branch 2 taken
     238              239:   if (const char *OverrideStr = ::getenv("QA_OVERRIDE_GCC3_OPTIONS")) {
     239                 :     // FIXME: Driver shouldn't take extra initial argument.
     240                1:     std::vector<const char*> StringPointers(argv, argv + argc);
     241                 : 
     242                1:     ApplyQAOverride(StringPointers, OverrideStr, SavedStrings);
     243                 : 
     244                 :     C.reset(TheDriver.BuildCompilation(StringPointers.size(),
     245                1:                                        &StringPointers[0]));
                        1: branch 1 taken
                      237: branch 2 taken
     246              238:   } else if (const char *Cur = ::getenv("CCC_ADD_ARGS")) {
     247                1:     std::vector<const char*> StringPointers;
     248                 : 
     249                 :     // FIXME: Driver shouldn't take extra initial argument.
     250                1:     StringPointers.push_back(argv[0]);
     251                 : 
     252                3:     for (;;) {
     253                4:       const char *Next = strchr(Cur, ',');
     254                 : 
                        3: branch 0 taken
                        1: branch 1 taken
     255                4:       if (Next) {
     256                 :         StringPointers.push_back(SaveStringInSet(SavedStrings,
     257                3:                                                  std::string(Cur, Next)));
     258                3:         Cur = Next + 1;
     259                 :       } else {
                        1: branch 0 taken
                        0: branch 1 not taken
     260                1:         if (*Cur != '\0')
     261                1:           StringPointers.push_back(SaveStringInSet(SavedStrings, Cur));
     262                 :         break;
     263                 :       }
     264                 :     }
     265                 : 
     266                1:     StringPointers.insert(StringPointers.end(), argv + 1, argv + argc);
     267                 : 
     268                 :     C.reset(TheDriver.BuildCompilation(StringPointers.size(),
     269                1:                                        &StringPointers[0]));
     270                 :   } else
     271              237:     C.reset(TheDriver.BuildCompilation(argc, argv));
     272                 : 
     273              239:   int Res = 0;
                      239: branch 1 taken
                        0: branch 2 not taken
     274              239:   if (C.get())
     275              239:     Res = TheDriver.ExecuteCompilation(*C);
     276                 : 
     277              239:   llvm::llvm_shutdown();
     278                 : 
     279              239:   return Res;
     280                 : }

Generated: 2010-02-10 01:31 by zcov