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);
94 Lexer *TheLexer =
new Lexer(FID, *InputFile, *
this, IsFirstIncludeOfFile);
95 if (GetDependencyDirectives && FID != PredefinesFileID)
97 if (
auto MaybeDepDirectives = (*GetDependencyDirectives)(*
File))
98 TheLexer->DepDirectives = *MaybeDepDirectives;
100 EnterSourceFileWithLexer(TheLexer, CurDir);
106void Preprocessor::EnterSourceFileWithLexer(
Lexer *TheLexer,
111 if (CurPPLexer || CurTokenLexer)
112 PushIncludeMacroStack();
114 CurLexer.reset(TheLexer);
115 CurPPLexer = TheLexer;
116 CurDirLookup = CurDir;
117 CurLexerSubmodule =
nullptr;
118 CurLexerCallback = TheLexer->isDependencyDirectivesLexer()
119 ? CLK_DependencyDirectivesLexer
123 if (Callbacks && !CurLexer->Is_PragmaLexer) {
125 SourceMgr.getFileCharacteristic(CurLexer->getFileLoc());
135 Callbacks->LexedFileChanged(CurLexer->getFileID(),
145 std::unique_ptr<TokenLexer> TokLexer;
146 if (NumCachedTokenLexers == 0) {
147 TokLexer = std::make_unique<TokenLexer>(
Tok, ILEnd,
Macro, Args, *
this);
149 TokLexer = std::move(TokenLexerCache[--NumCachedTokenLexers]);
150 TokLexer->Init(
Tok, ILEnd,
Macro, Args);
153 PushIncludeMacroStack();
154 CurDirLookup =
nullptr;
155 CurTokenLexer = std::move(TokLexer);
156 CurLexerCallback = CLK_TokenLexer;
171void Preprocessor::EnterTokenStream(
const Token *Toks,
unsigned NumToks,
172 bool DisableMacroExpansion,
bool OwnsTokens,
174 if (CurLexerCallback == CLK_CachingLexer) {
176 assert(IsReinject &&
"new tokens in the middle of cached stream");
180 Toks, Toks + NumToks);
188 ExitCachingLexMode();
189 EnterTokenStream(Toks, NumToks, DisableMacroExpansion, OwnsTokens,
191 EnterCachingLexMode();
196 std::unique_ptr<TokenLexer> TokLexer;
197 if (NumCachedTokenLexers == 0) {
198 TokLexer = std::make_unique<TokenLexer>(
199 Toks, NumToks, DisableMacroExpansion, OwnsTokens, IsReinject, *
this);
201 TokLexer = std::move(TokenLexerCache[--NumCachedTokenLexers]);
202 TokLexer->Init(Toks, NumToks, DisableMacroExpansion, OwnsTokens,
207 PushIncludeMacroStack();
208 CurDirLookup =
nullptr;
209 CurTokenLexer = std::move(TokLexer);
210 CurLexerCallback = CLK_TokenLexer;
219 StringRef FilePath =
File.getDir().getName();
220 StringRef Path = FilePath;
221 while (!Path.empty()) {
223 if (*CurDir == Dir) {
224 Result = FilePath.substr(Path.size());
225 llvm::sys::path::append(Result,
226 llvm::sys::path::filename(
File.getName()));
231 Path = llvm::sys::path::parent_path(Path);
234 Result =
File.getName();
237void Preprocessor::PropagateLineStartLeadingSpaceInfo(
Token &
Result) {
239 CurTokenLexer->PropagateLineStartLeadingSpaceInfo(
Result);
243 CurLexer->PropagateLineStartLeadingSpaceInfo(
Result);
256const char *Preprocessor::getCurLexerEndPos() {
257 const char *EndPos = CurLexer->BufferEnd;
258 if (EndPos != CurLexer->BufferStart &&
259 (EndPos[-1] ==
'\n' || EndPos[-1] ==
'\r')) {
263 if (EndPos != CurLexer->BufferStart &&
264 (EndPos[-1] ==
'\n' || EndPos[-1] ==
'\r') &&
265 EndPos[-1] != EndPos[0])
275 SubMods.push_back(&Mod);
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,
292 llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
294 for (llvm::vfs::recursive_directory_iterator Entry(FS, Dir->
getName(), EC),
296 Entry != End && !EC; Entry.increment(EC)) {
297 using llvm::StringSwitch;
301 if (!StringSwitch<bool>(llvm::sys::path::extension(Entry->path()))
302 .Cases({
".h",
".H",
".hh",
".hpp"},
true)
306 if (
auto Header =
getFileManager().getOptionalFileRef(Entry->path()))
310 SmallString<128> RelativePath;
312 Diag(ExpectedHeadersLoc, diag::warn_uncovered_module_header)
323 assert(!CurTokenLexer &&
324 "Ending a file when currently in a macro!");
328 if (IncludeMacroStack.empty() &&
332 Diag(UnclosedSafeBufferOptOutLoc,
333 diag::err_pp_unclosed_pragma_unsafe_buffer_usage);
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);
346 const char *EndPos = getCurLexerEndPos();
347 CurLexer->BufferPtr = EndPos;
348 CurLexer->FormTokenWithChars(
Result, EndPos, tok::annot_module_end);
350 Result.setAnnotationValue(M);
357 CurPPLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
360 HeaderInfo.SetFileControllingMacro(*FE, ControllingMacro);
362 MI->setUsedForHeaderGuard(
true);
364 CurPPLexer->MIOpt.GetDefinedMacro()) {
366 DefinedMacro != ControllingMacro &&
367 CurLexer->isFirstTimeLexingFile()) {
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) {
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
391 CurPPLexer->MIOpt.GetDefinedLocation(),
392 ControllingMacro->getName());
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);
415 if (PragmaAssumeNonNullLoc.isValid() &&
416 !isEndOfMacro && !(CurLexer && CurLexer->Is_PragmaLexer)) {
421 PreambleRecordedPragmaAssumeNonNullLoc = PragmaAssumeNonNullLoc;
423 Diag(PragmaAssumeNonNullLoc, diag::err_pp_eof_in_assume_nonnull);
428 bool LeavingPCHThroughHeader =
false;
432 if (!IncludeMacroStack.empty()) {
436 SourceMgr.getLocForStartOfFile(CurPPLexer->getFileID()) ==
437 CodeCompletionFileLoc) {
438 assert(CurLexer &&
"Got EOF but no current lexer set!");
440 CurLexer->FormTokenWithChars(
Result, CurLexer->BufferEnd, tok::eof);
441 PendingDestroyLexers.push_back(std::move(CurLexer));
443 CurPPLexer =
nullptr;
448 if (!isEndOfMacro && CurPPLexer &&
449 (SourceMgr.getIncludeLoc(CurPPLexer->getFileID()).isValid() ||
451 (PredefinesFileID.isValid() &&
452 CurPPLexer->getFileID() == PredefinesFileID))) {
456 SourceMgr.local_sloc_entry_size() -
457 CurPPLexer->getInitialNumSLocEntries() + 1;
458 SourceMgr.setNumCreatedFIDsForFileID(CurPPLexer->getFileID(), NumFIDs);
461 bool ExitedFromPredefinesFile =
false;
463 if (!isEndOfMacro && CurPPLexer) {
464 ExitedFID = CurPPLexer->getFileID();
466 assert(PredefinesFileID.isValid() &&
467 "HandleEndOfFile is called before PredefinesFileId is set");
468 ExitedFromPredefinesFile = (PredefinesFileID == ExitedFID);
471 if (LeavingSubmodule) {
476 const char *EndPos = getCurLexerEndPos();
478 CurLexer->BufferPtr = EndPos;
479 CurLexer->FormTokenWithChars(
Result, EndPos, tok::annot_module_end);
481 Result.setAnnotationValue(M);
484 bool FoundPCHThroughHeader =
false;
487 SourceMgr.getFileEntryForID(CurPPLexer->getFileID())))
488 FoundPCHThroughHeader =
true;
494 PropagateLineStartLeadingSpaceInfo(
Result);
497 if (Callbacks && !isEndOfMacro && CurPPLexer) {
500 SourceMgr.getFileCharacteristic(Loc);
502 Callbacks->LexedFileChanged(CurPPLexer->getFileID(),
504 FileType, ExitedFID, Loc);
510 if (ExitedFromPredefinesFile) {
511 replayPreambleConditionalStack();
512 if (PreambleRecordedPragmaAssumeNonNullLoc.isValid())
513 PragmaAssumeNonNullLoc = PreambleRecordedPragmaAssumeNonNullLoc;
516 if (!isEndOfMacro && CurPPLexer && FoundPCHThroughHeader &&
521 LeavingPCHThroughHeader =
true;
524 return LeavingSubmodule;
528 assert(CurLexer &&
"Got EOF but no current lexer set!");
529 const char *EndPos = getCurLexerEndPos();
531 CurLexer->BufferPtr = EndPos;
534 CurLexer->FormTokenWithChars(
Result, EndPos, tok::annot_repl_input_end);
536 Result.setAnnotationValue(
nullptr);
538 CurLexer->FormTokenWithChars(
Result, EndPos, tok::eof);
548 if (CurLexer->getFileLoc() == CodeCompletionFileLoc)
549 Result.setLocation(
Result.getLocation().getLocWithOffset(-1));
554 Diag(CurLexer->getFileLoc(), diag::err_pp_through_header_not_seen)
555 << PPOpts.PCHThroughHeader << 0;
563 if (LexLevel > 0 && CurLexer) {
564 PendingDestroyLexers.push_back(std::move(CurLexer));
571 CurPPLexer =
nullptr;
577 for (WarnUnusedMacroLocsTy::iterator
578 I=WarnUnusedMacroLocs.begin(), E=WarnUnusedMacroLocs.end();
580 Diag(*I, diag::pp_macro_not_used);
590 for (
auto *M : AllMods)
591 diagnoseMissingHeaderInUmbrellaDir(*M);
600 assert(CurTokenLexer && !CurPPLexer &&
601 "Ending a macro when currently in a #include file!");
603 if (!MacroExpandingLexersStack.empty() &&
604 MacroExpandingLexersStack.back().first == CurTokenLexer.get())
605 removeCachedMacroExpandedTokensOfLastLexer();
608 if (NumCachedTokenLexers == TokenLexerCacheSize)
609 CurTokenLexer.reset();
611 TokenLexerCache[NumCachedTokenLexers++] = std::move(CurTokenLexer);
621 assert(!IncludeMacroStack.empty() &&
"Ran out of stack entries to load");
625 if (NumCachedTokenLexers == TokenLexerCacheSize)
626 CurTokenLexer.reset();
628 TokenLexerCache[NumCachedTokenLexers++] = std::move(CurTokenLexer);
631 PopIncludeMacroStack();
638 assert(CurTokenLexer && !CurPPLexer &&
639 "Pasted comment can only be formed from macro");
644 bool LexerWasInPPMode =
false;
645 for (
const IncludeStackInfo &ISI : llvm::reverse(IncludeMacroStack)) {
646 if (ISI.ThePPLexer ==
nullptr)
continue;
654 FoundLexer = ISI.ThePPLexer;
672 while (
Tok.isNot(tok::eod) &&
Tok.isNot(tok::eof))
676 if (
Tok.is(tok::eod)) {
677 assert(FoundLexer &&
"Can't get end of line without an active lexer");
683 if (LexerWasInPPMode)
return;
694 assert(!FoundLexer &&
"Lexer should return EOD before EOF in PP mode");
701 BuildingSubmoduleStack.push_back(
702 BuildingSubmoduleInfo(M, ImportLoc, ForPragma, CurSubmoduleState,
703 PendingModuleMacroNames.size()));
705 Callbacks->EnteredSubmodule(M, ImportLoc, ForPragma);
719 auto R = Submodules.try_emplace(M);
720 auto &State = R.first->second;
721 bool FirstTime = R.second;
729 auto &StartingMacros = NullSubmoduleState.Macros;
733 for (
auto &
Macro : StartingMacros) {
735 if (!
Macro.second.getLatest() &&
736 Macro.second.getOverriddenMacros().empty())
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)));
746 BuildingSubmoduleStack.push_back(
747 BuildingSubmoduleInfo(M, ImportLoc, ForPragma, CurSubmoduleState,
748 PendingModuleMacroNames.size()));
751 Callbacks->EnteredSubmodule(M, ImportLoc, ForPragma);
754 CurSubmoduleState = &State;
762bool Preprocessor::needModuleMacros()
const {
764 if (BuildingSubmoduleStack.empty())
776 if (BuildingSubmoduleStack.empty() ||
777 BuildingSubmoduleStack.back().IsPragma != ForPragma) {
778 assert(ForPragma &&
"non-pragma module enter/leave mismatch");
782 auto &Info = BuildingSubmoduleStack.back();
784 Module *LeavingMod = Info.M;
787 if (!needModuleMacros() ||
793 BuildingSubmoduleStack.pop_back();
796 Callbacks->LeftSubmodule(LeavingMod, ImportLoc, ForPragma);
804 for (
unsigned I = Info.OuterPendingModuleMacroNames;
805 I != PendingModuleMacroNames.size(); ++I) {
806 auto *II = PendingModuleMacroNames[I];
807 if (!VisitedMacros.insert(II).second)
810 auto MacroIt = CurSubmoduleState->Macros.find(II);
811 if (MacroIt == CurSubmoduleState->Macros.end())
813 auto &
Macro = MacroIt->second;
817 auto *OldState = Info.OuterSubmoduleState;
819 OldState = &NullSubmoduleState;
820 if (OldState && OldState != CurSubmoduleState) {
823 auto &OldMacros = OldState->Macros;
824 auto OldMacroIt = OldMacros.find(II);
825 if (OldMacroIt == OldMacros.end())
828 OldMD = OldMacroIt->second.getLatest();
833 bool ExplicitlyPublic =
false;
835 assert(MD &&
"broken macro directive chain");
837 if (
auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
840 if (VisMD->isPublic())
841 ExplicitlyPublic =
true;
842 else if (!ExplicitlyPublic)
848 Def = DefMD->getInfo();
855 if (Def || !
Macro.getOverriddenMacros().empty())
862 Macro.setLatest(
nullptr);
863 Macro.setOverriddenMacros(*
this, {});
869 PendingModuleMacroNames.resize(Info.OuterPendingModuleMacroNames);
878 CurSubmoduleState = Info.OuterSubmoduleState;
880 BuildingSubmoduleStack.pop_back();
883 Callbacks->LeftSubmodule(LeavingMod, ImportLoc, ForPragma);
Defines the clang::FileManager interface and associated types.
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?
Lexer - This provides a simple interface that turns a text buffer into a stream of tokens.
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.