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