Go to the documentation of this file.
14 #ifndef LLVM_CLANG_LEX_PREPROCESSINGRECORD_H
15 #define LLVM_CLANG_LEX_PREPROCESSINGRECORD_H
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/None.h"
23 #include "llvm/ADT/Optional.h"
24 #include "llvm/ADT/PointerUnion.h"
25 #include "llvm/ADT/StringRef.h"
26 #include "llvm/ADT/iterator.h"
27 #include "llvm/ADT/iterator_range.h"
28 #include "llvm/Support/Allocator.h"
29 #include "llvm/Support/Compiler.h"
38 class PreprocessingRecord;
44 unsigned alignment = 8) noexcept;
114 unsigned alignment = 8) noexcept {
115 return ::operator
new(
bytes, PR, alignment);
118 void *
operator new(
size_t bytes,
void *mem) noexcept {
return mem; }
121 unsigned alignment) noexcept {
122 return ::operator
delete(ptr, PR, alignment);
126 void operator delete(
void *,
void *) noexcept {}
130 void *
operator new(
size_t bytes) noexcept;
131 void operator delete(
void *data) noexcept;
173 llvm::PointerUnion<IdentifierInfo *, MacroDefinitionRecord *> NameOrDef;
178 NameOrDef(BuiltinName) {}
233 unsigned InQuotes : 1;
242 unsigned ImportedModule : 1;
249 StringRef FileName,
bool InQuotes,
bool ImportedModule,
290 virtual std::pair<unsigned, unsigned>
311 llvm::BumpPtrAllocator BumpAlloc;
315 std::vector<PreprocessedEntity *> PreprocessedEntities;
322 std::vector<PreprocessedEntity *> LoadedPreprocessedEntities;
325 std::vector<SourceRange> SkippedRanges;
327 bool SkippedRangesAllLoaded =
true;
343 explicit PPEntityID(
int ID) :
ID(
ID) {}
346 PPEntityID() =
default;
349 static PPEntityID getPPEntityID(
unsigned Index,
bool isLoaded) {
350 return isLoaded ? PPEntityID(-
int(Index)-1) : PPEntityID(Index+1);
354 llvm::DenseMap<const MacroInfo *, MacroDefinitionRecord *> MacroDefinitions;
367 unsigned getNumLoadedPreprocessedEntities()
const {
368 return LoadedPreprocessedEntities.size();
373 std::pair<unsigned, unsigned>
375 unsigned findBeginLocalPreprocessedEntity(
SourceLocation Loc)
const;
376 unsigned findEndLocalPreprocessedEntity(
SourceLocation Loc)
const;
382 unsigned allocateLoadedEntities(
unsigned NumEntities);
389 unsigned allocateSkippedRanges(
unsigned NumRanges);
392 void ensureSkippedRangesLoaded();
402 void *
Allocate(
unsigned Size,
unsigned Align = 8) {
403 return BumpAlloc.Allocate(Size, Align);
429 class iterator :
public llvm::iterator_adaptor_base<
430 iterator, int, std::random_access_iterator_tag,
431 PreprocessedEntity *, int, PreprocessedEntity *,
432 PreprocessedEntity *> {
438 :
iterator::iterator_adaptor_base(Position), Self(Self) {}
444 bool isLoaded = this->I < 0;
445 unsigned Index = isLoaded ?
446 Self->LoadedPreprocessedEntities.size() + this->I : this->I;
447 PPEntityID
ID = Self->getPPEntityID(Index, isLoaded);
448 return Self->getPreprocessedEntity(
ID);
455 return iterator(
this, -(
int)LoadedPreprocessedEntities.size());
460 return iterator(
this, PreprocessedEntities.size());
470 return iterator(
this, PreprocessedEntities.size());
477 unsigned end = start + count;
478 assert(
end <= LoadedPreprocessedEntities.size());
479 return llvm::make_range(
480 iterator(
this,
int(start) - LoadedPreprocessedEntities.size()),
481 iterator(
this,
int(
end) - LoadedPreprocessedEntities.size()));
488 llvm::iterator_range<iterator>
508 return ExternalSource;
517 ensureSkippedRangesLoaded();
518 return SkippedRanges;
531 StringRef FileName,
bool IsAngled,
534 StringRef RelativePath,
const Module *Imported,
565 std::pair<int, int> getPreprocessedEntitiesInRangeSlow(
SourceRange R);
571 unsigned alignment) noexcept {
572 return PR.Allocate(
bytes, alignment);
580 #endif // LLVM_CLANG_LEX_PREPROCESSINGRECORD_H
Records the presence of a preprocessor directive.
void SetExternalSource(ExternalPreprocessingRecordSource &Source)
Set the external source for preprocessed entities.
bool isInvalid() const
Returns true if there was a problem loading the preprocessed entity.
PreprocessingDirective(EntityKind Kind, SourceRange Range)
@ LastPreprocessingDirective
An abstract class that should be subclassed by any external source of preprocessing record entries.
size_t getTotalMemory() const
virtual PreprocessedEntity * ReadPreprocessedEntity(unsigned Index)=0
Read a preallocated preprocessed entity from the external source.
A trivial tuple used to represent a source range.
virtual std::pair< unsigned, unsigned > findPreprocessedEntitiesInRange(SourceRange Range)=0
Returns a pair of [Begin, End) indices of preallocated preprocessed entities that Range encompasses.
MacroDefinitionRecord * getDefinition() const
The definition of the macro being expanded.
A description of the current definition of a macro.
iterator local_end()
End iterator for local, non-loaded, preprocessed entities.
Encodes a location in the source.
This interface provides a way to observe the actions of the preprocessor as it does its thing.
SourceLocation getBegin() const
llvm::iterator_range< iterator > getPreprocessedEntitiesInRange(SourceRange R)
Returns a range of preprocessed entities that source range R encompasses.
MacroArgs - An instance of this class captures information about the formal arguments specified to a ...
MacroExpansion(MacroDefinitionRecord *Definition, SourceRange Range)
Records the location of a macro expansion.
A record of the steps taken while preprocessing a source file, including the various preprocessing di...
Encapsulates the data about a macro definition (e.g.
CharacteristicKind
Indicates whether a file or directory holds normal user code, system code, or system code which is im...
@ MacroExpansionKind
A macro expansion.
PreprocessingRecord(SourceManager &SM)
Construct a new preprocessing record.
Token - This structure provides full information about a lexed token.
MacroExpansion(IdentifierInfo *BuiltinName, SourceRange Range)
@ MacroDefinitionKind
A macro definition.
@ InclusionDirectiveKind
An inclusion directive, such as #include, #import, or #include_next.
This class handles loading and caching of source files into memory.
SourceManager & getSourceManager() const
Optional< FileEntryRef > getFile() const
Retrieve the file entry for the actual file that was included by this directive.
MacroDefinitionRecord(const IdentifierInfo *Name, SourceRange Range)
__SIZE_TYPE__ size_t
The unsigned integer type of the result of the sizeof operator.
Iteration over the preprocessed entities.
SourceRange getSourceRange() const LLVM_READONLY
Retrieve the source range that covers this entire preprocessed entity.
InclusionKind
The kind of inclusion directives known to the preprocessor.
Describes a module or submodule.
void * Allocate(unsigned Size, unsigned Align=8)
Allocate memory in the preprocessing record.
virtual void Elifdef(SourceLocation Loc, const Token &MacroNameTok, const MacroDefinition &MD)
Hook called whenever an #elifdef branch is taken.
EntityKind
The kind of preprocessed entity an object describes.
virtual void Elifndef(SourceLocation Loc, const Token &MacroNameTok, const MacroDefinition &MD)
Hook called whenever an #elifndef branch is taken.
Record the location of a macro definition.
@ Include
An #include directive.
@ IncludeNext
A GNU #include_next directive.
llvm::iterator_range< iterator > getIteratorsForLoadedRange(unsigned start, unsigned count)
iterator range for the given range of loaded preprocessed entities.
Writes an AST file containing the contents of a translation unit.
@ Import
An Objective-C #import directive.
SourceLocation getLocation() const
Retrieve the location of the macro name in the definition.
std::pair< int, int > Result
bool wasInQuotes() const
Determine whether the included file name was written in quotes; otherwise, it was written in angle br...
Record the location of an inclusion directive, such as an #include or #import statement.
static bool classof(const PreprocessedEntity *PD)
EntityKind getKind() const
Retrieve the kind of preprocessed entity stored in this object.
bool isBuiltinMacro() const
True if it is a builtin macro.
static bool classof(const PreprocessedEntity *PE)
void Deallocate(void *Ptr)
Deallocate memory in the preprocessing record.
Represents a character-granular source range.
PreprocessedEntity * operator->() const
InclusionKind getKind() const
Determine what kind of inclusion directive this is.
@ IncludeMacros
A Clang #__include_macros directive.
iterator begin()
Begin iterator for all preprocessed entities.
One of these records is kept for each identifier that is lexed.
virtual Optional< bool > isPreprocessedEntityInFileID(unsigned Index, FileID FID)
Optionally returns true or false if the preallocated preprocessed entity with index Index came from f...
virtual SourceRange ReadSkippedRange(unsigned Index)=0
Read a preallocated skipped range from the external source.
static StringRef bytes(const std::vector< T, Allocator > &v)
Reads an AST files chain containing the contents of a translation unit.
MacroDefinitionRecord * findMacroDefinition(const MacroInfo *MI)
Retrieve the macro definition that corresponds to the given MacroInfo.
ExternalPreprocessingRecordSource * getExternalSource() const
Retrieve the external source for preprocessed entities.
StringRef getName() const
Return the actual identifier string.
iterator end()
End iterator for all preprocessed entities.
Encapsulates changes to the "macros namespace" (the location where the macro name became active,...
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
static bool classof(const PreprocessedEntity *PE)
PreprocessedEntity * operator*() const
@ InvalidKind
Indicates a problem trying to load the preprocessed entity.
bool isEntityInFileID(iterator PPEI, FileID FID)
Returns true if the preprocessed entity that PPEI iterator points to is coming from the file FID.
StringRef getFileName() const
Retrieve the included file name as it was written in the source.
const IdentifierInfo * getName() const
The name of the macro being expanded.
InclusionDirective(PreprocessingRecord &PPRec, InclusionKind Kind, StringRef FileName, bool InQuotes, bool ImportedModule, Optional< FileEntryRef > File, SourceRange Range)
PPEntityID addPreprocessedEntity(PreprocessedEntity *Entity)
Add a new preprocessed entity to this record.
const IdentifierInfo * getName() const
Retrieve the name of the macro being defined.
const std::vector< SourceRange > & getSkippedRanges()
Retrieve all ranges that got skipped while preprocessing.
bool importedModule() const
Determine whether the inclusion directive was automatically turned into a module import.
static bool classof(const PreprocessedEntity *PE)
@ FirstPreprocessingDirective
PreprocessedEntity(EntityKind Kind, SourceRange Range)
virtual ~ExternalPreprocessingRecordSource()
iterator local_begin()
Begin iterator for local, non-loaded, preprocessed entities.
Base class that describes a preprocessed entity, which may be a preprocessor directive or macro expan...