1//===--- ExecuteCompilerInvocation.cpp ------------------------------------===//
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 file holds ExecuteCompilerInvocation(). It is split into its own file to
11// minimize the impact of pulling in essentially everything else in Clang.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/FrontendTool/Utils.h"
16#include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
17#include "clang/ARCMigrate/ARCMTActions.h"
18#include "clang/CodeGen/CodeGenAction.h"
19#include "clang/Driver/CC1Options.h"
20#include "clang/Driver/OptTable.h"
21#include "clang/Frontend/CompilerInvocation.h"
22#include "clang/Frontend/CompilerInstance.h"
23#include "clang/Frontend/FrontendActions.h"
24#include "clang/Frontend/FrontendDiagnostic.h"
25#include "clang/Frontend/FrontendPluginRegistry.h"
26#include "clang/Rewrite/FrontendActions.h"
27#include "llvm/Support/ErrorHandling.h"
28#include "llvm/Support/DynamicLibrary.h"
29using namespace clang;
30
31static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) {
32  using namespace clang::frontend;
33
34  switch (CI.getFrontendOpts().ProgramAction) {
35  case ASTDump:                return new ASTDumpAction();
36  case ASTDumpXML:             return new ASTDumpXMLAction();
37  case ASTPrint:               return new ASTPrintAction();
38  case ASTView:                return new ASTViewAction();
39  case DumpRawTokens:          return new DumpRawTokensAction();
40  case DumpTokens:             return new DumpTokensAction();
41  case EmitAssembly:           return new EmitAssemblyAction();
42  case EmitBC:                 return new EmitBCAction();
43  case EmitHTML:               return new HTMLPrintAction();
44  case EmitLLVM:               return new EmitLLVMAction();
45  case EmitLLVMOnly:           return new EmitLLVMOnlyAction();
46  case EmitCodeGenOnly:        return new EmitCodeGenOnlyAction();
47  case EmitObj:                return new EmitObjAction();
48  case FixIt:                  return new FixItAction();
49  case GenerateModule:         return new GenerateModuleAction;
50  case GeneratePCH:            return new GeneratePCHAction;
51  case GeneratePTH:            return new GeneratePTHAction();
52  case InitOnly:               return new InitOnlyAction();
53  case ParseSyntaxOnly:        return new SyntaxOnlyAction();
54
55  case PluginAction: {
56    for (FrontendPluginRegistry::iterator it =
57           FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
58         it != ie; ++it) {
59      if (it->getName() == CI.getFrontendOpts().ActionName) {
60        OwningPtr<PluginASTAction> P(it->instantiate());
61        if (!P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs))
62          return 0;
63        return P.take();
64      }
65    }
66
67    CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
68      << CI.getFrontendOpts().ActionName;
69    return 0;
70  }
71
72  case PrintDeclContext:       return new DeclContextPrintAction();
73  case PrintPreamble:          return new PrintPreambleAction();
74  case PrintPreprocessedInput: return new PrintPreprocessedAction();
75  case RewriteMacros:          return new RewriteMacrosAction();
76  case RewriteObjC:            return new RewriteObjCAction();
77  case RewriteTest:            return new RewriteTestAction();
78  case RunAnalysis:            return new ento::AnalysisAction();
79  case MigrateSource:          return new arcmt::MigrateSourceAction();
80  case RunPreprocessorOnly:    return new PreprocessOnlyAction();
81  }
82  llvm_unreachable("Invalid program action!");
83}
84
85static FrontendAction *CreateFrontendAction(CompilerInstance &CI) {
86  // Create the underlying action.
87  FrontendAction *Act = CreateFrontendBaseAction(CI);
88  if (!Act)
89    return 0;
90
91  const FrontendOptions &FEOpts = CI.getFrontendOpts();
92
93  if (FEOpts.FixAndRecompile) {
94    Act = new FixItRecompile(Act);
95  }
96
97  // Potentially wrap the base FE action in an ARC Migrate Tool action.
98  switch (FEOpts.ARCMTAction) {
99  case FrontendOptions::ARCMT_None:
100    break;
101  case FrontendOptions::ARCMT_Check:
102    Act = new arcmt::CheckAction(Act);
103    break;
104  case FrontendOptions::ARCMT_Modify:
105    Act = new arcmt::ModifyAction(Act);
106    break;
107  case FrontendOptions::ARCMT_Migrate:
108    Act = new arcmt::MigrateAction(Act,
109                                   FEOpts.MTMigrateDir,
110                                   FEOpts.ARCMTMigrateReportOut,
111                                   FEOpts.ARCMTMigrateEmitARCErrors);
112    break;
113  }
114
115  if (FEOpts.ObjCMTAction != FrontendOptions::ObjCMT_None) {
116    Act = new arcmt::ObjCMigrateAction(Act, FEOpts.MTMigrateDir,
117                   FEOpts.ObjCMTAction & ~FrontendOptions::ObjCMT_Literals,
118                   FEOpts.ObjCMTAction & ~FrontendOptions::ObjCMT_Subscripting);
119  }
120
121  // If there are any AST files to merge, create a frontend action
122  // adaptor to perform the merge.
123  if (!FEOpts.ASTMergeFiles.empty())
124    Act = new ASTMergeAction(Act, FEOpts.ASTMergeFiles);
125
126  return Act;
127}
128
129bool clang::ExecuteCompilerInvocation(CompilerInstance *Clang) {
130  // Honor -help.
131  if (Clang->getFrontendOpts().ShowHelp) {
132    OwningPtr<driver::OptTable> Opts(driver::createCC1OptTable());
133    Opts->PrintHelp(llvm::outs(), "clang -cc1",
134                    "LLVM 'Clang' Compiler: http://clang.llvm.org");
135    return 0;
136  }
137
138  // Honor -version.
139  //
140  // FIXME: Use a better -version message?
141  if (Clang->getFrontendOpts().ShowVersion) {
142    llvm::cl::PrintVersionMessage();
143    return 0;
144  }
145
146  // Load any requested plugins.
147  for (unsigned i = 0,
148         e = Clang->getFrontendOpts().Plugins.size(); i != e; ++i) {
149    const std::string &Path = Clang->getFrontendOpts().Plugins[i];
150    std::string Error;
151    if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
152      Clang->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin)
153        << Path << Error;
154  }
155
156  // Honor -mllvm.
157  //
158  // FIXME: Remove this, one day.
159  // This should happen AFTER plugins have been loaded!
160  if (!Clang->getFrontendOpts().LLVMArgs.empty()) {
161    unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size();
162    const char **Args = new const char*[NumArgs + 2];
163    Args[0] = "clang (LLVM option parsing)";
164    for (unsigned i = 0; i != NumArgs; ++i)
165      Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str();
166    Args[NumArgs + 1] = 0;
167    llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args);
168  }
169
170  // Honor -analyzer-checker-help.
171  // This should happen AFTER plugins have been loaded!
172  if (Clang->getAnalyzerOpts().ShowCheckerHelp) {
173    ento::printCheckerHelp(llvm::outs(), Clang->getFrontendOpts().Plugins);
174    return 0;
175  }
176
177  // If there were errors in processing arguments, don't do anything else.
178  bool Success = false;
179  if (!Clang->getDiagnostics().hasErrorOccurred()) {
180    // Create and execute the frontend action.
181    OwningPtr<FrontendAction> Act(CreateFrontendAction(*Clang));
182    if (Act) {
183      Success = Clang->ExecuteAction(*Act);
184      if (Clang->getFrontendOpts().DisableFree)
185        Act.take();
186    }
187  }
188
189  return Success;
190}
191