10b57cec5SDimitry Andric //===- PreprocessingRecord.h - Record of Preprocessing ----------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file defines the PreprocessingRecord class, which maintains a record
100b57cec5SDimitry Andric // of what occurred during preprocessing.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric
140b57cec5SDimitry Andric #ifndef LLVM_CLANG_LEX_PREPROCESSINGRECORD_H
150b57cec5SDimitry Andric #define LLVM_CLANG_LEX_PREPROCESSINGRECORD_H
160b57cec5SDimitry Andric
170b57cec5SDimitry Andric #include "clang/Basic/IdentifierTable.h"
180b57cec5SDimitry Andric #include "clang/Basic/LLVM.h"
190b57cec5SDimitry Andric #include "clang/Basic/SourceLocation.h"
200b57cec5SDimitry Andric #include "clang/Lex/PPCallbacks.h"
210b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
220b57cec5SDimitry Andric #include "llvm/ADT/PointerUnion.h"
230b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
240b57cec5SDimitry Andric #include "llvm/ADT/iterator.h"
250b57cec5SDimitry Andric #include "llvm/ADT/iterator_range.h"
260b57cec5SDimitry Andric #include "llvm/Support/Allocator.h"
270b57cec5SDimitry Andric #include "llvm/Support/Compiler.h"
280b57cec5SDimitry Andric #include <cassert>
290b57cec5SDimitry Andric #include <cstddef>
300b57cec5SDimitry Andric #include <iterator>
31bdd1243dSDimitry Andric #include <optional>
320b57cec5SDimitry Andric #include <utility>
330b57cec5SDimitry Andric #include <vector>
340b57cec5SDimitry Andric
350b57cec5SDimitry Andric namespace clang {
360b57cec5SDimitry Andric
370b57cec5SDimitry Andric class PreprocessingRecord;
380b57cec5SDimitry Andric
390b57cec5SDimitry Andric } // namespace clang
400b57cec5SDimitry Andric
410b57cec5SDimitry Andric /// Allocates memory within a Clang preprocessing record.
420b57cec5SDimitry Andric void *operator new(size_t bytes, clang::PreprocessingRecord &PR,
430b57cec5SDimitry Andric unsigned alignment = 8) noexcept;
440b57cec5SDimitry Andric
450b57cec5SDimitry Andric /// Frees memory allocated in a Clang preprocessing record.
460b57cec5SDimitry Andric void operator delete(void *ptr, clang::PreprocessingRecord &PR,
470b57cec5SDimitry Andric unsigned) noexcept;
480b57cec5SDimitry Andric
490b57cec5SDimitry Andric namespace clang {
500b57cec5SDimitry Andric
510b57cec5SDimitry Andric class IdentifierInfo;
520b57cec5SDimitry Andric class MacroInfo;
530b57cec5SDimitry Andric class SourceManager;
540b57cec5SDimitry Andric class Token;
550b57cec5SDimitry Andric
560b57cec5SDimitry Andric /// Base class that describes a preprocessed entity, which may be a
570b57cec5SDimitry Andric /// preprocessor directive or macro expansion.
580b57cec5SDimitry Andric class PreprocessedEntity {
590b57cec5SDimitry Andric public:
600b57cec5SDimitry Andric /// The kind of preprocessed entity an object describes.
610b57cec5SDimitry Andric enum EntityKind {
620b57cec5SDimitry Andric /// Indicates a problem trying to load the preprocessed entity.
630b57cec5SDimitry Andric InvalidKind,
640b57cec5SDimitry Andric
650b57cec5SDimitry Andric /// A macro expansion.
660b57cec5SDimitry Andric MacroExpansionKind,
670b57cec5SDimitry Andric
680b57cec5SDimitry Andric /// \defgroup Preprocessing directives
690b57cec5SDimitry Andric /// @{
700b57cec5SDimitry Andric
710b57cec5SDimitry Andric /// A macro definition.
720b57cec5SDimitry Andric MacroDefinitionKind,
730b57cec5SDimitry Andric
740b57cec5SDimitry Andric /// An inclusion directive, such as \c \#include, \c
750b57cec5SDimitry Andric /// \#import, or \c \#include_next.
760b57cec5SDimitry Andric InclusionDirectiveKind,
770b57cec5SDimitry Andric
780b57cec5SDimitry Andric /// @}
790b57cec5SDimitry Andric
800b57cec5SDimitry Andric FirstPreprocessingDirective = MacroDefinitionKind,
810b57cec5SDimitry Andric LastPreprocessingDirective = InclusionDirectiveKind
820b57cec5SDimitry Andric };
830b57cec5SDimitry Andric
840b57cec5SDimitry Andric private:
850b57cec5SDimitry Andric /// The kind of preprocessed entity that this object describes.
860b57cec5SDimitry Andric EntityKind Kind;
870b57cec5SDimitry Andric
880b57cec5SDimitry Andric /// The source range that covers this preprocessed entity.
890b57cec5SDimitry Andric SourceRange Range;
900b57cec5SDimitry Andric
910b57cec5SDimitry Andric protected:
920b57cec5SDimitry Andric friend class PreprocessingRecord;
930b57cec5SDimitry Andric
PreprocessedEntity(EntityKind Kind,SourceRange Range)940b57cec5SDimitry Andric PreprocessedEntity(EntityKind Kind, SourceRange Range)
950b57cec5SDimitry Andric : Kind(Kind), Range(Range) {}
960b57cec5SDimitry Andric
970b57cec5SDimitry Andric public:
980b57cec5SDimitry Andric /// Retrieve the kind of preprocessed entity stored in this object.
getKind()990b57cec5SDimitry Andric EntityKind getKind() const { return Kind; }
1000b57cec5SDimitry Andric
1010b57cec5SDimitry Andric /// Retrieve the source range that covers this entire preprocessed
1020b57cec5SDimitry Andric /// entity.
getSourceRange()1030b57cec5SDimitry Andric SourceRange getSourceRange() const LLVM_READONLY { return Range; }
1040b57cec5SDimitry Andric
1050b57cec5SDimitry Andric /// Returns true if there was a problem loading the preprocessed
1060b57cec5SDimitry Andric /// entity.
isInvalid()1070b57cec5SDimitry Andric bool isInvalid() const { return Kind == InvalidKind; }
1080b57cec5SDimitry Andric
1090b57cec5SDimitry Andric // Only allow allocation of preprocessed entities using the allocator
1100b57cec5SDimitry Andric // in PreprocessingRecord or by doing a placement new.
1110b57cec5SDimitry Andric void *operator new(size_t bytes, PreprocessingRecord &PR,
1120b57cec5SDimitry Andric unsigned alignment = 8) noexcept {
1130b57cec5SDimitry Andric return ::operator new(bytes, PR, alignment);
1140b57cec5SDimitry Andric }
1150b57cec5SDimitry Andric
new(size_t bytes,void * mem)1160b57cec5SDimitry Andric void *operator new(size_t bytes, void *mem) noexcept { return mem; }
1170b57cec5SDimitry Andric
delete(void * ptr,PreprocessingRecord & PR,unsigned alignment)1180b57cec5SDimitry Andric void operator delete(void *ptr, PreprocessingRecord &PR,
1190b57cec5SDimitry Andric unsigned alignment) noexcept {
1200b57cec5SDimitry Andric return ::operator delete(ptr, PR, alignment);
1210b57cec5SDimitry Andric }
1220b57cec5SDimitry Andric
delete(void *,std::size_t)1230b57cec5SDimitry Andric void operator delete(void *, std::size_t) noexcept {}
delete(void *,void *)1240b57cec5SDimitry Andric void operator delete(void *, void *) noexcept {}
1250b57cec5SDimitry Andric
1260b57cec5SDimitry Andric private:
1270b57cec5SDimitry Andric // Make vanilla 'new' and 'delete' illegal for preprocessed entities.
1280b57cec5SDimitry Andric void *operator new(size_t bytes) noexcept;
1290b57cec5SDimitry Andric void operator delete(void *data) noexcept;
1300b57cec5SDimitry Andric };
1310b57cec5SDimitry Andric
1320b57cec5SDimitry Andric /// Records the presence of a preprocessor directive.
1330b57cec5SDimitry Andric class PreprocessingDirective : public PreprocessedEntity {
1340b57cec5SDimitry Andric public:
PreprocessingDirective(EntityKind Kind,SourceRange Range)1350b57cec5SDimitry Andric PreprocessingDirective(EntityKind Kind, SourceRange Range)
1360b57cec5SDimitry Andric : PreprocessedEntity(Kind, Range) {}
1370b57cec5SDimitry Andric
1380b57cec5SDimitry Andric // Implement isa/cast/dyncast/etc.
classof(const PreprocessedEntity * PD)1390b57cec5SDimitry Andric static bool classof(const PreprocessedEntity *PD) {
1400b57cec5SDimitry Andric return PD->getKind() >= FirstPreprocessingDirective &&
1410b57cec5SDimitry Andric PD->getKind() <= LastPreprocessingDirective;
1420b57cec5SDimitry Andric }
1430b57cec5SDimitry Andric };
1440b57cec5SDimitry Andric
1450b57cec5SDimitry Andric /// Record the location of a macro definition.
1460b57cec5SDimitry Andric class MacroDefinitionRecord : public PreprocessingDirective {
1470b57cec5SDimitry Andric /// The name of the macro being defined.
1480b57cec5SDimitry Andric const IdentifierInfo *Name;
1490b57cec5SDimitry Andric
1500b57cec5SDimitry Andric public:
MacroDefinitionRecord(const IdentifierInfo * Name,SourceRange Range)1510b57cec5SDimitry Andric explicit MacroDefinitionRecord(const IdentifierInfo *Name,
1520b57cec5SDimitry Andric SourceRange Range)
1530b57cec5SDimitry Andric : PreprocessingDirective(MacroDefinitionKind, Range), Name(Name) {}
1540b57cec5SDimitry Andric
1550b57cec5SDimitry Andric /// Retrieve the name of the macro being defined.
getName()1560b57cec5SDimitry Andric const IdentifierInfo *getName() const { return Name; }
1570b57cec5SDimitry Andric
1580b57cec5SDimitry Andric /// Retrieve the location of the macro name in the definition.
getLocation()1590b57cec5SDimitry Andric SourceLocation getLocation() const { return getSourceRange().getBegin(); }
1600b57cec5SDimitry Andric
1610b57cec5SDimitry Andric // Implement isa/cast/dyncast/etc.
classof(const PreprocessedEntity * PE)1620b57cec5SDimitry Andric static bool classof(const PreprocessedEntity *PE) {
1630b57cec5SDimitry Andric return PE->getKind() == MacroDefinitionKind;
1640b57cec5SDimitry Andric }
1650b57cec5SDimitry Andric };
1660b57cec5SDimitry Andric
1670b57cec5SDimitry Andric /// Records the location of a macro expansion.
1680b57cec5SDimitry Andric class MacroExpansion : public PreprocessedEntity {
1690b57cec5SDimitry Andric /// The definition of this macro or the name of the macro if it is
1700b57cec5SDimitry Andric /// a builtin macro.
1710b57cec5SDimitry Andric llvm::PointerUnion<IdentifierInfo *, MacroDefinitionRecord *> NameOrDef;
1720b57cec5SDimitry Andric
1730b57cec5SDimitry Andric public:
MacroExpansion(IdentifierInfo * BuiltinName,SourceRange Range)1740b57cec5SDimitry Andric MacroExpansion(IdentifierInfo *BuiltinName, SourceRange Range)
1750b57cec5SDimitry Andric : PreprocessedEntity(MacroExpansionKind, Range),
1760b57cec5SDimitry Andric NameOrDef(BuiltinName) {}
1770b57cec5SDimitry Andric
MacroExpansion(MacroDefinitionRecord * Definition,SourceRange Range)1780b57cec5SDimitry Andric MacroExpansion(MacroDefinitionRecord *Definition, SourceRange Range)
1790b57cec5SDimitry Andric : PreprocessedEntity(MacroExpansionKind, Range), NameOrDef(Definition) {
1800b57cec5SDimitry Andric }
1810b57cec5SDimitry Andric
1820b57cec5SDimitry Andric /// True if it is a builtin macro.
isBuiltinMacro()1830b57cec5SDimitry Andric bool isBuiltinMacro() const { return NameOrDef.is<IdentifierInfo *>(); }
1840b57cec5SDimitry Andric
1850b57cec5SDimitry Andric /// The name of the macro being expanded.
getName()1860b57cec5SDimitry Andric const IdentifierInfo *getName() const {
1870b57cec5SDimitry Andric if (MacroDefinitionRecord *Def = getDefinition())
1880b57cec5SDimitry Andric return Def->getName();
1890b57cec5SDimitry Andric return NameOrDef.get<IdentifierInfo *>();
1900b57cec5SDimitry Andric }
1910b57cec5SDimitry Andric
1920b57cec5SDimitry Andric /// The definition of the macro being expanded. May return null if
1930b57cec5SDimitry Andric /// this is a builtin macro.
getDefinition()1940b57cec5SDimitry Andric MacroDefinitionRecord *getDefinition() const {
1950b57cec5SDimitry Andric return NameOrDef.dyn_cast<MacroDefinitionRecord *>();
1960b57cec5SDimitry Andric }
1970b57cec5SDimitry Andric
1980b57cec5SDimitry Andric // Implement isa/cast/dyncast/etc.
classof(const PreprocessedEntity * PE)1990b57cec5SDimitry Andric static bool classof(const PreprocessedEntity *PE) {
2000b57cec5SDimitry Andric return PE->getKind() == MacroExpansionKind;
2010b57cec5SDimitry Andric }
2020b57cec5SDimitry Andric };
2030b57cec5SDimitry Andric
2040b57cec5SDimitry Andric /// Record the location of an inclusion directive, such as an
2050b57cec5SDimitry Andric /// \c \#include or \c \#import statement.
2060b57cec5SDimitry Andric class InclusionDirective : public PreprocessingDirective {
2070b57cec5SDimitry Andric public:
2080b57cec5SDimitry Andric /// The kind of inclusion directives known to the
2090b57cec5SDimitry Andric /// preprocessor.
2100b57cec5SDimitry Andric enum InclusionKind {
2110b57cec5SDimitry Andric /// An \c \#include directive.
2120b57cec5SDimitry Andric Include,
2130b57cec5SDimitry Andric
2140b57cec5SDimitry Andric /// An Objective-C \c \#import directive.
2150b57cec5SDimitry Andric Import,
2160b57cec5SDimitry Andric
2170b57cec5SDimitry Andric /// A GNU \c \#include_next directive.
2180b57cec5SDimitry Andric IncludeNext,
2190b57cec5SDimitry Andric
2200b57cec5SDimitry Andric /// A Clang \c \#__include_macros directive.
2210b57cec5SDimitry Andric IncludeMacros
2220b57cec5SDimitry Andric };
2230b57cec5SDimitry Andric
2240b57cec5SDimitry Andric private:
2250b57cec5SDimitry Andric /// The name of the file that was included, as written in
2260b57cec5SDimitry Andric /// the source.
2270b57cec5SDimitry Andric StringRef FileName;
2280b57cec5SDimitry Andric
2290b57cec5SDimitry Andric /// Whether the file name was in quotation marks; otherwise, it was
2300b57cec5SDimitry Andric /// in angle brackets.
2315f757f3fSDimitry Andric LLVM_PREFERRED_TYPE(bool)
2320b57cec5SDimitry Andric unsigned InQuotes : 1;
2330b57cec5SDimitry Andric
2340b57cec5SDimitry Andric /// The kind of inclusion directive we have.
2350b57cec5SDimitry Andric ///
2360b57cec5SDimitry Andric /// This is a value of type InclusionKind.
2375f757f3fSDimitry Andric LLVM_PREFERRED_TYPE(InclusionKind)
2380b57cec5SDimitry Andric unsigned Kind : 2;
2390b57cec5SDimitry Andric
2400b57cec5SDimitry Andric /// Whether the inclusion directive was automatically turned into
2410b57cec5SDimitry Andric /// a module import.
2425f757f3fSDimitry Andric LLVM_PREFERRED_TYPE(bool)
2430b57cec5SDimitry Andric unsigned ImportedModule : 1;
2440b57cec5SDimitry Andric
2450b57cec5SDimitry Andric /// The file that was included.
246bdd1243dSDimitry Andric OptionalFileEntryRef File;
2470b57cec5SDimitry Andric
2480b57cec5SDimitry Andric public:
24981ad6265SDimitry Andric InclusionDirective(PreprocessingRecord &PPRec, InclusionKind Kind,
25081ad6265SDimitry Andric StringRef FileName, bool InQuotes, bool ImportedModule,
251bdd1243dSDimitry Andric OptionalFileEntryRef File, SourceRange Range);
2520b57cec5SDimitry Andric
2530b57cec5SDimitry Andric /// Determine what kind of inclusion directive this is.
getKind()2540b57cec5SDimitry Andric InclusionKind getKind() const { return static_cast<InclusionKind>(Kind); }
2550b57cec5SDimitry Andric
2560b57cec5SDimitry Andric /// Retrieve the included file name as it was written in the source.
getFileName()2570b57cec5SDimitry Andric StringRef getFileName() const { return FileName; }
2580b57cec5SDimitry Andric
2590b57cec5SDimitry Andric /// Determine whether the included file name was written in quotes;
2600b57cec5SDimitry Andric /// otherwise, it was written in angle brackets.
wasInQuotes()2610b57cec5SDimitry Andric bool wasInQuotes() const { return InQuotes; }
2620b57cec5SDimitry Andric
2630b57cec5SDimitry Andric /// Determine whether the inclusion directive was automatically
2640b57cec5SDimitry Andric /// turned into a module import.
importedModule()2650b57cec5SDimitry Andric bool importedModule() const { return ImportedModule; }
2660b57cec5SDimitry Andric
2670b57cec5SDimitry Andric /// Retrieve the file entry for the actual file that was included
2680b57cec5SDimitry Andric /// by this directive.
getFile()269bdd1243dSDimitry Andric OptionalFileEntryRef getFile() const { return File; }
2700b57cec5SDimitry Andric
2710b57cec5SDimitry Andric // Implement isa/cast/dyncast/etc.
classof(const PreprocessedEntity * PE)2720b57cec5SDimitry Andric static bool classof(const PreprocessedEntity *PE) {
2730b57cec5SDimitry Andric return PE->getKind() == InclusionDirectiveKind;
2740b57cec5SDimitry Andric }
2750b57cec5SDimitry Andric };
2760b57cec5SDimitry Andric
2770b57cec5SDimitry Andric /// An abstract class that should be subclassed by any external source
2780b57cec5SDimitry Andric /// of preprocessing record entries.
2790b57cec5SDimitry Andric class ExternalPreprocessingRecordSource {
2800b57cec5SDimitry Andric public:
2810b57cec5SDimitry Andric virtual ~ExternalPreprocessingRecordSource();
2820b57cec5SDimitry Andric
2830b57cec5SDimitry Andric /// Read a preallocated preprocessed entity from the external source.
2840b57cec5SDimitry Andric ///
2850b57cec5SDimitry Andric /// \returns null if an error occurred that prevented the preprocessed
2860b57cec5SDimitry Andric /// entity from being loaded.
2870b57cec5SDimitry Andric virtual PreprocessedEntity *ReadPreprocessedEntity(unsigned Index) = 0;
2880b57cec5SDimitry Andric
2890b57cec5SDimitry Andric /// Returns a pair of [Begin, End) indices of preallocated
2900b57cec5SDimitry Andric /// preprocessed entities that \p Range encompasses.
2910b57cec5SDimitry Andric virtual std::pair<unsigned, unsigned>
2920b57cec5SDimitry Andric findPreprocessedEntitiesInRange(SourceRange Range) = 0;
2930b57cec5SDimitry Andric
2940b57cec5SDimitry Andric /// Optionally returns true or false if the preallocated preprocessed
2950b57cec5SDimitry Andric /// entity with index \p Index came from file \p FID.
isPreprocessedEntityInFileID(unsigned Index,FileID FID)296bdd1243dSDimitry Andric virtual std::optional<bool> isPreprocessedEntityInFileID(unsigned Index,
2970b57cec5SDimitry Andric FileID FID) {
298bdd1243dSDimitry Andric return std::nullopt;
2990b57cec5SDimitry Andric }
3000b57cec5SDimitry Andric
3010b57cec5SDimitry Andric /// Read a preallocated skipped range from the external source.
3020b57cec5SDimitry Andric virtual SourceRange ReadSkippedRange(unsigned Index) = 0;
3030b57cec5SDimitry Andric };
3040b57cec5SDimitry Andric
3050b57cec5SDimitry Andric /// A record of the steps taken while preprocessing a source file,
3060b57cec5SDimitry Andric /// including the various preprocessing directives processed, macros
3070b57cec5SDimitry Andric /// expanded, etc.
3080b57cec5SDimitry Andric class PreprocessingRecord : public PPCallbacks {
3090b57cec5SDimitry Andric SourceManager &SourceMgr;
3100b57cec5SDimitry Andric
3110b57cec5SDimitry Andric /// Allocator used to store preprocessing objects.
3120b57cec5SDimitry Andric llvm::BumpPtrAllocator BumpAlloc;
3130b57cec5SDimitry Andric
3140b57cec5SDimitry Andric /// The set of preprocessed entities in this record, in order they
3150b57cec5SDimitry Andric /// were seen.
3160b57cec5SDimitry Andric std::vector<PreprocessedEntity *> PreprocessedEntities;
3170b57cec5SDimitry Andric
3180b57cec5SDimitry Andric /// The set of preprocessed entities in this record that have been
3190b57cec5SDimitry Andric /// loaded from external sources.
3200b57cec5SDimitry Andric ///
3210b57cec5SDimitry Andric /// The entries in this vector are loaded lazily from the external source,
3220b57cec5SDimitry Andric /// and are referenced by the iterator using negative indices.
3230b57cec5SDimitry Andric std::vector<PreprocessedEntity *> LoadedPreprocessedEntities;
3240b57cec5SDimitry Andric
3250b57cec5SDimitry Andric /// The set of ranges that were skipped by the preprocessor,
3260b57cec5SDimitry Andric std::vector<SourceRange> SkippedRanges;
3270b57cec5SDimitry Andric
3280b57cec5SDimitry Andric bool SkippedRangesAllLoaded = true;
3290b57cec5SDimitry Andric
3300b57cec5SDimitry Andric /// Global (loaded or local) ID for a preprocessed entity.
3310b57cec5SDimitry Andric /// Negative values are used to indicate preprocessed entities
3320b57cec5SDimitry Andric /// loaded from the external source while non-negative values are used to
3330b57cec5SDimitry Andric /// indicate preprocessed entities introduced by the current preprocessor.
3340b57cec5SDimitry Andric /// Value -1 corresponds to element 0 in the loaded entities vector,
3350b57cec5SDimitry Andric /// value -2 corresponds to element 1 in the loaded entities vector, etc.
3360b57cec5SDimitry Andric /// Value 0 is an invalid value, the index to local entities is 1-based,
3370b57cec5SDimitry Andric /// value 1 corresponds to element 0 in the local entities vector,
3380b57cec5SDimitry Andric /// value 2 corresponds to element 1 in the local entities vector, etc.
3390b57cec5SDimitry Andric class PPEntityID {
3400b57cec5SDimitry Andric friend class PreprocessingRecord;
3410b57cec5SDimitry Andric
3420b57cec5SDimitry Andric int ID = 0;
3430b57cec5SDimitry Andric
PPEntityID(int ID)3440b57cec5SDimitry Andric explicit PPEntityID(int ID) : ID(ID) {}
3450b57cec5SDimitry Andric
3460b57cec5SDimitry Andric public:
3470b57cec5SDimitry Andric PPEntityID() = default;
3480b57cec5SDimitry Andric };
3490b57cec5SDimitry Andric
getPPEntityID(unsigned Index,bool isLoaded)3500b57cec5SDimitry Andric static PPEntityID getPPEntityID(unsigned Index, bool isLoaded) {
3510b57cec5SDimitry Andric return isLoaded ? PPEntityID(-int(Index)-1) : PPEntityID(Index+1);
3520b57cec5SDimitry Andric }
3530b57cec5SDimitry Andric
3540b57cec5SDimitry Andric /// Mapping from MacroInfo structures to their definitions.
3550b57cec5SDimitry Andric llvm::DenseMap<const MacroInfo *, MacroDefinitionRecord *> MacroDefinitions;
3560b57cec5SDimitry Andric
3570b57cec5SDimitry Andric /// External source of preprocessed entities.
3580b57cec5SDimitry Andric ExternalPreprocessingRecordSource *ExternalSource = nullptr;
3590b57cec5SDimitry Andric
3600b57cec5SDimitry Andric /// Retrieve the preprocessed entity at the given ID.
3610b57cec5SDimitry Andric PreprocessedEntity *getPreprocessedEntity(PPEntityID PPID);
3620b57cec5SDimitry Andric
3630b57cec5SDimitry Andric /// Retrieve the loaded preprocessed entity at the given index.
3640b57cec5SDimitry Andric PreprocessedEntity *getLoadedPreprocessedEntity(unsigned Index);
3650b57cec5SDimitry Andric
3660b57cec5SDimitry Andric /// Determine the number of preprocessed entities that were
3670b57cec5SDimitry Andric /// loaded (or can be loaded) from an external source.
getNumLoadedPreprocessedEntities()3680b57cec5SDimitry Andric unsigned getNumLoadedPreprocessedEntities() const {
3690b57cec5SDimitry Andric return LoadedPreprocessedEntities.size();
3700b57cec5SDimitry Andric }
3710b57cec5SDimitry Andric
3720b57cec5SDimitry Andric /// Returns a pair of [Begin, End) indices of local preprocessed
3730b57cec5SDimitry Andric /// entities that \p Range encompasses.
3740b57cec5SDimitry Andric std::pair<unsigned, unsigned>
3750b57cec5SDimitry Andric findLocalPreprocessedEntitiesInRange(SourceRange Range) const;
3760b57cec5SDimitry Andric unsigned findBeginLocalPreprocessedEntity(SourceLocation Loc) const;
3770b57cec5SDimitry Andric unsigned findEndLocalPreprocessedEntity(SourceLocation Loc) const;
3780b57cec5SDimitry Andric
3790b57cec5SDimitry Andric /// Allocate space for a new set of loaded preprocessed entities.
3800b57cec5SDimitry Andric ///
3810b57cec5SDimitry Andric /// \returns The index into the set of loaded preprocessed entities, which
3820b57cec5SDimitry Andric /// corresponds to the first newly-allocated entity.
3830b57cec5SDimitry Andric unsigned allocateLoadedEntities(unsigned NumEntities);
3840b57cec5SDimitry Andric
3850b57cec5SDimitry Andric /// Allocate space for a new set of loaded preprocessed skipped
3860b57cec5SDimitry Andric /// ranges.
3870b57cec5SDimitry Andric ///
3880b57cec5SDimitry Andric /// \returns The index into the set of loaded preprocessed ranges, which
3890b57cec5SDimitry Andric /// corresponds to the first newly-allocated range.
3900b57cec5SDimitry Andric unsigned allocateSkippedRanges(unsigned NumRanges);
3910b57cec5SDimitry Andric
3920b57cec5SDimitry Andric /// Ensures that all external skipped ranges have been loaded.
3930b57cec5SDimitry Andric void ensureSkippedRangesLoaded();
3940b57cec5SDimitry Andric
3950b57cec5SDimitry Andric /// Register a new macro definition.
3960b57cec5SDimitry Andric void RegisterMacroDefinition(MacroInfo *Macro, MacroDefinitionRecord *Def);
3970b57cec5SDimitry Andric
3980b57cec5SDimitry Andric public:
3990b57cec5SDimitry Andric /// Construct a new preprocessing record.
4000b57cec5SDimitry Andric explicit PreprocessingRecord(SourceManager &SM);
4010b57cec5SDimitry Andric
4020b57cec5SDimitry Andric /// Allocate memory in the preprocessing record.
4030b57cec5SDimitry Andric void *Allocate(unsigned Size, unsigned Align = 8) {
4040b57cec5SDimitry Andric return BumpAlloc.Allocate(Size, Align);
4050b57cec5SDimitry Andric }
4060b57cec5SDimitry Andric
4070b57cec5SDimitry Andric /// Deallocate memory in the preprocessing record.
Deallocate(void * Ptr)4080b57cec5SDimitry Andric void Deallocate(void *Ptr) {}
4090b57cec5SDimitry Andric
4100b57cec5SDimitry Andric size_t getTotalMemory() const;
4110b57cec5SDimitry Andric
getSourceManager()4120b57cec5SDimitry Andric SourceManager &getSourceManager() const { return SourceMgr; }
4130b57cec5SDimitry Andric
4140b57cec5SDimitry Andric /// Iteration over the preprocessed entities.
4150b57cec5SDimitry Andric ///
4160b57cec5SDimitry Andric /// In a complete iteration, the iterator walks the range [-M, N),
4170b57cec5SDimitry Andric /// where negative values are used to indicate preprocessed entities
4180b57cec5SDimitry Andric /// loaded from the external source while non-negative values are used to
4190b57cec5SDimitry Andric /// indicate preprocessed entities introduced by the current preprocessor.
4200b57cec5SDimitry Andric /// However, to provide iteration in source order (for, e.g., chained
4210b57cec5SDimitry Andric /// precompiled headers), dereferencing the iterator flips the negative
4220b57cec5SDimitry Andric /// values (corresponding to loaded entities), so that position -M
4230b57cec5SDimitry Andric /// corresponds to element 0 in the loaded entities vector, position -M+1
4240b57cec5SDimitry Andric /// corresponds to element 1 in the loaded entities vector, etc. This
4250b57cec5SDimitry Andric /// gives us a reasonably efficient, source-order walk.
4260b57cec5SDimitry Andric ///
4270b57cec5SDimitry Andric /// We define this as a wrapping iterator around an int. The
4280b57cec5SDimitry Andric /// iterator_adaptor_base class forwards the iterator methods to basic
4290b57cec5SDimitry Andric /// integer arithmetic.
4300b57cec5SDimitry Andric class iterator : public llvm::iterator_adaptor_base<
4310b57cec5SDimitry Andric iterator, int, std::random_access_iterator_tag,
4320b57cec5SDimitry Andric PreprocessedEntity *, int, PreprocessedEntity *,
4330b57cec5SDimitry Andric PreprocessedEntity *> {
4340b57cec5SDimitry Andric friend class PreprocessingRecord;
4350b57cec5SDimitry Andric
4360b57cec5SDimitry Andric PreprocessingRecord *Self;
4370b57cec5SDimitry Andric
iterator(PreprocessingRecord * Self,int Position)4380b57cec5SDimitry Andric iterator(PreprocessingRecord *Self, int Position)
4390b57cec5SDimitry Andric : iterator::iterator_adaptor_base(Position), Self(Self) {}
4400b57cec5SDimitry Andric
4410b57cec5SDimitry Andric public:
iterator()4420b57cec5SDimitry Andric iterator() : iterator(nullptr, 0) {}
4430b57cec5SDimitry Andric
4440b57cec5SDimitry Andric PreprocessedEntity *operator*() const {
4450b57cec5SDimitry Andric bool isLoaded = this->I < 0;
4460b57cec5SDimitry Andric unsigned Index = isLoaded ?
4470b57cec5SDimitry Andric Self->LoadedPreprocessedEntities.size() + this->I : this->I;
4480b57cec5SDimitry Andric PPEntityID ID = Self->getPPEntityID(Index, isLoaded);
4490b57cec5SDimitry Andric return Self->getPreprocessedEntity(ID);
4500b57cec5SDimitry Andric }
4510b57cec5SDimitry Andric PreprocessedEntity *operator->() const { return **this; }
4520b57cec5SDimitry Andric };
4530b57cec5SDimitry Andric
4540b57cec5SDimitry Andric /// Begin iterator for all preprocessed entities.
begin()4550b57cec5SDimitry Andric iterator begin() {
4560b57cec5SDimitry Andric return iterator(this, -(int)LoadedPreprocessedEntities.size());
4570b57cec5SDimitry Andric }
4580b57cec5SDimitry Andric
4590b57cec5SDimitry Andric /// End iterator for all preprocessed entities.
end()4600b57cec5SDimitry Andric iterator end() {
4610b57cec5SDimitry Andric return iterator(this, PreprocessedEntities.size());
4620b57cec5SDimitry Andric }
4630b57cec5SDimitry Andric
4640b57cec5SDimitry Andric /// Begin iterator for local, non-loaded, preprocessed entities.
local_begin()4650b57cec5SDimitry Andric iterator local_begin() {
4660b57cec5SDimitry Andric return iterator(this, 0);
4670b57cec5SDimitry Andric }
4680b57cec5SDimitry Andric
4690b57cec5SDimitry Andric /// End iterator for local, non-loaded, preprocessed entities.
local_end()4700b57cec5SDimitry Andric iterator local_end() {
4710b57cec5SDimitry Andric return iterator(this, PreprocessedEntities.size());
4720b57cec5SDimitry Andric }
4730b57cec5SDimitry Andric
4740b57cec5SDimitry Andric /// iterator range for the given range of loaded
4750b57cec5SDimitry Andric /// preprocessed entities.
getIteratorsForLoadedRange(unsigned start,unsigned count)4760b57cec5SDimitry Andric llvm::iterator_range<iterator> getIteratorsForLoadedRange(unsigned start,
4770b57cec5SDimitry Andric unsigned count) {
4780b57cec5SDimitry Andric unsigned end = start + count;
4790b57cec5SDimitry Andric assert(end <= LoadedPreprocessedEntities.size());
4800b57cec5SDimitry Andric return llvm::make_range(
4810b57cec5SDimitry Andric iterator(this, int(start) - LoadedPreprocessedEntities.size()),
4820b57cec5SDimitry Andric iterator(this, int(end) - LoadedPreprocessedEntities.size()));
4830b57cec5SDimitry Andric }
4840b57cec5SDimitry Andric
4850b57cec5SDimitry Andric /// Returns a range of preprocessed entities that source range \p R
4860b57cec5SDimitry Andric /// encompasses.
4870b57cec5SDimitry Andric ///
4880b57cec5SDimitry Andric /// \param R the range to look for preprocessed entities.
4890b57cec5SDimitry Andric llvm::iterator_range<iterator>
4900b57cec5SDimitry Andric getPreprocessedEntitiesInRange(SourceRange R);
4910b57cec5SDimitry Andric
4920b57cec5SDimitry Andric /// Returns true if the preprocessed entity that \p PPEI iterator
4930b57cec5SDimitry Andric /// points to is coming from the file \p FID.
4940b57cec5SDimitry Andric ///
4950b57cec5SDimitry Andric /// Can be used to avoid implicit deserializations of preallocated
4960b57cec5SDimitry Andric /// preprocessed entities if we only care about entities of a specific file
4970b57cec5SDimitry Andric /// and not from files \#included in the range given at
4980b57cec5SDimitry Andric /// \see getPreprocessedEntitiesInRange.
4990b57cec5SDimitry Andric bool isEntityInFileID(iterator PPEI, FileID FID);
5000b57cec5SDimitry Andric
5010b57cec5SDimitry Andric /// Add a new preprocessed entity to this record.
5020b57cec5SDimitry Andric PPEntityID addPreprocessedEntity(PreprocessedEntity *Entity);
5030b57cec5SDimitry Andric
5040b57cec5SDimitry Andric /// Set the external source for preprocessed entities.
5050b57cec5SDimitry Andric void SetExternalSource(ExternalPreprocessingRecordSource &Source);
5060b57cec5SDimitry Andric
5070b57cec5SDimitry Andric /// Retrieve the external source for preprocessed entities.
getExternalSource()5080b57cec5SDimitry Andric ExternalPreprocessingRecordSource *getExternalSource() const {
5090b57cec5SDimitry Andric return ExternalSource;
5100b57cec5SDimitry Andric }
5110b57cec5SDimitry Andric
5120b57cec5SDimitry Andric /// Retrieve the macro definition that corresponds to the given
5130b57cec5SDimitry Andric /// \c MacroInfo.
5140b57cec5SDimitry Andric MacroDefinitionRecord *findMacroDefinition(const MacroInfo *MI);
5150b57cec5SDimitry Andric
5160b57cec5SDimitry Andric /// Retrieve all ranges that got skipped while preprocessing.
getSkippedRanges()5170b57cec5SDimitry Andric const std::vector<SourceRange> &getSkippedRanges() {
5180b57cec5SDimitry Andric ensureSkippedRangesLoaded();
5190b57cec5SDimitry Andric return SkippedRanges;
5200b57cec5SDimitry Andric }
5210b57cec5SDimitry Andric
5220b57cec5SDimitry Andric private:
5230b57cec5SDimitry Andric friend class ASTReader;
5240b57cec5SDimitry Andric friend class ASTWriter;
5250b57cec5SDimitry Andric
5260b57cec5SDimitry Andric void MacroExpands(const Token &Id, const MacroDefinition &MD,
5270b57cec5SDimitry Andric SourceRange Range, const MacroArgs *Args) override;
5280b57cec5SDimitry Andric void MacroDefined(const Token &Id, const MacroDirective *MD) override;
5290b57cec5SDimitry Andric void MacroUndefined(const Token &Id, const MacroDefinition &MD,
5300b57cec5SDimitry Andric const MacroDirective *Undef) override;
5310b57cec5SDimitry Andric void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
5320b57cec5SDimitry Andric StringRef FileName, bool IsAngled,
5330b57cec5SDimitry Andric CharSourceRange FilenameRange,
534bdd1243dSDimitry Andric OptionalFileEntryRef File, StringRef SearchPath,
5350b57cec5SDimitry Andric StringRef RelativePath, const Module *Imported,
5360b57cec5SDimitry Andric SrcMgr::CharacteristicKind FileType) override;
5370b57cec5SDimitry Andric void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
5380b57cec5SDimitry Andric const MacroDefinition &MD) override;
5390b57cec5SDimitry Andric void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
5400b57cec5SDimitry Andric const MacroDefinition &MD) override;
5410b57cec5SDimitry Andric
542fe6060f1SDimitry Andric using PPCallbacks::Elifdef;
543fe6060f1SDimitry Andric using PPCallbacks::Elifndef;
544fe6060f1SDimitry Andric void Elifdef(SourceLocation Loc, const Token &MacroNameTok,
545fe6060f1SDimitry Andric const MacroDefinition &MD) override;
546fe6060f1SDimitry Andric void Elifndef(SourceLocation Loc, const Token &MacroNameTok,
547fe6060f1SDimitry Andric const MacroDefinition &MD) override;
548fe6060f1SDimitry Andric
5490b57cec5SDimitry Andric /// Hook called whenever the 'defined' operator is seen.
5500b57cec5SDimitry Andric void Defined(const Token &MacroNameTok, const MacroDefinition &MD,
5510b57cec5SDimitry Andric SourceRange Range) override;
5520b57cec5SDimitry Andric
5530b57cec5SDimitry Andric void SourceRangeSkipped(SourceRange Range,
5540b57cec5SDimitry Andric SourceLocation EndifLoc) override;
5550b57cec5SDimitry Andric
5560b57cec5SDimitry Andric void addMacroExpansion(const Token &Id, const MacroInfo *MI,
5570b57cec5SDimitry Andric SourceRange Range);
5580b57cec5SDimitry Andric
5590b57cec5SDimitry Andric /// Cached result of the last \see getPreprocessedEntitiesInRange
5600b57cec5SDimitry Andric /// query.
5610b57cec5SDimitry Andric struct {
5620b57cec5SDimitry Andric SourceRange Range;
5630b57cec5SDimitry Andric std::pair<int, int> Result;
5640b57cec5SDimitry Andric } CachedRangeQuery;
5650b57cec5SDimitry Andric
5660b57cec5SDimitry Andric std::pair<int, int> getPreprocessedEntitiesInRangeSlow(SourceRange R);
5670b57cec5SDimitry Andric };
5680b57cec5SDimitry Andric
5690b57cec5SDimitry Andric } // namespace clang
5700b57cec5SDimitry Andric
new(size_t bytes,clang::PreprocessingRecord & PR,unsigned alignment)5710b57cec5SDimitry Andric inline void *operator new(size_t bytes, clang::PreprocessingRecord &PR,
5720b57cec5SDimitry Andric unsigned alignment) noexcept {
5730b57cec5SDimitry Andric return PR.Allocate(bytes, alignment);
5740b57cec5SDimitry Andric }
5750b57cec5SDimitry Andric
delete(void * ptr,clang::PreprocessingRecord & PR,unsigned)5760b57cec5SDimitry Andric inline void operator delete(void *ptr, clang::PreprocessingRecord &PR,
5770b57cec5SDimitry Andric unsigned) noexcept {
5780b57cec5SDimitry Andric PR.Deallocate(ptr);
5790b57cec5SDimitry Andric }
5800b57cec5SDimitry Andric
5810b57cec5SDimitry Andric #endif // LLVM_CLANG_LEX_PREPROCESSINGRECORD_H
582