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 | 9119e7e | 2009-11-16 22:38:48 +0000 | [diff] [blame] | 18 | #include "clang/Basic/Diagnostic.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 | c88aa79 | 2009-11-30 07:18:13 +0000 | [diff] [blame] | 23 | #include "clang/Driver/Arg.h" |
| 24 | #include "clang/Driver/ArgList.h" |
| 25 | #include "clang/Driver/CC1Options.h" |
| 26 | #include "clang/Driver/DriverDiagnostic.h" |
| 27 | #include "clang/Driver/OptTable.h" |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 28 | #include "clang/Frontend/CompilerInstance.h" |
Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 29 | #include "clang/Frontend/CompilerInvocation.h" |
Daniel Dunbar | 4fdba99 | 2009-11-14 10:53:49 +0000 | [diff] [blame] | 30 | #include "clang/Frontend/FrontendActions.h" |
Daniel Dunbar | 50f4f46 | 2009-03-12 10:14:16 +0000 | [diff] [blame] | 31 | #include "clang/Frontend/FrontendDiagnostic.h" |
Daniel Dunbar | d10c5b8 | 2009-11-15 00:12:04 +0000 | [diff] [blame] | 32 | #include "clang/Frontend/FrontendPluginRegistry.h" |
Daniel Dunbar | 9deb313 | 2009-11-30 08:42:10 +0000 | [diff] [blame] | 33 | #include "clang/Frontend/TextDiagnosticBuffer.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" |
Chris Lattner | 09e94a3 | 2009-03-04 21:41:39 +0000 | [diff] [blame] | 40 | #include "llvm/Support/PrettyStackTrace.h" |
Chris Lattner | 0fa0daa | 2009-08-24 04:11:30 +0000 | [diff] [blame] | 41 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | efba227 | 2009-12-03 05:11:05 +0000 | [diff] [blame] | 42 | #include "llvm/System/DynamicLibrary.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" |
Daniel Dunbar | 1a50628 | 2009-11-25 10:53:00 +0000 | [diff] [blame] | 47 | #include <cstdio> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 48 | using namespace clang; |
| 49 | |
| 50 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | c363cb1 | 2009-11-16 22:38:40 +0000 | [diff] [blame] | 51 | // Main driver |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 52 | //===----------------------------------------------------------------------===// |
| 53 | |
Daniel Dunbar | 5e96f9d | 2009-12-03 07:23:38 +0000 | [diff] [blame] | 54 | void LLVMErrorHandler(void *UserData, const std::string &Message) { |
Daniel Dunbar | c363cb1 | 2009-11-16 22:38:40 +0000 | [diff] [blame] | 55 | Diagnostic &Diags = *static_cast<Diagnostic*>(UserData); |
| 56 | |
| 57 | Diags.Report(diag::err_fe_error_backend) << Message; |
| 58 | |
| 59 | // We cannot recover from llvm errors. |
| 60 | exit(1); |
| 61 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 62 | |
Daniel Dunbar | d10c5b8 | 2009-11-15 00:12:04 +0000 | [diff] [blame] | 63 | static FrontendAction *CreateFrontendAction(CompilerInstance &CI) { |
Daniel Dunbar | 9a8a83b | 2009-11-14 22:32:38 +0000 | [diff] [blame] | 64 | using namespace clang::frontend; |
| 65 | |
Daniel Dunbar | d10c5b8 | 2009-11-15 00:12:04 +0000 | [diff] [blame] | 66 | switch (CI.getFrontendOpts().ProgramAction) { |
| 67 | default: |
| 68 | llvm::llvm_unreachable("Invalid program action!"); |
| 69 | |
Daniel Dunbar | 4fdba99 | 2009-11-14 10:53:49 +0000 | [diff] [blame] | 70 | case ASTDump: return new ASTDumpAction(); |
| 71 | case ASTPrint: return new ASTPrintAction(); |
| 72 | case ASTPrintXML: return new ASTPrintXMLAction(); |
| 73 | case ASTView: return new ASTViewAction(); |
| 74 | case DumpRawTokens: return new DumpRawTokensAction(); |
| 75 | case DumpRecordLayouts: return new DumpRecordAction(); |
| 76 | case DumpTokens: return new DumpTokensAction(); |
| 77 | case EmitAssembly: return new EmitAssemblyAction(); |
| 78 | case EmitBC: return new EmitBCAction(); |
| 79 | case EmitHTML: return new HTMLPrintAction(); |
| 80 | case EmitLLVM: return new EmitLLVMAction(); |
| 81 | case EmitLLVMOnly: return new EmitLLVMOnlyAction(); |
| 82 | case FixIt: return new FixItAction(); |
| 83 | case GeneratePCH: return new GeneratePCHAction(); |
| 84 | case GeneratePTH: return new GeneratePTHAction(); |
| 85 | case InheritanceView: return new InheritanceViewAction(); |
| 86 | case ParseNoop: return new ParseOnlyAction(); |
| 87 | case ParsePrintCallbacks: return new PrintParseAction(); |
| 88 | case ParseSyntaxOnly: return new SyntaxOnlyAction(); |
Daniel Dunbar | d10c5b8 | 2009-11-15 00:12:04 +0000 | [diff] [blame] | 89 | |
| 90 | case PluginAction: { |
| 91 | if (CI.getFrontendOpts().ActionName == "help") { |
| 92 | llvm::errs() << "clang-cc plugins:\n"; |
| 93 | for (FrontendPluginRegistry::iterator it = |
| 94 | FrontendPluginRegistry::begin(), |
| 95 | ie = FrontendPluginRegistry::end(); |
| 96 | it != ie; ++it) |
| 97 | llvm::errs() << " " << it->getName() << " - " << it->getDesc() << "\n"; |
Daniel Dunbar | 36841b3 | 2009-12-03 09:13:43 +0000 | [diff] [blame] | 98 | return 0; |
Daniel Dunbar | d10c5b8 | 2009-11-15 00:12:04 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | for (FrontendPluginRegistry::iterator it = |
| 102 | FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end(); |
| 103 | it != ie; ++it) { |
| 104 | if (it->getName() == CI.getFrontendOpts().ActionName) |
| 105 | return it->instantiate(); |
| 106 | } |
| 107 | |
| 108 | CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name) |
| 109 | << CI.getFrontendOpts().ActionName; |
| 110 | return 0; |
| 111 | } |
| 112 | |
Daniel Dunbar | 4fdba99 | 2009-11-14 10:53:49 +0000 | [diff] [blame] | 113 | case PrintDeclContext: return new DeclContextPrintAction(); |
| 114 | case PrintPreprocessedInput: return new PrintPreprocessedAction(); |
| 115 | case RewriteBlocks: return new RewriteBlocksAction(); |
| 116 | case RewriteMacros: return new RewriteMacrosAction(); |
| 117 | case RewriteObjC: return new RewriteObjCAction(); |
| 118 | case RewriteTest: return new RewriteTestAction(); |
| 119 | case RunAnalysis: return new AnalysisAction(); |
| 120 | case RunPreprocessorOnly: return new PreprocessOnlyAction(); |
Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 121 | } |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Daniel Dunbar | c88aa79 | 2009-11-30 07:18:13 +0000 | [diff] [blame] | 124 | static int cc1_main(Diagnostic &Diags, |
| 125 | const char **ArgBegin, const char **ArgEnd, |
| 126 | const char *Argv0, void *MainAddr) { |
| 127 | using namespace clang::driver; |
| 128 | |
| 129 | llvm::errs() << "cc1 argv:"; |
| 130 | for (const char **i = ArgBegin; i != ArgEnd; ++i) |
| 131 | llvm::errs() << " \"" << *i << '"'; |
| 132 | llvm::errs() << "\n"; |
| 133 | |
| 134 | // Parse the arguments. |
| 135 | OptTable *Opts = createCC1OptTable(); |
| 136 | unsigned MissingArgIndex, MissingArgCount; |
| 137 | InputArgList *Args = Opts->ParseArgs(ArgBegin, ArgEnd, |
| 138 | MissingArgIndex, MissingArgCount); |
| 139 | |
| 140 | // Check for missing argument error. |
| 141 | if (MissingArgCount) |
| 142 | Diags.Report(clang::diag::err_drv_missing_argument) |
| 143 | << Args->getArgString(MissingArgIndex) << MissingArgCount; |
| 144 | |
| 145 | // Dump the parsed arguments. |
| 146 | llvm::errs() << "cc1 parsed options:\n"; |
| 147 | for (ArgList::const_iterator it = Args->begin(), ie = Args->end(); |
| 148 | it != ie; ++it) |
| 149 | (*it)->dump(); |
| 150 | |
| 151 | // Create a compiler invocation. |
| 152 | llvm::errs() << "cc1 creating invocation.\n"; |
| 153 | CompilerInvocation Invocation; |
| 154 | CompilerInvocation::CreateFromArgs(Invocation, ArgBegin, ArgEnd, |
| 155 | Argv0, MainAddr, Diags); |
| 156 | |
| 157 | // Convert the invocation back to argument strings. |
| 158 | std::vector<std::string> InvocationArgs; |
| 159 | Invocation.toArgs(InvocationArgs); |
| 160 | |
| 161 | // Dump the converted arguments. |
| 162 | llvm::SmallVector<const char*, 32> Invocation2Args; |
| 163 | llvm::errs() << "invocation argv :"; |
| 164 | for (unsigned i = 0, e = InvocationArgs.size(); i != e; ++i) { |
| 165 | Invocation2Args.push_back(InvocationArgs[i].c_str()); |
| 166 | llvm::errs() << " \"" << InvocationArgs[i] << '"'; |
| 167 | } |
| 168 | llvm::errs() << "\n"; |
| 169 | |
| 170 | // Convert those arguments to another invocation, and check that we got the |
| 171 | // same thing. |
| 172 | CompilerInvocation Invocation2; |
| 173 | CompilerInvocation::CreateFromArgs(Invocation2, Invocation2Args.begin(), |
| 174 | Invocation2Args.end(), Argv0, MainAddr, |
| 175 | Diags); |
| 176 | |
| 177 | // FIXME: Implement CompilerInvocation comparison. |
| 178 | if (true) { |
| 179 | //llvm::errs() << "warning: Invocations differ!\n"; |
| 180 | |
| 181 | std::vector<std::string> Invocation2Args; |
| 182 | Invocation2.toArgs(Invocation2Args); |
| 183 | llvm::errs() << "invocation2 argv:"; |
| 184 | for (unsigned i = 0, e = Invocation2Args.size(); i != e; ++i) |
| 185 | llvm::errs() << " \"" << Invocation2Args[i] << '"'; |
| 186 | llvm::errs() << "\n"; |
| 187 | } |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 192 | int main(int argc, char **argv) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 193 | llvm::sys::PrintStackTraceOnErrorSignal(); |
Chris Lattner | 09e94a3 | 2009-03-04 21:41:39 +0000 | [diff] [blame] | 194 | llvm::PrettyStackTraceProgram X(argc, argv); |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 195 | CompilerInstance Clang(&llvm::getGlobalContext(), false); |
Daniel Dunbar | d697081 | 2009-09-02 23:20:15 +0000 | [diff] [blame] | 196 | |
Daniel Dunbar | c88aa79 | 2009-11-30 07:18:13 +0000 | [diff] [blame] | 197 | // Run clang -cc1 test. |
| 198 | if (argc > 1 && llvm::StringRef(argv[1]) == "-cc1") { |
| 199 | TextDiagnosticPrinter DiagClient(llvm::errs(), DiagnosticOptions()); |
| 200 | Diagnostic Diags(&DiagClient); |
| 201 | return cc1_main(Diags, (const char**) argv + 2, (const char**) argv + argc, |
Daniel Dunbar | 5e96f9d | 2009-12-03 07:23:38 +0000 | [diff] [blame] | 202 | argv[0], (void*) (intptr_t) LLVMErrorHandler); |
Daniel Dunbar | c88aa79 | 2009-11-30 07:18:13 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Daniel Dunbar | 4d86151 | 2009-09-03 04:54:12 +0000 | [diff] [blame] | 205 | // Initialize targets first, so that --version shows registered targets. |
Chris Lattner | 2fe1194 | 2009-06-17 17:25:50 +0000 | [diff] [blame] | 206 | llvm::InitializeAllTargets(); |
| 207 | llvm::InitializeAllAsmPrinters(); |
Daniel Dunbar | d697081 | 2009-09-02 23:20:15 +0000 | [diff] [blame] | 208 | |
Daniel Dunbar | efba227 | 2009-12-03 05:11:05 +0000 | [diff] [blame] | 209 | // Buffer diagnostics from argument parsing so that we can output them using a |
| 210 | // well formed diagnostic object. |
Daniel Dunbar | 9deb313 | 2009-11-30 08:42:10 +0000 | [diff] [blame] | 211 | TextDiagnosticBuffer DiagsBuffer; |
| 212 | Diagnostic Diags(&DiagsBuffer); |
Daniel Dunbar | 9deb313 | 2009-11-30 08:42:10 +0000 | [diff] [blame] | 213 | CompilerInvocation::CreateFromArgs(Clang.getInvocation(), |
| 214 | (const char**) argv + 1, |
| 215 | (const char**) argv + argc, argv[0], |
Daniel Dunbar | 5e96f9d | 2009-12-03 07:23:38 +0000 | [diff] [blame] | 216 | (void*)(intptr_t) LLVMErrorHandler, |
Daniel Dunbar | 9deb313 | 2009-11-30 08:42:10 +0000 | [diff] [blame] | 217 | Diags); |
| 218 | |
Daniel Dunbar | 9b5e9ae | 2009-12-03 07:01:58 +0000 | [diff] [blame] | 219 | // Honor -help. |
| 220 | if (Clang.getInvocation().getFrontendOpts().ShowHelp) { |
| 221 | llvm::OwningPtr<driver::OptTable> Opts(driver::createCC1OptTable()); |
| 222 | Opts->PrintHelp(llvm::outs(), "clang-cc", |
| 223 | "LLVM 'Clang' Compiler: http://clang.llvm.org"); |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | // Honor -version. |
| 228 | // |
| 229 | // FIXME: Use a better -version message? |
| 230 | if (Clang.getInvocation().getFrontendOpts().ShowVersion) { |
| 231 | llvm::cl::PrintVersionMessage(); |
| 232 | return 0; |
| 233 | } |
| 234 | |
Daniel Dunbar | 9deb313 | 2009-11-30 08:42:10 +0000 | [diff] [blame] | 235 | // Create the actual diagnostics engine. |
| 236 | Clang.createDiagnostics(argc, argv); |
| 237 | if (!Clang.hasDiagnostics()) |
| 238 | return 1; |
Steve Naroff | e0c4d89 | 2009-12-05 02:14:08 +0000 | [diff] [blame] | 239 | |
Daniel Dunbar | 9deb313 | 2009-11-30 08:42:10 +0000 | [diff] [blame] | 240 | // Set an error handler, so that any LLVM backend diagnostics go through our |
| 241 | // error handler. |
| 242 | llvm::llvm_install_error_handler(LLVMErrorHandler, |
| 243 | static_cast<void*>(&Clang.getDiagnostics())); |
| 244 | |
| 245 | DiagsBuffer.FlushDiagnostics(Clang.getDiagnostics()); |
| 246 | |
Daniel Dunbar | efba227 | 2009-12-03 05:11:05 +0000 | [diff] [blame] | 247 | // Load any requested plugins. |
| 248 | for (unsigned i = 0, |
| 249 | e = Clang.getFrontendOpts().Plugins.size(); i != e; ++i) { |
| 250 | const std::string &Path = Clang.getFrontendOpts().Plugins[i]; |
| 251 | std::string Error; |
| 252 | if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error)) |
| 253 | Diags.Report(diag::err_fe_unable_to_load_plugin) << Path << Error; |
| 254 | } |
| 255 | |
Daniel Dunbar | 9deb313 | 2009-11-30 08:42:10 +0000 | [diff] [blame] | 256 | // If there were any errors in processing arguments, exit now. |
| 257 | if (Clang.getDiagnostics().getNumErrors()) |
| 258 | return 1; |
Daniel Dunbar | 33b26ea3 | 2009-11-20 16:55:31 +0000 | [diff] [blame] | 259 | |
| 260 | // Create the target instance. |
| 261 | Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(), |
| 262 | Clang.getTargetOpts())); |
Daniel Dunbar | 704e48a | 2009-11-13 08:20:57 +0000 | [diff] [blame] | 263 | if (!Clang.hasTarget()) |
Daniel Dunbar | 21dac5e | 2009-11-13 01:02:19 +0000 | [diff] [blame] | 264 | return 1; |
Daniel Dunbar | 2626688 | 2009-11-12 23:52:32 +0000 | [diff] [blame] | 265 | |
Daniel Dunbar | 33b26ea3 | 2009-11-20 16:55:31 +0000 | [diff] [blame] | 266 | // Inform the target of the language options |
| 267 | // |
| 268 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 269 | // created. This complexity should be lifted elsewhere. |
| 270 | Clang.getTarget().setForcedLangOptions(Clang.getLangOpts()); |
| 271 | |
Daniel Dunbar | 21dac5e | 2009-11-13 01:02:19 +0000 | [diff] [blame] | 272 | // Validate/process some options |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 273 | if (Clang.getHeaderSearchOpts().Verbose) |
Daniel Dunbar | 1417c74 | 2009-11-12 23:52:46 +0000 | [diff] [blame] | 274 | llvm::errs() << "clang-cc version " CLANG_VERSION_STRING |
| 275 | << " based upon " << PACKAGE_STRING |
| 276 | << " hosted on " << llvm::sys::getHostTriple() << "\n"; |
| 277 | |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 278 | if (Clang.getFrontendOpts().ShowTimers) |
Kovarththanan Rajaratnam | f79bafa | 2009-11-29 09:57:35 +0000 | [diff] [blame] | 279 | Clang.createFrontendTimer(); |
Daniel Dunbar | 2626688 | 2009-11-12 23:52:32 +0000 | [diff] [blame] | 280 | |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 281 | for (unsigned i = 0, e = Clang.getFrontendOpts().Inputs.size(); i != e; ++i) { |
| 282 | const std::string &InFile = Clang.getFrontendOpts().Inputs[i].second; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 283 | |
Daniel Dunbar | 4fdba99 | 2009-11-14 10:53:49 +0000 | [diff] [blame] | 284 | // If we aren't using an AST file, setup the file and source managers and |
| 285 | // the preprocessor. |
Daniel Dunbar | ae8e17e | 2009-11-30 07:18:20 +0000 | [diff] [blame] | 286 | bool IsAST = |
| 287 | Clang.getFrontendOpts().Inputs[i].first == FrontendOptions::IK_AST; |
Daniel Dunbar | 4fdba99 | 2009-11-14 10:53:49 +0000 | [diff] [blame] | 288 | if (!IsAST) { |
| 289 | if (!i) { |
| 290 | // Create a file manager object to provide access to and cache the |
| 291 | // filesystem. |
| 292 | Clang.createFileManager(); |
| 293 | |
| 294 | // Create the source manager. |
| 295 | Clang.createSourceManager(); |
| 296 | } else { |
| 297 | // Reset the ID tables if we are reusing the SourceManager. |
| 298 | Clang.getSourceManager().clearIDTables(); |
| 299 | } |
| 300 | |
| 301 | // Create the preprocessor. |
| 302 | Clang.createPreprocessor(); |
Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Daniel Dunbar | d10c5b8 | 2009-11-15 00:12:04 +0000 | [diff] [blame] | 305 | llvm::OwningPtr<FrontendAction> Act(CreateFrontendAction(Clang)); |
| 306 | if (!Act) |
| 307 | break; |
| 308 | |
Daniel Dunbar | 4fdba99 | 2009-11-14 10:53:49 +0000 | [diff] [blame] | 309 | if (Act->BeginSourceFile(Clang, InFile, IsAST)) { |
| 310 | Act->Execute(); |
| 311 | Act->EndSourceFile(); |
| 312 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 313 | } |
Chris Lattner | 1121519 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 314 | |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 315 | if (Clang.getDiagnosticOpts().ShowCarets) |
| 316 | if (unsigned NumDiagnostics = Clang.getDiagnostics().getNumDiagnostics()) |
Mike Stump | fc0fed3 | 2009-04-28 01:19:10 +0000 | [diff] [blame] | 317 | fprintf(stderr, "%d diagnostic%s generated.\n", NumDiagnostics, |
| 318 | (NumDiagnostics == 1 ? "" : "s")); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 319 | |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 320 | if (Clang.getFrontendOpts().ShowStats) { |
Daniel Dunbar | 16b7449 | 2009-11-13 04:12:06 +0000 | [diff] [blame] | 321 | Clang.getFileManager().PrintStats(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 322 | fprintf(stderr, "\n"); |
| 323 | } |
Chris Lattner | 75a97cb | 2009-04-17 21:05:01 +0000 | [diff] [blame] | 324 | |
Daniel Dunbar | f79dced | 2009-11-14 03:24:39 +0000 | [diff] [blame] | 325 | // Return the appropriate status when verifying diagnostics. |
| 326 | // |
| 327 | // FIXME: If we could make getNumErrors() do the right thing, we wouldn't need |
| 328 | // this. |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 329 | if (Clang.getDiagnosticOpts().VerifyDiagnostics) |
Daniel Dunbar | f79dced | 2009-11-14 03:24:39 +0000 | [diff] [blame] | 330 | return static_cast<VerifyDiagnosticsClient&>( |
| 331 | Clang.getDiagnosticClient()).HadErrors(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 332 | |
Daniel Dunbar | 524b86f | 2008-10-28 00:38:08 +0000 | [diff] [blame] | 333 | // Managed static deconstruction. Useful for making things like |
| 334 | // -time-passes usable. |
| 335 | llvm::llvm_shutdown(); |
| 336 | |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 337 | return (Clang.getDiagnostics().getNumErrors() != 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 338 | } |
Daniel Dunbar | d10c5b8 | 2009-11-15 00:12:04 +0000 | [diff] [blame] | 339 | |