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" |
Taewook Oh | 06b1af5 | 2017-03-07 20:20:23 +0000 | [diff] [blame] | 22 | #include "clang/Lex/LiteralSupport.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "clang/Lex/Preprocessor.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 24 | #include "clang/Lex/PreprocessorOptions.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 25 | #include "clang/Parse/ParseAST.h" |
Argyrios Kyrtzidis | a11aca4 | 2010-10-14 20:14:18 +0000 | [diff] [blame] | 26 | #include "clang/Serialization/ASTDeserializationListener.h" |
Jonathan D. Turner | 0248f57 | 2011-08-05 22:17:03 +0000 | [diff] [blame] | 27 | #include "clang/Serialization/ASTReader.h" |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 28 | #include "clang/Serialization/GlobalModuleIndex.h" |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 29 | #include "llvm/Support/ErrorHandling.h" |
Douglas Gregor | fc9e7a2 | 2012-10-23 06:18:24 +0000 | [diff] [blame] | 30 | #include "llvm/Support/FileSystem.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Path.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Timer.h" |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 33 | #include "llvm/Support/raw_ostream.h" |
Rafael Espindola | 8a8e554 | 2014-06-12 17:19:42 +0000 | [diff] [blame] | 34 | #include <system_error> |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 35 | using namespace clang; |
| 36 | |
John Brawn | 4d79ec7 | 2016-08-05 11:01:08 +0000 | [diff] [blame] | 37 | LLVM_INSTANTIATE_REGISTRY(FrontendPluginRegistry) |
NAKAMURA Takumi | ad4c06c | 2014-07-11 15:06:24 +0000 | [diff] [blame] | 38 | |
Argyrios Kyrtzidis | a11aca4 | 2010-10-14 20:14:18 +0000 | [diff] [blame] | 39 | namespace { |
| 40 | |
Argyrios Kyrtzidis | b12986f | 2011-10-28 22:54:31 +0000 | [diff] [blame] | 41 | class DelegatingDeserializationListener : public ASTDeserializationListener { |
Argyrios Kyrtzidis | a11aca4 | 2010-10-14 20:14:18 +0000 | [diff] [blame] | 42 | ASTDeserializationListener *Previous; |
Nico Weber | 824285e | 2014-05-08 04:26:47 +0000 | [diff] [blame] | 43 | bool DeletePrevious; |
Argyrios Kyrtzidis | a11aca4 | 2010-10-14 20:14:18 +0000 | [diff] [blame] | 44 | |
| 45 | public: |
Argyrios Kyrtzidis | b12986f | 2011-10-28 22:54:31 +0000 | [diff] [blame] | 46 | explicit DelegatingDeserializationListener( |
Nico Weber | 824285e | 2014-05-08 04:26:47 +0000 | [diff] [blame] | 47 | ASTDeserializationListener *Previous, bool DeletePrevious) |
| 48 | : Previous(Previous), DeletePrevious(DeletePrevious) {} |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 49 | ~DelegatingDeserializationListener() override { |
Nico Weber | 824285e | 2014-05-08 04:26:47 +0000 | [diff] [blame] | 50 | if (DeletePrevious) |
| 51 | delete Previous; |
| 52 | } |
Argyrios Kyrtzidis | a11aca4 | 2010-10-14 20:14:18 +0000 | [diff] [blame] | 53 | |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 54 | void ReaderInitialized(ASTReader *Reader) override { |
Argyrios Kyrtzidis | b12986f | 2011-10-28 22:54:31 +0000 | [diff] [blame] | 55 | if (Previous) |
| 56 | Previous->ReaderInitialized(Reader); |
| 57 | } |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 58 | void IdentifierRead(serialization::IdentID ID, |
| 59 | IdentifierInfo *II) override { |
Argyrios Kyrtzidis | b12986f | 2011-10-28 22:54:31 +0000 | [diff] [blame] | 60 | if (Previous) |
| 61 | Previous->IdentifierRead(ID, II); |
| 62 | } |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 63 | void TypeRead(serialization::TypeIdx Idx, QualType T) override { |
Argyrios Kyrtzidis | b12986f | 2011-10-28 22:54:31 +0000 | [diff] [blame] | 64 | if (Previous) |
| 65 | Previous->TypeRead(Idx, T); |
| 66 | } |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 67 | void DeclRead(serialization::DeclID ID, const Decl *D) override { |
Argyrios Kyrtzidis | b12986f | 2011-10-28 22:54:31 +0000 | [diff] [blame] | 68 | if (Previous) |
| 69 | Previous->DeclRead(ID, D); |
| 70 | } |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 71 | void SelectorRead(serialization::SelectorID ID, Selector Sel) override { |
Argyrios Kyrtzidis | b12986f | 2011-10-28 22:54:31 +0000 | [diff] [blame] | 72 | if (Previous) |
| 73 | Previous->SelectorRead(ID, Sel); |
| 74 | } |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 75 | void MacroDefinitionRead(serialization::PreprocessedEntityID PPID, |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 76 | MacroDefinitionRecord *MD) override { |
Argyrios Kyrtzidis | b12986f | 2011-10-28 22:54:31 +0000 | [diff] [blame] | 77 | if (Previous) |
| 78 | Previous->MacroDefinitionRead(PPID, MD); |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | /// \brief Dumps deserialized declarations. |
| 83 | class DeserializedDeclsDumper : public DelegatingDeserializationListener { |
| 84 | public: |
Nico Weber | 824285e | 2014-05-08 04:26:47 +0000 | [diff] [blame] | 85 | explicit DeserializedDeclsDumper(ASTDeserializationListener *Previous, |
| 86 | bool DeletePrevious) |
| 87 | : DelegatingDeserializationListener(Previous, DeletePrevious) {} |
Argyrios Kyrtzidis | b12986f | 2011-10-28 22:54:31 +0000 | [diff] [blame] | 88 | |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 89 | void DeclRead(serialization::DeclID ID, const Decl *D) override { |
Argyrios Kyrtzidis | a11aca4 | 2010-10-14 20:14:18 +0000 | [diff] [blame] | 90 | llvm::outs() << "PCH DECL: " << D->getDeclKindName(); |
| 91 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
Benjamin Kramer | db0fc51 | 2012-02-07 11:57:57 +0000 | [diff] [blame] | 92 | llvm::outs() << " - " << *ND; |
Argyrios Kyrtzidis | a11aca4 | 2010-10-14 20:14:18 +0000 | [diff] [blame] | 93 | llvm::outs() << "\n"; |
| 94 | |
Argyrios Kyrtzidis | b12986f | 2011-10-28 22:54:31 +0000 | [diff] [blame] | 95 | DelegatingDeserializationListener::DeclRead(ID, D); |
Argyrios Kyrtzidis | a11aca4 | 2010-10-14 20:14:18 +0000 | [diff] [blame] | 96 | } |
Argyrios Kyrtzidis | a11aca4 | 2010-10-14 20:14:18 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
David Blaikie | 48b8128 | 2012-05-29 17:05:42 +0000 | [diff] [blame] | 99 | /// \brief Checks deserialized declarations and emits error if a name |
| 100 | /// matches one given in command-line using -error-on-deserialized-decl. |
| 101 | class DeserializedDeclsChecker : public DelegatingDeserializationListener { |
| 102 | ASTContext &Ctx; |
| 103 | std::set<std::string> NamesToCheck; |
Argyrios Kyrtzidis | 0427be9 | 2010-10-14 20:14:25 +0000 | [diff] [blame] | 104 | |
David Blaikie | 48b8128 | 2012-05-29 17:05:42 +0000 | [diff] [blame] | 105 | public: |
| 106 | DeserializedDeclsChecker(ASTContext &Ctx, |
| 107 | const std::set<std::string> &NamesToCheck, |
Nico Weber | 824285e | 2014-05-08 04:26:47 +0000 | [diff] [blame] | 108 | ASTDeserializationListener *Previous, |
| 109 | bool DeletePrevious) |
| 110 | : DelegatingDeserializationListener(Previous, DeletePrevious), Ctx(Ctx), |
| 111 | NamesToCheck(NamesToCheck) {} |
Argyrios Kyrtzidis | 0427be9 | 2010-10-14 20:14:25 +0000 | [diff] [blame] | 112 | |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 113 | void DeclRead(serialization::DeclID ID, const Decl *D) override { |
David Blaikie | 48b8128 | 2012-05-29 17:05:42 +0000 | [diff] [blame] | 114 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
| 115 | if (NamesToCheck.find(ND->getNameAsString()) != NamesToCheck.end()) { |
| 116 | unsigned DiagID |
| 117 | = Ctx.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error, |
| 118 | "%0 was deserialized"); |
| 119 | Ctx.getDiagnostics().Report(Ctx.getFullLoc(D->getLocation()), DiagID) |
| 120 | << ND->getNameAsString(); |
| 121 | } |
Argyrios Kyrtzidis | 0427be9 | 2010-10-14 20:14:25 +0000 | [diff] [blame] | 122 | |
David Blaikie | 48b8128 | 2012-05-29 17:05:42 +0000 | [diff] [blame] | 123 | DelegatingDeserializationListener::DeclRead(ID, D); |
| 124 | } |
Argyrios Kyrtzidis | 0427be9 | 2010-10-14 20:14:25 +0000 | [diff] [blame] | 125 | }; |
| 126 | |
Argyrios Kyrtzidis | a11aca4 | 2010-10-14 20:14:18 +0000 | [diff] [blame] | 127 | } // end anonymous namespace |
| 128 | |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 129 | FrontendAction::FrontendAction() : Instance(nullptr) {} |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 130 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 131 | FrontendAction::~FrontendAction() {} |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 132 | |
Douglas Gregor | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 133 | void FrontendAction::setCurrentInput(const FrontendInputFile &CurrentInput, |
David Blaikie | f62d4e7 | 2014-08-10 17:03:42 +0000 | [diff] [blame] | 134 | std::unique_ptr<ASTUnit> AST) { |
Douglas Gregor | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 135 | this->CurrentInput = CurrentInput; |
David Blaikie | f62d4e7 | 2014-08-10 17:03:42 +0000 | [diff] [blame] | 136 | CurrentASTUnit = std::move(AST); |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Richard Smith | 8128f33 | 2017-05-05 22:18:51 +0000 | [diff] [blame] | 139 | Module *FrontendAction::getCurrentModule() const { |
| 140 | CompilerInstance &CI = getCompilerInstance(); |
| 141 | return CI.getPreprocessor().getHeaderSearchInfo().lookupModule( |
| 142 | CI.getLangOpts().CurrentModule, /*AllowSearch*/false); |
| 143 | } |
| 144 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 145 | std::unique_ptr<ASTConsumer> |
| 146 | FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI, |
| 147 | StringRef InFile) { |
| 148 | std::unique_ptr<ASTConsumer> Consumer = CreateASTConsumer(CI, InFile); |
Nico Weber | 2992efa | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 149 | if (!Consumer) |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 150 | return nullptr; |
Nico Weber | 2992efa | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 151 | |
John Brawn | 6c78974 | 2016-03-15 12:51:40 +0000 | [diff] [blame] | 152 | // If there are no registered plugins we don't need to wrap the consumer |
| 153 | if (FrontendPluginRegistry::begin() == FrontendPluginRegistry::end()) |
Nico Weber | 2992efa | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 154 | return Consumer; |
| 155 | |
John Brawn | 6c78974 | 2016-03-15 12:51:40 +0000 | [diff] [blame] | 156 | // Collect the list of plugins that go before the main action (in Consumers) |
| 157 | // or after it (in AfterConsumers) |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 158 | std::vector<std::unique_ptr<ASTConsumer>> Consumers; |
John Brawn | 6c78974 | 2016-03-15 12:51:40 +0000 | [diff] [blame] | 159 | std::vector<std::unique_ptr<ASTConsumer>> AfterConsumers; |
| 160 | for (FrontendPluginRegistry::iterator it = FrontendPluginRegistry::begin(), |
| 161 | ie = FrontendPluginRegistry::end(); |
| 162 | it != ie; ++it) { |
| 163 | std::unique_ptr<PluginASTAction> P = it->instantiate(); |
| 164 | PluginASTAction::ActionType ActionType = P->getActionType(); |
| 165 | if (ActionType == PluginASTAction::Cmdline) { |
| 166 | // This is O(|plugins| * |add_plugins|), but since both numbers are |
| 167 | // way below 50 in practice, that's ok. |
| 168 | for (size_t i = 0, e = CI.getFrontendOpts().AddPluginActions.size(); |
| 169 | i != e; ++i) { |
| 170 | if (it->getName() == CI.getFrontendOpts().AddPluginActions[i]) { |
| 171 | ActionType = PluginASTAction::AddAfterMainAction; |
| 172 | break; |
| 173 | } |
| 174 | } |
Nico Weber | 2992efa | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 175 | } |
John Brawn | 6c78974 | 2016-03-15 12:51:40 +0000 | [diff] [blame] | 176 | if ((ActionType == PluginASTAction::AddBeforeMainAction || |
| 177 | ActionType == PluginASTAction::AddAfterMainAction) && |
| 178 | P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs[it->getName()])) { |
| 179 | std::unique_ptr<ASTConsumer> PluginConsumer = P->CreateASTConsumer(CI, InFile); |
| 180 | if (ActionType == PluginASTAction::AddBeforeMainAction) { |
| 181 | Consumers.push_back(std::move(PluginConsumer)); |
| 182 | } else { |
| 183 | AfterConsumers.push_back(std::move(PluginConsumer)); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | // Add to Consumers the main consumer, then all the plugins that go after it |
| 189 | Consumers.push_back(std::move(Consumer)); |
| 190 | for (auto &C : AfterConsumers) { |
| 191 | Consumers.push_back(std::move(C)); |
Nico Weber | 2992efa | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 192 | } |
| 193 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 194 | return llvm::make_unique<MultiplexConsumer>(std::move(Consumers)); |
Nico Weber | 2992efa | 2011-01-25 20:34:14 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Richard Smith | 8128f33 | 2017-05-05 22:18:51 +0000 | [diff] [blame] | 197 | /// For preprocessed files, if the first line is the linemarker and specifies |
| 198 | /// the original source file name, use that name as the input file name. |
| 199 | /// Returns the location of the first token after the line marker directive. |
| 200 | /// |
| 201 | /// \param CI The compiler instance. |
| 202 | /// \param InputFile Populated with the filename from the line marker. |
| 203 | /// \param AddLineNote If \c true, add a line note corresponding to this line |
| 204 | /// directive. Only use this if the directive will not actually be |
| 205 | /// visited by the preprocessor. |
| 206 | static SourceLocation ReadOriginalFileName(CompilerInstance &CI, |
| 207 | std::string &InputFile, |
| 208 | bool AddLineNote = false) { |
Taewook Oh | 06b1af5 | 2017-03-07 20:20:23 +0000 | [diff] [blame] | 209 | auto &SourceMgr = CI.getSourceManager(); |
| 210 | auto MainFileID = SourceMgr.getMainFileID(); |
Richard Smith | 8128f33 | 2017-05-05 22:18:51 +0000 | [diff] [blame] | 211 | |
| 212 | bool Invalid = false; |
Taewook Oh | 06b1af5 | 2017-03-07 20:20:23 +0000 | [diff] [blame] | 213 | const auto *MainFileBuf = SourceMgr.getBuffer(MainFileID, &Invalid); |
| 214 | if (Invalid) |
Richard Smith | 8128f33 | 2017-05-05 22:18:51 +0000 | [diff] [blame] | 215 | return SourceLocation(); |
Taewook Oh | 06b1af5 | 2017-03-07 20:20:23 +0000 | [diff] [blame] | 216 | |
| 217 | std::unique_ptr<Lexer> RawLexer( |
| 218 | new Lexer(MainFileID, MainFileBuf, SourceMgr, CI.getLangOpts())); |
| 219 | |
| 220 | // If the first line has the syntax of |
| 221 | // |
| 222 | // # NUM "FILENAME" |
| 223 | // |
| 224 | // we use FILENAME as the input file name. |
| 225 | Token T; |
| 226 | if (RawLexer->LexFromRawLexer(T) || T.getKind() != tok::hash) |
Richard Smith | 8128f33 | 2017-05-05 22:18:51 +0000 | [diff] [blame] | 227 | return SourceLocation(); |
Taewook Oh | 06b1af5 | 2017-03-07 20:20:23 +0000 | [diff] [blame] | 228 | if (RawLexer->LexFromRawLexer(T) || T.isAtStartOfLine() || |
| 229 | T.getKind() != tok::numeric_constant) |
Richard Smith | 8128f33 | 2017-05-05 22:18:51 +0000 | [diff] [blame] | 230 | return SourceLocation(); |
| 231 | |
| 232 | unsigned LineNo; |
| 233 | SourceLocation LineNoLoc = T.getLocation(); |
| 234 | if (AddLineNote) { |
| 235 | llvm::SmallString<16> Buffer; |
| 236 | if (Lexer::getSpelling(LineNoLoc, Buffer, SourceMgr, CI.getLangOpts()) |
| 237 | .getAsInteger(10, LineNo)) |
| 238 | return SourceLocation(); |
| 239 | } |
| 240 | |
Taewook Oh | 06b1af5 | 2017-03-07 20:20:23 +0000 | [diff] [blame] | 241 | RawLexer->LexFromRawLexer(T); |
| 242 | if (T.isAtStartOfLine() || T.getKind() != tok::string_literal) |
Richard Smith | 8128f33 | 2017-05-05 22:18:51 +0000 | [diff] [blame] | 243 | return SourceLocation(); |
Taewook Oh | 06b1af5 | 2017-03-07 20:20:23 +0000 | [diff] [blame] | 244 | |
| 245 | StringLiteralParser Literal(T, CI.getPreprocessor()); |
| 246 | if (Literal.hadError) |
Richard Smith | 8128f33 | 2017-05-05 22:18:51 +0000 | [diff] [blame] | 247 | return SourceLocation(); |
| 248 | RawLexer->LexFromRawLexer(T); |
| 249 | if (T.isNot(tok::eof) && !T.isAtStartOfLine()) |
| 250 | return SourceLocation(); |
Taewook Oh | 06b1af5 | 2017-03-07 20:20:23 +0000 | [diff] [blame] | 251 | InputFile = Literal.GetString().str(); |
Richard Smith | 8128f33 | 2017-05-05 22:18:51 +0000 | [diff] [blame] | 252 | |
| 253 | if (AddLineNote) |
| 254 | CI.getSourceManager().AddLineNote( |
Reid Kleckner | eb00ee0 | 2017-05-22 21:42:58 +0000 | [diff] [blame] | 255 | LineNoLoc, LineNo, SourceMgr.getLineTableFilenameID(InputFile), false, |
| 256 | false, SrcMgr::C_User); |
Richard Smith | 8128f33 | 2017-05-05 22:18:51 +0000 | [diff] [blame] | 257 | |
| 258 | return T.getLocation(); |
Taewook Oh | 06b1af5 | 2017-03-07 20:20:23 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Richard Smith | f74d946 | 2017-04-28 01:49:42 +0000 | [diff] [blame] | 261 | static SmallVectorImpl<char> & |
| 262 | operator+=(SmallVectorImpl<char> &Includes, StringRef RHS) { |
| 263 | Includes.append(RHS.begin(), RHS.end()); |
| 264 | return Includes; |
| 265 | } |
| 266 | |
| 267 | static void addHeaderInclude(StringRef HeaderName, |
| 268 | SmallVectorImpl<char> &Includes, |
| 269 | const LangOptions &LangOpts, |
| 270 | bool IsExternC) { |
| 271 | if (IsExternC && LangOpts.CPlusPlus) |
| 272 | Includes += "extern \"C\" {\n"; |
| 273 | if (LangOpts.ObjC1) |
| 274 | Includes += "#import \""; |
| 275 | else |
| 276 | Includes += "#include \""; |
| 277 | |
| 278 | Includes += HeaderName; |
| 279 | |
| 280 | Includes += "\"\n"; |
| 281 | if (IsExternC && LangOpts.CPlusPlus) |
| 282 | Includes += "}\n"; |
| 283 | } |
| 284 | |
| 285 | /// \brief Collect the set of header includes needed to construct the given |
| 286 | /// module and update the TopHeaders file set of the module. |
| 287 | /// |
| 288 | /// \param Module The module we're collecting includes from. |
| 289 | /// |
| 290 | /// \param Includes Will be augmented with the set of \#includes or \#imports |
| 291 | /// needed to load all of the named headers. |
| 292 | static std::error_code |
| 293 | collectModuleHeaderIncludes(const LangOptions &LangOpts, FileManager &FileMgr, |
| 294 | ModuleMap &ModMap, clang::Module *Module, |
| 295 | SmallVectorImpl<char> &Includes) { |
| 296 | // Don't collect any headers for unavailable modules. |
| 297 | if (!Module->isAvailable()) |
| 298 | return std::error_code(); |
| 299 | |
| 300 | // Add includes for each of these headers. |
| 301 | for (auto HK : {Module::HK_Normal, Module::HK_Private}) { |
| 302 | for (Module::Header &H : Module->Headers[HK]) { |
| 303 | Module->addTopHeader(H.Entry); |
| 304 | // Use the path as specified in the module map file. We'll look for this |
| 305 | // file relative to the module build directory (the directory containing |
| 306 | // the module map file) so this will find the same file that we found |
| 307 | // while parsing the module map. |
| 308 | addHeaderInclude(H.NameAsWritten, Includes, LangOpts, Module->IsExternC); |
| 309 | } |
| 310 | } |
| 311 | // Note that Module->PrivateHeaders will not be a TopHeader. |
| 312 | |
| 313 | if (Module::Header UmbrellaHeader = Module->getUmbrellaHeader()) { |
| 314 | Module->addTopHeader(UmbrellaHeader.Entry); |
| 315 | if (Module->Parent) |
| 316 | // Include the umbrella header for submodules. |
| 317 | addHeaderInclude(UmbrellaHeader.NameAsWritten, Includes, LangOpts, |
| 318 | Module->IsExternC); |
| 319 | } else if (Module::DirectoryName UmbrellaDir = Module->getUmbrellaDir()) { |
| 320 | // Add all of the headers we find in this subdirectory. |
| 321 | std::error_code EC; |
| 322 | SmallString<128> DirNative; |
| 323 | llvm::sys::path::native(UmbrellaDir.Entry->getName(), DirNative); |
| 324 | |
| 325 | vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem(); |
| 326 | for (vfs::recursive_directory_iterator Dir(FS, DirNative, EC), End; |
| 327 | Dir != End && !EC; Dir.increment(EC)) { |
| 328 | // Check whether this entry has an extension typically associated with |
| 329 | // headers. |
| 330 | if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->getName())) |
| 331 | .Cases(".h", ".H", ".hh", ".hpp", true) |
| 332 | .Default(false)) |
| 333 | continue; |
| 334 | |
| 335 | const FileEntry *Header = FileMgr.getFile(Dir->getName()); |
| 336 | // FIXME: This shouldn't happen unless there is a file system race. Is |
| 337 | // that worth diagnosing? |
| 338 | if (!Header) |
| 339 | continue; |
| 340 | |
| 341 | // If this header is marked 'unavailable' in this module, don't include |
| 342 | // it. |
| 343 | if (ModMap.isHeaderUnavailableInModule(Header, Module)) |
| 344 | continue; |
| 345 | |
| 346 | // Compute the relative path from the directory to this file. |
| 347 | SmallVector<StringRef, 16> Components; |
| 348 | auto PathIt = llvm::sys::path::rbegin(Dir->getName()); |
| 349 | for (int I = 0; I != Dir.level() + 1; ++I, ++PathIt) |
| 350 | Components.push_back(*PathIt); |
| 351 | SmallString<128> RelativeHeader(UmbrellaDir.NameAsWritten); |
| 352 | for (auto It = Components.rbegin(), End = Components.rend(); It != End; |
| 353 | ++It) |
| 354 | llvm::sys::path::append(RelativeHeader, *It); |
| 355 | |
| 356 | // Include this header as part of the umbrella directory. |
| 357 | Module->addTopHeader(Header); |
| 358 | addHeaderInclude(RelativeHeader, Includes, LangOpts, Module->IsExternC); |
| 359 | } |
| 360 | |
| 361 | if (EC) |
| 362 | return EC; |
| 363 | } |
| 364 | |
| 365 | // Recurse into submodules. |
| 366 | for (clang::Module::submodule_iterator Sub = Module->submodule_begin(), |
| 367 | SubEnd = Module->submodule_end(); |
| 368 | Sub != SubEnd; ++Sub) |
| 369 | if (std::error_code Err = collectModuleHeaderIncludes( |
| 370 | LangOpts, FileMgr, ModMap, *Sub, Includes)) |
| 371 | return Err; |
| 372 | |
| 373 | return std::error_code(); |
| 374 | } |
| 375 | |
Richard Smith | 8128f33 | 2017-05-05 22:18:51 +0000 | [diff] [blame] | 376 | static bool |
| 377 | loadModuleMapForModuleBuild(CompilerInstance &CI, StringRef Filename, |
| 378 | bool IsSystem, bool IsPreprocessed, |
| 379 | unsigned &Offset) { |
| 380 | auto &SrcMgr = CI.getSourceManager(); |
| 381 | HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo(); |
| 382 | |
| 383 | // Map the current input to a file. |
| 384 | FileID ModuleMapID = SrcMgr.getMainFileID(); |
| 385 | const FileEntry *ModuleMap = SrcMgr.getFileEntryForID(ModuleMapID); |
| 386 | |
| 387 | // If the module map is preprocessed, handle the initial line marker; |
| 388 | // line directives are not part of the module map syntax in general. |
| 389 | Offset = 0; |
| 390 | if (IsPreprocessed) { |
| 391 | std::string PresumedModuleMapFile; |
| 392 | SourceLocation EndOfLineMarker = |
| 393 | ReadOriginalFileName(CI, PresumedModuleMapFile, /*AddLineNote*/true); |
| 394 | if (EndOfLineMarker.isValid()) |
| 395 | Offset = CI.getSourceManager().getDecomposedLoc(EndOfLineMarker).second; |
| 396 | // FIXME: Use PresumedModuleMapFile as the MODULE_MAP_FILE in the PCM. |
Richard Smith | f74d946 | 2017-04-28 01:49:42 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Richard Smith | 8128f33 | 2017-05-05 22:18:51 +0000 | [diff] [blame] | 399 | // Load the module map file. |
| 400 | if (HS.loadModuleMapFile(ModuleMap, IsSystem, ModuleMapID, &Offset)) |
| 401 | return true; |
| 402 | |
| 403 | if (SrcMgr.getBuffer(ModuleMapID)->getBufferSize() == Offset) |
| 404 | Offset = 0; |
| 405 | |
| 406 | return false; |
| 407 | } |
| 408 | |
| 409 | static Module *prepareToBuildModule(CompilerInstance &CI, |
| 410 | StringRef ModuleMapFilename) { |
Richard Smith | f74d946 | 2017-04-28 01:49:42 +0000 | [diff] [blame] | 411 | if (CI.getLangOpts().CurrentModule.empty()) { |
| 412 | CI.getDiagnostics().Report(diag::err_missing_module_name); |
Richard Smith | 8128f33 | 2017-05-05 22:18:51 +0000 | [diff] [blame] | 413 | |
Richard Smith | f74d946 | 2017-04-28 01:49:42 +0000 | [diff] [blame] | 414 | // FIXME: Eventually, we could consider asking whether there was just |
| 415 | // a single module described in the module map, and use that as a |
| 416 | // default. Then it would be fairly trivial to just "compile" a module |
| 417 | // map with a single module (the common case). |
| 418 | return nullptr; |
| 419 | } |
| 420 | |
Richard Smith | f74d946 | 2017-04-28 01:49:42 +0000 | [diff] [blame] | 421 | // Dig out the module definition. |
Richard Smith | 8128f33 | 2017-05-05 22:18:51 +0000 | [diff] [blame] | 422 | HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo(); |
Richard Smith | f74d946 | 2017-04-28 01:49:42 +0000 | [diff] [blame] | 423 | Module *M = HS.lookupModule(CI.getLangOpts().CurrentModule, |
| 424 | /*AllowSearch=*/false); |
| 425 | if (!M) { |
| 426 | CI.getDiagnostics().Report(diag::err_missing_module) |
Richard Smith | 8128f33 | 2017-05-05 22:18:51 +0000 | [diff] [blame] | 427 | << CI.getLangOpts().CurrentModule << ModuleMapFilename; |
| 428 | |
Richard Smith | f74d946 | 2017-04-28 01:49:42 +0000 | [diff] [blame] | 429 | return nullptr; |
| 430 | } |
| 431 | |
| 432 | // Check whether we can build this module at all. |
| 433 | clang::Module::Requirement Requirement; |
| 434 | clang::Module::UnresolvedHeaderDirective MissingHeader; |
| 435 | if (!M->isAvailable(CI.getLangOpts(), CI.getTarget(), Requirement, |
| 436 | MissingHeader)) { |
| 437 | if (MissingHeader.FileNameLoc.isValid()) { |
| 438 | CI.getDiagnostics().Report(MissingHeader.FileNameLoc, |
| 439 | diag::err_module_header_missing) |
| 440 | << MissingHeader.IsUmbrella << MissingHeader.FileName; |
| 441 | } else { |
| 442 | CI.getDiagnostics().Report(diag::err_module_unavailable) |
| 443 | << M->getFullModuleName() << Requirement.second << Requirement.first; |
| 444 | } |
| 445 | |
| 446 | return nullptr; |
| 447 | } |
| 448 | |
Richard Smith | 8128f33 | 2017-05-05 22:18:51 +0000 | [diff] [blame] | 449 | // Inform the preprocessor that includes from within the input buffer should |
| 450 | // be resolved relative to the build directory of the module map file. |
| 451 | CI.getPreprocessor().setMainFileDir(M->Directory); |
| 452 | |
| 453 | // If the module was inferred from a different module map (via an expanded |
| 454 | // umbrella module definition), track that fact. |
| 455 | // FIXME: It would be preferable to fill this in as part of processing |
| 456 | // the module map, rather than adding it after the fact. |
| 457 | StringRef OriginalModuleMapName = CI.getFrontendOpts().OriginalModuleMap; |
| 458 | if (!OriginalModuleMapName.empty()) { |
| 459 | auto *OriginalModuleMap = |
| 460 | CI.getFileManager().getFile(OriginalModuleMapName, |
| 461 | /*openFile*/ true); |
| 462 | if (!OriginalModuleMap) { |
| 463 | CI.getDiagnostics().Report(diag::err_module_map_not_found) |
| 464 | << OriginalModuleMapName; |
| 465 | return nullptr; |
| 466 | } |
| 467 | if (OriginalModuleMap != CI.getSourceManager().getFileEntryForID( |
| 468 | CI.getSourceManager().getMainFileID())) { |
| 469 | M->IsInferred = true; |
| 470 | CI.getPreprocessor().getHeaderSearchInfo().getModuleMap() |
| 471 | .setInferredModuleAllowedBy(M, OriginalModuleMap); |
| 472 | } |
Richard Smith | f74d946 | 2017-04-28 01:49:42 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Richard Smith | 8128f33 | 2017-05-05 22:18:51 +0000 | [diff] [blame] | 475 | // If we're being run from the command-line, the module build stack will not |
| 476 | // have been filled in yet, so complete it now in order to allow us to detect |
| 477 | // module cycles. |
| 478 | SourceManager &SourceMgr = CI.getSourceManager(); |
| 479 | if (SourceMgr.getModuleBuildStack().empty()) |
| 480 | SourceMgr.pushModuleBuildStack(CI.getLangOpts().CurrentModule, |
| 481 | FullSourceLoc(SourceLocation(), SourceMgr)); |
| 482 | return M; |
| 483 | } |
| 484 | |
| 485 | /// Compute the input buffer that should be used to build the specified module. |
| 486 | static std::unique_ptr<llvm::MemoryBuffer> |
| 487 | getInputBufferForModule(CompilerInstance &CI, Module *M) { |
Richard Smith | f74d946 | 2017-04-28 01:49:42 +0000 | [diff] [blame] | 488 | FileManager &FileMgr = CI.getFileManager(); |
| 489 | |
| 490 | // Collect the set of #includes we need to build the module. |
| 491 | SmallString<256> HeaderContents; |
| 492 | std::error_code Err = std::error_code(); |
| 493 | if (Module::Header UmbrellaHeader = M->getUmbrellaHeader()) |
| 494 | addHeaderInclude(UmbrellaHeader.NameAsWritten, HeaderContents, |
| 495 | CI.getLangOpts(), M->IsExternC); |
| 496 | Err = collectModuleHeaderIncludes( |
| 497 | CI.getLangOpts(), FileMgr, |
| 498 | CI.getPreprocessor().getHeaderSearchInfo().getModuleMap(), M, |
| 499 | HeaderContents); |
| 500 | |
| 501 | if (Err) { |
| 502 | CI.getDiagnostics().Report(diag::err_module_cannot_create_includes) |
| 503 | << M->getFullModuleName() << Err.message(); |
| 504 | return nullptr; |
| 505 | } |
| 506 | |
Richard Smith | f74d946 | 2017-04-28 01:49:42 +0000 | [diff] [blame] | 507 | return llvm::MemoryBuffer::getMemBufferCopy( |
| 508 | HeaderContents, Module::getModuleInputBufferName()); |
| 509 | } |
| 510 | |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 511 | bool FrontendAction::BeginSourceFile(CompilerInstance &CI, |
Douglas Gregor | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 512 | const FrontendInputFile &Input) { |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 513 | assert(!Instance && "Already processing a source file!"); |
Argyrios Kyrtzidis | 873c858 | 2012-11-09 19:40:39 +0000 | [diff] [blame] | 514 | assert(!Input.isEmpty() && "Unexpected empty filename!"); |
Douglas Gregor | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 515 | setCurrentInput(Input); |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 516 | setCompilerInstance(&CI); |
| 517 | |
Argyrios Kyrtzidis | 873c858 | 2012-11-09 19:40:39 +0000 | [diff] [blame] | 518 | StringRef InputFile = Input.getFile(); |
Jordan Rose | ea762b0 | 2012-08-10 01:06:08 +0000 | [diff] [blame] | 519 | bool HasBegunSourceFile = false; |
Argyrios Kyrtzidis | 90b6a2a | 2011-06-18 00:53:41 +0000 | [diff] [blame] | 520 | if (!BeginInvocation(CI)) |
| 521 | goto failure; |
| 522 | |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 523 | // AST files follow a very different path, since they share objects via the |
| 524 | // AST unit. |
Richard Smith | 40c0efa | 2017-04-26 18:57:40 +0000 | [diff] [blame] | 525 | if (Input.getKind().getFormat() == InputKind::Precompiled) { |
| 526 | // FIXME: We should not be asserting on bad command-line arguments. |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 527 | assert(!usesPreprocessorOnly() && |
| 528 | "Attempt to pass AST file to preprocessor only action!"); |
Daniel Dunbar | fa6214c | 2010-06-07 23:24:43 +0000 | [diff] [blame] | 529 | assert(hasASTFileSupport() && |
| 530 | "This action does not have AST file support!"); |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 531 | |
Dylan Noblesmith | c95d819 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 532 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics()); |
Alp Toker | 965f882 | 2013-11-27 05:22:15 +0000 | [diff] [blame] | 533 | |
Adrian Prantl | 6b21ab2 | 2015-08-27 19:46:20 +0000 | [diff] [blame] | 534 | std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile( |
| 535 | InputFile, CI.getPCHContainerReader(), Diags, CI.getFileSystemOpts(), |
| 536 | CI.getCodeGenOpts().DebugTypeExtRefs); |
David Blaikie | f62d4e7 | 2014-08-10 17:03:42 +0000 | [diff] [blame] | 537 | |
Daniel Dunbar | 5920300 | 2009-12-03 01:45:44 +0000 | [diff] [blame] | 538 | if (!AST) |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 539 | goto failure; |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 540 | |
Argyrios Kyrtzidis | c00f43a | 2013-03-18 22:55:24 +0000 | [diff] [blame] | 541 | // Inform the diagnostic client we are processing a source file. |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 542 | CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr); |
Argyrios Kyrtzidis | c00f43a | 2013-03-18 22:55:24 +0000 | [diff] [blame] | 543 | HasBegunSourceFile = true; |
| 544 | |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 545 | // Set the shared objects, these are reset when we finish processing the |
| 546 | // file, otherwise the CompilerInstance will happily destroy them. |
| 547 | CI.setFileManager(&AST->getFileManager()); |
| 548 | CI.setSourceManager(&AST->getSourceManager()); |
David Blaikie | 4156546 | 2017-01-05 19:48:07 +0000 | [diff] [blame] | 549 | CI.setPreprocessor(AST->getPreprocessorPtr()); |
David Blaikie | ee12322 | 2017-02-08 20:51:11 +0000 | [diff] [blame] | 550 | Preprocessor &PP = CI.getPreprocessor(); |
| 551 | PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(), |
| 552 | PP.getLangOpts()); |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 553 | CI.setASTContext(&AST->getASTContext()); |
| 554 | |
David Blaikie | f62d4e7 | 2014-08-10 17:03:42 +0000 | [diff] [blame] | 555 | setCurrentInput(Input, std::move(AST)); |
| 556 | |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 557 | // Initialize the action. |
Argyrios Kyrtzidis | 873c858 | 2012-11-09 19:40:39 +0000 | [diff] [blame] | 558 | if (!BeginSourceFileAction(CI, InputFile)) |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 559 | goto failure; |
| 560 | |
James Dennett | f831767 | 2013-01-23 00:45:44 +0000 | [diff] [blame] | 561 | // Create the AST consumer. |
Argyrios Kyrtzidis | 873c858 | 2012-11-09 19:40:39 +0000 | [diff] [blame] | 562 | CI.setASTConsumer(CreateWrappedASTConsumer(CI, InputFile)); |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 563 | if (!CI.hasASTConsumer()) |
| 564 | goto failure; |
| 565 | |
| 566 | return true; |
| 567 | } |
| 568 | |
Ben Langmuir | 8832c06 | 2014-04-15 18:16:25 +0000 | [diff] [blame] | 569 | if (!CI.hasVirtualFileSystem()) { |
| 570 | if (IntrusiveRefCntPtr<vfs::FileSystem> VFS = |
| 571 | createVFSFromCompilerInvocation(CI.getInvocation(), |
| 572 | CI.getDiagnostics())) |
| 573 | CI.setVirtualFileSystem(VFS); |
| 574 | else |
| 575 | goto failure; |
Ben Langmuir | 801272a | 2014-02-25 18:23:47 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Daniel Dunbar | 9507f9c | 2010-06-07 23:26:47 +0000 | [diff] [blame] | 578 | // Set up the file and source managers, if needed. |
Daniel Dunbar | aed46fc | 2010-06-07 23:23:50 +0000 | [diff] [blame] | 579 | if (!CI.hasFileManager()) |
| 580 | CI.createFileManager(); |
| 581 | if (!CI.hasSourceManager()) |
Chris Lattner | 5159f61 | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 582 | CI.createSourceManager(CI.getFileManager()); |
Daniel Dunbar | 9507f9c | 2010-06-07 23:26:47 +0000 | [diff] [blame] | 583 | |
Richard Smith | f74d946 | 2017-04-28 01:49:42 +0000 | [diff] [blame] | 584 | // Set up embedding for any specified files. Do this before we load any |
| 585 | // source files, including the primary module map for the compilation. |
| 586 | for (const auto &F : CI.getFrontendOpts().ModulesEmbedFiles) { |
| 587 | if (const auto *FE = CI.getFileManager().getFile(F, /*openFile*/true)) |
| 588 | CI.getSourceManager().setFileIsTransient(FE); |
| 589 | else |
| 590 | CI.getDiagnostics().Report(diag::err_modules_embed_file_not_found) << F; |
| 591 | } |
| 592 | if (CI.getFrontendOpts().ModulesEmbedAllFiles) |
| 593 | CI.getSourceManager().setAllFilesAreTransient(true); |
| 594 | |
Daniel Dunbar | 9507f9c | 2010-06-07 23:26:47 +0000 | [diff] [blame] | 595 | // IR files bypass the rest of initialization. |
Richard Smith | 40c0efa | 2017-04-26 18:57:40 +0000 | [diff] [blame] | 596 | if (Input.getKind().getLanguage() == InputKind::LLVM_IR) { |
Daniel Dunbar | 9507f9c | 2010-06-07 23:26:47 +0000 | [diff] [blame] | 597 | assert(hasIRSupport() && |
| 598 | "This action does not have IR file support!"); |
| 599 | |
| 600 | // Inform the diagnostic client we are processing a source file. |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 601 | CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr); |
Jordan Rose | ea762b0 | 2012-08-10 01:06:08 +0000 | [diff] [blame] | 602 | HasBegunSourceFile = true; |
Daniel Dunbar | 9507f9c | 2010-06-07 23:26:47 +0000 | [diff] [blame] | 603 | |
| 604 | // Initialize the action. |
Argyrios Kyrtzidis | 873c858 | 2012-11-09 19:40:39 +0000 | [diff] [blame] | 605 | if (!BeginSourceFileAction(CI, InputFile)) |
Daniel Dunbar | 9507f9c | 2010-06-07 23:26:47 +0000 | [diff] [blame] | 606 | goto failure; |
| 607 | |
Ben Langmuir | beee15e | 2014-04-14 18:00:01 +0000 | [diff] [blame] | 608 | // Initialize the main file entry. |
| 609 | if (!CI.InitializeSourceManager(CurrentInput)) |
| 610 | goto failure; |
| 611 | |
Daniel Dunbar | 9507f9c | 2010-06-07 23:26:47 +0000 | [diff] [blame] | 612 | return true; |
| 613 | } |
| 614 | |
Douglas Gregor | fc9e7a2 | 2012-10-23 06:18:24 +0000 | [diff] [blame] | 615 | // If the implicit PCH include is actually a directory, rather than |
| 616 | // a single file, search for a suitable PCH file in that directory. |
| 617 | if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) { |
| 618 | FileManager &FileMgr = CI.getFileManager(); |
| 619 | PreprocessorOptions &PPOpts = CI.getPreprocessorOpts(); |
| 620 | StringRef PCHInclude = PPOpts.ImplicitPCHInclude; |
Argyrios Kyrtzidis | bd0b651 | 2015-02-19 20:12:20 +0000 | [diff] [blame] | 621 | std::string SpecificModuleCachePath = CI.getSpecificModuleCachePath(); |
Douglas Gregor | fc9e7a2 | 2012-10-23 06:18:24 +0000 | [diff] [blame] | 622 | if (const DirectoryEntry *PCHDir = FileMgr.getDirectory(PCHInclude)) { |
Rafael Espindola | c080917 | 2014-06-12 14:02:15 +0000 | [diff] [blame] | 623 | std::error_code EC; |
Douglas Gregor | fc9e7a2 | 2012-10-23 06:18:24 +0000 | [diff] [blame] | 624 | SmallString<128> DirNative; |
| 625 | llvm::sys::path::native(PCHDir->getName(), DirNative); |
| 626 | bool Found = false; |
Bruno Cardoso Lopes | b4d56f1 | 2016-12-12 19:28:21 +0000 | [diff] [blame] | 627 | vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem(); |
| 628 | for (vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC), DirEnd; |
Douglas Gregor | fc9e7a2 | 2012-10-23 06:18:24 +0000 | [diff] [blame] | 629 | Dir != DirEnd && !EC; Dir.increment(EC)) { |
| 630 | // Check whether this is an acceptable AST file. |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 631 | if (ASTReader::isAcceptableASTFile( |
Bruno Cardoso Lopes | b4d56f1 | 2016-12-12 19:28:21 +0000 | [diff] [blame] | 632 | Dir->getName(), FileMgr, CI.getPCHContainerReader(), |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 633 | CI.getLangOpts(), CI.getTargetOpts(), CI.getPreprocessorOpts(), |
| 634 | SpecificModuleCachePath)) { |
Bruno Cardoso Lopes | b4d56f1 | 2016-12-12 19:28:21 +0000 | [diff] [blame] | 635 | PPOpts.ImplicitPCHInclude = Dir->getName(); |
Argyrios Kyrtzidis | 48b72d8 | 2013-02-05 16:36:52 +0000 | [diff] [blame] | 636 | Found = true; |
Douglas Gregor | fc9e7a2 | 2012-10-23 06:18:24 +0000 | [diff] [blame] | 637 | break; |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | if (!Found) { |
| 642 | CI.getDiagnostics().Report(diag::err_fe_no_pch_in_dir) << PCHInclude; |
Richard Smith | 81c7248 | 2015-09-04 21:44:32 +0000 | [diff] [blame] | 643 | goto failure; |
Douglas Gregor | fc9e7a2 | 2012-10-23 06:18:24 +0000 | [diff] [blame] | 644 | } |
| 645 | } |
| 646 | } |
| 647 | |
Ted Kremenek | eeccb30 | 2014-08-27 15:14:15 +0000 | [diff] [blame] | 648 | // Set up the preprocessor if needed. When parsing model files the |
| 649 | // preprocessor of the original source is reused. |
| 650 | if (!isModelParsingAction()) |
| 651 | CI.createPreprocessor(getTranslationUnitKind()); |
Daniel Dunbar | aed46fc | 2010-06-07 23:23:50 +0000 | [diff] [blame] | 652 | |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 653 | // Inform the diagnostic client we are processing a source file. |
| 654 | CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), |
| 655 | &CI.getPreprocessor()); |
Jordan Rose | ea762b0 | 2012-08-10 01:06:08 +0000 | [diff] [blame] | 656 | HasBegunSourceFile = true; |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 657 | |
Richard Smith | 8128f33 | 2017-05-05 22:18:51 +0000 | [diff] [blame] | 658 | // Initialize the main file entry. |
| 659 | if (!CI.InitializeSourceManager(Input)) |
| 660 | goto failure; |
| 661 | |
Richard Smith | f74d946 | 2017-04-28 01:49:42 +0000 | [diff] [blame] | 662 | // For module map files, we first parse the module map and synthesize a |
| 663 | // "<module-includes>" buffer before more conventional processing. |
| 664 | if (Input.getKind().getFormat() == InputKind::ModuleMap) { |
| 665 | CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleMap); |
| 666 | |
Richard Smith | 8128f33 | 2017-05-05 22:18:51 +0000 | [diff] [blame] | 667 | unsigned OffsetToContents; |
| 668 | if (loadModuleMapForModuleBuild(CI, Input.getFile(), Input.isSystem(), |
| 669 | Input.isPreprocessed(), OffsetToContents)) |
Richard Smith | f74d946 | 2017-04-28 01:49:42 +0000 | [diff] [blame] | 670 | goto failure; |
| 671 | |
Richard Smith | 8128f33 | 2017-05-05 22:18:51 +0000 | [diff] [blame] | 672 | auto *CurrentModule = prepareToBuildModule(CI, Input.getFile()); |
| 673 | if (!CurrentModule) |
| 674 | goto failure; |
Richard Smith | f74d946 | 2017-04-28 01:49:42 +0000 | [diff] [blame] | 675 | |
Richard Smith | 8128f33 | 2017-05-05 22:18:51 +0000 | [diff] [blame] | 676 | if (OffsetToContents) |
| 677 | // If the module contents are in the same file, skip to them. |
| 678 | CI.getPreprocessor().setSkipMainFilePreamble(OffsetToContents, true); |
| 679 | else { |
| 680 | // Otherwise, convert the module description to a suitable input buffer. |
| 681 | auto Buffer = getInputBufferForModule(CI, CurrentModule); |
| 682 | if (!Buffer) |
| 683 | goto failure; |
| 684 | |
| 685 | // Reinitialize the main file entry to refer to the new input. |
| 686 | if (!CI.InitializeSourceManager(FrontendInputFile( |
| 687 | Buffer.release(), Input.getKind().withFormat(InputKind::Source), |
| 688 | CurrentModule->IsSystem))) |
| 689 | goto failure; |
| 690 | } |
Richard Smith | f74d946 | 2017-04-28 01:49:42 +0000 | [diff] [blame] | 691 | } |
| 692 | |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 693 | // Initialize the action. |
Argyrios Kyrtzidis | 873c858 | 2012-11-09 19:40:39 +0000 | [diff] [blame] | 694 | if (!BeginSourceFileAction(CI, InputFile)) |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 695 | goto failure; |
| 696 | |
James Dennett | f831767 | 2013-01-23 00:45:44 +0000 | [diff] [blame] | 697 | // Create the AST context and consumer unless this is a preprocessor only |
| 698 | // action. |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 699 | if (!usesPreprocessorOnly()) { |
Ted Kremenek | eeccb30 | 2014-08-27 15:14:15 +0000 | [diff] [blame] | 700 | // Parsing a model file should reuse the existing ASTContext. |
| 701 | if (!isModelParsingAction()) |
| 702 | CI.createASTContext(); |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 703 | |
Taewook Oh | 06b1af5 | 2017-03-07 20:20:23 +0000 | [diff] [blame] | 704 | // For preprocessed files, check if the first line specifies the original |
| 705 | // source file name with a linemarker. |
Richard Smith | 8128f33 | 2017-05-05 22:18:51 +0000 | [diff] [blame] | 706 | std::string PresumedInputFile = InputFile; |
Taewook Oh | 06b1af5 | 2017-03-07 20:20:23 +0000 | [diff] [blame] | 707 | if (Input.isPreprocessed()) |
Richard Smith | 8128f33 | 2017-05-05 22:18:51 +0000 | [diff] [blame] | 708 | ReadOriginalFileName(CI, PresumedInputFile); |
Taewook Oh | 06b1af5 | 2017-03-07 20:20:23 +0000 | [diff] [blame] | 709 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 710 | std::unique_ptr<ASTConsumer> Consumer = |
Richard Smith | 8128f33 | 2017-05-05 22:18:51 +0000 | [diff] [blame] | 711 | CreateWrappedASTConsumer(CI, PresumedInputFile); |
Fariborz Jahanian | 2129ccf | 2010-10-29 19:49:13 +0000 | [diff] [blame] | 712 | if (!Consumer) |
| 713 | goto failure; |
Sebastian Redl | 07a89a8 | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 714 | |
Ted Kremenek | eeccb30 | 2014-08-27 15:14:15 +0000 | [diff] [blame] | 715 | // FIXME: should not overwrite ASTMutationListener when parsing model files? |
| 716 | if (!isModelParsingAction()) |
| 717 | CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener()); |
Richard Smith | e842a47 | 2014-10-22 02:05:46 +0000 | [diff] [blame] | 718 | |
Argyrios Kyrtzidis | 35dcda7 | 2011-03-09 17:21:42 +0000 | [diff] [blame] | 719 | if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) { |
| 720 | // Convert headers to PCH and chain them. |
Alp Toker | 9e0523d | 2014-07-07 11:07:10 +0000 | [diff] [blame] | 721 | IntrusiveRefCntPtr<ExternalSemaSource> source, FinalReader; |
| 722 | source = createChainedIncludesSource(CI, FinalReader); |
Argyrios Kyrtzidis | 35dcda7 | 2011-03-09 17:21:42 +0000 | [diff] [blame] | 723 | if (!source) |
| 724 | goto failure; |
Alp Toker | 9e0523d | 2014-07-07 11:07:10 +0000 | [diff] [blame] | 725 | CI.setModuleManager(static_cast<ASTReader *>(FinalReader.get())); |
Argyrios Kyrtzidis | 35dcda7 | 2011-03-09 17:21:42 +0000 | [diff] [blame] | 726 | CI.getASTContext().setExternalSource(source); |
Vassil Vassilev | a2c2d94 | 2017-02-07 21:49:41 +0000 | [diff] [blame] | 727 | } else if (CI.getLangOpts().Modules || |
| 728 | !CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) { |
| 729 | // Use PCM or PCH. |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 730 | assert(hasPCHSupport() && "This action does not have PCH support!"); |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 731 | ASTDeserializationListener *DeserialListener = |
| 732 | Consumer->GetASTDeserializationListener(); |
Nico Weber | 824285e | 2014-05-08 04:26:47 +0000 | [diff] [blame] | 733 | bool DeleteDeserialListener = false; |
| 734 | if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls) { |
| 735 | DeserialListener = new DeserializedDeclsDumper(DeserialListener, |
| 736 | DeleteDeserialListener); |
| 737 | DeleteDeserialListener = true; |
| 738 | } |
| 739 | if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty()) { |
| 740 | DeserialListener = new DeserializedDeclsChecker( |
| 741 | CI.getASTContext(), |
| 742 | CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn, |
| 743 | DeserialListener, DeleteDeserialListener); |
| 744 | DeleteDeserialListener = true; |
| 745 | } |
Vassil Vassilev | a2c2d94 | 2017-02-07 21:49:41 +0000 | [diff] [blame] | 746 | if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) { |
| 747 | CI.createPCHExternalASTSource( |
| 748 | CI.getPreprocessorOpts().ImplicitPCHInclude, |
| 749 | CI.getPreprocessorOpts().DisablePCHValidation, |
Nico Weber | 824285e | 2014-05-08 04:26:47 +0000 | [diff] [blame] | 750 | CI.getPreprocessorOpts().AllowPCHWithCompilerErrors, DeserialListener, |
Vassil Vassilev | a2c2d94 | 2017-02-07 21:49:41 +0000 | [diff] [blame] | 751 | DeleteDeserialListener); |
| 752 | if (!CI.getASTContext().getExternalSource()) |
| 753 | goto failure; |
| 754 | } |
| 755 | // If modules are enabled, create the module manager before creating |
| 756 | // any builtins, so that all declarations know that they might be |
| 757 | // extended by an external source. |
| 758 | if (CI.getLangOpts().Modules || !CI.hasASTContext() || |
| 759 | !CI.getASTContext().getExternalSource()) { |
| 760 | CI.createModuleManager(); |
| 761 | CI.getModuleManager()->setDeserializationListener(DeserialListener, |
| 762 | DeleteDeserialListener); |
| 763 | } |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 764 | } |
Sebastian Redl | 4d3af3e | 2010-07-09 21:00:24 +0000 | [diff] [blame] | 765 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 766 | CI.setASTConsumer(std::move(Consumer)); |
Sebastian Redl | 4d3af3e | 2010-07-09 21:00:24 +0000 | [diff] [blame] | 767 | if (!CI.hasASTConsumer()) |
| 768 | goto failure; |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 769 | } |
| 770 | |
Jonathan D. Turner | 0248f57 | 2011-08-05 22:17:03 +0000 | [diff] [blame] | 771 | // 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] | 772 | // source. |
Vassil Vassilev | a2c2d94 | 2017-02-07 21:49:41 +0000 | [diff] [blame] | 773 | if (CI.getLangOpts().Modules || !CI.hasASTContext() || |
| 774 | !CI.getASTContext().getExternalSource()) { |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 775 | Preprocessor &PP = CI.getPreprocessor(); |
Eric Christopher | 02d5d86 | 2015-08-06 01:01:12 +0000 | [diff] [blame] | 776 | PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(), |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 777 | PP.getLangOpts()); |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 778 | } else { |
| 779 | // FIXME: If this is a problem, recover from it by creating a multiplex |
| 780 | // source. |
| 781 | assert((!CI.getLangOpts().Modules || CI.getModuleManager()) && |
| 782 | "modules enabled but created an external source that " |
| 783 | "doesn't support modules"); |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 784 | } |
| 785 | |
Richard Smith | ac425e9 | 2015-01-23 00:01:13 +0000 | [diff] [blame] | 786 | // If we were asked to load any module map files, do so now. |
| 787 | for (const auto &Filename : CI.getFrontendOpts().ModuleMapFiles) { |
| 788 | if (auto *File = CI.getFileManager().getFile(Filename)) |
| 789 | CI.getPreprocessor().getHeaderSearchInfo().loadModuleMapFile( |
| 790 | File, /*IsSystem*/false); |
| 791 | else |
| 792 | CI.getDiagnostics().Report(diag::err_module_map_not_found) << Filename; |
| 793 | } |
| 794 | |
Richard Smith | d4b230b | 2014-10-27 23:01:16 +0000 | [diff] [blame] | 795 | // If we were asked to load any module files, do so now. |
| 796 | for (const auto &ModuleFile : CI.getFrontendOpts().ModuleFiles) |
| 797 | if (!CI.loadModuleFile(ModuleFile)) |
Richard Smith | e842a47 | 2014-10-22 02:05:46 +0000 | [diff] [blame] | 798 | goto failure; |
Richard Smith | e842a47 | 2014-10-22 02:05:46 +0000 | [diff] [blame] | 799 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 800 | // If there is a layout overrides file, attach an external AST source that |
| 801 | // provides the layouts from that file. |
Taewook Oh | 06b1af5 | 2017-03-07 20:20:23 +0000 | [diff] [blame] | 802 | if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() && |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 803 | CI.hasASTContext() && !CI.getASTContext().getExternalSource()) { |
Taewook Oh | 06b1af5 | 2017-03-07 20:20:23 +0000 | [diff] [blame] | 804 | IntrusiveRefCntPtr<ExternalASTSource> |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 805 | Override(new LayoutOverrideSource( |
| 806 | CI.getFrontendOpts().OverrideRecordLayoutsFile)); |
| 807 | CI.getASTContext().setExternalSource(Override); |
| 808 | } |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 809 | |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 810 | return true; |
| 811 | |
| 812 | // If we failed, reset state since the client will not end up calling the |
| 813 | // matching EndSourceFile(). |
| 814 | failure: |
| 815 | if (isCurrentFileAST()) { |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 816 | CI.setASTContext(nullptr); |
| 817 | CI.setPreprocessor(nullptr); |
| 818 | CI.setSourceManager(nullptr); |
| 819 | CI.setFileManager(nullptr); |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 820 | } |
| 821 | |
Jordan Rose | ea762b0 | 2012-08-10 01:06:08 +0000 | [diff] [blame] | 822 | if (HasBegunSourceFile) |
| 823 | CI.getDiagnosticClient().EndSourceFile(); |
Benjamin Kramer | 3c717b4 | 2012-10-14 19:21:21 +0000 | [diff] [blame] | 824 | CI.clearOutputFiles(/*EraseFiles=*/true); |
Richard Smith | f74d946 | 2017-04-28 01:49:42 +0000 | [diff] [blame] | 825 | CI.getLangOpts().setCompilingModule(LangOptions::CMK_None); |
Douglas Gregor | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 826 | setCurrentInput(FrontendInputFile()); |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 827 | setCompilerInstance(nullptr); |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 828 | return false; |
| 829 | } |
| 830 | |
Argyrios Kyrtzidis | 1416e17 | 2012-06-08 05:48:06 +0000 | [diff] [blame] | 831 | bool FrontendAction::Execute() { |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 832 | CompilerInstance &CI = getCompilerInstance(); |
| 833 | |
Kovarththanan Rajaratnam | 5505dff | 2009-11-29 09:57:35 +0000 | [diff] [blame] | 834 | if (CI.hasFrontendTimer()) { |
| 835 | llvm::TimeRegion Timer(CI.getFrontendTimer()); |
| 836 | ExecuteAction(); |
| 837 | } |
| 838 | else ExecuteAction(); |
Argyrios Kyrtzidis | 1416e17 | 2012-06-08 05:48:06 +0000 | [diff] [blame] | 839 | |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 840 | // 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] | 841 | // there were any module-build failures. |
| 842 | if (CI.shouldBuildGlobalModuleIndex() && CI.hasFileManager() && |
| 843 | CI.hasPreprocessor()) { |
Richard Smith | 3938f0c | 2015-08-15 00:34:15 +0000 | [diff] [blame] | 844 | StringRef Cache = |
| 845 | CI.getPreprocessor().getHeaderSearchInfo().getModuleCachePath(); |
| 846 | if (!Cache.empty()) |
| 847 | GlobalModuleIndex::writeIndex(CI.getFileManager(), |
| 848 | CI.getPCHContainerReader(), Cache); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Argyrios Kyrtzidis | 1416e17 | 2012-06-08 05:48:06 +0000 | [diff] [blame] | 851 | return true; |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | void FrontendAction::EndSourceFile() { |
| 855 | CompilerInstance &CI = getCompilerInstance(); |
| 856 | |
Douglas Gregor | 1388a89 | 2011-02-09 18:47:31 +0000 | [diff] [blame] | 857 | // Inform the diagnostic client we are done with this source file. |
| 858 | CI.getDiagnosticClient().EndSourceFile(); |
| 859 | |
Benjamin Kramer | 88d99e4 | 2014-08-07 20:51:16 +0000 | [diff] [blame] | 860 | // Inform the preprocessor we are done. |
| 861 | if (CI.hasPreprocessor()) |
| 862 | CI.getPreprocessor().EndSourceFile(); |
| 863 | |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 864 | // Finalize the action. |
| 865 | EndSourceFileAction(); |
| 866 | |
Nico Weber | 7de358e | 2014-04-24 02:42:04 +0000 | [diff] [blame] | 867 | // Sema references the ast consumer, so reset sema first. |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 868 | // |
| 869 | // FIXME: There is more per-file stuff we could just drop here? |
Nico Weber | 7de358e | 2014-04-24 02:42:04 +0000 | [diff] [blame] | 870 | bool DisableFree = CI.getFrontendOpts().DisableFree; |
| 871 | if (DisableFree) { |
Duncan P. N. Exon Smith | 4a8212a | 2015-05-04 14:59:20 +0000 | [diff] [blame] | 872 | CI.resetAndLeakSema(); |
| 873 | CI.resetAndLeakASTContext(); |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 874 | BuryPointer(CI.takeASTConsumer().get()); |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 875 | } else { |
Duncan P. N. Exon Smith | 4a8212a | 2015-05-04 14:59:20 +0000 | [diff] [blame] | 876 | CI.setSema(nullptr); |
| 877 | CI.setASTContext(nullptr); |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 878 | CI.setASTConsumer(nullptr); |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | if (CI.getFrontendOpts().ShowStats) { |
| 882 | llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n"; |
| 883 | CI.getPreprocessor().PrintStats(); |
| 884 | CI.getPreprocessor().getIdentifierTable().PrintStats(); |
| 885 | CI.getPreprocessor().getHeaderSearchInfo().PrintStats(); |
| 886 | CI.getSourceManager().PrintStats(); |
| 887 | llvm::errs() << "\n"; |
| 888 | } |
| 889 | |
Argyrios Kyrtzidis | f0168de | 2013-06-11 00:36:55 +0000 | [diff] [blame] | 890 | // Cleanup the output streams, and erase the output files if instructed by the |
| 891 | // FrontendAction. |
| 892 | CI.clearOutputFiles(/*EraseFiles=*/shouldEraseOutputFiles()); |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 893 | |
Nico Weber | 1f29ccf | 2014-04-24 03:31:27 +0000 | [diff] [blame] | 894 | if (isCurrentFileAST()) { |
Duncan P. N. Exon Smith | 4a8212a | 2015-05-04 14:59:20 +0000 | [diff] [blame] | 895 | if (DisableFree) { |
| 896 | CI.resetAndLeakPreprocessor(); |
| 897 | CI.resetAndLeakSourceManager(); |
| 898 | CI.resetAndLeakFileManager(); |
| 899 | } else { |
| 900 | CI.setPreprocessor(nullptr); |
| 901 | CI.setSourceManager(nullptr); |
| 902 | CI.setFileManager(nullptr); |
| 903 | } |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 904 | } |
| 905 | |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 906 | setCompilerInstance(nullptr); |
Douglas Gregor | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 907 | setCurrentInput(FrontendInputFile()); |
Richard Smith | f74d946 | 2017-04-28 01:49:42 +0000 | [diff] [blame] | 908 | CI.getLangOpts().setCompilingModule(LangOptions::CMK_None); |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 909 | } |
| 910 | |
Argyrios Kyrtzidis | f0168de | 2013-06-11 00:36:55 +0000 | [diff] [blame] | 911 | bool FrontendAction::shouldEraseOutputFiles() { |
| 912 | return getCompilerInstance().getDiagnostics().hasErrorOccurred(); |
| 913 | } |
| 914 | |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 915 | //===----------------------------------------------------------------------===// |
| 916 | // Utility Actions |
| 917 | //===----------------------------------------------------------------------===// |
| 918 | |
| 919 | void ASTFrontendAction::ExecuteAction() { |
| 920 | CompilerInstance &CI = getCompilerInstance(); |
Rafael Espindola | 5150f2f | 2013-07-28 13:23:37 +0000 | [diff] [blame] | 921 | if (!CI.hasPreprocessor()) |
| 922 | return; |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 923 | |
| 924 | // FIXME: Move the truncation aspect of this into Sema, we delayed this till |
| 925 | // here so the source manager would be initialized. |
| 926 | if (hasCodeCompletionSupport() && |
| 927 | !CI.getFrontendOpts().CodeCompletionAt.FileName.empty()) |
| 928 | CI.createCodeCompletionConsumer(); |
| 929 | |
| 930 | // Use a code completion consumer? |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 931 | CodeCompleteConsumer *CompletionConsumer = nullptr; |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 932 | if (CI.hasCodeCompletionConsumer()) |
| 933 | CompletionConsumer = &CI.getCodeCompletionConsumer(); |
| 934 | |
Douglas Gregor | 0e93f01 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 935 | if (!CI.hasSema()) |
Douglas Gregor | 69f74f8 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 936 | CI.createSema(getTranslationUnitKind(), CompletionConsumer); |
Douglas Gregor | 0e93f01 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 937 | |
Erik Verbruggen | 6e92251 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 938 | ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats, |
| 939 | CI.getFrontendOpts().SkipFunctionBodies); |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 940 | } |
| 941 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 942 | void PluginASTAction::anchor() { } |
| 943 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 944 | std::unique_ptr<ASTConsumer> |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 945 | PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 946 | StringRef InFile) { |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 947 | llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!"); |
Daniel Dunbar | a0ff58d | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 948 | } |
Chandler Carruth | b570351 | 2011-06-16 16:17:05 +0000 | [diff] [blame] | 949 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 950 | std::unique_ptr<ASTConsumer> |
| 951 | WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI, |
| 952 | StringRef InFile) { |
Chandler Carruth | b570351 | 2011-06-16 16:17:05 +0000 | [diff] [blame] | 953 | return WrappedAction->CreateASTConsumer(CI, InFile); |
| 954 | } |
Argyrios Kyrtzidis | 90b6a2a | 2011-06-18 00:53:41 +0000 | [diff] [blame] | 955 | bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) { |
| 956 | return WrappedAction->BeginInvocation(CI); |
| 957 | } |
Chandler Carruth | b570351 | 2011-06-16 16:17:05 +0000 | [diff] [blame] | 958 | bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 959 | StringRef Filename) { |
Douglas Gregor | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 960 | WrappedAction->setCurrentInput(getCurrentInput()); |
Argyrios Kyrtzidis | 90b6a2a | 2011-06-18 00:53:41 +0000 | [diff] [blame] | 961 | WrappedAction->setCompilerInstance(&CI); |
Argyrios Kyrtzidis | e89a179 | 2016-02-16 05:39:33 +0000 | [diff] [blame] | 962 | auto Ret = WrappedAction->BeginSourceFileAction(CI, Filename); |
| 963 | // BeginSourceFileAction may change CurrentInput, e.g. during module builds. |
| 964 | setCurrentInput(WrappedAction->getCurrentInput()); |
| 965 | return Ret; |
Chandler Carruth | b570351 | 2011-06-16 16:17:05 +0000 | [diff] [blame] | 966 | } |
| 967 | void WrapperFrontendAction::ExecuteAction() { |
| 968 | WrappedAction->ExecuteAction(); |
| 969 | } |
| 970 | void WrapperFrontendAction::EndSourceFileAction() { |
| 971 | WrappedAction->EndSourceFileAction(); |
| 972 | } |
| 973 | |
| 974 | bool WrapperFrontendAction::usesPreprocessorOnly() const { |
| 975 | return WrappedAction->usesPreprocessorOnly(); |
| 976 | } |
Douglas Gregor | 69f74f8 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 977 | TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() { |
| 978 | return WrappedAction->getTranslationUnitKind(); |
Chandler Carruth | b570351 | 2011-06-16 16:17:05 +0000 | [diff] [blame] | 979 | } |
| 980 | bool WrapperFrontendAction::hasPCHSupport() const { |
| 981 | return WrappedAction->hasPCHSupport(); |
| 982 | } |
| 983 | bool WrapperFrontendAction::hasASTFileSupport() const { |
| 984 | return WrappedAction->hasASTFileSupport(); |
| 985 | } |
| 986 | bool WrapperFrontendAction::hasIRSupport() const { |
| 987 | return WrappedAction->hasIRSupport(); |
| 988 | } |
| 989 | bool WrapperFrontendAction::hasCodeCompletionSupport() const { |
| 990 | return WrappedAction->hasCodeCompletionSupport(); |
| 991 | } |
| 992 | |
Argyrios Kyrtzidis | d35e98f | 2016-02-07 19:28:36 +0000 | [diff] [blame] | 993 | WrapperFrontendAction::WrapperFrontendAction( |
| 994 | std::unique_ptr<FrontendAction> WrappedAction) |
| 995 | : WrappedAction(std::move(WrappedAction)) {} |
Chandler Carruth | b570351 | 2011-06-16 16:17:05 +0000 | [diff] [blame] | 996 | |