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