Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 1 | //===--- FrontendAction.cpp -----------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "clang/Frontend/FrontendAction.h" |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 11 | #include "clang/AST/ASTConsumer.h" |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 12 | #include "clang/AST/ASTContext.h" |
Nico Weber | 5aa74af | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 13 | #include "clang/AST/DeclGroup.h" |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 14 | #include "clang/Lex/HeaderSearch.h" |
| 15 | #include "clang/Lex/Preprocessor.h" |
| 16 | #include "clang/Frontend/ASTUnit.h" |
| 17 | #include "clang/Frontend/CompilerInstance.h" |
| 18 | #include "clang/Frontend/FrontendDiagnostic.h" |
Nico Weber | 5aa74af | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 19 | #include "clang/Frontend/FrontendPluginRegistry.h" |
| 20 | #include "clang/Frontend/MultiplexConsumer.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 21 | #include "clang/Parse/ParseAST.h" |
Argyrios Kyrtzidis | b972858 | 2010-10-14 20:14:18 +0000 | [diff] [blame] | 22 | #include "clang/Serialization/ASTDeserializationListener.h" |
Argyrios Kyrtzidis | b0f4b9a | 2011-03-09 17:21:42 +0000 | [diff] [blame] | 23 | #include "clang/Serialization/ChainedIncludesSource.h" |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 24 | #include "llvm/Support/MemoryBuffer.h" |
| 25 | #include "llvm/Support/Timer.h" |
| 26 | #include "llvm/Support/ErrorHandling.h" |
| 27 | #include "llvm/Support/raw_ostream.h" |
| 28 | using namespace clang; |
| 29 | |
Argyrios Kyrtzidis | b972858 | 2010-10-14 20:14:18 +0000 | [diff] [blame] | 30 | namespace { |
| 31 | |
| 32 | /// \brief Dumps deserialized declarations. |
| 33 | class DeserializedDeclsDumper : public ASTDeserializationListener { |
| 34 | ASTDeserializationListener *Previous; |
| 35 | |
| 36 | public: |
| 37 | DeserializedDeclsDumper(ASTDeserializationListener *Previous) |
| 38 | : Previous(Previous) { } |
| 39 | |
| 40 | virtual void DeclRead(serialization::DeclID ID, const Decl *D) { |
| 41 | llvm::outs() << "PCH DECL: " << D->getDeclKindName(); |
| 42 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
| 43 | llvm::outs() << " - " << ND->getNameAsString(); |
| 44 | llvm::outs() << "\n"; |
| 45 | |
| 46 | if (Previous) |
| 47 | Previous->DeclRead(ID, D); |
| 48 | } |
Argyrios Kyrtzidis | b972858 | 2010-10-14 20:14:18 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
Argyrios Kyrtzidis | 3e78593 | 2010-10-14 20:14:25 +0000 | [diff] [blame] | 51 | /// \brief Checks deserialized declarations and emits error if a name |
| 52 | /// matches one given in command-line using -error-on-deserialized-decl. |
| 53 | class DeserializedDeclsChecker : public ASTDeserializationListener { |
| 54 | ASTContext &Ctx; |
| 55 | std::set<std::string> NamesToCheck; |
| 56 | ASTDeserializationListener *Previous; |
| 57 | |
| 58 | public: |
| 59 | DeserializedDeclsChecker(ASTContext &Ctx, |
| 60 | const std::set<std::string> &NamesToCheck, |
| 61 | ASTDeserializationListener *Previous) |
| 62 | : Ctx(Ctx), NamesToCheck(NamesToCheck), Previous(Previous) { } |
| 63 | |
| 64 | virtual void DeclRead(serialization::DeclID ID, const Decl *D) { |
| 65 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
| 66 | if (NamesToCheck.find(ND->getNameAsString()) != NamesToCheck.end()) { |
| 67 | unsigned DiagID |
| 68 | = Ctx.getDiagnostics().getCustomDiagID(Diagnostic::Error, |
| 69 | "%0 was deserialized"); |
| 70 | Ctx.getDiagnostics().Report(Ctx.getFullLoc(D->getLocation()), DiagID) |
| 71 | << ND->getNameAsString(); |
| 72 | } |
| 73 | |
| 74 | if (Previous) |
| 75 | Previous->DeclRead(ID, D); |
| 76 | } |
Argyrios Kyrtzidis | 3e78593 | 2010-10-14 20:14:25 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
Argyrios Kyrtzidis | b972858 | 2010-10-14 20:14:18 +0000 | [diff] [blame] | 79 | } // end anonymous namespace |
| 80 | |
Kovarththanan Rajaratnam | f79bafa | 2009-11-29 09:57:35 +0000 | [diff] [blame] | 81 | FrontendAction::FrontendAction() : Instance(0) {} |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 82 | |
| 83 | FrontendAction::~FrontendAction() {} |
| 84 | |
Daniel Dunbar | 685ac66 | 2010-06-07 23:25:49 +0000 | [diff] [blame] | 85 | void FrontendAction::setCurrentFile(llvm::StringRef Value, InputKind Kind, |
| 86 | ASTUnit *AST) { |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 87 | CurrentFile = Value; |
Daniel Dunbar | 685ac66 | 2010-06-07 23:25:49 +0000 | [diff] [blame] | 88 | CurrentFileKind = Kind; |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 89 | CurrentASTUnit.reset(AST); |
| 90 | } |
| 91 | |
Nico Weber | 5aa74af | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 92 | ASTConsumer* FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI, |
| 93 | llvm::StringRef InFile) { |
| 94 | ASTConsumer* Consumer = CreateASTConsumer(CI, InFile); |
| 95 | if (!Consumer) |
| 96 | return 0; |
| 97 | |
| 98 | if (CI.getFrontendOpts().AddPluginActions.size() == 0) |
| 99 | return Consumer; |
| 100 | |
| 101 | // Make sure the non-plugin consumer is first, so that plugins can't |
| 102 | // modifiy the AST. |
| 103 | std::vector<ASTConsumer*> Consumers(1, Consumer); |
| 104 | |
| 105 | for (size_t i = 0, e = CI.getFrontendOpts().AddPluginActions.size(); |
| 106 | i != e; ++i) { |
| 107 | // This is O(|plugins| * |add_plugins|), but since both numbers are |
| 108 | // way below 50 in practice, that's ok. |
| 109 | for (FrontendPluginRegistry::iterator |
| 110 | it = FrontendPluginRegistry::begin(), |
| 111 | ie = FrontendPluginRegistry::end(); |
| 112 | it != ie; ++it) { |
| 113 | if (it->getName() == CI.getFrontendOpts().AddPluginActions[i]) { |
| 114 | llvm::OwningPtr<PluginASTAction> P(it->instantiate()); |
| 115 | FrontendAction* c = P.get(); |
Nico Weber | f25649c | 2011-01-29 21:21:49 +0000 | [diff] [blame] | 116 | if (P->ParseArgs(CI, CI.getFrontendOpts().AddPluginArgs[i])) |
Nico Weber | 5aa74af | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 117 | Consumers.push_back(c->CreateASTConsumer(CI, InFile)); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return new MultiplexConsumer(Consumers); |
| 123 | } |
| 124 | |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 125 | bool FrontendAction::BeginSourceFile(CompilerInstance &CI, |
| 126 | llvm::StringRef Filename, |
Daniel Dunbar | d3598a6 | 2010-06-07 23:23:06 +0000 | [diff] [blame] | 127 | InputKind InputKind) { |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 128 | assert(!Instance && "Already processing a source file!"); |
| 129 | assert(!Filename.empty() && "Unexpected empty filename!"); |
Daniel Dunbar | 685ac66 | 2010-06-07 23:25:49 +0000 | [diff] [blame] | 130 | setCurrentFile(Filename, InputKind); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 131 | setCompilerInstance(&CI); |
| 132 | |
| 133 | // AST files follow a very different path, since they share objects via the |
| 134 | // AST unit. |
Daniel Dunbar | 2056048 | 2010-06-07 23:23:50 +0000 | [diff] [blame] | 135 | if (InputKind == IK_AST) { |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 136 | assert(!usesPreprocessorOnly() && |
| 137 | "Attempt to pass AST file to preprocessor only action!"); |
Daniel Dunbar | eb58d83 | 2010-06-07 23:24:43 +0000 | [diff] [blame] | 138 | assert(hasASTFileSupport() && |
| 139 | "This action does not have AST file support!"); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 140 | |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 141 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags(&CI.getDiagnostics()); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 142 | std::string Error; |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 143 | ASTUnit *AST = ASTUnit::LoadFromASTFile(Filename, Diags, |
| 144 | CI.getFileSystemOpts()); |
Daniel Dunbar | 5262fda | 2009-12-03 01:45:44 +0000 | [diff] [blame] | 145 | if (!AST) |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 146 | goto failure; |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 147 | |
Daniel Dunbar | 685ac66 | 2010-06-07 23:25:49 +0000 | [diff] [blame] | 148 | setCurrentFile(Filename, InputKind, AST); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 149 | |
| 150 | // Set the shared objects, these are reset when we finish processing the |
| 151 | // file, otherwise the CompilerInstance will happily destroy them. |
| 152 | CI.setFileManager(&AST->getFileManager()); |
| 153 | CI.setSourceManager(&AST->getSourceManager()); |
| 154 | CI.setPreprocessor(&AST->getPreprocessor()); |
| 155 | CI.setASTContext(&AST->getASTContext()); |
| 156 | |
| 157 | // Initialize the action. |
| 158 | if (!BeginSourceFileAction(CI, Filename)) |
| 159 | goto failure; |
| 160 | |
| 161 | /// Create the AST consumer. |
Nico Weber | 5aa74af | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 162 | CI.setASTConsumer(CreateWrappedASTConsumer(CI, Filename)); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 163 | if (!CI.hasASTConsumer()) |
| 164 | goto failure; |
| 165 | |
| 166 | return true; |
| 167 | } |
| 168 | |
Daniel Dunbar | faddc3e | 2010-06-07 23:26:47 +0000 | [diff] [blame] | 169 | // Set up the file and source managers, if needed. |
Daniel Dunbar | 2056048 | 2010-06-07 23:23:50 +0000 | [diff] [blame] | 170 | if (!CI.hasFileManager()) |
| 171 | CI.createFileManager(); |
| 172 | if (!CI.hasSourceManager()) |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 173 | CI.createSourceManager(CI.getFileManager()); |
Daniel Dunbar | faddc3e | 2010-06-07 23:26:47 +0000 | [diff] [blame] | 174 | |
| 175 | // IR files bypass the rest of initialization. |
| 176 | if (InputKind == IK_LLVM_IR) { |
| 177 | assert(hasIRSupport() && |
| 178 | "This action does not have IR file support!"); |
| 179 | |
| 180 | // Inform the diagnostic client we are processing a source file. |
| 181 | CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), 0); |
| 182 | |
| 183 | // Initialize the action. |
| 184 | if (!BeginSourceFileAction(CI, Filename)) |
| 185 | goto failure; |
| 186 | |
| 187 | return true; |
| 188 | } |
| 189 | |
| 190 | // Set up the preprocessor. |
Daniel Dunbar | 2056048 | 2010-06-07 23:23:50 +0000 | [diff] [blame] | 191 | CI.createPreprocessor(); |
| 192 | |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 193 | // Inform the diagnostic client we are processing a source file. |
| 194 | CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), |
| 195 | &CI.getPreprocessor()); |
| 196 | |
| 197 | // Initialize the action. |
| 198 | if (!BeginSourceFileAction(CI, Filename)) |
| 199 | goto failure; |
| 200 | |
| 201 | /// Create the AST context and consumer unless this is a preprocessor only |
| 202 | /// action. |
| 203 | if (!usesPreprocessorOnly()) { |
| 204 | CI.createASTContext(); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 205 | |
Nico Weber | 5aa74af | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 206 | llvm::OwningPtr<ASTConsumer> Consumer( |
| 207 | CreateWrappedASTConsumer(CI, Filename)); |
Fariborz Jahanian | d305719 | 2010-10-29 19:49:13 +0000 | [diff] [blame] | 208 | if (!Consumer) |
| 209 | goto failure; |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 210 | |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 211 | CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener()); |
| 212 | |
Argyrios Kyrtzidis | b0f4b9a | 2011-03-09 17:21:42 +0000 | [diff] [blame] | 213 | if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) { |
| 214 | // Convert headers to PCH and chain them. |
| 215 | llvm::OwningPtr<ExternalASTSource> source; |
| 216 | source.reset(ChainedIncludesSource::create(CI)); |
| 217 | if (!source) |
| 218 | goto failure; |
| 219 | CI.getASTContext().setExternalSource(source); |
| 220 | |
| 221 | } else if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) { |
| 222 | // Use PCH. |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 223 | assert(hasPCHSupport() && "This action does not have PCH support!"); |
Argyrios Kyrtzidis | b972858 | 2010-10-14 20:14:18 +0000 | [diff] [blame] | 224 | ASTDeserializationListener *DeserialListener |
| 225 | = CI.getInvocation().getFrontendOpts().ChainedPCH ? |
| 226 | Consumer->GetASTDeserializationListener() : 0; |
| 227 | if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls) |
| 228 | DeserialListener = new DeserializedDeclsDumper(DeserialListener); |
Argyrios Kyrtzidis | 3e78593 | 2010-10-14 20:14:25 +0000 | [diff] [blame] | 229 | if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty()) |
| 230 | DeserialListener = new DeserializedDeclsChecker(CI.getASTContext(), |
| 231 | CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn, |
| 232 | DeserialListener); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 233 | CI.createPCHExternalASTSource( |
Douglas Gregor | fae3b2f | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 234 | CI.getPreprocessorOpts().ImplicitPCHInclude, |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 235 | CI.getPreprocessorOpts().DisablePCHValidation, |
Douglas Gregor | 8ef6c8c | 2011-02-05 19:42:43 +0000 | [diff] [blame] | 236 | CI.getPreprocessorOpts().DisableStatCache, |
Argyrios Kyrtzidis | b972858 | 2010-10-14 20:14:18 +0000 | [diff] [blame] | 237 | DeserialListener); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 238 | if (!CI.getASTContext().getExternalSource()) |
| 239 | goto failure; |
| 240 | } |
Sebastian Redl | 77f4603 | 2010-07-09 21:00:24 +0000 | [diff] [blame] | 241 | |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 242 | CI.setASTConsumer(Consumer.take()); |
Sebastian Redl | 77f4603 | 2010-07-09 21:00:24 +0000 | [diff] [blame] | 243 | if (!CI.hasASTConsumer()) |
| 244 | goto failure; |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | // Initialize builtin info as long as we aren't using an external AST |
| 248 | // source. |
| 249 | if (!CI.hasASTContext() || !CI.getASTContext().getExternalSource()) { |
| 250 | Preprocessor &PP = CI.getPreprocessor(); |
| 251 | PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(), |
Fariborz Jahanian | 67aba81 | 2010-11-30 17:35:24 +0000 | [diff] [blame] | 252 | PP.getLangOptions()); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | return true; |
| 256 | |
| 257 | // If we failed, reset state since the client will not end up calling the |
| 258 | // matching EndSourceFile(). |
| 259 | failure: |
| 260 | if (isCurrentFileAST()) { |
Ted Kremenek | 4f32786 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 261 | CI.setASTContext(0); |
| 262 | CI.setPreprocessor(0); |
| 263 | CI.setSourceManager(0); |
| 264 | CI.setFileManager(0); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | CI.getDiagnosticClient().EndSourceFile(); |
Daniel Dunbar | 685ac66 | 2010-06-07 23:25:49 +0000 | [diff] [blame] | 268 | setCurrentFile("", IK_None); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 269 | setCompilerInstance(0); |
| 270 | return false; |
| 271 | } |
| 272 | |
| 273 | void FrontendAction::Execute() { |
| 274 | CompilerInstance &CI = getCompilerInstance(); |
| 275 | |
| 276 | // Initialize the main file entry. This needs to be delayed until after PCH |
| 277 | // has loaded. |
| 278 | if (isCurrentFileAST()) { |
| 279 | // Set the main file ID to an empty file. |
| 280 | // |
| 281 | // FIXME: We probably shouldn't need this, but for now this is the |
| 282 | // simplest way to reuse the logic in ParseAST. |
| 283 | const char *EmptyStr = ""; |
| 284 | llvm::MemoryBuffer *SB = |
Chris Lattner | a0a270c | 2010-04-05 22:42:27 +0000 | [diff] [blame] | 285 | llvm::MemoryBuffer::getMemBuffer(EmptyStr, "<dummy input>"); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 286 | CI.getSourceManager().createMainFileIDForMemBuffer(SB); |
| 287 | } else { |
| 288 | if (!CI.InitializeSourceManager(getCurrentFile())) |
| 289 | return; |
| 290 | } |
| 291 | |
Kovarththanan Rajaratnam | f79bafa | 2009-11-29 09:57:35 +0000 | [diff] [blame] | 292 | if (CI.hasFrontendTimer()) { |
| 293 | llvm::TimeRegion Timer(CI.getFrontendTimer()); |
| 294 | ExecuteAction(); |
| 295 | } |
| 296 | else ExecuteAction(); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | void FrontendAction::EndSourceFile() { |
| 300 | CompilerInstance &CI = getCompilerInstance(); |
| 301 | |
Douglas Gregor | 92b97f2 | 2011-02-09 18:47:31 +0000 | [diff] [blame] | 302 | // Inform the diagnostic client we are done with this source file. |
| 303 | CI.getDiagnosticClient().EndSourceFile(); |
| 304 | |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 305 | // Finalize the action. |
| 306 | EndSourceFileAction(); |
| 307 | |
| 308 | // Release the consumer and the AST, in that order since the consumer may |
| 309 | // perform actions in its destructor which require the context. |
| 310 | // |
| 311 | // FIXME: There is more per-file stuff we could just drop here? |
| 312 | if (CI.getFrontendOpts().DisableFree) { |
| 313 | CI.takeASTConsumer(); |
Douglas Gregor | f18d0d8 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 314 | if (!isCurrentFileAST()) { |
| 315 | CI.takeSema(); |
Ted Kremenek | 4f32786 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 316 | CI.resetAndLeakASTContext(); |
Douglas Gregor | f18d0d8 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 317 | } |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 318 | } else { |
Douglas Gregor | f18d0d8 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 319 | if (!isCurrentFileAST()) { |
| 320 | CI.setSema(0); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 321 | CI.setASTContext(0); |
Douglas Gregor | f18d0d8 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 322 | } |
| 323 | CI.setASTConsumer(0); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Daniel Dunbar | dbd8209 | 2010-03-23 05:09:10 +0000 | [diff] [blame] | 326 | // Inform the preprocessor we are done. |
| 327 | if (CI.hasPreprocessor()) |
| 328 | CI.getPreprocessor().EndSourceFile(); |
| 329 | |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 330 | if (CI.getFrontendOpts().ShowStats) { |
| 331 | llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n"; |
| 332 | CI.getPreprocessor().PrintStats(); |
| 333 | CI.getPreprocessor().getIdentifierTable().PrintStats(); |
| 334 | CI.getPreprocessor().getHeaderSearchInfo().PrintStats(); |
| 335 | CI.getSourceManager().PrintStats(); |
| 336 | llvm::errs() << "\n"; |
| 337 | } |
| 338 | |
| 339 | // Cleanup the output streams, and erase the output files if we encountered |
| 340 | // an error. |
Argyrios Kyrtzidis | be3aab6 | 2010-11-18 21:47:07 +0000 | [diff] [blame] | 341 | CI.clearOutputFiles(/*EraseFiles=*/CI.getDiagnostics().hasErrorOccurred()); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 342 | |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 343 | if (isCurrentFileAST()) { |
Douglas Gregor | f18d0d8 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 344 | CI.takeSema(); |
Ted Kremenek | 4f32786 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 345 | CI.resetAndLeakASTContext(); |
| 346 | CI.resetAndLeakPreprocessor(); |
| 347 | CI.resetAndLeakSourceManager(); |
| 348 | CI.resetAndLeakFileManager(); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | setCompilerInstance(0); |
Daniel Dunbar | 685ac66 | 2010-06-07 23:25:49 +0000 | [diff] [blame] | 352 | setCurrentFile("", IK_None); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | //===----------------------------------------------------------------------===// |
| 356 | // Utility Actions |
| 357 | //===----------------------------------------------------------------------===// |
| 358 | |
| 359 | void ASTFrontendAction::ExecuteAction() { |
| 360 | CompilerInstance &CI = getCompilerInstance(); |
| 361 | |
| 362 | // FIXME: Move the truncation aspect of this into Sema, we delayed this till |
| 363 | // here so the source manager would be initialized. |
| 364 | if (hasCodeCompletionSupport() && |
| 365 | !CI.getFrontendOpts().CodeCompletionAt.FileName.empty()) |
| 366 | CI.createCodeCompletionConsumer(); |
| 367 | |
| 368 | // Use a code completion consumer? |
| 369 | CodeCompleteConsumer *CompletionConsumer = 0; |
| 370 | if (CI.hasCodeCompletionConsumer()) |
| 371 | CompletionConsumer = &CI.getCodeCompletionConsumer(); |
| 372 | |
Douglas Gregor | f18d0d8 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 373 | if (!CI.hasSema()) |
| 374 | CI.createSema(usesCompleteTranslationUnit(), CompletionConsumer); |
| 375 | |
| 376 | ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | ASTConsumer * |
| 380 | PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI, |
| 381 | llvm::StringRef InFile) { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 382 | llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!"); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 383 | } |