clang 23.0.0git
Preprocessor.h
Go to the documentation of this file.
1//===- Preprocessor.h - C Language Family Preprocessor ----------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9/// \file
10/// Defines the clang::Preprocessor interface.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_LEX_PREPROCESSOR_H
15#define LLVM_CLANG_LEX_PREPROCESSOR_H
16
20#include "clang/Basic/LLVM.h"
22#include "clang/Basic/Module.h"
27#include "clang/Lex/Lexer.h"
28#include "clang/Lex/MacroInfo.h"
30#include "clang/Lex/ModuleMap.h"
33#include "clang/Lex/Token.h"
36#include "llvm/ADT/APSInt.h"
37#include "llvm/ADT/ArrayRef.h"
38#include "llvm/ADT/DenseMap.h"
39#include "llvm/ADT/FoldingSet.h"
40#include "llvm/ADT/FunctionExtras.h"
41#include "llvm/ADT/PointerUnion.h"
42#include "llvm/ADT/STLExtras.h"
43#include "llvm/ADT/SmallPtrSet.h"
44#include "llvm/ADT/SmallVector.h"
45#include "llvm/ADT/StringRef.h"
46#include "llvm/ADT/TinyPtrVector.h"
47#include "llvm/ADT/iterator_range.h"
48#include "llvm/Support/Allocator.h"
49#include "llvm/Support/Casting.h"
50#include "llvm/Support/Registry.h"
51#include "llvm/Support/TrailingObjects.h"
52#include <cassert>
53#include <cstddef>
54#include <cstdint>
55#include <map>
56#include <memory>
57#include <optional>
58#include <string>
59#include <utility>
60#include <vector>
61
62namespace llvm {
63
64template<unsigned InternalLen> class SmallString;
65
66} // namespace llvm
67
68namespace clang {
69
71class CommentHandler;
72class DirectoryEntry;
75class FileEntry;
76class FileManager;
77class HeaderSearch;
78class MacroArgs;
79class PragmaHandler;
80class PragmaNamespace;
84class ScratchBuffer;
85class TargetInfo;
87
88namespace Builtin {
89class Context;
90}
91
92/// Stores token information for comparing actual tokens with
93/// predefined values. Only handles simple tokens and identifiers.
95 tok::TokenKind Kind;
97
98public:
99 TokenValue(tok::TokenKind Kind) : Kind(Kind), II(nullptr) {
100 assert(Kind != tok::raw_identifier && "Raw identifiers are not supported.");
101 assert(Kind != tok::identifier &&
102 "Identifiers should be created by TokenValue(IdentifierInfo *)");
103 assert(!tok::isLiteral(Kind) && "Literals are not supported.");
104 assert(!tok::isAnnotation(Kind) && "Annotations are not supported.");
105 }
106
107 TokenValue(IdentifierInfo *II) : Kind(tok::identifier), II(II) {}
108
109 bool operator==(const Token &Tok) const {
110 return Tok.getKind() == Kind &&
111 (!II || II == Tok.getIdentifierInfo());
112 }
113};
114
115/// Context in which macro name is used.
117 // other than #define or #undef
119
120 // macro name specified in #define
122
123 // macro name specified in #undef
125};
126
127enum class EmbedResult {
128 Invalid = -1, // Parsing error occurred.
129 NotFound = 0, // Corresponds to __STDC_EMBED_NOT_FOUND__
130 Found = 1, // Corresponds to __STDC_EMBED_FOUND__
131 Empty = 2, // Corresponds to __STDC_EMBED_EMPTY__
132};
133
139
140/// Record the previous 'export' keyword info.
141///
142/// Since P1857R3, the standard introduced several rules to determine whether
143/// the 'module', 'export module', 'import', 'export import' is a valid
144/// directive introducer. This class is used to record the previous 'export'
145/// keyword token, and then handle 'export module' and 'export import'.
147 Token ExportTok;
148 bool AtPhysicalStartOfLine = false;
149
150public:
152 ExportContextualKeywordInfo(const Token &Tok, bool AtPhysicalStartOfLine)
153 : ExportTok(Tok), AtPhysicalStartOfLine(AtPhysicalStartOfLine) {}
154
155 bool isValid() const { return ExportTok.is(tok::kw_export); }
156 bool isAtPhysicalStartOfLine() const { return AtPhysicalStartOfLine; }
157 Token getExportTok() const { return ExportTok; }
158 void reset() {
159 ExportTok.startToken();
160 AtPhysicalStartOfLine = false;
161 }
162};
163
164class ModuleNameLoc final
165 : llvm::TrailingObjects<ModuleNameLoc, IdentifierLoc> {
166 friend TrailingObjects;
167 unsigned NumIdentifierLocs;
168 unsigned numTrailingObjects(OverloadToken<IdentifierLoc>) const {
169 return getNumIdentifierLocs();
170 }
171
172 ModuleNameLoc(ModuleIdPath Path) : NumIdentifierLocs(Path.size()) {
173 (void)llvm::copy(Path, getTrailingObjectsNonStrict<IdentifierLoc>());
174 }
175
176public:
177 static ModuleNameLoc *Create(Preprocessor &PP, ModuleIdPath Path);
178 unsigned getNumIdentifierLocs() const { return NumIdentifierLocs; }
180 return {getTrailingObjectsNonStrict<IdentifierLoc>(),
182 }
183
185 return getModuleIdPath().front().getLoc();
186 }
188 auto &Last = getModuleIdPath().back();
189 return Last.getLoc().getLocWithOffset(
190 Last.getIdentifierInfo()->getLength());
191 }
192 SourceRange getRange() const { return {getBeginLoc(), getEndLoc()}; }
193 std::string str() const {
195 }
196};
197
198/// Engages in a tight little dance with the lexer to efficiently
199/// preprocess tokens.
200///
201/// Lexers know only about tokens within a single source file, and don't
202/// know anything about preprocessor-level issues like the \#include stack,
203/// token expansion, etc.
207
208 llvm::unique_function<void(const clang::Token &)> OnToken;
209 /// Functor for getting the dependency preprocessor directives of a file.
210 ///
211 /// These are directives derived from a special form of lexing where the
212 /// source input is scanned for the preprocessor directives that might have an
213 /// effect on the dependencies for a compilation unit.
214 DependencyDirectivesGetter *GetDependencyDirectives = nullptr;
215 const PreprocessorOptions &PPOpts;
216 DiagnosticsEngine *Diags;
217 const LangOptions &LangOpts;
218 const TargetInfo *Target = nullptr;
219 const TargetInfo *AuxTarget = nullptr;
220 FileManager &FileMgr;
221 SourceManager &SourceMgr;
222 std::unique_ptr<ScratchBuffer> ScratchBuf;
223 HeaderSearch &HeaderInfo;
224 ModuleLoader &TheModuleLoader;
225
226 /// External source of macros.
227 ExternalPreprocessorSource *ExternalSource;
228
229 /// A BumpPtrAllocator object used to quickly allocate and release
230 /// objects internal to the Preprocessor.
231 llvm::BumpPtrAllocator BP;
232
233 /// Identifiers for builtin macros and other builtins.
234 IdentifierInfo *Ident__LINE__, *Ident__FILE__; // __LINE__, __FILE__
235 IdentifierInfo *Ident__DATE__, *Ident__TIME__; // __DATE__, __TIME__
236 IdentifierInfo *Ident__INCLUDE_LEVEL__; // __INCLUDE_LEVEL__
237 IdentifierInfo *Ident__BASE_FILE__; // __BASE_FILE__
238 IdentifierInfo *Ident__FILE_NAME__; // __FILE_NAME__
239 IdentifierInfo *Ident__TIMESTAMP__; // __TIMESTAMP__
240 IdentifierInfo *Ident__COUNTER__; // __COUNTER__
241 IdentifierInfo *Ident_Pragma, *Ident__pragma; // _Pragma, __pragma
242 IdentifierInfo *Ident__identifier; // __identifier
243 IdentifierInfo *Ident__VA_ARGS__; // __VA_ARGS__
244 IdentifierInfo *Ident__VA_OPT__; // __VA_OPT__
245 IdentifierInfo *Ident__has_feature; // __has_feature
246 IdentifierInfo *Ident__has_extension; // __has_extension
247 IdentifierInfo *Ident__has_builtin; // __has_builtin
248 IdentifierInfo *Ident__has_constexpr_builtin; // __has_constexpr_builtin
249 IdentifierInfo *Ident__has_attribute; // __has_attribute
250 IdentifierInfo *Ident__has_embed; // __has_embed
251 IdentifierInfo *Ident__has_include; // __has_include
252 IdentifierInfo *Ident__has_include_next; // __has_include_next
253 IdentifierInfo *Ident__has_warning; // __has_warning
254 IdentifierInfo *Ident__is_identifier; // __is_identifier
255 IdentifierInfo *Ident__building_module; // __building_module
256 IdentifierInfo *Ident__MODULE__; // __MODULE__
257 IdentifierInfo *Ident__has_cpp_attribute; // __has_cpp_attribute
258 IdentifierInfo *Ident__has_c_attribute; // __has_c_attribute
259 IdentifierInfo *Ident__has_declspec; // __has_declspec_attribute
260 IdentifierInfo *Ident__is_target_arch; // __is_target_arch
261 IdentifierInfo *Ident__is_target_vendor; // __is_target_vendor
262 IdentifierInfo *Ident__is_target_os; // __is_target_os
263 IdentifierInfo *Ident__is_target_environment; // __is_target_environment
264 IdentifierInfo *Ident__is_target_variant_os;
265 IdentifierInfo *Ident__is_target_variant_environment;
266 IdentifierInfo *Ident__FLT_EVAL_METHOD__; // __FLT_EVAL_METHOD
267
268 // Weak, only valid (and set) while InMacroArgs is true.
269 Token* ArgMacro;
270
271 SourceLocation DATELoc, TIMELoc;
272
273 // FEM_UnsetOnCommandLine means that an explicit evaluation method was
274 // not specified on the command line. The target is queried to set the
275 // default evaluation method.
276 LangOptions::FPEvalMethodKind CurrentFPEvalMethod =
278
279 // The most recent pragma location where the floating point evaluation
280 // method was modified. This is used to determine whether the
281 // 'pragma clang fp eval_method' was used whithin the current scope.
282 SourceLocation LastFPEvalPragmaLocation;
283
284 LangOptions::FPEvalMethodKind TUFPEvalMethod =
286
287 // Next __COUNTER__ value, starts at 0.
288 uint32_t CounterValue = 0;
289
290 enum {
291 /// Maximum depth of \#includes.
292 MaxAllowedIncludeStackDepth = 200
293 };
294
295 // State that is set before the preprocessor begins.
296 bool KeepComments : 1;
297 bool KeepMacroComments : 1;
298 bool SuppressIncludeNotFoundError : 1;
299
300 // State that changes while the preprocessor runs:
301 bool InMacroArgs : 1; // True if parsing fn macro invocation args.
302
303 /// Whether the preprocessor owns the header search object.
304 bool OwnsHeaderSearch : 1;
305
306 /// True if macro expansion is disabled.
307 bool DisableMacroExpansion : 1;
308
309 /// Temporarily disables DisableMacroExpansion (i.e. enables expansion)
310 /// when parsing preprocessor directives.
311 bool MacroExpansionInDirectivesOverride : 1;
312
313 class ResetMacroExpansionHelper;
314
315 /// Whether we have already loaded macros from the external source.
316 mutable bool ReadMacrosFromExternalSource : 1;
317
318 /// True if pragmas are enabled.
319 bool PragmasEnabled : 1;
320
321 /// True if the current build action is a preprocessing action.
322 bool PreprocessedOutput : 1;
323
324 /// True if we are currently preprocessing a #if or #elif directive
325 bool ParsingIfOrElifDirective;
326
327 /// True if we are pre-expanding macro arguments.
328 bool InMacroArgPreExpansion;
329
330 /// Mapping/lookup information for all identifiers in
331 /// the program, including program keywords.
332 mutable IdentifierTable Identifiers;
333
334 /// This table contains all the selectors in the program.
335 ///
336 /// Unlike IdentifierTable above, this table *isn't* populated by the
337 /// preprocessor. It is declared/expanded here because its role/lifetime is
338 /// conceptually similar to the IdentifierTable. In addition, the current
339 /// control flow (in clang::ParseAST()), make it convenient to put here.
340 ///
341 /// FIXME: Make sure the lifetime of Identifiers/Selectors *isn't* tied to
342 /// the lifetime of the preprocessor.
343 SelectorTable Selectors;
344
345 /// Information about builtins.
346 std::unique_ptr<Builtin::Context> BuiltinInfo;
347
348 /// Tracks all of the pragmas that the client registered
349 /// with this preprocessor.
350 std::unique_ptr<PragmaNamespace> PragmaHandlers;
351
352 /// Pragma handlers of the original source is stored here during the
353 /// parsing of a model file.
354 std::unique_ptr<PragmaNamespace> PragmaHandlersBackup;
355
356 /// Tracks all of the comment handlers that the client registered
357 /// with this preprocessor.
358 std::vector<CommentHandler *> CommentHandlers;
359
360 /// Empty line handler.
361 EmptylineHandler *Emptyline = nullptr;
362
363 /// True to avoid tearing down the lexer etc on EOF
364 bool IncrementalProcessing = false;
365
366public:
367 /// The kind of translation unit we are processing.
369
370 /// Returns a pointer into the given file's buffer that's guaranteed
371 /// to be between tokens. The returned pointer is always before \p Start.
372 /// The maximum distance betweenthe returned pointer and \p Start is
373 /// limited by a constant value, but also an implementation detail.
374 /// If no such check point exists, \c nullptr is returned.
375 const char *getCheckPoint(FileID FID, const char *Start) const;
376
377private:
378 /// The code-completion handler.
379 CodeCompletionHandler *CodeComplete = nullptr;
380
381 /// The file that we're performing code-completion for, if any.
382 const FileEntry *CodeCompletionFile = nullptr;
383
384 /// The offset in file for the code-completion point.
385 unsigned CodeCompletionOffset = 0;
386
387 /// The location for the code-completion point. This gets instantiated
388 /// when the CodeCompletionFile gets \#include'ed for preprocessing.
389 SourceLocation CodeCompletionLoc;
390
391 /// The start location for the file of the code-completion point.
392 ///
393 /// This gets instantiated when the CodeCompletionFile gets \#include'ed
394 /// for preprocessing.
395 SourceLocation CodeCompletionFileLoc;
396
397 /// The source location of the \c import contextual keyword we just
398 /// lexed, if any.
399 SourceLocation ModuleImportLoc;
400
401 /// The source location of the \c module contextual keyword we just
402 /// lexed, if any.
403 SourceLocation ModuleDeclLoc;
404
405 llvm::DenseMap<FileID, SmallVector<const char *>> CheckPoints;
406 unsigned CheckPointCounter = 0;
407
408 /// Whether the import is an `@import` or a standard c++ modules import.
409 bool IsAtImport = false;
410
411 /// Whether the last token we lexed was an '@'.
412 bool LastTokenWasAt = false;
413
414 /// Whether we're importing a standard C++20 named Modules.
415 bool ImportingCXXNamedModules = false;
416
417 /// Whether the last token we lexed was an 'export' keyword.
418 ExportContextualKeywordInfo LastTokenWasExportKeyword;
419
420 /// First pp-token source location in current translation unit.
421 SourceLocation FirstPPTokenLoc;
422
423 /// A preprocessor directive tracer to trace whether the preprocessing
424 /// state changed. These changes would mean most semantically observable
425 /// preprocessor state, particularly anything that is order dependent.
426 NoTrivialPPDirectiveTracer *DirTracer = nullptr;
427
428 /// A position within a C++20 import-seq.
429 class StdCXXImportSeq {
430 public:
431 enum State : int {
432 // Positive values represent a number of unclosed brackets.
433 AtTopLevel = 0,
434 AfterTopLevelTokenSeq = -1,
435 AfterExport = -2,
436 AfterImportSeq = -3,
437 };
438
439 StdCXXImportSeq(State S) : S(S) {}
440
441 /// Saw any kind of open bracket.
442 void handleOpenBracket() {
443 S = static_cast<State>(std::max<int>(S, 0) + 1);
444 }
445 /// Saw any kind of close bracket other than '}'.
446 void handleCloseBracket() {
447 S = static_cast<State>(std::max<int>(S, 1) - 1);
448 }
449 /// Saw a close brace.
450 void handleCloseBrace() {
451 handleCloseBracket();
452 if (S == AtTopLevel && !AfterHeaderName)
453 S = AfterTopLevelTokenSeq;
454 }
455 /// Saw a semicolon.
456 void handleSemi() {
457 if (atTopLevel()) {
458 S = AfterTopLevelTokenSeq;
459 AfterHeaderName = false;
460 }
461 }
462
463 /// Saw an 'export' identifier.
464 void handleExport() {
465 if (S == AfterTopLevelTokenSeq)
466 S = AfterExport;
467 else if (S <= 0)
468 S = AtTopLevel;
469 }
470 /// Saw an 'import' identifier.
471 void handleImport() {
472 if (S == AfterTopLevelTokenSeq || S == AfterExport)
473 S = AfterImportSeq;
474 else if (S <= 0)
475 S = AtTopLevel;
476 }
477
478 /// Saw a 'header-name' token; do not recognize any more 'import' tokens
479 /// until we reach a top-level semicolon.
480 void handleHeaderName() {
481 if (S == AfterImportSeq)
482 AfterHeaderName = true;
483 handleMisc();
484 }
485
486 /// Saw any other token.
487 void handleMisc() {
488 if (S <= 0)
489 S = AtTopLevel;
490 }
491
492 bool atTopLevel() { return S <= 0; }
493 bool afterImportSeq() { return S == AfterImportSeq; }
494 bool afterTopLevelSeq() { return S == AfterTopLevelTokenSeq; }
495
496 private:
497 State S;
498 /// Whether we're in the pp-import-suffix following the header-name in a
499 /// pp-import. If so, a close-brace is not sufficient to end the
500 /// top-level-token-seq of an import-seq.
501 bool AfterHeaderName = false;
502 };
503
504 /// Our current position within a C++20 import-seq.
505 StdCXXImportSeq StdCXXImportSeqState = StdCXXImportSeq::AfterTopLevelTokenSeq;
506
507 /// Track whether we are in a Global Module Fragment
508 class TrackGMF {
509 public:
510 enum GMFState : int {
511 GMFActive = 1,
512 MaybeGMF = 0,
513 BeforeGMFIntroducer = -1,
514 GMFAbsentOrEnded = -2,
515 };
516
517 TrackGMF(GMFState S) : S(S) {}
518
519 /// Saw a semicolon.
520 void handleSemi() {
521 // If it is immediately after the first instance of the module keyword,
522 // then that introduces the GMF.
523 if (S == MaybeGMF)
524 S = GMFActive;
525 }
526
527 /// Saw an 'export' identifier.
528 void handleExport() {
529 // The presence of an 'export' keyword always ends or excludes a GMF.
530 S = GMFAbsentOrEnded;
531 }
532
533 /// Saw an 'import' identifier.
534 void handleImport(bool AfterTopLevelTokenSeq) {
535 // If we see this before any 'module' kw, then we have no GMF.
536 if (AfterTopLevelTokenSeq && S == BeforeGMFIntroducer)
537 S = GMFAbsentOrEnded;
538 }
539
540 /// Saw a 'module' identifier.
541 void handleModule(bool AfterTopLevelTokenSeq) {
542 // This was the first module identifier and not preceded by any token
543 // that would exclude a GMF. It could begin a GMF, but only if directly
544 // followed by a semicolon.
545 if (AfterTopLevelTokenSeq && S == BeforeGMFIntroducer)
546 S = MaybeGMF;
547 else
548 S = GMFAbsentOrEnded;
549 }
550
551 /// Saw any other token.
552 void handleMisc() {
553 // We saw something other than ; after the 'module' kw, so not a GMF.
554 if (S == MaybeGMF)
555 S = GMFAbsentOrEnded;
556 }
557
558 bool inGMF() { return S == GMFActive; }
559
560 private:
561 /// Track the transitions into and out of a Global Module Fragment,
562 /// if one is present.
563 GMFState S;
564 };
565
566 TrackGMF TrackGMFState = TrackGMF::BeforeGMFIntroducer;
567
568 /// Track the status of the c++20 module decl.
569 ///
570 /// module-declaration:
571 /// 'export'[opt] 'module' module-name module-partition[opt]
572 /// attribute-specifier-seq[opt] ';'
573 ///
574 /// module-name:
575 /// module-name-qualifier[opt] identifier
576 ///
577 /// module-partition:
578 /// ':' module-name-qualifier[opt] identifier
579 ///
580 /// module-name-qualifier:
581 /// identifier '.'
582 /// module-name-qualifier identifier '.'
583 ///
584 /// Transition state:
585 ///
586 /// NotAModuleDecl --- export ---> FoundExport
587 /// NotAModuleDecl --- module ---> ImplementationCandidate
588 /// FoundExport --- module ---> InterfaceCandidate
589 /// ImplementationCandidate --- Identifier ---> ImplementationCandidate
590 /// ImplementationCandidate --- period ---> ImplementationCandidate
591 /// ImplementationCandidate --- colon ---> ImplementationCandidate
592 /// InterfaceCandidate --- Identifier ---> InterfaceCandidate
593 /// InterfaceCandidate --- period ---> InterfaceCandidate
594 /// InterfaceCandidate --- colon ---> InterfaceCandidate
595 /// ImplementationCandidate --- Semi ---> NamedModuleImplementation
596 /// NamedModuleInterface --- Semi ---> NamedModuleInterface
597 /// NamedModuleImplementation --- Anything ---> NamedModuleImplementation
598 /// NamedModuleInterface --- Anything ---> NamedModuleInterface
599 ///
600 /// FIXME: We haven't handle attribute-specifier-seq here. It may not be bad
601 /// soon since we don't support any module attributes yet.
602 class ModuleDeclSeq {
603 enum ModuleDeclState : int {
604 NotAModuleDecl,
605 FoundExport,
606 InterfaceCandidate,
607 ImplementationCandidate,
608 NamedModuleInterface,
609 NamedModuleImplementation,
610 };
611
612 public:
613 ModuleDeclSeq() = default;
614
615 void handleExport() {
616 if (State == NotAModuleDecl)
617 State = FoundExport;
618 else if (!isNamedModule())
619 reset();
620 }
621
622 void handleModule() {
623 if (State == FoundExport)
624 State = InterfaceCandidate;
625 else if (State == NotAModuleDecl)
626 State = ImplementationCandidate;
627 else if (!isNamedModule())
628 reset();
629 }
630
631 void handleModuleName(ModuleNameLoc *NameLoc) {
632 if (isModuleCandidate() && NameLoc)
633 Name += NameLoc->str();
634 else if (!isNamedModule())
635 reset();
636 }
637
638 void handleColon() {
639 if (isModuleCandidate())
640 Name += ":";
641 else if (!isNamedModule())
642 reset();
643 }
644
645 void handleSemi() {
646 if (!Name.empty() && isModuleCandidate()) {
647 if (State == InterfaceCandidate)
648 State = NamedModuleInterface;
649 else if (State == ImplementationCandidate)
650 State = NamedModuleImplementation;
651 else
652 llvm_unreachable("Unimaged ModuleDeclState.");
653 } else if (!isNamedModule())
654 reset();
655 }
656
657 void handleMisc() {
658 if (!isNamedModule())
659 reset();
660 }
661
662 bool isModuleCandidate() const {
663 return State == InterfaceCandidate || State == ImplementationCandidate;
664 }
665
666 bool isNamedModule() const {
667 return State == NamedModuleInterface ||
668 State == NamedModuleImplementation;
669 }
670
671 bool isNamedInterface() const { return State == NamedModuleInterface; }
672
673 bool isImplementationUnit() const {
674 return State == NamedModuleImplementation && !getName().contains(':');
675 }
676
677 bool isNotAModuleDecl() const { return State == NotAModuleDecl; }
678
679 StringRef getName() const {
680 assert(isNamedModule() && "Can't get name from a non named module");
681 return Name;
682 }
683
684 StringRef getPrimaryName() const {
685 assert(isNamedModule() && "Can't get name from a non named module");
686 return getName().split(':').first;
687 }
688
689 void reset() {
690 Name.clear();
691 State = NotAModuleDecl;
692 }
693
694 private:
695 ModuleDeclState State = NotAModuleDecl;
696 std::string Name;
697 };
698
699 ModuleDeclSeq ModuleDeclState;
700
701 /// The identifier and source location of the currently-active
702 /// \#pragma clang arc_cf_code_audited begin.
703 IdentifierLoc PragmaARCCFCodeAuditedInfo;
704
705 /// The source location of the currently-active
706 /// \#pragma clang assume_nonnull begin.
707 SourceLocation PragmaAssumeNonNullLoc;
708
709 /// Set only for preambles which end with an active
710 /// \#pragma clang assume_nonnull begin.
711 ///
712 /// When the preamble is loaded into the main file,
713 /// `PragmaAssumeNonNullLoc` will be set to this to
714 /// replay the unterminated assume_nonnull.
715 SourceLocation PreambleRecordedPragmaAssumeNonNullLoc;
716
717 /// True if we hit the code-completion point.
718 bool CodeCompletionReached = false;
719
720 /// The code completion token containing the information
721 /// on the stem that is to be code completed.
722 IdentifierInfo *CodeCompletionII = nullptr;
723
724 /// Range for the code completion token.
725 SourceRange CodeCompletionTokenRange;
726
727 /// The directory that the main file should be considered to occupy,
728 /// if it does not correspond to a real file (as happens when building a
729 /// module).
730 OptionalDirectoryEntryRef MainFileDir;
731
732 /// The number of bytes that we will initially skip when entering the
733 /// main file, along with a flag that indicates whether skipping this number
734 /// of bytes will place the lexer at the start of a line.
735 ///
736 /// This is used when loading a precompiled preamble.
737 std::pair<int, bool> SkipMainFilePreamble;
738
739 /// Whether we hit an error due to reaching max allowed include depth. Allows
740 /// to avoid hitting the same error over and over again.
741 bool HasReachedMaxIncludeDepth = false;
742
743 /// The number of currently-active calls to Lex.
744 ///
745 /// Lex is reentrant, and asking for an (end-of-phase-4) token can often
746 /// require asking for multiple additional tokens. This counter makes it
747 /// possible for Lex to detect whether it's producing a token for the end
748 /// of phase 4 of translation or for some other situation.
749 unsigned LexLevel = 0;
750
751 /// The number of (LexLevel 0) preprocessor tokens.
752 unsigned TokenCount = 0;
753
754 /// Preprocess every token regardless of LexLevel.
755 bool PreprocessToken = false;
756
757 /// The maximum number of (LexLevel 0) tokens before issuing a -Wmax-tokens
758 /// warning, or zero for unlimited.
759 unsigned MaxTokens = 0;
760 SourceLocation MaxTokensOverrideLoc;
761
762public:
777
778 using IncludedFilesSet = llvm::DenseSet<const FileEntry *>;
779
780private:
781 friend class ASTReader;
782 friend class MacroArgs;
783
784 class PreambleConditionalStackStore {
785 enum State {
786 Off = 0,
787 Recording = 1,
788 Replaying = 2,
789 };
790
791 public:
792 PreambleConditionalStackStore() = default;
793
794 void startRecording() { ConditionalStackState = Recording; }
795 void startReplaying() { ConditionalStackState = Replaying; }
796 bool isRecording() const { return ConditionalStackState == Recording; }
797 bool isReplaying() const { return ConditionalStackState == Replaying; }
798
799 ArrayRef<PPConditionalInfo> getStack() const {
800 return ConditionalStack;
801 }
802
803 void doneReplaying() {
804 ConditionalStack.clear();
805 ConditionalStackState = Off;
806 }
807
808 void setStack(ArrayRef<PPConditionalInfo> s) {
809 if (!isRecording() && !isReplaying())
810 return;
811 ConditionalStack.clear();
812 ConditionalStack.append(s.begin(), s.end());
813 }
814
815 bool hasRecordedPreamble() const { return !ConditionalStack.empty(); }
816
817 bool reachedEOFWhileSkipping() const { return SkipInfo.has_value(); }
818
819 void clearSkipInfo() { SkipInfo.reset(); }
820
821 std::optional<PreambleSkipInfo> SkipInfo;
822
823 private:
824 SmallVector<PPConditionalInfo, 4> ConditionalStack;
825 State ConditionalStackState = Off;
826 } PreambleConditionalStack;
827
828 /// The current top of the stack that we're lexing from if
829 /// not expanding a macro and we are lexing directly from source code.
830 ///
831 /// Only one of CurLexer, or CurTokenLexer will be non-null.
832 std::unique_ptr<Lexer> CurLexer;
833
834 /// Lexers that are pending destruction, deferred until the current
835 /// Stack of Lexer unwinds completely (LexLevel returns to 0).
836 /// This avoids use-after-free when HandleEndOfFile is called from
837 /// within a Lexer method that still needs to access its members.
838 SmallVector<std::unique_ptr<Lexer>, 2> PendingDestroyLexers;
839
840 /// The current top of the stack that we're lexing from
841 /// if not expanding a macro.
842 ///
843 /// This is an alias for CurLexer.
844 PreprocessorLexer *CurPPLexer = nullptr;
845
846 /// Used to find the current FileEntry, if CurLexer is non-null
847 /// and if applicable.
848 ///
849 /// This allows us to implement \#include_next and find directory-specific
850 /// properties.
851 ConstSearchDirIterator CurDirLookup = nullptr;
852
853 /// The current macro we are expanding, if we are expanding a macro.
854 ///
855 /// One of CurLexer and CurTokenLexer must be null.
856 std::unique_ptr<TokenLexer> CurTokenLexer;
857
858 /// The kind of lexer we're currently working with.
859 typedef bool (*LexerCallback)(Preprocessor &, Token &);
860 LexerCallback CurLexerCallback = &CLK_Lexer;
861
862 /// If the current lexer is for a submodule that is being built, this
863 /// is that submodule.
864 Module *CurLexerSubmodule = nullptr;
865
866 /// Keeps track of the stack of files currently
867 /// \#included, and macros currently being expanded from, not counting
868 /// CurLexer/CurTokenLexer.
869 struct IncludeStackInfo {
870 LexerCallback CurLexerCallback;
871 Module *TheSubmodule;
872 std::unique_ptr<Lexer> TheLexer;
873 PreprocessorLexer *ThePPLexer;
874 std::unique_ptr<TokenLexer> TheTokenLexer;
875 ConstSearchDirIterator TheDirLookup;
876
877 // The following constructors are completely useless copies of the default
878 // versions, only needed to pacify MSVC.
879 IncludeStackInfo(LexerCallback CurLexerCallback, Module *TheSubmodule,
880 std::unique_ptr<Lexer> &&TheLexer,
881 PreprocessorLexer *ThePPLexer,
882 std::unique_ptr<TokenLexer> &&TheTokenLexer,
883 ConstSearchDirIterator TheDirLookup)
884 : CurLexerCallback(std::move(CurLexerCallback)),
885 TheSubmodule(std::move(TheSubmodule)), TheLexer(std::move(TheLexer)),
886 ThePPLexer(std::move(ThePPLexer)),
887 TheTokenLexer(std::move(TheTokenLexer)),
888 TheDirLookup(std::move(TheDirLookup)) {}
889 };
890 std::vector<IncludeStackInfo> IncludeMacroStack;
891
892 /// Actions invoked when some preprocessor activity is
893 /// encountered (e.g. a file is \#included, etc).
894 std::unique_ptr<PPCallbacks> Callbacks;
895
896 struct MacroExpandsInfo {
897 Token Tok;
898 MacroDefinition MD;
899 SourceRange Range;
900
901 MacroExpandsInfo(Token Tok, MacroDefinition MD, SourceRange Range)
902 : Tok(Tok), MD(MD), Range(Range) {}
903 };
904 SmallVector<MacroExpandsInfo, 2> DelayedMacroExpandsCallbacks;
905
906 /// Information about a name that has been used to define a module macro.
907 struct ModuleMacroInfo {
908 /// The most recent macro directive for this identifier.
909 MacroDirective *MD;
910
911 /// The active module macros for this identifier.
912 llvm::TinyPtrVector<ModuleMacro *> ActiveModuleMacros;
913
914 /// The generation number at which we last updated ActiveModuleMacros.
915 /// \see Preprocessor::VisibleModules.
916 unsigned ActiveModuleMacrosGeneration = 0;
917
918 /// Whether this macro name is ambiguous.
919 bool IsAmbiguous = false;
920
921 /// The module macros that are overridden by this macro.
922 llvm::TinyPtrVector<ModuleMacro *> OverriddenMacros;
923
924 ModuleMacroInfo(MacroDirective *MD) : MD(MD) {}
925 };
926
927 /// The state of a macro for an identifier.
928 class MacroState {
929 mutable llvm::PointerUnion<MacroDirective *, ModuleMacroInfo *> State;
930
931 ModuleMacroInfo *getModuleInfo(Preprocessor &PP,
932 const IdentifierInfo *II) const {
933 if (II->isOutOfDate())
934 PP.updateOutOfDateIdentifier(*II);
935 // FIXME: Find a spare bit on IdentifierInfo and store a
936 // HasModuleMacros flag.
937 if (!II->hasMacroDefinition() ||
938 (!PP.getLangOpts().Modules &&
939 !PP.getLangOpts().ModulesLocalVisibility) ||
940 !PP.CurSubmoduleState->VisibleModules.getGeneration())
941 return nullptr;
942
943 auto *Info = dyn_cast_if_present<ModuleMacroInfo *>(State);
944 if (!Info) {
945 Info = new (PP.getPreprocessorAllocator())
946 ModuleMacroInfo(cast<MacroDirective *>(State));
947 State = Info;
948 }
949
950 if (PP.CurSubmoduleState->VisibleModules.getGeneration() !=
951 Info->ActiveModuleMacrosGeneration)
952 PP.updateModuleMacroInfo(II, *Info);
953 return Info;
954 }
955
956 public:
957 MacroState() : MacroState(nullptr) {}
958 MacroState(MacroDirective *MD) : State(MD) {}
959
960 MacroState(MacroState &&O) noexcept : State(O.State) {
961 O.State = (MacroDirective *)nullptr;
962 }
963
964 MacroState &operator=(MacroState &&O) noexcept {
965 auto S = O.State;
966 O.State = (MacroDirective *)nullptr;
967 State = S;
968 return *this;
969 }
970
971 ~MacroState() {
972 if (auto *Info = dyn_cast_if_present<ModuleMacroInfo *>(State))
973 Info->~ModuleMacroInfo();
974 }
975
976 MacroDirective *getLatest() const {
977 if (auto *Info = dyn_cast_if_present<ModuleMacroInfo *>(State))
978 return Info->MD;
979 return cast<MacroDirective *>(State);
980 }
981
982 void setLatest(MacroDirective *MD) {
983 if (auto *Info = dyn_cast_if_present<ModuleMacroInfo *>(State))
984 Info->MD = MD;
985 else
986 State = MD;
987 }
988
989 bool isAmbiguous(Preprocessor &PP, const IdentifierInfo *II) const {
990 auto *Info = getModuleInfo(PP, II);
991 return Info ? Info->IsAmbiguous : false;
992 }
993
994 ArrayRef<ModuleMacro *>
995 getActiveModuleMacros(Preprocessor &PP, const IdentifierInfo *II) const {
996 if (auto *Info = getModuleInfo(PP, II))
997 return Info->ActiveModuleMacros;
998 return {};
999 }
1000
1001 MacroDirective::DefInfo findDirectiveAtLoc(SourceLocation Loc,
1002 SourceManager &SourceMgr) const {
1003 // FIXME: Incorporate module macros into the result of this.
1004 if (auto *Latest = getLatest())
1005 return Latest->findDirectiveAtLoc(Loc, SourceMgr);
1006 return {};
1007 }
1008
1009 void overrideActiveModuleMacros(Preprocessor &PP, IdentifierInfo *II) {
1010 if (auto *Info = getModuleInfo(PP, II)) {
1011 Info->OverriddenMacros.insert(Info->OverriddenMacros.end(),
1012 Info->ActiveModuleMacros.begin(),
1013 Info->ActiveModuleMacros.end());
1014 Info->ActiveModuleMacros.clear();
1015 Info->IsAmbiguous = false;
1016 }
1017 }
1018
1019 ArrayRef<ModuleMacro*> getOverriddenMacros() const {
1020 if (auto *Info = dyn_cast_if_present<ModuleMacroInfo *>(State))
1021 return Info->OverriddenMacros;
1022 return {};
1023 }
1024
1025 void setOverriddenMacros(Preprocessor &PP,
1026 ArrayRef<ModuleMacro *> Overrides) {
1027 auto *Info = dyn_cast_if_present<ModuleMacroInfo *>(State);
1028 if (!Info) {
1029 if (Overrides.empty())
1030 return;
1031 Info = new (PP.getPreprocessorAllocator())
1032 ModuleMacroInfo(cast<MacroDirective *>(State));
1033 State = Info;
1034 }
1035 Info->OverriddenMacros.clear();
1036 Info->OverriddenMacros.insert(Info->OverriddenMacros.end(),
1037 Overrides.begin(), Overrides.end());
1038 Info->ActiveModuleMacrosGeneration = 0;
1039 }
1040 };
1041
1042 /// For each IdentifierInfo that was associated with a macro, we
1043 /// keep a mapping to the history of all macro definitions and #undefs in
1044 /// the reverse order (the latest one is in the head of the list).
1045 ///
1046 /// This mapping lives within the \p CurSubmoduleState.
1047 using MacroMap = llvm::DenseMap<const IdentifierInfo *, MacroState>;
1048
1049 struct SubmoduleState;
1050
1051 /// Information about a submodule that we're currently building.
1052 struct BuildingSubmoduleInfo {
1053 /// The module that we are building.
1054 Module *M;
1055
1056 /// The location at which the module was included.
1057 SourceLocation ImportLoc;
1058
1059 /// Whether we entered this submodule via a pragma.
1060 bool IsPragma;
1061
1062 /// The previous SubmoduleState.
1063 SubmoduleState *OuterSubmoduleState;
1064
1065 /// The number of pending module macro names when we started building this.
1066 unsigned OuterPendingModuleMacroNames;
1067
1068 BuildingSubmoduleInfo(Module *M, SourceLocation ImportLoc, bool IsPragma,
1069 SubmoduleState *OuterSubmoduleState,
1070 unsigned OuterPendingModuleMacroNames)
1071 : M(M), ImportLoc(ImportLoc), IsPragma(IsPragma),
1072 OuterSubmoduleState(OuterSubmoduleState),
1073 OuterPendingModuleMacroNames(OuterPendingModuleMacroNames) {}
1074 };
1075 SmallVector<BuildingSubmoduleInfo, 8> BuildingSubmoduleStack;
1076
1077 /// Information about a submodule's preprocessor state.
1078 struct SubmoduleState {
1079 /// The macros for the submodule.
1080 MacroMap Macros;
1081
1082 /// The set of modules that are visible within the submodule.
1083 VisibleModuleSet VisibleModules;
1084
1085 // FIXME: CounterValue?
1086 // FIXME: PragmaPushMacroInfo?
1087 };
1088 std::map<Module *, SubmoduleState> Submodules;
1089
1090 /// The preprocessor state for preprocessing outside of any submodule.
1091 SubmoduleState NullSubmoduleState;
1092
1093 /// The current submodule state. Will be \p NullSubmoduleState if we're not
1094 /// in a submodule.
1095 SubmoduleState *CurSubmoduleState;
1096
1097 /// The files that have been included.
1098 IncludedFilesSet IncludedFiles;
1099
1100 /// The set of top-level modules that affected preprocessing, but were not
1101 /// imported.
1102 llvm::SmallSetVector<Module *, 2> AffectingClangModules;
1103
1104 /// The set of known macros exported from modules.
1105 llvm::FoldingSet<ModuleMacro> ModuleMacros;
1106
1107 /// The names of potential module macros that we've not yet processed.
1108 llvm::SmallVector<IdentifierInfo *, 32> PendingModuleMacroNames;
1109
1110 /// The list of module macros, for each identifier, that are not overridden by
1111 /// any other module macro.
1112 llvm::DenseMap<const IdentifierInfo *, llvm::TinyPtrVector<ModuleMacro *>>
1113 LeafModuleMacros;
1114
1115 /// Macros that we want to warn because they are not used at the end
1116 /// of the translation unit.
1117 ///
1118 /// We store just their SourceLocations instead of
1119 /// something like MacroInfo*. The benefit of this is that when we are
1120 /// deserializing from PCH, we don't need to deserialize identifier & macros
1121 /// just so that we can report that they are unused, we just warn using
1122 /// the SourceLocations of this set (that will be filled by the ASTReader).
1123 using WarnUnusedMacroLocsTy = llvm::SmallDenseSet<SourceLocation, 32>;
1124 WarnUnusedMacroLocsTy WarnUnusedMacroLocs;
1125
1126 /// This is a pair of an optional message and source location used for pragmas
1127 /// that annotate macros like pragma clang restrict_expansion and pragma clang
1128 /// deprecated. This pair stores the optional message and the location of the
1129 /// annotation pragma for use producing diagnostics and notes.
1130 using MsgLocationPair = std::pair<std::string, SourceLocation>;
1131
1132 struct MacroAnnotationInfo {
1133 SourceLocation Location;
1134 std::string Message;
1135 };
1136
1137 struct MacroAnnotations {
1138 std::optional<MacroAnnotationInfo> DeprecationInfo;
1139 std::optional<MacroAnnotationInfo> RestrictExpansionInfo;
1140 std::optional<SourceLocation> FinalAnnotationLoc;
1141 };
1142
1143 /// Warning information for macro annotations.
1144 llvm::DenseMap<const IdentifierInfo *, MacroAnnotations> AnnotationInfos;
1145
1146 /// A "freelist" of MacroArg objects that can be
1147 /// reused for quick allocation.
1148 MacroArgs *MacroArgCache = nullptr;
1149
1150 /// For each IdentifierInfo used in a \#pragma push_macro directive,
1151 /// we keep a MacroInfo stack used to restore the previous macro value.
1152 llvm::DenseMap<IdentifierInfo *, std::vector<MacroInfo *>>
1153 PragmaPushMacroInfo;
1154
1155 // Various statistics we track for performance analysis.
1156 unsigned NumDirectives = 0;
1157 unsigned NumDefined = 0;
1158 unsigned NumUndefined = 0;
1159 unsigned NumPragma = 0;
1160 unsigned NumIf = 0;
1161 unsigned NumElse = 0;
1162 unsigned NumEndif = 0;
1163 unsigned NumEnteredSourceFiles = 0;
1164 unsigned MaxIncludeStackDepth = 0;
1165 unsigned NumMacroExpanded = 0;
1166 unsigned NumFnMacroExpanded = 0;
1167 unsigned NumBuiltinMacroExpanded = 0;
1168 unsigned NumFastMacroExpanded = 0;
1169 unsigned NumTokenPaste = 0;
1170 unsigned NumFastTokenPaste = 0;
1171 unsigned NumSkipped = 0;
1172
1173 /// The predefined macros that preprocessor should use from the
1174 /// command line etc.
1175 std::string Predefines;
1176
1177 /// The file ID for the preprocessor predefines.
1178 FileID PredefinesFileID;
1179
1180 /// The file ID for the PCH through header.
1181 FileID PCHThroughHeaderFileID;
1182
1183 /// Whether tokens are being skipped until a #pragma hdrstop is seen.
1184 bool SkippingUntilPragmaHdrStop = false;
1185
1186 /// Whether tokens are being skipped until the through header is seen.
1187 bool SkippingUntilPCHThroughHeader = false;
1188
1189 /// Whether the main file is preprocessed module file.
1190 bool MainFileIsPreprocessedModuleFile = false;
1191
1192 /// \{
1193 /// Cache of macro expanders to reduce malloc traffic.
1194 enum { TokenLexerCacheSize = 8 };
1195 unsigned NumCachedTokenLexers;
1196 std::unique_ptr<TokenLexer> TokenLexerCache[TokenLexerCacheSize];
1197 /// \}
1198
1199 /// Keeps macro expanded tokens for TokenLexers.
1200 //
1201 /// Works like a stack; a TokenLexer adds the macro expanded tokens that is
1202 /// going to lex in the cache and when it finishes the tokens are removed
1203 /// from the end of the cache.
1204 SmallVector<Token, 16> MacroExpandedTokens;
1205 std::vector<std::pair<TokenLexer *, size_t>> MacroExpandingLexersStack;
1206
1207 /// A record of the macro definitions and expansions that
1208 /// occurred during preprocessing.
1209 ///
1210 /// This is an optional side structure that can be enabled with
1211 /// \c createPreprocessingRecord() prior to preprocessing.
1212 PreprocessingRecord *Record = nullptr;
1213
1214 /// Cached tokens state.
1215 using CachedTokensTy = SmallVector<Token, 1>;
1216
1217 /// Cached tokens are stored here when we do backtracking or
1218 /// lookahead. They are "lexed" by the CachingLex() method.
1219 CachedTokensTy CachedTokens;
1220
1221 /// The position of the cached token that CachingLex() should
1222 /// "lex" next.
1223 ///
1224 /// If it points beyond the CachedTokens vector, it means that a normal
1225 /// Lex() should be invoked.
1226 CachedTokensTy::size_type CachedLexPos = 0;
1227
1228 /// Stack of backtrack positions, allowing nested backtracks.
1229 ///
1230 /// The EnableBacktrackAtThisPos() method pushes a position to
1231 /// indicate where CachedLexPos should be set when the BackTrack() method is
1232 /// invoked (at which point the last position is popped).
1233 std::vector<CachedTokensTy::size_type> BacktrackPositions;
1234
1235 /// Stack of cached tokens/initial number of cached tokens pairs, allowing
1236 /// nested unannotated backtracks.
1237 std::vector<std::pair<CachedTokensTy, CachedTokensTy::size_type>>
1238 UnannotatedBacktrackTokens;
1239
1240 /// True if \p Preprocessor::SkipExcludedConditionalBlock() is running.
1241 /// This is used to guard against calling this function recursively.
1242 ///
1243 /// See comments at the use-site for more context about why it is needed.
1244 bool SkippingExcludedConditionalBlock = false;
1245
1246 /// Keeps track of skipped range mappings that were recorded while skipping
1247 /// excluded conditional directives. It maps the source buffer pointer at
1248 /// the beginning of a skipped block, to the number of bytes that should be
1249 /// skipped.
1250 llvm::DenseMap<const char *, unsigned> RecordedSkippedRanges;
1251
1252 void updateOutOfDateIdentifier(const IdentifierInfo &II) const;
1253
1254public:
1255 Preprocessor(const PreprocessorOptions &PPOpts, DiagnosticsEngine &diags,
1256 const LangOptions &LangOpts, SourceManager &SM,
1257 HeaderSearch &Headers, ModuleLoader &TheModuleLoader,
1258 IdentifierInfoLookup *IILookup = nullptr,
1259 bool OwnsHeaderSearch = false,
1261
1262 ~Preprocessor();
1263
1264 /// Initialize the preprocessor using information about the target.
1265 ///
1266 /// \param Target is owned by the caller and must remain valid for the
1267 /// lifetime of the preprocessor.
1268 /// \param AuxTarget is owned by the caller and must remain valid for
1269 /// the lifetime of the preprocessor.
1270 void Initialize(const TargetInfo &Target,
1271 const TargetInfo *AuxTarget = nullptr);
1272
1273 /// Initialize the preprocessor to parse a model file
1274 ///
1275 /// To parse model files the preprocessor of the original source is reused to
1276 /// preserver the identifier table. However to avoid some duplicate
1277 /// information in the preprocessor some cleanup is needed before it is used
1278 /// to parse model files. This method does that cleanup.
1280
1281 /// Cleanup after model file parsing
1282 void FinalizeForModelFile();
1283
1284 /// Retrieve the preprocessor options used to initialize this preprocessor.
1285 const PreprocessorOptions &getPreprocessorOpts() const { return PPOpts; }
1286
1287 DiagnosticsEngine &getDiagnostics() const { return *Diags; }
1288 void setDiagnostics(DiagnosticsEngine &D) { Diags = &D; }
1289
1290 const LangOptions &getLangOpts() const { return LangOpts; }
1291 const TargetInfo &getTargetInfo() const { return *Target; }
1292 const TargetInfo *getAuxTargetInfo() const { return AuxTarget; }
1293 FileManager &getFileManager() const { return FileMgr; }
1294 SourceManager &getSourceManager() const { return SourceMgr; }
1295 HeaderSearch &getHeaderSearchInfo() const { return HeaderInfo; }
1296
1297 IdentifierTable &getIdentifierTable() { return Identifiers; }
1298 const IdentifierTable &getIdentifierTable() const { return Identifiers; }
1299 SelectorTable &getSelectorTable() { return Selectors; }
1300 Builtin::Context &getBuiltinInfo() { return *BuiltinInfo; }
1301 llvm::BumpPtrAllocator &getPreprocessorAllocator() { return BP; }
1302
1304 ExternalSource = Source;
1305 }
1306
1308 return ExternalSource;
1309 }
1310
1311 /// Retrieve the module loader associated with this preprocessor.
1312 ModuleLoader &getModuleLoader() const { return TheModuleLoader; }
1313
1315 return TheModuleLoader.HadFatalFailure;
1316 }
1317
1318 /// Retrieve the number of Directives that have been processed by the
1319 /// Preprocessor.
1320 unsigned getNumDirectives() const {
1321 return NumDirectives;
1322 }
1323
1324 /// True if we are currently preprocessing a #if or #elif directive
1326 return ParsingIfOrElifDirective;
1327 }
1328
1329 /// Control whether the preprocessor retains comments in output.
1330 void SetCommentRetentionState(bool KeepComments, bool KeepMacroComments) {
1331 this->KeepComments = KeepComments | KeepMacroComments;
1332 this->KeepMacroComments = KeepMacroComments;
1333 }
1334
1335 bool getCommentRetentionState() const { return KeepComments; }
1336
1337 void setPragmasEnabled(bool Enabled) { PragmasEnabled = Enabled; }
1338 bool getPragmasEnabled() const { return PragmasEnabled; }
1339
1341 SuppressIncludeNotFoundError = Suppress;
1342 }
1343
1345 return SuppressIncludeNotFoundError;
1346 }
1347
1348 /// Sets whether the preprocessor is responsible for producing output or if
1349 /// it is producing tokens to be consumed by Parse and Sema.
1350 void setPreprocessedOutput(bool IsPreprocessedOutput) {
1351 PreprocessedOutput = IsPreprocessedOutput;
1352 }
1353
1354 /// Returns true if the preprocessor is responsible for generating output,
1355 /// false if it is producing tokens to be consumed by Parse and Sema.
1356 bool isPreprocessedOutput() const { return PreprocessedOutput; }
1357
1358 /// Return true if we are lexing directly from the specified lexer.
1359 bool isCurrentLexer(const PreprocessorLexer *L) const {
1360 return CurPPLexer == L;
1361 }
1362
1363 /// Return the current lexer being lexed from.
1364 ///
1365 /// Note that this ignores any potentially active macro expansions and _Pragma
1366 /// expansions going on at the time.
1367 PreprocessorLexer *getCurrentLexer() const { return CurPPLexer; }
1368
1369 /// Return the current file lexer being lexed from.
1370 ///
1371 /// Note that this ignores any potentially active macro expansions and _Pragma
1372 /// expansions going on at the time.
1374
1375 /// Return the submodule owning the file being lexed. This may not be
1376 /// the current module if we have changed modules since entering the file.
1377 Module *getCurrentLexerSubmodule() const { return CurLexerSubmodule; }
1378
1379 /// Returns the FileID for the preprocessor predefines.
1380 FileID getPredefinesFileID() const { return PredefinesFileID; }
1381
1382 /// \{
1383 /// Accessors for preprocessor callbacks.
1384 ///
1385 /// Note that this class takes ownership of any PPCallbacks object given to
1386 /// it.
1387 PPCallbacks *getPPCallbacks() const { return Callbacks.get(); }
1388 void addPPCallbacks(std::unique_ptr<PPCallbacks> C) {
1389 if (Callbacks)
1390 C = std::make_unique<PPChainedCallbacks>(std::move(C),
1391 std::move(Callbacks));
1392 Callbacks = std::move(C);
1393 }
1394 void removePPCallbacks() { Callbacks.reset(); }
1395 /// \}
1396
1397 /// Get the number of tokens processed so far.
1398 unsigned getTokenCount() const { return TokenCount; }
1399
1400 /// Get the max number of tokens before issuing a -Wmax-tokens warning.
1401 unsigned getMaxTokens() const { return MaxTokens; }
1402
1404 MaxTokens = Value;
1405 MaxTokensOverrideLoc = Loc;
1406 };
1407
1408 SourceLocation getMaxTokensOverrideLoc() const { return MaxTokensOverrideLoc; }
1409
1410 /// Register a function that would be called on each token in the final
1411 /// expanded token stream.
1412 /// This also reports annotation tokens produced by the parser.
1413 void setTokenWatcher(llvm::unique_function<void(const clang::Token &)> F) {
1414 OnToken = std::move(F);
1415 }
1416
1418 GetDependencyDirectives = &Get;
1419 }
1420
1421 void setPreprocessToken(bool Preprocess) { PreprocessToken = Preprocess; }
1422
1423 bool isMacroDefined(StringRef Id) {
1424 return isMacroDefined(&Identifiers.get(Id));
1425 }
1427 return II->hasMacroDefinition() &&
1428 (!getLangOpts().Modules || (bool)getMacroDefinition(II));
1429 }
1430
1431 /// Determine whether II is defined as a macro within the module M,
1432 /// if that is a module that we've already preprocessed. Does not check for
1433 /// macros imported into M.
1435 if (!II->hasMacroDefinition())
1436 return false;
1437 auto I = Submodules.find(M);
1438 if (I == Submodules.end())
1439 return false;
1440 auto J = I->second.Macros.find(II);
1441 if (J == I->second.Macros.end())
1442 return false;
1443 auto *MD = J->second.getLatest();
1444 return MD && MD->isDefined();
1445 }
1446
1448 if (!II->hasMacroDefinition())
1449 return {};
1450
1451 MacroState &S = CurSubmoduleState->Macros[II];
1452 auto *MD = S.getLatest();
1453 while (isa_and_nonnull<VisibilityMacroDirective>(MD))
1454 MD = MD->getPrevious();
1455 return MacroDefinition(dyn_cast_or_null<DefMacroDirective>(MD),
1456 S.getActiveModuleMacros(*this, II),
1457 S.isAmbiguous(*this, II));
1458 }
1459
1461 SourceLocation Loc) {
1462 if (!II->hadMacroDefinition())
1463 return {};
1464
1465 MacroState &S = CurSubmoduleState->Macros[II];
1467 if (auto *MD = S.getLatest())
1468 DI = MD->findDirectiveAtLoc(Loc, getSourceManager());
1469 // FIXME: Compute the set of active module macros at the specified location.
1470 return MacroDefinition(DI.getDirective(),
1471 S.getActiveModuleMacros(*this, II),
1472 S.isAmbiguous(*this, II));
1473 }
1474
1475 /// Given an identifier, return its latest non-imported MacroDirective
1476 /// if it is \#define'd and not \#undef'd, or null if it isn't \#define'd.
1478 if (!II->hasMacroDefinition())
1479 return nullptr;
1480
1481 auto *MD = getLocalMacroDirectiveHistory(II);
1482 if (!MD || MD->getDefinition().isUndefined())
1483 return nullptr;
1484
1485 return MD;
1486 }
1487
1488 const MacroInfo *getMacroInfo(const IdentifierInfo *II) const {
1489 return const_cast<Preprocessor*>(this)->getMacroInfo(II);
1490 }
1491
1493 if (!II->hasMacroDefinition())
1494 return nullptr;
1495 if (auto MD = getMacroDefinition(II))
1496 return MD.getMacroInfo();
1497 return nullptr;
1498 }
1499
1500 /// Given an identifier, return the latest non-imported macro
1501 /// directive for that identifier.
1502 ///
1503 /// One can iterate over all previous macro directives from the most recent
1504 /// one.
1506
1507 /// Add a directive to the macro directive history for this identifier.
1510 SourceLocation Loc) {
1511 DefMacroDirective *MD = AllocateDefMacroDirective(MI, Loc);
1512 appendMacroDirective(II, MD);
1513 return MD;
1514 }
1519
1520 /// Set a MacroDirective that was loaded from a PCH file.
1522 MacroDirective *MD);
1523
1524 /// Register an exported macro for a module and identifier.
1527 ArrayRef<ModuleMacro *> Overrides, bool &IsNew);
1529
1530 /// Get the list of leaf (non-overridden) module macros for a name.
1532 if (II->isOutOfDate())
1533 updateOutOfDateIdentifier(*II);
1534 auto I = LeafModuleMacros.find(II);
1535 if (I != LeafModuleMacros.end())
1536 return I->second;
1537 return {};
1538 }
1539
1540 /// Get the list of submodules that we're currently building.
1542 return BuildingSubmoduleStack;
1543 }
1544
1545 /// \{
1546 /// Iterators for the macro history table. Currently defined macros have
1547 /// IdentifierInfo::hasMacroDefinition() set and an empty
1548 /// MacroInfo::getUndefLoc() at the head of the list.
1549 using macro_iterator = MacroMap::const_iterator;
1550
1551 macro_iterator macro_begin(bool IncludeExternalMacros = true) const;
1552 macro_iterator macro_end(bool IncludeExternalMacros = true) const;
1553
1554 llvm::iterator_range<macro_iterator>
1555 macros(bool IncludeExternalMacros = true) const {
1556 macro_iterator begin = macro_begin(IncludeExternalMacros);
1557 macro_iterator end = macro_end(IncludeExternalMacros);
1558 return llvm::make_range(begin, end);
1559 }
1560
1561 /// \}
1562
1563 /// Mark the given clang module as affecting the current clang module or translation unit.
1565 assert(M->isModuleMapModule());
1566 if (!BuildingSubmoduleStack.empty()) {
1567 if (M != BuildingSubmoduleStack.back().M)
1568 BuildingSubmoduleStack.back().M->AffectingClangModules.insert(M);
1569 } else {
1570 AffectingClangModules.insert(M);
1571 }
1572 }
1573
1574 /// Get the set of top-level clang modules that affected preprocessing, but were not
1575 /// imported.
1577 return AffectingClangModules;
1578 }
1579
1580 /// Mark the file as included.
1581 /// Returns true if this is the first time the file was included.
1583 HeaderInfo.getFileInfo(File).IsLocallyIncluded = true;
1584 return IncludedFiles.insert(File).second;
1585 }
1586
1587 /// Return true if this header has already been included.
1589 HeaderInfo.getFileInfo(File);
1590 return IncludedFiles.count(File);
1591 }
1592
1593 /// Get the set of included files.
1594 IncludedFilesSet &getIncludedFiles() { return IncludedFiles; }
1595 const IncludedFilesSet &getIncludedFiles() const { return IncludedFiles; }
1596
1597 /// Return the name of the macro defined before \p Loc that has
1598 /// spelling \p Tokens. If there are multiple macros with same spelling,
1599 /// return the last one defined.
1601 ArrayRef<TokenValue> Tokens) const;
1602
1603 /// Get the predefines for this processor.
1604 /// Used by some third-party tools to inspect and add predefines (see
1605 /// https://github.com/llvm/llvm-project/issues/57483).
1606 const std::string &getPredefines() const { return Predefines; }
1607
1608 /// Set the predefines for this Preprocessor.
1609 ///
1610 /// These predefines are automatically injected when parsing the main file.
1611 void setPredefines(std::string P) { Predefines = std::move(P); }
1612
1613 /// Return information about the specified preprocessor
1614 /// identifier token.
1615 IdentifierInfo *getIdentifierInfo(StringRef Name) const {
1616 return &Identifiers.get(Name);
1617 }
1618
1619 /// Add the specified pragma handler to this preprocessor.
1620 ///
1621 /// If \p Namespace is non-null, then it is a token required to exist on the
1622 /// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
1623 void AddPragmaHandler(StringRef Namespace, PragmaHandler *Handler);
1625 AddPragmaHandler(StringRef(), Handler);
1626 }
1627
1628 /// Remove the specific pragma handler from this preprocessor.
1629 ///
1630 /// If \p Namespace is non-null, then it should be the namespace that
1631 /// \p Handler was added to. It is an error to remove a handler that
1632 /// has not been registered.
1633 void RemovePragmaHandler(StringRef Namespace, PragmaHandler *Handler);
1635 RemovePragmaHandler(StringRef(), Handler);
1636 }
1637
1638 /// Install empty handlers for all pragmas (making them ignored).
1639 void IgnorePragmas();
1640
1641 /// Set empty line handler.
1642 void setEmptylineHandler(EmptylineHandler *Handler) { Emptyline = Handler; }
1643
1644 EmptylineHandler *getEmptylineHandler() const { return Emptyline; }
1645
1646 /// Add the specified comment handler to the preprocessor.
1647 void addCommentHandler(CommentHandler *Handler);
1648
1649 /// Remove the specified comment handler.
1650 ///
1651 /// It is an error to remove a handler that has not been registered.
1652 void removeCommentHandler(CommentHandler *Handler);
1653
1654 /// Set the code completion handler to the given object.
1656 CodeComplete = &Handler;
1657 }
1658
1659 /// Retrieve the current code-completion handler.
1661 return CodeComplete;
1662 }
1663
1664 /// Clear out the code completion handler.
1666 CodeComplete = nullptr;
1667 }
1668
1669 /// Hook used by the lexer to invoke the "included file" code
1670 /// completion point.
1671 void CodeCompleteIncludedFile(llvm::StringRef Dir, bool IsAngled);
1672
1673 /// Hook used by the lexer to invoke the "natural language" code
1674 /// completion point.
1676
1677 /// Set the code completion token for filtering purposes.
1679 CodeCompletionII = Filter;
1680 }
1681
1682 /// Set the code completion token range for detecting replacement range later
1683 /// on.
1685 const SourceLocation End) {
1686 CodeCompletionTokenRange = {Start, End};
1687 }
1689 return CodeCompletionTokenRange;
1690 }
1691
1692 /// Get the code completion token for filtering purposes.
1694 if (CodeCompletionII)
1695 return CodeCompletionII->getName();
1696 return {};
1697 }
1698
1699 /// Retrieve the preprocessing record, or NULL if there is no
1700 /// preprocessing record.
1702
1703 /// Create a new preprocessing record, which will keep track of
1704 /// all macro expansions, macro definitions, etc.
1706
1707 /// Returns true if the FileEntry is the PCH through header.
1708 bool isPCHThroughHeader(const FileEntry *FE);
1709
1710 /// True if creating a PCH with a through header.
1712
1713 /// True if using a PCH with a through header.
1715
1716 /// True if creating a PCH with a #pragma hdrstop.
1718
1719 /// True if using a PCH with a #pragma hdrstop.
1721
1722 /// Skip tokens until after the #include of the through header or
1723 /// until after a #pragma hdrstop.
1725
1726 /// Process directives while skipping until the through header or
1727 /// #pragma hdrstop is found.
1729 SourceLocation HashLoc);
1730
1731 /// Enter the specified FileID as the main source file,
1732 /// which implicitly adds the builtin defines etc.
1733 void EnterMainSourceFile();
1734
1735 /// Inform the preprocessor callbacks that processing is complete.
1736 void EndSourceFile();
1737
1738 /// Add a source file to the top of the include stack and
1739 /// start lexing tokens from it instead of the current buffer.
1740 ///
1741 /// Emits a diagnostic, doesn't enter the file, and returns true on error.
1743 SourceLocation Loc, bool IsFirstIncludeOfFile = true);
1744
1745 /// Add a Macro to the top of the include stack and start lexing
1746 /// tokens from it instead of the current buffer.
1747 ///
1748 /// \param Args specifies the tokens input to a function-like macro.
1749 /// \param ILEnd specifies the location of the ')' for a function-like macro
1750 /// or the identifier for an object-like macro.
1752 MacroArgs *Args);
1753
1754private:
1755 /// Add a "macro" context to the top of the include stack,
1756 /// which will cause the lexer to start returning the specified tokens.
1757 ///
1758 /// If \p DisableMacroExpansion is true, tokens lexed from the token stream
1759 /// will not be subject to further macro expansion. Otherwise, these tokens
1760 /// will be re-macro-expanded when/if expansion is enabled.
1761 ///
1762 /// If \p OwnsTokens is false, this method assumes that the specified stream
1763 /// of tokens has a permanent owner somewhere, so they do not need to be
1764 /// copied. If it is true, it assumes the array of tokens is allocated with
1765 /// \c new[] and the Preprocessor will delete[] it.
1766 ///
1767 /// If \p IsReinject the resulting tokens will have Token::IsReinjected flag
1768 /// set, see the flag documentation for details.
1769 void EnterTokenStream(const Token *Toks, unsigned NumToks,
1770 bool DisableMacroExpansion, bool OwnsTokens,
1771 bool IsReinject);
1772
1773public:
1774 void EnterTokenStream(std::unique_ptr<Token[]> Toks, unsigned NumToks,
1775 bool DisableMacroExpansion, bool IsReinject) {
1776 EnterTokenStream(Toks.release(), NumToks, DisableMacroExpansion, true,
1777 IsReinject);
1778 }
1779
1780 void EnterTokenStream(ArrayRef<Token> Toks, bool DisableMacroExpansion,
1781 bool IsReinject) {
1782 EnterTokenStream(Toks.data(), Toks.size(), DisableMacroExpansion, false,
1783 IsReinject);
1784 }
1785
1786 /// Pop the current lexer/macro exp off the top of the lexer stack.
1787 ///
1788 /// This should only be used in situations where the current state of the
1789 /// top-of-stack lexer is known.
1790 void RemoveTopOfLexerStack();
1791
1792 /// From the point that this method is called, and until
1793 /// CommitBacktrackedTokens() or Backtrack() is called, the Preprocessor
1794 /// keeps track of the lexed tokens so that a subsequent Backtrack() call will
1795 /// make the Preprocessor re-lex the same tokens.
1796 ///
1797 /// Nested backtracks are allowed, meaning that EnableBacktrackAtThisPos can
1798 /// be called multiple times and CommitBacktrackedTokens/Backtrack calls will
1799 /// be combined with the EnableBacktrackAtThisPos calls in reverse order.
1800 ///
1801 /// NOTE: *DO NOT* forget to call either CommitBacktrackedTokens or Backtrack
1802 /// at some point after EnableBacktrackAtThisPos. If you don't, caching of
1803 /// tokens will continue indefinitely.
1804 ///
1805 /// \param Unannotated Whether token annotations are reverted upon calling
1806 /// Backtrack().
1807 void EnableBacktrackAtThisPos(bool Unannotated = false);
1808
1809private:
1810 std::pair<CachedTokensTy::size_type, bool> LastBacktrackPos();
1811
1812 CachedTokensTy PopUnannotatedBacktrackTokens();
1813
1814public:
1815 /// Disable the last EnableBacktrackAtThisPos call.
1817
1818 /// Make Preprocessor re-lex the tokens that were lexed since
1819 /// EnableBacktrackAtThisPos() was previously called.
1820 void Backtrack();
1821
1822 /// True if EnableBacktrackAtThisPos() was called and
1823 /// caching of tokens is on.
1824 bool isBacktrackEnabled() const { return !BacktrackPositions.empty(); }
1825
1826 /// True if EnableBacktrackAtThisPos() was called and
1827 /// caching of unannotated tokens is on.
1829 return !UnannotatedBacktrackTokens.empty();
1830 }
1831
1832 /// Lex the next token for this preprocessor.
1833 void Lex(Token &Result);
1834
1835 /// Lex all tokens for this preprocessor until (and excluding) end of file.
1836 void LexTokensUntilEOF(std::vector<Token> *Tokens = nullptr);
1837
1838 /// Lex a token, forming a header-name token if possible.
1839 bool LexHeaderName(Token &Result, bool AllowMacroExpansion = true);
1840
1841 /// Lex the parameters for an #embed directive, returns nullopt on error.
1842 std::optional<LexEmbedParametersResult> LexEmbedParameters(Token &Current,
1843 bool ForHasEmbed);
1844
1845 /// Whether the main file is preprocessed module file.
1847 return MainFileIsPreprocessedModuleFile;
1848 }
1849
1850 /// Mark the main file as a preprocessed module file, then the 'module' and
1851 /// 'import' directive recognition will be suppressed. Only
1852 /// '__preprocessed_moduke' and '__preprocessed_import' are allowed.
1854 MainFileIsPreprocessedModuleFile = true;
1855 }
1856
1858 SmallVectorImpl<Token> &Suffix,
1860 bool AllowMacroExpansion = true,
1861 bool IsPartition = false);
1863 void HandleCXXImportDirective(Token Import);
1865
1866 /// Callback invoked when the lexer sees one of export, import or module token
1867 /// at the start of a line.
1868 ///
1869 /// This consumes the import/module directive, modifies the
1870 /// lexer/preprocessor state, and advances the lexer(s) so that the next token
1871 /// read is the correct one.
1873 bool TokAtPhysicalStartOfLine);
1874
1875 /// Get the start location of the first pp-token in main file.
1877 assert(FirstPPTokenLoc.isValid() &&
1878 "Did not see the first pp-token in the main file");
1879 return FirstPPTokenLoc;
1880 }
1881
1884 bool StopUntilEOD = false);
1886 bool StopUntilEOD = false);
1887
1889 bool IncludeExports = true);
1890
1892 return CurSubmoduleState->VisibleModules.getImportLoc(M);
1893 }
1894
1895 /// Lex a string literal, which may be the concatenation of multiple
1896 /// string literals and may even come from macro expansion.
1897 /// \returns true on success, false if a error diagnostic has been generated.
1898 bool LexStringLiteral(Token &Result, std::string &String,
1899 const char *DiagnosticTag, bool AllowMacroExpansion) {
1900 if (AllowMacroExpansion)
1901 Lex(Result);
1902 else
1904 return FinishLexStringLiteral(Result, String, DiagnosticTag,
1905 AllowMacroExpansion);
1906 }
1907
1908 /// Complete the lexing of a string literal where the first token has
1909 /// already been lexed (see LexStringLiteral).
1910 bool FinishLexStringLiteral(Token &Result, std::string &String,
1911 const char *DiagnosticTag,
1912 bool AllowMacroExpansion);
1913
1914 /// Lex a token. If it's a comment, keep lexing until we get
1915 /// something not a comment.
1916 ///
1917 /// This is useful in -E -C mode where comments would foul up preprocessor
1918 /// directive handling.
1920 do
1921 Lex(Result);
1922 while (Result.getKind() == tok::comment);
1923 }
1924
1925 /// Just like Lex, but disables macro expansion of identifier tokens.
1927 // Disable macro expansion.
1928 bool OldVal = DisableMacroExpansion;
1929 DisableMacroExpansion = true;
1930 // Lex the token.
1931 Lex(Result);
1932
1933 // Reenable it.
1934 DisableMacroExpansion = OldVal;
1935 }
1936
1937 /// Like LexNonComment, but this disables macro expansion of
1938 /// identifier tokens.
1940 do
1942 while (Result.getKind() == tok::comment);
1943 }
1944
1945 /// Parses a simple integer literal to get its numeric value. Floating
1946 /// point literals and user defined literals are rejected. Used primarily to
1947 /// handle pragmas that accept integer arguments.
1948 bool parseSimpleIntegerLiteral(Token &Tok, uint64_t &Value);
1949
1950 /// Disables macro expansion everywhere except for preprocessor directives.
1952 DisableMacroExpansion = true;
1953 MacroExpansionInDirectivesOverride = true;
1954 }
1955
1957 DisableMacroExpansion = MacroExpansionInDirectivesOverride = false;
1958 }
1959
1960 /// Peeks ahead N tokens and returns that token without consuming any
1961 /// tokens.
1962 ///
1963 /// LookAhead(0) returns the next token that would be returned by Lex(),
1964 /// LookAhead(1) returns the token after it, etc. This returns normal
1965 /// tokens after phase 5. As such, it is equivalent to using
1966 /// 'Lex', not 'LexUnexpandedToken'.
1967 const Token &LookAhead(unsigned N) {
1968 assert(LexLevel == 0 && "cannot use lookahead while lexing");
1969 if (CachedLexPos + N < CachedTokens.size())
1970 return CachedTokens[CachedLexPos+N];
1971 else
1972 return PeekAhead(N+1);
1973 }
1974
1975 /// When backtracking is enabled and tokens are cached,
1976 /// this allows to revert a specific number of tokens.
1977 ///
1978 /// Note that the number of tokens being reverted should be up to the last
1979 /// backtrack position, not more.
1980 void RevertCachedTokens(unsigned N) {
1981 assert(isBacktrackEnabled() &&
1982 "Should only be called when tokens are cached for backtracking");
1983 assert(signed(CachedLexPos) - signed(N) >=
1984 signed(LastBacktrackPos().first) &&
1985 "Should revert tokens up to the last backtrack position, not more");
1986 assert(signed(CachedLexPos) - signed(N) >= 0 &&
1987 "Corrupted backtrack positions ?");
1988 CachedLexPos -= N;
1989 }
1990
1991 /// Enters a token in the token stream to be lexed next.
1992 ///
1993 /// If BackTrack() is called afterwards, the token will remain at the
1994 /// insertion point.
1995 /// If \p IsReinject is true, resulting token will have Token::IsReinjected
1996 /// flag set. See the flag documentation for details.
1997 void EnterToken(const Token &Tok, bool IsReinject) {
1998 if (LexLevel) {
1999 // It's not correct in general to enter caching lex mode while in the
2000 // middle of a nested lexing action.
2001 auto TokCopy = std::make_unique<Token[]>(1);
2002 TokCopy[0] = Tok;
2003 EnterTokenStream(std::move(TokCopy), 1, true, IsReinject);
2004 } else {
2005 EnterCachingLexMode();
2006 assert(IsReinject && "new tokens in the middle of cached stream");
2007 CachedTokens.insert(CachedTokens.begin()+CachedLexPos, Tok);
2008 }
2009 }
2010
2011 /// We notify the Preprocessor that if it is caching tokens (because
2012 /// backtrack is enabled) it should replace the most recent cached tokens
2013 /// with the given annotation token. This function has no effect if
2014 /// backtracking is not enabled.
2015 ///
2016 /// Note that the use of this function is just for optimization, so that the
2017 /// cached tokens doesn't get re-parsed and re-resolved after a backtrack is
2018 /// invoked.
2020 assert(Tok.isAnnotation() && "Expected annotation token");
2021 if (CachedLexPos != 0 && isBacktrackEnabled())
2022 AnnotatePreviousCachedTokens(Tok);
2023 }
2024
2025 /// Get the location of the last cached token, suitable for setting the end
2026 /// location of an annotation token.
2028 assert(CachedLexPos != 0);
2029 return CachedTokens[CachedLexPos-1].getLastLoc();
2030 }
2031
2032 /// Whether \p Tok is the most recent token (`CachedLexPos - 1`) in
2033 /// CachedTokens.
2034 bool IsPreviousCachedToken(const Token &Tok) const;
2035
2036 /// Replace token in `CachedLexPos - 1` in CachedTokens by the tokens
2037 /// in \p NewToks.
2038 ///
2039 /// Useful when a token needs to be split in smaller ones and CachedTokens
2040 /// most recent token must to be updated to reflect that.
2042
2043 /// Replace the last token with an annotation token.
2044 ///
2045 /// Like AnnotateCachedTokens(), this routine replaces an
2046 /// already-parsed (and resolved) token with an annotation
2047 /// token. However, this routine only replaces the last token with
2048 /// the annotation token; it does not affect any other cached
2049 /// tokens. This function has no effect if backtracking is not
2050 /// enabled.
2052 assert(Tok.isAnnotation() && "Expected annotation token");
2053 if (CachedLexPos != 0 && isBacktrackEnabled())
2054 CachedTokens[CachedLexPos-1] = Tok;
2055 }
2056
2057 /// Enter an annotation token into the token stream.
2059 void *AnnotationVal);
2060
2061 /// Determine whether it's possible for a future call to Lex to produce an
2062 /// annotation token created by a previous call to EnterAnnotationToken.
2064 return CurLexerCallback != CLK_Lexer;
2065 }
2066
2067 /// Update the current token to represent the provided
2068 /// identifier, in order to cache an action performed by typo correction.
2070 assert(Tok.getIdentifierInfo() && "Expected identifier token");
2071 if (CachedLexPos != 0 && isBacktrackEnabled())
2072 CachedTokens[CachedLexPos-1] = Tok;
2073 }
2074
2075 /// Recompute the current lexer kind based on the CurLexer/
2076 /// CurTokenLexer pointers.
2077 void recomputeCurLexerKind();
2078
2079 /// Returns true if incremental processing is enabled
2080 bool isIncrementalProcessingEnabled() const { return IncrementalProcessing; }
2081
2082 /// Enables the incremental processing
2083 void enableIncrementalProcessing(bool value = true) {
2084 IncrementalProcessing = value;
2085 }
2086
2087 /// Specify the point at which code-completion will be performed.
2088 ///
2089 /// \param File the file in which code completion should occur. If
2090 /// this file is included multiple times, code-completion will
2091 /// perform completion the first time it is included. If NULL, this
2092 /// function clears out the code-completion point.
2093 ///
2094 /// \param Line the line at which code completion should occur
2095 /// (1-based).
2096 ///
2097 /// \param Column the column at which code completion should occur
2098 /// (1-based).
2099 ///
2100 /// \returns true if an error occurred, false otherwise.
2102 unsigned Column);
2103
2104 /// Determine if we are performing code completion.
2105 bool isCodeCompletionEnabled() const { return CodeCompletionFile != nullptr; }
2106
2107 /// Returns the location of the code-completion point.
2108 ///
2109 /// Returns an invalid location if code-completion is not enabled or the file
2110 /// containing the code-completion point has not been lexed yet.
2111 SourceLocation getCodeCompletionLoc() const { return CodeCompletionLoc; }
2112
2113 /// Returns the start location of the file of code-completion point.
2114 ///
2115 /// Returns an invalid location if code-completion is not enabled or the file
2116 /// containing the code-completion point has not been lexed yet.
2118 return CodeCompletionFileLoc;
2119 }
2120
2121 /// Returns true if code-completion is enabled and we have hit the
2122 /// code-completion point.
2123 bool isCodeCompletionReached() const { return CodeCompletionReached; }
2124
2125 /// Note that we hit the code-completion point.
2127 assert(isCodeCompletionEnabled() && "Code-completion not enabled!");
2128 CodeCompletionReached = true;
2129 // Silence any diagnostics that occur after we hit the code-completion.
2131 }
2132
2133 /// The location of the currently-active \#pragma clang
2134 /// arc_cf_code_audited begin.
2135 ///
2136 /// Returns an invalid location if there is no such pragma active.
2138 return PragmaARCCFCodeAuditedInfo;
2139 }
2140
2141 /// Set the location of the currently-active \#pragma clang
2142 /// arc_cf_code_audited begin. An invalid location ends the pragma.
2144 SourceLocation Loc) {
2145 PragmaARCCFCodeAuditedInfo = IdentifierLoc(Loc, Ident);
2146 }
2147
2148 /// The location of the currently-active \#pragma clang
2149 /// assume_nonnull begin.
2150 ///
2151 /// Returns an invalid location if there is no such pragma active.
2153 return PragmaAssumeNonNullLoc;
2154 }
2155
2156 /// Set the location of the currently-active \#pragma clang
2157 /// assume_nonnull begin. An invalid location ends the pragma.
2159 PragmaAssumeNonNullLoc = Loc;
2160 }
2161
2162 /// Get the location of the recorded unterminated \#pragma clang
2163 /// assume_nonnull begin in the preamble, if one exists.
2164 ///
2165 /// Returns an invalid location if the premable did not end with
2166 /// such a pragma active or if there is no recorded preamble.
2168 return PreambleRecordedPragmaAssumeNonNullLoc;
2169 }
2170
2171 /// Record the location of the unterminated \#pragma clang
2172 /// assume_nonnull begin in the preamble.
2174 PreambleRecordedPragmaAssumeNonNullLoc = Loc;
2175 }
2176
2177 /// Set the directory in which the main file should be considered
2178 /// to have been found, if it is not a real file.
2179 void setMainFileDir(DirectoryEntryRef Dir) { MainFileDir = Dir; }
2180
2181 /// Instruct the preprocessor to skip part of the main source file.
2182 ///
2183 /// \param Bytes The number of bytes in the preamble to skip.
2184 ///
2185 /// \param StartOfLine Whether skipping these bytes puts the lexer at the
2186 /// start of a line.
2187 void setSkipMainFilePreamble(unsigned Bytes, bool StartOfLine) {
2188 SkipMainFilePreamble.first = Bytes;
2189 SkipMainFilePreamble.second = StartOfLine;
2190 }
2191
2192 /// Forwarding function for diagnostics. This emits a diagnostic at
2193 /// the specified Token's location, translating the token's start
2194 /// position in the current buffer into a SourcePosition object for rendering.
2195 DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const {
2196 return Diags->Report(Loc, DiagID);
2197 }
2198
2199 DiagnosticBuilder Diag(const Token &Tok, unsigned DiagID) const {
2200 return Diags->Report(Tok.getLocation(), DiagID);
2201 }
2202
2203 /// Return the 'spelling' of the token at the given
2204 /// location; does not go up to the spelling location or down to the
2205 /// expansion location.
2206 ///
2207 /// \param buffer A buffer which will be used only if the token requires
2208 /// "cleaning", e.g. if it contains trigraphs or escaped newlines
2209 /// \param invalid If non-null, will be set \c true if an error occurs.
2211 SmallVectorImpl<char> &buffer,
2212 bool *invalid = nullptr) const {
2213 return Lexer::getSpelling(loc, buffer, SourceMgr, LangOpts, invalid);
2214 }
2215
2216 /// Return the 'spelling' of the Tok token.
2217 ///
2218 /// The spelling of a token is the characters used to represent the token in
2219 /// the source file after trigraph expansion and escaped-newline folding. In
2220 /// particular, this wants to get the true, uncanonicalized, spelling of
2221 /// things like digraphs, UCNs, etc.
2222 ///
2223 /// \param Invalid If non-null, will be set \c true if an error occurs.
2224 std::string getSpelling(const Token &Tok, bool *Invalid = nullptr) const {
2225 return Lexer::getSpelling(Tok, SourceMgr, LangOpts, Invalid);
2226 }
2227
2228 /// Get the spelling of a token into a preallocated buffer, instead
2229 /// of as an std::string.
2230 ///
2231 /// The caller is required to allocate enough space for the token, which is
2232 /// guaranteed to be at least Tok.getLength() bytes long. The length of the
2233 /// actual result is returned.
2234 ///
2235 /// Note that this method may do two possible things: it may either fill in
2236 /// the buffer specified with characters, or it may *change the input pointer*
2237 /// to point to a constant buffer with the data already in it (avoiding a
2238 /// copy). The caller is not allowed to modify the returned buffer pointer
2239 /// if an internal buffer is returned.
2240 unsigned getSpelling(const Token &Tok, const char *&Buffer,
2241 bool *Invalid = nullptr) const {
2242 return Lexer::getSpelling(Tok, Buffer, SourceMgr, LangOpts, Invalid);
2243 }
2244
2245 /// Get the spelling of a token into a SmallVector.
2246 ///
2247 /// Note that the returned StringRef may not point to the
2248 /// supplied buffer if a copy can be avoided.
2249 StringRef getSpelling(const Token &Tok,
2250 SmallVectorImpl<char> &Buffer,
2251 bool *Invalid = nullptr) const;
2252
2253 /// Relex the token at the specified location.
2254 /// \returns true if there was a failure, false on success.
2256 bool IgnoreWhiteSpace = false) {
2257 return Lexer::getRawToken(Loc, Result, SourceMgr, LangOpts, IgnoreWhiteSpace);
2258 }
2259
2260 /// Given a Token \p Tok that is a numeric constant with length 1,
2261 /// return the value of constant as an unsigned 8-bit integer.
2262 uint8_t
2264 bool *Invalid = nullptr) const {
2265 assert((Tok.is(tok::numeric_constant) || Tok.is(tok::binary_data)) &&
2266 Tok.getLength() == 1 && "Called on unsupported token");
2267 assert(!Tok.needsCleaning() && "Token can't need cleaning with length 1");
2268
2269 // If the token is carrying a literal data pointer, just use it.
2270 if (const char *D = Tok.getLiteralData())
2271 return (Tok.getKind() == tok::binary_data) ? *D : *D - '0';
2272
2273 assert(Tok.is(tok::numeric_constant) && "binary data with no data");
2274 // Otherwise, fall back on getCharacterData, which is slower, but always
2275 // works.
2276 return *SourceMgr.getCharacterData(Tok.getLocation(), Invalid) - '0';
2277 }
2278
2279 /// Retrieve the name of the immediate macro expansion.
2280 ///
2281 /// This routine starts from a source location, and finds the name of the
2282 /// macro responsible for its immediate expansion. It looks through any
2283 /// intervening macro argument expansions to compute this. It returns a
2284 /// StringRef that refers to the SourceManager-owned buffer of the source
2285 /// where that macro name is spelled. Thus, the result shouldn't out-live
2286 /// the SourceManager.
2288 return Lexer::getImmediateMacroName(Loc, SourceMgr, getLangOpts());
2289 }
2290
2291 /// Plop the specified string into a scratch buffer and set the
2292 /// specified token's location and length to it.
2293 ///
2294 /// If specified, the source location provides a location of the expansion
2295 /// point of the token.
2296 void CreateString(StringRef Str, Token &Tok,
2297 SourceLocation ExpansionLocStart = SourceLocation(),
2298 SourceLocation ExpansionLocEnd = SourceLocation());
2299
2300 /// Split the first Length characters out of the token starting at TokLoc
2301 /// and return a location pointing to the split token. Re-lexing from the
2302 /// split token will return the split token rather than the original.
2303 SourceLocation SplitToken(SourceLocation TokLoc, unsigned Length);
2304
2305 /// Computes the source location just past the end of the
2306 /// token at this source location.
2307 ///
2308 /// This routine can be used to produce a source location that
2309 /// points just past the end of the token referenced by \p Loc, and
2310 /// is generally used when a diagnostic needs to point just after a
2311 /// token where it expected something different that it received. If
2312 /// the returned source location would not be meaningful (e.g., if
2313 /// it points into a macro), this routine returns an invalid
2314 /// source location.
2315 ///
2316 /// \param Offset an offset from the end of the token, where the source
2317 /// location should refer to. The default offset (0) produces a source
2318 /// location pointing just past the end of the token; an offset of 1 produces
2319 /// a source location pointing to the last character in the token, etc.
2321 return Lexer::getLocForEndOfToken(Loc, Offset, SourceMgr, LangOpts);
2322 }
2323
2324 /// Returns true if the given MacroID location points at the first
2325 /// token of the macro expansion.
2326 ///
2327 /// \param MacroBegin If non-null and function returns true, it is set to
2328 /// begin location of the macro.
2330 SourceLocation *MacroBegin = nullptr) const {
2331 return Lexer::isAtStartOfMacroExpansion(loc, SourceMgr, LangOpts,
2332 MacroBegin);
2333 }
2334
2335 /// Returns true if the given MacroID location points at the last
2336 /// token of the macro expansion.
2337 ///
2338 /// \param MacroEnd If non-null and function returns true, it is set to
2339 /// end location of the macro.
2341 SourceLocation *MacroEnd = nullptr) const {
2342 return Lexer::isAtEndOfMacroExpansion(loc, SourceMgr, LangOpts, MacroEnd);
2343 }
2344
2345 /// Print the token to stderr, used for debugging.
2346 void DumpToken(const Token &Tok, bool DumpFlags = false) const;
2347 void DumpLocation(SourceLocation Loc) const;
2348 void DumpMacro(const MacroInfo &MI) const;
2349 void dumpMacroInfo(const IdentifierInfo *II);
2350
2351 /// Given a location that specifies the start of a
2352 /// token, return a new location that specifies a character within the token.
2354 unsigned Char) const {
2355 return Lexer::AdvanceToTokenCharacter(TokStart, Char, SourceMgr, LangOpts);
2356 }
2357
2358 /// Increment the counters for the number of token paste operations
2359 /// performed.
2360 ///
2361 /// If fast was specified, this is a 'fast paste' case we handled.
2362 void IncrementPasteCounter(bool isFast) {
2363 if (isFast)
2364 ++NumFastTokenPaste;
2365 else
2366 ++NumTokenPaste;
2367 }
2368
2369 void PrintStats();
2370
2371 size_t getTotalMemory() const;
2372
2373 /// When the macro expander pastes together a comment (/##/) in Microsoft
2374 /// mode, this method handles updating the current state, returning the
2375 /// token on the next source line.
2377
2378 //===--------------------------------------------------------------------===//
2379 // Preprocessor callback methods. These are invoked by a lexer as various
2380 // directives and events are found.
2381
2382 /// Given a tok::raw_identifier token, look up the
2383 /// identifier information for the token and install it into the token,
2384 /// updating the token kind accordingly.
2385 IdentifierInfo *LookUpIdentifierInfo(Token &Identifier) const;
2386
2387private:
2388 llvm::DenseMap<IdentifierInfo*,unsigned> PoisonReasons;
2389
2390public:
2391 /// Specifies the reason for poisoning an identifier.
2392 ///
2393 /// If that identifier is accessed while poisoned, then this reason will be
2394 /// used instead of the default "poisoned" diagnostic.
2395 void SetPoisonReason(IdentifierInfo *II, unsigned DiagID);
2396
2397 /// Display reason for poisoned identifier.
2398 void HandlePoisonedIdentifier(Token & Identifier);
2399
2401 if(IdentifierInfo * II = Identifier.getIdentifierInfo()) {
2402 if(II->isPoisoned()) {
2403 HandlePoisonedIdentifier(Identifier);
2404 }
2405 }
2406 }
2407
2408 /// isNextPPTokenOneOf - Check whether the next pp-token is one of the
2409 /// specificed token kind. this method should have no observable side-effect
2410 /// on the lexed tokens.
2411 template <typename... Ts> bool isNextPPTokenOneOf(Ts... Ks) const {
2412 static_assert(sizeof...(Ts) > 0,
2413 "requires at least one tok::TokenKind specified");
2414 auto NextTokOpt = peekNextPPToken();
2415 return NextTokOpt.has_value() ? NextTokOpt->is(Ks...) : false;
2416 }
2417
2418private:
2419 /// peekNextPPToken - Return std::nullopt if there are no more tokens in the
2420 /// buffer controlled by this lexer, otherwise return the next unexpanded
2421 /// token.
2422 std::optional<Token> peekNextPPToken() const;
2423
2424 /// Identifiers used for SEH handling in Borland. These are only
2425 /// allowed in particular circumstances
2426 // __except block
2427 IdentifierInfo *Ident__exception_code,
2428 *Ident___exception_code,
2429 *Ident_GetExceptionCode;
2430 // __except filter expression
2431 IdentifierInfo *Ident__exception_info,
2432 *Ident___exception_info,
2433 *Ident_GetExceptionInfo;
2434 // __finally
2435 IdentifierInfo *Ident__abnormal_termination,
2436 *Ident___abnormal_termination,
2437 *Ident_AbnormalTermination;
2438
2439 const char *getCurLexerEndPos();
2440 void diagnoseMissingHeaderInUmbrellaDir(const Module &Mod);
2441
2442public:
2443 void PoisonSEHIdentifiers(bool Poison = true); // Borland
2444
2445 /// Callback invoked when the lexer reads an identifier and has
2446 /// filled in the tokens IdentifierInfo member.
2447 ///
2448 /// This callback potentially macro expands it or turns it into a named
2449 /// token (like 'for').
2450 ///
2451 /// \returns true if we actually computed a token, false if we need to
2452 /// lex again.
2453 bool HandleIdentifier(Token &Identifier);
2454
2455 /// Callback invoked when the lexer hits the end of the current file.
2456 ///
2457 /// This either returns the EOF token and returns true, or
2458 /// pops a level off the include stack and returns false, at which point the
2459 /// client should call lex again.
2460 bool HandleEndOfFile(Token &Result, bool isEndOfMacro = false);
2461
2462 /// Callback invoked when the current TokenLexer hits the end of its
2463 /// token stream.
2465
2466 /// Callback invoked when the lexer sees a # token at the start of a
2467 /// line.
2468 ///
2469 /// This consumes the directive, modifies the lexer/preprocessor state, and
2470 /// advances the lexer(s) so that the next token read is the correct one.
2472
2473 /// Ensure that the next token is a tok::eod token.
2474 ///
2475 /// If not, emit a diagnostic and consume up until the eod.
2476 /// If \p EnableMacros is true, then we consider macros that expand to zero
2477 /// tokens as being ok.
2478 ///
2479 /// If \p ExtraToks not null, the extra tokens will be saved in this
2480 /// container.
2481 ///
2482 /// \return The location of the end of the directive (the terminating
2483 /// newline).
2485 CheckEndOfDirective(StringRef DirType, bool EnableMacros = false,
2486 SmallVectorImpl<Token> *ExtraToks = nullptr);
2487
2488 /// Read and discard all tokens remaining on the current line until
2489 /// the tok::eod token is found. Returns the range of the skipped tokens.
2492 Token Tmp;
2493 return DiscardUntilEndOfDirective(Tmp, DiscardedToks);
2494 }
2495
2496 /// Same as above except retains the token that was found.
2499 SmallVectorImpl<Token> *DiscardedToks = nullptr);
2500
2501 /// Returns true if the preprocessor has seen a use of
2502 /// __DATE__ or __TIME__ in the file so far.
2503 bool SawDateOrTime() const {
2504 return DATELoc != SourceLocation() || TIMELoc != SourceLocation();
2505 }
2506 uint32_t getCounterValue() const { return CounterValue; }
2507 void setCounterValue(uint32_t V) { CounterValue = V; }
2508
2510 assert(CurrentFPEvalMethod != LangOptions::FEM_UnsetOnCommandLine &&
2511 "FPEvalMethod should be set either from command line or from the "
2512 "target info");
2513 return CurrentFPEvalMethod;
2514 }
2515
2517 return TUFPEvalMethod;
2518 }
2519
2521 return LastFPEvalPragmaLocation;
2522 }
2523
2527 "FPEvalMethod should never be set to FEM_UnsetOnCommandLine");
2528 // This is the location of the '#pragma float_control" where the
2529 // execution state is modifed.
2530 LastFPEvalPragmaLocation = PragmaLoc;
2531 CurrentFPEvalMethod = Val;
2532 TUFPEvalMethod = Val;
2533 }
2534
2537 "TUPEvalMethod should never be set to FEM_UnsetOnCommandLine");
2538 TUFPEvalMethod = Val;
2539 }
2540
2541 /// Retrieves the module that we're currently building, if any.
2543
2544 /// Retrieves the module whose implementation we're current compiling, if any.
2546
2547 /// If we are preprocessing a named module.
2548 bool isInNamedModule() const { return ModuleDeclState.isNamedModule(); }
2549
2550 /// If we are proprocessing a named interface unit.
2551 /// Note that a module implementation partition is not considered as an
2552 /// named interface unit here although it is importable
2553 /// to ease the parsing.
2555 return ModuleDeclState.isNamedInterface();
2556 }
2557
2558 /// Get the named module name we're preprocessing.
2559 /// Requires we're preprocessing a named module.
2560 StringRef getNamedModuleName() const { return ModuleDeclState.getName(); }
2561
2562 /// If we are implementing an implementation module unit.
2563 /// Note that the module implementation partition is not considered as an
2564 /// implementation unit.
2566 return ModuleDeclState.isImplementationUnit();
2567 }
2568
2569 /// If we're importing a standard C++20 Named Modules.
2571 assert(getLangOpts().CPlusPlusModules &&
2572 "Import C++ named modules are only valid for C++20 modules");
2573 return ImportingCXXNamedModules;
2574 }
2575
2576 /// Allocate a new MacroInfo object with the provided SourceLocation.
2578
2579 /// Turn the specified lexer token into a fully checked and spelled
2580 /// filename, e.g. as an operand of \#include.
2581 ///
2582 /// The caller is expected to provide a buffer that is large enough to hold
2583 /// the spelling of the filename, but is also expected to handle the case
2584 /// when this method decides to use a different buffer.
2585 ///
2586 /// \returns true if the input filename was in <>'s or false if it was
2587 /// in ""'s.
2588 bool GetIncludeFilenameSpelling(SourceLocation Loc,StringRef &Buffer);
2589
2590 /// Given a "foo" or <foo> reference, look up the indicated file.
2591 ///
2592 /// Returns std::nullopt on failure. \p isAngled indicates whether the file
2593 /// reference is for system \#include's or not (i.e. using <> instead of "").
2595 LookupFile(SourceLocation FilenameLoc, StringRef Filename, bool isAngled,
2596 ConstSearchDirIterator FromDir, const FileEntry *FromFile,
2597 ConstSearchDirIterator *CurDir, SmallVectorImpl<char> *SearchPath,
2598 SmallVectorImpl<char> *RelativePath,
2599 ModuleMap::KnownHeader *SuggestedModule, bool *IsMapped,
2600 bool *IsFrameworkFound, bool SkipCache = false,
2601 bool OpenFile = true, bool CacheFailures = true);
2602
2603 /// Given a "Filename" or <Filename> reference, look up the indicated embed
2604 /// resource. \p isAngled indicates whether the file reference is for
2605 /// system \#include's or not (i.e. using <> instead of ""). If \p OpenFile
2606 /// is true, the file looked up is opened for reading, otherwise it only
2607 /// validates that the file exists. Quoted filenames are looked up relative
2608 /// to \p LookupFromFile if it is nonnull.
2609 ///
2610 /// Returns std::nullopt on failure.
2612 LookupEmbedFile(StringRef Filename, bool isAngled, bool OpenFile,
2613 const FileEntry *LookupFromFile = nullptr);
2614
2615 /// Return true if we're in the top-level file, not in a \#include.
2616 bool isInPrimaryFile() const;
2617
2618 /// Lex an on-off-switch (C99 6.10.6p2) and verify that it is
2619 /// followed by EOD. Return true if the token is not a valid on-off-switch.
2621
2622 bool CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
2623 bool *ShadowFlag = nullptr);
2624
2625 void EnterSubmodule(Module *M, SourceLocation ImportLoc, bool ForPragma);
2626 Module *LeaveSubmodule(bool ForPragma);
2627
2628private:
2629 friend void TokenLexer::ExpandFunctionArguments();
2630
2631 void PushIncludeMacroStack() {
2632 assert(CurLexerCallback != CLK_CachingLexer &&
2633 "cannot push a caching lexer");
2634 IncludeMacroStack.emplace_back(CurLexerCallback, CurLexerSubmodule,
2635 std::move(CurLexer), CurPPLexer,
2636 std::move(CurTokenLexer), CurDirLookup);
2637 CurPPLexer = nullptr;
2638 }
2639
2640 void PopIncludeMacroStack() {
2641 if (CurLexer)
2642 PendingDestroyLexers.push_back(std::move(CurLexer));
2643 CurLexer = std::move(IncludeMacroStack.back().TheLexer);
2644 CurPPLexer = IncludeMacroStack.back().ThePPLexer;
2645 CurTokenLexer = std::move(IncludeMacroStack.back().TheTokenLexer);
2646 CurDirLookup = IncludeMacroStack.back().TheDirLookup;
2647 CurLexerSubmodule = IncludeMacroStack.back().TheSubmodule;
2648 CurLexerCallback = IncludeMacroStack.back().CurLexerCallback;
2649 IncludeMacroStack.pop_back();
2650 }
2651
2652 void PropagateLineStartLeadingSpaceInfo(Token &Result);
2653
2654 /// Determine whether we need to create module macros for #defines in the
2655 /// current context.
2656 bool needModuleMacros() const;
2657
2658 /// Update the set of active module macros and ambiguity flag for a module
2659 /// macro name.
2660 void updateModuleMacroInfo(const IdentifierInfo *II, ModuleMacroInfo &Info);
2661
2662 DefMacroDirective *AllocateDefMacroDirective(MacroInfo *MI,
2663 SourceLocation Loc);
2664 UndefMacroDirective *AllocateUndefMacroDirective(SourceLocation UndefLoc);
2665 VisibilityMacroDirective *AllocateVisibilityMacroDirective(SourceLocation Loc,
2666 bool isPublic);
2667
2668 /// Lex and validate a macro name, which occurs after a
2669 /// \#define or \#undef.
2670 ///
2671 /// \param MacroNameTok Token that represents the name defined or undefined.
2672 /// \param IsDefineUndef Kind if preprocessor directive.
2673 /// \param ShadowFlag Points to flag that is set if macro name shadows
2674 /// a keyword.
2675 ///
2676 /// This emits a diagnostic, sets the token kind to eod,
2677 /// and discards the rest of the macro line if the macro name is invalid.
2678 void ReadMacroName(Token &MacroNameTok, MacroUse IsDefineUndef = MU_Other,
2679 bool *ShadowFlag = nullptr);
2680
2681 /// ReadOptionalMacroParameterListAndBody - This consumes all (i.e. the
2682 /// entire line) of the macro's tokens and adds them to MacroInfo, and while
2683 /// doing so performs certain validity checks including (but not limited to):
2684 /// - # (stringization) is followed by a macro parameter
2685 /// \param MacroNameTok - Token that represents the macro name
2686 /// \param ImmediatelyAfterHeaderGuard - Macro follows an #ifdef header guard
2687 ///
2688 /// Either returns a pointer to a MacroInfo object OR emits a diagnostic and
2689 /// returns a nullptr if an invalid sequence of tokens is encountered.
2690 MacroInfo *ReadOptionalMacroParameterListAndBody(
2691 const Token &MacroNameTok, bool ImmediatelyAfterHeaderGuard);
2692
2693 /// The ( starting an argument list of a macro definition has just been read.
2694 /// Lex the rest of the parameters and the closing ), updating \p MI with
2695 /// what we learn and saving in \p LastTok the last token read.
2696 /// Return true if an error occurs parsing the arg list.
2697 bool ReadMacroParameterList(MacroInfo *MI, Token& LastTok);
2698
2699 /// Provide a suggestion for a typoed directive. If there is no typo, then
2700 /// just skip suggesting.
2701 ///
2702 /// \param Tok - Token that represents the directive
2703 /// \param Directive - String reference for the directive name
2704 void SuggestTypoedDirective(const Token &Tok, StringRef Directive) const;
2705
2706 /// We just read a \#if or related directive and decided that the
2707 /// subsequent tokens are in the \#if'd out portion of the
2708 /// file. Lex the rest of the file, until we see an \#endif. If \p
2709 /// FoundNonSkipPortion is true, then we have already emitted code for part of
2710 /// this \#if directive, so \#else/\#elif blocks should never be entered. If
2711 /// \p FoundElse is false, then \#else directives are ok, if not, then we have
2712 /// already seen one so a \#else directive is a duplicate. When this returns,
2713 /// the caller can lex the first valid token.
2714 void SkipExcludedConditionalBlock(SourceLocation HashTokenLoc,
2715 SourceLocation IfTokenLoc,
2716 bool FoundNonSkipPortion, bool FoundElse,
2717 SourceLocation ElseLoc = SourceLocation());
2718
2719 /// Information about the result for evaluating an expression for a
2720 /// preprocessor directive.
2721 struct DirectiveEvalResult {
2722 /// The integral value of the expression.
2723 std::optional<llvm::APSInt> Value;
2724
2725 /// Whether the expression was evaluated as true or not.
2726 bool Conditional;
2727
2728 /// True if the expression contained identifiers that were undefined.
2729 bool IncludedUndefinedIds;
2730
2731 /// The source range for the expression.
2732 SourceRange ExprRange;
2733 };
2734
2735 /// Evaluate an integer constant expression that may occur after a
2736 /// \#if or \#elif directive and return a \p DirectiveEvalResult object.
2737 ///
2738 /// If the expression is equivalent to "!defined(X)" return X in IfNDefMacro.
2739 DirectiveEvalResult EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro,
2740 bool CheckForEoD = true);
2741
2742 /// Evaluate an integer constant expression that may occur after a
2743 /// \#if or \#elif directive and return a \p DirectiveEvalResult object.
2744 ///
2745 /// If the expression is equivalent to "!defined(X)" return X in IfNDefMacro.
2746 /// \p EvaluatedDefined will contain the result of whether "defined" appeared
2747 /// in the evaluated expression or not.
2748 DirectiveEvalResult EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro,
2749 Token &Tok,
2750 bool &EvaluatedDefined,
2751 bool CheckForEoD = true);
2752
2753 /// Process a '__has_embed("path" [, ...])' expression.
2754 ///
2755 /// Returns predefined `__STDC_EMBED_*` macro values if
2756 /// successful.
2757 EmbedResult EvaluateHasEmbed(Token &Tok, IdentifierInfo *II);
2758
2759 /// Process a '__has_include("path")' expression.
2760 ///
2761 /// Returns true if successful.
2762 bool EvaluateHasInclude(Token &Tok, IdentifierInfo *II);
2763
2764 /// Process '__has_include_next("path")' expression.
2765 ///
2766 /// Returns true if successful.
2767 bool EvaluateHasIncludeNext(Token &Tok, IdentifierInfo *II);
2768
2769 /// Get the directory and file from which to start \#include_next lookup.
2770 std::pair<ConstSearchDirIterator, const FileEntry *>
2771 getIncludeNextStart(const Token &IncludeNextTok) const;
2772
2773 /// Install the standard preprocessor pragmas:
2774 /// \#pragma GCC poison/system_header/dependency and \#pragma once.
2775 void RegisterBuiltinPragmas();
2776
2777 /// RegisterBuiltinMacro - Register the specified identifier in the identifier
2778 /// table and mark it as a builtin macro to be expanded.
2779 IdentifierInfo *RegisterBuiltinMacro(const char *Name) {
2780 // Get the identifier.
2781 IdentifierInfo *Id = getIdentifierInfo(Name);
2782
2783 // Mark it as being a macro that is builtin.
2784 MacroInfo *MI = AllocateMacroInfo(SourceLocation());
2785 MI->setIsBuiltinMacro();
2787 return Id;
2788 }
2789
2790 /// Register builtin macros such as __LINE__ with the identifier table.
2791 void RegisterBuiltinMacros();
2792
2793 /// If an identifier token is read that is to be expanded as a macro, handle
2794 /// it and return the next token as 'Tok'. If we lexed a token, return true;
2795 /// otherwise the caller should lex again.
2796 bool HandleMacroExpandedIdentifier(Token &Identifier, const MacroDefinition &MD);
2797
2798 /// Cache macro expanded tokens for TokenLexers.
2799 //
2800 /// Works like a stack; a TokenLexer adds the macro expanded tokens that is
2801 /// going to lex in the cache and when it finishes the tokens are removed
2802 /// from the end of the cache.
2803 Token *cacheMacroExpandedTokens(TokenLexer *tokLexer,
2804 ArrayRef<Token> tokens);
2805
2806 void removeCachedMacroExpandedTokensOfLastLexer();
2807
2808 /// After reading "MACRO(", this method is invoked to read all of the formal
2809 /// arguments specified for the macro invocation. Returns null on error.
2810 MacroArgs *ReadMacroCallArgumentList(Token &MacroName, MacroInfo *MI,
2811 SourceLocation &MacroEnd);
2812
2813 /// If an identifier token is read that is to be expanded
2814 /// as a builtin macro, handle it and return the next token as 'Tok'.
2815 void ExpandBuiltinMacro(Token &Tok);
2816
2817 /// Read a \c _Pragma directive, slice it up, process it, then
2818 /// return the first token after the directive.
2819 /// This assumes that the \c _Pragma token has just been read into \p Tok.
2820 void Handle_Pragma(Token &Tok);
2821
2822 /// Like Handle_Pragma except the pragma text is not enclosed within
2823 /// a string literal.
2824 void HandleMicrosoft__pragma(Token &Tok);
2825
2826 /// Add a lexer to the top of the include stack and
2827 /// start lexing tokens from it instead of the current buffer.
2828 void EnterSourceFileWithLexer(Lexer *TheLexer, ConstSearchDirIterator Dir);
2829
2830 /// Set the FileID for the preprocessor predefines.
2831 void setPredefinesFileID(FileID FID) {
2832 assert(PredefinesFileID.isInvalid() && "PredefinesFileID already set!");
2833 PredefinesFileID = FID;
2834 }
2835
2836 /// Set the FileID for the PCH through header.
2837 void setPCHThroughHeaderFileID(FileID FID);
2838
2839 /// Returns true if we are lexing from a file and not a
2840 /// pragma or a macro.
2841 static bool IsFileLexer(const Lexer* L, const PreprocessorLexer* P) {
2842 return L ? !L->isPragmaLexer() : P != nullptr;
2843 }
2844
2845 static bool IsFileLexer(const IncludeStackInfo& I) {
2846 return IsFileLexer(I.TheLexer.get(), I.ThePPLexer);
2847 }
2848
2849 bool IsFileLexer() const {
2850 return IsFileLexer(CurLexer.get(), CurPPLexer);
2851 }
2852
2853 //===--------------------------------------------------------------------===//
2854 // Standard Library Identification
2855 std::optional<CXXStandardLibraryVersionInfo> CXXStandardLibraryVersion;
2856
2857public:
2858 std::optional<std::uint64_t> getStdLibCxxVersion();
2859 bool NeedsStdLibCxxWorkaroundBefore(std::uint64_t FixedVersion);
2860
2861private:
2862 //===--------------------------------------------------------------------===//
2863 // Caching stuff.
2864 void CachingLex(Token &Result);
2865
2866 bool InCachingLexMode() const {
2867 // If the Lexer pointers are 0 and IncludeMacroStack is empty, it means
2868 // that we are past EOF, not that we are in CachingLex mode.
2869 return !CurPPLexer && !CurTokenLexer && !IncludeMacroStack.empty();
2870 }
2871
2872 void EnterCachingLexMode();
2873 void EnterCachingLexModeUnchecked();
2874
2875 void ExitCachingLexMode() {
2876 if (InCachingLexMode())
2878 }
2879
2880 const Token &PeekAhead(unsigned N);
2881 void AnnotatePreviousCachedTokens(const Token &Tok);
2882
2883 //===--------------------------------------------------------------------===//
2884 /// Handle*Directive - implement the various preprocessor directives. These
2885 /// should side-effect the current preprocessor object so that the next call
2886 /// to Lex() will return the appropriate token next.
2887 void HandleLineDirective();
2888 void HandleDigitDirective(Token &Tok);
2889 void HandleUserDiagnosticDirective(Token &Tok, bool isWarning);
2890 void HandleIdentSCCSDirective(Token &Tok);
2891 void HandleMacroPublicDirective(Token &Tok);
2892 void HandleMacroPrivateDirective();
2893
2894 /// An additional notification that can be produced by a header inclusion or
2895 /// import to tell the parser what happened.
2896 struct ImportAction {
2897 enum ActionKind {
2898 None,
2899 ModuleBegin,
2900 ModuleImport,
2901 HeaderUnitImport,
2902 SkippedModuleImport,
2903 Failure,
2904 } Kind;
2905 Module *ModuleForHeader = nullptr;
2906
2907 ImportAction(ActionKind AK, Module *Mod = nullptr)
2908 : Kind(AK), ModuleForHeader(Mod) {
2909 assert((AK == None || Mod || AK == Failure) &&
2910 "no module for module action");
2911 }
2912 };
2913
2914 OptionalFileEntryRef LookupHeaderIncludeOrImport(
2915 ConstSearchDirIterator *CurDir, StringRef &Filename,
2916 SourceLocation FilenameLoc, CharSourceRange FilenameRange,
2917 const Token &FilenameTok, bool &IsFrameworkFound, bool IsImportDecl,
2918 bool &IsMapped, ConstSearchDirIterator LookupFrom,
2919 const FileEntry *LookupFromFile, StringRef &LookupFilename,
2920 SmallVectorImpl<char> &RelativePath, SmallVectorImpl<char> &SearchPath,
2921 ModuleMap::KnownHeader &SuggestedModule, bool isAngled);
2922 // Binary data inclusion
2923 void HandleEmbedDirective(SourceLocation HashLoc, Token &Tok,
2924 const FileEntry *LookupFromFile = nullptr);
2925 void HandleEmbedDirectiveImpl(SourceLocation HashLoc,
2926 const LexEmbedParametersResult &Params,
2927 StringRef BinaryContents, StringRef FileName);
2928
2929 // File inclusion.
2930 void HandleIncludeDirective(SourceLocation HashLoc, Token &Tok,
2931 ConstSearchDirIterator LookupFrom = nullptr,
2932 const FileEntry *LookupFromFile = nullptr);
2933 ImportAction
2934 HandleHeaderIncludeOrImport(SourceLocation HashLoc, Token &IncludeTok,
2935 Token &FilenameTok, SourceLocation EndLoc,
2936 ConstSearchDirIterator LookupFrom = nullptr,
2937 const FileEntry *LookupFromFile = nullptr);
2938 void HandleIncludeNextDirective(SourceLocation HashLoc, Token &Tok);
2939 void HandleIncludeMacrosDirective(SourceLocation HashLoc, Token &Tok);
2940 void HandleImportDirective(SourceLocation HashLoc, Token &Tok);
2941 void HandleMicrosoftImportDirective(Token &Tok);
2942
2943public:
2944 /// Check that the given module is available, producing a diagnostic if not.
2945 /// \return \c true if the check failed (because the module is not available).
2946 /// \c false if the module appears to be usable.
2947 static bool checkModuleIsAvailable(const LangOptions &LangOpts,
2948 const TargetInfo &TargetInfo,
2949 const Module &M, DiagnosticsEngine &Diags);
2950
2951 // Module inclusion testing.
2952 /// Find the module that owns the source or header file that
2953 /// \p Loc points to. If the location is in a file that was included
2954 /// into a module, or is outside any module, returns nullptr.
2955 Module *getModuleForLocation(SourceLocation Loc, bool AllowTextual);
2956
2957 /// We want to produce a diagnostic at location IncLoc concerning an
2958 /// unreachable effect at location MLoc (eg, where a desired entity was
2959 /// declared or defined). Determine whether the right way to make MLoc
2960 /// reachable is by #include, and if so, what header should be included.
2961 ///
2962 /// This is not necessarily fast, and might load unexpected module maps, so
2963 /// should only be called by code that intends to produce an error.
2964 ///
2965 /// \param IncLoc The location at which the missing effect was detected.
2966 /// \param MLoc A location within an unimported module at which the desired
2967 /// effect occurred.
2968 /// \return A file that can be #included to provide the desired effect. Null
2969 /// if no such file could be determined or if a #include is not
2970 /// appropriate (eg, if a module should be imported instead).
2972 SourceLocation MLoc);
2973
2974 bool isRecordingPreamble() const {
2975 return PreambleConditionalStack.isRecording();
2976 }
2977
2978 bool hasRecordedPreamble() const {
2979 return PreambleConditionalStack.hasRecordedPreamble();
2980 }
2981
2983 return PreambleConditionalStack.getStack();
2984 }
2985
2987 PreambleConditionalStack.setStack(s);
2988 }
2989
2991 ArrayRef<PPConditionalInfo> s, std::optional<PreambleSkipInfo> SkipInfo) {
2992 PreambleConditionalStack.startReplaying();
2993 PreambleConditionalStack.setStack(s);
2994 PreambleConditionalStack.SkipInfo = SkipInfo;
2995 }
2996
2997 std::optional<PreambleSkipInfo> getPreambleSkipInfo() const {
2998 return PreambleConditionalStack.SkipInfo;
2999 }
3000
3001private:
3002 /// After processing predefined file, initialize the conditional stack from
3003 /// the preamble.
3004 void replayPreambleConditionalStack();
3005
3006 // Macro handling.
3007 void HandleDefineDirective(Token &Tok, bool ImmediatelyAfterHeaderGuard);
3008 void HandleUndefDirective();
3009
3010 // Conditional Inclusion.
3011 void HandleIfdefDirective(Token &Result, const Token &HashToken,
3012 bool isIfndef, bool ReadAnyTokensBeforeDirective);
3013 void HandleIfDirective(Token &IfToken, const Token &HashToken,
3014 bool ReadAnyTokensBeforeDirective);
3015 void HandleEndifDirective(Token &EndifToken);
3016 void HandleElseDirective(Token &Result, const Token &HashToken);
3017 void HandleElifFamilyDirective(Token &ElifToken, const Token &HashToken,
3018 tok::PPKeywordKind Kind);
3019
3020 // Pragmas.
3021 void HandlePragmaDirective(PragmaIntroducer Introducer);
3022
3023public:
3024 void HandlePragmaOnce(Token &OnceTok);
3025 void HandlePragmaMark(Token &MarkTok);
3026 void HandlePragmaPoison();
3027 void HandlePragmaSystemHeader(Token &SysHeaderTok);
3028 void HandlePragmaDependency(Token &DependencyTok);
3035
3036 // Return true and store the first token only if any CommentHandler
3037 // has inserted some tokens and getCommentRetentionState() is false.
3038 bool HandleComment(Token &result, SourceRange Comment);
3039
3040 /// A macro is used, update information about macros that need unused
3041 /// warnings.
3042 void markMacroAsUsed(MacroInfo *MI);
3043
3044 void addMacroDeprecationMsg(const IdentifierInfo *II, std::string Msg,
3045 SourceLocation AnnotationLoc) {
3046 AnnotationInfos[II].DeprecationInfo =
3047 MacroAnnotationInfo{AnnotationLoc, std::move(Msg)};
3048 }
3049
3050 void addRestrictExpansionMsg(const IdentifierInfo *II, std::string Msg,
3051 SourceLocation AnnotationLoc) {
3052 AnnotationInfos[II].RestrictExpansionInfo =
3053 MacroAnnotationInfo{AnnotationLoc, std::move(Msg)};
3054 }
3055
3056 void addFinalLoc(const IdentifierInfo *II, SourceLocation AnnotationLoc) {
3057 AnnotationInfos[II].FinalAnnotationLoc = AnnotationLoc;
3058 }
3059
3060 const MacroAnnotations &getMacroAnnotations(const IdentifierInfo *II) const {
3061 return AnnotationInfos.find(II)->second;
3062 }
3063
3064 void emitMacroExpansionWarnings(const Token &Identifier,
3065 bool IsIfnDef = false) const {
3066 IdentifierInfo *Info = Identifier.getIdentifierInfo();
3067 if (Info->isDeprecatedMacro())
3068 emitMacroDeprecationWarning(Identifier);
3069
3070 if (Info->isRestrictExpansion() &&
3071 !SourceMgr.isInMainFile(Identifier.getLocation()))
3072 emitRestrictExpansionWarning(Identifier);
3073
3074 if (!IsIfnDef) {
3075 if (Info->getName() == "INFINITY" && getLangOpts().NoHonorInfs)
3076 emitRestrictInfNaNWarning(Identifier, 0);
3077 if (Info->getName() == "NAN" && getLangOpts().NoHonorNaNs)
3078 emitRestrictInfNaNWarning(Identifier, 1);
3079 }
3080 }
3081
3083 const LangOptions &LangOpts,
3084 const TargetInfo &TI);
3085
3087 const PresumedLoc &PLoc,
3088 const LangOptions &LangOpts,
3089 const TargetInfo &TI);
3090
3091private:
3092 void emitMacroDeprecationWarning(const Token &Identifier) const;
3093 void emitRestrictExpansionWarning(const Token &Identifier) const;
3094 void emitFinalMacroWarning(const Token &Identifier, bool IsUndef) const;
3095 void emitRestrictInfNaNWarning(const Token &Identifier,
3096 unsigned DiagSelection) const;
3097
3098 /// This boolean state keeps track if the current scanned token (by this PP)
3099 /// is in an "-Wunsafe-buffer-usage" opt-out region. Assuming PP scans a
3100 /// translation unit in a linear order.
3101 bool InSafeBufferOptOutRegion = false;
3102
3103 /// Hold the start location of the current "-Wunsafe-buffer-usage" opt-out
3104 /// region if PP is currently in such a region. Hold undefined value
3105 /// otherwise.
3106 SourceLocation CurrentSafeBufferOptOutStart; // It is used to report the start location of an never-closed region.
3107
3108 using SafeBufferOptOutRegionsTy =
3110 // An ordered sequence of "-Wunsafe-buffer-usage" opt-out regions in this
3111 // translation unit. Each region is represented by a pair of start and
3112 // end locations.
3113 SafeBufferOptOutRegionsTy SafeBufferOptOutMap;
3114
3115 // The "-Wunsafe-buffer-usage" opt-out regions in loaded ASTs. We use the
3116 // following structure to manage them by their ASTs.
3117 struct {
3118 // A map from unique IDs to region maps of loaded ASTs. The ID identifies a
3119 // loaded AST. See `SourceManager::getUniqueLoadedASTID`.
3120 llvm::DenseMap<FileID, SafeBufferOptOutRegionsTy> LoadedRegions;
3121
3122 // Returns a reference to the safe buffer opt-out regions of the loaded
3123 // AST where `Loc` belongs to. (Construct if absent)
3124 SafeBufferOptOutRegionsTy &
3125 findAndConsLoadedOptOutMap(SourceLocation Loc, SourceManager &SrcMgr) {
3126 return LoadedRegions[SrcMgr.getUniqueLoadedASTFileID(Loc)];
3127 }
3128
3129 // Returns a reference to the safe buffer opt-out regions of the loaded
3130 // AST where `Loc` belongs to. (This const function returns nullptr if
3131 // absent.)
3132 const SafeBufferOptOutRegionsTy *
3133 lookupLoadedOptOutMap(SourceLocation Loc,
3134 const SourceManager &SrcMgr) const {
3135 FileID FID = SrcMgr.getUniqueLoadedASTFileID(Loc);
3136 auto Iter = LoadedRegions.find(FID);
3137
3138 if (Iter == LoadedRegions.end())
3139 return nullptr;
3140 return &Iter->getSecond();
3141 }
3142 } LoadedSafeBufferOptOutMap;
3143
3144public:
3145 /// \return true iff the given `Loc` is in a "-Wunsafe-buffer-usage" opt-out
3146 /// region. This `Loc` must be a source location that has been pre-processed.
3147 bool isSafeBufferOptOut(const SourceManager&SourceMgr, const SourceLocation &Loc) const;
3148
3149 /// Alter the state of whether this PP currently is in a
3150 /// "-Wunsafe-buffer-usage" opt-out region.
3151 ///
3152 /// \param isEnter true if this PP is entering a region; otherwise, this PP
3153 /// is exiting a region
3154 /// \param Loc the location of the entry or exit of a
3155 /// region
3156 /// \return true iff it is INVALID to enter or exit a region, i.e.,
3157 /// attempt to enter a region before exiting a previous region, or exiting a
3158 /// region that PP is not currently in.
3159 bool enterOrExitSafeBufferOptOutRegion(bool isEnter,
3160 const SourceLocation &Loc);
3161
3162 /// \return true iff this PP is currently in a "-Wunsafe-buffer-usage"
3163 /// opt-out region
3165
3166 /// \param StartLoc output argument. It will be set to the start location of
3167 /// the current "-Wunsafe-buffer-usage" opt-out region iff this function
3168 /// returns true.
3169 /// \return true iff this PP is currently in a "-Wunsafe-buffer-usage"
3170 /// opt-out region
3171 bool isPPInSafeBufferOptOutRegion(SourceLocation &StartLoc);
3172
3173 /// \return a sequence of SourceLocations representing ordered opt-out regions
3174 /// specified by
3175 /// `\#pragma clang unsafe_buffer_usage begin/end`s of this translation unit.
3176 SmallVector<SourceLocation, 64> serializeSafeBufferOptOutMap() const;
3177
3178 /// \param SrcLocSeqs a sequence of SourceLocations deserialized from a
3179 /// record of code `PP_UNSAFE_BUFFER_USAGE`.
3180 /// \return true iff the `Preprocessor` has been updated; false `Preprocessor`
3181 /// is same as itself before the call.
3183 const SmallVectorImpl<SourceLocation> &SrcLocSeqs);
3184
3185 /// Whether we've seen pp-directives which may have changed the preprocessing
3186 /// state.
3187 bool hasSeenNoTrivialPPDirective() const;
3188
3189private:
3190 /// Helper functions to forward lexing to the actual lexer. They all share the
3191 /// same signature.
3192 static bool CLK_Lexer(Preprocessor &P, Token &Result) {
3193 return P.CurLexer->Lex(Result);
3194 }
3195 static bool CLK_TokenLexer(Preprocessor &P, Token &Result) {
3196 return P.CurTokenLexer->Lex(Result);
3197 }
3198 static bool CLK_CachingLexer(Preprocessor &P, Token &Result) {
3199 P.CachingLex(Result);
3200 return true;
3201 }
3202 static bool CLK_DependencyDirectivesLexer(Preprocessor &P, Token &Result) {
3203 return P.CurLexer->LexDependencyDirectiveToken(Result);
3204 }
3205 static bool CLK_LexAfterModuleImport(Preprocessor &P, Token &Result) {
3206 return P.LexAfterModuleImport(Result);
3207 }
3208};
3209
3210/// Abstract base class that describes a handler that will receive
3211/// source ranges for each of the comments encountered in the source file.
3213public:
3215
3216 // The handler shall return true if it has pushed any tokens
3217 // to be read using e.g. EnterToken or EnterTokenStream.
3218 virtual bool HandleComment(Preprocessor &PP, SourceRange Comment) = 0;
3219};
3220
3221/// Abstract base class that describes a handler that will receive
3222/// source ranges for empty lines encountered in the source file.
3224public:
3226
3227 // The handler handles empty lines.
3228 virtual void HandleEmptyline(SourceRange Range) = 0;
3229};
3230
3231/// Helper class to shuttle information about #embed directives from the
3232/// preprocessor to the parser through an annotation token.
3234 StringRef BinaryData;
3235 StringRef FileName;
3236};
3237
3238/// Registry of pragma handlers added by plugins
3239using PragmaHandlerRegistry = llvm::Registry<PragmaHandler>;
3240
3241} // namespace clang
3242
3243namespace llvm {
3244extern template class CLANG_TEMPLATE_ABI Registry<clang::PragmaHandler>;
3245} // namespace llvm
3246
3247#endif // LLVM_CLANG_LEX_PREPROCESSOR_H
#define V(N, I)
Defines the Diagnostic-related interfaces.
Defines the Diagnostic IDs-related interfaces.
Token Tok
The Token.
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Defines the clang::LangOptions interface.
Defines the clang::MacroInfo and clang::MacroDirective classes.
Defines the clang::Module class, which describes a module in the source code.
#define SM(sm)
Defines the PPCallbacks interface.
Defines the clang::SourceLocation class and associated facilities.
Defines the SourceManager interface.
Defines the clang::TokenKind enum and support functions.
VerifyDiagnosticConsumer::Directive Directive
__device__ __2f16 float __ockl_bool s
Holds information about both target-independent and target-specific builtins, allowing easy queries b...
Definition Builtins.h:235
Callback handler that receives notifications when performing code completion within the preprocessor.
Abstract base class that describes a handler that will receive source ranges for each of the comments...
virtual bool HandleComment(Preprocessor &PP, SourceRange Comment)=0
A directive for a defined macro or a macro imported from a module.
Definition MacroInfo.h:432
Functor that returns the dependency directives for a given file.
A little helper class used to produce diagnostics.
Concrete class used by the front-end to report problems and issues.
Definition Diagnostic.h:232
void setSuppressAllDiagnostics(bool Val)
Suppress all diagnostics, to silence the front end when we know that we don't want any more diagnosti...
Definition Diagnostic.h:730
A reference to a DirectoryEntry that includes the name of the directory as it was accessed by the Fil...
Cached information about one directory (either on disk or in the virtual file system).
Abstract base class that describes a handler that will receive source ranges for empty lines encounte...
virtual void HandleEmptyline(SourceRange Range)=0
Record the previous 'export' keyword info.
ExportContextualKeywordInfo(const Token &Tok, bool AtPhysicalStartOfLine)
Abstract interface for external sources of preprocessor information.
A reference to a FileEntry that includes the name of the file as it was accessed by the FileManager's...
Definition FileEntry.h:57
Cached information about one file (either on disk or in the virtual file system).
Definition FileEntry.h:302
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Implements support for file system lookup, file system caching, and directory search management.
Definition FileManager.h:53
Encapsulates the information needed to find the file referenced by a #include or #include_next,...
One of these records is kept for each identifier that is lexed.
bool hadMacroDefinition() const
Returns true if this identifier was #defined to some value at any moment.
bool hasMacroDefinition() const
Return true if this identifier is #defined to some other value.
bool isDeprecatedMacro() const
bool isOutOfDate() const
Determine whether the information for this identifier is out of date with respect to the external sou...
StringRef getName() const
Return the actual identifier string.
bool isRestrictExpansion() const
A simple pair of identifier info and location.
Implements an efficient mapping from strings to IdentifierInfo nodes.
FPEvalMethodKind
Possible float expression evaluation method choices.
@ FEM_UnsetOnCommandLine
Used only for FE option processing; this is only used to indicate that the user did not specify an ex...
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
static StringRef getImmediateMacroName(SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts)
Retrieve the name of the immediate macro expansion.
Definition Lexer.cpp:1066
static bool isAtStartOfMacroExpansion(SourceLocation loc, const SourceManager &SM, const LangOptions &LangOpts, SourceLocation *MacroBegin=nullptr)
Returns true if the given MacroID location points at the first token of the macro expansion.
Definition Lexer.cpp:880
static SourceLocation AdvanceToTokenCharacter(SourceLocation TokStart, unsigned Characters, const SourceManager &SM, const LangOptions &LangOpts)
AdvanceToTokenCharacter - If the current SourceLocation specifies a location at the start of a token,...
Definition Lexer.h:399
static bool isAtEndOfMacroExpansion(SourceLocation loc, const SourceManager &SM, const LangOptions &LangOpts, SourceLocation *MacroEnd=nullptr)
Returns true if the given MacroID location points at the last token of the macro expansion.
Definition Lexer.cpp:902
static unsigned getSpelling(const Token &Tok, const char *&Buffer, const SourceManager &SourceMgr, const LangOptions &LangOpts, bool *Invalid=nullptr)
getSpelling - This method is used to get the spelling of a token into a preallocated buffer,...
Definition Lexer.cpp:461
static bool getRawToken(SourceLocation Loc, Token &Result, const SourceManager &SM, const LangOptions &LangOpts, bool IgnoreWhiteSpace=false)
Relex the token at the specified location.
Definition Lexer.cpp:519
static SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset, const SourceManager &SM, const LangOptions &LangOpts)
Computes the source location just past the end of the token at this source location.
Definition Lexer.cpp:858
MacroArgs - An instance of this class captures information about the formal arguments specified to a ...
Definition MacroArgs.h:30
A description of the current definition of a macro.
Definition MacroInfo.h:590
const DefMacroDirective * getDirective() const
Definition MacroInfo.h:375
Encapsulates changes to the "macros namespace" (the location where the macro name became active,...
Definition MacroInfo.h:313
Encapsulates the data about a macro definition (e.g.
Definition MacroInfo.h:39
SourceLocation getDefinitionLoc() const
Return the location that the macro was defined at.
Definition MacroInfo.h:125
Abstract interface for a module loader.
static std::string getFlatNameFromPath(ModuleIdPath Path)
Represents a macro directive exported by a module.
Definition MacroInfo.h:514
A header that is known to reside within a given module, whether it was included or excluded.
Definition ModuleMap.h:158
unsigned getNumIdentifierLocs() const
std::string str() const
SourceLocation getBeginLoc() const
SourceLocation getEndLoc() const
SourceRange getRange() const
ModuleIdPath getModuleIdPath() const
Describes a module or submodule.
Definition Module.h:144
bool isModuleMapModule() const
Definition Module.h:251
This interface provides a way to observe the actions of the preprocessor as it does its thing.
Definition PPCallbacks.h:37
PragmaHandler - Instances of this interface defined to handle the various pragmas that the language f...
Definition Pragma.h:65
PragmaNamespace - This PragmaHandler subdivides the namespace of pragmas, allowing hierarchical pragm...
Definition Pragma.h:96
A record of the steps taken while preprocessing a source file, including the various preprocessing di...
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
SourceLocation getLastFPEvalPragmaLocation() const
bool isMacroDefined(const IdentifierInfo *II)
MacroDirective * getLocalMacroDirective(const IdentifierInfo *II) const
Given an identifier, return its latest non-imported MacroDirective if it is #define'd and not #undef'...
bool markIncluded(FileEntryRef File)
Mark the file as included.
void HandlePragmaPushMacro(Token &Tok)
Handle #pragma push_macro.
Definition Pragma.cpp:633
void FinalizeForModelFile()
Cleanup after model file parsing.
bool FinishLexStringLiteral(Token &Result, std::string &String, const char *DiagnosticTag, bool AllowMacroExpansion)
Complete the lexing of a string literal where the first token has already been lexed (see LexStringLi...
void HandlePragmaPoison()
HandlePragmaPoison - Handle #pragma GCC poison. PoisonTok is the 'poison'.
Definition Pragma.cpp:438
void setCodeCompletionHandler(CodeCompletionHandler &Handler)
Set the code completion handler to the given object.
void dumpMacroInfo(const IdentifierInfo *II)
void HandlePragmaSystemHeader(Token &SysHeaderTok)
HandlePragmaSystemHeader - Implement #pragma GCC system_header.
Definition Pragma.cpp:480
bool creatingPCHWithThroughHeader()
True if creating a PCH with a through header.
void DumpToken(const Token &Tok, bool DumpFlags=false) const
Print the token to stderr, used for debugging.
void MaybeHandlePoisonedIdentifier(Token &Identifier)
ModuleMacro * addModuleMacro(Module *Mod, IdentifierInfo *II, MacroInfo *Macro, ArrayRef< ModuleMacro * > Overrides, bool &IsNew)
Register an exported macro for a module and identifier.
void setLoadedMacroDirective(IdentifierInfo *II, MacroDirective *ED, MacroDirective *MD)
Set a MacroDirective that was loaded from a PCH file.
MacroDefinition getMacroDefinitionAtLoc(const IdentifierInfo *II, SourceLocation Loc)
void EnterModuleSuffixTokenStream(ArrayRef< Token > Toks)
void markClangModuleAsAffecting(Module *M)
Mark the given clang module as affecting the current clang module or translation unit.
void setPragmaARCCFCodeAuditedInfo(IdentifierInfo *Ident, SourceLocation Loc)
Set the location of the currently-active #pragma clang arc_cf_code_audited begin.
void HandlePragmaModuleBuild(Token &Tok)
Definition Pragma.cpp:810
void InitializeForModelFile()
Initialize the preprocessor to parse a model file.
OptionalFileEntryRef LookupEmbedFile(StringRef Filename, bool isAngled, bool OpenFile, const FileEntry *LookupFromFile=nullptr)
Given a "Filename" or <Filename> reference, look up the indicated embed resource.
SourceLocation getCodeCompletionLoc() const
Returns the location of the code-completion point.
ArrayRef< ModuleMacro * > getLeafModuleMacros(const IdentifierInfo *II) const
Get the list of leaf (non-overridden) module macros for a name.
bool isIncrementalProcessingEnabled() const
Returns true if incremental processing is enabled.
void EnterToken(const Token &Tok, bool IsReinject)
Enters a token in the token stream to be lexed next.
void IgnorePragmas()
Install empty handlers for all pragmas (making them ignored).
Definition Pragma.cpp:2216
void HandleCXXImportDirective(Token Import)
HandleCXXImportDirective - Handle the C++ modules import directives.
DefMacroDirective * appendDefMacroDirective(IdentifierInfo *II, MacroInfo *MI)
PPCallbacks * getPPCallbacks() const
bool isInNamedInterfaceUnit() const
If we are proprocessing a named interface unit.
ArrayRef< PPConditionalInfo > getPreambleConditionalStack() const
void setPreambleRecordedPragmaAssumeNonNullLoc(SourceLocation Loc)
Record the location of the unterminated #pragma clang assume_nonnull begin in the preamble.
SourceRange DiscardUntilEndOfDirective(SmallVectorImpl< Token > *DiscardedToks=nullptr)
Read and discard all tokens remaining on the current line until the tok::eod token is found.
const MacroInfo * getMacroInfo(const IdentifierInfo *II) const
ArrayRef< BuildingSubmoduleInfo > getBuildingSubmodules() const
Get the list of submodules that we're currently building.
SourceLocation getCodeCompletionFileLoc() const
Returns the start location of the file of code-completion point.
DiagnosticBuilder Diag(const Token &Tok, unsigned DiagID) const
SourceRange getCodeCompletionTokenRange() const
SourceLocation getModuleImportLoc(Module *M) const
void overrideMaxTokens(unsigned Value, SourceLocation Loc)
void setCodeCompletionTokenRange(const SourceLocation Start, const SourceLocation End)
Set the code completion token range for detecting replacement range later on.
bool isRecordingPreamble() const
void HandleSkippedDirectiveWhileUsingPCH(Token &Result, SourceLocation HashLoc)
Process directives while skipping until the through header or pragma hdrstop is found.
void setRecordedPreambleConditionalStack(ArrayRef< PPConditionalInfo > s)
void enableIncrementalProcessing(bool value=true)
Enables the incremental processing.
bool LexAfterModuleImport(Token &Result)
Lex a token following the 'import' contextual keyword.
void TypoCorrectToken(const Token &Tok)
Update the current token to represent the provided identifier, in order to cache an action performed ...
bool GetSuppressIncludeNotFoundError()
bool isMacroDefinedInLocalModule(const IdentifierInfo *II, Module *M)
Determine whether II is defined as a macro within the module M, if that is a module that we've alread...
void setPragmaAssumeNonNullLoc(SourceLocation Loc)
Set the location of the currently-active #pragma clang assume_nonnull begin.
bool isInPrimaryFile() const
Return true if we're in the top-level file, not in a #include.
macro_iterator macro_begin(bool IncludeExternalMacros=true) const
void CreateString(StringRef Str, Token &Tok, SourceLocation ExpansionLocStart=SourceLocation(), SourceLocation ExpansionLocEnd=SourceLocation())
Plop the specified string into a scratch buffer and set the specified token's location and length to ...
void markMacroAsUsed(MacroInfo *MI)
A macro is used, update information about macros that need unused warnings.
LangOptions::FPEvalMethodKind getCurrentFPEvalMethod() const
void EnterSubmodule(Module *M, SourceLocation ImportLoc, bool ForPragma)
bool isSafeBufferOptOut(const SourceManager &SourceMgr, const SourceLocation &Loc) const
void addMacroDeprecationMsg(const IdentifierInfo *II, std::string Msg, SourceLocation AnnotationLoc)
const char * getCheckPoint(FileID FID, const char *Start) const
Returns a pointer into the given file's buffer that's guaranteed to be between tokens.
void addRestrictExpansionMsg(const IdentifierInfo *II, std::string Msg, SourceLocation AnnotationLoc)
IdentifierInfo * LookUpIdentifierInfo(Token &Identifier) const
Given a tok::raw_identifier token, look up the identifier information for the token and install it in...
MacroDirective * getLocalMacroDirectiveHistory(const IdentifierInfo *II) const
Given an identifier, return the latest non-imported macro directive for that identifier.
void setPreprocessedOutput(bool IsPreprocessedOutput)
Sets whether the preprocessor is responsible for producing output or if it is producing tokens to be ...
void addFinalLoc(const IdentifierInfo *II, SourceLocation AnnotationLoc)
bool IsPreviousCachedToken(const Token &Tok) const
Whether Tok is the most recent token (CachedLexPos - 1) in CachedTokens.
bool SawDateOrTime() const
Returns true if the preprocessor has seen a use of DATE or TIME in the file so far.
const TargetInfo * getAuxTargetInfo() const
void CommitBacktrackedTokens()
Disable the last EnableBacktrackAtThisPos call.
Definition PPCaching.cpp:56
friend class MacroArgs
void DumpMacro(const MacroInfo &MI) const
bool HandleEndOfTokenLexer(Token &Result)
Callback invoked when the current TokenLexer hits the end of its token stream.
void setDiagnostics(DiagnosticsEngine &D)
IncludedFilesSet & getIncludedFiles()
Get the set of included files.
void setCodeCompletionReached()
Note that we hit the code-completion point.
bool SetCodeCompletionPoint(FileEntryRef File, unsigned Line, unsigned Column)
Specify the point at which code-completion will be performed.
void AnnotateCachedTokens(const Token &Tok)
We notify the Preprocessor that if it is caching tokens (because backtrack is enabled) it should repl...
bool isPreprocessedOutput() const
Returns true if the preprocessor is responsible for generating output, false if it is producing token...
StringRef getNamedModuleName() const
Get the named module name we're preprocessing.
bool mightHavePendingAnnotationTokens()
Determine whether it's possible for a future call to Lex to produce an annotation token created by a ...
void Lex(Token &Result)
Lex the next token for this preprocessor.
void EnterTokenStream(ArrayRef< Token > Toks, bool DisableMacroExpansion, bool IsReinject)
const TranslationUnitKind TUKind
The kind of translation unit we are processing.
bool EnterSourceFile(FileID FID, ConstSearchDirIterator Dir, SourceLocation Loc, bool IsFirstIncludeOfFile=true)
Add a source file to the top of the include stack and start lexing tokens from it instead of the curr...
bool isParsingIfOrElifDirective() const
True if we are currently preprocessing a if or elif directive.
unsigned getNumDirectives() const
Retrieve the number of Directives that have been processed by the Preprocessor.
bool isInImplementationUnit() const
If we are implementing an implementation module unit.
void addCommentHandler(CommentHandler *Handler)
Add the specified comment handler to the preprocessor.
ModuleLoader & getModuleLoader() const
Retrieve the module loader associated with this preprocessor.
void LexNonComment(Token &Result)
Lex a token.
void removeCommentHandler(CommentHandler *Handler)
Remove the specified comment handler.
PreprocessorLexer * getCurrentLexer() const
Return the current lexer being lexed from.
bool LexOnOffSwitch(tok::OnOffSwitch &Result)
Lex an on-off-switch (C99 6.10.6p2) and verify that it is followed by EOD.
Definition Pragma.cpp:971
StringRef getCodeCompletionFilter()
Get the code completion token for filtering purposes.
void setMainFileDir(DirectoryEntryRef Dir)
Set the directory in which the main file should be considered to have been found, if it is not a real...
const IdentifierTable & getIdentifierTable() const
void HandlePragmaDependency(Token &DependencyTok)
HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah.
Definition Pragma.cpp:512
void HandlePoisonedIdentifier(Token &Identifier)
Display reason for poisoned identifier.
friend class ASTReader
void Backtrack()
Make Preprocessor re-lex the tokens that were lexed since EnableBacktrackAtThisPos() was previously c...
Definition PPCaching.cpp:66
bool isCurrentLexer(const PreprocessorLexer *L) const
Return true if we are lexing directly from the specified lexer.
bool HandleIdentifier(Token &Identifier)
Callback invoked when the lexer reads an identifier and has filled in the tokens IdentifierInfo membe...
void addPPCallbacks(std::unique_ptr< PPCallbacks > C)
bool enterOrExitSafeBufferOptOutRegion(bool isEnter, const SourceLocation &Loc)
Alter the state of whether this PP currently is in a "-Wunsafe-buffer-usage" opt-out region.
void IncrementPasteCounter(bool isFast)
Increment the counters for the number of token paste operations performed.
IdentifierLoc getPragmaARCCFCodeAuditedInfo() const
The location of the currently-active #pragma clang arc_cf_code_audited begin.
void EnterMainSourceFile()
Enter the specified FileID as the main source file, which implicitly adds the builtin defines etc.
void setReplayablePreambleConditionalStack(ArrayRef< PPConditionalInfo > s, std::optional< PreambleSkipInfo > SkipInfo)
const Token & LookAhead(unsigned N)
Peeks ahead N tokens and returns that token without consuming any tokens.
friend class VAOptDefinitionContext
const MacroAnnotations & getMacroAnnotations(const IdentifierInfo *II) const
IdentifierInfo * getIdentifierInfo(StringRef Name) const
Return information about the specified preprocessor identifier token.
uint8_t getSpellingOfSingleCharacterNumericConstant(const Token &Tok, bool *Invalid=nullptr) const
Given a Token Tok that is a numeric constant with length 1, return the value of constant as an unsign...
macro_iterator macro_end(bool IncludeExternalMacros=true) const
SourceManager & getSourceManager() const
bool isBacktrackEnabled() const
True if EnableBacktrackAtThisPos() was called and caching of tokens is on.
MacroDefinition getMacroDefinition(const IdentifierInfo *II)
bool CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef, bool *ShadowFlag=nullptr)
std::optional< PreambleSkipInfo > getPreambleSkipInfo() const
void setPreprocessToken(bool Preprocess)
bool isPreprocessedModuleFile() const
Whether the main file is preprocessed module file.
void SetPoisonReason(IdentifierInfo *II, unsigned DiagID)
Specifies the reason for poisoning an identifier.
void HandlePragmaOnce(Token &OnceTok)
HandlePragmaOnce - Handle #pragma once. OnceTok is the 'once'.
Definition Pragma.cpp:413
SourceLocation CheckEndOfDirective(StringRef DirType, bool EnableMacros=false, SmallVectorImpl< Token > *ExtraToks=nullptr)
Ensure that the next token is a tok::eod token.
EmptylineHandler * getEmptylineHandler() const
bool getCommentRetentionState() const
bool isMacroDefined(StringRef Id)
static bool checkModuleIsAvailable(const LangOptions &LangOpts, const TargetInfo &TargetInfo, const Module &M, DiagnosticsEngine &Diags)
Check that the given module is available, producing a diagnostic if not.
Module * getCurrentModuleImplementation()
Retrieves the module whose implementation we're current compiling, if any.
void SetMacroExpansionOnlyInDirectives()
Disables macro expansion everywhere except for preprocessor directives.
bool hasRecordedPreamble() const
SourceLocation AdvanceToTokenCharacter(SourceLocation TokStart, unsigned Char) const
Given a location that specifies the start of a token, return a new location that specifies a characte...
SourceLocation getPragmaAssumeNonNullLoc() const
The location of the currently-active #pragma clang assume_nonnull begin.
MacroMap::const_iterator macro_iterator
void createPreprocessingRecord()
Create a new preprocessing record, which will keep track of all macro expansions, macro definitions,...
SourceLocation SplitToken(SourceLocation TokLoc, unsigned Length)
Split the first Length characters out of the token starting at TokLoc and return a location pointing ...
bool isUnannotatedBacktrackEnabled() const
True if EnableBacktrackAtThisPos() was called and caching of unannotated tokens is on.
void EnterTokenStream(std::unique_ptr< Token[]> Toks, unsigned NumToks, bool DisableMacroExpansion, bool IsReinject)
void RevertCachedTokens(unsigned N)
When backtracking is enabled and tokens are cached, this allows to revert a specific number of tokens...
Module * getCurrentModule()
Retrieves the module that we're currently building, if any.
std::optional< std::uint64_t > getStdLibCxxVersion()
void RemovePragmaHandler(PragmaHandler *Handler)
unsigned getTokenCount() const
Get the number of tokens processed so far.
unsigned getMaxTokens() const
Get the max number of tokens before issuing a -Wmax-tokens warning.
SourceLocation getMaxTokensOverrideLoc() const
void makeModuleVisible(Module *M, SourceLocation Loc, bool IncludeExports=true)
bool hadModuleLoaderFatalFailure() const
static void processPathToFileName(SmallVectorImpl< char > &FileName, const PresumedLoc &PLoc, const LangOptions &LangOpts, const TargetInfo &TI)
void setCurrentFPEvalMethod(SourceLocation PragmaLoc, LangOptions::FPEvalMethodKind Val)
const TargetInfo & getTargetInfo() const
FileManager & getFileManager() const
bool LexHeaderName(Token &Result, bool AllowMacroExpansion=true)
Lex a token, forming a header-name token if possible.
std::string getSpelling(const Token &Tok, bool *Invalid=nullptr) const
Return the 'spelling' of the Tok token.
bool isPCHThroughHeader(const FileEntry *FE)
Returns true if the FileEntry is the PCH through header.
void DumpLocation(SourceLocation Loc) const
friend class VariadicMacroScopeGuard
Module * getCurrentLexerSubmodule() const
Return the submodule owning the file being lexed.
bool parseSimpleIntegerLiteral(Token &Tok, uint64_t &Value)
Parses a simple integer literal to get its numeric value.
MacroInfo * AllocateMacroInfo(SourceLocation L)
Allocate a new MacroInfo object with the provided SourceLocation.
void setDependencyDirectivesGetter(DependencyDirectivesGetter &Get)
void LexUnexpandedToken(Token &Result)
Just like Lex, but disables macro expansion of identifier tokens.
StringRef getImmediateMacroName(SourceLocation Loc)
Retrieve the name of the immediate macro expansion.
bool creatingPCHWithPragmaHdrStop()
True if creating a PCH with a pragma hdrstop.
bool alreadyIncluded(FileEntryRef File) const
Return true if this header has already been included.
llvm::iterator_range< macro_iterator > macros(bool IncludeExternalMacros=true) const
void Initialize(const TargetInfo &Target, const TargetInfo *AuxTarget=nullptr)
Initialize the preprocessor using information about the target.
FileID getPredefinesFileID() const
Returns the FileID for the preprocessor predefines.
void LexUnexpandedNonComment(Token &Result)
Like LexNonComment, but this disables macro expansion of identifier tokens.
void AddPragmaHandler(StringRef Namespace, PragmaHandler *Handler)
Add the specified pragma handler to this preprocessor.
Definition Pragma.cpp:918
llvm::BumpPtrAllocator & getPreprocessorAllocator()
ModuleMacro * getModuleMacro(Module *Mod, const IdentifierInfo *II)
StringRef getSpelling(SourceLocation loc, SmallVectorImpl< char > &buffer, bool *invalid=nullptr) const
Return the 'spelling' of the token at the given location; does not go up to the spelling location or ...
bool HandleComment(Token &result, SourceRange Comment)
bool isCodeCompletionEnabled() const
Determine if we are performing code completion.
bool GetIncludeFilenameSpelling(SourceLocation Loc, StringRef &Buffer)
Turn the specified lexer token into a fully checked and spelled filename, e.g.
PreprocessorLexer * getCurrentFileLexer() const
Return the current file lexer being lexed from.
HeaderSearch & getHeaderSearchInfo() const
void emitMacroExpansionWarnings(const Token &Identifier, bool IsIfnDef=false) const
bool setDeserializedSafeBufferOptOutMap(const SmallVectorImpl< SourceLocation > &SrcLocSeqs)
void HandlePragmaPopMacro(Token &Tok)
Handle #pragma pop_macro.
Definition Pragma.cpp:656
void ReplaceLastTokenWithAnnotation(const Token &Tok)
Replace the last token with an annotation token.
ExternalPreprocessorSource * getExternalSource() const
bool NeedsStdLibCxxWorkaroundBefore(std::uint64_t FixedVersion)
Module * LeaveSubmodule(bool ForPragma)
const std::string & getPredefines() const
Get the predefines for this processor.
void HandleDirective(Token &Result)
Callback invoked when the lexer sees a # token at the start of a line.
SmallVector< SourceLocation, 64 > serializeSafeBufferOptOutMap() const
CodeCompletionHandler * getCodeCompletionHandler() const
Retrieve the current code-completion handler.
void recomputeCurLexerKind()
Recompute the current lexer kind based on the CurLexer/ CurTokenLexer pointers.
void EnterAnnotationToken(SourceRange Range, tok::TokenKind Kind, void *AnnotationVal)
Enter an annotation token into the token stream.
void setTokenWatcher(llvm::unique_function< void(const clang::Token &)> F)
Register a function that would be called on each token in the final expanded token stream.
MacroInfo * getMacroInfo(const IdentifierInfo *II)
void setPredefines(std::string P)
Set the predefines for this Preprocessor.
OptionalFileEntryRef LookupFile(SourceLocation FilenameLoc, StringRef Filename, bool isAngled, ConstSearchDirIterator FromDir, const FileEntry *FromFile, ConstSearchDirIterator *CurDir, SmallVectorImpl< char > *SearchPath, SmallVectorImpl< char > *RelativePath, ModuleMap::KnownHeader *SuggestedModule, bool *IsMapped, bool *IsFrameworkFound, bool SkipCache=false, bool OpenFile=true, bool CacheFailures=true)
Given a "foo" or <foo> reference, look up the indicated file.
IdentifierTable & getIdentifierTable()
Builtin::Context & getBuiltinInfo()
void setSkipMainFilePreamble(unsigned Bytes, bool StartOfLine)
Instruct the preprocessor to skip part of the main source file.
const PreprocessorOptions & getPreprocessorOpts() const
Retrieve the preprocessor options used to initialize this preprocessor.
void ReplacePreviousCachedToken(ArrayRef< Token > NewToks)
Replace token in CachedLexPos - 1 in CachedTokens by the tokens in NewToks.
LangOptions::FPEvalMethodKind getTUFPEvalMethod() const
const LangOptions & getLangOpts() const
bool isImportingCXXNamedModules() const
If we're importing a standard C++20 Named Modules.
void setTUFPEvalMethod(LangOptions::FPEvalMethodKind Val)
void CodeCompleteIncludedFile(llvm::StringRef Dir, bool IsAngled)
Hook used by the lexer to invoke the "included file" code completion point.
void SetSuppressIncludeNotFoundError(bool Suppress)
static void processPathForFileMacro(SmallVectorImpl< char > &Path, const LangOptions &LangOpts, const TargetInfo &TI)
llvm::DenseMap< FileID, SafeBufferOptOutRegionsTy > LoadedRegions
bool isInNamedModule() const
If we are preprocessing a named module.
void EnableBacktrackAtThisPos(bool Unannotated=false)
From the point that this method is called, and until CommitBacktrackedTokens() or Backtrack() is call...
Definition PPCaching.cpp:34
void RemoveTopOfLexerStack()
Pop the current lexer/macro exp off the top of the lexer stack.
void PoisonSEHIdentifiers(bool Poison=true)
bool HandleModuleContextualKeyword(Token &Result, bool TokAtPhysicalStartOfLine)
Callback invoked when the lexer sees one of export, import or module token at the start of a line.
bool isAtStartOfMacroExpansion(SourceLocation loc, SourceLocation *MacroBegin=nullptr) const
Returns true if the given MacroID location points at the first token of the macro expansion.
size_t getTotalMemory() const
void setCounterValue(uint32_t V)
void setExternalSource(ExternalPreprocessorSource *Source)
void clearCodeCompletionHandler()
Clear out the code completion handler.
void AddPragmaHandler(PragmaHandler *Handler)
OptionalFileEntryRef getHeaderToIncludeForDiagnostics(SourceLocation IncLoc, SourceLocation MLoc)
We want to produce a diagnostic at location IncLoc concerning an unreachable effect at location MLoc ...
bool isCodeCompletionReached() const
Returns true if code-completion is enabled and we have hit the code-completion point.
IdentifierInfo * ParsePragmaPushOrPopMacro(Token &Tok)
ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
Definition Pragma.cpp:568
void LexTokensUntilEOF(std::vector< Token > *Tokens=nullptr)
Lex all tokens for this preprocessor until (and excluding) end of file.
bool getRawToken(SourceLocation Loc, Token &Result, bool IgnoreWhiteSpace=false)
Relex the token at the specified location.
bool isNextPPTokenOneOf(Ts... Ks) const
isNextPPTokenOneOf - Check whether the next pp-token is one of the specificed token kind.
bool usingPCHWithPragmaHdrStop()
True if using a PCH with a pragma hdrstop.
void CodeCompleteNaturalLanguage()
Hook used by the lexer to invoke the "natural language" code completion point.
void EndSourceFile()
Inform the preprocessor callbacks that processing is complete.
bool HandleEndOfFile(Token &Result, bool isEndOfMacro=false)
Callback invoked when the lexer hits the end of the current file.
void setPragmasEnabled(bool Enabled)
DefMacroDirective * appendDefMacroDirective(IdentifierInfo *II, MacroInfo *MI, SourceLocation Loc)
void SetCommentRetentionState(bool KeepComments, bool KeepMacroComments)
Control whether the preprocessor retains comments in output.
bool isAtEndOfMacroExpansion(SourceLocation loc, SourceLocation *MacroEnd=nullptr) const
Returns true if the given MacroID location points at the last token of the macro expansion.
SourceLocation getMainFileFirstPPTokenLoc() const
Get the start location of the first pp-token in main file.
void HandlePragmaMark(Token &MarkTok)
Definition Pragma.cpp:428
bool LexModuleNameContinue(Token &Tok, SourceLocation UseLoc, SmallVectorImpl< Token > &Suffix, SmallVectorImpl< IdentifierLoc > &Path, bool AllowMacroExpansion=true, bool IsPartition=false)
void CollectPPImportSuffix(SmallVectorImpl< Token > &Toks, bool StopUntilEOD=false)
Collect the tokens of a C++20 pp-import-suffix.
bool getPragmasEnabled() const
void HandlePragmaHdrstop(Token &Tok)
Definition Pragma.cpp:884
PreprocessingRecord * getPreprocessingRecord() const
Retrieve the preprocessing record, or NULL if there is no preprocessing record.
void setEmptylineHandler(EmptylineHandler *Handler)
Set empty line handler.
DiagnosticsEngine & getDiagnostics() const
void HandleCXXModuleDirective(Token Module)
HandleCXXModuleDirective - Handle C++ module declaration directives.
SourceLocation getLastCachedTokenLocation() const
Get the location of the last cached token, suitable for setting the end location of an annotation tok...
bool hasSeenNoTrivialPPDirective() const
Whether we've seen pp-directives which may have changed the preprocessing state.
llvm::DenseSet< const FileEntry * > IncludedFilesSet
unsigned getSpelling(const Token &Tok, const char *&Buffer, bool *Invalid=nullptr) const
Get the spelling of a token into a preallocated buffer, instead of as an std::string.
SelectorTable & getSelectorTable()
void RemovePragmaHandler(StringRef Namespace, PragmaHandler *Handler)
Remove the specific pragma handler from this preprocessor.
Definition Pragma.cpp:949
SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset=0)
Computes the source location just past the end of the token at this source location.
const llvm::SmallSetVector< Module *, 2 > & getAffectingClangModules() const
Get the set of top-level clang modules that affected preprocessing, but were not imported.
std::optional< LexEmbedParametersResult > LexEmbedParameters(Token &Current, bool ForHasEmbed)
Lex the parameters for an embed directive, returns nullopt on error.
const IncludedFilesSet & getIncludedFiles() const
StringRef getLastMacroWithSpelling(SourceLocation Loc, ArrayRef< TokenValue > Tokens) const
Return the name of the macro defined before Loc that has spelling Tokens.
void HandlePragmaIncludeAlias(Token &Tok)
Definition Pragma.cpp:691
Module * getModuleForLocation(SourceLocation Loc, bool AllowTextual)
Find the module that owns the source or header file that Loc points to.
uint32_t getCounterValue() const
void setCodeCompletionIdentifierInfo(IdentifierInfo *Filter)
Set the code completion token for filtering purposes.
SourceLocation getPreambleRecordedPragmaAssumeNonNullLoc() const
Get the location of the recorded unterminated #pragma clang assume_nonnull begin in the preamble,...
void EnterMacro(Token &Tok, SourceLocation ILEnd, MacroInfo *Macro, MacroArgs *Args)
Add a Macro to the top of the include stack and start lexing tokens from it instead of the current bu...
DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const
Forwarding function for diagnostics.
void SkipTokensWhileUsingPCH()
Skip tokens until after the include of the through header or until after a pragma hdrstop.
bool usingPCHWithThroughHeader()
True if using a PCH with a through header.
bool CollectPPImportSuffixAndEnterStream(SmallVectorImpl< Token > &Toks, bool StopUntilEOD=false)
void markMainFileAsPreprocessedModuleFile()
Mark the main file as a preprocessed module file, then the 'module' and 'import' directive recognitio...
bool LexStringLiteral(Token &Result, std::string &String, const char *DiagnosticTag, bool AllowMacroExpansion)
Lex a string literal, which may be the concatenation of multiple string literals and may even come fr...
void HandleMicrosoftCommentPaste(Token &Tok)
When the macro expander pastes together a comment (/##/) in Microsoft mode, this method handles updat...
Preprocessor(const PreprocessorOptions &PPOpts, DiagnosticsEngine &diags, const LangOptions &LangOpts, SourceManager &SM, HeaderSearch &Headers, ModuleLoader &TheModuleLoader, IdentifierInfoLookup *IILookup=nullptr, bool OwnsHeaderSearch=false, TranslationUnitKind TUKind=TU_Complete)
void appendMacroDirective(IdentifierInfo *II, MacroDirective *MD)
Add a directive to the macro directive history for this identifier.
Represents an unpacked "presumed" location which can be presented to the user.
ScratchBuffer - This class exposes a simple interface for the dynamic construction of tokens.
This table allows us to fully hide how we implement multi-keyword caching.
Encodes a location in the source.
This class handles loading and caching of source files into memory.
A trivial tuple used to represent a source range.
Exposes information about the current target.
Definition TargetInfo.h:226
TokenValue(IdentifierInfo *II)
TokenValue(tok::TokenKind Kind)
bool operator==(const Token &Tok) const
Token - This structure provides full information about a lexed token.
Definition Token.h:36
IdentifierInfo * getIdentifierInfo() const
Definition Token.h:195
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file.
Definition Token.h:140
#define bool
Definition gpuintrin.h:32
Public enums and private classes that are part of the SourceManager implementation.
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
Definition TokenKinds.h:25
OnOffSwitch
Defines the possible values of an on-off-switch (C99 6.10.6p2).
Definition TokenKinds.h:56
bool isLiteral(TokenKind K)
Return true if this is a "literal" kind, like a numeric constant, string, etc.
Definition TokenKinds.h:101
PPKeywordKind
Provides a namespace for preprocessor keywords which start with a '#' at the beginning of the line.
Definition TokenKinds.h:33
bool isAnnotation(TokenKind K)
Return true if this is any of tok::annot_* kinds.
The JSON file list parser is used to communicate input to InstallAPI.
CustomizableOptional< FileEntryRef > OptionalFileEntryRef
Definition FileEntry.h:208
llvm::Registry< PragmaHandler > PragmaHandlerRegistry
Registry of pragma handlers added by plugins.
ArrayRef< IdentifierLoc > ModuleIdPath
A sequence of identifier/location pairs used to describe a particular module or submodule,...
@ Conditional
A conditional (?:) operator.
Definition Sema.h:669
detail::SearchDirIteratorImpl< true > ConstSearchDirIterator
@ Create
'create' clause, allowed on Compute and Combined constructs, plus 'data', 'enter data',...
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
MacroUse
Context in which macro name is used.
@ Module
Module linkage, which indicates that the entity can be referred to from other translation units withi...
Definition Linkage.h:54
@ Result
The result type of a method or function.
Definition TypeBase.h:905
TranslationUnitKind
Describes the kind of translation unit being processed.
@ TU_Complete
The translation unit is a complete translation unit.
CustomizableOptional< DirectoryEntryRef > OptionalDirectoryEntryRef
U cast(CodeGen::Address addr)
Definition Address.h:327
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
Helper class to shuttle information about embed directives from the preprocessor to the parser throug...
Describes how and where the pragma was introduced.
Definition Pragma.h:51
PreambleSkipInfo(SourceLocation HashTokenLoc, SourceLocation IfTokenLoc, bool FoundNonSkipPortion, bool FoundElse, SourceLocation ElseLoc)