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