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