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