10b57cec5SDimitry Andric //===- Preprocessor.h - C Language Family Preprocessor ----------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric /// \file 100b57cec5SDimitry Andric /// Defines the clang::Preprocessor interface. 110b57cec5SDimitry Andric // 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric #ifndef LLVM_CLANG_LEX_PREPROCESSOR_H 150b57cec5SDimitry Andric #define LLVM_CLANG_LEX_PREPROCESSOR_H 160b57cec5SDimitry Andric 170b57cec5SDimitry Andric #include "clang/Basic/Diagnostic.h" 18349cc55cSDimitry Andric #include "clang/Basic/DiagnosticIDs.h" 190b57cec5SDimitry Andric #include "clang/Basic/IdentifierTable.h" 200b57cec5SDimitry Andric #include "clang/Basic/LLVM.h" 210b57cec5SDimitry Andric #include "clang/Basic/LangOptions.h" 220b57cec5SDimitry Andric #include "clang/Basic/Module.h" 230b57cec5SDimitry Andric #include "clang/Basic/SourceLocation.h" 240b57cec5SDimitry Andric #include "clang/Basic/SourceManager.h" 250b57cec5SDimitry Andric #include "clang/Basic/TokenKinds.h" 2681ad6265SDimitry Andric #include "clang/Lex/HeaderSearch.h" 270b57cec5SDimitry Andric #include "clang/Lex/Lexer.h" 280b57cec5SDimitry Andric #include "clang/Lex/MacroInfo.h" 290b57cec5SDimitry Andric #include "clang/Lex/ModuleLoader.h" 300b57cec5SDimitry Andric #include "clang/Lex/ModuleMap.h" 310b57cec5SDimitry Andric #include "clang/Lex/PPCallbacks.h" 320b57cec5SDimitry Andric #include "clang/Lex/Token.h" 330b57cec5SDimitry Andric #include "clang/Lex/TokenLexer.h" 340b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h" 350b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 360b57cec5SDimitry Andric #include "llvm/ADT/FoldingSet.h" 370b57cec5SDimitry Andric #include "llvm/ADT/FunctionExtras.h" 380b57cec5SDimitry Andric #include "llvm/ADT/PointerUnion.h" 390b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 400b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h" 410b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 420b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 430b57cec5SDimitry Andric #include "llvm/ADT/TinyPtrVector.h" 440b57cec5SDimitry Andric #include "llvm/ADT/iterator_range.h" 450b57cec5SDimitry Andric #include "llvm/Support/Allocator.h" 460b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 470b57cec5SDimitry Andric #include "llvm/Support/Registry.h" 480b57cec5SDimitry Andric #include <cassert> 490b57cec5SDimitry Andric #include <cstddef> 500b57cec5SDimitry Andric #include <cstdint> 510b57cec5SDimitry Andric #include <map> 520b57cec5SDimitry Andric #include <memory> 53bdd1243dSDimitry Andric #include <optional> 540b57cec5SDimitry Andric #include <string> 550b57cec5SDimitry Andric #include <utility> 560b57cec5SDimitry Andric #include <vector> 570b57cec5SDimitry Andric 580b57cec5SDimitry Andric namespace llvm { 590b57cec5SDimitry Andric 600b57cec5SDimitry Andric template<unsigned InternalLen> class SmallString; 610b57cec5SDimitry Andric 620b57cec5SDimitry Andric } // namespace llvm 630b57cec5SDimitry Andric 640b57cec5SDimitry Andric namespace clang { 650b57cec5SDimitry Andric 660b57cec5SDimitry Andric class CodeCompletionHandler; 670b57cec5SDimitry Andric class CommentHandler; 680b57cec5SDimitry Andric class DirectoryEntry; 69e8d8bef9SDimitry Andric class EmptylineHandler; 700b57cec5SDimitry Andric class ExternalPreprocessorSource; 710b57cec5SDimitry Andric class FileEntry; 720b57cec5SDimitry Andric class FileManager; 730b57cec5SDimitry Andric class HeaderSearch; 740b57cec5SDimitry Andric class MacroArgs; 750b57cec5SDimitry Andric class PragmaHandler; 760b57cec5SDimitry Andric class PragmaNamespace; 770b57cec5SDimitry Andric class PreprocessingRecord; 780b57cec5SDimitry Andric class PreprocessorLexer; 790b57cec5SDimitry Andric class PreprocessorOptions; 800b57cec5SDimitry Andric class ScratchBuffer; 810b57cec5SDimitry Andric class TargetInfo; 820b57cec5SDimitry Andric 83480093f4SDimitry Andric namespace Builtin { 84480093f4SDimitry Andric class Context; 85480093f4SDimitry Andric } 86480093f4SDimitry Andric 870b57cec5SDimitry Andric /// Stores token information for comparing actual tokens with 880b57cec5SDimitry Andric /// predefined values. Only handles simple tokens and identifiers. 890b57cec5SDimitry Andric class TokenValue { 900b57cec5SDimitry Andric tok::TokenKind Kind; 910b57cec5SDimitry Andric IdentifierInfo *II; 920b57cec5SDimitry Andric 930b57cec5SDimitry Andric public: TokenValue(tok::TokenKind Kind)940b57cec5SDimitry Andric TokenValue(tok::TokenKind Kind) : Kind(Kind), II(nullptr) { 950b57cec5SDimitry Andric assert(Kind != tok::raw_identifier && "Raw identifiers are not supported."); 960b57cec5SDimitry Andric assert(Kind != tok::identifier && 970b57cec5SDimitry Andric "Identifiers should be created by TokenValue(IdentifierInfo *)"); 980b57cec5SDimitry Andric assert(!tok::isLiteral(Kind) && "Literals are not supported."); 990b57cec5SDimitry Andric assert(!tok::isAnnotation(Kind) && "Annotations are not supported."); 1000b57cec5SDimitry Andric } 1010b57cec5SDimitry Andric TokenValue(IdentifierInfo * II)1020b57cec5SDimitry Andric TokenValue(IdentifierInfo *II) : Kind(tok::identifier), II(II) {} 1030b57cec5SDimitry Andric 1040b57cec5SDimitry Andric bool operator==(const Token &Tok) const { 1050b57cec5SDimitry Andric return Tok.getKind() == Kind && 1060b57cec5SDimitry Andric (!II || II == Tok.getIdentifierInfo()); 1070b57cec5SDimitry Andric } 1080b57cec5SDimitry Andric }; 1090b57cec5SDimitry Andric 1100b57cec5SDimitry Andric /// Context in which macro name is used. 1110b57cec5SDimitry Andric enum MacroUse { 1120b57cec5SDimitry Andric // other than #define or #undef 1130b57cec5SDimitry Andric MU_Other = 0, 1140b57cec5SDimitry Andric 1150b57cec5SDimitry Andric // macro name specified in #define 1160b57cec5SDimitry Andric MU_Define = 1, 1170b57cec5SDimitry Andric 1180b57cec5SDimitry Andric // macro name specified in #undef 1190b57cec5SDimitry Andric MU_Undef = 2 1200b57cec5SDimitry Andric }; 1210b57cec5SDimitry Andric 1220b57cec5SDimitry Andric /// Engages in a tight little dance with the lexer to efficiently 1230b57cec5SDimitry Andric /// preprocess tokens. 1240b57cec5SDimitry Andric /// 1250b57cec5SDimitry Andric /// Lexers know only about tokens within a single source file, and don't 1260b57cec5SDimitry Andric /// know anything about preprocessor-level issues like the \#include stack, 1270b57cec5SDimitry Andric /// token expansion, etc. 1280b57cec5SDimitry Andric class Preprocessor { 1290b57cec5SDimitry Andric friend class VAOptDefinitionContext; 1300b57cec5SDimitry Andric friend class VariadicMacroScopeGuard; 1310b57cec5SDimitry Andric 1320b57cec5SDimitry Andric llvm::unique_function<void(const clang::Token &)> OnToken; 1330b57cec5SDimitry Andric std::shared_ptr<PreprocessorOptions> PPOpts; 1340b57cec5SDimitry Andric DiagnosticsEngine *Diags; 1355f757f3fSDimitry Andric const LangOptions &LangOpts; 1360b57cec5SDimitry Andric const TargetInfo *Target = nullptr; 1370b57cec5SDimitry Andric const TargetInfo *AuxTarget = nullptr; 1380b57cec5SDimitry Andric FileManager &FileMgr; 1390b57cec5SDimitry Andric SourceManager &SourceMgr; 1400b57cec5SDimitry Andric std::unique_ptr<ScratchBuffer> ScratchBuf; 1410b57cec5SDimitry Andric HeaderSearch &HeaderInfo; 1420b57cec5SDimitry Andric ModuleLoader &TheModuleLoader; 1430b57cec5SDimitry Andric 1440b57cec5SDimitry Andric /// External source of macros. 1450b57cec5SDimitry Andric ExternalPreprocessorSource *ExternalSource; 1460b57cec5SDimitry Andric 1470b57cec5SDimitry Andric /// A BumpPtrAllocator object used to quickly allocate and release 1480b57cec5SDimitry Andric /// objects internal to the Preprocessor. 1490b57cec5SDimitry Andric llvm::BumpPtrAllocator BP; 1500b57cec5SDimitry Andric 1510b57cec5SDimitry Andric /// Identifiers for builtin macros and other builtins. 1520b57cec5SDimitry Andric IdentifierInfo *Ident__LINE__, *Ident__FILE__; // __LINE__, __FILE__ 1530b57cec5SDimitry Andric IdentifierInfo *Ident__DATE__, *Ident__TIME__; // __DATE__, __TIME__ 1540b57cec5SDimitry Andric IdentifierInfo *Ident__INCLUDE_LEVEL__; // __INCLUDE_LEVEL__ 1550b57cec5SDimitry Andric IdentifierInfo *Ident__BASE_FILE__; // __BASE_FILE__ 1560b57cec5SDimitry Andric IdentifierInfo *Ident__FILE_NAME__; // __FILE_NAME__ 1570b57cec5SDimitry Andric IdentifierInfo *Ident__TIMESTAMP__; // __TIMESTAMP__ 1580b57cec5SDimitry Andric IdentifierInfo *Ident__COUNTER__; // __COUNTER__ 1590b57cec5SDimitry Andric IdentifierInfo *Ident_Pragma, *Ident__pragma; // _Pragma, __pragma 1600b57cec5SDimitry Andric IdentifierInfo *Ident__identifier; // __identifier 1610b57cec5SDimitry Andric IdentifierInfo *Ident__VA_ARGS__; // __VA_ARGS__ 1620b57cec5SDimitry Andric IdentifierInfo *Ident__VA_OPT__; // __VA_OPT__ 1630b57cec5SDimitry Andric IdentifierInfo *Ident__has_feature; // __has_feature 1640b57cec5SDimitry Andric IdentifierInfo *Ident__has_extension; // __has_extension 1650b57cec5SDimitry Andric IdentifierInfo *Ident__has_builtin; // __has_builtin 166bdd1243dSDimitry Andric IdentifierInfo *Ident__has_constexpr_builtin; // __has_constexpr_builtin 1670b57cec5SDimitry Andric IdentifierInfo *Ident__has_attribute; // __has_attribute 1680b57cec5SDimitry Andric IdentifierInfo *Ident__has_include; // __has_include 1690b57cec5SDimitry Andric IdentifierInfo *Ident__has_include_next; // __has_include_next 1700b57cec5SDimitry Andric IdentifierInfo *Ident__has_warning; // __has_warning 1710b57cec5SDimitry Andric IdentifierInfo *Ident__is_identifier; // __is_identifier 1720b57cec5SDimitry Andric IdentifierInfo *Ident__building_module; // __building_module 1730b57cec5SDimitry Andric IdentifierInfo *Ident__MODULE__; // __MODULE__ 1740b57cec5SDimitry Andric IdentifierInfo *Ident__has_cpp_attribute; // __has_cpp_attribute 1750b57cec5SDimitry Andric IdentifierInfo *Ident__has_c_attribute; // __has_c_attribute 1760b57cec5SDimitry Andric IdentifierInfo *Ident__has_declspec; // __has_declspec_attribute 1770b57cec5SDimitry Andric IdentifierInfo *Ident__is_target_arch; // __is_target_arch 1780b57cec5SDimitry Andric IdentifierInfo *Ident__is_target_vendor; // __is_target_vendor 1790b57cec5SDimitry Andric IdentifierInfo *Ident__is_target_os; // __is_target_os 1800b57cec5SDimitry Andric IdentifierInfo *Ident__is_target_environment; // __is_target_environment 181a4a491e2SDimitry Andric IdentifierInfo *Ident__is_target_variant_os; 182a4a491e2SDimitry Andric IdentifierInfo *Ident__is_target_variant_environment; 18381ad6265SDimitry Andric IdentifierInfo *Ident__FLT_EVAL_METHOD__; // __FLT_EVAL_METHOD 1840b57cec5SDimitry Andric 1850b57cec5SDimitry Andric // Weak, only valid (and set) while InMacroArgs is true. 1860b57cec5SDimitry Andric Token* ArgMacro; 1870b57cec5SDimitry Andric 1880b57cec5SDimitry Andric SourceLocation DATELoc, TIMELoc; 1890b57cec5SDimitry Andric 19081ad6265SDimitry Andric // FEM_UnsetOnCommandLine means that an explicit evaluation method was 19181ad6265SDimitry Andric // not specified on the command line. The target is queried to set the 19281ad6265SDimitry Andric // default evaluation method. 19381ad6265SDimitry Andric LangOptions::FPEvalMethodKind CurrentFPEvalMethod = 19481ad6265SDimitry Andric LangOptions::FPEvalMethodKind::FEM_UnsetOnCommandLine; 19581ad6265SDimitry Andric 19681ad6265SDimitry Andric // The most recent pragma location where the floating point evaluation 19781ad6265SDimitry Andric // method was modified. This is used to determine whether the 19881ad6265SDimitry Andric // 'pragma clang fp eval_method' was used whithin the current scope. 19981ad6265SDimitry Andric SourceLocation LastFPEvalPragmaLocation; 20081ad6265SDimitry Andric 20181ad6265SDimitry Andric LangOptions::FPEvalMethodKind TUFPEvalMethod = 20281ad6265SDimitry Andric LangOptions::FPEvalMethodKind::FEM_UnsetOnCommandLine; 20381ad6265SDimitry Andric 2040b57cec5SDimitry Andric // Next __COUNTER__ value, starts at 0. 2050b57cec5SDimitry Andric unsigned CounterValue = 0; 2060b57cec5SDimitry Andric 2070b57cec5SDimitry Andric enum { 2080b57cec5SDimitry Andric /// Maximum depth of \#includes. 2090b57cec5SDimitry Andric MaxAllowedIncludeStackDepth = 200 2100b57cec5SDimitry Andric }; 2110b57cec5SDimitry Andric 2120b57cec5SDimitry Andric // State that is set before the preprocessor begins. 2130b57cec5SDimitry Andric bool KeepComments : 1; 2140b57cec5SDimitry Andric bool KeepMacroComments : 1; 2150b57cec5SDimitry Andric bool SuppressIncludeNotFoundError : 1; 2160b57cec5SDimitry Andric 2170b57cec5SDimitry Andric // State that changes while the preprocessor runs: 2180b57cec5SDimitry Andric bool InMacroArgs : 1; // True if parsing fn macro invocation args. 2190b57cec5SDimitry Andric 2200b57cec5SDimitry Andric /// Whether the preprocessor owns the header search object. 2210b57cec5SDimitry Andric bool OwnsHeaderSearch : 1; 2220b57cec5SDimitry Andric 2230b57cec5SDimitry Andric /// True if macro expansion is disabled. 2240b57cec5SDimitry Andric bool DisableMacroExpansion : 1; 2250b57cec5SDimitry Andric 2260b57cec5SDimitry Andric /// Temporarily disables DisableMacroExpansion (i.e. enables expansion) 2270b57cec5SDimitry Andric /// when parsing preprocessor directives. 2280b57cec5SDimitry Andric bool MacroExpansionInDirectivesOverride : 1; 2290b57cec5SDimitry Andric 2300b57cec5SDimitry Andric class ResetMacroExpansionHelper; 2310b57cec5SDimitry Andric 2320b57cec5SDimitry Andric /// Whether we have already loaded macros from the external source. 2330b57cec5SDimitry Andric mutable bool ReadMacrosFromExternalSource : 1; 2340b57cec5SDimitry Andric 2350b57cec5SDimitry Andric /// True if pragmas are enabled. 2360b57cec5SDimitry Andric bool PragmasEnabled : 1; 2370b57cec5SDimitry Andric 2380b57cec5SDimitry Andric /// True if the current build action is a preprocessing action. 2390b57cec5SDimitry Andric bool PreprocessedOutput : 1; 2400b57cec5SDimitry Andric 2410b57cec5SDimitry Andric /// True if we are currently preprocessing a #if or #elif directive 2420b57cec5SDimitry Andric bool ParsingIfOrElifDirective; 2430b57cec5SDimitry Andric 2440b57cec5SDimitry Andric /// True if we are pre-expanding macro arguments. 2450b57cec5SDimitry Andric bool InMacroArgPreExpansion; 2460b57cec5SDimitry Andric 2470b57cec5SDimitry Andric /// Mapping/lookup information for all identifiers in 2480b57cec5SDimitry Andric /// the program, including program keywords. 2490b57cec5SDimitry Andric mutable IdentifierTable Identifiers; 2500b57cec5SDimitry Andric 2510b57cec5SDimitry Andric /// This table contains all the selectors in the program. 2520b57cec5SDimitry Andric /// 2530b57cec5SDimitry Andric /// Unlike IdentifierTable above, this table *isn't* populated by the 2540b57cec5SDimitry Andric /// preprocessor. It is declared/expanded here because its role/lifetime is 2550b57cec5SDimitry Andric /// conceptually similar to the IdentifierTable. In addition, the current 2560b57cec5SDimitry Andric /// control flow (in clang::ParseAST()), make it convenient to put here. 2570b57cec5SDimitry Andric /// 2580b57cec5SDimitry Andric /// FIXME: Make sure the lifetime of Identifiers/Selectors *isn't* tied to 2590b57cec5SDimitry Andric /// the lifetime of the preprocessor. 2600b57cec5SDimitry Andric SelectorTable Selectors; 2610b57cec5SDimitry Andric 2620b57cec5SDimitry Andric /// Information about builtins. 263480093f4SDimitry Andric std::unique_ptr<Builtin::Context> BuiltinInfo; 2640b57cec5SDimitry Andric 2650b57cec5SDimitry Andric /// Tracks all of the pragmas that the client registered 2660b57cec5SDimitry Andric /// with this preprocessor. 2670b57cec5SDimitry Andric std::unique_ptr<PragmaNamespace> PragmaHandlers; 2680b57cec5SDimitry Andric 2690b57cec5SDimitry Andric /// Pragma handlers of the original source is stored here during the 2700b57cec5SDimitry Andric /// parsing of a model file. 2710b57cec5SDimitry Andric std::unique_ptr<PragmaNamespace> PragmaHandlersBackup; 2720b57cec5SDimitry Andric 2730b57cec5SDimitry Andric /// Tracks all of the comment handlers that the client registered 2740b57cec5SDimitry Andric /// with this preprocessor. 2750b57cec5SDimitry Andric std::vector<CommentHandler *> CommentHandlers; 2760b57cec5SDimitry Andric 277e8d8bef9SDimitry Andric /// Empty line handler. 278e8d8bef9SDimitry Andric EmptylineHandler *Emptyline = nullptr; 279e8d8bef9SDimitry Andric 2805f757f3fSDimitry Andric /// True to avoid tearing down the lexer etc on EOF 2815f757f3fSDimitry Andric bool IncrementalProcessing = false; 2825f757f3fSDimitry Andric 283fe6060f1SDimitry Andric public: 2840b57cec5SDimitry Andric /// The kind of translation unit we are processing. 285fe6060f1SDimitry Andric const TranslationUnitKind TUKind; 2860b57cec5SDimitry Andric 287fe6060f1SDimitry Andric private: 2880b57cec5SDimitry Andric /// The code-completion handler. 2890b57cec5SDimitry Andric CodeCompletionHandler *CodeComplete = nullptr; 2900b57cec5SDimitry Andric 2910b57cec5SDimitry Andric /// The file that we're performing code-completion for, if any. 2920b57cec5SDimitry Andric const FileEntry *CodeCompletionFile = nullptr; 2930b57cec5SDimitry Andric 2940b57cec5SDimitry Andric /// The offset in file for the code-completion point. 2950b57cec5SDimitry Andric unsigned CodeCompletionOffset = 0; 2960b57cec5SDimitry Andric 2970b57cec5SDimitry Andric /// The location for the code-completion point. This gets instantiated 2980b57cec5SDimitry Andric /// when the CodeCompletionFile gets \#include'ed for preprocessing. 2990b57cec5SDimitry Andric SourceLocation CodeCompletionLoc; 3000b57cec5SDimitry Andric 3010b57cec5SDimitry Andric /// The start location for the file of the code-completion point. 3020b57cec5SDimitry Andric /// 3030b57cec5SDimitry Andric /// This gets instantiated when the CodeCompletionFile gets \#include'ed 3040b57cec5SDimitry Andric /// for preprocessing. 3050b57cec5SDimitry Andric SourceLocation CodeCompletionFileLoc; 3060b57cec5SDimitry Andric 3070b57cec5SDimitry Andric /// The source location of the \c import contextual keyword we just 3080b57cec5SDimitry Andric /// lexed, if any. 3090b57cec5SDimitry Andric SourceLocation ModuleImportLoc; 3100b57cec5SDimitry Andric 311bdd1243dSDimitry Andric /// The import path for named module that we're currently processing. 312bdd1243dSDimitry Andric SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> NamedModuleImportPath; 3130b57cec5SDimitry Andric 3141ac55f4cSDimitry Andric /// Whether the import is an `@import` or a standard c++ modules import. 3151ac55f4cSDimitry Andric bool IsAtImport = false; 3161ac55f4cSDimitry Andric 3170b57cec5SDimitry Andric /// Whether the last token we lexed was an '@'. 3180b57cec5SDimitry Andric bool LastTokenWasAt = false; 3190b57cec5SDimitry Andric 3200b57cec5SDimitry Andric /// A position within a C++20 import-seq. 321bdd1243dSDimitry Andric class StdCXXImportSeq { 3220b57cec5SDimitry Andric public: 3230b57cec5SDimitry Andric enum State : int { 3240b57cec5SDimitry Andric // Positive values represent a number of unclosed brackets. 3250b57cec5SDimitry Andric AtTopLevel = 0, 3260b57cec5SDimitry Andric AfterTopLevelTokenSeq = -1, 3270b57cec5SDimitry Andric AfterExport = -2, 3280b57cec5SDimitry Andric AfterImportSeq = -3, 3290b57cec5SDimitry Andric }; 3300b57cec5SDimitry Andric StdCXXImportSeq(State S)331bdd1243dSDimitry Andric StdCXXImportSeq(State S) : S(S) {} 3320b57cec5SDimitry Andric 3330b57cec5SDimitry Andric /// Saw any kind of open bracket. handleOpenBracket()3340b57cec5SDimitry Andric void handleOpenBracket() { 3350b57cec5SDimitry Andric S = static_cast<State>(std::max<int>(S, 0) + 1); 3360b57cec5SDimitry Andric } 3370b57cec5SDimitry Andric /// Saw any kind of close bracket other than '}'. handleCloseBracket()3380b57cec5SDimitry Andric void handleCloseBracket() { 3390b57cec5SDimitry Andric S = static_cast<State>(std::max<int>(S, 1) - 1); 3400b57cec5SDimitry Andric } 3410b57cec5SDimitry Andric /// Saw a close brace. handleCloseBrace()3420b57cec5SDimitry Andric void handleCloseBrace() { 3430b57cec5SDimitry Andric handleCloseBracket(); 3440b57cec5SDimitry Andric if (S == AtTopLevel && !AfterHeaderName) 3450b57cec5SDimitry Andric S = AfterTopLevelTokenSeq; 3460b57cec5SDimitry Andric } 3470b57cec5SDimitry Andric /// Saw a semicolon. handleSemi()3480b57cec5SDimitry Andric void handleSemi() { 3490b57cec5SDimitry Andric if (atTopLevel()) { 3500b57cec5SDimitry Andric S = AfterTopLevelTokenSeq; 3510b57cec5SDimitry Andric AfterHeaderName = false; 3520b57cec5SDimitry Andric } 3530b57cec5SDimitry Andric } 3540b57cec5SDimitry Andric 3550b57cec5SDimitry Andric /// Saw an 'export' identifier. handleExport()3560b57cec5SDimitry Andric void handleExport() { 3570b57cec5SDimitry Andric if (S == AfterTopLevelTokenSeq) 3580b57cec5SDimitry Andric S = AfterExport; 3590b57cec5SDimitry Andric else if (S <= 0) 3600b57cec5SDimitry Andric S = AtTopLevel; 3610b57cec5SDimitry Andric } 3620b57cec5SDimitry Andric /// Saw an 'import' identifier. handleImport()3630b57cec5SDimitry Andric void handleImport() { 3640b57cec5SDimitry Andric if (S == AfterTopLevelTokenSeq || S == AfterExport) 3650b57cec5SDimitry Andric S = AfterImportSeq; 3660b57cec5SDimitry Andric else if (S <= 0) 3670b57cec5SDimitry Andric S = AtTopLevel; 3680b57cec5SDimitry Andric } 3690b57cec5SDimitry Andric 3700b57cec5SDimitry Andric /// Saw a 'header-name' token; do not recognize any more 'import' tokens 3710b57cec5SDimitry Andric /// until we reach a top-level semicolon. handleHeaderName()3720b57cec5SDimitry Andric void handleHeaderName() { 3730b57cec5SDimitry Andric if (S == AfterImportSeq) 3740b57cec5SDimitry Andric AfterHeaderName = true; 3750b57cec5SDimitry Andric handleMisc(); 3760b57cec5SDimitry Andric } 3770b57cec5SDimitry Andric 3780b57cec5SDimitry Andric /// Saw any other token. handleMisc()3790b57cec5SDimitry Andric void handleMisc() { 3800b57cec5SDimitry Andric if (S <= 0) 3810b57cec5SDimitry Andric S = AtTopLevel; 3820b57cec5SDimitry Andric } 3830b57cec5SDimitry Andric atTopLevel()3840b57cec5SDimitry Andric bool atTopLevel() { return S <= 0; } afterImportSeq()3850b57cec5SDimitry Andric bool afterImportSeq() { return S == AfterImportSeq; } afterTopLevelSeq()386753f127fSDimitry Andric bool afterTopLevelSeq() { return S == AfterTopLevelTokenSeq; } 3870b57cec5SDimitry Andric 3880b57cec5SDimitry Andric private: 3890b57cec5SDimitry Andric State S; 3900b57cec5SDimitry Andric /// Whether we're in the pp-import-suffix following the header-name in a 3910b57cec5SDimitry Andric /// pp-import. If so, a close-brace is not sufficient to end the 3920b57cec5SDimitry Andric /// top-level-token-seq of an import-seq. 3930b57cec5SDimitry Andric bool AfterHeaderName = false; 3940b57cec5SDimitry Andric }; 3950b57cec5SDimitry Andric 3960b57cec5SDimitry Andric /// Our current position within a C++20 import-seq. 397bdd1243dSDimitry Andric StdCXXImportSeq StdCXXImportSeqState = StdCXXImportSeq::AfterTopLevelTokenSeq; 3980b57cec5SDimitry Andric 399753f127fSDimitry Andric /// Track whether we are in a Global Module Fragment 400753f127fSDimitry Andric class TrackGMF { 401753f127fSDimitry Andric public: 402753f127fSDimitry Andric enum GMFState : int { 403753f127fSDimitry Andric GMFActive = 1, 404753f127fSDimitry Andric MaybeGMF = 0, 405753f127fSDimitry Andric BeforeGMFIntroducer = -1, 406753f127fSDimitry Andric GMFAbsentOrEnded = -2, 407753f127fSDimitry Andric }; 408753f127fSDimitry Andric TrackGMF(GMFState S)409753f127fSDimitry Andric TrackGMF(GMFState S) : S(S) {} 410753f127fSDimitry Andric 411753f127fSDimitry Andric /// Saw a semicolon. handleSemi()412753f127fSDimitry Andric void handleSemi() { 413753f127fSDimitry Andric // If it is immediately after the first instance of the module keyword, 414753f127fSDimitry Andric // then that introduces the GMF. 415753f127fSDimitry Andric if (S == MaybeGMF) 416753f127fSDimitry Andric S = GMFActive; 417753f127fSDimitry Andric } 418753f127fSDimitry Andric 419753f127fSDimitry Andric /// Saw an 'export' identifier. handleExport()420753f127fSDimitry Andric void handleExport() { 421753f127fSDimitry Andric // The presence of an 'export' keyword always ends or excludes a GMF. 422753f127fSDimitry Andric S = GMFAbsentOrEnded; 423753f127fSDimitry Andric } 424753f127fSDimitry Andric 425753f127fSDimitry Andric /// Saw an 'import' identifier. handleImport(bool AfterTopLevelTokenSeq)426753f127fSDimitry Andric void handleImport(bool AfterTopLevelTokenSeq) { 427753f127fSDimitry Andric // If we see this before any 'module' kw, then we have no GMF. 428753f127fSDimitry Andric if (AfterTopLevelTokenSeq && S == BeforeGMFIntroducer) 429753f127fSDimitry Andric S = GMFAbsentOrEnded; 430753f127fSDimitry Andric } 431753f127fSDimitry Andric 432753f127fSDimitry Andric /// Saw a 'module' identifier. handleModule(bool AfterTopLevelTokenSeq)433753f127fSDimitry Andric void handleModule(bool AfterTopLevelTokenSeq) { 434753f127fSDimitry Andric // This was the first module identifier and not preceded by any token 435753f127fSDimitry Andric // that would exclude a GMF. It could begin a GMF, but only if directly 436753f127fSDimitry Andric // followed by a semicolon. 437753f127fSDimitry Andric if (AfterTopLevelTokenSeq && S == BeforeGMFIntroducer) 438753f127fSDimitry Andric S = MaybeGMF; 439753f127fSDimitry Andric else 440753f127fSDimitry Andric S = GMFAbsentOrEnded; 441753f127fSDimitry Andric } 442753f127fSDimitry Andric 443753f127fSDimitry Andric /// Saw any other token. handleMisc()444753f127fSDimitry Andric void handleMisc() { 445753f127fSDimitry Andric // We saw something other than ; after the 'module' kw, so not a GMF. 446753f127fSDimitry Andric if (S == MaybeGMF) 447753f127fSDimitry Andric S = GMFAbsentOrEnded; 448753f127fSDimitry Andric } 449753f127fSDimitry Andric inGMF()450753f127fSDimitry Andric bool inGMF() { return S == GMFActive; } 451753f127fSDimitry Andric 452753f127fSDimitry Andric private: 453753f127fSDimitry Andric /// Track the transitions into and out of a Global Module Fragment, 454753f127fSDimitry Andric /// if one is present. 455753f127fSDimitry Andric GMFState S; 456753f127fSDimitry Andric }; 457753f127fSDimitry Andric 458753f127fSDimitry Andric TrackGMF TrackGMFState = TrackGMF::BeforeGMFIntroducer; 459753f127fSDimitry Andric 4601ac55f4cSDimitry Andric /// Track the status of the c++20 module decl. 4611ac55f4cSDimitry Andric /// 4621ac55f4cSDimitry Andric /// module-declaration: 4631ac55f4cSDimitry Andric /// 'export'[opt] 'module' module-name module-partition[opt] 4641ac55f4cSDimitry Andric /// attribute-specifier-seq[opt] ';' 4651ac55f4cSDimitry Andric /// 4661ac55f4cSDimitry Andric /// module-name: 4671ac55f4cSDimitry Andric /// module-name-qualifier[opt] identifier 4681ac55f4cSDimitry Andric /// 4691ac55f4cSDimitry Andric /// module-partition: 4701ac55f4cSDimitry Andric /// ':' module-name-qualifier[opt] identifier 4711ac55f4cSDimitry Andric /// 4721ac55f4cSDimitry Andric /// module-name-qualifier: 4731ac55f4cSDimitry Andric /// identifier '.' 4741ac55f4cSDimitry Andric /// module-name-qualifier identifier '.' 4751ac55f4cSDimitry Andric /// 4761ac55f4cSDimitry Andric /// Transition state: 4771ac55f4cSDimitry Andric /// 4781ac55f4cSDimitry Andric /// NotAModuleDecl --- export ---> FoundExport 4791ac55f4cSDimitry Andric /// NotAModuleDecl --- module ---> ImplementationCandidate 4801ac55f4cSDimitry Andric /// FoundExport --- module ---> InterfaceCandidate 4811ac55f4cSDimitry Andric /// ImplementationCandidate --- Identifier ---> ImplementationCandidate 4821ac55f4cSDimitry Andric /// ImplementationCandidate --- period ---> ImplementationCandidate 4831ac55f4cSDimitry Andric /// ImplementationCandidate --- colon ---> ImplementationCandidate 4841ac55f4cSDimitry Andric /// InterfaceCandidate --- Identifier ---> InterfaceCandidate 4851ac55f4cSDimitry Andric /// InterfaceCandidate --- period ---> InterfaceCandidate 4861ac55f4cSDimitry Andric /// InterfaceCandidate --- colon ---> InterfaceCandidate 4871ac55f4cSDimitry Andric /// ImplementationCandidate --- Semi ---> NamedModuleImplementation 4881ac55f4cSDimitry Andric /// NamedModuleInterface --- Semi ---> NamedModuleInterface 4891ac55f4cSDimitry Andric /// NamedModuleImplementation --- Anything ---> NamedModuleImplementation 4901ac55f4cSDimitry Andric /// NamedModuleInterface --- Anything ---> NamedModuleInterface 4911ac55f4cSDimitry Andric /// 4921ac55f4cSDimitry Andric /// FIXME: We haven't handle attribute-specifier-seq here. It may not be bad 4931ac55f4cSDimitry Andric /// soon since we don't support any module attributes yet. 4941ac55f4cSDimitry Andric class ModuleDeclSeq { 4951ac55f4cSDimitry Andric enum ModuleDeclState : int { 4961ac55f4cSDimitry Andric NotAModuleDecl, 4971ac55f4cSDimitry Andric FoundExport, 4981ac55f4cSDimitry Andric InterfaceCandidate, 4991ac55f4cSDimitry Andric ImplementationCandidate, 5001ac55f4cSDimitry Andric NamedModuleInterface, 5011ac55f4cSDimitry Andric NamedModuleImplementation, 5021ac55f4cSDimitry Andric }; 5031ac55f4cSDimitry Andric 5041ac55f4cSDimitry Andric public: 50506c3fb27SDimitry Andric ModuleDeclSeq() = default; 5061ac55f4cSDimitry Andric handleExport()5071ac55f4cSDimitry Andric void handleExport() { 5081ac55f4cSDimitry Andric if (State == NotAModuleDecl) 5091ac55f4cSDimitry Andric State = FoundExport; 5101ac55f4cSDimitry Andric else if (!isNamedModule()) 5111ac55f4cSDimitry Andric reset(); 5121ac55f4cSDimitry Andric } 5131ac55f4cSDimitry Andric handleModule()5141ac55f4cSDimitry Andric void handleModule() { 5151ac55f4cSDimitry Andric if (State == FoundExport) 5161ac55f4cSDimitry Andric State = InterfaceCandidate; 5171ac55f4cSDimitry Andric else if (State == NotAModuleDecl) 5181ac55f4cSDimitry Andric State = ImplementationCandidate; 5191ac55f4cSDimitry Andric else if (!isNamedModule()) 5201ac55f4cSDimitry Andric reset(); 5211ac55f4cSDimitry Andric } 5221ac55f4cSDimitry Andric handleIdentifier(IdentifierInfo * Identifier)5231ac55f4cSDimitry Andric void handleIdentifier(IdentifierInfo *Identifier) { 5241ac55f4cSDimitry Andric if (isModuleCandidate() && Identifier) 5251ac55f4cSDimitry Andric Name += Identifier->getName().str(); 5261ac55f4cSDimitry Andric else if (!isNamedModule()) 5271ac55f4cSDimitry Andric reset(); 5281ac55f4cSDimitry Andric } 5291ac55f4cSDimitry Andric handleColon()5301ac55f4cSDimitry Andric void handleColon() { 5311ac55f4cSDimitry Andric if (isModuleCandidate()) 5321ac55f4cSDimitry Andric Name += ":"; 5331ac55f4cSDimitry Andric else if (!isNamedModule()) 5341ac55f4cSDimitry Andric reset(); 5351ac55f4cSDimitry Andric } 5361ac55f4cSDimitry Andric handlePeriod()5371ac55f4cSDimitry Andric void handlePeriod() { 5381ac55f4cSDimitry Andric if (isModuleCandidate()) 5391ac55f4cSDimitry Andric Name += "."; 5401ac55f4cSDimitry Andric else if (!isNamedModule()) 5411ac55f4cSDimitry Andric reset(); 5421ac55f4cSDimitry Andric } 5431ac55f4cSDimitry Andric handleSemi()5441ac55f4cSDimitry Andric void handleSemi() { 5451ac55f4cSDimitry Andric if (!Name.empty() && isModuleCandidate()) { 5461ac55f4cSDimitry Andric if (State == InterfaceCandidate) 5471ac55f4cSDimitry Andric State = NamedModuleInterface; 5481ac55f4cSDimitry Andric else if (State == ImplementationCandidate) 5491ac55f4cSDimitry Andric State = NamedModuleImplementation; 5501ac55f4cSDimitry Andric else 5511ac55f4cSDimitry Andric llvm_unreachable("Unimaged ModuleDeclState."); 5521ac55f4cSDimitry Andric } else if (!isNamedModule()) 5531ac55f4cSDimitry Andric reset(); 5541ac55f4cSDimitry Andric } 5551ac55f4cSDimitry Andric handleMisc()5561ac55f4cSDimitry Andric void handleMisc() { 5571ac55f4cSDimitry Andric if (!isNamedModule()) 5581ac55f4cSDimitry Andric reset(); 5591ac55f4cSDimitry Andric } 5601ac55f4cSDimitry Andric isModuleCandidate()5611ac55f4cSDimitry Andric bool isModuleCandidate() const { 5621ac55f4cSDimitry Andric return State == InterfaceCandidate || State == ImplementationCandidate; 5631ac55f4cSDimitry Andric } 5641ac55f4cSDimitry Andric isNamedModule()5651ac55f4cSDimitry Andric bool isNamedModule() const { 5661ac55f4cSDimitry Andric return State == NamedModuleInterface || 5671ac55f4cSDimitry Andric State == NamedModuleImplementation; 5681ac55f4cSDimitry Andric } 5691ac55f4cSDimitry Andric isNamedInterface()5701ac55f4cSDimitry Andric bool isNamedInterface() const { return State == NamedModuleInterface; } 5711ac55f4cSDimitry Andric isImplementationUnit()5721ac55f4cSDimitry Andric bool isImplementationUnit() const { 5731ac55f4cSDimitry Andric return State == NamedModuleImplementation && !getName().contains(':'); 5741ac55f4cSDimitry Andric } 5751ac55f4cSDimitry Andric getName()5761ac55f4cSDimitry Andric StringRef getName() const { 5771ac55f4cSDimitry Andric assert(isNamedModule() && "Can't get name from a non named module"); 5781ac55f4cSDimitry Andric return Name; 5791ac55f4cSDimitry Andric } 5801ac55f4cSDimitry Andric getPrimaryName()5811ac55f4cSDimitry Andric StringRef getPrimaryName() const { 5821ac55f4cSDimitry Andric assert(isNamedModule() && "Can't get name from a non named module"); 5831ac55f4cSDimitry Andric return getName().split(':').first; 5841ac55f4cSDimitry Andric } 5851ac55f4cSDimitry Andric reset()5861ac55f4cSDimitry Andric void reset() { 5871ac55f4cSDimitry Andric Name.clear(); 5881ac55f4cSDimitry Andric State = NotAModuleDecl; 5891ac55f4cSDimitry Andric } 5901ac55f4cSDimitry Andric 5911ac55f4cSDimitry Andric private: 59206c3fb27SDimitry Andric ModuleDeclState State = NotAModuleDecl; 5931ac55f4cSDimitry Andric std::string Name; 5941ac55f4cSDimitry Andric }; 5951ac55f4cSDimitry Andric 5961ac55f4cSDimitry Andric ModuleDeclSeq ModuleDeclState; 5971ac55f4cSDimitry Andric 5980b57cec5SDimitry Andric /// Whether the module import expects an identifier next. Otherwise, 5990b57cec5SDimitry Andric /// it expects a '.' or ';'. 6000b57cec5SDimitry Andric bool ModuleImportExpectsIdentifier = false; 6010b57cec5SDimitry Andric 602a7dea167SDimitry Andric /// The identifier and source location of the currently-active 6030b57cec5SDimitry Andric /// \#pragma clang arc_cf_code_audited begin. 604a7dea167SDimitry Andric std::pair<IdentifierInfo *, SourceLocation> PragmaARCCFCodeAuditedInfo; 6050b57cec5SDimitry Andric 6060b57cec5SDimitry Andric /// The source location of the currently-active 6070b57cec5SDimitry Andric /// \#pragma clang assume_nonnull begin. 6080b57cec5SDimitry Andric SourceLocation PragmaAssumeNonNullLoc; 6090b57cec5SDimitry Andric 61081ad6265SDimitry Andric /// Set only for preambles which end with an active 61181ad6265SDimitry Andric /// \#pragma clang assume_nonnull begin. 61281ad6265SDimitry Andric /// 61381ad6265SDimitry Andric /// When the preamble is loaded into the main file, 61481ad6265SDimitry Andric /// `PragmaAssumeNonNullLoc` will be set to this to 61581ad6265SDimitry Andric /// replay the unterminated assume_nonnull. 61681ad6265SDimitry Andric SourceLocation PreambleRecordedPragmaAssumeNonNullLoc; 61781ad6265SDimitry Andric 6180b57cec5SDimitry Andric /// True if we hit the code-completion point. 6190b57cec5SDimitry Andric bool CodeCompletionReached = false; 6200b57cec5SDimitry Andric 6210b57cec5SDimitry Andric /// The code completion token containing the information 6220b57cec5SDimitry Andric /// on the stem that is to be code completed. 6230b57cec5SDimitry Andric IdentifierInfo *CodeCompletionII = nullptr; 6240b57cec5SDimitry Andric 6250b57cec5SDimitry Andric /// Range for the code completion token. 6260b57cec5SDimitry Andric SourceRange CodeCompletionTokenRange; 6270b57cec5SDimitry Andric 6280b57cec5SDimitry Andric /// The directory that the main file should be considered to occupy, 6290b57cec5SDimitry Andric /// if it does not correspond to a real file (as happens when building a 6300b57cec5SDimitry Andric /// module). 63106c3fb27SDimitry Andric OptionalDirectoryEntryRef MainFileDir; 6320b57cec5SDimitry Andric 6330b57cec5SDimitry Andric /// The number of bytes that we will initially skip when entering the 6340b57cec5SDimitry Andric /// main file, along with a flag that indicates whether skipping this number 6350b57cec5SDimitry Andric /// of bytes will place the lexer at the start of a line. 6360b57cec5SDimitry Andric /// 6370b57cec5SDimitry Andric /// This is used when loading a precompiled preamble. 6380b57cec5SDimitry Andric std::pair<int, bool> SkipMainFilePreamble; 6390b57cec5SDimitry Andric 6400b57cec5SDimitry Andric /// Whether we hit an error due to reaching max allowed include depth. Allows 6410b57cec5SDimitry Andric /// to avoid hitting the same error over and over again. 6420b57cec5SDimitry Andric bool HasReachedMaxIncludeDepth = false; 6430b57cec5SDimitry Andric 6440b57cec5SDimitry Andric /// The number of currently-active calls to Lex. 6450b57cec5SDimitry Andric /// 6460b57cec5SDimitry Andric /// Lex is reentrant, and asking for an (end-of-phase-4) token can often 6470b57cec5SDimitry Andric /// require asking for multiple additional tokens. This counter makes it 6480b57cec5SDimitry Andric /// possible for Lex to detect whether it's producing a token for the end 6490b57cec5SDimitry Andric /// of phase 4 of translation or for some other situation. 6500b57cec5SDimitry Andric unsigned LexLevel = 0; 6510b57cec5SDimitry Andric 6525ffd83dbSDimitry Andric /// The number of (LexLevel 0) preprocessor tokens. 6535ffd83dbSDimitry Andric unsigned TokenCount = 0; 6545ffd83dbSDimitry Andric 655e8d8bef9SDimitry Andric /// Preprocess every token regardless of LexLevel. 656e8d8bef9SDimitry Andric bool PreprocessToken = false; 657e8d8bef9SDimitry Andric 6585ffd83dbSDimitry Andric /// The maximum number of (LexLevel 0) tokens before issuing a -Wmax-tokens 6595ffd83dbSDimitry Andric /// warning, or zero for unlimited. 6605ffd83dbSDimitry Andric unsigned MaxTokens = 0; 6615ffd83dbSDimitry Andric SourceLocation MaxTokensOverrideLoc; 6625ffd83dbSDimitry Andric 6630b57cec5SDimitry Andric public: 6640b57cec5SDimitry Andric struct PreambleSkipInfo { 6650b57cec5SDimitry Andric SourceLocation HashTokenLoc; 6660b57cec5SDimitry Andric SourceLocation IfTokenLoc; 6670b57cec5SDimitry Andric bool FoundNonSkipPortion; 6680b57cec5SDimitry Andric bool FoundElse; 6690b57cec5SDimitry Andric SourceLocation ElseLoc; 6700b57cec5SDimitry Andric PreambleSkipInfoPreambleSkipInfo6710b57cec5SDimitry Andric PreambleSkipInfo(SourceLocation HashTokenLoc, SourceLocation IfTokenLoc, 6720b57cec5SDimitry Andric bool FoundNonSkipPortion, bool FoundElse, 6730b57cec5SDimitry Andric SourceLocation ElseLoc) 6740b57cec5SDimitry Andric : HashTokenLoc(HashTokenLoc), IfTokenLoc(IfTokenLoc), 6750b57cec5SDimitry Andric FoundNonSkipPortion(FoundNonSkipPortion), FoundElse(FoundElse), 6760b57cec5SDimitry Andric ElseLoc(ElseLoc) {} 6770b57cec5SDimitry Andric }; 6780b57cec5SDimitry Andric 67904eeddc0SDimitry Andric using IncludedFilesSet = llvm::DenseSet<const FileEntry *>; 68004eeddc0SDimitry Andric 6810b57cec5SDimitry Andric private: 6820b57cec5SDimitry Andric friend class ASTReader; 6830b57cec5SDimitry Andric friend class MacroArgs; 6840b57cec5SDimitry Andric 6850b57cec5SDimitry Andric class PreambleConditionalStackStore { 6860b57cec5SDimitry Andric enum State { 6870b57cec5SDimitry Andric Off = 0, 6880b57cec5SDimitry Andric Recording = 1, 6890b57cec5SDimitry Andric Replaying = 2, 6900b57cec5SDimitry Andric }; 6910b57cec5SDimitry Andric 6920b57cec5SDimitry Andric public: 6930b57cec5SDimitry Andric PreambleConditionalStackStore() = default; 6940b57cec5SDimitry Andric startRecording()6950b57cec5SDimitry Andric void startRecording() { ConditionalStackState = Recording; } startReplaying()6960b57cec5SDimitry Andric void startReplaying() { ConditionalStackState = Replaying; } isRecording()6970b57cec5SDimitry Andric bool isRecording() const { return ConditionalStackState == Recording; } isReplaying()6980b57cec5SDimitry Andric bool isReplaying() const { return ConditionalStackState == Replaying; } 6990b57cec5SDimitry Andric getStack()7000b57cec5SDimitry Andric ArrayRef<PPConditionalInfo> getStack() const { 7010b57cec5SDimitry Andric return ConditionalStack; 7020b57cec5SDimitry Andric } 7030b57cec5SDimitry Andric doneReplaying()7040b57cec5SDimitry Andric void doneReplaying() { 7050b57cec5SDimitry Andric ConditionalStack.clear(); 7060b57cec5SDimitry Andric ConditionalStackState = Off; 7070b57cec5SDimitry Andric } 7080b57cec5SDimitry Andric setStack(ArrayRef<PPConditionalInfo> s)7090b57cec5SDimitry Andric void setStack(ArrayRef<PPConditionalInfo> s) { 7100b57cec5SDimitry Andric if (!isRecording() && !isReplaying()) 7110b57cec5SDimitry Andric return; 7120b57cec5SDimitry Andric ConditionalStack.clear(); 7130b57cec5SDimitry Andric ConditionalStack.append(s.begin(), s.end()); 7140b57cec5SDimitry Andric } 7150b57cec5SDimitry Andric hasRecordedPreamble()7160b57cec5SDimitry Andric bool hasRecordedPreamble() const { return !ConditionalStack.empty(); } 7170b57cec5SDimitry Andric reachedEOFWhileSkipping()71881ad6265SDimitry Andric bool reachedEOFWhileSkipping() const { return SkipInfo.has_value(); } 7190b57cec5SDimitry Andric clearSkipInfo()7200b57cec5SDimitry Andric void clearSkipInfo() { SkipInfo.reset(); } 7210b57cec5SDimitry Andric 722bdd1243dSDimitry Andric std::optional<PreambleSkipInfo> SkipInfo; 7230b57cec5SDimitry Andric 7240b57cec5SDimitry Andric private: 7250b57cec5SDimitry Andric SmallVector<PPConditionalInfo, 4> ConditionalStack; 7260b57cec5SDimitry Andric State ConditionalStackState = Off; 7270b57cec5SDimitry Andric } PreambleConditionalStack; 7280b57cec5SDimitry Andric 7290b57cec5SDimitry Andric /// The current top of the stack that we're lexing from if 7300b57cec5SDimitry Andric /// not expanding a macro and we are lexing directly from source code. 7310b57cec5SDimitry Andric /// 7320b57cec5SDimitry Andric /// Only one of CurLexer, or CurTokenLexer will be non-null. 7330b57cec5SDimitry Andric std::unique_ptr<Lexer> CurLexer; 7340b57cec5SDimitry Andric 73506c3fb27SDimitry Andric /// The current top of the stack that we're lexing from 7360b57cec5SDimitry Andric /// if not expanding a macro. 7370b57cec5SDimitry Andric /// 7380b57cec5SDimitry Andric /// This is an alias for CurLexer. 7390b57cec5SDimitry Andric PreprocessorLexer *CurPPLexer = nullptr; 7400b57cec5SDimitry Andric 7410b57cec5SDimitry Andric /// Used to find the current FileEntry, if CurLexer is non-null 7420b57cec5SDimitry Andric /// and if applicable. 7430b57cec5SDimitry Andric /// 7440b57cec5SDimitry Andric /// This allows us to implement \#include_next and find directory-specific 7450b57cec5SDimitry Andric /// properties. 74681ad6265SDimitry Andric ConstSearchDirIterator CurDirLookup = nullptr; 7470b57cec5SDimitry Andric 7480b57cec5SDimitry Andric /// The current macro we are expanding, if we are expanding a macro. 7490b57cec5SDimitry Andric /// 7500b57cec5SDimitry Andric /// One of CurLexer and CurTokenLexer must be null. 7510b57cec5SDimitry Andric std::unique_ptr<TokenLexer> CurTokenLexer; 7520b57cec5SDimitry Andric 7530b57cec5SDimitry Andric /// The kind of lexer we're currently working with. 7545f757f3fSDimitry Andric typedef bool (*LexerCallback)(Preprocessor &, Token &); 7555f757f3fSDimitry Andric LexerCallback CurLexerCallback = &CLK_Lexer; 7560b57cec5SDimitry Andric 7570b57cec5SDimitry Andric /// If the current lexer is for a submodule that is being built, this 7580b57cec5SDimitry Andric /// is that submodule. 7590b57cec5SDimitry Andric Module *CurLexerSubmodule = nullptr; 7600b57cec5SDimitry Andric 7610b57cec5SDimitry Andric /// Keeps track of the stack of files currently 7620b57cec5SDimitry Andric /// \#included, and macros currently being expanded from, not counting 7630b57cec5SDimitry Andric /// CurLexer/CurTokenLexer. 7640b57cec5SDimitry Andric struct IncludeStackInfo { 7655f757f3fSDimitry Andric LexerCallback CurLexerCallback; 7660b57cec5SDimitry Andric Module *TheSubmodule; 7670b57cec5SDimitry Andric std::unique_ptr<Lexer> TheLexer; 7680b57cec5SDimitry Andric PreprocessorLexer *ThePPLexer; 7690b57cec5SDimitry Andric std::unique_ptr<TokenLexer> TheTokenLexer; 77081ad6265SDimitry Andric ConstSearchDirIterator TheDirLookup; 7710b57cec5SDimitry Andric 7720b57cec5SDimitry Andric // The following constructors are completely useless copies of the default 7730b57cec5SDimitry Andric // versions, only needed to pacify MSVC. IncludeStackInfoIncludeStackInfo7745f757f3fSDimitry Andric IncludeStackInfo(LexerCallback CurLexerCallback, Module *TheSubmodule, 7750b57cec5SDimitry Andric std::unique_ptr<Lexer> &&TheLexer, 7760b57cec5SDimitry Andric PreprocessorLexer *ThePPLexer, 7770b57cec5SDimitry Andric std::unique_ptr<TokenLexer> &&TheTokenLexer, 77881ad6265SDimitry Andric ConstSearchDirIterator TheDirLookup) 7795f757f3fSDimitry Andric : CurLexerCallback(std::move(CurLexerCallback)), 7800b57cec5SDimitry Andric TheSubmodule(std::move(TheSubmodule)), TheLexer(std::move(TheLexer)), 7810b57cec5SDimitry Andric ThePPLexer(std::move(ThePPLexer)), 7820b57cec5SDimitry Andric TheTokenLexer(std::move(TheTokenLexer)), 7830b57cec5SDimitry Andric TheDirLookup(std::move(TheDirLookup)) {} 7840b57cec5SDimitry Andric }; 7850b57cec5SDimitry Andric std::vector<IncludeStackInfo> IncludeMacroStack; 7860b57cec5SDimitry Andric 7870b57cec5SDimitry Andric /// Actions invoked when some preprocessor activity is 7880b57cec5SDimitry Andric /// encountered (e.g. a file is \#included, etc). 7890b57cec5SDimitry Andric std::unique_ptr<PPCallbacks> Callbacks; 7900b57cec5SDimitry Andric 7910b57cec5SDimitry Andric struct MacroExpandsInfo { 7920b57cec5SDimitry Andric Token Tok; 7930b57cec5SDimitry Andric MacroDefinition MD; 7940b57cec5SDimitry Andric SourceRange Range; 7950b57cec5SDimitry Andric MacroExpandsInfoMacroExpandsInfo7960b57cec5SDimitry Andric MacroExpandsInfo(Token Tok, MacroDefinition MD, SourceRange Range) 7970b57cec5SDimitry Andric : Tok(Tok), MD(MD), Range(Range) {} 7980b57cec5SDimitry Andric }; 7990b57cec5SDimitry Andric SmallVector<MacroExpandsInfo, 2> DelayedMacroExpandsCallbacks; 8000b57cec5SDimitry Andric 8010b57cec5SDimitry Andric /// Information about a name that has been used to define a module macro. 8020b57cec5SDimitry Andric struct ModuleMacroInfo { 8030b57cec5SDimitry Andric /// The most recent macro directive for this identifier. 8040b57cec5SDimitry Andric MacroDirective *MD; 8050b57cec5SDimitry Andric 8060b57cec5SDimitry Andric /// The active module macros for this identifier. 8070b57cec5SDimitry Andric llvm::TinyPtrVector<ModuleMacro *> ActiveModuleMacros; 8080b57cec5SDimitry Andric 8090b57cec5SDimitry Andric /// The generation number at which we last updated ActiveModuleMacros. 8100b57cec5SDimitry Andric /// \see Preprocessor::VisibleModules. 8110b57cec5SDimitry Andric unsigned ActiveModuleMacrosGeneration = 0; 8120b57cec5SDimitry Andric 8130b57cec5SDimitry Andric /// Whether this macro name is ambiguous. 8140b57cec5SDimitry Andric bool IsAmbiguous = false; 8150b57cec5SDimitry Andric 8160b57cec5SDimitry Andric /// The module macros that are overridden by this macro. 8170b57cec5SDimitry Andric llvm::TinyPtrVector<ModuleMacro *> OverriddenMacros; 8180b57cec5SDimitry Andric ModuleMacroInfoModuleMacroInfo8190b57cec5SDimitry Andric ModuleMacroInfo(MacroDirective *MD) : MD(MD) {} 8200b57cec5SDimitry Andric }; 8210b57cec5SDimitry Andric 8220b57cec5SDimitry Andric /// The state of a macro for an identifier. 8230b57cec5SDimitry Andric class MacroState { 8240b57cec5SDimitry Andric mutable llvm::PointerUnion<MacroDirective *, ModuleMacroInfo *> State; 8250b57cec5SDimitry Andric getModuleInfo(Preprocessor & PP,const IdentifierInfo * II)8260b57cec5SDimitry Andric ModuleMacroInfo *getModuleInfo(Preprocessor &PP, 8270b57cec5SDimitry Andric const IdentifierInfo *II) const { 8280b57cec5SDimitry Andric if (II->isOutOfDate()) 8290b57cec5SDimitry Andric PP.updateOutOfDateIdentifier(const_cast<IdentifierInfo&>(*II)); 8300b57cec5SDimitry Andric // FIXME: Find a spare bit on IdentifierInfo and store a 8310b57cec5SDimitry Andric // HasModuleMacros flag. 8320b57cec5SDimitry Andric if (!II->hasMacroDefinition() || 8330b57cec5SDimitry Andric (!PP.getLangOpts().Modules && 8340b57cec5SDimitry Andric !PP.getLangOpts().ModulesLocalVisibility) || 8350b57cec5SDimitry Andric !PP.CurSubmoduleState->VisibleModules.getGeneration()) 8360b57cec5SDimitry Andric return nullptr; 8370b57cec5SDimitry Andric 8380b57cec5SDimitry Andric auto *Info = State.dyn_cast<ModuleMacroInfo*>(); 8390b57cec5SDimitry Andric if (!Info) { 8400b57cec5SDimitry Andric Info = new (PP.getPreprocessorAllocator()) 8410b57cec5SDimitry Andric ModuleMacroInfo(State.get<MacroDirective *>()); 8420b57cec5SDimitry Andric State = Info; 8430b57cec5SDimitry Andric } 8440b57cec5SDimitry Andric 8450b57cec5SDimitry Andric if (PP.CurSubmoduleState->VisibleModules.getGeneration() != 8460b57cec5SDimitry Andric Info->ActiveModuleMacrosGeneration) 8470b57cec5SDimitry Andric PP.updateModuleMacroInfo(II, *Info); 8480b57cec5SDimitry Andric return Info; 8490b57cec5SDimitry Andric } 8500b57cec5SDimitry Andric 8510b57cec5SDimitry Andric public: MacroState()8520b57cec5SDimitry Andric MacroState() : MacroState(nullptr) {} MacroState(MacroDirective * MD)8530b57cec5SDimitry Andric MacroState(MacroDirective *MD) : State(MD) {} 8540b57cec5SDimitry Andric MacroState(MacroState && O)8550b57cec5SDimitry Andric MacroState(MacroState &&O) noexcept : State(O.State) { 8560b57cec5SDimitry Andric O.State = (MacroDirective *)nullptr; 8570b57cec5SDimitry Andric } 8580b57cec5SDimitry Andric 8590b57cec5SDimitry Andric MacroState &operator=(MacroState &&O) noexcept { 8600b57cec5SDimitry Andric auto S = O.State; 8610b57cec5SDimitry Andric O.State = (MacroDirective *)nullptr; 8620b57cec5SDimitry Andric State = S; 8630b57cec5SDimitry Andric return *this; 8640b57cec5SDimitry Andric } 8650b57cec5SDimitry Andric ~MacroState()8660b57cec5SDimitry Andric ~MacroState() { 8670b57cec5SDimitry Andric if (auto *Info = State.dyn_cast<ModuleMacroInfo*>()) 8680b57cec5SDimitry Andric Info->~ModuleMacroInfo(); 8690b57cec5SDimitry Andric } 8700b57cec5SDimitry Andric getLatest()8710b57cec5SDimitry Andric MacroDirective *getLatest() const { 8720b57cec5SDimitry Andric if (auto *Info = State.dyn_cast<ModuleMacroInfo*>()) 8730b57cec5SDimitry Andric return Info->MD; 8740b57cec5SDimitry Andric return State.get<MacroDirective*>(); 8750b57cec5SDimitry Andric } 8760b57cec5SDimitry Andric setLatest(MacroDirective * MD)8770b57cec5SDimitry Andric void setLatest(MacroDirective *MD) { 8780b57cec5SDimitry Andric if (auto *Info = State.dyn_cast<ModuleMacroInfo*>()) 8790b57cec5SDimitry Andric Info->MD = MD; 8800b57cec5SDimitry Andric else 8810b57cec5SDimitry Andric State = MD; 8820b57cec5SDimitry Andric } 8830b57cec5SDimitry Andric isAmbiguous(Preprocessor & PP,const IdentifierInfo * II)8840b57cec5SDimitry Andric bool isAmbiguous(Preprocessor &PP, const IdentifierInfo *II) const { 8850b57cec5SDimitry Andric auto *Info = getModuleInfo(PP, II); 8860b57cec5SDimitry Andric return Info ? Info->IsAmbiguous : false; 8870b57cec5SDimitry Andric } 8880b57cec5SDimitry Andric 8890b57cec5SDimitry Andric ArrayRef<ModuleMacro *> getActiveModuleMacros(Preprocessor & PP,const IdentifierInfo * II)8900b57cec5SDimitry Andric getActiveModuleMacros(Preprocessor &PP, const IdentifierInfo *II) const { 8910b57cec5SDimitry Andric if (auto *Info = getModuleInfo(PP, II)) 8920b57cec5SDimitry Andric return Info->ActiveModuleMacros; 893bdd1243dSDimitry Andric return std::nullopt; 8940b57cec5SDimitry Andric } 8950b57cec5SDimitry Andric findDirectiveAtLoc(SourceLocation Loc,SourceManager & SourceMgr)8960b57cec5SDimitry Andric MacroDirective::DefInfo findDirectiveAtLoc(SourceLocation Loc, 8970b57cec5SDimitry Andric SourceManager &SourceMgr) const { 8980b57cec5SDimitry Andric // FIXME: Incorporate module macros into the result of this. 8990b57cec5SDimitry Andric if (auto *Latest = getLatest()) 9000b57cec5SDimitry Andric return Latest->findDirectiveAtLoc(Loc, SourceMgr); 9010b57cec5SDimitry Andric return {}; 9020b57cec5SDimitry Andric } 9030b57cec5SDimitry Andric overrideActiveModuleMacros(Preprocessor & PP,IdentifierInfo * II)9040b57cec5SDimitry Andric void overrideActiveModuleMacros(Preprocessor &PP, IdentifierInfo *II) { 9050b57cec5SDimitry Andric if (auto *Info = getModuleInfo(PP, II)) { 9060b57cec5SDimitry Andric Info->OverriddenMacros.insert(Info->OverriddenMacros.end(), 9070b57cec5SDimitry Andric Info->ActiveModuleMacros.begin(), 9080b57cec5SDimitry Andric Info->ActiveModuleMacros.end()); 9090b57cec5SDimitry Andric Info->ActiveModuleMacros.clear(); 9100b57cec5SDimitry Andric Info->IsAmbiguous = false; 9110b57cec5SDimitry Andric } 9120b57cec5SDimitry Andric } 9130b57cec5SDimitry Andric getOverriddenMacros()9140b57cec5SDimitry Andric ArrayRef<ModuleMacro*> getOverriddenMacros() const { 9150b57cec5SDimitry Andric if (auto *Info = State.dyn_cast<ModuleMacroInfo*>()) 9160b57cec5SDimitry Andric return Info->OverriddenMacros; 917bdd1243dSDimitry Andric return std::nullopt; 9180b57cec5SDimitry Andric } 9190b57cec5SDimitry Andric setOverriddenMacros(Preprocessor & PP,ArrayRef<ModuleMacro * > Overrides)9200b57cec5SDimitry Andric void setOverriddenMacros(Preprocessor &PP, 9210b57cec5SDimitry Andric ArrayRef<ModuleMacro *> Overrides) { 9220b57cec5SDimitry Andric auto *Info = State.dyn_cast<ModuleMacroInfo*>(); 9230b57cec5SDimitry Andric if (!Info) { 9240b57cec5SDimitry Andric if (Overrides.empty()) 9250b57cec5SDimitry Andric return; 9260b57cec5SDimitry Andric Info = new (PP.getPreprocessorAllocator()) 9270b57cec5SDimitry Andric ModuleMacroInfo(State.get<MacroDirective *>()); 9280b57cec5SDimitry Andric State = Info; 9290b57cec5SDimitry Andric } 9300b57cec5SDimitry Andric Info->OverriddenMacros.clear(); 9310b57cec5SDimitry Andric Info->OverriddenMacros.insert(Info->OverriddenMacros.end(), 9320b57cec5SDimitry Andric Overrides.begin(), Overrides.end()); 9330b57cec5SDimitry Andric Info->ActiveModuleMacrosGeneration = 0; 9340b57cec5SDimitry Andric } 9350b57cec5SDimitry Andric }; 9360b57cec5SDimitry Andric 9370b57cec5SDimitry Andric /// For each IdentifierInfo that was associated with a macro, we 9380b57cec5SDimitry Andric /// keep a mapping to the history of all macro definitions and #undefs in 9390b57cec5SDimitry Andric /// the reverse order (the latest one is in the head of the list). 9400b57cec5SDimitry Andric /// 9410b57cec5SDimitry Andric /// This mapping lives within the \p CurSubmoduleState. 9420b57cec5SDimitry Andric using MacroMap = llvm::DenseMap<const IdentifierInfo *, MacroState>; 9430b57cec5SDimitry Andric 9440b57cec5SDimitry Andric struct SubmoduleState; 9450b57cec5SDimitry Andric 9460b57cec5SDimitry Andric /// Information about a submodule that we're currently building. 9470b57cec5SDimitry Andric struct BuildingSubmoduleInfo { 9480b57cec5SDimitry Andric /// The module that we are building. 9490b57cec5SDimitry Andric Module *M; 9500b57cec5SDimitry Andric 9510b57cec5SDimitry Andric /// The location at which the module was included. 9520b57cec5SDimitry Andric SourceLocation ImportLoc; 9530b57cec5SDimitry Andric 9540b57cec5SDimitry Andric /// Whether we entered this submodule via a pragma. 9550b57cec5SDimitry Andric bool IsPragma; 9560b57cec5SDimitry Andric 9570b57cec5SDimitry Andric /// The previous SubmoduleState. 9580b57cec5SDimitry Andric SubmoduleState *OuterSubmoduleState; 9590b57cec5SDimitry Andric 9600b57cec5SDimitry Andric /// The number of pending module macro names when we started building this. 9610b57cec5SDimitry Andric unsigned OuterPendingModuleMacroNames; 9620b57cec5SDimitry Andric BuildingSubmoduleInfoBuildingSubmoduleInfo9630b57cec5SDimitry Andric BuildingSubmoduleInfo(Module *M, SourceLocation ImportLoc, bool IsPragma, 9640b57cec5SDimitry Andric SubmoduleState *OuterSubmoduleState, 9650b57cec5SDimitry Andric unsigned OuterPendingModuleMacroNames) 9660b57cec5SDimitry Andric : M(M), ImportLoc(ImportLoc), IsPragma(IsPragma), 9670b57cec5SDimitry Andric OuterSubmoduleState(OuterSubmoduleState), 9680b57cec5SDimitry Andric OuterPendingModuleMacroNames(OuterPendingModuleMacroNames) {} 9690b57cec5SDimitry Andric }; 9700b57cec5SDimitry Andric SmallVector<BuildingSubmoduleInfo, 8> BuildingSubmoduleStack; 9710b57cec5SDimitry Andric 9720b57cec5SDimitry Andric /// Information about a submodule's preprocessor state. 9730b57cec5SDimitry Andric struct SubmoduleState { 9740b57cec5SDimitry Andric /// The macros for the submodule. 9750b57cec5SDimitry Andric MacroMap Macros; 9760b57cec5SDimitry Andric 9770b57cec5SDimitry Andric /// The set of modules that are visible within the submodule. 9780b57cec5SDimitry Andric VisibleModuleSet VisibleModules; 9790b57cec5SDimitry Andric 9800b57cec5SDimitry Andric // FIXME: CounterValue? 9810b57cec5SDimitry Andric // FIXME: PragmaPushMacroInfo? 9820b57cec5SDimitry Andric }; 9830b57cec5SDimitry Andric std::map<Module *, SubmoduleState> Submodules; 9840b57cec5SDimitry Andric 9850b57cec5SDimitry Andric /// The preprocessor state for preprocessing outside of any submodule. 9860b57cec5SDimitry Andric SubmoduleState NullSubmoduleState; 9870b57cec5SDimitry Andric 9880b57cec5SDimitry Andric /// The current submodule state. Will be \p NullSubmoduleState if we're not 9890b57cec5SDimitry Andric /// in a submodule. 9900b57cec5SDimitry Andric SubmoduleState *CurSubmoduleState; 9910b57cec5SDimitry Andric 99204eeddc0SDimitry Andric /// The files that have been included. 99304eeddc0SDimitry Andric IncludedFilesSet IncludedFiles; 99404eeddc0SDimitry Andric 995bdd1243dSDimitry Andric /// The set of top-level modules that affected preprocessing, but were not 996bdd1243dSDimitry Andric /// imported. 997bdd1243dSDimitry Andric llvm::SmallSetVector<Module *, 2> AffectingClangModules; 998bdd1243dSDimitry Andric 9990b57cec5SDimitry Andric /// The set of known macros exported from modules. 10000b57cec5SDimitry Andric llvm::FoldingSet<ModuleMacro> ModuleMacros; 10010b57cec5SDimitry Andric 10020b57cec5SDimitry Andric /// The names of potential module macros that we've not yet processed. 10030b57cec5SDimitry Andric llvm::SmallVector<const IdentifierInfo *, 32> PendingModuleMacroNames; 10040b57cec5SDimitry Andric 10050b57cec5SDimitry Andric /// The list of module macros, for each identifier, that are not overridden by 10060b57cec5SDimitry Andric /// any other module macro. 10070b57cec5SDimitry Andric llvm::DenseMap<const IdentifierInfo *, llvm::TinyPtrVector<ModuleMacro *>> 10080b57cec5SDimitry Andric LeafModuleMacros; 10090b57cec5SDimitry Andric 10100b57cec5SDimitry Andric /// Macros that we want to warn because they are not used at the end 10110b57cec5SDimitry Andric /// of the translation unit. 10120b57cec5SDimitry Andric /// 10130b57cec5SDimitry Andric /// We store just their SourceLocations instead of 10140b57cec5SDimitry Andric /// something like MacroInfo*. The benefit of this is that when we are 10150b57cec5SDimitry Andric /// deserializing from PCH, we don't need to deserialize identifier & macros 10160b57cec5SDimitry Andric /// just so that we can report that they are unused, we just warn using 10170b57cec5SDimitry Andric /// the SourceLocations of this set (that will be filled by the ASTReader). 1018fe6060f1SDimitry Andric using WarnUnusedMacroLocsTy = llvm::SmallDenseSet<SourceLocation, 32>; 10190b57cec5SDimitry Andric WarnUnusedMacroLocsTy WarnUnusedMacroLocs; 10200b57cec5SDimitry Andric 1021349cc55cSDimitry Andric /// This is a pair of an optional message and source location used for pragmas 1022349cc55cSDimitry Andric /// that annotate macros like pragma clang restrict_expansion and pragma clang 1023349cc55cSDimitry Andric /// deprecated. This pair stores the optional message and the location of the 1024349cc55cSDimitry Andric /// annotation pragma for use producing diagnostics and notes. 1025349cc55cSDimitry Andric using MsgLocationPair = std::pair<std::string, SourceLocation>; 1026349cc55cSDimitry Andric 1027349cc55cSDimitry Andric struct MacroAnnotationInfo { 1028349cc55cSDimitry Andric SourceLocation Location; 1029349cc55cSDimitry Andric std::string Message; 1030349cc55cSDimitry Andric }; 1031349cc55cSDimitry Andric 1032349cc55cSDimitry Andric struct MacroAnnotations { 1033bdd1243dSDimitry Andric std::optional<MacroAnnotationInfo> DeprecationInfo; 1034bdd1243dSDimitry Andric std::optional<MacroAnnotationInfo> RestrictExpansionInfo; 1035bdd1243dSDimitry Andric std::optional<SourceLocation> FinalAnnotationLoc; 1036349cc55cSDimitry Andric makeDeprecationMacroAnnotations1037349cc55cSDimitry Andric static MacroAnnotations makeDeprecation(SourceLocation Loc, 1038349cc55cSDimitry Andric std::string Msg) { 1039349cc55cSDimitry Andric return MacroAnnotations{MacroAnnotationInfo{Loc, std::move(Msg)}, 1040bdd1243dSDimitry Andric std::nullopt, std::nullopt}; 1041349cc55cSDimitry Andric } 1042349cc55cSDimitry Andric makeRestrictExpansionMacroAnnotations1043349cc55cSDimitry Andric static MacroAnnotations makeRestrictExpansion(SourceLocation Loc, 1044349cc55cSDimitry Andric std::string Msg) { 1045349cc55cSDimitry Andric return MacroAnnotations{ 1046bdd1243dSDimitry Andric std::nullopt, MacroAnnotationInfo{Loc, std::move(Msg)}, std::nullopt}; 1047349cc55cSDimitry Andric } 1048349cc55cSDimitry Andric makeFinalMacroAnnotations1049349cc55cSDimitry Andric static MacroAnnotations makeFinal(SourceLocation Loc) { 1050bdd1243dSDimitry Andric return MacroAnnotations{std::nullopt, std::nullopt, Loc}; 1051349cc55cSDimitry Andric } 1052349cc55cSDimitry Andric }; 1053349cc55cSDimitry Andric 1054349cc55cSDimitry Andric /// Warning information for macro annotations. 1055349cc55cSDimitry Andric llvm::DenseMap<const IdentifierInfo *, MacroAnnotations> AnnotationInfos; 1056349cc55cSDimitry Andric 10570b57cec5SDimitry Andric /// A "freelist" of MacroArg objects that can be 10580b57cec5SDimitry Andric /// reused for quick allocation. 10590b57cec5SDimitry Andric MacroArgs *MacroArgCache = nullptr; 10600b57cec5SDimitry Andric 10610b57cec5SDimitry Andric /// For each IdentifierInfo used in a \#pragma push_macro directive, 10620b57cec5SDimitry Andric /// we keep a MacroInfo stack used to restore the previous macro value. 10630b57cec5SDimitry Andric llvm::DenseMap<IdentifierInfo *, std::vector<MacroInfo *>> 10640b57cec5SDimitry Andric PragmaPushMacroInfo; 10650b57cec5SDimitry Andric 10660b57cec5SDimitry Andric // Various statistics we track for performance analysis. 10670b57cec5SDimitry Andric unsigned NumDirectives = 0; 10680b57cec5SDimitry Andric unsigned NumDefined = 0; 10690b57cec5SDimitry Andric unsigned NumUndefined = 0; 10700b57cec5SDimitry Andric unsigned NumPragma = 0; 10710b57cec5SDimitry Andric unsigned NumIf = 0; 10720b57cec5SDimitry Andric unsigned NumElse = 0; 10730b57cec5SDimitry Andric unsigned NumEndif = 0; 10740b57cec5SDimitry Andric unsigned NumEnteredSourceFiles = 0; 10750b57cec5SDimitry Andric unsigned MaxIncludeStackDepth = 0; 10760b57cec5SDimitry Andric unsigned NumMacroExpanded = 0; 10770b57cec5SDimitry Andric unsigned NumFnMacroExpanded = 0; 10780b57cec5SDimitry Andric unsigned NumBuiltinMacroExpanded = 0; 10790b57cec5SDimitry Andric unsigned NumFastMacroExpanded = 0; 10800b57cec5SDimitry Andric unsigned NumTokenPaste = 0; 10810b57cec5SDimitry Andric unsigned NumFastTokenPaste = 0; 10820b57cec5SDimitry Andric unsigned NumSkipped = 0; 10830b57cec5SDimitry Andric 10840b57cec5SDimitry Andric /// The predefined macros that preprocessor should use from the 10850b57cec5SDimitry Andric /// command line etc. 10860b57cec5SDimitry Andric std::string Predefines; 10870b57cec5SDimitry Andric 10880b57cec5SDimitry Andric /// The file ID for the preprocessor predefines. 10890b57cec5SDimitry Andric FileID PredefinesFileID; 10900b57cec5SDimitry Andric 10910b57cec5SDimitry Andric /// The file ID for the PCH through header. 10920b57cec5SDimitry Andric FileID PCHThroughHeaderFileID; 10930b57cec5SDimitry Andric 10940b57cec5SDimitry Andric /// Whether tokens are being skipped until a #pragma hdrstop is seen. 10950b57cec5SDimitry Andric bool SkippingUntilPragmaHdrStop = false; 10960b57cec5SDimitry Andric 10970b57cec5SDimitry Andric /// Whether tokens are being skipped until the through header is seen. 10980b57cec5SDimitry Andric bool SkippingUntilPCHThroughHeader = false; 10990b57cec5SDimitry Andric 11000b57cec5SDimitry Andric /// \{ 11010b57cec5SDimitry Andric /// Cache of macro expanders to reduce malloc traffic. 11020b57cec5SDimitry Andric enum { TokenLexerCacheSize = 8 }; 11030b57cec5SDimitry Andric unsigned NumCachedTokenLexers; 11040b57cec5SDimitry Andric std::unique_ptr<TokenLexer> TokenLexerCache[TokenLexerCacheSize]; 11050b57cec5SDimitry Andric /// \} 11060b57cec5SDimitry Andric 11070b57cec5SDimitry Andric /// Keeps macro expanded tokens for TokenLexers. 11080b57cec5SDimitry Andric // 11090b57cec5SDimitry Andric /// Works like a stack; a TokenLexer adds the macro expanded tokens that is 11100b57cec5SDimitry Andric /// going to lex in the cache and when it finishes the tokens are removed 11110b57cec5SDimitry Andric /// from the end of the cache. 11120b57cec5SDimitry Andric SmallVector<Token, 16> MacroExpandedTokens; 11130b57cec5SDimitry Andric std::vector<std::pair<TokenLexer *, size_t>> MacroExpandingLexersStack; 11140b57cec5SDimitry Andric 11150b57cec5SDimitry Andric /// A record of the macro definitions and expansions that 11160b57cec5SDimitry Andric /// occurred during preprocessing. 11170b57cec5SDimitry Andric /// 11180b57cec5SDimitry Andric /// This is an optional side structure that can be enabled with 11190b57cec5SDimitry Andric /// \c createPreprocessingRecord() prior to preprocessing. 11200b57cec5SDimitry Andric PreprocessingRecord *Record = nullptr; 11210b57cec5SDimitry Andric 11220b57cec5SDimitry Andric /// Cached tokens state. 11230b57cec5SDimitry Andric using CachedTokensTy = SmallVector<Token, 1>; 11240b57cec5SDimitry Andric 11250b57cec5SDimitry Andric /// Cached tokens are stored here when we do backtracking or 11260b57cec5SDimitry Andric /// lookahead. They are "lexed" by the CachingLex() method. 11270b57cec5SDimitry Andric CachedTokensTy CachedTokens; 11280b57cec5SDimitry Andric 11290b57cec5SDimitry Andric /// The position of the cached token that CachingLex() should 11300b57cec5SDimitry Andric /// "lex" next. 11310b57cec5SDimitry Andric /// 11320b57cec5SDimitry Andric /// If it points beyond the CachedTokens vector, it means that a normal 11330b57cec5SDimitry Andric /// Lex() should be invoked. 11340b57cec5SDimitry Andric CachedTokensTy::size_type CachedLexPos = 0; 11350b57cec5SDimitry Andric 11360b57cec5SDimitry Andric /// Stack of backtrack positions, allowing nested backtracks. 11370b57cec5SDimitry Andric /// 11380b57cec5SDimitry Andric /// The EnableBacktrackAtThisPos() method pushes a position to 11390b57cec5SDimitry Andric /// indicate where CachedLexPos should be set when the BackTrack() method is 11400b57cec5SDimitry Andric /// invoked (at which point the last position is popped). 11410b57cec5SDimitry Andric std::vector<CachedTokensTy::size_type> BacktrackPositions; 11420b57cec5SDimitry Andric 114381ad6265SDimitry Andric /// True if \p Preprocessor::SkipExcludedConditionalBlock() is running. 114481ad6265SDimitry Andric /// This is used to guard against calling this function recursively. 114581ad6265SDimitry Andric /// 114681ad6265SDimitry Andric /// See comments at the use-site for more context about why it is needed. 114781ad6265SDimitry Andric bool SkippingExcludedConditionalBlock = false; 114881ad6265SDimitry Andric 114981ad6265SDimitry Andric /// Keeps track of skipped range mappings that were recorded while skipping 115081ad6265SDimitry Andric /// excluded conditional directives. It maps the source buffer pointer at 115181ad6265SDimitry Andric /// the beginning of a skipped block, to the number of bytes that should be 115281ad6265SDimitry Andric /// skipped. 115381ad6265SDimitry Andric llvm::DenseMap<const char *, unsigned> RecordedSkippedRanges; 115481ad6265SDimitry Andric 11550b57cec5SDimitry Andric void updateOutOfDateIdentifier(IdentifierInfo &II) const; 11560b57cec5SDimitry Andric 11570b57cec5SDimitry Andric public: 11580b57cec5SDimitry Andric Preprocessor(std::shared_ptr<PreprocessorOptions> PPOpts, 11595f757f3fSDimitry Andric DiagnosticsEngine &diags, const LangOptions &LangOpts, 11605f757f3fSDimitry Andric SourceManager &SM, HeaderSearch &Headers, 11615f757f3fSDimitry Andric ModuleLoader &TheModuleLoader, 11620b57cec5SDimitry Andric IdentifierInfoLookup *IILookup = nullptr, 11630b57cec5SDimitry Andric bool OwnsHeaderSearch = false, 11640b57cec5SDimitry Andric TranslationUnitKind TUKind = TU_Complete); 11650b57cec5SDimitry Andric 11660b57cec5SDimitry Andric ~Preprocessor(); 11670b57cec5SDimitry Andric 11680b57cec5SDimitry Andric /// Initialize the preprocessor using information about the target. 11690b57cec5SDimitry Andric /// 11700b57cec5SDimitry Andric /// \param Target is owned by the caller and must remain valid for the 11710b57cec5SDimitry Andric /// lifetime of the preprocessor. 11720b57cec5SDimitry Andric /// \param AuxTarget is owned by the caller and must remain valid for 11730b57cec5SDimitry Andric /// the lifetime of the preprocessor. 11740b57cec5SDimitry Andric void Initialize(const TargetInfo &Target, 11750b57cec5SDimitry Andric const TargetInfo *AuxTarget = nullptr); 11760b57cec5SDimitry Andric 11770b57cec5SDimitry Andric /// Initialize the preprocessor to parse a model file 11780b57cec5SDimitry Andric /// 11790b57cec5SDimitry Andric /// To parse model files the preprocessor of the original source is reused to 11800b57cec5SDimitry Andric /// preserver the identifier table. However to avoid some duplicate 11810b57cec5SDimitry Andric /// information in the preprocessor some cleanup is needed before it is used 11820b57cec5SDimitry Andric /// to parse model files. This method does that cleanup. 11830b57cec5SDimitry Andric void InitializeForModelFile(); 11840b57cec5SDimitry Andric 11850b57cec5SDimitry Andric /// Cleanup after model file parsing 11860b57cec5SDimitry Andric void FinalizeForModelFile(); 11870b57cec5SDimitry Andric 11880b57cec5SDimitry Andric /// Retrieve the preprocessor options used to initialize this 11890b57cec5SDimitry Andric /// preprocessor. getPreprocessorOpts()11900b57cec5SDimitry Andric PreprocessorOptions &getPreprocessorOpts() const { return *PPOpts; } 11910b57cec5SDimitry Andric getDiagnostics()11920b57cec5SDimitry Andric DiagnosticsEngine &getDiagnostics() const { return *Diags; } setDiagnostics(DiagnosticsEngine & D)11930b57cec5SDimitry Andric void setDiagnostics(DiagnosticsEngine &D) { Diags = &D; } 11940b57cec5SDimitry Andric getLangOpts()11950b57cec5SDimitry Andric const LangOptions &getLangOpts() const { return LangOpts; } getTargetInfo()11960b57cec5SDimitry Andric const TargetInfo &getTargetInfo() const { return *Target; } getAuxTargetInfo()11970b57cec5SDimitry Andric const TargetInfo *getAuxTargetInfo() const { return AuxTarget; } getFileManager()11980b57cec5SDimitry Andric FileManager &getFileManager() const { return FileMgr; } getSourceManager()11990b57cec5SDimitry Andric SourceManager &getSourceManager() const { return SourceMgr; } getHeaderSearchInfo()12000b57cec5SDimitry Andric HeaderSearch &getHeaderSearchInfo() const { return HeaderInfo; } 12010b57cec5SDimitry Andric getIdentifierTable()12020b57cec5SDimitry Andric IdentifierTable &getIdentifierTable() { return Identifiers; } getIdentifierTable()12030b57cec5SDimitry Andric const IdentifierTable &getIdentifierTable() const { return Identifiers; } getSelectorTable()12040b57cec5SDimitry Andric SelectorTable &getSelectorTable() { return Selectors; } getBuiltinInfo()1205480093f4SDimitry Andric Builtin::Context &getBuiltinInfo() { return *BuiltinInfo; } getPreprocessorAllocator()12060b57cec5SDimitry Andric llvm::BumpPtrAllocator &getPreprocessorAllocator() { return BP; } 12070b57cec5SDimitry Andric setExternalSource(ExternalPreprocessorSource * Source)12080b57cec5SDimitry Andric void setExternalSource(ExternalPreprocessorSource *Source) { 12090b57cec5SDimitry Andric ExternalSource = Source; 12100b57cec5SDimitry Andric } 12110b57cec5SDimitry Andric getExternalSource()12120b57cec5SDimitry Andric ExternalPreprocessorSource *getExternalSource() const { 12130b57cec5SDimitry Andric return ExternalSource; 12140b57cec5SDimitry Andric } 12150b57cec5SDimitry Andric 12160b57cec5SDimitry Andric /// Retrieve the module loader associated with this preprocessor. getModuleLoader()12170b57cec5SDimitry Andric ModuleLoader &getModuleLoader() const { return TheModuleLoader; } 12180b57cec5SDimitry Andric hadModuleLoaderFatalFailure()12190b57cec5SDimitry Andric bool hadModuleLoaderFatalFailure() const { 12200b57cec5SDimitry Andric return TheModuleLoader.HadFatalFailure; 12210b57cec5SDimitry Andric } 12220b57cec5SDimitry Andric 1223480093f4SDimitry Andric /// Retrieve the number of Directives that have been processed by the 1224480093f4SDimitry Andric /// Preprocessor. getNumDirectives()1225480093f4SDimitry Andric unsigned getNumDirectives() const { 1226480093f4SDimitry Andric return NumDirectives; 1227480093f4SDimitry Andric } 1228480093f4SDimitry Andric 12290b57cec5SDimitry Andric /// True if we are currently preprocessing a #if or #elif directive isParsingIfOrElifDirective()12300b57cec5SDimitry Andric bool isParsingIfOrElifDirective() const { 12310b57cec5SDimitry Andric return ParsingIfOrElifDirective; 12320b57cec5SDimitry Andric } 12330b57cec5SDimitry Andric 12340b57cec5SDimitry Andric /// Control whether the preprocessor retains comments in output. SetCommentRetentionState(bool KeepComments,bool KeepMacroComments)12350b57cec5SDimitry Andric void SetCommentRetentionState(bool KeepComments, bool KeepMacroComments) { 12360b57cec5SDimitry Andric this->KeepComments = KeepComments | KeepMacroComments; 12370b57cec5SDimitry Andric this->KeepMacroComments = KeepMacroComments; 12380b57cec5SDimitry Andric } 12390b57cec5SDimitry Andric getCommentRetentionState()12400b57cec5SDimitry Andric bool getCommentRetentionState() const { return KeepComments; } 12410b57cec5SDimitry Andric setPragmasEnabled(bool Enabled)12420b57cec5SDimitry Andric void setPragmasEnabled(bool Enabled) { PragmasEnabled = Enabled; } getPragmasEnabled()12430b57cec5SDimitry Andric bool getPragmasEnabled() const { return PragmasEnabled; } 12440b57cec5SDimitry Andric SetSuppressIncludeNotFoundError(bool Suppress)12450b57cec5SDimitry Andric void SetSuppressIncludeNotFoundError(bool Suppress) { 12460b57cec5SDimitry Andric SuppressIncludeNotFoundError = Suppress; 12470b57cec5SDimitry Andric } 12480b57cec5SDimitry Andric GetSuppressIncludeNotFoundError()12490b57cec5SDimitry Andric bool GetSuppressIncludeNotFoundError() { 12500b57cec5SDimitry Andric return SuppressIncludeNotFoundError; 12510b57cec5SDimitry Andric } 12520b57cec5SDimitry Andric 12530b57cec5SDimitry Andric /// Sets whether the preprocessor is responsible for producing output or if 12540b57cec5SDimitry Andric /// it is producing tokens to be consumed by Parse and Sema. setPreprocessedOutput(bool IsPreprocessedOutput)12550b57cec5SDimitry Andric void setPreprocessedOutput(bool IsPreprocessedOutput) { 12560b57cec5SDimitry Andric PreprocessedOutput = IsPreprocessedOutput; 12570b57cec5SDimitry Andric } 12580b57cec5SDimitry Andric 12590b57cec5SDimitry Andric /// Returns true if the preprocessor is responsible for generating output, 12600b57cec5SDimitry Andric /// false if it is producing tokens to be consumed by Parse and Sema. isPreprocessedOutput()12610b57cec5SDimitry Andric bool isPreprocessedOutput() const { return PreprocessedOutput; } 12620b57cec5SDimitry Andric 12630b57cec5SDimitry Andric /// Return true if we are lexing directly from the specified lexer. isCurrentLexer(const PreprocessorLexer * L)12640b57cec5SDimitry Andric bool isCurrentLexer(const PreprocessorLexer *L) const { 12650b57cec5SDimitry Andric return CurPPLexer == L; 12660b57cec5SDimitry Andric } 12670b57cec5SDimitry Andric 12680b57cec5SDimitry Andric /// Return the current lexer being lexed from. 12690b57cec5SDimitry Andric /// 12700b57cec5SDimitry Andric /// Note that this ignores any potentially active macro expansions and _Pragma 12710b57cec5SDimitry Andric /// expansions going on at the time. getCurrentLexer()12720b57cec5SDimitry Andric PreprocessorLexer *getCurrentLexer() const { return CurPPLexer; } 12730b57cec5SDimitry Andric 12740b57cec5SDimitry Andric /// Return the current file lexer being lexed from. 12750b57cec5SDimitry Andric /// 12760b57cec5SDimitry Andric /// Note that this ignores any potentially active macro expansions and _Pragma 12770b57cec5SDimitry Andric /// expansions going on at the time. 12780b57cec5SDimitry Andric PreprocessorLexer *getCurrentFileLexer() const; 12790b57cec5SDimitry Andric 12800b57cec5SDimitry Andric /// Return the submodule owning the file being lexed. This may not be 12810b57cec5SDimitry Andric /// the current module if we have changed modules since entering the file. getCurrentLexerSubmodule()12820b57cec5SDimitry Andric Module *getCurrentLexerSubmodule() const { return CurLexerSubmodule; } 12830b57cec5SDimitry Andric 12840b57cec5SDimitry Andric /// Returns the FileID for the preprocessor predefines. getPredefinesFileID()12850b57cec5SDimitry Andric FileID getPredefinesFileID() const { return PredefinesFileID; } 12860b57cec5SDimitry Andric 12870b57cec5SDimitry Andric /// \{ 12880b57cec5SDimitry Andric /// Accessors for preprocessor callbacks. 12890b57cec5SDimitry Andric /// 12900b57cec5SDimitry Andric /// Note that this class takes ownership of any PPCallbacks object given to 12910b57cec5SDimitry Andric /// it. getPPCallbacks()12920b57cec5SDimitry Andric PPCallbacks *getPPCallbacks() const { return Callbacks.get(); } addPPCallbacks(std::unique_ptr<PPCallbacks> C)12930b57cec5SDimitry Andric void addPPCallbacks(std::unique_ptr<PPCallbacks> C) { 12940b57cec5SDimitry Andric if (Callbacks) 1295a7dea167SDimitry Andric C = std::make_unique<PPChainedCallbacks>(std::move(C), 12960b57cec5SDimitry Andric std::move(Callbacks)); 12970b57cec5SDimitry Andric Callbacks = std::move(C); 12980b57cec5SDimitry Andric } 12990b57cec5SDimitry Andric /// \} 13000b57cec5SDimitry Andric 13015ffd83dbSDimitry Andric /// Get the number of tokens processed so far. getTokenCount()13025ffd83dbSDimitry Andric unsigned getTokenCount() const { return TokenCount; } 13035ffd83dbSDimitry Andric 13045ffd83dbSDimitry Andric /// Get the max number of tokens before issuing a -Wmax-tokens warning. getMaxTokens()13055ffd83dbSDimitry Andric unsigned getMaxTokens() const { return MaxTokens; } 13065ffd83dbSDimitry Andric overrideMaxTokens(unsigned Value,SourceLocation Loc)13075ffd83dbSDimitry Andric void overrideMaxTokens(unsigned Value, SourceLocation Loc) { 13085ffd83dbSDimitry Andric MaxTokens = Value; 13095ffd83dbSDimitry Andric MaxTokensOverrideLoc = Loc; 13105ffd83dbSDimitry Andric }; 13115ffd83dbSDimitry Andric getMaxTokensOverrideLoc()13125ffd83dbSDimitry Andric SourceLocation getMaxTokensOverrideLoc() const { return MaxTokensOverrideLoc; } 13135ffd83dbSDimitry Andric 13140b57cec5SDimitry Andric /// Register a function that would be called on each token in the final 13150b57cec5SDimitry Andric /// expanded token stream. 13160b57cec5SDimitry Andric /// This also reports annotation tokens produced by the parser. setTokenWatcher(llvm::unique_function<void (const clang::Token &)> F)13170b57cec5SDimitry Andric void setTokenWatcher(llvm::unique_function<void(const clang::Token &)> F) { 13180b57cec5SDimitry Andric OnToken = std::move(F); 13190b57cec5SDimitry Andric } 13200b57cec5SDimitry Andric setPreprocessToken(bool Preprocess)1321e8d8bef9SDimitry Andric void setPreprocessToken(bool Preprocess) { PreprocessToken = Preprocess; } 1322e8d8bef9SDimitry Andric isMacroDefined(StringRef Id)13230b57cec5SDimitry Andric bool isMacroDefined(StringRef Id) { 13240b57cec5SDimitry Andric return isMacroDefined(&Identifiers.get(Id)); 13250b57cec5SDimitry Andric } isMacroDefined(const IdentifierInfo * II)13260b57cec5SDimitry Andric bool isMacroDefined(const IdentifierInfo *II) { 13270b57cec5SDimitry Andric return II->hasMacroDefinition() && 13280b57cec5SDimitry Andric (!getLangOpts().Modules || (bool)getMacroDefinition(II)); 13290b57cec5SDimitry Andric } 13300b57cec5SDimitry Andric 13310b57cec5SDimitry Andric /// Determine whether II is defined as a macro within the module M, 13320b57cec5SDimitry Andric /// if that is a module that we've already preprocessed. Does not check for 13330b57cec5SDimitry Andric /// macros imported into M. isMacroDefinedInLocalModule(const IdentifierInfo * II,Module * M)13340b57cec5SDimitry Andric bool isMacroDefinedInLocalModule(const IdentifierInfo *II, Module *M) { 13350b57cec5SDimitry Andric if (!II->hasMacroDefinition()) 13360b57cec5SDimitry Andric return false; 13370b57cec5SDimitry Andric auto I = Submodules.find(M); 13380b57cec5SDimitry Andric if (I == Submodules.end()) 13390b57cec5SDimitry Andric return false; 13400b57cec5SDimitry Andric auto J = I->second.Macros.find(II); 13410b57cec5SDimitry Andric if (J == I->second.Macros.end()) 13420b57cec5SDimitry Andric return false; 13430b57cec5SDimitry Andric auto *MD = J->second.getLatest(); 13440b57cec5SDimitry Andric return MD && MD->isDefined(); 13450b57cec5SDimitry Andric } 13460b57cec5SDimitry Andric getMacroDefinition(const IdentifierInfo * II)13470b57cec5SDimitry Andric MacroDefinition getMacroDefinition(const IdentifierInfo *II) { 13480b57cec5SDimitry Andric if (!II->hasMacroDefinition()) 13490b57cec5SDimitry Andric return {}; 13500b57cec5SDimitry Andric 13510b57cec5SDimitry Andric MacroState &S = CurSubmoduleState->Macros[II]; 13520b57cec5SDimitry Andric auto *MD = S.getLatest(); 13530b57cec5SDimitry Andric while (MD && isa<VisibilityMacroDirective>(MD)) 13540b57cec5SDimitry Andric MD = MD->getPrevious(); 13550b57cec5SDimitry Andric return MacroDefinition(dyn_cast_or_null<DefMacroDirective>(MD), 13560b57cec5SDimitry Andric S.getActiveModuleMacros(*this, II), 13570b57cec5SDimitry Andric S.isAmbiguous(*this, II)); 13580b57cec5SDimitry Andric } 13590b57cec5SDimitry Andric getMacroDefinitionAtLoc(const IdentifierInfo * II,SourceLocation Loc)13600b57cec5SDimitry Andric MacroDefinition getMacroDefinitionAtLoc(const IdentifierInfo *II, 13610b57cec5SDimitry Andric SourceLocation Loc) { 13620b57cec5SDimitry Andric if (!II->hadMacroDefinition()) 13630b57cec5SDimitry Andric return {}; 13640b57cec5SDimitry Andric 13650b57cec5SDimitry Andric MacroState &S = CurSubmoduleState->Macros[II]; 13660b57cec5SDimitry Andric MacroDirective::DefInfo DI; 13670b57cec5SDimitry Andric if (auto *MD = S.getLatest()) 13680b57cec5SDimitry Andric DI = MD->findDirectiveAtLoc(Loc, getSourceManager()); 13690b57cec5SDimitry Andric // FIXME: Compute the set of active module macros at the specified location. 13700b57cec5SDimitry Andric return MacroDefinition(DI.getDirective(), 13710b57cec5SDimitry Andric S.getActiveModuleMacros(*this, II), 13720b57cec5SDimitry Andric S.isAmbiguous(*this, II)); 13730b57cec5SDimitry Andric } 13740b57cec5SDimitry Andric 13750b57cec5SDimitry Andric /// Given an identifier, return its latest non-imported MacroDirective 13760b57cec5SDimitry Andric /// if it is \#define'd and not \#undef'd, or null if it isn't \#define'd. getLocalMacroDirective(const IdentifierInfo * II)13770b57cec5SDimitry Andric MacroDirective *getLocalMacroDirective(const IdentifierInfo *II) const { 13780b57cec5SDimitry Andric if (!II->hasMacroDefinition()) 13790b57cec5SDimitry Andric return nullptr; 13800b57cec5SDimitry Andric 13810b57cec5SDimitry Andric auto *MD = getLocalMacroDirectiveHistory(II); 13820b57cec5SDimitry Andric if (!MD || MD->getDefinition().isUndefined()) 13830b57cec5SDimitry Andric return nullptr; 13840b57cec5SDimitry Andric 13850b57cec5SDimitry Andric return MD; 13860b57cec5SDimitry Andric } 13870b57cec5SDimitry Andric getMacroInfo(const IdentifierInfo * II)13880b57cec5SDimitry Andric const MacroInfo *getMacroInfo(const IdentifierInfo *II) const { 13890b57cec5SDimitry Andric return const_cast<Preprocessor*>(this)->getMacroInfo(II); 13900b57cec5SDimitry Andric } 13910b57cec5SDimitry Andric getMacroInfo(const IdentifierInfo * II)13920b57cec5SDimitry Andric MacroInfo *getMacroInfo(const IdentifierInfo *II) { 13930b57cec5SDimitry Andric if (!II->hasMacroDefinition()) 13940b57cec5SDimitry Andric return nullptr; 13950b57cec5SDimitry Andric if (auto MD = getMacroDefinition(II)) 13960b57cec5SDimitry Andric return MD.getMacroInfo(); 13970b57cec5SDimitry Andric return nullptr; 13980b57cec5SDimitry Andric } 13990b57cec5SDimitry Andric 14000b57cec5SDimitry Andric /// Given an identifier, return the latest non-imported macro 14010b57cec5SDimitry Andric /// directive for that identifier. 14020b57cec5SDimitry Andric /// 14030b57cec5SDimitry Andric /// One can iterate over all previous macro directives from the most recent 14040b57cec5SDimitry Andric /// one. 14050b57cec5SDimitry Andric MacroDirective *getLocalMacroDirectiveHistory(const IdentifierInfo *II) const; 14060b57cec5SDimitry Andric 14070b57cec5SDimitry Andric /// Add a directive to the macro directive history for this identifier. 14080b57cec5SDimitry Andric void appendMacroDirective(IdentifierInfo *II, MacroDirective *MD); appendDefMacroDirective(IdentifierInfo * II,MacroInfo * MI,SourceLocation Loc)14090b57cec5SDimitry Andric DefMacroDirective *appendDefMacroDirective(IdentifierInfo *II, MacroInfo *MI, 14100b57cec5SDimitry Andric SourceLocation Loc) { 14110b57cec5SDimitry Andric DefMacroDirective *MD = AllocateDefMacroDirective(MI, Loc); 14120b57cec5SDimitry Andric appendMacroDirective(II, MD); 14130b57cec5SDimitry Andric return MD; 14140b57cec5SDimitry Andric } appendDefMacroDirective(IdentifierInfo * II,MacroInfo * MI)14150b57cec5SDimitry Andric DefMacroDirective *appendDefMacroDirective(IdentifierInfo *II, 14160b57cec5SDimitry Andric MacroInfo *MI) { 14170b57cec5SDimitry Andric return appendDefMacroDirective(II, MI, MI->getDefinitionLoc()); 14180b57cec5SDimitry Andric } 14190b57cec5SDimitry Andric 14200b57cec5SDimitry Andric /// Set a MacroDirective that was loaded from a PCH file. 14210b57cec5SDimitry Andric void setLoadedMacroDirective(IdentifierInfo *II, MacroDirective *ED, 14220b57cec5SDimitry Andric MacroDirective *MD); 14230b57cec5SDimitry Andric 14240b57cec5SDimitry Andric /// Register an exported macro for a module and identifier. 14250b57cec5SDimitry Andric ModuleMacro *addModuleMacro(Module *Mod, IdentifierInfo *II, MacroInfo *Macro, 14260b57cec5SDimitry Andric ArrayRef<ModuleMacro *> Overrides, bool &IsNew); 1427fe6060f1SDimitry Andric ModuleMacro *getModuleMacro(Module *Mod, const IdentifierInfo *II); 14280b57cec5SDimitry Andric 14290b57cec5SDimitry Andric /// Get the list of leaf (non-overridden) module macros for a name. getLeafModuleMacros(const IdentifierInfo * II)14300b57cec5SDimitry Andric ArrayRef<ModuleMacro*> getLeafModuleMacros(const IdentifierInfo *II) const { 14310b57cec5SDimitry Andric if (II->isOutOfDate()) 14320b57cec5SDimitry Andric updateOutOfDateIdentifier(const_cast<IdentifierInfo&>(*II)); 14330b57cec5SDimitry Andric auto I = LeafModuleMacros.find(II); 14340b57cec5SDimitry Andric if (I != LeafModuleMacros.end()) 14350b57cec5SDimitry Andric return I->second; 1436bdd1243dSDimitry Andric return std::nullopt; 14370b57cec5SDimitry Andric } 14380b57cec5SDimitry Andric 1439fe6060f1SDimitry Andric /// Get the list of submodules that we're currently building. getBuildingSubmodules()1440fe6060f1SDimitry Andric ArrayRef<BuildingSubmoduleInfo> getBuildingSubmodules() const { 1441fe6060f1SDimitry Andric return BuildingSubmoduleStack; 1442fe6060f1SDimitry Andric } 1443fe6060f1SDimitry Andric 14440b57cec5SDimitry Andric /// \{ 14450b57cec5SDimitry Andric /// Iterators for the macro history table. Currently defined macros have 14460b57cec5SDimitry Andric /// IdentifierInfo::hasMacroDefinition() set and an empty 14470b57cec5SDimitry Andric /// MacroInfo::getUndefLoc() at the head of the list. 14480b57cec5SDimitry Andric using macro_iterator = MacroMap::const_iterator; 14490b57cec5SDimitry Andric 14500b57cec5SDimitry Andric macro_iterator macro_begin(bool IncludeExternalMacros = true) const; 14510b57cec5SDimitry Andric macro_iterator macro_end(bool IncludeExternalMacros = true) const; 14520b57cec5SDimitry Andric 14530b57cec5SDimitry Andric llvm::iterator_range<macro_iterator> 14540b57cec5SDimitry Andric macros(bool IncludeExternalMacros = true) const { 14550b57cec5SDimitry Andric macro_iterator begin = macro_begin(IncludeExternalMacros); 14560b57cec5SDimitry Andric macro_iterator end = macro_end(IncludeExternalMacros); 14570b57cec5SDimitry Andric return llvm::make_range(begin, end); 14580b57cec5SDimitry Andric } 14590b57cec5SDimitry Andric 14600b57cec5SDimitry Andric /// \} 14610b57cec5SDimitry Andric 1462bdd1243dSDimitry Andric /// Mark the given clang module as affecting the current clang module or translation unit. markClangModuleAsAffecting(Module * M)1463bdd1243dSDimitry Andric void markClangModuleAsAffecting(Module *M) { 1464bdd1243dSDimitry Andric assert(M->isModuleMapModule()); 1465bdd1243dSDimitry Andric if (!BuildingSubmoduleStack.empty()) { 1466bdd1243dSDimitry Andric if (M != BuildingSubmoduleStack.back().M) 1467bdd1243dSDimitry Andric BuildingSubmoduleStack.back().M->AffectingClangModules.insert(M); 1468bdd1243dSDimitry Andric } else { 1469bdd1243dSDimitry Andric AffectingClangModules.insert(M); 1470bdd1243dSDimitry Andric } 1471bdd1243dSDimitry Andric } 1472bdd1243dSDimitry Andric 1473bdd1243dSDimitry Andric /// Get the set of top-level clang modules that affected preprocessing, but were not 1474bdd1243dSDimitry Andric /// imported. getAffectingClangModules()1475bdd1243dSDimitry Andric const llvm::SmallSetVector<Module *, 2> &getAffectingClangModules() const { 1476bdd1243dSDimitry Andric return AffectingClangModules; 1477bdd1243dSDimitry Andric } 1478bdd1243dSDimitry Andric 147904eeddc0SDimitry Andric /// Mark the file as included. 148004eeddc0SDimitry Andric /// Returns true if this is the first time the file was included. markIncluded(FileEntryRef File)14815f757f3fSDimitry Andric bool markIncluded(FileEntryRef File) { 148204eeddc0SDimitry Andric HeaderInfo.getFileInfo(File); 148304eeddc0SDimitry Andric return IncludedFiles.insert(File).second; 148404eeddc0SDimitry Andric } 148504eeddc0SDimitry Andric 148604eeddc0SDimitry Andric /// Return true if this header has already been included. alreadyIncluded(FileEntryRef File)14875f757f3fSDimitry Andric bool alreadyIncluded(FileEntryRef File) const { 148806c3fb27SDimitry Andric HeaderInfo.getFileInfo(File); 148904eeddc0SDimitry Andric return IncludedFiles.count(File); 149004eeddc0SDimitry Andric } 149104eeddc0SDimitry Andric 149204eeddc0SDimitry Andric /// Get the set of included files. getIncludedFiles()149304eeddc0SDimitry Andric IncludedFilesSet &getIncludedFiles() { return IncludedFiles; } getIncludedFiles()149404eeddc0SDimitry Andric const IncludedFilesSet &getIncludedFiles() const { return IncludedFiles; } 149504eeddc0SDimitry Andric 14960b57cec5SDimitry Andric /// Return the name of the macro defined before \p Loc that has 14970b57cec5SDimitry Andric /// spelling \p Tokens. If there are multiple macros with same spelling, 14980b57cec5SDimitry Andric /// return the last one defined. 14990b57cec5SDimitry Andric StringRef getLastMacroWithSpelling(SourceLocation Loc, 15000b57cec5SDimitry Andric ArrayRef<TokenValue> Tokens) const; 15010b57cec5SDimitry Andric 1502a4a491e2SDimitry Andric /// Get the predefines for this processor. 1503a4a491e2SDimitry Andric /// Used by some third-party tools to inspect and add predefines (see 1504a4a491e2SDimitry Andric /// https://github.com/llvm/llvm-project/issues/57483). getPredefines()1505a4a491e2SDimitry Andric const std::string &getPredefines() const { return Predefines; } 1506a4a491e2SDimitry Andric 15070b57cec5SDimitry Andric /// Set the predefines for this Preprocessor. 15080b57cec5SDimitry Andric /// 15090b57cec5SDimitry Andric /// These predefines are automatically injected when parsing the main file. setPredefines(std::string P)151081ad6265SDimitry Andric void setPredefines(std::string P) { Predefines = std::move(P); } 15110b57cec5SDimitry Andric 15120b57cec5SDimitry Andric /// Return information about the specified preprocessor 15130b57cec5SDimitry Andric /// identifier token. getIdentifierInfo(StringRef Name)15140b57cec5SDimitry Andric IdentifierInfo *getIdentifierInfo(StringRef Name) const { 15150b57cec5SDimitry Andric return &Identifiers.get(Name); 15160b57cec5SDimitry Andric } 15170b57cec5SDimitry Andric 15180b57cec5SDimitry Andric /// Add the specified pragma handler to this preprocessor. 15190b57cec5SDimitry Andric /// 15200b57cec5SDimitry Andric /// If \p Namespace is non-null, then it is a token required to exist on the 15210b57cec5SDimitry Andric /// pragma line before the pragma string starts, e.g. "STDC" or "GCC". 15220b57cec5SDimitry Andric void AddPragmaHandler(StringRef Namespace, PragmaHandler *Handler); AddPragmaHandler(PragmaHandler * Handler)15230b57cec5SDimitry Andric void AddPragmaHandler(PragmaHandler *Handler) { 15240b57cec5SDimitry Andric AddPragmaHandler(StringRef(), Handler); 15250b57cec5SDimitry Andric } 15260b57cec5SDimitry Andric 15270b57cec5SDimitry Andric /// Remove the specific pragma handler from this preprocessor. 15280b57cec5SDimitry Andric /// 15290b57cec5SDimitry Andric /// If \p Namespace is non-null, then it should be the namespace that 15300b57cec5SDimitry Andric /// \p Handler was added to. It is an error to remove a handler that 15310b57cec5SDimitry Andric /// has not been registered. 15320b57cec5SDimitry Andric void RemovePragmaHandler(StringRef Namespace, PragmaHandler *Handler); RemovePragmaHandler(PragmaHandler * Handler)15330b57cec5SDimitry Andric void RemovePragmaHandler(PragmaHandler *Handler) { 15340b57cec5SDimitry Andric RemovePragmaHandler(StringRef(), Handler); 15350b57cec5SDimitry Andric } 15360b57cec5SDimitry Andric 15370b57cec5SDimitry Andric /// Install empty handlers for all pragmas (making them ignored). 15380b57cec5SDimitry Andric void IgnorePragmas(); 15390b57cec5SDimitry Andric 1540e8d8bef9SDimitry Andric /// Set empty line handler. setEmptylineHandler(EmptylineHandler * Handler)1541e8d8bef9SDimitry Andric void setEmptylineHandler(EmptylineHandler *Handler) { Emptyline = Handler; } 1542e8d8bef9SDimitry Andric getEmptylineHandler()1543e8d8bef9SDimitry Andric EmptylineHandler *getEmptylineHandler() const { return Emptyline; } 1544e8d8bef9SDimitry Andric 15450b57cec5SDimitry Andric /// Add the specified comment handler to the preprocessor. 15460b57cec5SDimitry Andric void addCommentHandler(CommentHandler *Handler); 15470b57cec5SDimitry Andric 15480b57cec5SDimitry Andric /// Remove the specified comment handler. 15490b57cec5SDimitry Andric /// 15500b57cec5SDimitry Andric /// It is an error to remove a handler that has not been registered. 15510b57cec5SDimitry Andric void removeCommentHandler(CommentHandler *Handler); 15520b57cec5SDimitry Andric 15530b57cec5SDimitry Andric /// Set the code completion handler to the given object. setCodeCompletionHandler(CodeCompletionHandler & Handler)15540b57cec5SDimitry Andric void setCodeCompletionHandler(CodeCompletionHandler &Handler) { 15550b57cec5SDimitry Andric CodeComplete = &Handler; 15560b57cec5SDimitry Andric } 15570b57cec5SDimitry Andric 15580b57cec5SDimitry Andric /// Retrieve the current code-completion handler. getCodeCompletionHandler()15590b57cec5SDimitry Andric CodeCompletionHandler *getCodeCompletionHandler() const { 15600b57cec5SDimitry Andric return CodeComplete; 15610b57cec5SDimitry Andric } 15620b57cec5SDimitry Andric 15630b57cec5SDimitry Andric /// Clear out the code completion handler. clearCodeCompletionHandler()15640b57cec5SDimitry Andric void clearCodeCompletionHandler() { 15650b57cec5SDimitry Andric CodeComplete = nullptr; 15660b57cec5SDimitry Andric } 15670b57cec5SDimitry Andric 15680b57cec5SDimitry Andric /// Hook used by the lexer to invoke the "included file" code 15690b57cec5SDimitry Andric /// completion point. 15700b57cec5SDimitry Andric void CodeCompleteIncludedFile(llvm::StringRef Dir, bool IsAngled); 15710b57cec5SDimitry Andric 15720b57cec5SDimitry Andric /// Hook used by the lexer to invoke the "natural language" code 15730b57cec5SDimitry Andric /// completion point. 15740b57cec5SDimitry Andric void CodeCompleteNaturalLanguage(); 15750b57cec5SDimitry Andric 15760b57cec5SDimitry Andric /// Set the code completion token for filtering purposes. setCodeCompletionIdentifierInfo(IdentifierInfo * Filter)15770b57cec5SDimitry Andric void setCodeCompletionIdentifierInfo(IdentifierInfo *Filter) { 15780b57cec5SDimitry Andric CodeCompletionII = Filter; 15790b57cec5SDimitry Andric } 15800b57cec5SDimitry Andric 15810b57cec5SDimitry Andric /// Set the code completion token range for detecting replacement range later 15820b57cec5SDimitry Andric /// on. setCodeCompletionTokenRange(const SourceLocation Start,const SourceLocation End)15830b57cec5SDimitry Andric void setCodeCompletionTokenRange(const SourceLocation Start, 15840b57cec5SDimitry Andric const SourceLocation End) { 15850b57cec5SDimitry Andric CodeCompletionTokenRange = {Start, End}; 15860b57cec5SDimitry Andric } getCodeCompletionTokenRange()15870b57cec5SDimitry Andric SourceRange getCodeCompletionTokenRange() const { 15880b57cec5SDimitry Andric return CodeCompletionTokenRange; 15890b57cec5SDimitry Andric } 15900b57cec5SDimitry Andric 15910b57cec5SDimitry Andric /// Get the code completion token for filtering purposes. getCodeCompletionFilter()15920b57cec5SDimitry Andric StringRef getCodeCompletionFilter() { 15930b57cec5SDimitry Andric if (CodeCompletionII) 15940b57cec5SDimitry Andric return CodeCompletionII->getName(); 15950b57cec5SDimitry Andric return {}; 15960b57cec5SDimitry Andric } 15970b57cec5SDimitry Andric 15980b57cec5SDimitry Andric /// Retrieve the preprocessing record, or NULL if there is no 15990b57cec5SDimitry Andric /// preprocessing record. getPreprocessingRecord()16000b57cec5SDimitry Andric PreprocessingRecord *getPreprocessingRecord() const { return Record; } 16010b57cec5SDimitry Andric 16020b57cec5SDimitry Andric /// Create a new preprocessing record, which will keep track of 16030b57cec5SDimitry Andric /// all macro expansions, macro definitions, etc. 16040b57cec5SDimitry Andric void createPreprocessingRecord(); 16050b57cec5SDimitry Andric 16060b57cec5SDimitry Andric /// Returns true if the FileEntry is the PCH through header. 16070b57cec5SDimitry Andric bool isPCHThroughHeader(const FileEntry *FE); 16080b57cec5SDimitry Andric 16090b57cec5SDimitry Andric /// True if creating a PCH with a through header. 16100b57cec5SDimitry Andric bool creatingPCHWithThroughHeader(); 16110b57cec5SDimitry Andric 16120b57cec5SDimitry Andric /// True if using a PCH with a through header. 16130b57cec5SDimitry Andric bool usingPCHWithThroughHeader(); 16140b57cec5SDimitry Andric 16150b57cec5SDimitry Andric /// True if creating a PCH with a #pragma hdrstop. 16160b57cec5SDimitry Andric bool creatingPCHWithPragmaHdrStop(); 16170b57cec5SDimitry Andric 16180b57cec5SDimitry Andric /// True if using a PCH with a #pragma hdrstop. 16190b57cec5SDimitry Andric bool usingPCHWithPragmaHdrStop(); 16200b57cec5SDimitry Andric 16210b57cec5SDimitry Andric /// Skip tokens until after the #include of the through header or 16220b57cec5SDimitry Andric /// until after a #pragma hdrstop. 16230b57cec5SDimitry Andric void SkipTokensWhileUsingPCH(); 16240b57cec5SDimitry Andric 16250b57cec5SDimitry Andric /// Process directives while skipping until the through header or 16260b57cec5SDimitry Andric /// #pragma hdrstop is found. 16270b57cec5SDimitry Andric void HandleSkippedDirectiveWhileUsingPCH(Token &Result, 16280b57cec5SDimitry Andric SourceLocation HashLoc); 16290b57cec5SDimitry Andric 16300b57cec5SDimitry Andric /// Enter the specified FileID as the main source file, 16310b57cec5SDimitry Andric /// which implicitly adds the builtin defines etc. 16320b57cec5SDimitry Andric void EnterMainSourceFile(); 16330b57cec5SDimitry Andric 16340b57cec5SDimitry Andric /// Inform the preprocessor callbacks that processing is complete. 16350b57cec5SDimitry Andric void EndSourceFile(); 16360b57cec5SDimitry Andric 16370b57cec5SDimitry Andric /// Add a source file to the top of the include stack and 16380b57cec5SDimitry Andric /// start lexing tokens from it instead of the current buffer. 16390b57cec5SDimitry Andric /// 16400b57cec5SDimitry Andric /// Emits a diagnostic, doesn't enter the file, and returns true on error. 164181ad6265SDimitry Andric bool EnterSourceFile(FileID FID, ConstSearchDirIterator Dir, 1642349cc55cSDimitry Andric SourceLocation Loc, bool IsFirstIncludeOfFile = true); 16430b57cec5SDimitry Andric 16440b57cec5SDimitry Andric /// Add a Macro to the top of the include stack and start lexing 16450b57cec5SDimitry Andric /// tokens from it instead of the current buffer. 16460b57cec5SDimitry Andric /// 16470b57cec5SDimitry Andric /// \param Args specifies the tokens input to a function-like macro. 16480b57cec5SDimitry Andric /// \param ILEnd specifies the location of the ')' for a function-like macro 16490b57cec5SDimitry Andric /// or the identifier for an object-like macro. 16500b57cec5SDimitry Andric void EnterMacro(Token &Tok, SourceLocation ILEnd, MacroInfo *Macro, 16510b57cec5SDimitry Andric MacroArgs *Args); 16520b57cec5SDimitry Andric 16530b57cec5SDimitry Andric private: 16540b57cec5SDimitry Andric /// Add a "macro" context to the top of the include stack, 16550b57cec5SDimitry Andric /// which will cause the lexer to start returning the specified tokens. 16560b57cec5SDimitry Andric /// 16570b57cec5SDimitry Andric /// If \p DisableMacroExpansion is true, tokens lexed from the token stream 16580b57cec5SDimitry Andric /// will not be subject to further macro expansion. Otherwise, these tokens 16590b57cec5SDimitry Andric /// will be re-macro-expanded when/if expansion is enabled. 16600b57cec5SDimitry Andric /// 16610b57cec5SDimitry Andric /// If \p OwnsTokens is false, this method assumes that the specified stream 16620b57cec5SDimitry Andric /// of tokens has a permanent owner somewhere, so they do not need to be 16630b57cec5SDimitry Andric /// copied. If it is true, it assumes the array of tokens is allocated with 16640b57cec5SDimitry Andric /// \c new[] and the Preprocessor will delete[] it. 16650b57cec5SDimitry Andric /// 16660b57cec5SDimitry Andric /// If \p IsReinject the resulting tokens will have Token::IsReinjected flag 16670b57cec5SDimitry Andric /// set, see the flag documentation for details. 16680b57cec5SDimitry Andric void EnterTokenStream(const Token *Toks, unsigned NumToks, 16690b57cec5SDimitry Andric bool DisableMacroExpansion, bool OwnsTokens, 16700b57cec5SDimitry Andric bool IsReinject); 16710b57cec5SDimitry Andric 16720b57cec5SDimitry Andric public: EnterTokenStream(std::unique_ptr<Token[]> Toks,unsigned NumToks,bool DisableMacroExpansion,bool IsReinject)16730b57cec5SDimitry Andric void EnterTokenStream(std::unique_ptr<Token[]> Toks, unsigned NumToks, 16740b57cec5SDimitry Andric bool DisableMacroExpansion, bool IsReinject) { 16750b57cec5SDimitry Andric EnterTokenStream(Toks.release(), NumToks, DisableMacroExpansion, true, 16760b57cec5SDimitry Andric IsReinject); 16770b57cec5SDimitry Andric } 16780b57cec5SDimitry Andric EnterTokenStream(ArrayRef<Token> Toks,bool DisableMacroExpansion,bool IsReinject)16790b57cec5SDimitry Andric void EnterTokenStream(ArrayRef<Token> Toks, bool DisableMacroExpansion, 16800b57cec5SDimitry Andric bool IsReinject) { 16810b57cec5SDimitry Andric EnterTokenStream(Toks.data(), Toks.size(), DisableMacroExpansion, false, 16820b57cec5SDimitry Andric IsReinject); 16830b57cec5SDimitry Andric } 16840b57cec5SDimitry Andric 16850b57cec5SDimitry Andric /// Pop the current lexer/macro exp off the top of the lexer stack. 16860b57cec5SDimitry Andric /// 16870b57cec5SDimitry Andric /// This should only be used in situations where the current state of the 16880b57cec5SDimitry Andric /// top-of-stack lexer is known. 16890b57cec5SDimitry Andric void RemoveTopOfLexerStack(); 16900b57cec5SDimitry Andric 16910b57cec5SDimitry Andric /// From the point that this method is called, and until 16920b57cec5SDimitry Andric /// CommitBacktrackedTokens() or Backtrack() is called, the Preprocessor 16930b57cec5SDimitry Andric /// keeps track of the lexed tokens so that a subsequent Backtrack() call will 16940b57cec5SDimitry Andric /// make the Preprocessor re-lex the same tokens. 16950b57cec5SDimitry Andric /// 16960b57cec5SDimitry Andric /// Nested backtracks are allowed, meaning that EnableBacktrackAtThisPos can 16970b57cec5SDimitry Andric /// be called multiple times and CommitBacktrackedTokens/Backtrack calls will 16980b57cec5SDimitry Andric /// be combined with the EnableBacktrackAtThisPos calls in reverse order. 16990b57cec5SDimitry Andric /// 17000b57cec5SDimitry Andric /// NOTE: *DO NOT* forget to call either CommitBacktrackedTokens or Backtrack 17010b57cec5SDimitry Andric /// at some point after EnableBacktrackAtThisPos. If you don't, caching of 17020b57cec5SDimitry Andric /// tokens will continue indefinitely. 17030b57cec5SDimitry Andric /// 17040b57cec5SDimitry Andric void EnableBacktrackAtThisPos(); 17050b57cec5SDimitry Andric 17060b57cec5SDimitry Andric /// Disable the last EnableBacktrackAtThisPos call. 17070b57cec5SDimitry Andric void CommitBacktrackedTokens(); 17080b57cec5SDimitry Andric 17090b57cec5SDimitry Andric /// Make Preprocessor re-lex the tokens that were lexed since 17100b57cec5SDimitry Andric /// EnableBacktrackAtThisPos() was previously called. 17110b57cec5SDimitry Andric void Backtrack(); 17120b57cec5SDimitry Andric 17130b57cec5SDimitry Andric /// True if EnableBacktrackAtThisPos() was called and 17140b57cec5SDimitry Andric /// caching of tokens is on. isBacktrackEnabled()17150b57cec5SDimitry Andric bool isBacktrackEnabled() const { return !BacktrackPositions.empty(); } 17160b57cec5SDimitry Andric 17170b57cec5SDimitry Andric /// Lex the next token for this preprocessor. 17180b57cec5SDimitry Andric void Lex(Token &Result); 17190b57cec5SDimitry Andric 17205f757f3fSDimitry Andric /// Lex all tokens for this preprocessor until (and excluding) end of file. 17215f757f3fSDimitry Andric void LexTokensUntilEOF(std::vector<Token> *Tokens = nullptr); 17225f757f3fSDimitry Andric 17230b57cec5SDimitry Andric /// Lex a token, forming a header-name token if possible. 17240b57cec5SDimitry Andric bool LexHeaderName(Token &Result, bool AllowMacroExpansion = true); 17250b57cec5SDimitry Andric 17260b57cec5SDimitry Andric bool LexAfterModuleImport(Token &Result); 17270b57cec5SDimitry Andric void CollectPpImportSuffix(SmallVectorImpl<Token> &Toks); 17280b57cec5SDimitry Andric 17290b57cec5SDimitry Andric void makeModuleVisible(Module *M, SourceLocation Loc); 17300b57cec5SDimitry Andric getModuleImportLoc(Module * M)17310b57cec5SDimitry Andric SourceLocation getModuleImportLoc(Module *M) const { 17320b57cec5SDimitry Andric return CurSubmoduleState->VisibleModules.getImportLoc(M); 17330b57cec5SDimitry Andric } 17340b57cec5SDimitry Andric 17350b57cec5SDimitry Andric /// Lex a string literal, which may be the concatenation of multiple 17360b57cec5SDimitry Andric /// string literals and may even come from macro expansion. 17370b57cec5SDimitry Andric /// \returns true on success, false if a error diagnostic has been generated. LexStringLiteral(Token & Result,std::string & String,const char * DiagnosticTag,bool AllowMacroExpansion)17380b57cec5SDimitry Andric bool LexStringLiteral(Token &Result, std::string &String, 17390b57cec5SDimitry Andric const char *DiagnosticTag, bool AllowMacroExpansion) { 17400b57cec5SDimitry Andric if (AllowMacroExpansion) 17410b57cec5SDimitry Andric Lex(Result); 17420b57cec5SDimitry Andric else 17430b57cec5SDimitry Andric LexUnexpandedToken(Result); 17440b57cec5SDimitry Andric return FinishLexStringLiteral(Result, String, DiagnosticTag, 17450b57cec5SDimitry Andric AllowMacroExpansion); 17460b57cec5SDimitry Andric } 17470b57cec5SDimitry Andric 17480b57cec5SDimitry Andric /// Complete the lexing of a string literal where the first token has 17490b57cec5SDimitry Andric /// already been lexed (see LexStringLiteral). 17500b57cec5SDimitry Andric bool FinishLexStringLiteral(Token &Result, std::string &String, 17510b57cec5SDimitry Andric const char *DiagnosticTag, 17520b57cec5SDimitry Andric bool AllowMacroExpansion); 17530b57cec5SDimitry Andric 17540b57cec5SDimitry Andric /// Lex a token. If it's a comment, keep lexing until we get 17550b57cec5SDimitry Andric /// something not a comment. 17560b57cec5SDimitry Andric /// 17570b57cec5SDimitry Andric /// This is useful in -E -C mode where comments would foul up preprocessor 17580b57cec5SDimitry Andric /// directive handling. LexNonComment(Token & Result)17590b57cec5SDimitry Andric void LexNonComment(Token &Result) { 17600b57cec5SDimitry Andric do 17610b57cec5SDimitry Andric Lex(Result); 17620b57cec5SDimitry Andric while (Result.getKind() == tok::comment); 17630b57cec5SDimitry Andric } 17640b57cec5SDimitry Andric 17650b57cec5SDimitry Andric /// Just like Lex, but disables macro expansion of identifier tokens. LexUnexpandedToken(Token & Result)17660b57cec5SDimitry Andric void LexUnexpandedToken(Token &Result) { 17670b57cec5SDimitry Andric // Disable macro expansion. 17680b57cec5SDimitry Andric bool OldVal = DisableMacroExpansion; 17690b57cec5SDimitry Andric DisableMacroExpansion = true; 17700b57cec5SDimitry Andric // Lex the token. 17710b57cec5SDimitry Andric Lex(Result); 17720b57cec5SDimitry Andric 17730b57cec5SDimitry Andric // Reenable it. 17740b57cec5SDimitry Andric DisableMacroExpansion = OldVal; 17750b57cec5SDimitry Andric } 17760b57cec5SDimitry Andric 17770b57cec5SDimitry Andric /// Like LexNonComment, but this disables macro expansion of 17780b57cec5SDimitry Andric /// identifier tokens. LexUnexpandedNonComment(Token & Result)17790b57cec5SDimitry Andric void LexUnexpandedNonComment(Token &Result) { 17800b57cec5SDimitry Andric do 17810b57cec5SDimitry Andric LexUnexpandedToken(Result); 17820b57cec5SDimitry Andric while (Result.getKind() == tok::comment); 17830b57cec5SDimitry Andric } 17840b57cec5SDimitry Andric 17850b57cec5SDimitry Andric /// Parses a simple integer literal to get its numeric value. Floating 17860b57cec5SDimitry Andric /// point literals and user defined literals are rejected. Used primarily to 17870b57cec5SDimitry Andric /// handle pragmas that accept integer arguments. 17880b57cec5SDimitry Andric bool parseSimpleIntegerLiteral(Token &Tok, uint64_t &Value); 17890b57cec5SDimitry Andric 17900b57cec5SDimitry Andric /// Disables macro expansion everywhere except for preprocessor directives. SetMacroExpansionOnlyInDirectives()17910b57cec5SDimitry Andric void SetMacroExpansionOnlyInDirectives() { 17920b57cec5SDimitry Andric DisableMacroExpansion = true; 17930b57cec5SDimitry Andric MacroExpansionInDirectivesOverride = true; 17940b57cec5SDimitry Andric } 17950b57cec5SDimitry Andric 17960b57cec5SDimitry Andric /// Peeks ahead N tokens and returns that token without consuming any 17970b57cec5SDimitry Andric /// tokens. 17980b57cec5SDimitry Andric /// 17990b57cec5SDimitry Andric /// LookAhead(0) returns the next token that would be returned by Lex(), 18000b57cec5SDimitry Andric /// LookAhead(1) returns the token after it, etc. This returns normal 18010b57cec5SDimitry Andric /// tokens after phase 5. As such, it is equivalent to using 18020b57cec5SDimitry Andric /// 'Lex', not 'LexUnexpandedToken'. LookAhead(unsigned N)18030b57cec5SDimitry Andric const Token &LookAhead(unsigned N) { 18040b57cec5SDimitry Andric assert(LexLevel == 0 && "cannot use lookahead while lexing"); 18050b57cec5SDimitry Andric if (CachedLexPos + N < CachedTokens.size()) 18060b57cec5SDimitry Andric return CachedTokens[CachedLexPos+N]; 18070b57cec5SDimitry Andric else 18080b57cec5SDimitry Andric return PeekAhead(N+1); 18090b57cec5SDimitry Andric } 18100b57cec5SDimitry Andric 18110b57cec5SDimitry Andric /// When backtracking is enabled and tokens are cached, 18120b57cec5SDimitry Andric /// this allows to revert a specific number of tokens. 18130b57cec5SDimitry Andric /// 18140b57cec5SDimitry Andric /// Note that the number of tokens being reverted should be up to the last 18150b57cec5SDimitry Andric /// backtrack position, not more. RevertCachedTokens(unsigned N)18160b57cec5SDimitry Andric void RevertCachedTokens(unsigned N) { 18170b57cec5SDimitry Andric assert(isBacktrackEnabled() && 18180b57cec5SDimitry Andric "Should only be called when tokens are cached for backtracking"); 18190b57cec5SDimitry Andric assert(signed(CachedLexPos) - signed(N) >= signed(BacktrackPositions.back()) 18200b57cec5SDimitry Andric && "Should revert tokens up to the last backtrack position, not more"); 18210b57cec5SDimitry Andric assert(signed(CachedLexPos) - signed(N) >= 0 && 18220b57cec5SDimitry Andric "Corrupted backtrack positions ?"); 18230b57cec5SDimitry Andric CachedLexPos -= N; 18240b57cec5SDimitry Andric } 18250b57cec5SDimitry Andric 18260b57cec5SDimitry Andric /// Enters a token in the token stream to be lexed next. 18270b57cec5SDimitry Andric /// 18280b57cec5SDimitry Andric /// If BackTrack() is called afterwards, the token will remain at the 18290b57cec5SDimitry Andric /// insertion point. 18300b57cec5SDimitry Andric /// If \p IsReinject is true, resulting token will have Token::IsReinjected 18310b57cec5SDimitry Andric /// flag set. See the flag documentation for details. EnterToken(const Token & Tok,bool IsReinject)18320b57cec5SDimitry Andric void EnterToken(const Token &Tok, bool IsReinject) { 18330b57cec5SDimitry Andric if (LexLevel) { 18340b57cec5SDimitry Andric // It's not correct in general to enter caching lex mode while in the 18350b57cec5SDimitry Andric // middle of a nested lexing action. 1836a7dea167SDimitry Andric auto TokCopy = std::make_unique<Token[]>(1); 18370b57cec5SDimitry Andric TokCopy[0] = Tok; 18380b57cec5SDimitry Andric EnterTokenStream(std::move(TokCopy), 1, true, IsReinject); 18390b57cec5SDimitry Andric } else { 18400b57cec5SDimitry Andric EnterCachingLexMode(); 18410b57cec5SDimitry Andric assert(IsReinject && "new tokens in the middle of cached stream"); 18420b57cec5SDimitry Andric CachedTokens.insert(CachedTokens.begin()+CachedLexPos, Tok); 18430b57cec5SDimitry Andric } 18440b57cec5SDimitry Andric } 18450b57cec5SDimitry Andric 18460b57cec5SDimitry Andric /// We notify the Preprocessor that if it is caching tokens (because 18470b57cec5SDimitry Andric /// backtrack is enabled) it should replace the most recent cached tokens 18480b57cec5SDimitry Andric /// with the given annotation token. This function has no effect if 18490b57cec5SDimitry Andric /// backtracking is not enabled. 18500b57cec5SDimitry Andric /// 18510b57cec5SDimitry Andric /// Note that the use of this function is just for optimization, so that the 18520b57cec5SDimitry Andric /// cached tokens doesn't get re-parsed and re-resolved after a backtrack is 18530b57cec5SDimitry Andric /// invoked. AnnotateCachedTokens(const Token & Tok)18540b57cec5SDimitry Andric void AnnotateCachedTokens(const Token &Tok) { 18550b57cec5SDimitry Andric assert(Tok.isAnnotation() && "Expected annotation token"); 18560b57cec5SDimitry Andric if (CachedLexPos != 0 && isBacktrackEnabled()) 18570b57cec5SDimitry Andric AnnotatePreviousCachedTokens(Tok); 18580b57cec5SDimitry Andric } 18590b57cec5SDimitry Andric 18600b57cec5SDimitry Andric /// Get the location of the last cached token, suitable for setting the end 18610b57cec5SDimitry Andric /// location of an annotation token. getLastCachedTokenLocation()18620b57cec5SDimitry Andric SourceLocation getLastCachedTokenLocation() const { 18630b57cec5SDimitry Andric assert(CachedLexPos != 0); 18640b57cec5SDimitry Andric return CachedTokens[CachedLexPos-1].getLastLoc(); 18650b57cec5SDimitry Andric } 18660b57cec5SDimitry Andric 18670b57cec5SDimitry Andric /// Whether \p Tok is the most recent token (`CachedLexPos - 1`) in 18680b57cec5SDimitry Andric /// CachedTokens. 18690b57cec5SDimitry Andric bool IsPreviousCachedToken(const Token &Tok) const; 18700b57cec5SDimitry Andric 18710b57cec5SDimitry Andric /// Replace token in `CachedLexPos - 1` in CachedTokens by the tokens 18720b57cec5SDimitry Andric /// in \p NewToks. 18730b57cec5SDimitry Andric /// 18740b57cec5SDimitry Andric /// Useful when a token needs to be split in smaller ones and CachedTokens 18750b57cec5SDimitry Andric /// most recent token must to be updated to reflect that. 18760b57cec5SDimitry Andric void ReplacePreviousCachedToken(ArrayRef<Token> NewToks); 18770b57cec5SDimitry Andric 18780b57cec5SDimitry Andric /// Replace the last token with an annotation token. 18790b57cec5SDimitry Andric /// 18800b57cec5SDimitry Andric /// Like AnnotateCachedTokens(), this routine replaces an 18810b57cec5SDimitry Andric /// already-parsed (and resolved) token with an annotation 18820b57cec5SDimitry Andric /// token. However, this routine only replaces the last token with 18830b57cec5SDimitry Andric /// the annotation token; it does not affect any other cached 18840b57cec5SDimitry Andric /// tokens. This function has no effect if backtracking is not 18850b57cec5SDimitry Andric /// enabled. ReplaceLastTokenWithAnnotation(const Token & Tok)18860b57cec5SDimitry Andric void ReplaceLastTokenWithAnnotation(const Token &Tok) { 18870b57cec5SDimitry Andric assert(Tok.isAnnotation() && "Expected annotation token"); 18880b57cec5SDimitry Andric if (CachedLexPos != 0 && isBacktrackEnabled()) 18890b57cec5SDimitry Andric CachedTokens[CachedLexPos-1] = Tok; 18900b57cec5SDimitry Andric } 18910b57cec5SDimitry Andric 18920b57cec5SDimitry Andric /// Enter an annotation token into the token stream. 18930b57cec5SDimitry Andric void EnterAnnotationToken(SourceRange Range, tok::TokenKind Kind, 18940b57cec5SDimitry Andric void *AnnotationVal); 18950b57cec5SDimitry Andric 18965ffd83dbSDimitry Andric /// Determine whether it's possible for a future call to Lex to produce an 18975ffd83dbSDimitry Andric /// annotation token created by a previous call to EnterAnnotationToken. mightHavePendingAnnotationTokens()18985ffd83dbSDimitry Andric bool mightHavePendingAnnotationTokens() { 18995f757f3fSDimitry Andric return CurLexerCallback != CLK_Lexer; 19005ffd83dbSDimitry Andric } 19015ffd83dbSDimitry Andric 19020b57cec5SDimitry Andric /// Update the current token to represent the provided 19030b57cec5SDimitry Andric /// identifier, in order to cache an action performed by typo correction. TypoCorrectToken(const Token & Tok)19040b57cec5SDimitry Andric void TypoCorrectToken(const Token &Tok) { 19050b57cec5SDimitry Andric assert(Tok.getIdentifierInfo() && "Expected identifier token"); 19060b57cec5SDimitry Andric if (CachedLexPos != 0 && isBacktrackEnabled()) 19070b57cec5SDimitry Andric CachedTokens[CachedLexPos-1] = Tok; 19080b57cec5SDimitry Andric } 19090b57cec5SDimitry Andric 19100b57cec5SDimitry Andric /// Recompute the current lexer kind based on the CurLexer/ 19110b57cec5SDimitry Andric /// CurTokenLexer pointers. 19120b57cec5SDimitry Andric void recomputeCurLexerKind(); 19130b57cec5SDimitry Andric 19140b57cec5SDimitry Andric /// Returns true if incremental processing is enabled isIncrementalProcessingEnabled()19155f757f3fSDimitry Andric bool isIncrementalProcessingEnabled() const { return IncrementalProcessing; } 19160b57cec5SDimitry Andric 19170b57cec5SDimitry Andric /// Enables the incremental processing 19180b57cec5SDimitry Andric void enableIncrementalProcessing(bool value = true) { 19195f757f3fSDimitry Andric IncrementalProcessing = value; 19200b57cec5SDimitry Andric } 19210b57cec5SDimitry Andric 19220b57cec5SDimitry Andric /// Specify the point at which code-completion will be performed. 19230b57cec5SDimitry Andric /// 19240b57cec5SDimitry Andric /// \param File the file in which code completion should occur. If 19250b57cec5SDimitry Andric /// this file is included multiple times, code-completion will 19260b57cec5SDimitry Andric /// perform completion the first time it is included. If NULL, this 19270b57cec5SDimitry Andric /// function clears out the code-completion point. 19280b57cec5SDimitry Andric /// 19290b57cec5SDimitry Andric /// \param Line the line at which code completion should occur 19300b57cec5SDimitry Andric /// (1-based). 19310b57cec5SDimitry Andric /// 19320b57cec5SDimitry Andric /// \param Column the column at which code completion should occur 19330b57cec5SDimitry Andric /// (1-based). 19340b57cec5SDimitry Andric /// 19350b57cec5SDimitry Andric /// \returns true if an error occurred, false otherwise. 19365f757f3fSDimitry Andric bool SetCodeCompletionPoint(FileEntryRef File, unsigned Line, 19375f757f3fSDimitry Andric unsigned Column); 19380b57cec5SDimitry Andric 19390b57cec5SDimitry Andric /// Determine if we are performing code completion. isCodeCompletionEnabled()19400b57cec5SDimitry Andric bool isCodeCompletionEnabled() const { return CodeCompletionFile != nullptr; } 19410b57cec5SDimitry Andric 19420b57cec5SDimitry Andric /// Returns the location of the code-completion point. 19430b57cec5SDimitry Andric /// 19440b57cec5SDimitry Andric /// Returns an invalid location if code-completion is not enabled or the file 19450b57cec5SDimitry Andric /// containing the code-completion point has not been lexed yet. getCodeCompletionLoc()19460b57cec5SDimitry Andric SourceLocation getCodeCompletionLoc() const { return CodeCompletionLoc; } 19470b57cec5SDimitry Andric 19480b57cec5SDimitry Andric /// Returns the start location of the file of code-completion point. 19490b57cec5SDimitry Andric /// 19500b57cec5SDimitry Andric /// Returns an invalid location if code-completion is not enabled or the file 19510b57cec5SDimitry Andric /// containing the code-completion point has not been lexed yet. getCodeCompletionFileLoc()19520b57cec5SDimitry Andric SourceLocation getCodeCompletionFileLoc() const { 19530b57cec5SDimitry Andric return CodeCompletionFileLoc; 19540b57cec5SDimitry Andric } 19550b57cec5SDimitry Andric 19560b57cec5SDimitry Andric /// Returns true if code-completion is enabled and we have hit the 19570b57cec5SDimitry Andric /// code-completion point. isCodeCompletionReached()19580b57cec5SDimitry Andric bool isCodeCompletionReached() const { return CodeCompletionReached; } 19590b57cec5SDimitry Andric 19600b57cec5SDimitry Andric /// Note that we hit the code-completion point. setCodeCompletionReached()19610b57cec5SDimitry Andric void setCodeCompletionReached() { 19620b57cec5SDimitry Andric assert(isCodeCompletionEnabled() && "Code-completion not enabled!"); 19630b57cec5SDimitry Andric CodeCompletionReached = true; 19640b57cec5SDimitry Andric // Silence any diagnostics that occur after we hit the code-completion. 19650b57cec5SDimitry Andric getDiagnostics().setSuppressAllDiagnostics(true); 19660b57cec5SDimitry Andric } 19670b57cec5SDimitry Andric 19680b57cec5SDimitry Andric /// The location of the currently-active \#pragma clang 19690b57cec5SDimitry Andric /// arc_cf_code_audited begin. 19700b57cec5SDimitry Andric /// 19710b57cec5SDimitry Andric /// Returns an invalid location if there is no such pragma active. 1972a7dea167SDimitry Andric std::pair<IdentifierInfo *, SourceLocation> getPragmaARCCFCodeAuditedInfo()1973a7dea167SDimitry Andric getPragmaARCCFCodeAuditedInfo() const { 1974a7dea167SDimitry Andric return PragmaARCCFCodeAuditedInfo; 19750b57cec5SDimitry Andric } 19760b57cec5SDimitry Andric 19770b57cec5SDimitry Andric /// Set the location of the currently-active \#pragma clang 19780b57cec5SDimitry Andric /// arc_cf_code_audited begin. An invalid location ends the pragma. setPragmaARCCFCodeAuditedInfo(IdentifierInfo * Ident,SourceLocation Loc)1979a7dea167SDimitry Andric void setPragmaARCCFCodeAuditedInfo(IdentifierInfo *Ident, 1980a7dea167SDimitry Andric SourceLocation Loc) { 1981a7dea167SDimitry Andric PragmaARCCFCodeAuditedInfo = {Ident, Loc}; 19820b57cec5SDimitry Andric } 19830b57cec5SDimitry Andric 19840b57cec5SDimitry Andric /// The location of the currently-active \#pragma clang 19850b57cec5SDimitry Andric /// assume_nonnull begin. 19860b57cec5SDimitry Andric /// 19870b57cec5SDimitry Andric /// Returns an invalid location if there is no such pragma active. getPragmaAssumeNonNullLoc()19880b57cec5SDimitry Andric SourceLocation getPragmaAssumeNonNullLoc() const { 19890b57cec5SDimitry Andric return PragmaAssumeNonNullLoc; 19900b57cec5SDimitry Andric } 19910b57cec5SDimitry Andric 19920b57cec5SDimitry Andric /// Set the location of the currently-active \#pragma clang 19930b57cec5SDimitry Andric /// assume_nonnull begin. An invalid location ends the pragma. setPragmaAssumeNonNullLoc(SourceLocation Loc)19940b57cec5SDimitry Andric void setPragmaAssumeNonNullLoc(SourceLocation Loc) { 19950b57cec5SDimitry Andric PragmaAssumeNonNullLoc = Loc; 19960b57cec5SDimitry Andric } 19970b57cec5SDimitry Andric 199881ad6265SDimitry Andric /// Get the location of the recorded unterminated \#pragma clang 199981ad6265SDimitry Andric /// assume_nonnull begin in the preamble, if one exists. 200081ad6265SDimitry Andric /// 200181ad6265SDimitry Andric /// Returns an invalid location if the premable did not end with 200281ad6265SDimitry Andric /// such a pragma active or if there is no recorded preamble. getPreambleRecordedPragmaAssumeNonNullLoc()200381ad6265SDimitry Andric SourceLocation getPreambleRecordedPragmaAssumeNonNullLoc() const { 200481ad6265SDimitry Andric return PreambleRecordedPragmaAssumeNonNullLoc; 200581ad6265SDimitry Andric } 200681ad6265SDimitry Andric 200781ad6265SDimitry Andric /// Record the location of the unterminated \#pragma clang 200881ad6265SDimitry Andric /// assume_nonnull begin in the preamble. setPreambleRecordedPragmaAssumeNonNullLoc(SourceLocation Loc)200981ad6265SDimitry Andric void setPreambleRecordedPragmaAssumeNonNullLoc(SourceLocation Loc) { 201081ad6265SDimitry Andric PreambleRecordedPragmaAssumeNonNullLoc = Loc; 201181ad6265SDimitry Andric } 201281ad6265SDimitry Andric 20130b57cec5SDimitry Andric /// Set the directory in which the main file should be considered 20140b57cec5SDimitry Andric /// to have been found, if it is not a real file. setMainFileDir(DirectoryEntryRef Dir)201506c3fb27SDimitry Andric void setMainFileDir(DirectoryEntryRef Dir) { MainFileDir = Dir; } 20160b57cec5SDimitry Andric 20170b57cec5SDimitry Andric /// Instruct the preprocessor to skip part of the main source file. 20180b57cec5SDimitry Andric /// 20190b57cec5SDimitry Andric /// \param Bytes The number of bytes in the preamble to skip. 20200b57cec5SDimitry Andric /// 20210b57cec5SDimitry Andric /// \param StartOfLine Whether skipping these bytes puts the lexer at the 20220b57cec5SDimitry Andric /// start of a line. setSkipMainFilePreamble(unsigned Bytes,bool StartOfLine)20230b57cec5SDimitry Andric void setSkipMainFilePreamble(unsigned Bytes, bool StartOfLine) { 20240b57cec5SDimitry Andric SkipMainFilePreamble.first = Bytes; 20250b57cec5SDimitry Andric SkipMainFilePreamble.second = StartOfLine; 20260b57cec5SDimitry Andric } 20270b57cec5SDimitry Andric 20280b57cec5SDimitry Andric /// Forwarding function for diagnostics. This emits a diagnostic at 20290b57cec5SDimitry Andric /// the specified Token's location, translating the token's start 20300b57cec5SDimitry Andric /// position in the current buffer into a SourcePosition object for rendering. Diag(SourceLocation Loc,unsigned DiagID)20310b57cec5SDimitry Andric DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const { 20320b57cec5SDimitry Andric return Diags->Report(Loc, DiagID); 20330b57cec5SDimitry Andric } 20340b57cec5SDimitry Andric Diag(const Token & Tok,unsigned DiagID)20350b57cec5SDimitry Andric DiagnosticBuilder Diag(const Token &Tok, unsigned DiagID) const { 20360b57cec5SDimitry Andric return Diags->Report(Tok.getLocation(), DiagID); 20370b57cec5SDimitry Andric } 20380b57cec5SDimitry Andric 20390b57cec5SDimitry Andric /// Return the 'spelling' of the token at the given 20400b57cec5SDimitry Andric /// location; does not go up to the spelling location or down to the 20410b57cec5SDimitry Andric /// expansion location. 20420b57cec5SDimitry Andric /// 20430b57cec5SDimitry Andric /// \param buffer A buffer which will be used only if the token requires 20440b57cec5SDimitry Andric /// "cleaning", e.g. if it contains trigraphs or escaped newlines 20450b57cec5SDimitry Andric /// \param invalid If non-null, will be set \c true if an error occurs. 20460b57cec5SDimitry Andric StringRef getSpelling(SourceLocation loc, 20470b57cec5SDimitry Andric SmallVectorImpl<char> &buffer, 20480b57cec5SDimitry Andric bool *invalid = nullptr) const { 20490b57cec5SDimitry Andric return Lexer::getSpelling(loc, buffer, SourceMgr, LangOpts, invalid); 20500b57cec5SDimitry Andric } 20510b57cec5SDimitry Andric 20520b57cec5SDimitry Andric /// Return the 'spelling' of the Tok token. 20530b57cec5SDimitry Andric /// 20540b57cec5SDimitry Andric /// The spelling of a token is the characters used to represent the token in 20550b57cec5SDimitry Andric /// the source file after trigraph expansion and escaped-newline folding. In 20560b57cec5SDimitry Andric /// particular, this wants to get the true, uncanonicalized, spelling of 20570b57cec5SDimitry Andric /// things like digraphs, UCNs, etc. 20580b57cec5SDimitry Andric /// 20590b57cec5SDimitry Andric /// \param Invalid If non-null, will be set \c true if an error occurs. 20600b57cec5SDimitry Andric std::string getSpelling(const Token &Tok, bool *Invalid = nullptr) const { 20610b57cec5SDimitry Andric return Lexer::getSpelling(Tok, SourceMgr, LangOpts, Invalid); 20620b57cec5SDimitry Andric } 20630b57cec5SDimitry Andric 20640b57cec5SDimitry Andric /// Get the spelling of a token into a preallocated buffer, instead 20650b57cec5SDimitry Andric /// of as an std::string. 20660b57cec5SDimitry Andric /// 20670b57cec5SDimitry Andric /// The caller is required to allocate enough space for the token, which is 20680b57cec5SDimitry Andric /// guaranteed to be at least Tok.getLength() bytes long. The length of the 20690b57cec5SDimitry Andric /// actual result is returned. 20700b57cec5SDimitry Andric /// 20710b57cec5SDimitry Andric /// Note that this method may do two possible things: it may either fill in 20720b57cec5SDimitry Andric /// the buffer specified with characters, or it may *change the input pointer* 20730b57cec5SDimitry Andric /// to point to a constant buffer with the data already in it (avoiding a 20740b57cec5SDimitry Andric /// copy). The caller is not allowed to modify the returned buffer pointer 20750b57cec5SDimitry Andric /// if an internal buffer is returned. 20760b57cec5SDimitry Andric unsigned getSpelling(const Token &Tok, const char *&Buffer, 20770b57cec5SDimitry Andric bool *Invalid = nullptr) const { 20780b57cec5SDimitry Andric return Lexer::getSpelling(Tok, Buffer, SourceMgr, LangOpts, Invalid); 20790b57cec5SDimitry Andric } 20800b57cec5SDimitry Andric 20810b57cec5SDimitry Andric /// Get the spelling of a token into a SmallVector. 20820b57cec5SDimitry Andric /// 20830b57cec5SDimitry Andric /// Note that the returned StringRef may not point to the 20840b57cec5SDimitry Andric /// supplied buffer if a copy can be avoided. 20850b57cec5SDimitry Andric StringRef getSpelling(const Token &Tok, 20860b57cec5SDimitry Andric SmallVectorImpl<char> &Buffer, 20870b57cec5SDimitry Andric bool *Invalid = nullptr) const; 20880b57cec5SDimitry Andric 20890b57cec5SDimitry Andric /// Relex the token at the specified location. 20900b57cec5SDimitry Andric /// \returns true if there was a failure, false on success. 20910b57cec5SDimitry Andric bool getRawToken(SourceLocation Loc, Token &Result, 20920b57cec5SDimitry Andric bool IgnoreWhiteSpace = false) { 20930b57cec5SDimitry Andric return Lexer::getRawToken(Loc, Result, SourceMgr, LangOpts, IgnoreWhiteSpace); 20940b57cec5SDimitry Andric } 20950b57cec5SDimitry Andric 20960b57cec5SDimitry Andric /// Given a Token \p Tok that is a numeric constant with length 1, 20970b57cec5SDimitry Andric /// return the character. 20980b57cec5SDimitry Andric char 20990b57cec5SDimitry Andric getSpellingOfSingleCharacterNumericConstant(const Token &Tok, 21000b57cec5SDimitry Andric bool *Invalid = nullptr) const { 21010b57cec5SDimitry Andric assert(Tok.is(tok::numeric_constant) && 21020b57cec5SDimitry Andric Tok.getLength() == 1 && "Called on unsupported token"); 21030b57cec5SDimitry Andric assert(!Tok.needsCleaning() && "Token can't need cleaning with length 1"); 21040b57cec5SDimitry Andric 21050b57cec5SDimitry Andric // If the token is carrying a literal data pointer, just use it. 21060b57cec5SDimitry Andric if (const char *D = Tok.getLiteralData()) 21070b57cec5SDimitry Andric return *D; 21080b57cec5SDimitry Andric 21090b57cec5SDimitry Andric // Otherwise, fall back on getCharacterData, which is slower, but always 21100b57cec5SDimitry Andric // works. 21110b57cec5SDimitry Andric return *SourceMgr.getCharacterData(Tok.getLocation(), Invalid); 21120b57cec5SDimitry Andric } 21130b57cec5SDimitry Andric 21140b57cec5SDimitry Andric /// Retrieve the name of the immediate macro expansion. 21150b57cec5SDimitry Andric /// 21160b57cec5SDimitry Andric /// This routine starts from a source location, and finds the name of the 21170b57cec5SDimitry Andric /// macro responsible for its immediate expansion. It looks through any 21180b57cec5SDimitry Andric /// intervening macro argument expansions to compute this. It returns a 21190b57cec5SDimitry Andric /// StringRef that refers to the SourceManager-owned buffer of the source 21200b57cec5SDimitry Andric /// where that macro name is spelled. Thus, the result shouldn't out-live 21210b57cec5SDimitry Andric /// the SourceManager. getImmediateMacroName(SourceLocation Loc)21220b57cec5SDimitry Andric StringRef getImmediateMacroName(SourceLocation Loc) { 21230b57cec5SDimitry Andric return Lexer::getImmediateMacroName(Loc, SourceMgr, getLangOpts()); 21240b57cec5SDimitry Andric } 21250b57cec5SDimitry Andric 21260b57cec5SDimitry Andric /// Plop the specified string into a scratch buffer and set the 21270b57cec5SDimitry Andric /// specified token's location and length to it. 21280b57cec5SDimitry Andric /// 21290b57cec5SDimitry Andric /// If specified, the source location provides a location of the expansion 21300b57cec5SDimitry Andric /// point of the token. 21310b57cec5SDimitry Andric void CreateString(StringRef Str, Token &Tok, 21320b57cec5SDimitry Andric SourceLocation ExpansionLocStart = SourceLocation(), 21330b57cec5SDimitry Andric SourceLocation ExpansionLocEnd = SourceLocation()); 21340b57cec5SDimitry Andric 21350b57cec5SDimitry Andric /// Split the first Length characters out of the token starting at TokLoc 21360b57cec5SDimitry Andric /// and return a location pointing to the split token. Re-lexing from the 21370b57cec5SDimitry Andric /// split token will return the split token rather than the original. 21380b57cec5SDimitry Andric SourceLocation SplitToken(SourceLocation TokLoc, unsigned Length); 21390b57cec5SDimitry Andric 21400b57cec5SDimitry Andric /// Computes the source location just past the end of the 21410b57cec5SDimitry Andric /// token at this source location. 21420b57cec5SDimitry Andric /// 21430b57cec5SDimitry Andric /// This routine can be used to produce a source location that 21440b57cec5SDimitry Andric /// points just past the end of the token referenced by \p Loc, and 21450b57cec5SDimitry Andric /// is generally used when a diagnostic needs to point just after a 21460b57cec5SDimitry Andric /// token where it expected something different that it received. If 21470b57cec5SDimitry Andric /// the returned source location would not be meaningful (e.g., if 21480b57cec5SDimitry Andric /// it points into a macro), this routine returns an invalid 21490b57cec5SDimitry Andric /// source location. 21500b57cec5SDimitry Andric /// 21510b57cec5SDimitry Andric /// \param Offset an offset from the end of the token, where the source 21520b57cec5SDimitry Andric /// location should refer to. The default offset (0) produces a source 21530b57cec5SDimitry Andric /// location pointing just past the end of the token; an offset of 1 produces 21540b57cec5SDimitry Andric /// a source location pointing to the last character in the token, etc. 21550b57cec5SDimitry Andric SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset = 0) { 21560b57cec5SDimitry Andric return Lexer::getLocForEndOfToken(Loc, Offset, SourceMgr, LangOpts); 21570b57cec5SDimitry Andric } 21580b57cec5SDimitry Andric 21590b57cec5SDimitry Andric /// Returns true if the given MacroID location points at the first 21600b57cec5SDimitry Andric /// token of the macro expansion. 21610b57cec5SDimitry Andric /// 21620b57cec5SDimitry Andric /// \param MacroBegin If non-null and function returns true, it is set to 21630b57cec5SDimitry Andric /// begin location of the macro. 21640b57cec5SDimitry Andric bool isAtStartOfMacroExpansion(SourceLocation loc, 21650b57cec5SDimitry Andric SourceLocation *MacroBegin = nullptr) const { 21660b57cec5SDimitry Andric return Lexer::isAtStartOfMacroExpansion(loc, SourceMgr, LangOpts, 21670b57cec5SDimitry Andric MacroBegin); 21680b57cec5SDimitry Andric } 21690b57cec5SDimitry Andric 21700b57cec5SDimitry Andric /// Returns true if the given MacroID location points at the last 21710b57cec5SDimitry Andric /// token of the macro expansion. 21720b57cec5SDimitry Andric /// 21730b57cec5SDimitry Andric /// \param MacroEnd If non-null and function returns true, it is set to 21740b57cec5SDimitry Andric /// end location of the macro. 21750b57cec5SDimitry Andric bool isAtEndOfMacroExpansion(SourceLocation loc, 21760b57cec5SDimitry Andric SourceLocation *MacroEnd = nullptr) const { 21770b57cec5SDimitry Andric return Lexer::isAtEndOfMacroExpansion(loc, SourceMgr, LangOpts, MacroEnd); 21780b57cec5SDimitry Andric } 21790b57cec5SDimitry Andric 21800b57cec5SDimitry Andric /// Print the token to stderr, used for debugging. 21810b57cec5SDimitry Andric void DumpToken(const Token &Tok, bool DumpFlags = false) const; 21820b57cec5SDimitry Andric void DumpLocation(SourceLocation Loc) const; 21830b57cec5SDimitry Andric void DumpMacro(const MacroInfo &MI) const; 21840b57cec5SDimitry Andric void dumpMacroInfo(const IdentifierInfo *II); 21850b57cec5SDimitry Andric 21860b57cec5SDimitry Andric /// Given a location that specifies the start of a 21870b57cec5SDimitry Andric /// token, return a new location that specifies a character within the token. AdvanceToTokenCharacter(SourceLocation TokStart,unsigned Char)21880b57cec5SDimitry Andric SourceLocation AdvanceToTokenCharacter(SourceLocation TokStart, 21890b57cec5SDimitry Andric unsigned Char) const { 21900b57cec5SDimitry Andric return Lexer::AdvanceToTokenCharacter(TokStart, Char, SourceMgr, LangOpts); 21910b57cec5SDimitry Andric } 21920b57cec5SDimitry Andric 21930b57cec5SDimitry Andric /// Increment the counters for the number of token paste operations 21940b57cec5SDimitry Andric /// performed. 21950b57cec5SDimitry Andric /// 21960b57cec5SDimitry Andric /// If fast was specified, this is a 'fast paste' case we handled. IncrementPasteCounter(bool isFast)21970b57cec5SDimitry Andric void IncrementPasteCounter(bool isFast) { 21980b57cec5SDimitry Andric if (isFast) 21990b57cec5SDimitry Andric ++NumFastTokenPaste; 22000b57cec5SDimitry Andric else 22010b57cec5SDimitry Andric ++NumTokenPaste; 22020b57cec5SDimitry Andric } 22030b57cec5SDimitry Andric 22040b57cec5SDimitry Andric void PrintStats(); 22050b57cec5SDimitry Andric 22060b57cec5SDimitry Andric size_t getTotalMemory() const; 22070b57cec5SDimitry Andric 22080b57cec5SDimitry Andric /// When the macro expander pastes together a comment (/##/) in Microsoft 22090b57cec5SDimitry Andric /// mode, this method handles updating the current state, returning the 22100b57cec5SDimitry Andric /// token on the next source line. 22110b57cec5SDimitry Andric void HandleMicrosoftCommentPaste(Token &Tok); 22120b57cec5SDimitry Andric 22130b57cec5SDimitry Andric //===--------------------------------------------------------------------===// 22140b57cec5SDimitry Andric // Preprocessor callback methods. These are invoked by a lexer as various 22150b57cec5SDimitry Andric // directives and events are found. 22160b57cec5SDimitry Andric 22170b57cec5SDimitry Andric /// Given a tok::raw_identifier token, look up the 22180b57cec5SDimitry Andric /// identifier information for the token and install it into the token, 22190b57cec5SDimitry Andric /// updating the token kind accordingly. 22200b57cec5SDimitry Andric IdentifierInfo *LookUpIdentifierInfo(Token &Identifier) const; 22210b57cec5SDimitry Andric 22220b57cec5SDimitry Andric private: 22230b57cec5SDimitry Andric llvm::DenseMap<IdentifierInfo*,unsigned> PoisonReasons; 22240b57cec5SDimitry Andric 22250b57cec5SDimitry Andric public: 22260b57cec5SDimitry Andric /// Specifies the reason for poisoning an identifier. 22270b57cec5SDimitry Andric /// 22280b57cec5SDimitry Andric /// If that identifier is accessed while poisoned, then this reason will be 22290b57cec5SDimitry Andric /// used instead of the default "poisoned" diagnostic. 22300b57cec5SDimitry Andric void SetPoisonReason(IdentifierInfo *II, unsigned DiagID); 22310b57cec5SDimitry Andric 22320b57cec5SDimitry Andric /// Display reason for poisoned identifier. 22330b57cec5SDimitry Andric void HandlePoisonedIdentifier(Token & Identifier); 22340b57cec5SDimitry Andric MaybeHandlePoisonedIdentifier(Token & Identifier)22350b57cec5SDimitry Andric void MaybeHandlePoisonedIdentifier(Token & Identifier) { 22360b57cec5SDimitry Andric if(IdentifierInfo * II = Identifier.getIdentifierInfo()) { 22370b57cec5SDimitry Andric if(II->isPoisoned()) { 22380b57cec5SDimitry Andric HandlePoisonedIdentifier(Identifier); 22390b57cec5SDimitry Andric } 22400b57cec5SDimitry Andric } 22410b57cec5SDimitry Andric } 22420b57cec5SDimitry Andric 22430b57cec5SDimitry Andric private: 22440b57cec5SDimitry Andric /// Identifiers used for SEH handling in Borland. These are only 22450b57cec5SDimitry Andric /// allowed in particular circumstances 22460b57cec5SDimitry Andric // __except block 22470b57cec5SDimitry Andric IdentifierInfo *Ident__exception_code, 22480b57cec5SDimitry Andric *Ident___exception_code, 22490b57cec5SDimitry Andric *Ident_GetExceptionCode; 22500b57cec5SDimitry Andric // __except filter expression 22510b57cec5SDimitry Andric IdentifierInfo *Ident__exception_info, 22520b57cec5SDimitry Andric *Ident___exception_info, 22530b57cec5SDimitry Andric *Ident_GetExceptionInfo; 22540b57cec5SDimitry Andric // __finally 22550b57cec5SDimitry Andric IdentifierInfo *Ident__abnormal_termination, 22560b57cec5SDimitry Andric *Ident___abnormal_termination, 22570b57cec5SDimitry Andric *Ident_AbnormalTermination; 22580b57cec5SDimitry Andric 22590b57cec5SDimitry Andric const char *getCurLexerEndPos(); 22600b57cec5SDimitry Andric void diagnoseMissingHeaderInUmbrellaDir(const Module &Mod); 22610b57cec5SDimitry Andric 22620b57cec5SDimitry Andric public: 22630b57cec5SDimitry Andric void PoisonSEHIdentifiers(bool Poison = true); // Borland 22640b57cec5SDimitry Andric 22650b57cec5SDimitry Andric /// Callback invoked when the lexer reads an identifier and has 22660b57cec5SDimitry Andric /// filled in the tokens IdentifierInfo member. 22670b57cec5SDimitry Andric /// 22680b57cec5SDimitry Andric /// This callback potentially macro expands it or turns it into a named 22690b57cec5SDimitry Andric /// token (like 'for'). 22700b57cec5SDimitry Andric /// 22710b57cec5SDimitry Andric /// \returns true if we actually computed a token, false if we need to 22720b57cec5SDimitry Andric /// lex again. 22730b57cec5SDimitry Andric bool HandleIdentifier(Token &Identifier); 22740b57cec5SDimitry Andric 22750b57cec5SDimitry Andric /// Callback invoked when the lexer hits the end of the current file. 22760b57cec5SDimitry Andric /// 22770b57cec5SDimitry Andric /// This either returns the EOF token and returns true, or 22780b57cec5SDimitry Andric /// pops a level off the include stack and returns false, at which point the 22790b57cec5SDimitry Andric /// client should call lex again. 228081ad6265SDimitry Andric bool HandleEndOfFile(Token &Result, bool isEndOfMacro = false); 22810b57cec5SDimitry Andric 22820b57cec5SDimitry Andric /// Callback invoked when the current TokenLexer hits the end of its 22830b57cec5SDimitry Andric /// token stream. 22840b57cec5SDimitry Andric bool HandleEndOfTokenLexer(Token &Result); 22850b57cec5SDimitry Andric 22860b57cec5SDimitry Andric /// Callback invoked when the lexer sees a # token at the start of a 22870b57cec5SDimitry Andric /// line. 22880b57cec5SDimitry Andric /// 22890b57cec5SDimitry Andric /// This consumes the directive, modifies the lexer/preprocessor state, and 22900b57cec5SDimitry Andric /// advances the lexer(s) so that the next token read is the correct one. 22910b57cec5SDimitry Andric void HandleDirective(Token &Result); 22920b57cec5SDimitry Andric 22930b57cec5SDimitry Andric /// Ensure that the next token is a tok::eod token. 22940b57cec5SDimitry Andric /// 22950b57cec5SDimitry Andric /// If not, emit a diagnostic and consume up until the eod. 22960b57cec5SDimitry Andric /// If \p EnableMacros is true, then we consider macros that expand to zero 22970b57cec5SDimitry Andric /// tokens as being ok. 22980b57cec5SDimitry Andric /// 22990b57cec5SDimitry Andric /// \return The location of the end of the directive (the terminating 23000b57cec5SDimitry Andric /// newline). 23010b57cec5SDimitry Andric SourceLocation CheckEndOfDirective(const char *DirType, 23020b57cec5SDimitry Andric bool EnableMacros = false); 23030b57cec5SDimitry Andric 23040b57cec5SDimitry Andric /// Read and discard all tokens remaining on the current line until 23050b57cec5SDimitry Andric /// the tok::eod token is found. Returns the range of the skipped tokens. 23060b57cec5SDimitry Andric SourceRange DiscardUntilEndOfDirective(); 23070b57cec5SDimitry Andric 23080b57cec5SDimitry Andric /// Returns true if the preprocessor has seen a use of 23090b57cec5SDimitry Andric /// __DATE__ or __TIME__ in the file so far. SawDateOrTime()23100b57cec5SDimitry Andric bool SawDateOrTime() const { 23110b57cec5SDimitry Andric return DATELoc != SourceLocation() || TIMELoc != SourceLocation(); 23120b57cec5SDimitry Andric } getCounterValue()23130b57cec5SDimitry Andric unsigned getCounterValue() const { return CounterValue; } setCounterValue(unsigned V)23140b57cec5SDimitry Andric void setCounterValue(unsigned V) { CounterValue = V; } 23150b57cec5SDimitry Andric getCurrentFPEvalMethod()231681ad6265SDimitry Andric LangOptions::FPEvalMethodKind getCurrentFPEvalMethod() const { 231781ad6265SDimitry Andric assert(CurrentFPEvalMethod != LangOptions::FEM_UnsetOnCommandLine && 231881ad6265SDimitry Andric "FPEvalMethod should be set either from command line or from the " 231981ad6265SDimitry Andric "target info"); 232081ad6265SDimitry Andric return CurrentFPEvalMethod; 232181ad6265SDimitry Andric } 232281ad6265SDimitry Andric getTUFPEvalMethod()232381ad6265SDimitry Andric LangOptions::FPEvalMethodKind getTUFPEvalMethod() const { 232481ad6265SDimitry Andric return TUFPEvalMethod; 232581ad6265SDimitry Andric } 232681ad6265SDimitry Andric getLastFPEvalPragmaLocation()232781ad6265SDimitry Andric SourceLocation getLastFPEvalPragmaLocation() const { 232881ad6265SDimitry Andric return LastFPEvalPragmaLocation; 232981ad6265SDimitry Andric } 233081ad6265SDimitry Andric setCurrentFPEvalMethod(SourceLocation PragmaLoc,LangOptions::FPEvalMethodKind Val)233181ad6265SDimitry Andric void setCurrentFPEvalMethod(SourceLocation PragmaLoc, 233281ad6265SDimitry Andric LangOptions::FPEvalMethodKind Val) { 233381ad6265SDimitry Andric assert(Val != LangOptions::FEM_UnsetOnCommandLine && 233481ad6265SDimitry Andric "FPEvalMethod should never be set to FEM_UnsetOnCommandLine"); 233581ad6265SDimitry Andric // This is the location of the '#pragma float_control" where the 233681ad6265SDimitry Andric // execution state is modifed. 233781ad6265SDimitry Andric LastFPEvalPragmaLocation = PragmaLoc; 233881ad6265SDimitry Andric CurrentFPEvalMethod = Val; 233981ad6265SDimitry Andric TUFPEvalMethod = Val; 234081ad6265SDimitry Andric } 234181ad6265SDimitry Andric setTUFPEvalMethod(LangOptions::FPEvalMethodKind Val)234281ad6265SDimitry Andric void setTUFPEvalMethod(LangOptions::FPEvalMethodKind Val) { 234381ad6265SDimitry Andric assert(Val != LangOptions::FEM_UnsetOnCommandLine && 234481ad6265SDimitry Andric "TUPEvalMethod should never be set to FEM_UnsetOnCommandLine"); 234581ad6265SDimitry Andric TUFPEvalMethod = Val; 234681ad6265SDimitry Andric } 234781ad6265SDimitry Andric 23480b57cec5SDimitry Andric /// Retrieves the module that we're currently building, if any. 23490b57cec5SDimitry Andric Module *getCurrentModule(); 23500b57cec5SDimitry Andric 2351bdd1243dSDimitry Andric /// Retrieves the module whose implementation we're current compiling, if any. 2352bdd1243dSDimitry Andric Module *getCurrentModuleImplementation(); 2353bdd1243dSDimitry Andric 23541ac55f4cSDimitry Andric /// If we are preprocessing a named module. isInNamedModule()23551ac55f4cSDimitry Andric bool isInNamedModule() const { return ModuleDeclState.isNamedModule(); } 23561ac55f4cSDimitry Andric 23571ac55f4cSDimitry Andric /// If we are proprocessing a named interface unit. 23581ac55f4cSDimitry Andric /// Note that a module implementation partition is not considered as an 23591ac55f4cSDimitry Andric /// named interface unit here although it is importable 23601ac55f4cSDimitry Andric /// to ease the parsing. isInNamedInterfaceUnit()23611ac55f4cSDimitry Andric bool isInNamedInterfaceUnit() const { 23621ac55f4cSDimitry Andric return ModuleDeclState.isNamedInterface(); 23631ac55f4cSDimitry Andric } 23641ac55f4cSDimitry Andric 23651ac55f4cSDimitry Andric /// Get the named module name we're preprocessing. 23661ac55f4cSDimitry Andric /// Requires we're preprocessing a named module. getNamedModuleName()23671ac55f4cSDimitry Andric StringRef getNamedModuleName() const { return ModuleDeclState.getName(); } 23681ac55f4cSDimitry Andric 23691ac55f4cSDimitry Andric /// If we are implementing an implementation module unit. 23701ac55f4cSDimitry Andric /// Note that the module implementation partition is not considered as an 23711ac55f4cSDimitry Andric /// implementation unit. isInImplementationUnit()23721ac55f4cSDimitry Andric bool isInImplementationUnit() const { 23731ac55f4cSDimitry Andric return ModuleDeclState.isImplementationUnit(); 23741ac55f4cSDimitry Andric } 23751ac55f4cSDimitry Andric 23761ac55f4cSDimitry Andric /// If we're importing a standard C++20 Named Modules. isInImportingCXXNamedModules()23771ac55f4cSDimitry Andric bool isInImportingCXXNamedModules() const { 23781ac55f4cSDimitry Andric // NamedModuleImportPath will be non-empty only if we're importing 23791ac55f4cSDimitry Andric // Standard C++ named modules. 23801ac55f4cSDimitry Andric return !NamedModuleImportPath.empty() && getLangOpts().CPlusPlusModules && 23811ac55f4cSDimitry Andric !IsAtImport; 23821ac55f4cSDimitry Andric } 23831ac55f4cSDimitry Andric 23840b57cec5SDimitry Andric /// Allocate a new MacroInfo object with the provided SourceLocation. 23850b57cec5SDimitry Andric MacroInfo *AllocateMacroInfo(SourceLocation L); 23860b57cec5SDimitry Andric 23870b57cec5SDimitry Andric /// Turn the specified lexer token into a fully checked and spelled 23880b57cec5SDimitry Andric /// filename, e.g. as an operand of \#include. 23890b57cec5SDimitry Andric /// 23900b57cec5SDimitry Andric /// The caller is expected to provide a buffer that is large enough to hold 23910b57cec5SDimitry Andric /// the spelling of the filename, but is also expected to handle the case 23920b57cec5SDimitry Andric /// when this method decides to use a different buffer. 23930b57cec5SDimitry Andric /// 23940b57cec5SDimitry Andric /// \returns true if the input filename was in <>'s or false if it was 23950b57cec5SDimitry Andric /// in ""'s. 23960b57cec5SDimitry Andric bool GetIncludeFilenameSpelling(SourceLocation Loc,StringRef &Buffer); 23970b57cec5SDimitry Andric 23980b57cec5SDimitry Andric /// Given a "foo" or \<foo> reference, look up the indicated file. 23990b57cec5SDimitry Andric /// 2400bdd1243dSDimitry Andric /// Returns std::nullopt on failure. \p isAngled indicates whether the file 24010b57cec5SDimitry Andric /// reference is for system \#include's or not (i.e. using <> instead of ""). 2402bdd1243dSDimitry Andric OptionalFileEntryRef 2403a7dea167SDimitry Andric LookupFile(SourceLocation FilenameLoc, StringRef Filename, bool isAngled, 240481ad6265SDimitry Andric ConstSearchDirIterator FromDir, const FileEntry *FromFile, 240581ad6265SDimitry Andric ConstSearchDirIterator *CurDir, SmallVectorImpl<char> *SearchPath, 24060b57cec5SDimitry Andric SmallVectorImpl<char> *RelativePath, 2407a7dea167SDimitry Andric ModuleMap::KnownHeader *SuggestedModule, bool *IsMapped, 2408bdd1243dSDimitry Andric bool *IsFrameworkFound, bool SkipCache = false, 2409bdd1243dSDimitry Andric bool OpenFile = true, bool CacheFailures = true); 24100b57cec5SDimitry Andric 24110b57cec5SDimitry Andric /// Return true if we're in the top-level file, not in a \#include. 24120b57cec5SDimitry Andric bool isInPrimaryFile() const; 24130b57cec5SDimitry Andric 24140b57cec5SDimitry Andric /// Lex an on-off-switch (C99 6.10.6p2) and verify that it is 24150b57cec5SDimitry Andric /// followed by EOD. Return true if the token is not a valid on-off-switch. 24160b57cec5SDimitry Andric bool LexOnOffSwitch(tok::OnOffSwitch &Result); 24170b57cec5SDimitry Andric 24180b57cec5SDimitry Andric bool CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef, 24190b57cec5SDimitry Andric bool *ShadowFlag = nullptr); 24200b57cec5SDimitry Andric 24210b57cec5SDimitry Andric void EnterSubmodule(Module *M, SourceLocation ImportLoc, bool ForPragma); 24220b57cec5SDimitry Andric Module *LeaveSubmodule(bool ForPragma); 24230b57cec5SDimitry Andric 24240b57cec5SDimitry Andric private: 24250b57cec5SDimitry Andric friend void TokenLexer::ExpandFunctionArguments(); 24260b57cec5SDimitry Andric PushIncludeMacroStack()24270b57cec5SDimitry Andric void PushIncludeMacroStack() { 24285f757f3fSDimitry Andric assert(CurLexerCallback != CLK_CachingLexer && 24295f757f3fSDimitry Andric "cannot push a caching lexer"); 24305f757f3fSDimitry Andric IncludeMacroStack.emplace_back(CurLexerCallback, CurLexerSubmodule, 24310b57cec5SDimitry Andric std::move(CurLexer), CurPPLexer, 24320b57cec5SDimitry Andric std::move(CurTokenLexer), CurDirLookup); 24330b57cec5SDimitry Andric CurPPLexer = nullptr; 24340b57cec5SDimitry Andric } 24350b57cec5SDimitry Andric PopIncludeMacroStack()24360b57cec5SDimitry Andric void PopIncludeMacroStack() { 24370b57cec5SDimitry Andric CurLexer = std::move(IncludeMacroStack.back().TheLexer); 24380b57cec5SDimitry Andric CurPPLexer = IncludeMacroStack.back().ThePPLexer; 24390b57cec5SDimitry Andric CurTokenLexer = std::move(IncludeMacroStack.back().TheTokenLexer); 24400b57cec5SDimitry Andric CurDirLookup = IncludeMacroStack.back().TheDirLookup; 24410b57cec5SDimitry Andric CurLexerSubmodule = IncludeMacroStack.back().TheSubmodule; 24425f757f3fSDimitry Andric CurLexerCallback = IncludeMacroStack.back().CurLexerCallback; 24430b57cec5SDimitry Andric IncludeMacroStack.pop_back(); 24440b57cec5SDimitry Andric } 24450b57cec5SDimitry Andric 24460b57cec5SDimitry Andric void PropagateLineStartLeadingSpaceInfo(Token &Result); 24470b57cec5SDimitry Andric 24480b57cec5SDimitry Andric /// Determine whether we need to create module macros for #defines in the 24490b57cec5SDimitry Andric /// current context. 24500b57cec5SDimitry Andric bool needModuleMacros() const; 24510b57cec5SDimitry Andric 24520b57cec5SDimitry Andric /// Update the set of active module macros and ambiguity flag for a module 24530b57cec5SDimitry Andric /// macro name. 24540b57cec5SDimitry Andric void updateModuleMacroInfo(const IdentifierInfo *II, ModuleMacroInfo &Info); 24550b57cec5SDimitry Andric 24560b57cec5SDimitry Andric DefMacroDirective *AllocateDefMacroDirective(MacroInfo *MI, 24570b57cec5SDimitry Andric SourceLocation Loc); 24580b57cec5SDimitry Andric UndefMacroDirective *AllocateUndefMacroDirective(SourceLocation UndefLoc); 24590b57cec5SDimitry Andric VisibilityMacroDirective *AllocateVisibilityMacroDirective(SourceLocation Loc, 24600b57cec5SDimitry Andric bool isPublic); 24610b57cec5SDimitry Andric 24620b57cec5SDimitry Andric /// Lex and validate a macro name, which occurs after a 24630b57cec5SDimitry Andric /// \#define or \#undef. 24640b57cec5SDimitry Andric /// 24650b57cec5SDimitry Andric /// \param MacroNameTok Token that represents the name defined or undefined. 24660b57cec5SDimitry Andric /// \param IsDefineUndef Kind if preprocessor directive. 24670b57cec5SDimitry Andric /// \param ShadowFlag Points to flag that is set if macro name shadows 24680b57cec5SDimitry Andric /// a keyword. 24690b57cec5SDimitry Andric /// 24700b57cec5SDimitry Andric /// This emits a diagnostic, sets the token kind to eod, 24710b57cec5SDimitry Andric /// and discards the rest of the macro line if the macro name is invalid. 24720b57cec5SDimitry Andric void ReadMacroName(Token &MacroNameTok, MacroUse IsDefineUndef = MU_Other, 24730b57cec5SDimitry Andric bool *ShadowFlag = nullptr); 24740b57cec5SDimitry Andric 24750b57cec5SDimitry Andric /// ReadOptionalMacroParameterListAndBody - This consumes all (i.e. the 24760b57cec5SDimitry Andric /// entire line) of the macro's tokens and adds them to MacroInfo, and while 24770b57cec5SDimitry Andric /// doing so performs certain validity checks including (but not limited to): 24780b57cec5SDimitry Andric /// - # (stringization) is followed by a macro parameter 24790b57cec5SDimitry Andric /// \param MacroNameTok - Token that represents the macro name 24800b57cec5SDimitry Andric /// \param ImmediatelyAfterHeaderGuard - Macro follows an #ifdef header guard 24810b57cec5SDimitry Andric /// 24820b57cec5SDimitry Andric /// Either returns a pointer to a MacroInfo object OR emits a diagnostic and 24830b57cec5SDimitry Andric /// returns a nullptr if an invalid sequence of tokens is encountered. 24840b57cec5SDimitry Andric MacroInfo *ReadOptionalMacroParameterListAndBody( 24850b57cec5SDimitry Andric const Token &MacroNameTok, bool ImmediatelyAfterHeaderGuard); 24860b57cec5SDimitry Andric 24870b57cec5SDimitry Andric /// The ( starting an argument list of a macro definition has just been read. 24880b57cec5SDimitry Andric /// Lex the rest of the parameters and the closing ), updating \p MI with 24890b57cec5SDimitry Andric /// what we learn and saving in \p LastTok the last token read. 24900b57cec5SDimitry Andric /// Return true if an error occurs parsing the arg list. 24910b57cec5SDimitry Andric bool ReadMacroParameterList(MacroInfo *MI, Token& LastTok); 24920b57cec5SDimitry Andric 249381ad6265SDimitry Andric /// Provide a suggestion for a typoed directive. If there is no typo, then 249481ad6265SDimitry Andric /// just skip suggesting. 249581ad6265SDimitry Andric /// 249681ad6265SDimitry Andric /// \param Tok - Token that represents the directive 249781ad6265SDimitry Andric /// \param Directive - String reference for the directive name 249881ad6265SDimitry Andric void SuggestTypoedDirective(const Token &Tok, StringRef Directive) const; 249981ad6265SDimitry Andric 25000b57cec5SDimitry Andric /// We just read a \#if or related directive and decided that the 25010b57cec5SDimitry Andric /// subsequent tokens are in the \#if'd out portion of the 25020b57cec5SDimitry Andric /// file. Lex the rest of the file, until we see an \#endif. If \p 25030b57cec5SDimitry Andric /// FoundNonSkipPortion is true, then we have already emitted code for part of 25040b57cec5SDimitry Andric /// this \#if directive, so \#else/\#elif blocks should never be entered. If 25050b57cec5SDimitry Andric /// \p FoundElse is false, then \#else directives are ok, if not, then we have 25060b57cec5SDimitry Andric /// already seen one so a \#else directive is a duplicate. When this returns, 25070b57cec5SDimitry Andric /// the caller can lex the first valid token. 25080b57cec5SDimitry Andric void SkipExcludedConditionalBlock(SourceLocation HashTokenLoc, 25090b57cec5SDimitry Andric SourceLocation IfTokenLoc, 25100b57cec5SDimitry Andric bool FoundNonSkipPortion, bool FoundElse, 25110b57cec5SDimitry Andric SourceLocation ElseLoc = SourceLocation()); 25120b57cec5SDimitry Andric 25130b57cec5SDimitry Andric /// Information about the result for evaluating an expression for a 25140b57cec5SDimitry Andric /// preprocessor directive. 25150b57cec5SDimitry Andric struct DirectiveEvalResult { 25160b57cec5SDimitry Andric /// Whether the expression was evaluated as true or not. 25170b57cec5SDimitry Andric bool Conditional; 25180b57cec5SDimitry Andric 25190b57cec5SDimitry Andric /// True if the expression contained identifiers that were undefined. 25200b57cec5SDimitry Andric bool IncludedUndefinedIds; 25210b57cec5SDimitry Andric 25220b57cec5SDimitry Andric /// The source range for the expression. 25230b57cec5SDimitry Andric SourceRange ExprRange; 25240b57cec5SDimitry Andric }; 25250b57cec5SDimitry Andric 25260b57cec5SDimitry Andric /// Evaluate an integer constant expression that may occur after a 25270b57cec5SDimitry Andric /// \#if or \#elif directive and return a \p DirectiveEvalResult object. 25280b57cec5SDimitry Andric /// 25290b57cec5SDimitry Andric /// If the expression is equivalent to "!defined(X)" return X in IfNDefMacro. 25300b57cec5SDimitry Andric DirectiveEvalResult EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro); 25310b57cec5SDimitry Andric 253281ad6265SDimitry Andric /// Process a '__has_include("path")' expression. 253381ad6265SDimitry Andric /// 253481ad6265SDimitry Andric /// Returns true if successful. 253581ad6265SDimitry Andric bool EvaluateHasInclude(Token &Tok, IdentifierInfo *II); 253681ad6265SDimitry Andric 253781ad6265SDimitry Andric /// Process '__has_include_next("path")' expression. 253881ad6265SDimitry Andric /// 253981ad6265SDimitry Andric /// Returns true if successful. 254081ad6265SDimitry Andric bool EvaluateHasIncludeNext(Token &Tok, IdentifierInfo *II); 254181ad6265SDimitry Andric 254281ad6265SDimitry Andric /// Get the directory and file from which to start \#include_next lookup. 254381ad6265SDimitry Andric std::pair<ConstSearchDirIterator, const FileEntry *> 254481ad6265SDimitry Andric getIncludeNextStart(const Token &IncludeNextTok) const; 254581ad6265SDimitry Andric 25460b57cec5SDimitry Andric /// Install the standard preprocessor pragmas: 25470b57cec5SDimitry Andric /// \#pragma GCC poison/system_header/dependency and \#pragma once. 25480b57cec5SDimitry Andric void RegisterBuiltinPragmas(); 25490b57cec5SDimitry Andric 25500b57cec5SDimitry Andric /// Register builtin macros such as __LINE__ with the identifier table. 25510b57cec5SDimitry Andric void RegisterBuiltinMacros(); 25520b57cec5SDimitry Andric 25530b57cec5SDimitry Andric /// If an identifier token is read that is to be expanded as a macro, handle 25540b57cec5SDimitry Andric /// it and return the next token as 'Tok'. If we lexed a token, return true; 25550b57cec5SDimitry Andric /// otherwise the caller should lex again. 25560b57cec5SDimitry Andric bool HandleMacroExpandedIdentifier(Token &Identifier, const MacroDefinition &MD); 25570b57cec5SDimitry Andric 25580b57cec5SDimitry Andric /// Cache macro expanded tokens for TokenLexers. 25590b57cec5SDimitry Andric // 25600b57cec5SDimitry Andric /// Works like a stack; a TokenLexer adds the macro expanded tokens that is 25610b57cec5SDimitry Andric /// going to lex in the cache and when it finishes the tokens are removed 25620b57cec5SDimitry Andric /// from the end of the cache. 25630b57cec5SDimitry Andric Token *cacheMacroExpandedTokens(TokenLexer *tokLexer, 25640b57cec5SDimitry Andric ArrayRef<Token> tokens); 25650b57cec5SDimitry Andric 25660b57cec5SDimitry Andric void removeCachedMacroExpandedTokensOfLastLexer(); 25670b57cec5SDimitry Andric 25680b57cec5SDimitry Andric /// Determine whether the next preprocessor token to be 25690b57cec5SDimitry Andric /// lexed is a '('. If so, consume the token and return true, if not, this 25700b57cec5SDimitry Andric /// method should have no observable side-effect on the lexed tokens. 25710b57cec5SDimitry Andric bool isNextPPTokenLParen(); 25720b57cec5SDimitry Andric 25730b57cec5SDimitry Andric /// After reading "MACRO(", this method is invoked to read all of the formal 25740b57cec5SDimitry Andric /// arguments specified for the macro invocation. Returns null on error. 25750b57cec5SDimitry Andric MacroArgs *ReadMacroCallArgumentList(Token &MacroName, MacroInfo *MI, 25760b57cec5SDimitry Andric SourceLocation &MacroEnd); 25770b57cec5SDimitry Andric 25780b57cec5SDimitry Andric /// If an identifier token is read that is to be expanded 25790b57cec5SDimitry Andric /// as a builtin macro, handle it and return the next token as 'Tok'. 25800b57cec5SDimitry Andric void ExpandBuiltinMacro(Token &Tok); 25810b57cec5SDimitry Andric 25820b57cec5SDimitry Andric /// Read a \c _Pragma directive, slice it up, process it, then 25830b57cec5SDimitry Andric /// return the first token after the directive. 25840b57cec5SDimitry Andric /// This assumes that the \c _Pragma token has just been read into \p Tok. 25850b57cec5SDimitry Andric void Handle_Pragma(Token &Tok); 25860b57cec5SDimitry Andric 25870b57cec5SDimitry Andric /// Like Handle_Pragma except the pragma text is not enclosed within 25880b57cec5SDimitry Andric /// a string literal. 25890b57cec5SDimitry Andric void HandleMicrosoft__pragma(Token &Tok); 25900b57cec5SDimitry Andric 25910b57cec5SDimitry Andric /// Add a lexer to the top of the include stack and 25920b57cec5SDimitry Andric /// start lexing tokens from it instead of the current buffer. 259381ad6265SDimitry Andric void EnterSourceFileWithLexer(Lexer *TheLexer, ConstSearchDirIterator Dir); 25940b57cec5SDimitry Andric 25950b57cec5SDimitry Andric /// Set the FileID for the preprocessor predefines. setPredefinesFileID(FileID FID)25960b57cec5SDimitry Andric void setPredefinesFileID(FileID FID) { 25970b57cec5SDimitry Andric assert(PredefinesFileID.isInvalid() && "PredefinesFileID already set!"); 25980b57cec5SDimitry Andric PredefinesFileID = FID; 25990b57cec5SDimitry Andric } 26000b57cec5SDimitry Andric 26010b57cec5SDimitry Andric /// Set the FileID for the PCH through header. 26020b57cec5SDimitry Andric void setPCHThroughHeaderFileID(FileID FID); 26030b57cec5SDimitry Andric 26040b57cec5SDimitry Andric /// Returns true if we are lexing from a file and not a 26050b57cec5SDimitry Andric /// pragma or a macro. IsFileLexer(const Lexer * L,const PreprocessorLexer * P)26060b57cec5SDimitry Andric static bool IsFileLexer(const Lexer* L, const PreprocessorLexer* P) { 26070b57cec5SDimitry Andric return L ? !L->isPragmaLexer() : P != nullptr; 26080b57cec5SDimitry Andric } 26090b57cec5SDimitry Andric IsFileLexer(const IncludeStackInfo & I)26100b57cec5SDimitry Andric static bool IsFileLexer(const IncludeStackInfo& I) { 26110b57cec5SDimitry Andric return IsFileLexer(I.TheLexer.get(), I.ThePPLexer); 26120b57cec5SDimitry Andric } 26130b57cec5SDimitry Andric IsFileLexer()26140b57cec5SDimitry Andric bool IsFileLexer() const { 26150b57cec5SDimitry Andric return IsFileLexer(CurLexer.get(), CurPPLexer); 26160b57cec5SDimitry Andric } 26170b57cec5SDimitry Andric 26180b57cec5SDimitry Andric //===--------------------------------------------------------------------===// 26190b57cec5SDimitry Andric // Caching stuff. 26200b57cec5SDimitry Andric void CachingLex(Token &Result); 26210b57cec5SDimitry Andric InCachingLexMode()26220b57cec5SDimitry Andric bool InCachingLexMode() const { 26230b57cec5SDimitry Andric // If the Lexer pointers are 0 and IncludeMacroStack is empty, it means 26240b57cec5SDimitry Andric // that we are past EOF, not that we are in CachingLex mode. 26250b57cec5SDimitry Andric return !CurPPLexer && !CurTokenLexer && !IncludeMacroStack.empty(); 26260b57cec5SDimitry Andric } 26270b57cec5SDimitry Andric 26280b57cec5SDimitry Andric void EnterCachingLexMode(); 26290b57cec5SDimitry Andric void EnterCachingLexModeUnchecked(); 26300b57cec5SDimitry Andric ExitCachingLexMode()26310b57cec5SDimitry Andric void ExitCachingLexMode() { 26320b57cec5SDimitry Andric if (InCachingLexMode()) 26330b57cec5SDimitry Andric RemoveTopOfLexerStack(); 26340b57cec5SDimitry Andric } 26350b57cec5SDimitry Andric 26360b57cec5SDimitry Andric const Token &PeekAhead(unsigned N); 26370b57cec5SDimitry Andric void AnnotatePreviousCachedTokens(const Token &Tok); 26380b57cec5SDimitry Andric 26390b57cec5SDimitry Andric //===--------------------------------------------------------------------===// 26400b57cec5SDimitry Andric /// Handle*Directive - implement the various preprocessor directives. These 26410b57cec5SDimitry Andric /// should side-effect the current preprocessor object so that the next call 26420b57cec5SDimitry Andric /// to Lex() will return the appropriate token next. 26430b57cec5SDimitry Andric void HandleLineDirective(); 26440b57cec5SDimitry Andric void HandleDigitDirective(Token &Tok); 26450b57cec5SDimitry Andric void HandleUserDiagnosticDirective(Token &Tok, bool isWarning); 26460b57cec5SDimitry Andric void HandleIdentSCCSDirective(Token &Tok); 26470b57cec5SDimitry Andric void HandleMacroPublicDirective(Token &Tok); 26480b57cec5SDimitry Andric void HandleMacroPrivateDirective(); 26490b57cec5SDimitry Andric 26500b57cec5SDimitry Andric /// An additional notification that can be produced by a header inclusion or 26510b57cec5SDimitry Andric /// import to tell the parser what happened. 26520b57cec5SDimitry Andric struct ImportAction { 26530b57cec5SDimitry Andric enum ActionKind { 26540b57cec5SDimitry Andric None, 26550b57cec5SDimitry Andric ModuleBegin, 26560b57cec5SDimitry Andric ModuleImport, 2657753f127fSDimitry Andric HeaderUnitImport, 26580b57cec5SDimitry Andric SkippedModuleImport, 26595ffd83dbSDimitry Andric Failure, 26600b57cec5SDimitry Andric } Kind; 26610b57cec5SDimitry Andric Module *ModuleForHeader = nullptr; 26620b57cec5SDimitry Andric 26630b57cec5SDimitry Andric ImportAction(ActionKind AK, Module *Mod = nullptr) KindImportAction26640b57cec5SDimitry Andric : Kind(AK), ModuleForHeader(Mod) { 26655ffd83dbSDimitry Andric assert((AK == None || Mod || AK == Failure) && 26665ffd83dbSDimitry Andric "no module for module action"); 26670b57cec5SDimitry Andric } 26680b57cec5SDimitry Andric }; 26690b57cec5SDimitry Andric 2670bdd1243dSDimitry Andric OptionalFileEntryRef LookupHeaderIncludeOrImport( 267181ad6265SDimitry Andric ConstSearchDirIterator *CurDir, StringRef &Filename, 2672a7dea167SDimitry Andric SourceLocation FilenameLoc, CharSourceRange FilenameRange, 2673a7dea167SDimitry Andric const Token &FilenameTok, bool &IsFrameworkFound, bool IsImportDecl, 267481ad6265SDimitry Andric bool &IsMapped, ConstSearchDirIterator LookupFrom, 26755ffd83dbSDimitry Andric const FileEntry *LookupFromFile, StringRef &LookupFilename, 2676a7dea167SDimitry Andric SmallVectorImpl<char> &RelativePath, SmallVectorImpl<char> &SearchPath, 2677a7dea167SDimitry Andric ModuleMap::KnownHeader &SuggestedModule, bool isAngled); 2678a7dea167SDimitry Andric 26790b57cec5SDimitry Andric // File inclusion. 26800b57cec5SDimitry Andric void HandleIncludeDirective(SourceLocation HashLoc, Token &Tok, 268181ad6265SDimitry Andric ConstSearchDirIterator LookupFrom = nullptr, 26820b57cec5SDimitry Andric const FileEntry *LookupFromFile = nullptr); 26830b57cec5SDimitry Andric ImportAction 26840b57cec5SDimitry Andric HandleHeaderIncludeOrImport(SourceLocation HashLoc, Token &IncludeTok, 26850b57cec5SDimitry Andric Token &FilenameTok, SourceLocation EndLoc, 268681ad6265SDimitry Andric ConstSearchDirIterator LookupFrom = nullptr, 26870b57cec5SDimitry Andric const FileEntry *LookupFromFile = nullptr); 26880b57cec5SDimitry Andric void HandleIncludeNextDirective(SourceLocation HashLoc, Token &Tok); 26890b57cec5SDimitry Andric void HandleIncludeMacrosDirective(SourceLocation HashLoc, Token &Tok); 26900b57cec5SDimitry Andric void HandleImportDirective(SourceLocation HashLoc, Token &Tok); 26910b57cec5SDimitry Andric void HandleMicrosoftImportDirective(Token &Tok); 26920b57cec5SDimitry Andric 26930b57cec5SDimitry Andric public: 26940b57cec5SDimitry Andric /// Check that the given module is available, producing a diagnostic if not. 26950b57cec5SDimitry Andric /// \return \c true if the check failed (because the module is not available). 26960b57cec5SDimitry Andric /// \c false if the module appears to be usable. 26970b57cec5SDimitry Andric static bool checkModuleIsAvailable(const LangOptions &LangOpts, 26980b57cec5SDimitry Andric const TargetInfo &TargetInfo, 26995f757f3fSDimitry Andric const Module &M, DiagnosticsEngine &Diags); 27000b57cec5SDimitry Andric 27010b57cec5SDimitry Andric // Module inclusion testing. 27020b57cec5SDimitry Andric /// Find the module that owns the source or header file that 27030b57cec5SDimitry Andric /// \p Loc points to. If the location is in a file that was included 27040b57cec5SDimitry Andric /// into a module, or is outside any module, returns nullptr. 2705bdd1243dSDimitry Andric Module *getModuleForLocation(SourceLocation Loc, bool AllowTextual); 27060b57cec5SDimitry Andric 27075ffd83dbSDimitry Andric /// We want to produce a diagnostic at location IncLoc concerning an 27085ffd83dbSDimitry Andric /// unreachable effect at location MLoc (eg, where a desired entity was 27095ffd83dbSDimitry Andric /// declared or defined). Determine whether the right way to make MLoc 27105ffd83dbSDimitry Andric /// reachable is by #include, and if so, what header should be included. 27110b57cec5SDimitry Andric /// 27125ffd83dbSDimitry Andric /// This is not necessarily fast, and might load unexpected module maps, so 27135ffd83dbSDimitry Andric /// should only be called by code that intends to produce an error. 27140b57cec5SDimitry Andric /// 27155ffd83dbSDimitry Andric /// \param IncLoc The location at which the missing effect was detected. 27165ffd83dbSDimitry Andric /// \param MLoc A location within an unimported module at which the desired 27175ffd83dbSDimitry Andric /// effect occurred. 27185ffd83dbSDimitry Andric /// \return A file that can be #included to provide the desired effect. Null 27195ffd83dbSDimitry Andric /// if no such file could be determined or if a #include is not 27205ffd83dbSDimitry Andric /// appropriate (eg, if a module should be imported instead). 27215f757f3fSDimitry Andric OptionalFileEntryRef getHeaderToIncludeForDiagnostics(SourceLocation IncLoc, 27220b57cec5SDimitry Andric SourceLocation MLoc); 27230b57cec5SDimitry Andric isRecordingPreamble()27240b57cec5SDimitry Andric bool isRecordingPreamble() const { 27250b57cec5SDimitry Andric return PreambleConditionalStack.isRecording(); 27260b57cec5SDimitry Andric } 27270b57cec5SDimitry Andric hasRecordedPreamble()27280b57cec5SDimitry Andric bool hasRecordedPreamble() const { 27290b57cec5SDimitry Andric return PreambleConditionalStack.hasRecordedPreamble(); 27300b57cec5SDimitry Andric } 27310b57cec5SDimitry Andric getPreambleConditionalStack()27320b57cec5SDimitry Andric ArrayRef<PPConditionalInfo> getPreambleConditionalStack() const { 27330b57cec5SDimitry Andric return PreambleConditionalStack.getStack(); 27340b57cec5SDimitry Andric } 27350b57cec5SDimitry Andric setRecordedPreambleConditionalStack(ArrayRef<PPConditionalInfo> s)27360b57cec5SDimitry Andric void setRecordedPreambleConditionalStack(ArrayRef<PPConditionalInfo> s) { 27370b57cec5SDimitry Andric PreambleConditionalStack.setStack(s); 27380b57cec5SDimitry Andric } 27390b57cec5SDimitry Andric setReplayablePreambleConditionalStack(ArrayRef<PPConditionalInfo> s,std::optional<PreambleSkipInfo> SkipInfo)2740bdd1243dSDimitry Andric void setReplayablePreambleConditionalStack( 2741bdd1243dSDimitry Andric ArrayRef<PPConditionalInfo> s, std::optional<PreambleSkipInfo> SkipInfo) { 27420b57cec5SDimitry Andric PreambleConditionalStack.startReplaying(); 27430b57cec5SDimitry Andric PreambleConditionalStack.setStack(s); 27440b57cec5SDimitry Andric PreambleConditionalStack.SkipInfo = SkipInfo; 27450b57cec5SDimitry Andric } 27460b57cec5SDimitry Andric getPreambleSkipInfo()2747bdd1243dSDimitry Andric std::optional<PreambleSkipInfo> getPreambleSkipInfo() const { 27480b57cec5SDimitry Andric return PreambleConditionalStack.SkipInfo; 27490b57cec5SDimitry Andric } 27500b57cec5SDimitry Andric 27510b57cec5SDimitry Andric private: 27520b57cec5SDimitry Andric /// After processing predefined file, initialize the conditional stack from 27530b57cec5SDimitry Andric /// the preamble. 27540b57cec5SDimitry Andric void replayPreambleConditionalStack(); 27550b57cec5SDimitry Andric 27560b57cec5SDimitry Andric // Macro handling. 27570b57cec5SDimitry Andric void HandleDefineDirective(Token &Tok, bool ImmediatelyAfterHeaderGuard); 27580b57cec5SDimitry Andric void HandleUndefDirective(); 27590b57cec5SDimitry Andric 27600b57cec5SDimitry Andric // Conditional Inclusion. 27610b57cec5SDimitry Andric void HandleIfdefDirective(Token &Result, const Token &HashToken, 27620b57cec5SDimitry Andric bool isIfndef, bool ReadAnyTokensBeforeDirective); 27630b57cec5SDimitry Andric void HandleIfDirective(Token &IfToken, const Token &HashToken, 27640b57cec5SDimitry Andric bool ReadAnyTokensBeforeDirective); 27650b57cec5SDimitry Andric void HandleEndifDirective(Token &EndifToken); 27660b57cec5SDimitry Andric void HandleElseDirective(Token &Result, const Token &HashToken); 2767fe6060f1SDimitry Andric void HandleElifFamilyDirective(Token &ElifToken, const Token &HashToken, 2768fe6060f1SDimitry Andric tok::PPKeywordKind Kind); 27690b57cec5SDimitry Andric 27700b57cec5SDimitry Andric // Pragmas. 27710b57cec5SDimitry Andric void HandlePragmaDirective(PragmaIntroducer Introducer); 27720b57cec5SDimitry Andric 27730b57cec5SDimitry Andric public: 27740b57cec5SDimitry Andric void HandlePragmaOnce(Token &OnceTok); 2775fe6060f1SDimitry Andric void HandlePragmaMark(Token &MarkTok); 27760b57cec5SDimitry Andric void HandlePragmaPoison(); 27770b57cec5SDimitry Andric void HandlePragmaSystemHeader(Token &SysHeaderTok); 27780b57cec5SDimitry Andric void HandlePragmaDependency(Token &DependencyTok); 27790b57cec5SDimitry Andric void HandlePragmaPushMacro(Token &Tok); 27800b57cec5SDimitry Andric void HandlePragmaPopMacro(Token &Tok); 27810b57cec5SDimitry Andric void HandlePragmaIncludeAlias(Token &Tok); 27820b57cec5SDimitry Andric void HandlePragmaModuleBuild(Token &Tok); 27830b57cec5SDimitry Andric void HandlePragmaHdrstop(Token &Tok); 27840b57cec5SDimitry Andric IdentifierInfo *ParsePragmaPushOrPopMacro(Token &Tok); 27850b57cec5SDimitry Andric 27860b57cec5SDimitry Andric // Return true and store the first token only if any CommentHandler 27870b57cec5SDimitry Andric // has inserted some tokens and getCommentRetentionState() is false. 27880b57cec5SDimitry Andric bool HandleComment(Token &result, SourceRange Comment); 27890b57cec5SDimitry Andric 27900b57cec5SDimitry Andric /// A macro is used, update information about macros that need unused 27910b57cec5SDimitry Andric /// warnings. 27920b57cec5SDimitry Andric void markMacroAsUsed(MacroInfo *MI); 2793a7dea167SDimitry Andric addMacroDeprecationMsg(const IdentifierInfo * II,std::string Msg,SourceLocation AnnotationLoc)2794349cc55cSDimitry Andric void addMacroDeprecationMsg(const IdentifierInfo *II, std::string Msg, 2795349cc55cSDimitry Andric SourceLocation AnnotationLoc) { 2796349cc55cSDimitry Andric auto Annotations = AnnotationInfos.find(II); 2797349cc55cSDimitry Andric if (Annotations == AnnotationInfos.end()) 2798349cc55cSDimitry Andric AnnotationInfos.insert(std::make_pair( 2799349cc55cSDimitry Andric II, 2800349cc55cSDimitry Andric MacroAnnotations::makeDeprecation(AnnotationLoc, std::move(Msg)))); 2801349cc55cSDimitry Andric else 2802349cc55cSDimitry Andric Annotations->second.DeprecationInfo = 2803349cc55cSDimitry Andric MacroAnnotationInfo{AnnotationLoc, std::move(Msg)}; 2804349cc55cSDimitry Andric } 2805349cc55cSDimitry Andric addRestrictExpansionMsg(const IdentifierInfo * II,std::string Msg,SourceLocation AnnotationLoc)2806349cc55cSDimitry Andric void addRestrictExpansionMsg(const IdentifierInfo *II, std::string Msg, 2807349cc55cSDimitry Andric SourceLocation AnnotationLoc) { 2808349cc55cSDimitry Andric auto Annotations = AnnotationInfos.find(II); 2809349cc55cSDimitry Andric if (Annotations == AnnotationInfos.end()) 2810349cc55cSDimitry Andric AnnotationInfos.insert( 2811349cc55cSDimitry Andric std::make_pair(II, MacroAnnotations::makeRestrictExpansion( 2812349cc55cSDimitry Andric AnnotationLoc, std::move(Msg)))); 2813349cc55cSDimitry Andric else 2814349cc55cSDimitry Andric Annotations->second.RestrictExpansionInfo = 2815349cc55cSDimitry Andric MacroAnnotationInfo{AnnotationLoc, std::move(Msg)}; 2816349cc55cSDimitry Andric } 2817349cc55cSDimitry Andric addFinalLoc(const IdentifierInfo * II,SourceLocation AnnotationLoc)2818349cc55cSDimitry Andric void addFinalLoc(const IdentifierInfo *II, SourceLocation AnnotationLoc) { 2819349cc55cSDimitry Andric auto Annotations = AnnotationInfos.find(II); 2820349cc55cSDimitry Andric if (Annotations == AnnotationInfos.end()) 2821349cc55cSDimitry Andric AnnotationInfos.insert( 2822349cc55cSDimitry Andric std::make_pair(II, MacroAnnotations::makeFinal(AnnotationLoc))); 2823349cc55cSDimitry Andric else 2824349cc55cSDimitry Andric Annotations->second.FinalAnnotationLoc = AnnotationLoc; 2825349cc55cSDimitry Andric } 2826349cc55cSDimitry Andric getMacroAnnotations(const IdentifierInfo * II)2827349cc55cSDimitry Andric const MacroAnnotations &getMacroAnnotations(const IdentifierInfo *II) const { 2828349cc55cSDimitry Andric return AnnotationInfos.find(II)->second; 2829349cc55cSDimitry Andric } 2830349cc55cSDimitry Andric 2831b3edf446SDimitry Andric void emitMacroExpansionWarnings(const Token &Identifier, 2832b3edf446SDimitry Andric bool IsIfnDef = false) const { 28337a6dacacSDimitry Andric IdentifierInfo *Info = Identifier.getIdentifierInfo(); 28347a6dacacSDimitry Andric if (Info->isDeprecatedMacro()) 2835349cc55cSDimitry Andric emitMacroDeprecationWarning(Identifier); 2836349cc55cSDimitry Andric 28377a6dacacSDimitry Andric if (Info->isRestrictExpansion() && 2838349cc55cSDimitry Andric !SourceMgr.isInMainFile(Identifier.getLocation())) 2839349cc55cSDimitry Andric emitRestrictExpansionWarning(Identifier); 28407a6dacacSDimitry Andric 2841b3edf446SDimitry Andric if (!IsIfnDef) { 2842b3edf446SDimitry Andric if (Info->getName() == "INFINITY" && getLangOpts().NoHonorInfs) 28437a6dacacSDimitry Andric emitRestrictInfNaNWarning(Identifier, 0); 2844b3edf446SDimitry Andric if (Info->getName() == "NAN" && getLangOpts().NoHonorNaNs) 28457a6dacacSDimitry Andric emitRestrictInfNaNWarning(Identifier, 1); 2846349cc55cSDimitry Andric } 2847b3edf446SDimitry Andric } 2848349cc55cSDimitry Andric 284981ad6265SDimitry Andric static void processPathForFileMacro(SmallVectorImpl<char> &Path, 285081ad6265SDimitry Andric const LangOptions &LangOpts, 285181ad6265SDimitry Andric const TargetInfo &TI); 285281ad6265SDimitry Andric 285306c3fb27SDimitry Andric static void processPathToFileName(SmallVectorImpl<char> &FileName, 285406c3fb27SDimitry Andric const PresumedLoc &PLoc, 285506c3fb27SDimitry Andric const LangOptions &LangOpts, 285606c3fb27SDimitry Andric const TargetInfo &TI); 285706c3fb27SDimitry Andric 2858a7dea167SDimitry Andric private: 2859349cc55cSDimitry Andric void emitMacroDeprecationWarning(const Token &Identifier) const; 2860349cc55cSDimitry Andric void emitRestrictExpansionWarning(const Token &Identifier) const; 2861349cc55cSDimitry Andric void emitFinalMacroWarning(const Token &Identifier, bool IsUndef) const; 28627a6dacacSDimitry Andric void emitRestrictInfNaNWarning(const Token &Identifier, 28637a6dacacSDimitry Andric unsigned DiagSelection) const; 286406c3fb27SDimitry Andric 286506c3fb27SDimitry Andric /// This boolean state keeps track if the current scanned token (by this PP) 286606c3fb27SDimitry Andric /// is in an "-Wunsafe-buffer-usage" opt-out region. Assuming PP scans a 286706c3fb27SDimitry Andric /// translation unit in a linear order. 286806c3fb27SDimitry Andric bool InSafeBufferOptOutRegion = false; 286906c3fb27SDimitry Andric 287006c3fb27SDimitry Andric /// Hold the start location of the current "-Wunsafe-buffer-usage" opt-out 287106c3fb27SDimitry Andric /// region if PP is currently in such a region. Hold undefined value 287206c3fb27SDimitry Andric /// otherwise. 287306c3fb27SDimitry Andric SourceLocation CurrentSafeBufferOptOutStart; // It is used to report the start location of an never-closed region. 287406c3fb27SDimitry Andric 287506c3fb27SDimitry Andric // An ordered sequence of "-Wunsafe-buffer-usage" opt-out regions in one 287606c3fb27SDimitry Andric // translation unit. Each region is represented by a pair of start and end 287706c3fb27SDimitry Andric // locations. A region is "open" if its' start and end locations are 287806c3fb27SDimitry Andric // identical. 287906c3fb27SDimitry Andric SmallVector<std::pair<SourceLocation, SourceLocation>, 8> SafeBufferOptOutMap; 288006c3fb27SDimitry Andric 288106c3fb27SDimitry Andric public: 288206c3fb27SDimitry Andric /// \return true iff the given `Loc` is in a "-Wunsafe-buffer-usage" opt-out 288306c3fb27SDimitry Andric /// region. This `Loc` must be a source location that has been pre-processed. 288406c3fb27SDimitry Andric bool isSafeBufferOptOut(const SourceManager&SourceMgr, const SourceLocation &Loc) const; 288506c3fb27SDimitry Andric 288606c3fb27SDimitry Andric /// Alter the state of whether this PP currently is in a 288706c3fb27SDimitry Andric /// "-Wunsafe-buffer-usage" opt-out region. 288806c3fb27SDimitry Andric /// 28895f757f3fSDimitry Andric /// \param isEnter true if this PP is entering a region; otherwise, this PP 289006c3fb27SDimitry Andric /// is exiting a region 28915f757f3fSDimitry Andric /// \param Loc the location of the entry or exit of a 289206c3fb27SDimitry Andric /// region 289306c3fb27SDimitry Andric /// \return true iff it is INVALID to enter or exit a region, i.e., 289406c3fb27SDimitry Andric /// attempt to enter a region before exiting a previous region, or exiting a 289506c3fb27SDimitry Andric /// region that PP is not currently in. 289606c3fb27SDimitry Andric bool enterOrExitSafeBufferOptOutRegion(bool isEnter, 289706c3fb27SDimitry Andric const SourceLocation &Loc); 289806c3fb27SDimitry Andric 289906c3fb27SDimitry Andric /// \return true iff this PP is currently in a "-Wunsafe-buffer-usage" 290006c3fb27SDimitry Andric /// opt-out region 290106c3fb27SDimitry Andric bool isPPInSafeBufferOptOutRegion(); 290206c3fb27SDimitry Andric 29035f757f3fSDimitry Andric /// \param StartLoc output argument. It will be set to the start location of 290406c3fb27SDimitry Andric /// the current "-Wunsafe-buffer-usage" opt-out region iff this function 290506c3fb27SDimitry Andric /// returns true. 290606c3fb27SDimitry Andric /// \return true iff this PP is currently in a "-Wunsafe-buffer-usage" 290706c3fb27SDimitry Andric /// opt-out region 290806c3fb27SDimitry Andric bool isPPInSafeBufferOptOutRegion(SourceLocation &StartLoc); 29095f757f3fSDimitry Andric 29105f757f3fSDimitry Andric private: 29115f757f3fSDimitry Andric /// Helper functions to forward lexing to the actual lexer. They all share the 29125f757f3fSDimitry Andric /// same signature. CLK_Lexer(Preprocessor & P,Token & Result)29135f757f3fSDimitry Andric static bool CLK_Lexer(Preprocessor &P, Token &Result) { 29145f757f3fSDimitry Andric return P.CurLexer->Lex(Result); 29155f757f3fSDimitry Andric } CLK_TokenLexer(Preprocessor & P,Token & Result)29165f757f3fSDimitry Andric static bool CLK_TokenLexer(Preprocessor &P, Token &Result) { 29175f757f3fSDimitry Andric return P.CurTokenLexer->Lex(Result); 29185f757f3fSDimitry Andric } CLK_CachingLexer(Preprocessor & P,Token & Result)29195f757f3fSDimitry Andric static bool CLK_CachingLexer(Preprocessor &P, Token &Result) { 29205f757f3fSDimitry Andric P.CachingLex(Result); 29215f757f3fSDimitry Andric return true; 29225f757f3fSDimitry Andric } CLK_DependencyDirectivesLexer(Preprocessor & P,Token & Result)29235f757f3fSDimitry Andric static bool CLK_DependencyDirectivesLexer(Preprocessor &P, Token &Result) { 29245f757f3fSDimitry Andric return P.CurLexer->LexDependencyDirectiveToken(Result); 29255f757f3fSDimitry Andric } CLK_LexAfterModuleImport(Preprocessor & P,Token & Result)29265f757f3fSDimitry Andric static bool CLK_LexAfterModuleImport(Preprocessor &P, Token &Result) { 29275f757f3fSDimitry Andric return P.LexAfterModuleImport(Result); 29285f757f3fSDimitry Andric } 29290b57cec5SDimitry Andric }; 29300b57cec5SDimitry Andric 29310b57cec5SDimitry Andric /// Abstract base class that describes a handler that will receive 29320b57cec5SDimitry Andric /// source ranges for each of the comments encountered in the source file. 29330b57cec5SDimitry Andric class CommentHandler { 29340b57cec5SDimitry Andric public: 29350b57cec5SDimitry Andric virtual ~CommentHandler(); 29360b57cec5SDimitry Andric 29370b57cec5SDimitry Andric // The handler shall return true if it has pushed any tokens 29380b57cec5SDimitry Andric // to be read using e.g. EnterToken or EnterTokenStream. 29390b57cec5SDimitry Andric virtual bool HandleComment(Preprocessor &PP, SourceRange Comment) = 0; 29400b57cec5SDimitry Andric }; 29410b57cec5SDimitry Andric 2942e8d8bef9SDimitry Andric /// Abstract base class that describes a handler that will receive 2943e8d8bef9SDimitry Andric /// source ranges for empty lines encountered in the source file. 2944e8d8bef9SDimitry Andric class EmptylineHandler { 2945e8d8bef9SDimitry Andric public: 2946e8d8bef9SDimitry Andric virtual ~EmptylineHandler(); 2947e8d8bef9SDimitry Andric 2948e8d8bef9SDimitry Andric // The handler handles empty lines. 2949e8d8bef9SDimitry Andric virtual void HandleEmptyline(SourceRange Range) = 0; 2950e8d8bef9SDimitry Andric }; 2951e8d8bef9SDimitry Andric 29520b57cec5SDimitry Andric /// Registry of pragma handlers added by plugins 29530b57cec5SDimitry Andric using PragmaHandlerRegistry = llvm::Registry<PragmaHandler>; 29540b57cec5SDimitry Andric 29550b57cec5SDimitry Andric } // namespace clang 29560b57cec5SDimitry Andric 29570b57cec5SDimitry Andric #endif // LLVM_CLANG_LEX_PREPROCESSOR_H 2958