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 | c88aa79 | 2009-11-30 07:18:13 +0000 | [diff] [blame] | 24 | #include "clang/Driver/Arg.h" |
| 25 | #include "clang/Driver/ArgList.h" |
| 26 | #include "clang/Driver/CC1Options.h" |
| 27 | #include "clang/Driver/DriverDiagnostic.h" |
| 28 | #include "clang/Driver/OptTable.h" |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 29 | #include "clang/Frontend/CompilerInstance.h" |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 30 | #include "clang/Frontend/CompilerInvocation.h" |
Daniel Dunbar | 4fdba99 | 2009-11-14 10:53:49 +0000 | [diff] [blame] | 31 | #include "clang/Frontend/FrontendActions.h" |
Daniel Dunbar | 50f4f46 | 2009-03-12 10:14:16 +0000 | [diff] [blame] | 32 | #include "clang/Frontend/FrontendDiagnostic.h" |
Daniel Dunbar | d10c5b8 | 2009-11-15 00:12:04 +0000 | [diff] [blame] | 33 | #include "clang/Frontend/FrontendPluginRegistry.h" |
Daniel Dunbar | c88aa79 | 2009-11-30 07:18:13 +0000 | [diff] [blame] | 34 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Daniel Dunbar | f79dced | 2009-11-14 03:24:39 +0000 | [diff] [blame] | 35 | #include "clang/Frontend/VerifyDiagnosticsClient.h" |
Daniel Dunbar | 4fdba99 | 2009-11-14 10:53:49 +0000 | [diff] [blame] | 36 | #include "llvm/LLVMContext.h" |
Daniel Dunbar | 9119e7e | 2009-11-16 22:38:48 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/OwningPtr.h" |
Daniel Dunbar | 70121eb | 2009-08-10 03:40:28 +0000 | [diff] [blame] | 38 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | 524b86f | 2008-10-28 00:38:08 +0000 | [diff] [blame] | 39 | #include "llvm/Support/ManagedStatic.h" |
Zhongxing Xu | 2092236 | 2008-11-26 05:23:17 +0000 | [diff] [blame] | 40 | #include "llvm/Support/PluginLoader.h" |
Chris Lattner | 09e94a3 | 2009-03-04 21:41:39 +0000 | [diff] [blame] | 41 | #include "llvm/Support/PrettyStackTrace.h" |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 42 | #include "llvm/Support/Timer.h" |
Chris Lattner | 0fa0daa | 2009-08-24 04:11:30 +0000 | [diff] [blame] | 43 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | e553a72 | 2008-10-02 01:21:33 +0000 | [diff] [blame] | 44 | #include "llvm/System/Host.h" |
Chris Lattner | dcaa096 | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 45 | #include "llvm/System/Path.h" |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 46 | #include "llvm/System/Signals.h" |
Chris Lattner | 2fe1194 | 2009-06-17 17:25:50 +0000 | [diff] [blame] | 47 | #include "llvm/Target/TargetSelect.h" |
Daniel Dunbar | 1a50628 | 2009-11-25 10:53:00 +0000 | [diff] [blame] | 48 | #include <cstdio> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 49 | using namespace clang; |
| 50 | |
| 51 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | c363cb1 | 2009-11-16 22:38:40 +0000 | [diff] [blame] | 52 | // Main driver |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 53 | //===----------------------------------------------------------------------===// |
| 54 | |
Daniel Dunbar | 750156a | 2009-11-07 04:19:57 +0000 | [diff] [blame] | 55 | std::string GetBuiltinIncludePath(const char *Argv0) { |
| 56 | llvm::sys::Path P = |
| 57 | llvm::sys::Path::GetMainExecutable(Argv0, |
| 58 | (void*)(intptr_t) GetBuiltinIncludePath); |
Rafael Espindola | 1bb15a9 | 2009-10-05 13:12:17 +0000 | [diff] [blame] | 59 | |
Daniel Dunbar | 750156a | 2009-11-07 04:19:57 +0000 | [diff] [blame] | 60 | if (!P.isEmpty()) { |
| 61 | P.eraseComponent(); // Remove /clang from foo/bin/clang |
| 62 | P.eraseComponent(); // Remove /bin from foo/bin |
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 | // Get foo/lib/clang/<version>/include |
| 65 | P.appendComponent("lib"); |
| 66 | P.appendComponent("clang"); |
| 67 | P.appendComponent(CLANG_VERSION_STRING); |
| 68 | P.appendComponent("include"); |
| 69 | } |
Rafael Espindola | 1bb15a9 | 2009-10-05 13:12:17 +0000 | [diff] [blame] | 70 | |
Daniel Dunbar | 750156a | 2009-11-07 04:19:57 +0000 | [diff] [blame] | 71 | return P.str(); |
Rafael Espindola | 1bb15a9 | 2009-10-05 13:12:17 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Daniel Dunbar | c363cb1 | 2009-11-16 22:38:40 +0000 | [diff] [blame] | 74 | static void LLVMErrorHandler(void *UserData, const std::string &Message) { |
| 75 | Diagnostic &Diags = *static_cast<Diagnostic*>(UserData); |
| 76 | |
| 77 | Diags.Report(diag::err_fe_error_backend) << Message; |
| 78 | |
| 79 | // We cannot recover from llvm errors. |
| 80 | exit(1); |
| 81 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 82 | |
Daniel Dunbar | d10c5b8 | 2009-11-15 00:12:04 +0000 | [diff] [blame] | 83 | static FrontendAction *CreateFrontendAction(CompilerInstance &CI) { |
Daniel Dunbar | 9a8a83b | 2009-11-14 22:32:38 +0000 | [diff] [blame] | 84 | using namespace clang::frontend; |
| 85 | |
Daniel Dunbar | d10c5b8 | 2009-11-15 00:12:04 +0000 | [diff] [blame] | 86 | switch (CI.getFrontendOpts().ProgramAction) { |
| 87 | default: |
| 88 | llvm::llvm_unreachable("Invalid program action!"); |
| 89 | |
Daniel Dunbar | 4fdba99 | 2009-11-14 10:53:49 +0000 | [diff] [blame] | 90 | case ASTDump: return new ASTDumpAction(); |
| 91 | case ASTPrint: return new ASTPrintAction(); |
| 92 | case ASTPrintXML: return new ASTPrintXMLAction(); |
| 93 | case ASTView: return new ASTViewAction(); |
| 94 | case DumpRawTokens: return new DumpRawTokensAction(); |
| 95 | case DumpRecordLayouts: return new DumpRecordAction(); |
| 96 | case DumpTokens: return new DumpTokensAction(); |
| 97 | case EmitAssembly: return new EmitAssemblyAction(); |
| 98 | case EmitBC: return new EmitBCAction(); |
| 99 | case EmitHTML: return new HTMLPrintAction(); |
| 100 | case EmitLLVM: return new EmitLLVMAction(); |
| 101 | case EmitLLVMOnly: return new EmitLLVMOnlyAction(); |
| 102 | case FixIt: return new FixItAction(); |
| 103 | case GeneratePCH: return new GeneratePCHAction(); |
| 104 | case GeneratePTH: return new GeneratePTHAction(); |
| 105 | case InheritanceView: return new InheritanceViewAction(); |
| 106 | case ParseNoop: return new ParseOnlyAction(); |
| 107 | case ParsePrintCallbacks: return new PrintParseAction(); |
| 108 | case ParseSyntaxOnly: return new SyntaxOnlyAction(); |
Daniel Dunbar | d10c5b8 | 2009-11-15 00:12:04 +0000 | [diff] [blame] | 109 | |
| 110 | case PluginAction: { |
| 111 | if (CI.getFrontendOpts().ActionName == "help") { |
| 112 | llvm::errs() << "clang-cc plugins:\n"; |
| 113 | for (FrontendPluginRegistry::iterator it = |
| 114 | FrontendPluginRegistry::begin(), |
| 115 | ie = FrontendPluginRegistry::end(); |
| 116 | it != ie; ++it) |
| 117 | llvm::errs() << " " << it->getName() << " - " << it->getDesc() << "\n"; |
| 118 | exit(1); |
| 119 | } |
| 120 | |
| 121 | for (FrontendPluginRegistry::iterator it = |
| 122 | FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end(); |
| 123 | it != ie; ++it) { |
| 124 | if (it->getName() == CI.getFrontendOpts().ActionName) |
| 125 | return it->instantiate(); |
| 126 | } |
| 127 | |
| 128 | CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name) |
| 129 | << CI.getFrontendOpts().ActionName; |
| 130 | return 0; |
| 131 | } |
| 132 | |
Daniel Dunbar | 4fdba99 | 2009-11-14 10:53:49 +0000 | [diff] [blame] | 133 | case PrintDeclContext: return new DeclContextPrintAction(); |
| 134 | case PrintPreprocessedInput: return new PrintPreprocessedAction(); |
| 135 | case RewriteBlocks: return new RewriteBlocksAction(); |
| 136 | case RewriteMacros: return new RewriteMacrosAction(); |
| 137 | case RewriteObjC: return new RewriteObjCAction(); |
| 138 | case RewriteTest: return new RewriteTestAction(); |
| 139 | case RunAnalysis: return new AnalysisAction(); |
| 140 | case RunPreprocessorOnly: return new PreprocessOnlyAction(); |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 141 | } |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Daniel Dunbar | 33b26ea3 | 2009-11-20 16:55:31 +0000 | [diff] [blame] | 144 | static bool ConstructCompilerInvocation(CompilerInvocation &Opts, |
Daniel Dunbar | ae8e17e | 2009-11-30 07:18:20 +0000 | [diff] [blame] | 145 | Diagnostic &Diags, const char *Argv0) { |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 146 | // Initialize target options. |
| 147 | InitializeTargetOptions(Opts.getTargetOpts()); |
Daniel Dunbar | 21dac5e | 2009-11-13 01:02:19 +0000 | [diff] [blame] | 148 | |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 149 | // Initialize frontend options. |
| 150 | InitializeFrontendOptions(Opts.getFrontendOpts()); |
Daniel Dunbar | fcb0c3b | 2009-11-10 18:47:35 +0000 | [diff] [blame] | 151 | |
Daniel Dunbar | fbe2faf | 2009-11-13 02:06:12 +0000 | [diff] [blame] | 152 | // Determine the input language, we currently require all files to match. |
| 153 | FrontendOptions::InputKind IK = Opts.getFrontendOpts().Inputs[0].first; |
| 154 | for (unsigned i = 1, e = Opts.getFrontendOpts().Inputs.size(); i != e; ++i) { |
| 155 | if (Opts.getFrontendOpts().Inputs[i].first != IK) { |
| 156 | llvm::errs() << "error: cannot have multiple input files of distinct " |
| 157 | << "language kinds without -x\n"; |
Daniel Dunbar | 33b26ea3 | 2009-11-20 16:55:31 +0000 | [diff] [blame] | 158 | return false; |
Daniel Dunbar | fbe2faf | 2009-11-13 02:06:12 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 162 | // Initialize language options. |
Daniel Dunbar | 5746f1f | 2009-11-12 00:24:10 +0000 | [diff] [blame] | 163 | // |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 164 | // FIXME: These aren't used during operations on ASTs. Split onto a separate |
| 165 | // code path to make this obvious. |
Daniel Dunbar | ae8e17e | 2009-11-30 07:18:20 +0000 | [diff] [blame] | 166 | if (IK != FrontendOptions::IK_AST) |
Daniel Dunbar | 33b26ea3 | 2009-11-20 16:55:31 +0000 | [diff] [blame] | 167 | InitializeLangOptions(Opts.getLangOpts(), IK); |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 168 | |
Daniel Dunbar | 5746f1f | 2009-11-12 00:24:10 +0000 | [diff] [blame] | 169 | // Initialize the static analyzer options. |
| 170 | InitializeAnalyzerOptions(Opts.getAnalyzerOpts()); |
| 171 | |
Daniel Dunbar | 0e0bae8 | 2009-11-11 21:43:12 +0000 | [diff] [blame] | 172 | // Initialize the dependency output options (-M...). |
| 173 | InitializeDependencyOutputOptions(Opts.getDependencyOutputOpts()); |
| 174 | |
Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 175 | // Initialize the header search options. |
Daniel Dunbar | f797329 | 2009-11-11 08:13:32 +0000 | [diff] [blame] | 176 | InitializeHeaderSearchOptions(Opts.getHeaderSearchOpts(), |
Daniel Dunbar | c363cb1 | 2009-11-16 22:38:40 +0000 | [diff] [blame] | 177 | GetBuiltinIncludePath(Argv0)); |
Daniel Dunbar | 5fc7d34 | 2009-11-09 23:12:31 +0000 | [diff] [blame] | 178 | |
| 179 | // Initialize the other preprocessor options. |
| 180 | InitializePreprocessorOptions(Opts.getPreprocessorOpts()); |
Daniel Dunbar | 36f4ec3 | 2009-11-10 16:19:45 +0000 | [diff] [blame] | 181 | |
Daniel Dunbar | 29cf746 | 2009-11-11 10:07:44 +0000 | [diff] [blame] | 182 | // Initialize the preprocessed output options. |
| 183 | InitializePreprocessorOutputOptions(Opts.getPreprocessorOutputOpts()); |
| 184 | |
Daniel Dunbar | 33b26ea3 | 2009-11-20 16:55:31 +0000 | [diff] [blame] | 185 | // Initialize backend options. |
Daniel Dunbar | b33fbaa | 2009-11-30 08:39:52 +0000 | [diff] [blame] | 186 | InitializeCodeGenOptions(Opts.getCodeGenOpts(), Opts.getLangOpts()); |
Daniel Dunbar | 21dac5e | 2009-11-13 01:02:19 +0000 | [diff] [blame] | 187 | |
Daniel Dunbar | 33b26ea3 | 2009-11-20 16:55:31 +0000 | [diff] [blame] | 188 | return true; |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Daniel Dunbar | c88aa79 | 2009-11-30 07:18:13 +0000 | [diff] [blame] | 191 | static int cc1_main(Diagnostic &Diags, |
| 192 | const char **ArgBegin, const char **ArgEnd, |
| 193 | const char *Argv0, void *MainAddr) { |
| 194 | using namespace clang::driver; |
| 195 | |
| 196 | llvm::errs() << "cc1 argv:"; |
| 197 | for (const char **i = ArgBegin; i != ArgEnd; ++i) |
| 198 | llvm::errs() << " \"" << *i << '"'; |
| 199 | llvm::errs() << "\n"; |
| 200 | |
| 201 | // Parse the arguments. |
| 202 | OptTable *Opts = createCC1OptTable(); |
| 203 | unsigned MissingArgIndex, MissingArgCount; |
| 204 | InputArgList *Args = Opts->ParseArgs(ArgBegin, ArgEnd, |
| 205 | MissingArgIndex, MissingArgCount); |
| 206 | |
| 207 | // Check for missing argument error. |
| 208 | if (MissingArgCount) |
| 209 | Diags.Report(clang::diag::err_drv_missing_argument) |
| 210 | << Args->getArgString(MissingArgIndex) << MissingArgCount; |
| 211 | |
| 212 | // Dump the parsed arguments. |
| 213 | llvm::errs() << "cc1 parsed options:\n"; |
| 214 | for (ArgList::const_iterator it = Args->begin(), ie = Args->end(); |
| 215 | it != ie; ++it) |
| 216 | (*it)->dump(); |
| 217 | |
| 218 | // Create a compiler invocation. |
| 219 | llvm::errs() << "cc1 creating invocation.\n"; |
| 220 | CompilerInvocation Invocation; |
| 221 | CompilerInvocation::CreateFromArgs(Invocation, ArgBegin, ArgEnd, |
| 222 | Argv0, MainAddr, Diags); |
| 223 | |
| 224 | // Convert the invocation back to argument strings. |
| 225 | std::vector<std::string> InvocationArgs; |
| 226 | Invocation.toArgs(InvocationArgs); |
| 227 | |
| 228 | // Dump the converted arguments. |
| 229 | llvm::SmallVector<const char*, 32> Invocation2Args; |
| 230 | llvm::errs() << "invocation argv :"; |
| 231 | for (unsigned i = 0, e = InvocationArgs.size(); i != e; ++i) { |
| 232 | Invocation2Args.push_back(InvocationArgs[i].c_str()); |
| 233 | llvm::errs() << " \"" << InvocationArgs[i] << '"'; |
| 234 | } |
| 235 | llvm::errs() << "\n"; |
| 236 | |
| 237 | // Convert those arguments to another invocation, and check that we got the |
| 238 | // same thing. |
| 239 | CompilerInvocation Invocation2; |
| 240 | CompilerInvocation::CreateFromArgs(Invocation2, Invocation2Args.begin(), |
| 241 | Invocation2Args.end(), Argv0, MainAddr, |
| 242 | Diags); |
| 243 | |
| 244 | // FIXME: Implement CompilerInvocation comparison. |
| 245 | if (true) { |
| 246 | //llvm::errs() << "warning: Invocations differ!\n"; |
| 247 | |
| 248 | std::vector<std::string> Invocation2Args; |
| 249 | Invocation2.toArgs(Invocation2Args); |
| 250 | llvm::errs() << "invocation2 argv:"; |
| 251 | for (unsigned i = 0, e = Invocation2Args.size(); i != e; ++i) |
| 252 | llvm::errs() << " \"" << Invocation2Args[i] << '"'; |
| 253 | llvm::errs() << "\n"; |
| 254 | } |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 259 | int main(int argc, char **argv) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 260 | llvm::sys::PrintStackTraceOnErrorSignal(); |
Chris Lattner | 09e94a3 | 2009-03-04 21:41:39 +0000 | [diff] [blame] | 261 | llvm::PrettyStackTraceProgram X(argc, argv); |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 262 | CompilerInstance Clang(&llvm::getGlobalContext(), false); |
Daniel Dunbar | d697081 | 2009-09-02 23:20:15 +0000 | [diff] [blame] | 263 | |
Daniel Dunbar | c88aa79 | 2009-11-30 07:18:13 +0000 | [diff] [blame] | 264 | // Run clang -cc1 test. |
| 265 | if (argc > 1 && llvm::StringRef(argv[1]) == "-cc1") { |
| 266 | TextDiagnosticPrinter DiagClient(llvm::errs(), DiagnosticOptions()); |
| 267 | Diagnostic Diags(&DiagClient); |
| 268 | return cc1_main(Diags, (const char**) argv + 2, (const char**) argv + argc, |
| 269 | argv[0], (void*) (intptr_t) GetBuiltinIncludePath); |
| 270 | } |
| 271 | |
Daniel Dunbar | 4d86151 | 2009-09-03 04:54:12 +0000 | [diff] [blame] | 272 | // Initialize targets first, so that --version shows registered targets. |
Chris Lattner | 2fe1194 | 2009-06-17 17:25:50 +0000 | [diff] [blame] | 273 | llvm::InitializeAllTargets(); |
| 274 | llvm::InitializeAllAsmPrinters(); |
Daniel Dunbar | d697081 | 2009-09-02 23:20:15 +0000 | [diff] [blame] | 275 | |
| 276 | llvm::cl::ParseCommandLineOptions(argc, argv, |
| 277 | "LLVM 'Clang' Compiler: http://clang.llvm.org\n"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 278 | |
Daniel Dunbar | 00e5b8d | 2009-11-12 06:48:31 +0000 | [diff] [blame] | 279 | // Construct the diagnostic engine first, so that we can build a diagnostic |
| 280 | // client to use for any errors during option handling. |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 281 | InitializeDiagnosticOptions(Clang.getDiagnosticOpts()); |
| 282 | Clang.createDiagnostics(argc, argv); |
Daniel Dunbar | 704e48a | 2009-11-13 08:20:57 +0000 | [diff] [blame] | 283 | if (!Clang.hasDiagnostics()) |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 284 | return 1; |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 285 | |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 286 | // Set an error handler, so that any LLVM backend diagnostics go through our |
| 287 | // error handler. |
| 288 | llvm::llvm_install_error_handler(LLVMErrorHandler, |
| 289 | static_cast<void*>(&Clang.getDiagnostics())); |
Daniel Dunbar | 70121eb | 2009-08-10 03:40:28 +0000 | [diff] [blame] | 290 | |
Daniel Dunbar | 21dac5e | 2009-11-13 01:02:19 +0000 | [diff] [blame] | 291 | // Now that we have initialized the diagnostics engine, create the target and |
| 292 | // the compiler invocation object. |
Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 293 | // |
| 294 | // FIXME: We should move .ast inputs to taking a separate path, they are |
| 295 | // really quite different. |
Daniel Dunbar | 33b26ea3 | 2009-11-20 16:55:31 +0000 | [diff] [blame] | 296 | if (!ConstructCompilerInvocation(Clang.getInvocation(), |
Daniel Dunbar | ae8e17e | 2009-11-30 07:18:20 +0000 | [diff] [blame] | 297 | Clang.getDiagnostics(), argv[0])) |
Daniel Dunbar | 33b26ea3 | 2009-11-20 16:55:31 +0000 | [diff] [blame] | 298 | return 1; |
| 299 | |
| 300 | // Create the target instance. |
| 301 | Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(), |
| 302 | Clang.getTargetOpts())); |
Daniel Dunbar | 704e48a | 2009-11-13 08:20:57 +0000 | [diff] [blame] | 303 | if (!Clang.hasTarget()) |
Daniel Dunbar | 21dac5e | 2009-11-13 01:02:19 +0000 | [diff] [blame] | 304 | return 1; |
Daniel Dunbar | 2626688 | 2009-11-12 23:52:32 +0000 | [diff] [blame] | 305 | |
Daniel Dunbar | 33b26ea3 | 2009-11-20 16:55:31 +0000 | [diff] [blame] | 306 | // Inform the target of the language options |
| 307 | // |
| 308 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 309 | // created. This complexity should be lifted elsewhere. |
| 310 | Clang.getTarget().setForcedLangOptions(Clang.getLangOpts()); |
| 311 | |
Daniel Dunbar | 21dac5e | 2009-11-13 01:02:19 +0000 | [diff] [blame] | 312 | // Validate/process some options |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 313 | if (Clang.getHeaderSearchOpts().Verbose) |
Daniel Dunbar | 1417c74 | 2009-11-12 23:52:46 +0000 | [diff] [blame] | 314 | llvm::errs() << "clang-cc version " CLANG_VERSION_STRING |
| 315 | << " based upon " << PACKAGE_STRING |
| 316 | << " hosted on " << llvm::sys::getHostTriple() << "\n"; |
| 317 | |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 318 | if (Clang.getFrontendOpts().ShowTimers) |
Kovarththanan Rajaratnam | f79bafa | 2009-11-29 09:57:35 +0000 | [diff] [blame] | 319 | Clang.createFrontendTimer(); |
Daniel Dunbar | 2626688 | 2009-11-12 23:52:32 +0000 | [diff] [blame] | 320 | |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 321 | for (unsigned i = 0, e = Clang.getFrontendOpts().Inputs.size(); i != e; ++i) { |
| 322 | const std::string &InFile = Clang.getFrontendOpts().Inputs[i].second; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 323 | |
Daniel Dunbar | 4fdba99 | 2009-11-14 10:53:49 +0000 | [diff] [blame] | 324 | // If we aren't using an AST file, setup the file and source managers and |
| 325 | // the preprocessor. |
Daniel Dunbar | ae8e17e | 2009-11-30 07:18:20 +0000 | [diff] [blame] | 326 | bool IsAST = |
| 327 | Clang.getFrontendOpts().Inputs[i].first == FrontendOptions::IK_AST; |
Daniel Dunbar | 4fdba99 | 2009-11-14 10:53:49 +0000 | [diff] [blame] | 328 | if (!IsAST) { |
| 329 | if (!i) { |
| 330 | // Create a file manager object to provide access to and cache the |
| 331 | // filesystem. |
| 332 | Clang.createFileManager(); |
| 333 | |
| 334 | // Create the source manager. |
| 335 | Clang.createSourceManager(); |
| 336 | } else { |
| 337 | // Reset the ID tables if we are reusing the SourceManager. |
| 338 | Clang.getSourceManager().clearIDTables(); |
| 339 | } |
| 340 | |
| 341 | // Create the preprocessor. |
| 342 | Clang.createPreprocessor(); |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Daniel Dunbar | d10c5b8 | 2009-11-15 00:12:04 +0000 | [diff] [blame] | 345 | llvm::OwningPtr<FrontendAction> Act(CreateFrontendAction(Clang)); |
| 346 | if (!Act) |
| 347 | break; |
| 348 | |
Daniel Dunbar | 4fdba99 | 2009-11-14 10:53:49 +0000 | [diff] [blame] | 349 | if (Act->BeginSourceFile(Clang, InFile, IsAST)) { |
| 350 | Act->Execute(); |
| 351 | Act->EndSourceFile(); |
| 352 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 353 | } |
Chris Lattner | 1121519 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 354 | |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 355 | if (Clang.getDiagnosticOpts().ShowCarets) |
| 356 | if (unsigned NumDiagnostics = Clang.getDiagnostics().getNumDiagnostics()) |
Mike Stump | fc0fed3 | 2009-04-28 01:19:10 +0000 | [diff] [blame] | 357 | fprintf(stderr, "%d diagnostic%s generated.\n", NumDiagnostics, |
| 358 | (NumDiagnostics == 1 ? "" : "s")); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 359 | |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 360 | if (Clang.getFrontendOpts().ShowStats) { |
Daniel Dunbar | 16b7449 | 2009-11-13 04:12:06 +0000 | [diff] [blame] | 361 | Clang.getFileManager().PrintStats(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 362 | fprintf(stderr, "\n"); |
| 363 | } |
Chris Lattner | 75a97cb | 2009-04-17 21:05:01 +0000 | [diff] [blame] | 364 | |
Daniel Dunbar | f79dced | 2009-11-14 03:24:39 +0000 | [diff] [blame] | 365 | // Return the appropriate status when verifying diagnostics. |
| 366 | // |
| 367 | // FIXME: If we could make getNumErrors() do the right thing, we wouldn't need |
| 368 | // this. |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 369 | if (Clang.getDiagnosticOpts().VerifyDiagnostics) |
Daniel Dunbar | f79dced | 2009-11-14 03:24:39 +0000 | [diff] [blame] | 370 | return static_cast<VerifyDiagnosticsClient&>( |
| 371 | Clang.getDiagnosticClient()).HadErrors(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 372 | |
Daniel Dunbar | 524b86f | 2008-10-28 00:38:08 +0000 | [diff] [blame] | 373 | // Managed static deconstruction. Useful for making things like |
| 374 | // -time-passes usable. |
| 375 | llvm::llvm_shutdown(); |
| 376 | |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 377 | return (Clang.getDiagnostics().getNumErrors() != 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 378 | } |
Daniel Dunbar | d10c5b8 | 2009-11-15 00:12:04 +0000 | [diff] [blame] | 379 | |