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