15 #ifndef LLVM_CLANG_LEX_PREPROCESSOR_H 16 #define LLVM_CLANG_LEX_PREPROCESSOR_H 35 #include "llvm/ADT/ArrayRef.h" 36 #include "llvm/ADT/DenseMap.h" 37 #include "llvm/ADT/FoldingSet.h" 38 #include "llvm/ADT/None.h" 39 #include "llvm/ADT/Optional.h" 40 #include "llvm/ADT/PointerUnion.h" 41 #include "llvm/ADT/STLExtras.h" 42 #include "llvm/ADT/SmallPtrSet.h" 43 #include "llvm/ADT/SmallVector.h" 44 #include "llvm/ADT/StringRef.h" 45 #include "llvm/ADT/TinyPtrVector.h" 46 #include "llvm/ADT/iterator_range.h" 47 #include "llvm/Support/Allocator.h" 48 #include "llvm/Support/Casting.h" 49 #include "llvm/Support/Registry.h" 61 template<
unsigned InternalLen>
class SmallString;
67 class CodeCompletionHandler;
70 class DirectoryLookup;
71 class ExternalPreprocessorSource;
76 class MemoryBufferCache;
78 class PragmaNamespace;
79 class PreprocessingRecord;
80 class PreprocessorLexer;
81 class PreprocessorOptions;
94 assert(Kind != tok::raw_identifier &&
"Raw identifiers are not supported.");
95 assert(Kind != tok::identifier &&
96 "Identifiers should be created by TokenValue(IdentifierInfo *)");
104 return Tok.
getKind() == Kind &&
131 std::shared_ptr<PreprocessorOptions> PPOpts;
139 std::unique_ptr<ScratchBuffer> ScratchBuf;
148 std::unique_ptr<PTHManager> PTH;
152 llvm::BumpPtrAllocator BP;
186 unsigned CounterValue = 0;
190 MaxAllowedIncludeStackDepth = 200
194 bool KeepComments : 1;
195 bool KeepMacroComments : 1;
196 bool SuppressIncludeNotFoundError : 1;
199 bool InMacroArgs : 1;
202 bool OwnsHeaderSearch : 1;
205 bool DisableMacroExpansion : 1;
209 bool MacroExpansionInDirectivesOverride : 1;
214 mutable bool ReadMacrosFromExternalSource : 1;
217 bool PragmasEnabled : 1;
220 bool PreprocessedOutput : 1;
223 bool ParsingIfOrElifDirective;
226 bool InMacroArgPreExpansion;
248 std::unique_ptr<PragmaNamespace> PragmaHandlers;
252 std::unique_ptr<PragmaNamespace> PragmaHandlersBackup;
256 std::vector<CommentHandler *> CommentHandlers;
260 bool IncrementalProcessing =
false;
269 const FileEntry *CodeCompletionFile =
nullptr;
272 unsigned CodeCompletionOffset = 0;
292 bool LastTokenWasAt =
false;
296 bool ModuleImportExpectsIdentifier =
false;
307 bool CodeCompletionReached =
false;
323 std::pair<int, bool> SkipMainFilePreamble;
334 bool FoundNonSkipPortion,
bool FoundElse,
336 : HashTokenLoc(HashTokenLoc), IfTokenLoc(IfTokenLoc),
337 FoundNonSkipPortion(FoundNonSkipPortion), FoundElse(FoundElse),
345 class PreambleConditionalStackStore {
353 PreambleConditionalStackStore() =
default;
355 void startRecording() { ConditionalStackState = Recording; }
356 void startReplaying() { ConditionalStackState = Replaying; }
357 bool isRecording()
const {
return ConditionalStackState == Recording; }
358 bool isReplaying()
const {
return ConditionalStackState == Replaying; }
361 return ConditionalStack;
364 void doneReplaying() {
365 ConditionalStack.clear();
366 ConditionalStackState = Off;
370 if (!isRecording() && !isReplaying())
372 ConditionalStack.clear();
373 ConditionalStack.append(s.begin(), s.end());
376 bool hasRecordedPreamble()
const {
return !ConditionalStack.empty(); }
378 bool reachedEOFWhileSkipping()
const {
return SkipInfo.hasValue(); }
380 void clearSkipInfo() { SkipInfo.reset(); }
386 State ConditionalStackState = Off;
387 } PreambleConditionalStack;
393 std::unique_ptr<Lexer> CurLexer;
399 std::unique_ptr<PTHLexer> CurPTHLexer;
417 std::unique_ptr<TokenLexer> CurTokenLexer;
425 CLK_LexAfterModuleImport
426 } CurLexerKind = CLK_Lexer;
430 Module *CurLexerSubmodule =
nullptr;
435 struct IncludeStackInfo {
436 enum CurLexerKind CurLexerKind;
438 std::unique_ptr<Lexer> TheLexer;
439 std::unique_ptr<PTHLexer> ThePTHLexer;
441 std::unique_ptr<TokenLexer> TheTokenLexer;
446 IncludeStackInfo(
enum CurLexerKind CurLexerKind,
Module *TheSubmodule,
447 std::unique_ptr<Lexer> &&TheLexer,
448 std::unique_ptr<PTHLexer> &&ThePTHLexer,
450 std::unique_ptr<TokenLexer> &&TheTokenLexer,
452 : CurLexerKind(
std::move(CurLexerKind)),
453 TheSubmodule(
std::move(TheSubmodule)), TheLexer(
std::move(TheLexer)),
454 ThePTHLexer(
std::move(ThePTHLexer)),
455 ThePPLexer(
std::move(ThePPLexer)),
456 TheTokenLexer(
std::move(TheTokenLexer)),
457 TheDirLookup(
std::move(TheDirLookup)) {}
459 std::vector<IncludeStackInfo> IncludeMacroStack;
463 std::unique_ptr<PPCallbacks> Callbacks;
465 struct MacroExpandsInfo {
471 : Tok(Tok), MD(MD), Range(Range) {}
476 struct ModuleMacroInfo {
481 llvm::TinyPtrVector<ModuleMacro *> ActiveModuleMacros;
485 unsigned ActiveModuleMacrosGeneration = 0;
488 bool IsAmbiguous =
false;
491 llvm::TinyPtrVector<ModuleMacro *> OverriddenMacros;
498 mutable llvm::PointerUnion<MacroDirective *, ModuleMacroInfo *>
State;
503 PP.updateOutOfDateIdentifier(const_cast<IdentifierInfo&>(*II));
509 !PP.CurSubmoduleState->VisibleModules.getGeneration())
512 auto *Info = State.dyn_cast<ModuleMacroInfo*>();
519 if (PP.CurSubmoduleState->VisibleModules.getGeneration() !=
520 Info->ActiveModuleMacrosGeneration)
521 PP.updateModuleMacroInfo(II, *Info);
526 MacroState() : MacroState(nullptr) {}
529 MacroState(MacroState &&O) noexcept :
State(O.State) {
533 MacroState &operator=(MacroState &&O) noexcept {
541 if (
auto *Info = State.dyn_cast<ModuleMacroInfo*>())
542 Info->~ModuleMacroInfo();
546 if (
auto *Info = State.dyn_cast<ModuleMacroInfo*>())
552 if (
auto *Info = State.dyn_cast<ModuleMacroInfo*>())
559 auto *Info = getModuleInfo(PP, II);
560 return Info ? Info->IsAmbiguous :
false;
565 if (
auto *Info = getModuleInfo(PP, II))
566 return Info->ActiveModuleMacros;
573 if (
auto *Latest = getLatest())
574 return Latest->findDirectiveAtLoc(Loc, SourceMgr);
579 if (
auto *Info = getModuleInfo(PP, II)) {
580 Info->OverriddenMacros.insert(Info->OverriddenMacros.end(),
581 Info->ActiveModuleMacros.begin(),
582 Info->ActiveModuleMacros.end());
583 Info->ActiveModuleMacros.clear();
584 Info->IsAmbiguous =
false;
589 if (
auto *Info = State.dyn_cast<ModuleMacroInfo*>())
590 return Info->OverriddenMacros;
596 auto *Info = State.dyn_cast<ModuleMacroInfo*>();
598 if (Overrides.empty())
604 Info->OverriddenMacros.clear();
605 Info->OverriddenMacros.insert(Info->OverriddenMacros.end(),
606 Overrides.begin(), Overrides.end());
607 Info->ActiveModuleMacrosGeneration = 0;
616 using MacroMap = llvm::DenseMap<const IdentifierInfo *, MacroState>;
618 struct SubmoduleState;
621 struct BuildingSubmoduleInfo {
632 SubmoduleState *OuterSubmoduleState;
635 unsigned OuterPendingModuleMacroNames;
638 SubmoduleState *OuterSubmoduleState,
639 unsigned OuterPendingModuleMacroNames)
640 : M(M), ImportLoc(ImportLoc), IsPragma(IsPragma),
641 OuterSubmoduleState(OuterSubmoduleState),
642 OuterPendingModuleMacroNames(OuterPendingModuleMacroNames) {}
647 struct SubmoduleState {
657 std::map<Module *, SubmoduleState> Submodules;
660 SubmoduleState NullSubmoduleState;
664 SubmoduleState *CurSubmoduleState;
667 llvm::FoldingSet<ModuleMacro> ModuleMacros;
674 llvm::DenseMap<const IdentifierInfo *, llvm::TinyPtrVector<ModuleMacro *>>
686 using WarnUnusedMacroLocsTy = llvm::SmallPtrSet<SourceLocation, 32>;
687 WarnUnusedMacroLocsTy WarnUnusedMacroLocs;
695 llvm::DenseMap<IdentifierInfo *, std::vector<MacroInfo *>>
699 unsigned NumDirectives = 0;
700 unsigned NumDefined = 0;
701 unsigned NumUndefined = 0;
702 unsigned NumPragma = 0;
704 unsigned NumElse = 0;
705 unsigned NumEndif = 0;
706 unsigned NumEnteredSourceFiles = 0;
707 unsigned MaxIncludeStackDepth = 0;
708 unsigned NumMacroExpanded = 0;
709 unsigned NumFnMacroExpanded = 0;
710 unsigned NumBuiltinMacroExpanded = 0;
711 unsigned NumFastMacroExpanded = 0;
712 unsigned NumTokenPaste = 0;
713 unsigned NumFastTokenPaste = 0;
714 unsigned NumSkipped = 0;
718 std::string Predefines;
725 enum { TokenLexerCacheSize = 8 };
726 unsigned NumCachedTokenLexers;
727 std::unique_ptr<TokenLexer> TokenLexerCache[TokenLexerCacheSize];
736 std::vector<std::pair<TokenLexer *, size_t>> MacroExpandingLexersStack;
757 CachedTokensTy::size_type CachedLexPos = 0;
764 std::vector<CachedTokensTy::size_type> BacktrackPositions;
766 struct MacroInfoChain {
768 MacroInfoChain *Next;
773 MacroInfoChain *MIChainHead =
nullptr;
778 Preprocessor(std::shared_ptr<PreprocessorOptions> PPOpts,
783 bool OwnsHeaderSearch =
false,
803 void InitializeForModelFile();
806 void FinalizeForModelFile();
834 ExternalSource = Source;
838 return ExternalSource;
850 return ParsingIfOrElifDirective;
855 this->KeepComments = KeepComments | KeepMacroComments;
856 this->KeepMacroComments = KeepMacroComments;
865 SuppressIncludeNotFoundError = Suppress;
869 return SuppressIncludeNotFoundError;
875 PreprocessedOutput = IsPreprocessedOutput;
884 return CurPPLexer == L;
914 C = llvm::make_unique<PPChainedCallbacks>(std::move(C),
915 std::move(Callbacks));
916 Callbacks = std::move(C);
925 (!getLangOpts().Modules || (
bool)getMacroDefinition(II));
934 auto I = Submodules.find(M);
935 if (I == Submodules.end())
937 auto J = I->second.Macros.find(II);
938 if (J == I->second.Macros.end())
940 auto *MD = J->second.getLatest();
948 MacroState &S = CurSubmoduleState->Macros[II];
949 auto *MD = S.getLatest();
950 while (MD && isa<VisibilityMacroDirective>(MD))
953 S.getActiveModuleMacros(*
this, II),
954 S.isAmbiguous(*
this, II));
962 MacroState &S = CurSubmoduleState->Macros[II];
964 if (
auto *MD = S.getLatest())
968 S.getActiveModuleMacros(*
this, II),
969 S.isAmbiguous(*
this, II));
978 auto *MD = getLocalMacroDirectiveHistory(II);
986 return const_cast<Preprocessor*
>(
this)->getMacroInfo(II);
992 if (
auto MD = getMacroDefinition(II))
1009 appendMacroDirective(II, MD);
1029 updateOutOfDateIdentifier(const_cast<IdentifierInfo&>(*II));
1030 auto I = LeafModuleMacros.find(II);
1031 if (I != LeafModuleMacros.end())
1042 macro_iterator macro_begin(
bool IncludeExternalMacros =
true)
const;
1043 macro_iterator macro_end(
bool IncludeExternalMacros =
true)
const;
1044 llvm::iterator_range<macro_iterator>
1046 macros(
bool IncludeExternalMacros =
true)
const {
1047 return llvm::make_range(macro_begin(IncludeExternalMacros),
1048 macro_end(IncludeExternalMacros));
1069 return &Identifiers.
get(Name);
1076 void AddPragmaHandler(StringRef Namespace,
PragmaHandler *Handler);
1078 AddPragmaHandler(StringRef(), Handler);
1086 void RemovePragmaHandler(StringRef Namespace,
PragmaHandler *Handler);
1088 RemovePragmaHandler(StringRef(), Handler);
1092 void IgnorePragmas();
1104 CodeComplete = &Handler;
1109 return CodeComplete;
1114 CodeComplete =
nullptr;
1119 void CodeCompleteNaturalLanguage();
1123 CodeCompletionII = Filter;
1128 if (CodeCompletionII)
1129 return CodeCompletionII->
getName();
1139 void createPreprocessingRecord();
1143 void EnterMainSourceFile();
1146 void EndSourceFile();
1176 void EnterTokenStream(
const Token *Toks,
unsigned NumToks,
1177 bool DisableMacroExpansion,
bool OwnsTokens);
1181 bool DisableMacroExpansion) {
1182 EnterTokenStream(Toks.release(), NumToks, DisableMacroExpansion,
true);
1186 EnterTokenStream(Toks.data(), Toks.size(), DisableMacroExpansion,
false);
1193 void RemoveTopOfLexerStack();
1208 void EnableBacktrackAtThisPos();
1211 void CommitBacktrackedTokens();
1240 void Lex(
Token &Result);
1242 void LexAfterModuleImport(
Token &Result);
1247 return CurSubmoduleState->VisibleModules.getImportLoc(M);
1254 const char *DiagnosticTag,
bool AllowMacroExpansion) {
1255 if (AllowMacroExpansion)
1258 LexUnexpandedToken(Result);
1259 return FinishLexStringLiteral(Result, String, DiagnosticTag,
1260 AllowMacroExpansion);
1265 bool FinishLexStringLiteral(
Token &Result, std::string &String,
1266 const char *DiagnosticTag,
1267 bool AllowMacroExpansion);
1277 while (Result.
getKind() == tok::comment);
1283 bool OldVal = DisableMacroExpansion;
1284 DisableMacroExpansion =
true;
1289 DisableMacroExpansion = OldVal;
1296 LexUnexpandedToken(Result);
1297 while (Result.
getKind() == tok::comment);
1303 bool parseSimpleIntegerLiteral(
Token &
Tok, uint64_t &
Value);
1307 DisableMacroExpansion =
true;
1308 MacroExpansionInDirectivesOverride =
true;
1319 if (CachedLexPos + N < CachedTokens.size())
1320 return CachedTokens[CachedLexPos+N];
1322 return PeekAhead(N+1);
1331 assert(isBacktrackEnabled() &&
1332 "Should only be called when tokens are cached for backtracking");
1333 assert(
signed(CachedLexPos) -
signed(N) >=
signed(BacktrackPositions.back())
1334 &&
"Should revert tokens up to the last backtrack position, not more");
1335 assert(
signed(CachedLexPos) -
signed(N) >= 0 &&
1336 "Corrupted backtrack positions ?");
1345 EnterCachingLexMode();
1346 CachedTokens.insert(CachedTokens.begin()+CachedLexPos,
Tok);
1358 assert(Tok.
isAnnotation() &&
"Expected annotation token");
1359 if (CachedLexPos != 0 && isBacktrackEnabled())
1360 AnnotatePreviousCachedTokens(Tok);
1366 assert(CachedLexPos != 0);
1367 return CachedTokens[CachedLexPos-1].getLastLoc();
1372 bool IsPreviousCachedToken(
const Token &Tok)
const;
1390 assert(Tok.
isAnnotation() &&
"Expected annotation token");
1391 if (CachedLexPos != 0 && isBacktrackEnabled())
1392 CachedTokens[CachedLexPos-1] = Tok;
1397 void *AnnotationVal);
1403 if (CachedLexPos != 0 && isBacktrackEnabled())
1404 CachedTokens[CachedLexPos-1] = Tok;
1409 void recomputeCurLexerKind();
1416 IncrementalProcessing = value;
1433 bool SetCodeCompletionPoint(
const FileEntry *File,
1434 unsigned Line,
unsigned Column);
1450 return CodeCompletionFileLoc;
1459 assert(isCodeCompletionEnabled() &&
"Code-completion not enabled!");
1460 CodeCompletionReached =
true;
1462 getDiagnostics().setSuppressAllDiagnostics(
true);
1470 return PragmaARCCFCodeAuditedLoc;
1476 PragmaARCCFCodeAuditedLoc = Loc;
1484 return PragmaAssumeNonNullLoc;
1490 PragmaAssumeNonNullLoc = Loc;
1506 SkipMainFilePreamble.first = Bytes;
1507 SkipMainFilePreamble.second = StartOfLine;
1514 return Diags->
Report(Loc, DiagID);
1530 bool *invalid =
nullptr)
const {
1531 return Lexer::getSpelling(loc, buffer, SourceMgr, LangOpts, invalid);
1543 return Lexer::getSpelling(Tok, SourceMgr, LangOpts, Invalid);
1559 bool *Invalid =
nullptr)
const {
1560 return Lexer::getSpelling(Tok, Buffer, SourceMgr, LangOpts, Invalid);
1567 StringRef getSpelling(
const Token &Tok,
1569 bool *Invalid =
nullptr)
const;
1574 bool IgnoreWhiteSpace =
false) {
1575 return Lexer::getRawToken(Loc, Result, SourceMgr, LangOpts, IgnoreWhiteSpace);
1582 bool *Invalid =
nullptr)
const {
1583 assert(Tok.
is(tok::numeric_constant) &&
1584 Tok.
getLength() == 1 &&
"Called on unsupported token");
1585 assert(!Tok.
needsCleaning() &&
"Token can't need cleaning with length 1");
1605 return Lexer::getImmediateMacroName(Loc, SourceMgr, getLangOpts());
1613 void CreateString(StringRef Str,
Token &Tok,
1633 return Lexer::getLocForEndOfToken(Loc,
Offset, SourceMgr, LangOpts);
1643 return Lexer::isAtStartOfMacroExpansion(loc, SourceMgr, LangOpts,
1654 return Lexer::isAtEndOfMacroExpansion(loc, SourceMgr, LangOpts, MacroEnd);
1658 void DumpToken(
const Token &Tok,
bool DumpFlags =
false)
const;
1660 void DumpMacro(
const MacroInfo &MI)
const;
1666 unsigned Char)
const {
1667 return Lexer::AdvanceToTokenCharacter(TokStart, Char, SourceMgr, LangOpts);
1676 ++NumFastTokenPaste;
1683 size_t getTotalMemory()
const;
1688 void HandleMicrosoftCommentPaste(
Token &Tok);
1700 llvm::DenseMap<IdentifierInfo*,unsigned> PoisonReasons;
1710 void HandlePoisonedIdentifier(
Token & Tok);
1715 HandlePoisonedIdentifier(Identifier);
1725 *Ident___exception_code,
1726 *Ident_GetExceptionCode;
1729 *Ident___exception_info,
1730 *Ident_GetExceptionInfo;
1733 *Ident___abnormal_termination,
1734 *Ident_AbnormalTermination;
1736 const char *getCurLexerEndPos();
1737 void diagnoseMissingHeaderInUmbrellaDir(
const Module &Mod);
1740 void PoisonSEHIdentifiers(
bool Poison =
true);
1750 bool HandleIdentifier(
Token &Identifier);
1757 bool HandleEndOfFile(
Token &Result,
bool isEndOfMacro =
false);
1761 bool HandleEndOfTokenLexer(
Token &Result);
1768 void HandleDirective(
Token &Result);
1775 void CheckEndOfDirective(
const char *
Directive,
bool EnableMacros =
false);
1779 void DiscardUntilEndOfDirective();
1790 Module *getCurrentModule();
1817 bool *IsMapped,
bool SkipCache =
false);
1827 bool isInPrimaryFile()
const;
1849 bool CheckMacroName(
Token &MacroNameTok,
MacroUse isDefineUndef,
1850 bool *ShadowFlag =
nullptr);
1853 Module *LeaveSubmodule(
bool ForPragma);
1856 friend void TokenLexer::ExpandFunctionArguments();
1858 void PushIncludeMacroStack() {
1859 assert(CurLexerKind != CLK_CachingLexer &&
"cannot push a caching lexer");
1860 IncludeMacroStack.emplace_back(CurLexerKind, CurLexerSubmodule,
1861 std::move(CurLexer), std::move(CurPTHLexer),
1862 CurPPLexer, std::move(CurTokenLexer),
1864 CurPPLexer =
nullptr;
1867 void PopIncludeMacroStack() {
1868 CurLexer = std::move(IncludeMacroStack.back().TheLexer);
1869 CurPTHLexer = std::move(IncludeMacroStack.back().ThePTHLexer);
1870 CurPPLexer = IncludeMacroStack.back().ThePPLexer;
1871 CurTokenLexer = std::move(IncludeMacroStack.back().TheTokenLexer);
1872 CurDirLookup = IncludeMacroStack.back().TheDirLookup;
1873 CurLexerSubmodule = IncludeMacroStack.back().TheSubmodule;
1874 CurLexerKind = IncludeMacroStack.back().CurLexerKind;
1875 IncludeMacroStack.pop_back();
1878 void PropagateLineStartLeadingSpaceInfo(
Token &Result);
1882 bool needModuleMacros()
const;
1886 void updateModuleMacroInfo(
const IdentifierInfo *II, ModuleMacroInfo &Info);
1905 bool *ShadowFlag =
nullptr);
1916 MacroInfo *ReadOptionalMacroParameterListAndBody(
1917 const Token &MacroNameTok,
bool ImmediatelyAfterHeaderGuard);
1935 bool FoundNonSkipPortion,
bool FoundElse,
1939 void PTHSkipExcludedConditionalBlock();
1943 struct DirectiveEvalResult {
1948 bool IncludedUndefinedIds;
1955 DirectiveEvalResult EvaluateDirectiveExpression(
IdentifierInfo *&IfNDefMacro);
1959 void RegisterBuiltinPragmas();
1962 void RegisterBuiltinMacros();
1977 void removeCachedMacroExpandedTokensOfLastLexer();
1982 bool isNextPPTokenLParen();
1991 void ExpandBuiltinMacro(
Token &Tok);
1996 void Handle_Pragma(
Token &Tok);
2000 void HandleMicrosoft__pragma(
Token &Tok);
2011 void setPredefinesFileID(
FileID FID) {
2012 assert(PredefinesFileID.
isInvalid() &&
"PredefinesFileID already set!");
2013 PredefinesFileID = FID;
2022 static bool IsFileLexer(
const IncludeStackInfo& I) {
2023 return IsFileLexer(I.TheLexer.get(), I.ThePPLexer);
2026 bool IsFileLexer()
const {
2027 return IsFileLexer(CurLexer.get(), CurPPLexer);
2032 void CachingLex(
Token &Result);
2034 bool InCachingLexMode()
const {
2037 return !CurPPLexer && !CurTokenLexer && !CurPTHLexer &&
2038 !IncludeMacroStack.empty();
2041 void EnterCachingLexMode();
2043 void ExitCachingLexMode() {
2044 if (InCachingLexMode())
2045 RemoveTopOfLexerStack();
2048 const Token &PeekAhead(
unsigned N);
2049 void AnnotatePreviousCachedTokens(
const Token &Tok);
2055 void HandleLineDirective();
2056 void HandleDigitDirective(
Token &Tok);
2057 void HandleUserDiagnosticDirective(
Token &Tok,
bool isWarning);
2058 void HandleIdentSCCSDirective(
Token &Tok);
2059 void HandleMacroPublicDirective(
Token &Tok);
2060 void HandleMacroPrivateDirective();
2066 const FileEntry *LookupFromFile =
nullptr,
2067 bool isImport =
false);
2071 void HandleMicrosoftImportDirective(
Token &Tok);
2077 static bool checkModuleIsAvailable(
const LangOptions &LangOpts,
2103 return PreambleConditionalStack.isRecording();
2107 return PreambleConditionalStack.hasRecordedPreamble();
2111 return PreambleConditionalStack.getStack();
2115 PreambleConditionalStack.setStack(s);
2120 PreambleConditionalStack.startReplaying();
2121 PreambleConditionalStack.setStack(s);
2122 PreambleConditionalStack.SkipInfo = SkipInfo;
2126 return PreambleConditionalStack.SkipInfo;
2132 void replayPreambleConditionalStack();
2135 void HandleDefineDirective(
Token &Tok,
bool ImmediatelyAfterTopLevelIfndef);
2136 void HandleUndefDirective();
2139 void HandleIfdefDirective(
Token &Tok,
const Token &HashToken,
2140 bool isIfndef,
bool ReadAnyTokensBeforeDirective);
2141 void HandleIfDirective(
Token &Tok,
const Token &HashToken,
2142 bool ReadAnyTokensBeforeDirective);
2143 void HandleEndifDirective(
Token &Tok);
2144 void HandleElseDirective(
Token &Tok,
const Token &HashToken);
2145 void HandleElifDirective(
Token &Tok,
const Token &HashToken);
2152 void HandlePragmaOnce(
Token &OnceTok);
2153 void HandlePragmaMark();
2154 void HandlePragmaPoison();
2155 void HandlePragmaSystemHeader(
Token &SysHeaderTok);
2156 void HandlePragmaDependency(
Token &DependencyTok);
2157 void HandlePragmaPushMacro(
Token &Tok);
2158 void HandlePragmaPopMacro(
Token &Tok);
2159 void HandlePragmaIncludeAlias(
Token &Tok);
2160 void HandlePragmaModuleBuild(
Token &Tok);
2188 #endif // LLVM_CLANG_LEX_PREPROCESSOR_H IdentifierInfo * get(StringRef Name) override
get - Return the identifier token info for the specified named identifier.
PreambleSkipInfo(SourceLocation HashTokenLoc, SourceLocation IfTokenLoc, bool FoundNonSkipPortion, bool FoundElse, SourceLocation ElseLoc)
CachedTokensTy::size_type End
A set of visible modules.
llvm::BumpPtrAllocator & getPreprocessorAllocator()
Lexer - This provides a simple interface that turns a text buffer into a stream of tokens...
SelectorTable & getSelectorTable()
Holds information about both target-independent and target-specific builtins, allowing easy queries b...
Implements support for file system lookup, file system caching, and directory search management...
IdentifierInfo * getIdentifierInfo(StringRef Name) const
Return information about the specified preprocessor identifier token.
std::string getSpelling(const Token &Tok, bool *Invalid=nullptr) const
Return the 'spelling' of the Tok token.
MemoryBufferCache & getPCMCache() const
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
const Token & LookAhead(unsigned N)
Peeks ahead N tokens and returns that token without consuming any tokens.
void MaybeHandlePoisonedIdentifier(Token &Identifier)
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.
static const Builtin::Info BuiltinInfo[]
bool LexStringLiteral(Token &Result, std::string &String, const char *DiagnosticTag, bool AllowMacroExpansion)
Lex a string literal, which may be the concatenation of multiple string literals and may even come fr...
FileManager & getFileManager() const
Defines the clang::Module class, which describes a module in the source code.
bool isCodeCompletionReached() const
Returns true if code-completion is enabled and we have hit the code-completion point.
SourceLocation getPragmaARCCFCodeAuditedLoc() const
The location of the currently-active #pragma clang arc_cf_code_audited begin.
const char * getCharacterData(SourceLocation SL, bool *Invalid=nullptr) const
Return a pointer to the start of the specified location in the appropriate spelling MemoryBuffer...
Defines the clang::MacroInfo and clang::MacroDirective classes.
A description of the current definition of a macro.
bool isOutOfDate() const
Determine whether the information for this identifier is out of date with respect to the external sou...
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
A directive for an undefined macro.
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
void setCodeCompletionIdentifierInfo(IdentifierInfo *Filter)
Set the code completion token for filtering purposes.
void setCodeCompletionReached()
Note that we hit the code-completion point.
bool hadModuleLoaderFatalFailure() const
const DefInfo findDirectiveAtLoc(SourceLocation L, SourceManager &SM) const
Find macro definition active in the specified source location.
void EnterToken(const Token &Tok)
Enters a token in the token stream to be lexed next.
Manage memory buffers across multiple users.
bool getRawToken(SourceLocation Loc, Token &Result, bool IgnoreWhiteSpace=false)
Relex the token at the specified location.
bool operator==(const Token &Tok) const
Builtin::Context & getBuiltinInfo()
void setPredefines(const char *P)
Set the predefines for this Preprocessor.
SourceLocation getCodeCompletionFileLoc() const
Returns the start location of the file of code-completion point.
char getSpellingOfSingleCharacterNumericConstant(const Token &Tok, bool *Invalid=nullptr) const
Given a Token Tok that is a numeric constant with length 1, return the character. ...
This interface provides a way to observe the actions of the preprocessor as it does its thing...
bool isAnnotation() const
Return true if this is any of tok::annot_* kind tokens.
const MacroDirective * getPrevious() const
Get previous definition of the macro with the same name.
tok::TokenKind getKind() const
TokenValue(tok::TokenKind Kind)
SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset=0)
Computes the source location just past the end of the token at this source location.
DefMacroDirective * appendDefMacroDirective(IdentifierInfo *II, MacroInfo *MI, SourceLocation Loc)
One of these records is kept for each identifier that is lexed.
ArrayRef< PPConditionalInfo > getPreambleConditionalStack() const
Represents a macro directive exported by a module.
A directive for a defined macro or a macro imported from a module.
This table allows us to fully hide how we implement multi-keyword caching.
const TargetInfo & getTargetInfo() const
Token - This structure provides full information about a lexed token.
void SetCommentRetentionState(bool KeepComments, bool KeepMacroComments)
Control whether the preprocessor retains comments in output.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
const LangOptions & getLangOpts() const
Describes a module or submodule.
bool isPragmaLexer() const
isPragmaLexer - Returns true if this Lexer is being used to lex a pragma.
A directive for setting the module visibility of a macro.
void SetSuppressIncludeNotFoundError(bool Suppress)
SourceLocation AdvanceToTokenCharacter(SourceLocation TokStart, unsigned Char) const
Given a location that specifies the start of a token, return a new location that specifies a characte...
MacroUse
Context in which macro name is used.
llvm::Registry< PragmaHandler > PragmaHandlerRegistry
Registry of pragma handlers added by plugins.
Module * getCurrentLexerSubmodule() const
Return the submodule owning the file being lexed.
StringRef getSpelling(SourceLocation loc, SmallVectorImpl< char > &buffer, bool *invalid=nullptr) const
Return the 'spelling' of the token at the given location; does not go up to the spelling location or ...
A record of the steps taken while preprocessing a source file, including the various preprocessing di...
HeaderSearch & getHeaderSearchInfo() const
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
void LexNonComment(Token &Result)
Lex a token.
Concrete class used by the front-end to report problems and issues.
bool isMacroDefined(const IdentifierInfo *II)
bool isMacroDefinedInLocalModule(const IdentifierInfo *II, Module *M)
Determine whether II is defined as a macro within the module M, if that is a module that we've alread...
Defines the Diagnostic-related interfaces.
const TargetInfo * getAuxTargetInfo() const
TokenLexer - This implements a lexer that returns tokens from a macro body or token stream instead of...
void setCodeCompletionHandler(CodeCompletionHandler &Handler)
Set the code completion handler to the given object.
void setPreprocessedOutput(bool IsPreprocessedOutput)
Sets whether the preprocessor is responsible for producing output or if it is producing tokens to be ...
PragmaIntroducerKind
Describes how the pragma was introduced, e.g., with #pragma, _Pragma, or __pragma.
void LexUnexpandedToken(Token &Result)
Just like Lex, but disables macro expansion of identifier tokens.
A little helper class used to produce diagnostics.
void clearCodeCompletionHandler()
Clear out the code completion handler.
Provides lookups to, and iteration over, IdentiferInfo objects.
Exposes information about the current target.
const IdentifierTable & getIdentifierTable() const
bool isCodeCompletionEnabled() const
Determine if we are performing code completion.
Abstract interface for external sources of preprocessor information.
Defines the clang::LangOptions interface.
unsigned getCounterValue() const
const DefMacroDirective * getDirective() const
void AnnotateCachedTokens(const Token &Tok)
We notify the Preprocessor that if it is caching tokens (because backtrack is enabled) it should repl...
const AnnotatedLine * Line
SourceLocation HashTokenLoc
void SetMacroExpansionOnlyInDirectives()
Disables macro expansion everywhere except for preprocessor directives.
bool isCurrentLexer(const PreprocessorLexer *L) const
Return true if we are lexing directly from the specified lexer.
Implements an efficient mapping from strings to IdentifierInfo nodes.
MacroArgs - An instance of this class captures information about the formal arguments specified to a ...
void RevertCachedTokens(unsigned N)
When backtracking is enabled and tokens are cached, this allows to revert a specific number of tokens...
A class for tracking whether we're inside a VA_OPT during a traversal of the tokens of a variadic mac...
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file. ...
DefInfo getDefinition()
Traverses the macro directives history and returns the next macro definition directive along with inf...
void IncrementPasteCounter(bool isFast)
Increment the counters for the number of token paste operations performed.
Stores token information for comparing actual tokens with predefined values.
llvm::Optional< PreambleSkipInfo > getPreambleSkipInfo() const
DiagnosticBuilder Diag(const Token &Tok, unsigned DiagID) const
void setPragmaAssumeNonNullLoc(SourceLocation Loc)
Set the location of the currently-active #pragma clang assume_nonnull begin.
const MacroInfo * getMacroInfo(const IdentifierInfo *II) const
bool getCommentRetentionState() const
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
TokenValue(IdentifierInfo *II)
PPCallbacks * getPPCallbacks() const
Accessors for preprocessor callbacks.
DirectoryLookup - This class represents one entry in the search list that specifies the search order ...
bool isRecordingPreamble() const
const DirectoryLookup * GetCurDirLookup()
Get the DirectoryLookup structure used to find the current FileEntry, if CurLexer is non-null and if ...
SourceManager & getSourceManager() const
MacroDirective * getLocalMacroDirective(const IdentifierInfo *II) const
Given an identifier, return its latest non-imported MacroDirective if it is #define'd and not #undef'...
ExternalPreprocessorSource * getExternalSource() const
Encapsulates changes to the "macros namespace" (the location where the macro name became active...
void TypoCorrectToken(const Token &Tok)
Update the current token to represent the provided identifier, in order to cache an action performed ...
void setExternalSource(ExternalPreprocessorSource *Source)
Encodes a location in the source.
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
bool isAnnotation(TokenKind K)
Return true if this is any of tok::annot_* kinds.
void setPragmaARCCFCodeAuditedLoc(SourceLocation Loc)
Set the location of the currently-active #pragma clang arc_cf_code_audited begin. ...
MacroDefinition getMacroDefinition(const IdentifierInfo *II)
IdentifierInfo * getIdentifierInfo() const
IdentifierTable & getIdentifierTable()
void setPragmasEnabled(bool Enabled)
Cached information about one file (either on disk or in the virtual file system). ...
CodeCompletionHandler * getCodeCompletionHandler() const
Retrieve the current code-completion handler.
bool isAtStartOfMacroExpansion(SourceLocation loc, SourceLocation *MacroBegin=nullptr) const
Returns true if the given MacroID location points at the first token of the macro expansion...
bool isLiteral(TokenKind K)
Return true if this is a "literal" kind, like a numeric constant, string, etc.
bool isPoisoned() const
Return true if this token has been poisoned.
bool SawDateOrTime() const
Returns true if the preprocessor has seen a use of DATE or TIME in the file so far.
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
void enableIncrementalProcessing(bool value=true)
Enables the incremental processing.
MacroDefinition getMacroDefinitionAtLoc(const IdentifierInfo *II, SourceLocation Loc)
PreprocessorLexer * getCurrentLexer() const
Return the current lexer being lexed from.
bool isAtEndOfMacroExpansion(SourceLocation loc, SourceLocation *MacroEnd=nullptr) const
Returns true if the given MacroID location points at the last token of the macro expansion.
void ReplaceLastTokenWithAnnotation(const Token &Tok)
Replace the last token with an annotation token.
SourceLocation getPragmaAssumeNonNullLoc() const
The location of the currently-active #pragma clang assume_nonnull begin.
SourceLocation getCodeCompletionLoc() const
Returns the location of the code-completion point.
const MacroInfo * getMacroInfo() const
StringRef getName() const
Return the actual identifier string.
bool isMacroDefined(StringRef Id)
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
bool isBacktrackEnabled() const
True if EnableBacktrackAtThisPos() was called and caching of tokens is on.
bool hadMacroDefinition() const
Returns true if this identifier was #defined to some value at any moment.
Dataflow Directional Tag Classes.
SourceLocation IfTokenLoc
void EnterTokenStream(ArrayRef< Token > Toks, bool DisableMacroExpansion)
SmallVector< Token, 4 > CachedTokens
A set of tokens that has been cached for later parsing.
void EnterTokenStream(std::unique_ptr< Token[]> Toks, unsigned NumToks, bool DisableMacroExpansion)
PreprocessingRecord * getPreprocessingRecord() const
Retrieve the preprocessing record, or NULL if there is no preprocessing record.
Reads an AST files chain containing the contents of a translation unit.
PragmaHandler - Instances of this interface defined to handle the various pragmas that the language f...
llvm::iterator_range< macro_iterator > macros(bool IncludeExternalMacros=true) const
SourceLocation getLastCachedTokenLocation() const
Get the location of the last cached token, suitable for setting the end location of an annotation tok...
StringRef getCodeCompletionFilter()
Get the code completion token for filtering purposes.
Abstract interface for a module loader.
unsigned getLength() const
Encapsulates the data about a macro definition (e.g.
OnOffSwitch
Defines the possible values of an on-off-switch (C99 6.10.6p2).
const char * getLiteralData() const
getLiteralData - For a literal token (numeric constant, string, etc), this returns a pointer to the s...
bool getPragmasEnabled() const
unsigned getSpelling(const Token &Tok, const char *&Buffer, bool *Invalid=nullptr) const
Get the spelling of a token into a preallocated buffer, instead of as an std::string.
SourceLocation getDefinitionLoc() const
Return the location that the macro was defined at.
ModuleLoader & getModuleLoader() const
Retrieve the module loader associated with this preprocessor.
void LexUnexpandedNonComment(Token &Result)
Like LexNonComment, but this disables macro expansion of identifier tokens.
PreprocessorOptions & getPreprocessorOpts() const
Retrieve the preprocessor options used to initialize this preprocessor.
Cached information about one directory (either on disk or in the virtual file system).
Defines the PPCallbacks interface.
Defines the clang::TokenKind enum and support functions.
DefMacroDirective * appendDefMacroDirective(IdentifierInfo *II, MacroInfo *MI)
SourceLocation getModuleImportLoc(Module *M) const
Defines the clang::SourceLocation class and associated facilities.
void RemovePragmaHandler(PragmaHandler *Handler)
FileID getPredefinesFileID() const
Returns the FileID for the preprocessor predefines.
DiagnosticsEngine & getDiagnostics() const
void setMainFileDir(const DirectoryEntry *Dir)
Set the directory in which the main file should be considered to have been found, if it is not a real...
static bool isMacroDefined(const Sema &S, SourceLocation Loc, StringRef Name)
TranslationUnitKind
Describes the kind of translation unit being processed.
bool hasRecordedPreamble() const
const std::string & getPredefines() const
bool needsCleaning() const
Return true if this token has trigraphs or escaped newlines in it.
void setPredefines(StringRef P)
ArrayRef< ModuleMacro * > getLeafModuleMacros(const IdentifierInfo *II) const
Get the list of leaf (non-overridden) module macros for a name.
bool isIncrementalProcessingEnabled() const
Returns true if incremental processing is enabled.
The translation unit is a complete translation unit.
StringRef getImmediateMacroName(SourceLocation Loc)
Retrieve the name of the immediate macro expansion.
MacroMap::const_iterator macro_iterator
void setCounterValue(unsigned V)
bool isParsingIfOrElifDirective() const
True if we are currently preprocessing a if or #elif directive.
void setSkipMainFilePreamble(unsigned Bytes, bool StartOfLine)
Instruct the preprocessor to skip part of the main source file.
A trivial tuple used to represent a source range.
bool GetSuppressIncludeNotFoundError()
void setReplayablePreambleConditionalStack(ArrayRef< PPConditionalInfo > s, llvm::Optional< PreambleSkipInfo > SkipInfo)
Directive - Abstract class representing a parsed verify directive.
MacroInfo * getMacroInfo(const IdentifierInfo *II)
Callback handler that receives notifications when performing code completion within the preprocessor...
bool isPreprocessedOutput() const
Returns true if the preprocessor is responsible for generating output, false if it is producing token...
PTHManager * getPTHManager()
bool hasMacroDefinition() const
Return true if this identifier is #defined to some other value.
DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const
Forwarding function for diagnostics.
void addPPCallbacks(std::unique_ptr< PPCallbacks > C)
void setRecordedPreambleConditionalStack(ArrayRef< PPConditionalInfo > s)
This class handles loading and caching of source files into memory.
Defines enum values for all the target-independent builtin functions.
An RAII class that tracks when the Preprocessor starts and stops lexing the definition of a (ISO C/C+...
void AddPragmaHandler(PragmaHandler *Handler)
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
void setDiagnostics(DiagnosticsEngine &D)