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