clang 23.0.0git
PPLexerChange.cpp
Go to the documentation of this file.
1//===--- PPLexerChange.cpp - Handle changing lexers in the preprocessor ---===//
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// This file implements pieces of the Preprocessor interface that manage the
10// current lexer stack.
11//
12//===----------------------------------------------------------------------===//
13
18#include "clang/Lex/MacroInfo.h"
21#include "llvm/ADT/StringSwitch.h"
22#include "llvm/Support/MemoryBufferRef.h"
23#include "llvm/Support/Path.h"
24#include <optional>
25
26using namespace clang;
27
28//===----------------------------------------------------------------------===//
29// Miscellaneous Methods.
30//===----------------------------------------------------------------------===//
31
32/// isInPrimaryFile - Return true if we're in the top-level file, not in a
33/// \#include. This looks through macro expansions and active _Pragma lexers.
35 if (IsFileLexer())
36 return IncludeMacroStack.empty();
37
38 // If there are any stacked lexers, we're in a #include.
39 assert(IsFileLexer(IncludeMacroStack[0]) &&
40 "Top level include stack isn't our primary lexer?");
41 return llvm::none_of(
42 llvm::drop_begin(IncludeMacroStack),
43 [&](const IncludeStackInfo &ISI) -> bool { return IsFileLexer(ISI); });
44}
45
46/// getCurrentLexer - Return the current file lexer being lexed from. Note
47/// that this ignores any potentially active macro expansions and _Pragma
48/// expansions going on at the time.
50 if (IsFileLexer())
51 return CurPPLexer;
52
53 // Look for a stacked lexer.
54 for (const IncludeStackInfo &ISI : llvm::reverse(IncludeMacroStack)) {
55 if (IsFileLexer(ISI))
56 return ISI.ThePPLexer;
57 }
58 return nullptr;
59}
60
61
62//===----------------------------------------------------------------------===//
63// Methods for Entering and Callbacks for leaving various contexts
64//===----------------------------------------------------------------------===//
65
66/// EnterSourceFile - Add a source file to the top of the include stack and
67/// start lexing tokens from it instead of the current buffer.
70 bool IsFirstIncludeOfFile) {
71 assert(!CurTokenLexer && "Cannot #include a file inside a macro!");
72 ++NumEnteredSourceFiles;
73
74 if (MaxIncludeStackDepth < IncludeMacroStack.size())
75 MaxIncludeStackDepth = IncludeMacroStack.size();
76
77 // Get the MemoryBuffer for this FID, if it fails, we fail.
78 std::optional<llvm::MemoryBufferRef> InputFile =
80 if (!InputFile) {
81 SourceLocation FileStart = SourceMgr.getLocForStartOfFile(FID);
82 Diag(Loc, diag::err_pp_error_opening_file)
83 << std::string(SourceMgr.getBufferName(FileStart)) << "";
84 return true;
85 }
86
88 SourceMgr.getFileEntryForID(FID) == CodeCompletionFile) {
89 CodeCompletionFileLoc = SourceMgr.getLocForStartOfFile(FID);
90 CodeCompletionLoc =
91 CodeCompletionFileLoc.getLocWithOffset(CodeCompletionOffset);
92 }
93
94 Lexer *TheLexer = new Lexer(FID, *InputFile, *this, IsFirstIncludeOfFile);
95 if (GetDependencyDirectives && FID != PredefinesFileID)
96 if (OptionalFileEntryRef File = SourceMgr.getFileEntryRefForID(FID))
97 if (auto MaybeDepDirectives = (*GetDependencyDirectives)(*File))
98 TheLexer->DepDirectives = *MaybeDepDirectives;
99
100 EnterSourceFileWithLexer(TheLexer, CurDir);
101 return false;
102}
103
104/// EnterSourceFileWithLexer - Add a source file to the top of the include stack
105/// and start lexing tokens from it instead of the current buffer.
106void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer,
107 ConstSearchDirIterator CurDir) {
108 PreprocessorLexer *PrevPPLexer = CurPPLexer;
109
110 // Add the current lexer to the include stack.
111 if (CurPPLexer || CurTokenLexer)
112 PushIncludeMacroStack();
113
114 CurLexer.reset(TheLexer);
115 CurPPLexer = TheLexer;
116 CurDirLookup = CurDir;
117 CurLexerSubmodule = nullptr;
118 CurLexerCallback = TheLexer->isDependencyDirectivesLexer()
119 ? CLK_DependencyDirectivesLexer
120 : CLK_Lexer;
121
122 // Notify the client, if desired, that we are in a new source file.
123 if (Callbacks && !CurLexer->Is_PragmaLexer) {
125 SourceMgr.getFileCharacteristic(CurLexer->getFileLoc());
126
127 FileID PrevFID;
128 SourceLocation EnterLoc;
129 if (PrevPPLexer) {
130 PrevFID = PrevPPLexer->getFileID();
131 EnterLoc = PrevPPLexer->getSourceLocation();
132 }
133 Callbacks->FileChanged(CurLexer->getFileLoc(), PPCallbacks::EnterFile,
134 FileType, PrevFID);
135 Callbacks->LexedFileChanged(CurLexer->getFileID(),
137 FileType, PrevFID, EnterLoc);
138 }
139}
140
141/// EnterMacro - Add a Macro to the top of the include stack and start lexing
142/// tokens from it instead of the current buffer.
144 MacroInfo *Macro, MacroArgs *Args) {
145 std::unique_ptr<TokenLexer> TokLexer;
146 if (NumCachedTokenLexers == 0) {
147 TokLexer = std::make_unique<TokenLexer>(Tok, ILEnd, Macro, Args, *this);
148 } else {
149 TokLexer = std::move(TokenLexerCache[--NumCachedTokenLexers]);
150 TokLexer->Init(Tok, ILEnd, Macro, Args);
151 }
152
153 PushIncludeMacroStack();
154 CurDirLookup = nullptr;
155 CurTokenLexer = std::move(TokLexer);
156 CurLexerCallback = CLK_TokenLexer;
157}
158
159/// EnterTokenStream - Add a "macro" context to the top of the include stack,
160/// which will cause the lexer to start returning the specified tokens.
161///
162/// If DisableMacroExpansion is true, tokens lexed from the token stream will
163/// not be subject to further macro expansion. Otherwise, these tokens will
164/// be re-macro-expanded when/if expansion is enabled.
165///
166/// If OwnsTokens is false, this method assumes that the specified stream of
167/// tokens has a permanent owner somewhere, so they do not need to be copied.
168/// If it is true, it assumes the array of tokens is allocated with new[] and
169/// must be freed.
170///
171void Preprocessor::EnterTokenStream(const Token *Toks, unsigned NumToks,
172 bool DisableMacroExpansion, bool OwnsTokens,
173 bool IsReinject) {
174 if (CurLexerCallback == CLK_CachingLexer) {
175 if (CachedLexPos < CachedTokens.size()) {
176 assert(IsReinject && "new tokens in the middle of cached stream");
177 // We're entering tokens into the middle of our cached token stream. We
178 // can't represent that, so just insert the tokens into the buffer.
179 CachedTokens.insert(CachedTokens.begin() + CachedLexPos,
180 Toks, Toks + NumToks);
181 if (OwnsTokens)
182 delete [] Toks;
183 return;
184 }
185
186 // New tokens are at the end of the cached token sequnece; insert the
187 // token stream underneath the caching lexer.
188 ExitCachingLexMode();
189 EnterTokenStream(Toks, NumToks, DisableMacroExpansion, OwnsTokens,
190 IsReinject);
191 EnterCachingLexMode();
192 return;
193 }
194
195 // Create a macro expander to expand from the specified token stream.
196 std::unique_ptr<TokenLexer> TokLexer;
197 if (NumCachedTokenLexers == 0) {
198 TokLexer = std::make_unique<TokenLexer>(
199 Toks, NumToks, DisableMacroExpansion, OwnsTokens, IsReinject, *this);
200 } else {
201 TokLexer = std::move(TokenLexerCache[--NumCachedTokenLexers]);
202 TokLexer->Init(Toks, NumToks, DisableMacroExpansion, OwnsTokens,
203 IsReinject);
204 }
205
206 // Save our current state.
207 PushIncludeMacroStack();
208 CurDirLookup = nullptr;
209 CurTokenLexer = std::move(TokLexer);
210 CurLexerCallback = CLK_TokenLexer;
211}
212
213/// Compute the relative path that names the given file relative to
214/// the given directory.
217 Result.clear();
218
219 StringRef FilePath = File.getDir().getName();
220 StringRef Path = FilePath;
221 while (!Path.empty()) {
222 if (auto CurDir = FM.getOptionalDirectoryRef(Path)) {
223 if (*CurDir == Dir) {
224 Result = FilePath.substr(Path.size());
225 llvm::sys::path::append(Result,
226 llvm::sys::path::filename(File.getName()));
227 return;
228 }
229 }
230
231 Path = llvm::sys::path::parent_path(Path);
232 }
233
234 Result = File.getName();
235}
236
237void Preprocessor::PropagateLineStartLeadingSpaceInfo(Token &Result) {
238 if (CurTokenLexer) {
239 CurTokenLexer->PropagateLineStartLeadingSpaceInfo(Result);
240 return;
241 }
242 if (CurLexer) {
243 CurLexer->PropagateLineStartLeadingSpaceInfo(Result);
244 return;
245 }
246 // FIXME: Handle other kinds of lexers? It generally shouldn't matter,
247 // but it might if they're empty?
248}
249
250/// Determine the location to use as the end of the buffer for a lexer.
251///
252/// If the file ends with a newline, form the EOF token on the newline itself,
253/// rather than "on the line following it", which doesn't exist. This makes
254/// diagnostics relating to the end of file include the last file that the user
255/// actually typed, which is goodness.
256const char *Preprocessor::getCurLexerEndPos() {
257 const char *EndPos = CurLexer->BufferEnd;
258 if (EndPos != CurLexer->BufferStart &&
259 (EndPos[-1] == '\n' || EndPos[-1] == '\r')) {
260 --EndPos;
261
262 // Handle \n\r and \r\n:
263 if (EndPos != CurLexer->BufferStart &&
264 (EndPos[-1] == '\n' || EndPos[-1] == '\r') &&
265 EndPos[-1] != EndPos[0])
266 --EndPos;
267 }
268
269 return EndPos;
270}
271
273 const Module &Mod, SmallVectorImpl<const Module *> &SubMods) {
275 SubMods.push_back(&Mod);
276 for (auto *M : Mod.submodules())
278}
279
280void Preprocessor::diagnoseMissingHeaderInUmbrellaDir(const Module &Mod) {
281 std::optional<Module::Header> UmbrellaHeader =
283 assert(UmbrellaHeader && "Module must use umbrella header");
284 const FileID &File = SourceMgr.translateFile(UmbrellaHeader->Entry);
285 SourceLocation ExpectedHeadersLoc = SourceMgr.getLocForEndOfFile(File);
286 if (getDiagnostics().isIgnored(diag::warn_uncovered_module_header,
287 ExpectedHeadersLoc))
288 return;
289
290 ModuleMap &ModMap = getHeaderSearchInfo().getModuleMap();
292 llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
293 std::error_code EC;
294 for (llvm::vfs::recursive_directory_iterator Entry(FS, Dir->getName(), EC),
295 End;
296 Entry != End && !EC; Entry.increment(EC)) {
297 using llvm::StringSwitch;
298
299 // Check whether this entry has an extension typically associated with
300 // headers.
301 if (!StringSwitch<bool>(llvm::sys::path::extension(Entry->path()))
302 .Cases({".h", ".H", ".hh", ".hpp"}, true)
303 .Default(false))
304 continue;
305
306 if (auto Header = getFileManager().getOptionalFileRef(Entry->path()))
307 if (!getSourceManager().hasFileInfo(*Header)) {
308 if (!ModMap.isHeaderInUnavailableModule(*Header)) {
309 // Find the relative path that would access this header.
310 SmallString<128> RelativePath;
311 computeRelativePath(FileMgr, *Dir, *Header, RelativePath);
312 Diag(ExpectedHeadersLoc, diag::warn_uncovered_module_header)
313 << Mod.getFullModuleName() << RelativePath;
314 }
315 }
316 }
317}
318
319/// HandleEndOfFile - This callback is invoked when the lexer hits the end of
320/// the current file. This either returns the EOF token or pops a level off
321/// the include stack and keeps going.
322bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) {
323 assert(!CurTokenLexer &&
324 "Ending a file when currently in a macro!");
325
326 SourceLocation UnclosedSafeBufferOptOutLoc;
327
328 if (IncludeMacroStack.empty() &&
329 isPPInSafeBufferOptOutRegion(UnclosedSafeBufferOptOutLoc)) {
330 // To warn if a "-Wunsafe-buffer-usage" opt-out region is still open by the
331 // end of a file.
332 Diag(UnclosedSafeBufferOptOutLoc,
333 diag::err_pp_unclosed_pragma_unsafe_buffer_usage);
334 }
335 // If we have an unclosed module region from a pragma at the end of a
336 // module, complain and close it now.
337 const bool LeavingSubmodule = CurLexer && CurLexerSubmodule;
338 if ((LeavingSubmodule || IncludeMacroStack.empty()) &&
339 !BuildingSubmoduleStack.empty() &&
340 BuildingSubmoduleStack.back().IsPragma) {
341 Diag(BuildingSubmoduleStack.back().ImportLoc,
342 diag::err_pp_module_begin_without_module_end);
343 Module *M = LeaveSubmodule(/*ForPragma*/true);
344
345 Result.startToken();
346 const char *EndPos = getCurLexerEndPos();
347 CurLexer->BufferPtr = EndPos;
348 CurLexer->FormTokenWithChars(Result, EndPos, tok::annot_module_end);
349 Result.setAnnotationEndLoc(Result.getLocation());
350 Result.setAnnotationValue(M);
351 return true;
352 }
353
354 // See if this file had a controlling macro.
355 if (CurPPLexer) { // Not ending a macro, ignore it.
356 if (const IdentifierInfo *ControllingMacro =
357 CurPPLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
358 // Okay, this has a controlling macro, remember in HeaderFileInfo.
359 if (OptionalFileEntryRef FE = CurPPLexer->getFileEntry()) {
360 HeaderInfo.SetFileControllingMacro(*FE, ControllingMacro);
361 if (MacroInfo *MI = getMacroInfo(ControllingMacro))
362 MI->setUsedForHeaderGuard(true);
363 if (const IdentifierInfo *DefinedMacro =
364 CurPPLexer->MIOpt.GetDefinedMacro()) {
365 if (!isMacroDefined(ControllingMacro) &&
366 DefinedMacro != ControllingMacro &&
367 CurLexer->isFirstTimeLexingFile()) {
368
369 // If the edit distance between the two macros is more than 50%,
370 // DefinedMacro may not be header guard, or can be header guard of
371 // another header file. Therefore, it maybe defining something
372 // completely different. This can be observed in the wild when
373 // handling feature macros or header guards in different files.
374
375 const StringRef ControllingMacroName = ControllingMacro->getName();
376 const StringRef DefinedMacroName = DefinedMacro->getName();
377 const size_t MaxHalfLength = std::max(ControllingMacroName.size(),
378 DefinedMacroName.size()) / 2;
379 const unsigned ED = ControllingMacroName.edit_distance(
380 DefinedMacroName, true, MaxHalfLength);
381 if (ED <= MaxHalfLength) {
382 // Emit a warning for a bad header guard.
383 Diag(CurPPLexer->MIOpt.GetMacroLocation(),
384 diag::warn_header_guard)
385 << CurPPLexer->MIOpt.GetMacroLocation() << ControllingMacro;
386 Diag(CurPPLexer->MIOpt.GetDefinedLocation(),
387 diag::note_header_guard)
388 << CurPPLexer->MIOpt.GetDefinedLocation() << DefinedMacro
389 << ControllingMacro
391 CurPPLexer->MIOpt.GetDefinedLocation(),
392 ControllingMacro->getName());
393 }
394 }
395 }
396 }
397 }
398 }
399
400 // Complain about reaching a true EOF within arc_cf_code_audited.
401 // We don't want to complain about reaching the end of a macro
402 // instantiation or a _Pragma.
403 if (PragmaARCCFCodeAuditedInfo.getLoc().isValid() && !isEndOfMacro &&
404 !(CurLexer && CurLexer->Is_PragmaLexer)) {
405 Diag(PragmaARCCFCodeAuditedInfo.getLoc(),
406 diag::err_pp_eof_in_arc_cf_code_audited);
407
408 // Recover by leaving immediately.
409 PragmaARCCFCodeAuditedInfo = IdentifierLoc();
410 }
411
412 // Complain about reaching a true EOF within assume_nonnull.
413 // We don't want to complain about reaching the end of a macro
414 // instantiation or a _Pragma.
415 if (PragmaAssumeNonNullLoc.isValid() &&
416 !isEndOfMacro && !(CurLexer && CurLexer->Is_PragmaLexer)) {
417 // If we're at the end of generating a preamble, we should record the
418 // unterminated \#pragma clang assume_nonnull so we can restore it later
419 // when the preamble is loaded into the main file.
421 PreambleRecordedPragmaAssumeNonNullLoc = PragmaAssumeNonNullLoc;
422 else
423 Diag(PragmaAssumeNonNullLoc, diag::err_pp_eof_in_assume_nonnull);
424 // Recover by leaving immediately.
425 PragmaAssumeNonNullLoc = SourceLocation();
426 }
427
428 bool LeavingPCHThroughHeader = false;
429
430 // If this is a #include'd file, pop it off the include stack and continue
431 // lexing the #includer file.
432 if (!IncludeMacroStack.empty()) {
433
434 // If we lexed the code-completion file, act as if we reached EOF.
435 if (isCodeCompletionEnabled() && CurPPLexer &&
436 SourceMgr.getLocForStartOfFile(CurPPLexer->getFileID()) ==
437 CodeCompletionFileLoc) {
438 assert(CurLexer && "Got EOF but no current lexer set!");
439 Result.startToken();
440 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
441 PendingDestroyLexers.push_back(std::move(CurLexer));
442
443 CurPPLexer = nullptr;
445 return true;
446 }
447
448 if (!isEndOfMacro && CurPPLexer &&
449 (SourceMgr.getIncludeLoc(CurPPLexer->getFileID()).isValid() ||
450 // Predefines file doesn't have a valid include location.
451 (PredefinesFileID.isValid() &&
452 CurPPLexer->getFileID() == PredefinesFileID))) {
453 // Notify SourceManager to record the number of FileIDs that were created
454 // during lexing of the #include'd file.
455 unsigned NumFIDs =
456 SourceMgr.local_sloc_entry_size() -
457 CurPPLexer->getInitialNumSLocEntries() + 1/*#include'd file*/;
458 SourceMgr.setNumCreatedFIDsForFileID(CurPPLexer->getFileID(), NumFIDs);
459 }
460
461 bool ExitedFromPredefinesFile = false;
462 FileID ExitedFID;
463 if (!isEndOfMacro && CurPPLexer) {
464 ExitedFID = CurPPLexer->getFileID();
465
466 assert(PredefinesFileID.isValid() &&
467 "HandleEndOfFile is called before PredefinesFileId is set");
468 ExitedFromPredefinesFile = (PredefinesFileID == ExitedFID);
469 }
470
471 if (LeavingSubmodule) {
472 // We're done with this submodule.
473 Module *M = LeaveSubmodule(/*ForPragma*/false);
474
475 // Notify the parser that we've left the module.
476 const char *EndPos = getCurLexerEndPos();
477 Result.startToken();
478 CurLexer->BufferPtr = EndPos;
479 CurLexer->FormTokenWithChars(Result, EndPos, tok::annot_module_end);
480 Result.setAnnotationEndLoc(Result.getLocation());
481 Result.setAnnotationValue(M);
482 }
483
484 bool FoundPCHThroughHeader = false;
485 if (CurPPLexer && creatingPCHWithThroughHeader() &&
487 SourceMgr.getFileEntryForID(CurPPLexer->getFileID())))
488 FoundPCHThroughHeader = true;
489
490 // We're done with the #included file.
492
493 // Propagate info about start-of-line/leading white-space/etc.
494 PropagateLineStartLeadingSpaceInfo(Result);
495
496 // Notify the client, if desired, that we are in a new source file.
497 if (Callbacks && !isEndOfMacro && CurPPLexer) {
498 SourceLocation Loc = CurPPLexer->getSourceLocation();
500 SourceMgr.getFileCharacteristic(Loc);
501 Callbacks->FileChanged(Loc, PPCallbacks::ExitFile, FileType, ExitedFID);
502 Callbacks->LexedFileChanged(CurPPLexer->getFileID(),
504 FileType, ExitedFID, Loc);
505 }
506
507 // Restore conditional stack as well as the recorded
508 // \#pragma clang assume_nonnull from the preamble right after exiting
509 // from the predefines file.
510 if (ExitedFromPredefinesFile) {
511 replayPreambleConditionalStack();
512 if (PreambleRecordedPragmaAssumeNonNullLoc.isValid())
513 PragmaAssumeNonNullLoc = PreambleRecordedPragmaAssumeNonNullLoc;
514 }
515
516 if (!isEndOfMacro && CurPPLexer && FoundPCHThroughHeader &&
517 (isInPrimaryFile() ||
518 CurPPLexer->getFileID() == getPredefinesFileID())) {
519 // Leaving the through header. Continue directly to end of main file
520 // processing.
521 LeavingPCHThroughHeader = true;
522 } else {
523 // Client should lex another token unless we generated an EOM.
524 return LeavingSubmodule;
525 }
526 }
527 // If this is the end of the main file, form an EOF token.
528 assert(CurLexer && "Got EOF but no current lexer set!");
529 const char *EndPos = getCurLexerEndPos();
530 Result.startToken();
531 CurLexer->BufferPtr = EndPos;
532
533 if (getLangOpts().IncrementalExtensions) {
534 CurLexer->FormTokenWithChars(Result, EndPos, tok::annot_repl_input_end);
535 Result.setAnnotationEndLoc(Result.getLocation());
536 Result.setAnnotationValue(nullptr);
537 } else {
538 CurLexer->FormTokenWithChars(Result, EndPos, tok::eof);
539 }
540
542 // Inserting the code-completion point increases the source buffer by 1,
543 // but the main FileID was created before inserting the point.
544 // Compensate by reducing the EOF location by 1, otherwise the location
545 // will point to the next FileID.
546 // FIXME: This is hacky, the code-completion point should probably be
547 // inserted before the main FileID is created.
548 if (CurLexer->getFileLoc() == CodeCompletionFileLoc)
549 Result.setLocation(Result.getLocation().getLocWithOffset(-1));
550 }
551
552 if (creatingPCHWithThroughHeader() && !LeavingPCHThroughHeader) {
553 // Reached the end of the compilation without finding the through header.
554 Diag(CurLexer->getFileLoc(), diag::err_pp_through_header_not_seen)
555 << PPOpts.PCHThroughHeader << 0;
556 }
557
559 // We're done with lexing. If we're inside a nested Lex call (LexLevel > 0),
560 // defer destruction of the lexer until Lex returns to avoid use-after-free
561 // when HandleEndOfFile is called from within Lexer methods that still need
562 // to access their members after this function returns.
563 if (LexLevel > 0 && CurLexer) {
564 PendingDestroyLexers.push_back(std::move(CurLexer));
565 } else {
566 CurLexer.reset();
567 }
568 }
569
571 CurPPLexer = nullptr;
572
573 if (TUKind == TU_Complete) {
574 // This is the end of the top-level file. 'WarnUnusedMacroLocs' has
575 // collected all macro locations that we need to warn because they are not
576 // used.
577 for (WarnUnusedMacroLocsTy::iterator
578 I=WarnUnusedMacroLocs.begin(), E=WarnUnusedMacroLocs.end();
579 I!=E; ++I)
580 Diag(*I, diag::pp_macro_not_used);
581 }
582
583 // If we are building a module that has an umbrella header, make sure that
584 // each of the headers within the directory, including all submodules, is
585 // covered by the umbrella header was actually included by the umbrella
586 // header.
587 if (Module *Mod = getCurrentModule()) {
590 for (auto *M : AllMods)
591 diagnoseMissingHeaderInUmbrellaDir(*M);
592 }
593
594 return true;
595}
596
597/// HandleEndOfTokenLexer - This callback is invoked when the current TokenLexer
598/// hits the end of its token stream.
600 assert(CurTokenLexer && !CurPPLexer &&
601 "Ending a macro when currently in a #include file!");
602
603 if (!MacroExpandingLexersStack.empty() &&
604 MacroExpandingLexersStack.back().first == CurTokenLexer.get())
605 removeCachedMacroExpandedTokensOfLastLexer();
606
607 // Delete or cache the now-dead macro expander.
608 if (NumCachedTokenLexers == TokenLexerCacheSize)
609 CurTokenLexer.reset();
610 else
611 TokenLexerCache[NumCachedTokenLexers++] = std::move(CurTokenLexer);
612
613 // Handle this like a #include file being popped off the stack.
614 return HandleEndOfFile(Result, true);
615}
616
617/// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the
618/// lexer stack. This should only be used in situations where the current
619/// state of the top-of-stack lexer is unknown.
621 assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load");
622
623 if (CurTokenLexer) {
624 // Delete or cache the now-dead macro expander.
625 if (NumCachedTokenLexers == TokenLexerCacheSize)
626 CurTokenLexer.reset();
627 else
628 TokenLexerCache[NumCachedTokenLexers++] = std::move(CurTokenLexer);
629 }
630
631 PopIncludeMacroStack();
632}
633
634/// HandleMicrosoftCommentPaste - When the macro expander pastes together a
635/// comment (/##/) in microsoft mode, this method handles updating the current
636/// state, returning the token on the next source line.
638 assert(CurTokenLexer && !CurPPLexer &&
639 "Pasted comment can only be formed from macro");
640 // We handle this by scanning for the closest real lexer, switching it to
641 // raw mode and preprocessor mode. This will cause it to return \n as an
642 // explicit EOD token.
643 PreprocessorLexer *FoundLexer = nullptr;
644 bool LexerWasInPPMode = false;
645 for (const IncludeStackInfo &ISI : llvm::reverse(IncludeMacroStack)) {
646 if (ISI.ThePPLexer == nullptr) continue; // Scan for a real lexer.
647
648 // Once we find a real lexer, mark it as raw mode (disabling macro
649 // expansions) and preprocessor mode (return EOD). We know that the lexer
650 // was *not* in raw mode before, because the macro that the comment came
651 // from was expanded. However, it could have already been in preprocessor
652 // mode (#if COMMENT) in which case we have to return it to that mode and
653 // return EOD.
654 FoundLexer = ISI.ThePPLexer;
655 FoundLexer->LexingRawMode = true;
656 LexerWasInPPMode = FoundLexer->ParsingPreprocessorDirective;
657 FoundLexer->ParsingPreprocessorDirective = true;
658 break;
659 }
660
661 // Okay, we either found and switched over the lexer, or we didn't find a
662 // lexer. In either case, finish off the macro the comment came from, getting
663 // the next token.
665
666 // Discarding comments as long as we don't have EOF or EOD. This 'comments
667 // out' the rest of the line, including any tokens that came from other macros
668 // that were active, as in:
669 // #define submacro a COMMENT b
670 // submacro c
671 // which should lex to 'a' only: 'b' and 'c' should be removed.
672 while (Tok.isNot(tok::eod) && Tok.isNot(tok::eof))
673 Lex(Tok);
674
675 // If we got an eod token, then we successfully found the end of the line.
676 if (Tok.is(tok::eod)) {
677 assert(FoundLexer && "Can't get end of line without an active lexer");
678 // Restore the lexer back to normal mode instead of raw mode.
679 FoundLexer->LexingRawMode = false;
680
681 // If the lexer was already in preprocessor mode, just return the EOD token
682 // to finish the preprocessor line.
683 if (LexerWasInPPMode) return;
684
685 // Otherwise, switch out of PP mode and return the next lexed token.
686 FoundLexer->ParsingPreprocessorDirective = false;
687 return Lex(Tok);
688 }
689
690 // If we got an EOF token, then we reached the end of the token stream but
691 // didn't find an explicit \n. This can only happen if there was no lexer
692 // active (an active lexer would return EOD at EOF if there was no \n in
693 // preprocessor directive mode), so just return EOF as our token.
694 assert(!FoundLexer && "Lexer should return EOD before EOF in PP mode");
695}
696
698 bool ForPragma) {
699 if (!getLangOpts().ModulesLocalVisibility) {
700 // Just track that we entered this submodule.
701 BuildingSubmoduleStack.push_back(
702 BuildingSubmoduleInfo(M, ImportLoc, ForPragma, CurSubmoduleState,
703 PendingModuleMacroNames.size()));
704 if (Callbacks)
705 Callbacks->EnteredSubmodule(M, ImportLoc, ForPragma);
706 return;
707 }
708
709 // Resolve as much of the module definition as we can now, before we enter
710 // one of its headers.
711 // FIXME: Can we enable Complain here?
712 // FIXME: Can we do this when local visibility is disabled?
714 ModMap.resolveExports(M, /*Complain=*/false);
715 ModMap.resolveUses(M, /*Complain=*/false);
716 ModMap.resolveConflicts(M, /*Complain=*/false);
717
718 // If this is the first time we've entered this module, set up its state.
719 auto R = Submodules.try_emplace(M);
720 auto &State = R.first->second;
721 bool FirstTime = R.second;
722 if (FirstTime) {
723 // Determine the set of starting macros for this submodule; take these
724 // from the "null" module (the predefines buffer).
725 //
726 // FIXME: If we have local visibility but not modules enabled, the
727 // NullSubmoduleState is polluted by #defines in the top-level source
728 // file.
729 auto &StartingMacros = NullSubmoduleState.Macros;
730
731 // Restore to the starting state.
732 // FIXME: Do this lazily, when each macro name is first referenced.
733 for (auto &Macro : StartingMacros) {
734 // Skip uninteresting macros.
735 if (!Macro.second.getLatest() &&
736 Macro.second.getOverriddenMacros().empty())
737 continue;
738
739 MacroState MS(Macro.second.getLatest());
740 MS.setOverriddenMacros(*this, Macro.second.getOverriddenMacros());
741 State.Macros.insert(std::make_pair(Macro.first, std::move(MS)));
742 }
743 }
744
745 // Track that we entered this module.
746 BuildingSubmoduleStack.push_back(
747 BuildingSubmoduleInfo(M, ImportLoc, ForPragma, CurSubmoduleState,
748 PendingModuleMacroNames.size()));
749
750 if (Callbacks)
751 Callbacks->EnteredSubmodule(M, ImportLoc, ForPragma);
752
753 // Switch to this submodule as the current submodule.
754 CurSubmoduleState = &State;
755
756 // This module is visible to itself, but exports should not be made visible
757 // until they are imported.
758 if (FirstTime)
759 makeModuleVisible(M, ImportLoc, /*IncludeExports=*/false);
760}
761
762bool Preprocessor::needModuleMacros() const {
763 // If we're not within a submodule, we never need to create ModuleMacros.
764 if (BuildingSubmoduleStack.empty())
765 return false;
766 // If we are tracking module macro visibility even for textually-included
767 // headers, we need ModuleMacros.
768 if (getLangOpts().ModulesLocalVisibility)
769 return true;
770 // Otherwise, we only need module macros if we're actually compiling a module
771 // interface.
773}
774
776 if (BuildingSubmoduleStack.empty() ||
777 BuildingSubmoduleStack.back().IsPragma != ForPragma) {
778 assert(ForPragma && "non-pragma module enter/leave mismatch");
779 return nullptr;
780 }
781
782 auto &Info = BuildingSubmoduleStack.back();
783
784 Module *LeavingMod = Info.M;
785 SourceLocation ImportLoc = Info.ImportLoc;
786
787 if (!needModuleMacros() ||
788 (!getLangOpts().ModulesLocalVisibility &&
789 LeavingMod->getTopLevelModuleName() != getLangOpts().CurrentModule)) {
790 // If we don't need module macros, or this is not a module for which we
791 // are tracking macro visibility, don't build any, and preserve the list
792 // of pending names for the surrounding submodule.
793 BuildingSubmoduleStack.pop_back();
794
795 if (Callbacks)
796 Callbacks->LeftSubmodule(LeavingMod, ImportLoc, ForPragma);
797
798 makeModuleVisible(LeavingMod, ImportLoc);
799 return LeavingMod;
800 }
801
802 // Create ModuleMacros for any macros defined in this submodule.
804 for (unsigned I = Info.OuterPendingModuleMacroNames;
805 I != PendingModuleMacroNames.size(); ++I) {
806 auto *II = PendingModuleMacroNames[I];
807 if (!VisitedMacros.insert(II).second)
808 continue;
809
810 auto MacroIt = CurSubmoduleState->Macros.find(II);
811 if (MacroIt == CurSubmoduleState->Macros.end())
812 continue;
813 auto &Macro = MacroIt->second;
814
815 // Find the starting point for the MacroDirective chain in this submodule.
816 MacroDirective *OldMD = nullptr;
817 auto *OldState = Info.OuterSubmoduleState;
818 if (getLangOpts().ModulesLocalVisibility)
819 OldState = &NullSubmoduleState;
820 if (OldState && OldState != CurSubmoduleState) {
821 // FIXME: It'd be better to start at the state from when we most recently
822 // entered this submodule, but it doesn't really matter.
823 auto &OldMacros = OldState->Macros;
824 auto OldMacroIt = OldMacros.find(II);
825 if (OldMacroIt == OldMacros.end())
826 OldMD = nullptr;
827 else
828 OldMD = OldMacroIt->second.getLatest();
829 }
830
831 // This module may have exported a new macro. If so, create a ModuleMacro
832 // representing that fact.
833 bool ExplicitlyPublic = false;
834 for (auto *MD = Macro.getLatest(); MD != OldMD; MD = MD->getPrevious()) {
835 assert(MD && "broken macro directive chain");
836
837 if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
838 // The latest visibility directive for a name in a submodule affects
839 // all the directives that come before it.
840 if (VisMD->isPublic())
841 ExplicitlyPublic = true;
842 else if (!ExplicitlyPublic)
843 // Private with no following public directive: not exported.
844 break;
845 } else {
846 MacroInfo *Def = nullptr;
847 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD))
848 Def = DefMD->getInfo();
849
850 // FIXME: Issue a warning if multiple headers for the same submodule
851 // define a macro, rather than silently ignoring all but the first.
852 bool IsNew;
853 // Don't bother creating a module macro if it would represent a #undef
854 // that doesn't override anything.
855 if (Def || !Macro.getOverriddenMacros().empty())
856 addModuleMacro(LeavingMod, II, Def, Macro.getOverriddenMacros(),
857 IsNew);
858
859 if (!getLangOpts().ModulesLocalVisibility) {
860 // This macro is exposed to the rest of this compilation as a
861 // ModuleMacro; we don't need to track its MacroDirective any more.
862 Macro.setLatest(nullptr);
863 Macro.setOverriddenMacros(*this, {});
864 }
865 break;
866 }
867 }
868 }
869 PendingModuleMacroNames.resize(Info.OuterPendingModuleMacroNames);
870
871 // FIXME: Before we leave this submodule, we should parse all the other
872 // headers within it. Otherwise, we're left with an inconsistent state
873 // where we've made the module visible but don't yet have its complete
874 // contents.
875
876 // Put back the outer module's state, if we're tracking it.
877 if (getLangOpts().ModulesLocalVisibility)
878 CurSubmoduleState = Info.OuterSubmoduleState;
879
880 BuildingSubmoduleStack.pop_back();
881
882 if (Callbacks)
883 Callbacks->LeftSubmodule(LeavingMod, ImportLoc, ForPragma);
884
885 // A nested #include makes the included submodule visible.
886 makeModuleVisible(LeavingMod, ImportLoc);
887 return LeavingMod;
888}
Defines the clang::FileManager interface and associated types.
Token Tok
The Token.
static StringRef computeRelativePath(StringRef FileName, StringRef DirPath)
Compute relative path from DirPath to FileName by stripping the DirPath prefix.
llvm::MachO::FileType FileType
Definition MachO.h:46
Defines the clang::MacroInfo and clang::MacroDirective classes.
static void collectAllSubModulesWithUmbrellaHeader(const Module &Mod, SmallVectorImpl< const Module * > &SubMods)
Defines the clang::Preprocessor interface.
Defines the SourceManager interface.
A directive for a defined macro or a macro imported from a module.
Definition MacroInfo.h:432
StringRef getName() const
Cached information about one directory (either on disk or in the virtual file system).
A reference to a FileEntry that includes the name of the file as it was accessed by the FileManager's...
Definition FileEntry.h:57
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Implements support for file system lookup, file system caching, and directory search management.
Definition FileManager.h:53
OptionalDirectoryEntryRef getOptionalDirectoryRef(StringRef DirName, bool CacheFailure=true)
Get a DirectoryEntryRef if it exists, without doing anything on error.
static FixItHint CreateReplacement(CharSourceRange RemoveRange, StringRef Code)
Create a code modification hint that replaces the given source range with the given code string.
Definition Diagnostic.h:141
ModuleMap & getModuleMap()
Retrieve the module map.
One of these records is kept for each identifier that is lexed.
A simple pair of identifier info and location.
bool isCompilingModule() const
Are we compiling a module?
Lexer - This provides a simple interface that turns a text buffer into a stream of tokens.
Definition Lexer.h:78
Encapsulates changes to the "macros namespace" (the location where the macro name became active,...
Definition MacroInfo.h:313
const MacroDirective * getPrevious() const
Get previous definition of the macro with the same name.
Definition MacroInfo.h:354
Encapsulates the data about a macro definition (e.g.
Definition MacroInfo.h:39
bool resolveExports(Module *Mod, bool Complain)
Resolve all of the unresolved exports in the given module.
bool resolveConflicts(Module *Mod, bool Complain)
Resolve all of the unresolved conflicts in the given module.
bool isHeaderInUnavailableModule(FileEntryRef Header) const
Determine whether the given header is part of a module marked 'unavailable'.
bool resolveUses(Module *Mod, bool Complain)
Resolve all of the unresolved uses in the given module.
Describes a module or submodule.
Definition Module.h:246
StringRef getTopLevelModuleName() const
Retrieve the name of the top-level module.
Definition Module.h:838
llvm::iterator_range< submodule_iterator > submodules()
Definition Module.h:955
std::optional< Header > getUmbrellaHeaderAsWritten() const
Retrieve the umbrella header as written.
Definition Module.h:873
std::string getFullModuleName(bool AllowStringLiterals=false) const
Retrieve the full name of this module, including the path from its top-level module.
Definition Module.cpp:259
OptionalDirectoryEntryRef getEffectiveUmbrellaDir() const
Get the effective umbrella directory for this module: either the one explicitly written in the module...
Definition Module.cpp:283
bool LexingRawMode
True if in raw mode.
bool ParsingPreprocessorDirective
True when parsing #XXX; turns '\n' into a tok::eod token.
virtual SourceLocation getSourceLocation()=0
Return the source location for the next observable location.
bool creatingPCHWithThroughHeader()
True if creating a PCH with a through header.
ModuleMacro * addModuleMacro(Module *Mod, IdentifierInfo *II, MacroInfo *Macro, ArrayRef< ModuleMacro * > Overrides, bool &IsNew)
Register an exported macro for a module and identifier.
bool isIncrementalProcessingEnabled() const
Returns true if incremental processing is enabled.
const MacroInfo * getMacroInfo(const IdentifierInfo *II) const
bool isRecordingPreamble() const
bool isInPrimaryFile() const
Return true if we're in the top-level file, not in a #include.
void EnterSubmodule(Module *M, SourceLocation ImportLoc, bool ForPragma)
friend class MacroArgs
bool HandleEndOfTokenLexer(Token &Result)
Callback invoked when the current TokenLexer hits the end of its token stream.
void Lex(Token &Result)
Lex the next token for this preprocessor.
const TranslationUnitKind TUKind
The kind of translation unit we are processing.
bool EnterSourceFile(FileID FID, ConstSearchDirIterator Dir, SourceLocation Loc, bool IsFirstIncludeOfFile=true)
Add a source file to the top of the include stack and start lexing tokens from it instead of the curr...
SourceManager & getSourceManager() const
bool isMacroDefined(StringRef Id)
Module * getCurrentModule()
Retrieves the module that we're currently building, if any.
void makeModuleVisible(Module *M, SourceLocation Loc, bool IncludeExports=true)
FileManager & getFileManager() const
bool isPCHThroughHeader(const FileEntry *FE)
Returns true if the FileEntry is the PCH through header.
FileID getPredefinesFileID() const
Returns the FileID for the preprocessor predefines.
bool isCodeCompletionEnabled() const
Determine if we are performing code completion.
PreprocessorLexer * getCurrentFileLexer() const
Return the current file lexer being lexed from.
HeaderSearch & getHeaderSearchInfo() const
Module * LeaveSubmodule(bool ForPragma)
void recomputeCurLexerKind()
Recompute the current lexer kind based on the CurLexer/ CurTokenLexer pointers.
const LangOptions & getLangOpts() const
void RemoveTopOfLexerStack()
Pop the current lexer/macro exp off the top of the lexer stack.
bool HandleEndOfFile(Token &Result, bool isEndOfMacro=false)
Callback invoked when the lexer hits the end of the current file.
DiagnosticsEngine & getDiagnostics() const
void EnterMacro(Token &Tok, SourceLocation ILEnd, MacroInfo *Macro, MacroArgs *Args)
Add a Macro to the top of the include stack and start lexing tokens from it instead of the current bu...
DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const
Forwarding function for diagnostics.
void HandleMicrosoftCommentPaste(Token &Tok)
When the macro expander pastes together a comment (/##/) in Microsoft mode, this method handles updat...
Encodes a location in the source.
std::optional< llvm::MemoryBufferRef > getBufferOrNone(FileID FID, SourceLocation Loc=SourceLocation()) const
Return the buffer for the specified FileID.
Token - This structure provides full information about a lexed token.
Definition Token.h:36
CharacteristicKind
Indicates whether a file or directory holds normal user code, system code, or system code which is im...
The JSON file list parser is used to communicate input to InstallAPI.
CustomizableOptional< FileEntryRef > OptionalFileEntryRef
Definition FileEntry.h:208
detail::SearchDirIteratorImpl< true > ConstSearchDirIterator
@ Result
The result type of a method or function.
Definition TypeBase.h:905
@ TU_Complete
The translation unit is a complete translation unit.
CustomizableOptional< DirectoryEntryRef > OptionalDirectoryEntryRef
SmallVector< Token, 4 > CachedTokens
A set of tokens that has been cached for later parsing.
Definition DeclSpec.h:1250