21#include "llvm/ADT/StringSwitch.h"
22#include "llvm/Support/MemoryBufferRef.h"
23#include "llvm/Support/Path.h"
36 return IncludeMacroStack.empty();
39 assert(IsFileLexer(IncludeMacroStack[0]) &&
40 "Top level include stack isn't our primary lexer?");
42 llvm::drop_begin(IncludeMacroStack),
43 [&](
const IncludeStackInfo &ISI) ->
bool {
return IsFileLexer(ISI); });
54 for (
const IncludeStackInfo &ISI : llvm::reverse(IncludeMacroStack)) {
56 return ISI.ThePPLexer;
70 bool IsFirstIncludeOfFile) {
71 assert(!CurTokenLexer &&
"Cannot #include a file inside a macro!");
72 ++NumEnteredSourceFiles;
74 if (MaxIncludeStackDepth < IncludeMacroStack.size())
75 MaxIncludeStackDepth = IncludeMacroStack.size();
78 std::optional<llvm::MemoryBufferRef> InputFile =
82 Diag(Loc, diag::err_pp_error_opening_file)
83 << std::string(SourceMgr.getBufferName(FileStart)) <<
"";
88 SourceMgr.getFileEntryForID(FID) == CodeCompletionFile) {
89 CodeCompletionFileLoc = SourceMgr.getLocForStartOfFile(FID);
91 CodeCompletionFileLoc.getLocWithOffset(CodeCompletionOffset);
95 std::make_unique<Lexer>(FID, *InputFile, *
this, IsFirstIncludeOfFile);
96 if (GetDependencyDirectives && FID != PredefinesFileID)
98 if (
auto MaybeDepDirectives = (*GetDependencyDirectives)(*
File))
99 TheLexer->DepDirectives = *MaybeDepDirectives;
101 EnterSourceFileWithLexer(std::move(TheLexer), CurDir);
107void Preprocessor::EnterSourceFileWithLexer(std::unique_ptr<Lexer> TheLexer,
112 if (CurPPLexer || CurTokenLexer)
113 PushIncludeMacroStack();
115 CurLexer = std::move(TheLexer);
116 CurPPLexer = CurLexer.get();
117 CurDirLookup = CurDir;
118 CurLexerSubmodule =
nullptr;
119 CurLexerCallback = CurLexer->isDependencyDirectivesLexer()
120 ? CLK_DependencyDirectivesLexer
124 if (Callbacks && !CurLexer->Is_PragmaLexer) {
126 SourceMgr.getFileCharacteristic(CurLexer->getFileLoc());
136 Callbacks->LexedFileChanged(CurLexer->getFileID(),
146 std::unique_ptr<TokenLexer> TokLexer;
147 if (NumCachedTokenLexers == 0) {
148 TokLexer = std::make_unique<TokenLexer>(
Tok, ILEnd,
Macro, Args, *
this);
150 TokLexer = std::move(TokenLexerCache[--NumCachedTokenLexers]);
151 TokLexer->Init(
Tok, ILEnd,
Macro, Args);
154 PushIncludeMacroStack();
155 CurDirLookup =
nullptr;
156 CurTokenLexer = std::move(TokLexer);
157 CurLexerCallback = CLK_TokenLexer;
172void Preprocessor::EnterTokenStream(
const Token *Toks,
unsigned NumToks,
173 bool DisableMacroExpansion,
bool OwnsTokens,
175 if (CurLexerCallback == CLK_CachingLexer) {
177 assert(IsReinject &&
"new tokens in the middle of cached stream");
181 Toks, Toks + NumToks);
189 ExitCachingLexMode();
190 EnterTokenStream(Toks, NumToks, DisableMacroExpansion, OwnsTokens,
192 EnterCachingLexMode();
197 std::unique_ptr<TokenLexer> TokLexer;
198 if (NumCachedTokenLexers == 0) {
199 TokLexer = std::make_unique<TokenLexer>(
200 Toks, NumToks, DisableMacroExpansion, OwnsTokens, IsReinject, *
this);
202 TokLexer = std::move(TokenLexerCache[--NumCachedTokenLexers]);
203 TokLexer->Init(Toks, NumToks, DisableMacroExpansion, OwnsTokens,
208 PushIncludeMacroStack();
209 CurDirLookup =
nullptr;
210 CurTokenLexer = std::move(TokLexer);
211 CurLexerCallback = CLK_TokenLexer;
220 StringRef FilePath =
File.getDir().getName();
221 StringRef Path = FilePath;
222 while (!Path.empty()) {
224 if (*CurDir == Dir) {
225 Result = FilePath.substr(Path.size());
226 llvm::sys::path::append(
Result,
227 llvm::sys::path::filename(
File.getName()));
232 Path = llvm::sys::path::parent_path(Path);
238void Preprocessor::PropagateLineStartLeadingSpaceInfo(
Token &
Result) {
240 CurTokenLexer->PropagateLineStartLeadingSpaceInfo(
Result);
244 CurLexer->PropagateLineStartLeadingSpaceInfo(
Result);
257const char *Preprocessor::getCurLexerEndPos() {
258 const char *EndPos = CurLexer->BufferEnd;
259 if (EndPos != CurLexer->BufferStart &&
260 (EndPos[-1] ==
'\n' || EndPos[-1] ==
'\r')) {
264 if (EndPos != CurLexer->BufferStart &&
265 (EndPos[-1] ==
'\n' || EndPos[-1] ==
'\r') &&
266 EndPos[-1] != EndPos[0])
276 SubMods.push_back(&Mod);
281void Preprocessor::diagnoseMissingHeaderInUmbrellaDir(
const Module &Mod) {
282 std::optional<Module::Header> UmbrellaHeader =
284 assert(UmbrellaHeader &&
"Module must use umbrella header");
285 const FileID &
File = SourceMgr.translateFile(UmbrellaHeader->Entry);
286 SourceLocation ExpectedHeadersLoc = SourceMgr.getLocForEndOfFile(
File);
287 if (
getDiagnostics().isIgnored(diag::warn_uncovered_module_header,
293 llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
295 for (llvm::vfs::recursive_directory_iterator Entry(FS, Dir->
getName(), EC),
297 Entry != End && !EC; Entry.increment(EC)) {
298 using llvm::StringSwitch;
302 if (!StringSwitch<bool>(llvm::sys::path::extension(Entry->path()))
303 .Cases({
".h",
".H",
".hh",
".hpp"},
true)
307 if (
auto Header =
getFileManager().getOptionalFileRef(Entry->path()))
311 SmallString<128> RelativePath;
313 Diag(ExpectedHeadersLoc, diag::warn_uncovered_module_header)
324 assert(!CurTokenLexer &&
325 "Ending a file when currently in a macro!");
329 if (IncludeMacroStack.empty() &&
333 Diag(UnclosedSafeBufferOptOutLoc,
334 diag::err_pp_unclosed_pragma_unsafe_buffer_usage);
338 const bool LeavingSubmodule = CurLexer && CurLexerSubmodule;
339 if ((LeavingSubmodule || IncludeMacroStack.empty()) &&
340 !BuildingSubmoduleStack.empty() &&
341 BuildingSubmoduleStack.back().IsPragma) {
342 Diag(BuildingSubmoduleStack.back().ImportLoc,
343 diag::err_pp_module_begin_without_module_end);
347 const char *EndPos = getCurLexerEndPos();
348 CurLexer->BufferPtr = EndPos;
349 CurLexer->FormTokenWithChars(
Result, EndPos, tok::annot_module_end);
351 Result.setAnnotationValue(M);
358 CurPPLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
361 HeaderInfo.SetFileControllingMacro(*FE, ControllingMacro);
363 MI->setUsedForHeaderGuard(
true);
365 CurPPLexer->MIOpt.GetDefinedMacro()) {
367 DefinedMacro != ControllingMacro &&
368 CurLexer->isFirstTimeLexingFile()) {
376 const StringRef ControllingMacroName = ControllingMacro->getName();
377 const StringRef DefinedMacroName = DefinedMacro->getName();
378 const size_t MaxHalfLength = std::max(ControllingMacroName.size(),
379 DefinedMacroName.size()) / 2;
380 const unsigned ED = ControllingMacroName.edit_distance(
381 DefinedMacroName,
true, MaxHalfLength);
382 if (ED <= MaxHalfLength) {
384 Diag(CurPPLexer->MIOpt.GetMacroLocation(),
385 diag::warn_header_guard)
386 << CurPPLexer->MIOpt.GetMacroLocation() << ControllingMacro;
387 Diag(CurPPLexer->MIOpt.GetDefinedLocation(),
388 diag::note_header_guard)
389 << CurPPLexer->MIOpt.GetDefinedLocation() << DefinedMacro
392 CurPPLexer->MIOpt.GetDefinedLocation(),
393 ControllingMacro->getName());
404 if (PragmaARCCFCodeAuditedInfo.getLoc().isValid() && !isEndOfMacro &&
405 !(CurLexer && CurLexer->Is_PragmaLexer)) {
406 Diag(PragmaARCCFCodeAuditedInfo.getLoc(),
407 diag::err_pp_eof_in_arc_cf_code_audited);
416 if (PragmaAssumeNonNullLoc.isValid() &&
417 !isEndOfMacro && !(CurLexer && CurLexer->Is_PragmaLexer)) {
422 PreambleRecordedPragmaAssumeNonNullLoc = PragmaAssumeNonNullLoc;
424 Diag(PragmaAssumeNonNullLoc, diag::err_pp_eof_in_assume_nonnull);
429 bool LeavingPCHThroughHeader =
false;
433 if (!IncludeMacroStack.empty()) {
437 SourceMgr.getLocForStartOfFile(CurPPLexer->getFileID()) ==
438 CodeCompletionFileLoc) {
439 assert(CurLexer &&
"Got EOF but no current lexer set!");
441 CurLexer->FormTokenWithChars(
Result, CurLexer->BufferEnd, tok::eof);
442 PendingDestroyLexers.push_back(std::move(CurLexer));
444 CurPPLexer =
nullptr;
449 if (!isEndOfMacro && CurPPLexer &&
450 (SourceMgr.getIncludeLoc(CurPPLexer->getFileID()).isValid() ||
452 (PredefinesFileID.isValid() &&
453 CurPPLexer->getFileID() == PredefinesFileID))) {
457 SourceMgr.local_sloc_entry_size() -
458 CurPPLexer->getInitialNumSLocEntries() + 1;
459 SourceMgr.setNumCreatedFIDsForFileID(CurPPLexer->getFileID(), NumFIDs);
462 bool ExitedFromPredefinesFile =
false;
464 if (!isEndOfMacro && CurPPLexer) {
465 ExitedFID = CurPPLexer->getFileID();
467 assert(PredefinesFileID.isValid() &&
468 "HandleEndOfFile is called before PredefinesFileId is set");
469 ExitedFromPredefinesFile = (PredefinesFileID == ExitedFID);
472 if (LeavingSubmodule) {
477 const char *EndPos = getCurLexerEndPos();
479 CurLexer->BufferPtr = EndPos;
480 CurLexer->FormTokenWithChars(
Result, EndPos, tok::annot_module_end);
482 Result.setAnnotationValue(M);
485 bool FoundPCHThroughHeader =
false;
488 SourceMgr.getFileEntryForID(CurPPLexer->getFileID())))
489 FoundPCHThroughHeader =
true;
495 PropagateLineStartLeadingSpaceInfo(
Result);
498 if (Callbacks && !isEndOfMacro && CurPPLexer) {
501 SourceMgr.getFileCharacteristic(Loc);
503 Callbacks->LexedFileChanged(CurPPLexer->getFileID(),
505 FileType, ExitedFID, Loc);
511 if (ExitedFromPredefinesFile) {
512 replayPreambleConditionalStack();
513 if (PreambleRecordedPragmaAssumeNonNullLoc.isValid())
514 PragmaAssumeNonNullLoc = PreambleRecordedPragmaAssumeNonNullLoc;
517 if (!isEndOfMacro && CurPPLexer && FoundPCHThroughHeader &&
522 LeavingPCHThroughHeader =
true;
525 return LeavingSubmodule;
529 assert(CurLexer &&
"Got EOF but no current lexer set!");
530 const char *EndPos = getCurLexerEndPos();
532 CurLexer->BufferPtr = EndPos;
535 CurLexer->FormTokenWithChars(
Result, EndPos, tok::annot_repl_input_end);
537 Result.setAnnotationValue(
nullptr);
539 CurLexer->FormTokenWithChars(
Result, EndPos, tok::eof);
549 if (CurLexer->getFileLoc() == CodeCompletionFileLoc)
550 Result.setLocation(
Result.getLocation().getLocWithOffset(-1));
555 Diag(CurLexer->getFileLoc(), diag::err_pp_through_header_not_seen)
556 << PPOpts.PCHThroughHeader << 0;
564 if (LexLevel > 0 && CurLexer) {
565 PendingDestroyLexers.push_back(std::move(CurLexer));
572 CurPPLexer =
nullptr;
578 for (WarnUnusedMacroLocsTy::iterator
579 I=WarnUnusedMacroLocs.begin(), E=WarnUnusedMacroLocs.end();
581 Diag(*I, diag::pp_macro_not_used);
591 for (
auto *M : AllMods)
592 diagnoseMissingHeaderInUmbrellaDir(*M);
601 assert(CurTokenLexer && !CurPPLexer &&
602 "Ending a macro when currently in a #include file!");
604 if (!MacroExpandingLexersStack.empty() &&
605 MacroExpandingLexersStack.back().first == CurTokenLexer.get())
606 removeCachedMacroExpandedTokensOfLastLexer();
609 if (NumCachedTokenLexers == TokenLexerCacheSize)
610 CurTokenLexer.reset();
612 TokenLexerCache[NumCachedTokenLexers++] = std::move(CurTokenLexer);
622 assert(!IncludeMacroStack.empty() &&
"Ran out of stack entries to load");
626 if (NumCachedTokenLexers == TokenLexerCacheSize)
627 CurTokenLexer.reset();
629 TokenLexerCache[NumCachedTokenLexers++] = std::move(CurTokenLexer);
632 PopIncludeMacroStack();
639 assert(CurTokenLexer && !CurPPLexer &&
640 "Pasted comment can only be formed from macro");
645 bool LexerWasInPPMode =
false;
646 for (
const IncludeStackInfo &ISI : llvm::reverse(IncludeMacroStack)) {
647 if (ISI.ThePPLexer ==
nullptr)
continue;
655 FoundLexer = ISI.ThePPLexer;
673 while (
Tok.isNot(tok::eod) &&
Tok.isNot(tok::eof))
677 if (
Tok.is(tok::eod)) {
678 assert(FoundLexer &&
"Can't get end of line without an active lexer");
684 if (LexerWasInPPMode)
return;
695 assert(!FoundLexer &&
"Lexer should return EOD before EOF in PP mode");
702 BuildingSubmoduleStack.push_back(
703 BuildingSubmoduleInfo(M, ImportLoc, ForPragma, CurSubmoduleState,
704 PendingModuleMacroNames.size()));
706 Callbacks->EnteredSubmodule(M, ImportLoc, ForPragma);
720 auto R = Submodules.try_emplace(M);
721 auto &State = R.first->second;
722 bool FirstTime = R.second;
730 auto &StartingMacros = NullSubmoduleState.Macros;
734 for (
auto &
Macro : StartingMacros) {
736 if (!
Macro.second.getLatest() &&
737 Macro.second.getOverriddenMacros().empty())
740 MacroState MS(
Macro.second.getLatest());
741 MS.setOverriddenMacros(*
this,
Macro.second.getOverriddenMacros());
742 State.Macros.insert(std::make_pair(
Macro.first, std::move(MS)));
747 BuildingSubmoduleStack.push_back(
748 BuildingSubmoduleInfo(M, ImportLoc, ForPragma, CurSubmoduleState,
749 PendingModuleMacroNames.size()));
752 Callbacks->EnteredSubmodule(M, ImportLoc, ForPragma);
755 CurSubmoduleState = &State;
763bool Preprocessor::needModuleMacros()
const {
765 if (BuildingSubmoduleStack.empty())
777 if (BuildingSubmoduleStack.empty() ||
778 BuildingSubmoduleStack.back().IsPragma != ForPragma) {
779 assert(ForPragma &&
"non-pragma module enter/leave mismatch");
783 auto &Info = BuildingSubmoduleStack.back();
785 Module *LeavingMod = Info.M;
788 if (!needModuleMacros() ||
794 BuildingSubmoduleStack.pop_back();
797 Callbacks->LeftSubmodule(LeavingMod, ImportLoc, ForPragma);
805 for (
unsigned I = Info.OuterPendingModuleMacroNames;
806 I != PendingModuleMacroNames.size(); ++I) {
807 auto *II = PendingModuleMacroNames[I];
808 if (!VisitedMacros.insert(II).second)
811 auto MacroIt = CurSubmoduleState->Macros.find(II);
812 if (MacroIt == CurSubmoduleState->Macros.end())
814 auto &
Macro = MacroIt->second;
818 auto *OldState = Info.OuterSubmoduleState;
820 OldState = &NullSubmoduleState;
821 if (OldState && OldState != CurSubmoduleState) {
824 auto &OldMacros = OldState->Macros;
825 auto OldMacroIt = OldMacros.find(II);
826 if (OldMacroIt == OldMacros.end())
829 OldMD = OldMacroIt->second.getLatest();
834 bool ExplicitlyPublic =
false;
836 assert(MD &&
"broken macro directive chain");
838 if (
auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
841 if (VisMD->isPublic())
842 ExplicitlyPublic =
true;
843 else if (!ExplicitlyPublic)
849 Def = DefMD->getInfo();
856 if (Def || !
Macro.getOverriddenMacros().empty())
863 Macro.setLatest(
nullptr);
864 Macro.setOverriddenMacros(*
this, {});
870 PendingModuleMacroNames.resize(Info.OuterPendingModuleMacroNames);
879 CurSubmoduleState = Info.OuterSubmoduleState;
881 BuildingSubmoduleStack.pop_back();
884 Callbacks->LeftSubmodule(LeavingMod, ImportLoc, ForPragma);
Defines the clang::FileManager interface and associated types.
Result
Implement __builtin_bit_cast and related operations.
llvm::MachO::FileType FileType
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.
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...
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.
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.
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?
Encapsulates changes to the "macros namespace" (the location where the macro name became active,...
const MacroDirective * getPrevious() const
Get previous definition of the macro with the same name.
Encapsulates the data about a macro definition (e.g.
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.
StringRef getTopLevelModuleName() const
Retrieve the name of the top-level module.
llvm::iterator_range< submodule_iterator > submodules()
std::optional< Header > getUmbrellaHeaderAsWritten() const
Retrieve the umbrella header as written.
std::string getFullModuleName(bool AllowStringLiterals=false) const
Retrieve the full name of this module, including the path from its top-level module.
OptionalDirectoryEntryRef getEffectiveUmbrellaDir() const
Get the effective umbrella directory for this module: either the one explicitly written in the module...
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)
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.
bool isPPInSafeBufferOptOutRegion()
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.
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
detail::SearchDirIteratorImpl< true > ConstSearchDirIterator
@ Result
The result type of a method or function.
@ 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.