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