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