22 #include "llvm/ADT/StringSwitch.h" 23 #include "llvm/Support/FileSystem.h" 24 #include "llvm/Support/MemoryBuffer.h" 25 #include "llvm/Support/Path.h" 26 using namespace clang;
38 return IncludeMacroStack.empty();
41 assert(IsFileLexer(IncludeMacroStack[0]) &&
42 "Top level include stack isn't our primary lexer?");
44 IncludeMacroStack.begin() + 1, IncludeMacroStack.end(),
45 [&](
const IncludeStackInfo &ISI) ->
bool {
return IsFileLexer(ISI); });
56 for (
const IncludeStackInfo &ISI : llvm::reverse(IncludeMacroStack)) {
58 return ISI.ThePPLexer;
72 assert(!CurTokenLexer &&
"Cannot #include a file inside a macro!");
73 ++NumEnteredSourceFiles;
75 if (MaxIncludeStackDepth < IncludeMacroStack.size())
76 MaxIncludeStackDepth = IncludeMacroStack.size();
79 if (
PTHLexer *PL = PTH->CreateLexer(FID)) {
80 EnterSourceFileWithPTH(PL, CurDir);
87 const llvm::MemoryBuffer *InputFile =
88 getSourceManager().getBuffer(FID, Loc, &Invalid);
91 Diag(Loc, diag::err_pp_error_opening_file)
92 << std::string(SourceMgr.getBufferName(FileStart)) <<
"";
96 if (isCodeCompletionEnabled() &&
97 SourceMgr.getFileEntryForID(FID) == CodeCompletionFile) {
98 CodeCompletionFileLoc = SourceMgr.getLocForStartOfFile(FID);
100 CodeCompletionFileLoc.getLocWithOffset(CodeCompletionOffset);
103 EnterSourceFileWithLexer(
new Lexer(FID, InputFile, *
this), CurDir);
109 void Preprocessor::EnterSourceFileWithLexer(
Lexer *TheLexer,
113 if (CurPPLexer || CurTokenLexer)
114 PushIncludeMacroStack();
116 CurLexer.reset(TheLexer);
117 CurPPLexer = TheLexer;
118 CurDirLookup = CurDir;
119 CurLexerSubmodule =
nullptr;
120 if (CurLexerKind != CLK_LexAfterModuleImport)
121 CurLexerKind = CLK_Lexer;
124 if (Callbacks && !CurLexer->Is_PragmaLexer) {
126 SourceMgr.getFileCharacteristic(CurLexer->getFileLoc());
128 Callbacks->FileChanged(CurLexer->getFileLoc(),
135 void Preprocessor::EnterSourceFileWithPTH(
PTHLexer *PL,
138 if (CurPPLexer || CurTokenLexer)
139 PushIncludeMacroStack();
141 CurDirLookup = CurDir;
142 CurPTHLexer.reset(PL);
143 CurPPLexer = CurPTHLexer.get();
144 CurLexerSubmodule =
nullptr;
145 if (CurLexerKind != CLK_LexAfterModuleImport)
146 CurLexerKind = CLK_PTHLexer;
150 FileID FID = CurPPLexer->getFileID();
153 SourceMgr.getFileCharacteristic(EnterLoc);
162 std::unique_ptr<TokenLexer> TokLexer;
163 if (NumCachedTokenLexers == 0) {
164 TokLexer = llvm::make_unique<TokenLexer>(
Tok, ILEnd, Macro, Args, *
this);
166 TokLexer = std::move(TokenLexerCache[--NumCachedTokenLexers]);
167 TokLexer->Init(Tok, ILEnd, Macro, Args);
170 PushIncludeMacroStack();
171 CurDirLookup =
nullptr;
172 CurTokenLexer = std::move(TokLexer);
173 if (CurLexerKind != CLK_LexAfterModuleImport)
174 CurLexerKind = CLK_TokenLexer;
189 void Preprocessor::EnterTokenStream(
const Token *Toks,
unsigned NumToks,
190 bool DisableMacroExpansion,
192 if (CurLexerKind == CLK_CachingLexer) {
197 Toks, Toks + NumToks);
205 ExitCachingLexMode();
206 EnterTokenStream(Toks, NumToks, DisableMacroExpansion, OwnsTokens);
207 EnterCachingLexMode();
212 std::unique_ptr<TokenLexer> TokLexer;
213 if (NumCachedTokenLexers == 0) {
214 TokLexer = llvm::make_unique<TokenLexer>(
215 Toks, NumToks, DisableMacroExpansion, OwnsTokens, *
this);
217 TokLexer = std::move(TokenLexerCache[--NumCachedTokenLexers]);
218 TokLexer->Init(Toks, NumToks, DisableMacroExpansion, OwnsTokens);
222 PushIncludeMacroStack();
223 CurDirLookup =
nullptr;
224 CurTokenLexer = std::move(TokLexer);
225 if (CurLexerKind != CLK_LexAfterModuleImport)
226 CurLexerKind = CLK_TokenLexer;
237 StringRef Path = FilePath;
238 while (!Path.empty()) {
241 Result = FilePath.substr(Path.size());
242 llvm::sys::path::append(Result,
243 llvm::sys::path::filename(File->
getName()));
248 Path = llvm::sys::path::parent_path(Path);
254 void Preprocessor::PropagateLineStartLeadingSpaceInfo(
Token &
Result) {
256 CurTokenLexer->PropagateLineStartLeadingSpaceInfo(Result);
260 CurLexer->PropagateLineStartLeadingSpaceInfo(Result);
273 const char *Preprocessor::getCurLexerEndPos() {
274 const char *EndPos = CurLexer->BufferEnd;
275 if (EndPos != CurLexer->BufferStart &&
276 (EndPos[-1] ==
'\n' || EndPos[-1] ==
'\r')) {
280 if (EndPos != CurLexer->BufferStart &&
281 (EndPos[-1] ==
'\n' || EndPos[-1] ==
'\r') &&
282 EndPos[-1] != EndPos[0])
292 SubMods.push_back(&Mod);
297 void Preprocessor::diagnoseMissingHeaderInUmbrellaDir(
const Module &Mod) {
300 SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
301 if (getDiagnostics().isIgnored(diag::warn_uncovered_module_header, StartLoc))
304 ModuleMap &ModMap = getHeaderSearchInfo().getModuleMap();
309 Entry !=
End && !EC; Entry.increment(EC)) {
310 using llvm::StringSwitch;
314 if (!StringSwitch<bool>(llvm::sys::path::extension(Entry->getName()))
315 .Cases(
".h",
".H",
".hh",
".hpp",
true)
319 if (
const FileEntry *Header = getFileManager().getFile(Entry->getName()))
320 if (!getSourceManager().hasFileInfo(Header)) {
325 Diag(StartLoc, diag::warn_uncovered_module_header)
336 assert(!CurTokenLexer &&
337 "Ending a file when currently in a macro!");
342 const bool LeavingSubmodule = CurLexer && CurLexerSubmodule;
343 if ((LeavingSubmodule || IncludeMacroStack.empty()) &&
344 !BuildingSubmoduleStack.empty() &&
345 BuildingSubmoduleStack.back().IsPragma) {
346 Diag(BuildingSubmoduleStack.back().ImportLoc,
347 diag::err_pp_module_begin_without_module_end);
348 Module *M = LeaveSubmodule(
true);
351 const char *EndPos = getCurLexerEndPos();
352 CurLexer->BufferPtr = EndPos;
353 CurLexer->FormTokenWithChars(Result, EndPos, tok::annot_module_end);
362 CurPPLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
364 if (
const FileEntry *FE = CurPPLexer->getFileEntry()) {
365 HeaderInfo.SetFileControllingMacro(FE, ControllingMacro);
367 getMacroInfo(const_cast<IdentifierInfo*>(ControllingMacro)))
368 MI->setUsedForHeaderGuard(
true);
370 CurPPLexer->MIOpt.GetDefinedMacro()) {
372 DefinedMacro != ControllingMacro &&
373 HeaderInfo.FirstTimeLexingFile(FE)) {
381 const StringRef ControllingMacroName = ControllingMacro->getName();
382 const StringRef DefinedMacroName = DefinedMacro->getName();
383 const size_t MaxHalfLength =
std::max(ControllingMacroName.size(),
384 DefinedMacroName.size()) / 2;
385 const unsigned ED = ControllingMacroName.edit_distance(
386 DefinedMacroName,
true, MaxHalfLength);
387 if (ED <= MaxHalfLength) {
389 Diag(CurPPLexer->MIOpt.GetMacroLocation(),
390 diag::warn_header_guard)
391 << CurPPLexer->MIOpt.GetMacroLocation() << ControllingMacro;
392 Diag(CurPPLexer->MIOpt.GetDefinedLocation(),
393 diag::note_header_guard)
394 << CurPPLexer->MIOpt.GetDefinedLocation() << DefinedMacro
397 CurPPLexer->MIOpt.GetDefinedLocation(),
398 ControllingMacro->getName());
409 if (PragmaARCCFCodeAuditedLoc.isValid() &&
410 !isEndOfMacro && !(CurLexer && CurLexer->Is_PragmaLexer)) {
411 Diag(PragmaARCCFCodeAuditedLoc, diag::err_pp_eof_in_arc_cf_code_audited);
420 if (PragmaAssumeNonNullLoc.isValid() &&
421 !isEndOfMacro && !(CurLexer && CurLexer->Is_PragmaLexer)) {
422 Diag(PragmaAssumeNonNullLoc, diag::err_pp_eof_in_assume_nonnull);
430 if (!IncludeMacroStack.empty()) {
433 if (isCodeCompletionEnabled() && CurPPLexer &&
434 SourceMgr.getLocForStartOfFile(CurPPLexer->getFileID()) ==
435 CodeCompletionFileLoc) {
438 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd,
tok::eof);
441 assert(CurPTHLexer &&
"Got EOF but no current lexer set!");
442 CurPTHLexer->getEOF(Result);
446 CurPPLexer =
nullptr;
447 recomputeCurLexerKind();
451 if (!isEndOfMacro && CurPPLexer &&
452 SourceMgr.getIncludeLoc(CurPPLexer->getFileID()).isValid()) {
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) {
473 Module *M = LeaveSubmodule(
false);
476 const char *EndPos = getCurLexerEndPos();
478 CurLexer->BufferPtr = EndPos;
479 CurLexer->FormTokenWithChars(Result, EndPos, tok::annot_module_end);
485 RemoveTopOfLexerStack();
488 PropagateLineStartLeadingSpaceInfo(Result);
491 if (Callbacks && !isEndOfMacro && CurPPLexer) {
493 SourceMgr.getFileCharacteristic(CurPPLexer->getSourceLocation());
494 Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
500 if (ExitedFromPredefinesFile)
501 replayPreambleConditionalStack();
504 return LeavingSubmodule;
509 const char *EndPos = getCurLexerEndPos();
511 CurLexer->BufferPtr = EndPos;
512 CurLexer->FormTokenWithChars(Result, EndPos,
tok::eof);
514 if (isCodeCompletionEnabled()) {
521 if (CurLexer->getFileLoc() == CodeCompletionFileLoc)
525 if (!isIncrementalProcessingEnabled())
529 assert(CurPTHLexer &&
"Got EOF but no current lexer set!");
530 CurPTHLexer->getEOF(Result);
534 if (!isIncrementalProcessingEnabled())
535 CurPPLexer =
nullptr;
541 for (WarnUnusedMacroLocsTy::iterator
542 I=WarnUnusedMacroLocs.begin(), E=WarnUnusedMacroLocs.end();
544 Diag(*I, diag::pp_macro_not_used);
551 if (
Module *Mod = getCurrentModule()) {
554 for (
auto *M : AllMods)
555 diagnoseMissingHeaderInUmbrellaDir(*M);
564 assert(CurTokenLexer && !CurPPLexer &&
565 "Ending a macro when currently in a #include file!");
567 if (!MacroExpandingLexersStack.empty() &&
568 MacroExpandingLexersStack.back().first == CurTokenLexer.get())
569 removeCachedMacroExpandedTokensOfLastLexer();
572 if (NumCachedTokenLexers == TokenLexerCacheSize)
573 CurTokenLexer.reset();
575 TokenLexerCache[NumCachedTokenLexers++] = std::move(CurTokenLexer);
578 return HandleEndOfFile(Result,
true);
585 assert(!IncludeMacroStack.empty() &&
"Ran out of stack entries to load");
589 if (NumCachedTokenLexers == TokenLexerCacheSize)
590 CurTokenLexer.reset();
592 TokenLexerCache[NumCachedTokenLexers++] = std::move(CurTokenLexer);
595 PopIncludeMacroStack();
602 assert(CurTokenLexer && !CurPPLexer &&
603 "Pasted comment can only be formed from macro");
608 bool LexerWasInPPMode =
false;
609 for (
const IncludeStackInfo &ISI : llvm::reverse(IncludeMacroStack)) {
610 if (ISI.ThePPLexer ==
nullptr)
continue;
618 FoundLexer = ISI.ThePPLexer;
628 if (!HandleEndOfTokenLexer(Tok)) Lex(Tok);
640 if (Tok.
is(tok::eod)) {
641 assert(FoundLexer &&
"Can't get end of line without an active lexer");
647 if (LexerWasInPPMode)
return;
658 assert(!FoundLexer &&
"Lexer should return EOD before EOF in PP mode");
663 if (!getLangOpts().ModulesLocalVisibility) {
665 BuildingSubmoduleStack.push_back(
666 BuildingSubmoduleInfo(M, ImportLoc, ForPragma, CurSubmoduleState,
667 PendingModuleMacroNames.size()));
675 ModuleMap &ModMap = getHeaderSearchInfo().getModuleMap();
681 auto R = Submodules.insert(std::make_pair(M, SubmoduleState()));
682 auto &
State = R.first->second;
683 bool FirstTime = R.second;
691 auto &StartingMacros = NullSubmoduleState.Macros;
695 for (
auto &Macro : StartingMacros) {
697 if (!Macro.second.getLatest() &&
698 Macro.second.getOverriddenMacros().empty())
701 MacroState MS(Macro.second.getLatest());
702 MS.setOverriddenMacros(*
this, Macro.second.getOverriddenMacros());
703 State.Macros.insert(std::make_pair(Macro.first, std::move(MS)));
708 BuildingSubmoduleStack.push_back(
709 BuildingSubmoduleInfo(M, ImportLoc, ForPragma, CurSubmoduleState,
710 PendingModuleMacroNames.size()));
713 CurSubmoduleState = &
State;
717 makeModuleVisible(M, ImportLoc);
720 bool Preprocessor::needModuleMacros()
const {
722 if (BuildingSubmoduleStack.empty())
726 if (getLangOpts().ModulesLocalVisibility)
730 return getLangOpts().isCompilingModule();
734 if (BuildingSubmoduleStack.empty() ||
735 BuildingSubmoduleStack.back().IsPragma != ForPragma) {
736 assert(ForPragma &&
"non-pragma module enter/leave mismatch");
740 auto &Info = BuildingSubmoduleStack.back();
742 Module *LeavingMod = Info.M;
745 if (!needModuleMacros() ||
746 (!getLangOpts().ModulesLocalVisibility &&
751 BuildingSubmoduleStack.pop_back();
752 makeModuleVisible(LeavingMod, ImportLoc);
757 llvm::SmallPtrSet<const IdentifierInfo*, 8> VisitedMacros;
758 for (
unsigned I = Info.OuterPendingModuleMacroNames;
759 I != PendingModuleMacroNames.size(); ++I) {
760 auto *II =
const_cast<IdentifierInfo*
>(PendingModuleMacroNames[I]);
761 if (!VisitedMacros.insert(II).second)
764 auto MacroIt = CurSubmoduleState->Macros.find(II);
765 if (MacroIt == CurSubmoduleState->Macros.end())
767 auto &Macro = MacroIt->second;
771 auto *OldState = Info.OuterSubmoduleState;
772 if (getLangOpts().ModulesLocalVisibility)
773 OldState = &NullSubmoduleState;
774 if (OldState && OldState != CurSubmoduleState) {
777 auto &OldMacros = OldState->Macros;
778 auto OldMacroIt = OldMacros.find(II);
779 if (OldMacroIt == OldMacros.end())
782 OldMD = OldMacroIt->second.getLatest();
787 bool ExplicitlyPublic =
false;
788 for (
auto *MD = Macro.getLatest(); MD != OldMD; MD = MD->
getPrevious()) {
789 assert(MD &&
"broken macro directive chain");
791 if (
auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
794 if (VisMD->isPublic())
795 ExplicitlyPublic =
true;
796 else if (!ExplicitlyPublic)
802 Def = DefMD->getInfo();
809 if (Def || !Macro.getOverriddenMacros().empty())
810 addModuleMacro(LeavingMod, II, Def,
811 Macro.getOverriddenMacros(), IsNew);
813 if (!getLangOpts().ModulesLocalVisibility) {
816 Macro.setLatest(
nullptr);
817 Macro.setOverriddenMacros(*
this, {});
823 PendingModuleMacroNames.resize(Info.OuterPendingModuleMacroNames);
831 if (getLangOpts().ModulesLocalVisibility)
832 CurSubmoduleState = Info.OuterSubmoduleState;
834 BuildingSubmoduleStack.pop_back();
837 makeModuleVisible(LeavingMod, ImportLoc);
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, unsigned DiagID)
Produce a diagnostic highlighting some portion of a literal.
Lexer - This provides a simple interface that turns a text buffer into a stream of tokens...
SourceLocation getLocWithOffset(int Offset) const
Return a source location with the specified offset from this SourceLocation.
Implements support for file system lookup, file system caching, and directory search management...
Defines the clang::FileManager interface and associated types.
bool is(tok::TokenKind K) const
is/isNot - Predicates to check if this token is a specific kind, as in "if (Tok.is(tok::l_brace)) {...
Defines the SourceManager interface.
bool isInPrimaryFile() const
Return true if we're in the top-level file, not in a #include.
Defines the clang::MacroInfo and clang::MacroDirective classes.
bool resolveUses(Module *Mod, bool Complain)
Resolve all of the unresolved uses in the given module.
CharacteristicKind
Indicates whether a file or directory holds normal user code, system code, or system code which is im...
const MacroDirective * getPrevious() const
Get previous definition of the macro with the same name.
bool HandleEndOfTokenLexer(Token &Result)
Callback invoked when the current TokenLexer hits the end of its token stream.
The virtual file system interface.
One of these records is kept for each identifier that is lexed.
A directive for a defined macro or a macro imported from a module.
bool ParsingPreprocessorDirective
True when parsing #XXX; turns '\n' into a tok::eod token.
bool resolveConflicts(Module *Mod, bool Complain)
Resolve all of the unresolved conflicts in the given module.
void EnterSubmodule(Module *M, SourceLocation ImportLoc, bool ForPragma)
Module * LeaveSubmodule(bool ForPragma)
Header getUmbrellaHeader() const
Retrieve the header that serves as the umbrella header for this module.
Token - This structure provides full information about a lexed token.
An input iterator over the recursive contents of a virtual path, similar to llvm::sys::fs::recursive_...
Describes a module or submodule.
StringRef getTopLevelModuleName() const
Retrieve the name of the top-level module.
std::string getFullModuleName(bool AllowStringLiterals=false) const
Retrieve the full name of this module, including the path from its top-level module.
const DirectoryEntry * getDirectory(StringRef DirName, bool CacheFailure=true)
Lookup, cache, and verify the specified directory (real or virtual).
const DirectoryEntry * Entry
void setAnnotationValue(void *val)
bool LexingRawMode
True if in raw mode.
MacroArgs - An instance of this class captures information about the formal arguments specified to a ...
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file. ...
Defines the clang::Preprocessor interface.
void HandleMicrosoftCommentPaste(Token &Tok)
When the macro expander pastes together a comment (/##/) in Microsoft mode, this method handles updat...
bool resolveExports(Module *Mod, bool Complain)
Resolve all of the unresolved exports in the given module.
static void computeRelativePath(FileManager &FM, const DirectoryEntry *Dir, const FileEntry *File, SmallString< 128 > &Result)
Compute the relative path that names the given file relative to the given directory.
bool HandleEndOfFile(Token &Result, bool isEndOfMacro=false)
Callback invoked when the lexer hits the end of the current file.
The result type of a method or function.
DirectoryLookup - This class represents one entry in the search list that specifies the search order ...
const DirectoryEntry * getDir() const
Return the directory the file lives in.
Encapsulates changes to the "macros namespace" (the location where the macro name became active...
Encodes a location in the source.
StringRef getName() const
void setAnnotationEndLoc(SourceLocation L)
Cached information about one file (either on disk or in the virtual file system). ...
void EnterMacro(Token &Identifier, 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...
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
bool isNot(tok::TokenKind K) const
Dataflow Directional Tag Classes.
llvm::iterator_range< submodule_iterator > submodules()
Encapsulates the data about a macro definition (e.g.
DirectoryName getUmbrellaDir() const
Retrieve the directory for which this module serves as the umbrella.
Cached information about one directory (either on disk or in the virtual file system).
char __ovld __cnfn max(char x, char y)
Returns y if x < y, otherwise it returns x.
static void collectAllSubModulesWithUmbrellaHeader(const Module &Mod, SmallVectorImpl< const Module *> &SubMods)
static bool isMacroDefined(const Sema &S, SourceLocation Loc, StringRef Name)
void RemoveTopOfLexerStack()
Pop the current lexer/macro exp off the top of the lexer stack.
static FixItHint CreateReplacement(CharSourceRange RemoveRange, StringRef Code)
Create a code modification hint that replaces the given source range with the given code string...
The translation unit is a complete translation unit.
bool isHeaderInUnavailableModule(const FileEntry *Header) const
Determine whether the given header is part of a module marked 'unavailable'.
void setLocation(SourceLocation L)
PreprocessorLexer * getCurrentFileLexer() const
Return the current file lexer being lexed from.
void startToken()
Reset all flags to cleared.
StringRef getName() const
bool EnterSourceFile(FileID CurFileID, const DirectoryLookup *Dir, SourceLocation Loc)
Add a source file to the top of the include stack and start lexing tokens from it instead of the curr...