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