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