Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- clang.cpp - C-Language Front-end ---------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This utility may be invoked in the following manner: |
Daniel Dunbar | 4fdba99 | 2009-11-14 10:53:49 +0000 | [diff] [blame] | 11 | // clang-cc --help - Output help info. |
| 12 | // clang-cc [options] - Read from stdin. |
| 13 | // clang-cc [options] file - Read from "file". |
| 14 | // clang-cc [options] file1 file2 - Read these files. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 17 | |
Daniel Dunbar | 0498cfc | 2009-11-10 19:51:53 +0000 | [diff] [blame] | 18 | #include "Options.h" |
Daniel Dunbar | 9119e7e | 2009-11-16 22:38:48 +0000 | [diff] [blame] | 19 | #include "clang/Basic/Diagnostic.h" |
Daniel Dunbar | 775bee7 | 2009-11-11 10:07:22 +0000 | [diff] [blame] | 20 | #include "clang/Basic/FileManager.h" |
| 21 | #include "clang/Basic/SourceManager.h" |
| 22 | #include "clang/Basic/TargetInfo.h" |
| 23 | #include "clang/Basic/Version.h" |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 24 | #include "clang/Frontend/CompilerInstance.h" |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 25 | #include "clang/Frontend/CompilerInvocation.h" |
Daniel Dunbar | 4fdba99 | 2009-11-14 10:53:49 +0000 | [diff] [blame] | 26 | #include "clang/Frontend/FrontendActions.h" |
Daniel Dunbar | 50f4f46 | 2009-03-12 10:14:16 +0000 | [diff] [blame] | 27 | #include "clang/Frontend/FrontendDiagnostic.h" |
Daniel Dunbar | d10c5b8 | 2009-11-15 00:12:04 +0000 | [diff] [blame] | 28 | #include "clang/Frontend/FrontendPluginRegistry.h" |
Daniel Dunbar | f79dced | 2009-11-14 03:24:39 +0000 | [diff] [blame] | 29 | #include "clang/Frontend/VerifyDiagnosticsClient.h" |
Daniel Dunbar | 4fdba99 | 2009-11-14 10:53:49 +0000 | [diff] [blame] | 30 | #include "llvm/LLVMContext.h" |
Daniel Dunbar | 9119e7e | 2009-11-16 22:38:48 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/OwningPtr.h" |
Daniel Dunbar | 70121eb | 2009-08-10 03:40:28 +0000 | [diff] [blame] | 32 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | 524b86f | 2008-10-28 00:38:08 +0000 | [diff] [blame] | 33 | #include "llvm/Support/ManagedStatic.h" |
Zhongxing Xu | 2092236 | 2008-11-26 05:23:17 +0000 | [diff] [blame] | 34 | #include "llvm/Support/PluginLoader.h" |
Chris Lattner | 09e94a3 | 2009-03-04 21:41:39 +0000 | [diff] [blame] | 35 | #include "llvm/Support/PrettyStackTrace.h" |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Timer.h" |
Chris Lattner | 0fa0daa | 2009-08-24 04:11:30 +0000 | [diff] [blame] | 37 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | e553a72 | 2008-10-02 01:21:33 +0000 | [diff] [blame] | 38 | #include "llvm/System/Host.h" |
Chris Lattner | dcaa096 | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 39 | #include "llvm/System/Path.h" |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 40 | #include "llvm/System/Signals.h" |
Chris Lattner | 2fe1194 | 2009-06-17 17:25:50 +0000 | [diff] [blame] | 41 | #include "llvm/Target/TargetSelect.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 42 | using namespace clang; |
| 43 | |
| 44 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | c363cb1 | 2009-11-16 22:38:40 +0000 | [diff] [blame] | 45 | // Main driver |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 46 | //===----------------------------------------------------------------------===// |
| 47 | |
Daniel Dunbar | 750156a | 2009-11-07 04:19:57 +0000 | [diff] [blame] | 48 | std::string GetBuiltinIncludePath(const char *Argv0) { |
| 49 | llvm::sys::Path P = |
| 50 | llvm::sys::Path::GetMainExecutable(Argv0, |
| 51 | (void*)(intptr_t) GetBuiltinIncludePath); |
Rafael Espindola | 1bb15a9 | 2009-10-05 13:12:17 +0000 | [diff] [blame] | 52 | |
Daniel Dunbar | 750156a | 2009-11-07 04:19:57 +0000 | [diff] [blame] | 53 | if (!P.isEmpty()) { |
| 54 | P.eraseComponent(); // Remove /clang from foo/bin/clang |
| 55 | P.eraseComponent(); // Remove /bin from foo/bin |
Rafael Espindola | 1bb15a9 | 2009-10-05 13:12:17 +0000 | [diff] [blame] | 56 | |
Daniel Dunbar | 750156a | 2009-11-07 04:19:57 +0000 | [diff] [blame] | 57 | // Get foo/lib/clang/<version>/include |
| 58 | P.appendComponent("lib"); |
| 59 | P.appendComponent("clang"); |
| 60 | P.appendComponent(CLANG_VERSION_STRING); |
| 61 | P.appendComponent("include"); |
| 62 | } |
Rafael Espindola | 1bb15a9 | 2009-10-05 13:12:17 +0000 | [diff] [blame] | 63 | |
Daniel Dunbar | 750156a | 2009-11-07 04:19:57 +0000 | [diff] [blame] | 64 | return P.str(); |
Rafael Espindola | 1bb15a9 | 2009-10-05 13:12:17 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Daniel Dunbar | c363cb1 | 2009-11-16 22:38:40 +0000 | [diff] [blame] | 67 | static void LLVMErrorHandler(void *UserData, const std::string &Message) { |
| 68 | Diagnostic &Diags = *static_cast<Diagnostic*>(UserData); |
| 69 | |
| 70 | Diags.Report(diag::err_fe_error_backend) << Message; |
| 71 | |
| 72 | // We cannot recover from llvm errors. |
| 73 | exit(1); |
| 74 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 75 | |
Daniel Dunbar | aa57614 | 2009-11-12 15:23:20 +0000 | [diff] [blame] | 76 | /// ClangFrontendTimer - The front-end activities should charge time to it with |
| 77 | /// TimeRegion. The -ftime-report option controls whether this will do |
| 78 | /// anything. |
| 79 | llvm::Timer *ClangFrontendTimer = 0; |
| 80 | |
Daniel Dunbar | d10c5b8 | 2009-11-15 00:12:04 +0000 | [diff] [blame] | 81 | static FrontendAction *CreateFrontendAction(CompilerInstance &CI) { |
Daniel Dunbar | 9a8a83b | 2009-11-14 22:32:38 +0000 | [diff] [blame] | 82 | using namespace clang::frontend; |
| 83 | |
Daniel Dunbar | d10c5b8 | 2009-11-15 00:12:04 +0000 | [diff] [blame] | 84 | switch (CI.getFrontendOpts().ProgramAction) { |
| 85 | default: |
| 86 | llvm::llvm_unreachable("Invalid program action!"); |
| 87 | |
Daniel Dunbar | 4fdba99 | 2009-11-14 10:53:49 +0000 | [diff] [blame] | 88 | case ASTDump: return new ASTDumpAction(); |
| 89 | case ASTPrint: return new ASTPrintAction(); |
| 90 | case ASTPrintXML: return new ASTPrintXMLAction(); |
| 91 | case ASTView: return new ASTViewAction(); |
| 92 | case DumpRawTokens: return new DumpRawTokensAction(); |
| 93 | case DumpRecordLayouts: return new DumpRecordAction(); |
| 94 | case DumpTokens: return new DumpTokensAction(); |
| 95 | case EmitAssembly: return new EmitAssemblyAction(); |
| 96 | case EmitBC: return new EmitBCAction(); |
| 97 | case EmitHTML: return new HTMLPrintAction(); |
| 98 | case EmitLLVM: return new EmitLLVMAction(); |
| 99 | case EmitLLVMOnly: return new EmitLLVMOnlyAction(); |
| 100 | case FixIt: return new FixItAction(); |
| 101 | case GeneratePCH: return new GeneratePCHAction(); |
| 102 | case GeneratePTH: return new GeneratePTHAction(); |
| 103 | case InheritanceView: return new InheritanceViewAction(); |
| 104 | case ParseNoop: return new ParseOnlyAction(); |
| 105 | case ParsePrintCallbacks: return new PrintParseAction(); |
| 106 | case ParseSyntaxOnly: return new SyntaxOnlyAction(); |
Daniel Dunbar | d10c5b8 | 2009-11-15 00:12:04 +0000 | [diff] [blame] | 107 | |
| 108 | case PluginAction: { |
| 109 | if (CI.getFrontendOpts().ActionName == "help") { |
| 110 | llvm::errs() << "clang-cc plugins:\n"; |
| 111 | for (FrontendPluginRegistry::iterator it = |
| 112 | FrontendPluginRegistry::begin(), |
| 113 | ie = FrontendPluginRegistry::end(); |
| 114 | it != ie; ++it) |
| 115 | llvm::errs() << " " << it->getName() << " - " << it->getDesc() << "\n"; |
| 116 | exit(1); |
| 117 | } |
| 118 | |
| 119 | for (FrontendPluginRegistry::iterator it = |
| 120 | FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end(); |
| 121 | it != ie; ++it) { |
| 122 | if (it->getName() == CI.getFrontendOpts().ActionName) |
| 123 | return it->instantiate(); |
| 124 | } |
| 125 | |
| 126 | CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name) |
| 127 | << CI.getFrontendOpts().ActionName; |
| 128 | return 0; |
| 129 | } |
| 130 | |
Daniel Dunbar | 4fdba99 | 2009-11-14 10:53:49 +0000 | [diff] [blame] | 131 | case PrintDeclContext: return new DeclContextPrintAction(); |
| 132 | case PrintPreprocessedInput: return new PrintPreprocessedAction(); |
| 133 | case RewriteBlocks: return new RewriteBlocksAction(); |
| 134 | case RewriteMacros: return new RewriteMacrosAction(); |
| 135 | case RewriteObjC: return new RewriteObjCAction(); |
| 136 | case RewriteTest: return new RewriteTestAction(); |
| 137 | case RunAnalysis: return new AnalysisAction(); |
| 138 | case RunPreprocessorOnly: return new PreprocessOnlyAction(); |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 139 | } |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Daniel Dunbar | 21dac5e | 2009-11-13 01:02:19 +0000 | [diff] [blame] | 142 | static TargetInfo * |
| 143 | ConstructCompilerInvocation(CompilerInvocation &Opts, Diagnostic &Diags, |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 144 | const char *Argv0, bool &IsAST) { |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 145 | // Initialize target options. |
| 146 | InitializeTargetOptions(Opts.getTargetOpts()); |
Daniel Dunbar | 21dac5e | 2009-11-13 01:02:19 +0000 | [diff] [blame] | 147 | |
| 148 | // Get information about the target being compiled for. |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 149 | llvm::OwningPtr<TargetInfo> Target( |
| 150 | TargetInfo::CreateTargetInfo(Diags, Opts.getTargetOpts())); |
| 151 | if (!Target) |
Daniel Dunbar | 21dac5e | 2009-11-13 01:02:19 +0000 | [diff] [blame] | 152 | return 0; |
Daniel Dunbar | 21dac5e | 2009-11-13 01:02:19 +0000 | [diff] [blame] | 153 | |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 154 | // Initialize frontend options. |
| 155 | InitializeFrontendOptions(Opts.getFrontendOpts()); |
Daniel Dunbar | fcb0c3b | 2009-11-10 18:47:35 +0000 | [diff] [blame] | 156 | |
Daniel Dunbar | fbe2faf | 2009-11-13 02:06:12 +0000 | [diff] [blame] | 157 | // Determine the input language, we currently require all files to match. |
| 158 | FrontendOptions::InputKind IK = Opts.getFrontendOpts().Inputs[0].first; |
| 159 | for (unsigned i = 1, e = Opts.getFrontendOpts().Inputs.size(); i != e; ++i) { |
| 160 | if (Opts.getFrontendOpts().Inputs[i].first != IK) { |
| 161 | llvm::errs() << "error: cannot have multiple input files of distinct " |
| 162 | << "language kinds without -x\n"; |
| 163 | return 0; |
| 164 | } |
| 165 | } |
| 166 | |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 167 | // Initialize language options. |
Daniel Dunbar | 5746f1f | 2009-11-12 00:24:10 +0000 | [diff] [blame] | 168 | // |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 169 | // FIXME: These aren't used during operations on ASTs. Split onto a separate |
| 170 | // code path to make this obvious. |
Daniel Dunbar | fbe2faf | 2009-11-13 02:06:12 +0000 | [diff] [blame] | 171 | IsAST = (IK == FrontendOptions::IK_AST); |
Daniel Dunbar | 2626688 | 2009-11-12 23:52:32 +0000 | [diff] [blame] | 172 | if (!IsAST) |
Daniel Dunbar | 6143ea2 | 2009-11-16 22:38:14 +0000 | [diff] [blame] | 173 | InitializeLangOptions(Opts.getLangOpts(), IK, *Target); |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 174 | |
Daniel Dunbar | 5746f1f | 2009-11-12 00:24:10 +0000 | [diff] [blame] | 175 | // Initialize the static analyzer options. |
| 176 | InitializeAnalyzerOptions(Opts.getAnalyzerOpts()); |
| 177 | |
Daniel Dunbar | 0e0bae8 | 2009-11-11 21:43:12 +0000 | [diff] [blame] | 178 | // Initialize the dependency output options (-M...). |
| 179 | InitializeDependencyOutputOptions(Opts.getDependencyOutputOpts()); |
| 180 | |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 181 | // Initialize the header search options. |
Daniel Dunbar | f797329 | 2009-11-11 08:13:32 +0000 | [diff] [blame] | 182 | InitializeHeaderSearchOptions(Opts.getHeaderSearchOpts(), |
Daniel Dunbar | c363cb1 | 2009-11-16 22:38:40 +0000 | [diff] [blame] | 183 | GetBuiltinIncludePath(Argv0)); |
Daniel Dunbar | 5fc7d34 | 2009-11-09 23:12:31 +0000 | [diff] [blame] | 184 | |
| 185 | // Initialize the other preprocessor options. |
| 186 | InitializePreprocessorOptions(Opts.getPreprocessorOpts()); |
Daniel Dunbar | 36f4ec3 | 2009-11-10 16:19:45 +0000 | [diff] [blame] | 187 | |
Daniel Dunbar | 29cf746 | 2009-11-11 10:07:44 +0000 | [diff] [blame] | 188 | // Initialize the preprocessed output options. |
| 189 | InitializePreprocessorOutputOptions(Opts.getPreprocessorOutputOpts()); |
| 190 | |
Daniel Dunbar | 6143ea2 | 2009-11-16 22:38:14 +0000 | [diff] [blame] | 191 | // Initialize backend options, which may also be used to key some language |
| 192 | // options. |
| 193 | InitializeCodeGenOptions(Opts.getCodeGenOpts(), Opts.getLangOpts(), |
| 194 | Opts.getFrontendOpts().ShowTimers); |
Daniel Dunbar | 21dac5e | 2009-11-13 01:02:19 +0000 | [diff] [blame] | 195 | |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 196 | return Target.take(); |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 199 | int main(int argc, char **argv) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 200 | llvm::sys::PrintStackTraceOnErrorSignal(); |
Chris Lattner | 09e94a3 | 2009-03-04 21:41:39 +0000 | [diff] [blame] | 201 | llvm::PrettyStackTraceProgram X(argc, argv); |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 202 | CompilerInstance Clang(&llvm::getGlobalContext(), false); |
Daniel Dunbar | d697081 | 2009-09-02 23:20:15 +0000 | [diff] [blame] | 203 | |
Daniel Dunbar | 4d86151 | 2009-09-03 04:54:12 +0000 | [diff] [blame] | 204 | // Initialize targets first, so that --version shows registered targets. |
Chris Lattner | 2fe1194 | 2009-06-17 17:25:50 +0000 | [diff] [blame] | 205 | llvm::InitializeAllTargets(); |
| 206 | llvm::InitializeAllAsmPrinters(); |
Daniel Dunbar | d697081 | 2009-09-02 23:20:15 +0000 | [diff] [blame] | 207 | |
| 208 | llvm::cl::ParseCommandLineOptions(argc, argv, |
| 209 | "LLVM 'Clang' Compiler: http://clang.llvm.org\n"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 210 | |
Daniel Dunbar | 00e5b8d | 2009-11-12 06:48:31 +0000 | [diff] [blame] | 211 | // Construct the diagnostic engine first, so that we can build a diagnostic |
| 212 | // client to use for any errors during option handling. |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 213 | InitializeDiagnosticOptions(Clang.getDiagnosticOpts()); |
| 214 | Clang.createDiagnostics(argc, argv); |
Daniel Dunbar | 704e48a | 2009-11-13 08:20:57 +0000 | [diff] [blame] | 215 | if (!Clang.hasDiagnostics()) |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 216 | return 1; |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 217 | |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 218 | // Set an error handler, so that any LLVM backend diagnostics go through our |
| 219 | // error handler. |
| 220 | llvm::llvm_install_error_handler(LLVMErrorHandler, |
| 221 | static_cast<void*>(&Clang.getDiagnostics())); |
Daniel Dunbar | 70121eb | 2009-08-10 03:40:28 +0000 | [diff] [blame] | 222 | |
Daniel Dunbar | 21dac5e | 2009-11-13 01:02:19 +0000 | [diff] [blame] | 223 | // Now that we have initialized the diagnostics engine, create the target and |
| 224 | // the compiler invocation object. |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 225 | // |
| 226 | // FIXME: We should move .ast inputs to taking a separate path, they are |
| 227 | // really quite different. |
Daniel Dunbar | 67f401b | 2009-11-17 21:51:03 +0000 | [diff] [blame^] | 228 | bool IsAST = false; |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 229 | Clang.setTarget( |
| 230 | ConstructCompilerInvocation(Clang.getInvocation(), Clang.getDiagnostics(), |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 231 | argv[0], IsAST)); |
Daniel Dunbar | 704e48a | 2009-11-13 08:20:57 +0000 | [diff] [blame] | 232 | if (!Clang.hasTarget()) |
Daniel Dunbar | 21dac5e | 2009-11-13 01:02:19 +0000 | [diff] [blame] | 233 | return 1; |
Daniel Dunbar | 2626688 | 2009-11-12 23:52:32 +0000 | [diff] [blame] | 234 | |
Daniel Dunbar | 21dac5e | 2009-11-13 01:02:19 +0000 | [diff] [blame] | 235 | // Validate/process some options |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 236 | if (Clang.getHeaderSearchOpts().Verbose) |
Daniel Dunbar | 1417c74 | 2009-11-12 23:52:46 +0000 | [diff] [blame] | 237 | llvm::errs() << "clang-cc version " CLANG_VERSION_STRING |
| 238 | << " based upon " << PACKAGE_STRING |
| 239 | << " hosted on " << llvm::sys::getHostTriple() << "\n"; |
| 240 | |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 241 | if (Clang.getFrontendOpts().ShowTimers) |
Daniel Dunbar | 2626688 | 2009-11-12 23:52:32 +0000 | [diff] [blame] | 242 | ClangFrontendTimer = new llvm::Timer("Clang front-end time"); |
| 243 | |
Daniel Dunbar | 79b55f9 | 2009-11-14 04:39:29 +0000 | [diff] [blame] | 244 | // Enforce certain implications. |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 245 | if (!Clang.getFrontendOpts().ViewClassInheritance.empty()) |
Daniel Dunbar | 9a8a83b | 2009-11-14 22:32:38 +0000 | [diff] [blame] | 246 | Clang.getFrontendOpts().ProgramAction = frontend::InheritanceView; |
Daniel Dunbar | 79b55f9 | 2009-11-14 04:39:29 +0000 | [diff] [blame] | 247 | if (!Clang.getFrontendOpts().FixItLocations.empty()) |
Daniel Dunbar | 9a8a83b | 2009-11-14 22:32:38 +0000 | [diff] [blame] | 248 | Clang.getFrontendOpts().ProgramAction = frontend::FixIt; |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 249 | |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 250 | for (unsigned i = 0, e = Clang.getFrontendOpts().Inputs.size(); i != e; ++i) { |
| 251 | const std::string &InFile = Clang.getFrontendOpts().Inputs[i].second; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 252 | |
Daniel Dunbar | 4fdba99 | 2009-11-14 10:53:49 +0000 | [diff] [blame] | 253 | // If we aren't using an AST file, setup the file and source managers and |
| 254 | // the preprocessor. |
| 255 | if (!IsAST) { |
| 256 | if (!i) { |
| 257 | // Create a file manager object to provide access to and cache the |
| 258 | // filesystem. |
| 259 | Clang.createFileManager(); |
| 260 | |
| 261 | // Create the source manager. |
| 262 | Clang.createSourceManager(); |
| 263 | } else { |
| 264 | // Reset the ID tables if we are reusing the SourceManager. |
| 265 | Clang.getSourceManager().clearIDTables(); |
| 266 | } |
| 267 | |
| 268 | // Create the preprocessor. |
| 269 | Clang.createPreprocessor(); |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Daniel Dunbar | d10c5b8 | 2009-11-15 00:12:04 +0000 | [diff] [blame] | 272 | llvm::OwningPtr<FrontendAction> Act(CreateFrontendAction(Clang)); |
| 273 | if (!Act) |
| 274 | break; |
| 275 | |
Daniel Dunbar | 4fdba99 | 2009-11-14 10:53:49 +0000 | [diff] [blame] | 276 | Act->setCurrentTimer(ClangFrontendTimer); |
| 277 | if (Act->BeginSourceFile(Clang, InFile, IsAST)) { |
| 278 | Act->Execute(); |
| 279 | Act->EndSourceFile(); |
| 280 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 281 | } |
Chris Lattner | 1121519 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 282 | |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 283 | if (Clang.getDiagnosticOpts().ShowCarets) |
| 284 | if (unsigned NumDiagnostics = Clang.getDiagnostics().getNumDiagnostics()) |
Mike Stump | fc0fed3 | 2009-04-28 01:19:10 +0000 | [diff] [blame] | 285 | fprintf(stderr, "%d diagnostic%s generated.\n", NumDiagnostics, |
| 286 | (NumDiagnostics == 1 ? "" : "s")); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 287 | |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 288 | if (Clang.getFrontendOpts().ShowStats) { |
Daniel Dunbar | 16b7449 | 2009-11-13 04:12:06 +0000 | [diff] [blame] | 289 | Clang.getFileManager().PrintStats(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 290 | fprintf(stderr, "\n"); |
| 291 | } |
Chris Lattner | 75a97cb | 2009-04-17 21:05:01 +0000 | [diff] [blame] | 292 | |
| 293 | delete ClangFrontendTimer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 294 | |
Daniel Dunbar | f79dced | 2009-11-14 03:24:39 +0000 | [diff] [blame] | 295 | // Return the appropriate status when verifying diagnostics. |
| 296 | // |
| 297 | // FIXME: If we could make getNumErrors() do the right thing, we wouldn't need |
| 298 | // this. |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 299 | if (Clang.getDiagnosticOpts().VerifyDiagnostics) |
Daniel Dunbar | f79dced | 2009-11-14 03:24:39 +0000 | [diff] [blame] | 300 | return static_cast<VerifyDiagnosticsClient&>( |
| 301 | Clang.getDiagnosticClient()).HadErrors(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 302 | |
Daniel Dunbar | 524b86f | 2008-10-28 00:38:08 +0000 | [diff] [blame] | 303 | // Managed static deconstruction. Useful for making things like |
| 304 | // -time-passes usable. |
| 305 | llvm::llvm_shutdown(); |
| 306 | |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 307 | return (Clang.getDiagnostics().getNumErrors() != 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 308 | } |
Daniel Dunbar | d10c5b8 | 2009-11-15 00:12:04 +0000 | [diff] [blame] | 309 | |