1 //===--- PPLexerChange.cpp - Handle changing lexers in the preprocessor ---===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements pieces of the Preprocessor interface that manage the
10 // current lexer stack.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Basic/FileManager.h"
15 #include "clang/Basic/SourceManager.h"
16 #include "clang/Lex/HeaderSearch.h"
17 #include "clang/Lex/LexDiagnostic.h"
18 #include "clang/Lex/MacroInfo.h"
19 #include "clang/Lex/Preprocessor.h"
20 #include "clang/Lex/PreprocessorOptions.h"
21 #include "llvm/ADT/StringSwitch.h"
22 #include "llvm/Support/FileSystem.h"
23 #include "llvm/Support/MemoryBufferRef.h"
24 #include "llvm/Support/Path.h"
27 using namespace clang
;
29 //===----------------------------------------------------------------------===//
30 // Miscellaneous Methods.
31 //===----------------------------------------------------------------------===//
33 /// isInPrimaryFile - Return true if we're in the top-level file, not in a
34 /// \#include. This looks through macro expansions and active _Pragma lexers.
35 bool Preprocessor::isInPrimaryFile() const {
37 return IncludeMacroStack
.empty();
39 // If there are any stacked lexers, we're in a #include.
40 assert(IsFileLexer(IncludeMacroStack
[0]) &&
41 "Top level include stack isn't our primary lexer?");
43 llvm::drop_begin(IncludeMacroStack
),
44 [&](const IncludeStackInfo
&ISI
) -> bool { return IsFileLexer(ISI
); });
47 /// getCurrentLexer - Return the current file lexer being lexed from. Note
48 /// that this ignores any potentially active macro expansions and _Pragma
49 /// expansions going on at the time.
50 PreprocessorLexer
*Preprocessor::getCurrentFileLexer() const {
54 // Look for a stacked lexer.
55 for (const IncludeStackInfo
&ISI
: llvm::reverse(IncludeMacroStack
)) {
57 return ISI
.ThePPLexer
;
63 //===----------------------------------------------------------------------===//
64 // Methods for Entering and Callbacks for leaving various contexts
65 //===----------------------------------------------------------------------===//
67 /// EnterSourceFile - Add a source file to the top of the include stack and
68 /// start lexing tokens from it instead of the current buffer.
69 bool Preprocessor::EnterSourceFile(FileID FID
, ConstSearchDirIterator CurDir
,
71 bool IsFirstIncludeOfFile
) {
72 assert(!CurTokenLexer
&& "Cannot #include a file inside a macro!");
73 ++NumEnteredSourceFiles
;
75 if (MaxIncludeStackDepth
< IncludeMacroStack
.size())
76 MaxIncludeStackDepth
= IncludeMacroStack
.size();
78 // Get the MemoryBuffer for this FID, if it fails, we fail.
79 std::optional
<llvm::MemoryBufferRef
> InputFile
=
80 getSourceManager().getBufferOrNone(FID
, Loc
);
82 SourceLocation FileStart
= SourceMgr
.getLocForStartOfFile(FID
);
83 Diag(Loc
, diag::err_pp_error_opening_file
)
84 << std::string(SourceMgr
.getBufferName(FileStart
)) << "";
88 if (isCodeCompletionEnabled() &&
89 SourceMgr
.getFileEntryForID(FID
) == CodeCompletionFile
) {
90 CodeCompletionFileLoc
= SourceMgr
.getLocForStartOfFile(FID
);
92 CodeCompletionFileLoc
.getLocWithOffset(CodeCompletionOffset
);
95 Lexer
*TheLexer
= new Lexer(FID
, *InputFile
, *this, IsFirstIncludeOfFile
);
96 if (getPreprocessorOpts().DependencyDirectivesForFile
&&
97 FID
!= PredefinesFileID
) {
98 if (OptionalFileEntryRef File
= SourceMgr
.getFileEntryRefForID(FID
)) {
99 if (std::optional
<ArrayRef
<dependency_directives_scan::Directive
>>
101 getPreprocessorOpts().DependencyDirectivesForFile(*File
)) {
102 TheLexer
->DepDirectives
= *DepDirectives
;
107 EnterSourceFileWithLexer(TheLexer
, CurDir
);
111 /// EnterSourceFileWithLexer - Add a source file to the top of the include stack
112 /// and start lexing tokens from it instead of the current buffer.
113 void Preprocessor::EnterSourceFileWithLexer(Lexer
*TheLexer
,
114 ConstSearchDirIterator CurDir
) {
115 PreprocessorLexer
*PrevPPLexer
= CurPPLexer
;
117 // Add the current lexer to the include stack.
118 if (CurPPLexer
|| CurTokenLexer
)
119 PushIncludeMacroStack();
121 CurLexer
.reset(TheLexer
);
122 CurPPLexer
= TheLexer
;
123 CurDirLookup
= CurDir
;
124 CurLexerSubmodule
= nullptr;
125 if (CurLexerKind
!= CLK_LexAfterModuleImport
)
126 CurLexerKind
= TheLexer
->isDependencyDirectivesLexer()
127 ? CLK_DependencyDirectivesLexer
130 // Notify the client, if desired, that we are in a new source file.
131 if (Callbacks
&& !CurLexer
->Is_PragmaLexer
) {
132 SrcMgr::CharacteristicKind FileType
=
133 SourceMgr
.getFileCharacteristic(CurLexer
->getFileLoc());
136 SourceLocation EnterLoc
;
138 PrevFID
= PrevPPLexer
->getFileID();
139 EnterLoc
= PrevPPLexer
->getSourceLocation();
141 Callbacks
->FileChanged(CurLexer
->getFileLoc(), PPCallbacks::EnterFile
,
143 Callbacks
->LexedFileChanged(CurLexer
->getFileID(),
144 PPCallbacks::LexedFileChangeReason::EnterFile
,
145 FileType
, PrevFID
, EnterLoc
);
149 /// EnterMacro - Add a Macro to the top of the include stack and start lexing
150 /// tokens from it instead of the current buffer.
151 void Preprocessor::EnterMacro(Token
&Tok
, SourceLocation ILEnd
,
152 MacroInfo
*Macro
, MacroArgs
*Args
) {
153 std::unique_ptr
<TokenLexer
> TokLexer
;
154 if (NumCachedTokenLexers
== 0) {
155 TokLexer
= std::make_unique
<TokenLexer
>(Tok
, ILEnd
, Macro
, Args
, *this);
157 TokLexer
= std::move(TokenLexerCache
[--NumCachedTokenLexers
]);
158 TokLexer
->Init(Tok
, ILEnd
, Macro
, Args
);
161 PushIncludeMacroStack();
162 CurDirLookup
= nullptr;
163 CurTokenLexer
= std::move(TokLexer
);
164 if (CurLexerKind
!= CLK_LexAfterModuleImport
)
165 CurLexerKind
= CLK_TokenLexer
;
168 /// EnterTokenStream - Add a "macro" context to the top of the include stack,
169 /// which will cause the lexer to start returning the specified tokens.
171 /// If DisableMacroExpansion is true, tokens lexed from the token stream will
172 /// not be subject to further macro expansion. Otherwise, these tokens will
173 /// be re-macro-expanded when/if expansion is enabled.
175 /// If OwnsTokens is false, this method assumes that the specified stream of
176 /// tokens has a permanent owner somewhere, so they do not need to be copied.
177 /// If it is true, it assumes the array of tokens is allocated with new[] and
180 void Preprocessor::EnterTokenStream(const Token
*Toks
, unsigned NumToks
,
181 bool DisableMacroExpansion
, bool OwnsTokens
,
183 if (CurLexerKind
== CLK_CachingLexer
) {
184 if (CachedLexPos
< CachedTokens
.size()) {
185 assert(IsReinject
&& "new tokens in the middle of cached stream");
186 // We're entering tokens into the middle of our cached token stream. We
187 // can't represent that, so just insert the tokens into the buffer.
188 CachedTokens
.insert(CachedTokens
.begin() + CachedLexPos
,
189 Toks
, Toks
+ NumToks
);
195 // New tokens are at the end of the cached token sequnece; insert the
196 // token stream underneath the caching lexer.
197 ExitCachingLexMode();
198 EnterTokenStream(Toks
, NumToks
, DisableMacroExpansion
, OwnsTokens
,
200 EnterCachingLexMode();
204 // Create a macro expander to expand from the specified token stream.
205 std::unique_ptr
<TokenLexer
> TokLexer
;
206 if (NumCachedTokenLexers
== 0) {
207 TokLexer
= std::make_unique
<TokenLexer
>(
208 Toks
, NumToks
, DisableMacroExpansion
, OwnsTokens
, IsReinject
, *this);
210 TokLexer
= std::move(TokenLexerCache
[--NumCachedTokenLexers
]);
211 TokLexer
->Init(Toks
, NumToks
, DisableMacroExpansion
, OwnsTokens
,
215 // Save our current state.
216 PushIncludeMacroStack();
217 CurDirLookup
= nullptr;
218 CurTokenLexer
= std::move(TokLexer
);
219 if (CurLexerKind
!= CLK_LexAfterModuleImport
)
220 CurLexerKind
= CLK_TokenLexer
;
223 /// Compute the relative path that names the given file relative to
224 /// the given directory.
225 static void computeRelativePath(FileManager
&FM
, const DirectoryEntry
*Dir
,
226 FileEntryRef File
, SmallString
<128> &Result
) {
229 StringRef FilePath
= File
.getDir().getName();
230 StringRef Path
= FilePath
;
231 while (!Path
.empty()) {
232 if (auto CurDir
= FM
.getDirectory(Path
)) {
233 if (*CurDir
== Dir
) {
234 Result
= FilePath
.substr(Path
.size());
235 llvm::sys::path::append(Result
,
236 llvm::sys::path::filename(File
.getName()));
241 Path
= llvm::sys::path::parent_path(Path
);
244 Result
= File
.getName();
247 void Preprocessor::PropagateLineStartLeadingSpaceInfo(Token
&Result
) {
249 CurTokenLexer
->PropagateLineStartLeadingSpaceInfo(Result
);
253 CurLexer
->PropagateLineStartLeadingSpaceInfo(Result
);
256 // FIXME: Handle other kinds of lexers? It generally shouldn't matter,
257 // but it might if they're empty?
260 /// Determine the location to use as the end of the buffer for a lexer.
262 /// If the file ends with a newline, form the EOF token on the newline itself,
263 /// rather than "on the line following it", which doesn't exist. This makes
264 /// diagnostics relating to the end of file include the last file that the user
265 /// actually typed, which is goodness.
266 const char *Preprocessor::getCurLexerEndPos() {
267 const char *EndPos
= CurLexer
->BufferEnd
;
268 if (EndPos
!= CurLexer
->BufferStart
&&
269 (EndPos
[-1] == '\n' || EndPos
[-1] == '\r')) {
272 // Handle \n\r and \r\n:
273 if (EndPos
!= CurLexer
->BufferStart
&&
274 (EndPos
[-1] == '\n' || EndPos
[-1] == '\r') &&
275 EndPos
[-1] != EndPos
[0])
282 static void collectAllSubModulesWithUmbrellaHeader(
283 const Module
&Mod
, SmallVectorImpl
<const Module
*> &SubMods
) {
284 if (Mod
.getUmbrellaHeaderAsWritten())
285 SubMods
.push_back(&Mod
);
286 for (auto *M
: Mod
.submodules())
287 collectAllSubModulesWithUmbrellaHeader(*M
, SubMods
);
290 void Preprocessor::diagnoseMissingHeaderInUmbrellaDir(const Module
&Mod
) {
291 std::optional
<Module::Header
> UmbrellaHeader
=
292 Mod
.getUmbrellaHeaderAsWritten();
293 assert(UmbrellaHeader
&& "Module must use umbrella header");
294 const FileID
&File
= SourceMgr
.translateFile(UmbrellaHeader
->Entry
);
295 SourceLocation ExpectedHeadersLoc
= SourceMgr
.getLocForEndOfFile(File
);
296 if (getDiagnostics().isIgnored(diag::warn_uncovered_module_header
,
300 ModuleMap
&ModMap
= getHeaderSearchInfo().getModuleMap();
301 OptionalDirectoryEntryRef Dir
= Mod
.getEffectiveUmbrellaDir();
302 llvm::vfs::FileSystem
&FS
= FileMgr
.getVirtualFileSystem();
304 for (llvm::vfs::recursive_directory_iterator
Entry(FS
, Dir
->getName(), EC
),
306 Entry
!= End
&& !EC
; Entry
.increment(EC
)) {
307 using llvm::StringSwitch
;
309 // Check whether this entry has an extension typically associated with
311 if (!StringSwitch
<bool>(llvm::sys::path::extension(Entry
->path()))
312 .Cases(".h", ".H", ".hh", ".hpp", true)
316 if (auto Header
= getFileManager().getOptionalFileRef(Entry
->path()))
317 if (!getSourceManager().hasFileInfo(*Header
)) {
318 if (!ModMap
.isHeaderInUnavailableModule(*Header
)) {
319 // Find the relative path that would access this header.
320 SmallString
<128> RelativePath
;
321 computeRelativePath(FileMgr
, *Dir
, *Header
, RelativePath
);
322 Diag(ExpectedHeadersLoc
, diag::warn_uncovered_module_header
)
323 << Mod
.getFullModuleName() << RelativePath
;
329 /// HandleEndOfFile - This callback is invoked when the lexer hits the end of
330 /// the current file. This either returns the EOF token or pops a level off
331 /// the include stack and keeps going.
332 bool Preprocessor::HandleEndOfFile(Token
&Result
, bool isEndOfMacro
) {
333 assert(!CurTokenLexer
&&
334 "Ending a file when currently in a macro!");
336 SourceLocation UnclosedSafeBufferOptOutLoc
;
338 if (IncludeMacroStack
.empty() &&
339 isPPInSafeBufferOptOutRegion(UnclosedSafeBufferOptOutLoc
)) {
340 // To warn if a "-Wunsafe-buffer-usage" opt-out region is still open by the
342 Diag(UnclosedSafeBufferOptOutLoc
,
343 diag::err_pp_unclosed_pragma_unsafe_buffer_usage
);
345 // If we have an unclosed module region from a pragma at the end of a
346 // module, complain and close it now.
347 const bool LeavingSubmodule
= CurLexer
&& CurLexerSubmodule
;
348 if ((LeavingSubmodule
|| IncludeMacroStack
.empty()) &&
349 !BuildingSubmoduleStack
.empty() &&
350 BuildingSubmoduleStack
.back().IsPragma
) {
351 Diag(BuildingSubmoduleStack
.back().ImportLoc
,
352 diag::err_pp_module_begin_without_module_end
);
353 Module
*M
= LeaveSubmodule(/*ForPragma*/true);
356 const char *EndPos
= getCurLexerEndPos();
357 CurLexer
->BufferPtr
= EndPos
;
358 CurLexer
->FormTokenWithChars(Result
, EndPos
, tok::annot_module_end
);
359 Result
.setAnnotationEndLoc(Result
.getLocation());
360 Result
.setAnnotationValue(M
);
364 // See if this file had a controlling macro.
365 if (CurPPLexer
) { // Not ending a macro, ignore it.
366 if (const IdentifierInfo
*ControllingMacro
=
367 CurPPLexer
->MIOpt
.GetControllingMacroAtEndOfFile()) {
368 // Okay, this has a controlling macro, remember in HeaderFileInfo.
369 if (OptionalFileEntryRef FE
= CurPPLexer
->getFileEntry()) {
370 HeaderInfo
.SetFileControllingMacro(*FE
, ControllingMacro
);
372 getMacroInfo(const_cast<IdentifierInfo
*>(ControllingMacro
)))
373 MI
->setUsedForHeaderGuard(true);
374 if (const IdentifierInfo
*DefinedMacro
=
375 CurPPLexer
->MIOpt
.GetDefinedMacro()) {
376 if (!isMacroDefined(ControllingMacro
) &&
377 DefinedMacro
!= ControllingMacro
&&
378 CurLexer
->isFirstTimeLexingFile()) {
380 // If the edit distance between the two macros is more than 50%,
381 // DefinedMacro may not be header guard, or can be header guard of
382 // another header file. Therefore, it maybe defining something
383 // completely different. This can be observed in the wild when
384 // handling feature macros or header guards in different files.
386 const StringRef ControllingMacroName
= ControllingMacro
->getName();
387 const StringRef DefinedMacroName
= DefinedMacro
->getName();
388 const size_t MaxHalfLength
= std::max(ControllingMacroName
.size(),
389 DefinedMacroName
.size()) / 2;
390 const unsigned ED
= ControllingMacroName
.edit_distance(
391 DefinedMacroName
, true, MaxHalfLength
);
392 if (ED
<= MaxHalfLength
) {
393 // Emit a warning for a bad header guard.
394 Diag(CurPPLexer
->MIOpt
.GetMacroLocation(),
395 diag::warn_header_guard
)
396 << CurPPLexer
->MIOpt
.GetMacroLocation() << ControllingMacro
;
397 Diag(CurPPLexer
->MIOpt
.GetDefinedLocation(),
398 diag::note_header_guard
)
399 << CurPPLexer
->MIOpt
.GetDefinedLocation() << DefinedMacro
401 << FixItHint::CreateReplacement(
402 CurPPLexer
->MIOpt
.GetDefinedLocation(),
403 ControllingMacro
->getName());
411 // Complain about reaching a true EOF within arc_cf_code_audited.
412 // We don't want to complain about reaching the end of a macro
413 // instantiation or a _Pragma.
414 if (PragmaARCCFCodeAuditedInfo
.second
.isValid() && !isEndOfMacro
&&
415 !(CurLexer
&& CurLexer
->Is_PragmaLexer
)) {
416 Diag(PragmaARCCFCodeAuditedInfo
.second
,
417 diag::err_pp_eof_in_arc_cf_code_audited
);
419 // Recover by leaving immediately.
420 PragmaARCCFCodeAuditedInfo
= {nullptr, SourceLocation()};
423 // Complain about reaching a true EOF within assume_nonnull.
424 // We don't want to complain about reaching the end of a macro
425 // instantiation or a _Pragma.
426 if (PragmaAssumeNonNullLoc
.isValid() &&
427 !isEndOfMacro
&& !(CurLexer
&& CurLexer
->Is_PragmaLexer
)) {
428 // If we're at the end of generating a preamble, we should record the
429 // unterminated \#pragma clang assume_nonnull so we can restore it later
430 // when the preamble is loaded into the main file.
431 if (isRecordingPreamble() && isInPrimaryFile())
432 PreambleRecordedPragmaAssumeNonNullLoc
= PragmaAssumeNonNullLoc
;
434 Diag(PragmaAssumeNonNullLoc
, diag::err_pp_eof_in_assume_nonnull
);
435 // Recover by leaving immediately.
436 PragmaAssumeNonNullLoc
= SourceLocation();
439 bool LeavingPCHThroughHeader
= false;
441 // If this is a #include'd file, pop it off the include stack and continue
442 // lexing the #includer file.
443 if (!IncludeMacroStack
.empty()) {
445 // If we lexed the code-completion file, act as if we reached EOF.
446 if (isCodeCompletionEnabled() && CurPPLexer
&&
447 SourceMgr
.getLocForStartOfFile(CurPPLexer
->getFileID()) ==
448 CodeCompletionFileLoc
) {
449 assert(CurLexer
&& "Got EOF but no current lexer set!");
451 CurLexer
->FormTokenWithChars(Result
, CurLexer
->BufferEnd
, tok::eof
);
454 CurPPLexer
= nullptr;
455 recomputeCurLexerKind();
459 if (!isEndOfMacro
&& CurPPLexer
&&
460 (SourceMgr
.getIncludeLoc(CurPPLexer
->getFileID()).isValid() ||
461 // Predefines file doesn't have a valid include location.
462 (PredefinesFileID
.isValid() &&
463 CurPPLexer
->getFileID() == PredefinesFileID
))) {
464 // Notify SourceManager to record the number of FileIDs that were created
465 // during lexing of the #include'd file.
467 SourceMgr
.local_sloc_entry_size() -
468 CurPPLexer
->getInitialNumSLocEntries() + 1/*#include'd file*/;
469 SourceMgr
.setNumCreatedFIDsForFileID(CurPPLexer
->getFileID(), NumFIDs
);
472 bool ExitedFromPredefinesFile
= false;
474 if (!isEndOfMacro
&& CurPPLexer
) {
475 ExitedFID
= CurPPLexer
->getFileID();
477 assert(PredefinesFileID
.isValid() &&
478 "HandleEndOfFile is called before PredefinesFileId is set");
479 ExitedFromPredefinesFile
= (PredefinesFileID
== ExitedFID
);
482 if (LeavingSubmodule
) {
483 // We're done with this submodule.
484 Module
*M
= LeaveSubmodule(/*ForPragma*/false);
486 // Notify the parser that we've left the module.
487 const char *EndPos
= getCurLexerEndPos();
489 CurLexer
->BufferPtr
= EndPos
;
490 CurLexer
->FormTokenWithChars(Result
, EndPos
, tok::annot_module_end
);
491 Result
.setAnnotationEndLoc(Result
.getLocation());
492 Result
.setAnnotationValue(M
);
495 bool FoundPCHThroughHeader
= false;
496 if (CurPPLexer
&& creatingPCHWithThroughHeader() &&
498 SourceMgr
.getFileEntryForID(CurPPLexer
->getFileID())))
499 FoundPCHThroughHeader
= true;
501 // We're done with the #included file.
502 RemoveTopOfLexerStack();
504 // Propagate info about start-of-line/leading white-space/etc.
505 PropagateLineStartLeadingSpaceInfo(Result
);
507 // Notify the client, if desired, that we are in a new source file.
508 if (Callbacks
&& !isEndOfMacro
&& CurPPLexer
) {
509 SourceLocation Loc
= CurPPLexer
->getSourceLocation();
510 SrcMgr::CharacteristicKind FileType
=
511 SourceMgr
.getFileCharacteristic(Loc
);
512 Callbacks
->FileChanged(Loc
, PPCallbacks::ExitFile
, FileType
, ExitedFID
);
513 Callbacks
->LexedFileChanged(CurPPLexer
->getFileID(),
514 PPCallbacks::LexedFileChangeReason::ExitFile
,
515 FileType
, ExitedFID
, Loc
);
518 // Restore conditional stack as well as the recorded
519 // \#pragma clang assume_nonnull from the preamble right after exiting
520 // from the predefines file.
521 if (ExitedFromPredefinesFile
) {
522 replayPreambleConditionalStack();
523 if (PreambleRecordedPragmaAssumeNonNullLoc
.isValid())
524 PragmaAssumeNonNullLoc
= PreambleRecordedPragmaAssumeNonNullLoc
;
527 if (!isEndOfMacro
&& CurPPLexer
&& FoundPCHThroughHeader
&&
528 (isInPrimaryFile() ||
529 CurPPLexer
->getFileID() == getPredefinesFileID())) {
530 // Leaving the through header. Continue directly to end of main file
532 LeavingPCHThroughHeader
= true;
534 // Client should lex another token unless we generated an EOM.
535 return LeavingSubmodule
;
538 // If this is the end of the main file, form an EOF token.
539 assert(CurLexer
&& "Got EOF but no current lexer set!");
540 const char *EndPos
= getCurLexerEndPos();
542 CurLexer
->BufferPtr
= EndPos
;
544 if (getLangOpts().IncrementalExtensions
) {
545 CurLexer
->FormTokenWithChars(Result
, EndPos
, tok::annot_repl_input_end
);
546 Result
.setAnnotationEndLoc(Result
.getLocation());
547 Result
.setAnnotationValue(nullptr);
549 CurLexer
->FormTokenWithChars(Result
, EndPos
, tok::eof
);
552 if (isCodeCompletionEnabled()) {
553 // Inserting the code-completion point increases the source buffer by 1,
554 // but the main FileID was created before inserting the point.
555 // Compensate by reducing the EOF location by 1, otherwise the location
556 // will point to the next FileID.
557 // FIXME: This is hacky, the code-completion point should probably be
558 // inserted before the main FileID is created.
559 if (CurLexer
->getFileLoc() == CodeCompletionFileLoc
)
560 Result
.setLocation(Result
.getLocation().getLocWithOffset(-1));
563 if (creatingPCHWithThroughHeader() && !LeavingPCHThroughHeader
) {
564 // Reached the end of the compilation without finding the through header.
565 Diag(CurLexer
->getFileLoc(), diag::err_pp_through_header_not_seen
)
566 << PPOpts
->PCHThroughHeader
<< 0;
569 if (!isIncrementalProcessingEnabled())
570 // We're done with lexing.
573 if (!isIncrementalProcessingEnabled())
574 CurPPLexer
= nullptr;
576 if (TUKind
== TU_Complete
) {
577 // This is the end of the top-level file. 'WarnUnusedMacroLocs' has
578 // collected all macro locations that we need to warn because they are not
580 for (WarnUnusedMacroLocsTy::iterator
581 I
=WarnUnusedMacroLocs
.begin(), E
=WarnUnusedMacroLocs
.end();
583 Diag(*I
, diag::pp_macro_not_used
);
586 // If we are building a module that has an umbrella header, make sure that
587 // each of the headers within the directory, including all submodules, is
588 // covered by the umbrella header was actually included by the umbrella
590 if (Module
*Mod
= getCurrentModule()) {
591 llvm::SmallVector
<const Module
*, 4> AllMods
;
592 collectAllSubModulesWithUmbrellaHeader(*Mod
, AllMods
);
593 for (auto *M
: AllMods
)
594 diagnoseMissingHeaderInUmbrellaDir(*M
);
600 /// HandleEndOfTokenLexer - This callback is invoked when the current TokenLexer
601 /// hits the end of its token stream.
602 bool Preprocessor::HandleEndOfTokenLexer(Token
&Result
) {
603 assert(CurTokenLexer
&& !CurPPLexer
&&
604 "Ending a macro when currently in a #include file!");
606 if (!MacroExpandingLexersStack
.empty() &&
607 MacroExpandingLexersStack
.back().first
== CurTokenLexer
.get())
608 removeCachedMacroExpandedTokensOfLastLexer();
610 // Delete or cache the now-dead macro expander.
611 if (NumCachedTokenLexers
== TokenLexerCacheSize
)
612 CurTokenLexer
.reset();
614 TokenLexerCache
[NumCachedTokenLexers
++] = std::move(CurTokenLexer
);
616 // Handle this like a #include file being popped off the stack.
617 return HandleEndOfFile(Result
, true);
620 /// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the
621 /// lexer stack. This should only be used in situations where the current
622 /// state of the top-of-stack lexer is unknown.
623 void Preprocessor::RemoveTopOfLexerStack() {
624 assert(!IncludeMacroStack
.empty() && "Ran out of stack entries to load");
627 // Delete or cache the now-dead macro expander.
628 if (NumCachedTokenLexers
== TokenLexerCacheSize
)
629 CurTokenLexer
.reset();
631 TokenLexerCache
[NumCachedTokenLexers
++] = std::move(CurTokenLexer
);
634 PopIncludeMacroStack();
637 /// HandleMicrosoftCommentPaste - When the macro expander pastes together a
638 /// comment (/##/) in microsoft mode, this method handles updating the current
639 /// state, returning the token on the next source line.
640 void Preprocessor::HandleMicrosoftCommentPaste(Token
&Tok
) {
641 assert(CurTokenLexer
&& !CurPPLexer
&&
642 "Pasted comment can only be formed from macro");
643 // We handle this by scanning for the closest real lexer, switching it to
644 // raw mode and preprocessor mode. This will cause it to return \n as an
645 // explicit EOD token.
646 PreprocessorLexer
*FoundLexer
= nullptr;
647 bool LexerWasInPPMode
= false;
648 for (const IncludeStackInfo
&ISI
: llvm::reverse(IncludeMacroStack
)) {
649 if (ISI
.ThePPLexer
== nullptr) continue; // Scan for a real lexer.
651 // Once we find a real lexer, mark it as raw mode (disabling macro
652 // expansions) and preprocessor mode (return EOD). We know that the lexer
653 // was *not* in raw mode before, because the macro that the comment came
654 // from was expanded. However, it could have already been in preprocessor
655 // mode (#if COMMENT) in which case we have to return it to that mode and
657 FoundLexer
= ISI
.ThePPLexer
;
658 FoundLexer
->LexingRawMode
= true;
659 LexerWasInPPMode
= FoundLexer
->ParsingPreprocessorDirective
;
660 FoundLexer
->ParsingPreprocessorDirective
= true;
664 // Okay, we either found and switched over the lexer, or we didn't find a
665 // lexer. In either case, finish off the macro the comment came from, getting
667 if (!HandleEndOfTokenLexer(Tok
)) Lex(Tok
);
669 // Discarding comments as long as we don't have EOF or EOD. This 'comments
670 // out' the rest of the line, including any tokens that came from other macros
671 // that were active, as in:
672 // #define submacro a COMMENT b
674 // which should lex to 'a' only: 'b' and 'c' should be removed.
675 while (Tok
.isNot(tok::eod
) && Tok
.isNot(tok::eof
))
678 // If we got an eod token, then we successfully found the end of the line.
679 if (Tok
.is(tok::eod
)) {
680 assert(FoundLexer
&& "Can't get end of line without an active lexer");
681 // Restore the lexer back to normal mode instead of raw mode.
682 FoundLexer
->LexingRawMode
= false;
684 // If the lexer was already in preprocessor mode, just return the EOD token
685 // to finish the preprocessor line.
686 if (LexerWasInPPMode
) return;
688 // Otherwise, switch out of PP mode and return the next lexed token.
689 FoundLexer
->ParsingPreprocessorDirective
= false;
693 // If we got an EOF token, then we reached the end of the token stream but
694 // didn't find an explicit \n. This can only happen if there was no lexer
695 // active (an active lexer would return EOD at EOF if there was no \n in
696 // preprocessor directive mode), so just return EOF as our token.
697 assert(!FoundLexer
&& "Lexer should return EOD before EOF in PP mode");
700 void Preprocessor::EnterSubmodule(Module
*M
, SourceLocation ImportLoc
,
702 if (!getLangOpts().ModulesLocalVisibility
) {
703 // Just track that we entered this submodule.
704 BuildingSubmoduleStack
.push_back(
705 BuildingSubmoduleInfo(M
, ImportLoc
, ForPragma
, CurSubmoduleState
,
706 PendingModuleMacroNames
.size()));
708 Callbacks
->EnteredSubmodule(M
, ImportLoc
, ForPragma
);
712 // Resolve as much of the module definition as we can now, before we enter
713 // one of its headers.
714 // FIXME: Can we enable Complain here?
715 // FIXME: Can we do this when local visibility is disabled?
716 ModuleMap
&ModMap
= getHeaderSearchInfo().getModuleMap();
717 ModMap
.resolveExports(M
, /*Complain=*/false);
718 ModMap
.resolveUses(M
, /*Complain=*/false);
719 ModMap
.resolveConflicts(M
, /*Complain=*/false);
721 // If this is the first time we've entered this module, set up its state.
722 auto R
= Submodules
.insert(std::make_pair(M
, SubmoduleState()));
723 auto &State
= R
.first
->second
;
724 bool FirstTime
= R
.second
;
726 // Determine the set of starting macros for this submodule; take these
727 // from the "null" module (the predefines buffer).
729 // FIXME: If we have local visibility but not modules enabled, the
730 // NullSubmoduleState is polluted by #defines in the top-level source
732 auto &StartingMacros
= NullSubmoduleState
.Macros
;
734 // Restore to the starting state.
735 // FIXME: Do this lazily, when each macro name is first referenced.
736 for (auto &Macro
: StartingMacros
) {
737 // Skip uninteresting macros.
738 if (!Macro
.second
.getLatest() &&
739 Macro
.second
.getOverriddenMacros().empty())
742 MacroState
MS(Macro
.second
.getLatest());
743 MS
.setOverriddenMacros(*this, Macro
.second
.getOverriddenMacros());
744 State
.Macros
.insert(std::make_pair(Macro
.first
, std::move(MS
)));
748 // Track that we entered this module.
749 BuildingSubmoduleStack
.push_back(
750 BuildingSubmoduleInfo(M
, ImportLoc
, ForPragma
, CurSubmoduleState
,
751 PendingModuleMacroNames
.size()));
754 Callbacks
->EnteredSubmodule(M
, ImportLoc
, ForPragma
);
756 // Switch to this submodule as the current submodule.
757 CurSubmoduleState
= &State
;
759 // This module is visible to itself.
761 makeModuleVisible(M
, ImportLoc
);
764 bool Preprocessor::needModuleMacros() const {
765 // If we're not within a submodule, we never need to create ModuleMacros.
766 if (BuildingSubmoduleStack
.empty())
768 // If we are tracking module macro visibility even for textually-included
769 // headers, we need ModuleMacros.
770 if (getLangOpts().ModulesLocalVisibility
)
772 // Otherwise, we only need module macros if we're actually compiling a module
774 return getLangOpts().isCompilingModule();
777 Module
*Preprocessor::LeaveSubmodule(bool ForPragma
) {
778 if (BuildingSubmoduleStack
.empty() ||
779 BuildingSubmoduleStack
.back().IsPragma
!= ForPragma
) {
780 assert(ForPragma
&& "non-pragma module enter/leave mismatch");
784 auto &Info
= BuildingSubmoduleStack
.back();
786 Module
*LeavingMod
= Info
.M
;
787 SourceLocation ImportLoc
= Info
.ImportLoc
;
789 if (!needModuleMacros() ||
790 (!getLangOpts().ModulesLocalVisibility
&&
791 LeavingMod
->getTopLevelModuleName() != getLangOpts().CurrentModule
)) {
792 // If we don't need module macros, or this is not a module for which we
793 // are tracking macro visibility, don't build any, and preserve the list
794 // of pending names for the surrounding submodule.
795 BuildingSubmoduleStack
.pop_back();
798 Callbacks
->LeftSubmodule(LeavingMod
, ImportLoc
, ForPragma
);
800 makeModuleVisible(LeavingMod
, ImportLoc
);
804 // Create ModuleMacros for any macros defined in this submodule.
805 llvm::SmallPtrSet
<const IdentifierInfo
*, 8> VisitedMacros
;
806 for (unsigned I
= Info
.OuterPendingModuleMacroNames
;
807 I
!= PendingModuleMacroNames
.size(); ++I
) {
808 auto *II
= const_cast<IdentifierInfo
*>(PendingModuleMacroNames
[I
]);
809 if (!VisitedMacros
.insert(II
).second
)
812 auto MacroIt
= CurSubmoduleState
->Macros
.find(II
);
813 if (MacroIt
== CurSubmoduleState
->Macros
.end())
815 auto &Macro
= MacroIt
->second
;
817 // Find the starting point for the MacroDirective chain in this submodule.
818 MacroDirective
*OldMD
= nullptr;
819 auto *OldState
= Info
.OuterSubmoduleState
;
820 if (getLangOpts().ModulesLocalVisibility
)
821 OldState
= &NullSubmoduleState
;
822 if (OldState
&& OldState
!= CurSubmoduleState
) {
823 // FIXME: It'd be better to start at the state from when we most recently
824 // entered this submodule, but it doesn't really matter.
825 auto &OldMacros
= OldState
->Macros
;
826 auto OldMacroIt
= OldMacros
.find(II
);
827 if (OldMacroIt
== OldMacros
.end())
830 OldMD
= OldMacroIt
->second
.getLatest();
833 // This module may have exported a new macro. If so, create a ModuleMacro
834 // representing that fact.
835 bool ExplicitlyPublic
= false;
836 for (auto *MD
= Macro
.getLatest(); MD
!= OldMD
; MD
= MD
->getPrevious()) {
837 assert(MD
&& "broken macro directive chain");
839 if (auto *VisMD
= dyn_cast
<VisibilityMacroDirective
>(MD
)) {
840 // The latest visibility directive for a name in a submodule affects
841 // all the directives that come before it.
842 if (VisMD
->isPublic())
843 ExplicitlyPublic
= true;
844 else if (!ExplicitlyPublic
)
845 // Private with no following public directive: not exported.
848 MacroInfo
*Def
= nullptr;
849 if (DefMacroDirective
*DefMD
= dyn_cast
<DefMacroDirective
>(MD
))
850 Def
= DefMD
->getInfo();
852 // FIXME: Issue a warning if multiple headers for the same submodule
853 // define a macro, rather than silently ignoring all but the first.
855 // Don't bother creating a module macro if it would represent a #undef
856 // that doesn't override anything.
857 if (Def
|| !Macro
.getOverriddenMacros().empty())
858 addModuleMacro(LeavingMod
, II
, Def
,
859 Macro
.getOverriddenMacros(), IsNew
);
861 if (!getLangOpts().ModulesLocalVisibility
) {
862 // This macro is exposed to the rest of this compilation as a
863 // ModuleMacro; we don't need to track its MacroDirective any more.
864 Macro
.setLatest(nullptr);
865 Macro
.setOverriddenMacros(*this, {});
871 PendingModuleMacroNames
.resize(Info
.OuterPendingModuleMacroNames
);
873 // FIXME: Before we leave this submodule, we should parse all the other
874 // headers within it. Otherwise, we're left with an inconsistent state
875 // where we've made the module visible but don't yet have its complete
878 // Put back the outer module's state, if we're tracking it.
879 if (getLangOpts().ModulesLocalVisibility
)
880 CurSubmoduleState
= Info
.OuterSubmoduleState
;
882 BuildingSubmoduleStack
.pop_back();
885 Callbacks
->LeftSubmodule(LeavingMod
, ImportLoc
, ForPragma
);
887 // A nested #include makes the included submodule visible.
888 makeModuleVisible(LeavingMod
, ImportLoc
);