1 //===--- PreprocessingRecord.h - Record of Preprocessing --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the PreprocessingRecord class, which maintains a record
11 // of what occurred during preprocessing.
13 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_LEX_PREPROCESSINGRECORD_H
15 #define LLVM_CLANG_LEX_PREPROCESSINGRECORD_H
17 #include "clang/Basic/IdentifierTable.h"
18 #include "clang/Basic/SourceLocation.h"
19 #include "clang/Lex/PPCallbacks.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/Optional.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/Support/Allocator.h"
24 #include "llvm/Support/Compiler.h"
30 class PreprocessingRecord;
33 /// \brief Allocates memory within a Clang preprocessing record.
34 void* operator new(size_t bytes, clang::PreprocessingRecord& PR,
35 unsigned alignment = 8) throw();
37 /// \brief Frees memory allocated in a Clang preprocessing record.
38 void operator delete(void* ptr, clang::PreprocessingRecord& PR,
42 class MacroDefinition;
45 /// \brief Base class that describes a preprocessed entity, which may be a
46 /// preprocessor directive or macro expansion.
47 class PreprocessedEntity {
49 /// \brief The kind of preprocessed entity an object describes.
51 /// \brief Indicates a problem trying to load the preprocessed entity.
54 /// \brief A macro expansion.
57 /// \defgroup Preprocessing directives
60 /// \brief A macro definition.
63 /// \brief An inclusion directive, such as \c \#include, \c
64 /// \#import, or \c \#include_next.
65 InclusionDirectiveKind,
69 FirstPreprocessingDirective = MacroDefinitionKind,
70 LastPreprocessingDirective = InclusionDirectiveKind
74 /// \brief The kind of preprocessed entity that this object describes.
77 /// \brief The source range that covers this preprocessed entity.
81 PreprocessedEntity(EntityKind Kind, SourceRange Range)
82 : Kind(Kind), Range(Range) { }
84 friend class PreprocessingRecord;
87 /// \brief Retrieve the kind of preprocessed entity stored in this object.
88 EntityKind getKind() const { return Kind; }
90 /// \brief Retrieve the source range that covers this entire preprocessed
92 SourceRange getSourceRange() const LLVM_READONLY { return Range; }
94 /// \brief Returns true if there was a problem loading the preprocessed
96 bool isInvalid() const { return Kind == InvalidKind; }
98 // Only allow allocation of preprocessed entities using the allocator
99 // in PreprocessingRecord or by doing a placement new.
100 void* operator new(size_t bytes, PreprocessingRecord& PR,
101 unsigned alignment = 8) throw() {
102 return ::operator new(bytes, PR, alignment);
105 void* operator new(size_t bytes, void* mem) throw() {
109 void operator delete(void* ptr, PreprocessingRecord& PR,
110 unsigned alignment) throw() {
111 return ::operator delete(ptr, PR, alignment);
114 void operator delete(void*, std::size_t) throw() { }
115 void operator delete(void*, void*) throw() { }
118 // Make vanilla 'new' and 'delete' illegal for preprocessed entities.
119 void* operator new(size_t bytes) throw();
120 void operator delete(void* data) throw();
123 /// \brief Records the presence of a preprocessor directive.
124 class PreprocessingDirective : public PreprocessedEntity {
126 PreprocessingDirective(EntityKind Kind, SourceRange Range)
127 : PreprocessedEntity(Kind, Range) { }
129 // Implement isa/cast/dyncast/etc.
130 static bool classof(const PreprocessedEntity *PD) {
131 return PD->getKind() >= FirstPreprocessingDirective &&
132 PD->getKind() <= LastPreprocessingDirective;
136 /// \brief Record the location of a macro definition.
137 class MacroDefinition : public PreprocessingDirective {
138 /// \brief The name of the macro being defined.
139 const IdentifierInfo *Name;
142 explicit MacroDefinition(const IdentifierInfo *Name, SourceRange Range)
143 : PreprocessingDirective(MacroDefinitionKind, Range), Name(Name) { }
145 /// \brief Retrieve the name of the macro being defined.
146 const IdentifierInfo *getName() const { return Name; }
148 /// \brief Retrieve the location of the macro name in the definition.
149 SourceLocation getLocation() const { return getSourceRange().getBegin(); }
151 // Implement isa/cast/dyncast/etc.
152 static bool classof(const PreprocessedEntity *PE) {
153 return PE->getKind() == MacroDefinitionKind;
157 /// \brief Records the location of a macro expansion.
158 class MacroExpansion : public PreprocessedEntity {
159 /// \brief The definition of this macro or the name of the macro if it is
161 llvm::PointerUnion<IdentifierInfo *, MacroDefinition *> NameOrDef;
164 MacroExpansion(IdentifierInfo *BuiltinName, SourceRange Range)
165 : PreprocessedEntity(MacroExpansionKind, Range),
166 NameOrDef(BuiltinName) { }
168 MacroExpansion(MacroDefinition *Definition, SourceRange Range)
169 : PreprocessedEntity(MacroExpansionKind, Range),
170 NameOrDef(Definition) { }
172 /// \brief True if it is a builtin macro.
173 bool isBuiltinMacro() const { return NameOrDef.is<IdentifierInfo *>(); }
175 /// \brief The name of the macro being expanded.
176 const IdentifierInfo *getName() const {
177 if (MacroDefinition *Def = getDefinition())
178 return Def->getName();
179 return NameOrDef.get<IdentifierInfo*>();
182 /// \brief The definition of the macro being expanded. May return null if
183 /// this is a builtin macro.
184 MacroDefinition *getDefinition() const {
185 return NameOrDef.dyn_cast<MacroDefinition *>();
188 // Implement isa/cast/dyncast/etc.
189 static bool classof(const PreprocessedEntity *PE) {
190 return PE->getKind() == MacroExpansionKind;
194 /// \brief Record the location of an inclusion directive, such as an
195 /// \c \#include or \c \#import statement.
196 class InclusionDirective : public PreprocessingDirective {
198 /// \brief The kind of inclusion directives known to the
201 /// \brief An \c \#include directive.
203 /// \brief An Objective-C \c \#import directive.
205 /// \brief A GNU \c \#include_next directive.
207 /// \brief A Clang \c \#__include_macros directive.
212 /// \brief The name of the file that was included, as written in
216 /// \brief Whether the file name was in quotation marks; otherwise, it was
217 /// in angle brackets.
218 unsigned InQuotes : 1;
220 /// \brief The kind of inclusion directive we have.
222 /// This is a value of type InclusionKind.
225 /// \brief Whether the inclusion directive was automatically turned into
227 unsigned ImportedModule : 1;
229 /// \brief The file that was included.
230 const FileEntry *File;
233 InclusionDirective(PreprocessingRecord &PPRec,
234 InclusionKind Kind, StringRef FileName,
235 bool InQuotes, bool ImportedModule,
236 const FileEntry *File, SourceRange Range);
238 /// \brief Determine what kind of inclusion directive this is.
239 InclusionKind getKind() const { return static_cast<InclusionKind>(Kind); }
241 /// \brief Retrieve the included file name as it was written in the source.
242 StringRef getFileName() const { return FileName; }
244 /// \brief Determine whether the included file name was written in quotes;
245 /// otherwise, it was written in angle brackets.
246 bool wasInQuotes() const { return InQuotes; }
248 /// \brief Determine whether the inclusion directive was automatically
249 /// turned into a module import.
250 bool importedModule() const { return ImportedModule; }
252 /// \brief Retrieve the file entry for the actual file that was included
253 /// by this directive.
254 const FileEntry *getFile() const { return File; }
256 // Implement isa/cast/dyncast/etc.
257 static bool classof(const PreprocessedEntity *PE) {
258 return PE->getKind() == InclusionDirectiveKind;
262 /// \brief An abstract class that should be subclassed by any external source
263 /// of preprocessing record entries.
264 class ExternalPreprocessingRecordSource {
266 virtual ~ExternalPreprocessingRecordSource();
268 /// \brief Read a preallocated preprocessed entity from the external source.
270 /// \returns null if an error occurred that prevented the preprocessed
271 /// entity from being loaded.
272 virtual PreprocessedEntity *ReadPreprocessedEntity(unsigned Index) = 0;
274 /// \brief Returns a pair of [Begin, End) indices of preallocated
275 /// preprocessed entities that \p Range encompasses.
276 virtual std::pair<unsigned, unsigned>
277 findPreprocessedEntitiesInRange(SourceRange Range) = 0;
279 /// \brief Optionally returns true or false if the preallocated preprocessed
280 /// entity with index \p Index came from file \p FID.
281 virtual Optional<bool> isPreprocessedEntityInFileID(unsigned Index,
287 /// \brief A record of the steps taken while preprocessing a source file,
288 /// including the various preprocessing directives processed, macros
290 class PreprocessingRecord : public PPCallbacks {
291 SourceManager &SourceMgr;
293 /// \brief Allocator used to store preprocessing objects.
294 llvm::BumpPtrAllocator BumpAlloc;
296 /// \brief The set of preprocessed entities in this record, in order they
298 std::vector<PreprocessedEntity *> PreprocessedEntities;
300 /// \brief The set of preprocessed entities in this record that have been
301 /// loaded from external sources.
303 /// The entries in this vector are loaded lazily from the external source,
304 /// and are referenced by the iterator using negative indices.
305 std::vector<PreprocessedEntity *> LoadedPreprocessedEntities;
307 /// \brief Global (loaded or local) ID for a preprocessed entity.
308 /// Negative values are used to indicate preprocessed entities
309 /// loaded from the external source while non-negative values are used to
310 /// indicate preprocessed entities introduced by the current preprocessor.
311 /// Value -1 corresponds to element 0 in the loaded entities vector,
312 /// value -2 corresponds to element 1 in the loaded entities vector, etc.
313 /// Value 0 is an invalid value, the index to local entities is 1-based,
314 /// value 1 corresponds to element 0 in the local entities vector,
315 /// value 2 corresponds to element 1 in the local entities vector, etc.
318 explicit PPEntityID(int ID) : ID(ID) {}
319 friend class PreprocessingRecord;
321 PPEntityID() : ID(0) {}
324 static PPEntityID getPPEntityID(unsigned Index, bool isLoaded) {
325 return isLoaded ? PPEntityID(-int(Index)-1) : PPEntityID(Index+1);
328 /// \brief Mapping from MacroInfo structures to their definitions.
329 llvm::DenseMap<const MacroInfo *, MacroDefinition *> MacroDefinitions;
331 /// \brief External source of preprocessed entities.
332 ExternalPreprocessingRecordSource *ExternalSource;
334 /// \brief Retrieve the preprocessed entity at the given ID.
335 PreprocessedEntity *getPreprocessedEntity(PPEntityID PPID);
337 /// \brief Retrieve the loaded preprocessed entity at the given index.
338 PreprocessedEntity *getLoadedPreprocessedEntity(unsigned Index);
340 /// \brief Determine the number of preprocessed entities that were
341 /// loaded (or can be loaded) from an external source.
342 unsigned getNumLoadedPreprocessedEntities() const {
343 return LoadedPreprocessedEntities.size();
346 /// \brief Returns a pair of [Begin, End) indices of local preprocessed
347 /// entities that \p Range encompasses.
348 std::pair<unsigned, unsigned>
349 findLocalPreprocessedEntitiesInRange(SourceRange Range) const;
350 unsigned findBeginLocalPreprocessedEntity(SourceLocation Loc) const;
351 unsigned findEndLocalPreprocessedEntity(SourceLocation Loc) const;
353 /// \brief Allocate space for a new set of loaded preprocessed entities.
355 /// \returns The index into the set of loaded preprocessed entities, which
356 /// corresponds to the first newly-allocated entity.
357 unsigned allocateLoadedEntities(unsigned NumEntities);
359 /// \brief Register a new macro definition.
360 void RegisterMacroDefinition(MacroInfo *Macro, MacroDefinition *Def);
363 /// \brief Construct a new preprocessing record.
364 explicit PreprocessingRecord(SourceManager &SM);
366 /// \brief Allocate memory in the preprocessing record.
367 void *Allocate(unsigned Size, unsigned Align = 8) {
368 return BumpAlloc.Allocate(Size, Align);
371 /// \brief Deallocate memory in the preprocessing record.
372 void Deallocate(void *Ptr) { }
374 size_t getTotalMemory() const;
376 SourceManager &getSourceManager() const { return SourceMgr; }
378 // Iteration over the preprocessed entities.
380 PreprocessingRecord *Self;
382 /// \brief Position within the preprocessed entity sequence.
384 /// In a complete iteration, the Position field walks the range [-M, N),
385 /// where negative values are used to indicate preprocessed entities
386 /// loaded from the external source while non-negative values are used to
387 /// indicate preprocessed entities introduced by the current preprocessor.
388 /// However, to provide iteration in source order (for, e.g., chained
389 /// precompiled headers), dereferencing the iterator flips the negative
390 /// values (corresponding to loaded entities), so that position -M
391 /// corresponds to element 0 in the loaded entities vector, position -M+1
392 /// corresponds to element 1 in the loaded entities vector, etc. This
393 /// gives us a reasonably efficient, source-order walk.
397 typedef PreprocessedEntity *value_type;
398 typedef value_type& reference;
399 typedef value_type* pointer;
400 typedef std::random_access_iterator_tag iterator_category;
401 typedef int difference_type;
403 iterator() : Self(0), Position(0) { }
405 iterator(PreprocessingRecord *Self, int Position)
406 : Self(Self), Position(Position) { }
408 value_type operator*() const {
409 bool isLoaded = Position < 0;
410 unsigned Index = isLoaded ?
411 Self->LoadedPreprocessedEntities.size() + Position : Position;
412 PPEntityID ID = Self->getPPEntityID(Index, isLoaded);
413 return Self->getPreprocessedEntity(ID);
416 value_type operator[](difference_type D) {
420 iterator &operator++() {
425 iterator operator++(int) {
426 iterator Prev(*this);
431 iterator &operator--() {
436 iterator operator--(int) {
437 iterator Prev(*this);
442 friend bool operator==(const iterator &X, const iterator &Y) {
443 return X.Position == Y.Position;
446 friend bool operator!=(const iterator &X, const iterator &Y) {
447 return X.Position != Y.Position;
450 friend bool operator<(const iterator &X, const iterator &Y) {
451 return X.Position < Y.Position;
454 friend bool operator>(const iterator &X, const iterator &Y) {
455 return X.Position > Y.Position;
458 friend bool operator<=(const iterator &X, const iterator &Y) {
459 return X.Position < Y.Position;
462 friend bool operator>=(const iterator &X, const iterator &Y) {
463 return X.Position > Y.Position;
466 friend iterator& operator+=(iterator &X, difference_type D) {
471 friend iterator& operator-=(iterator &X, difference_type D) {
476 friend iterator operator+(iterator X, difference_type D) {
481 friend iterator operator+(difference_type D, iterator X) {
486 friend difference_type operator-(const iterator &X, const iterator &Y) {
487 return X.Position - Y.Position;
490 friend iterator operator-(iterator X, difference_type D) {
494 friend class PreprocessingRecord;
496 friend class iterator;
498 /// \brief Begin iterator for all preprocessed entities.
500 return iterator(this, -(int)LoadedPreprocessedEntities.size());
503 /// \brief End iterator for all preprocessed entities.
505 return iterator(this, PreprocessedEntities.size());
508 /// \brief Begin iterator for local, non-loaded, preprocessed entities.
509 iterator local_begin() {
510 return iterator(this, 0);
513 /// \brief End iterator for local, non-loaded, preprocessed entities.
514 iterator local_end() {
515 return iterator(this, PreprocessedEntities.size());
518 /// \brief begin/end iterator pair for the given range of loaded
519 /// preprocessed entities.
520 std::pair<iterator, iterator>
521 getIteratorsForLoadedRange(unsigned start, unsigned count) {
522 unsigned end = start + count;
523 assert(end <= LoadedPreprocessedEntities.size());
524 return std::make_pair(
525 iterator(this, int(start)-LoadedPreprocessedEntities.size()),
526 iterator(this, int(end)-LoadedPreprocessedEntities.size()));
529 /// \brief Returns a pair of [Begin, End) iterators of preprocessed entities
530 /// that source range \p R encompasses.
532 /// \param R the range to look for preprocessed entities.
534 std::pair<iterator, iterator> getPreprocessedEntitiesInRange(SourceRange R);
536 /// \brief Returns true if the preprocessed entity that \p PPEI iterator
537 /// points to is coming from the file \p FID.
539 /// Can be used to avoid implicit deserializations of preallocated
540 /// preprocessed entities if we only care about entities of a specific file
541 /// and not from files \#included in the range given at
542 /// \see getPreprocessedEntitiesInRange.
543 bool isEntityInFileID(iterator PPEI, FileID FID);
545 /// \brief Add a new preprocessed entity to this record.
546 PPEntityID addPreprocessedEntity(PreprocessedEntity *Entity);
548 /// \brief Set the external source for preprocessed entities.
549 void SetExternalSource(ExternalPreprocessingRecordSource &Source);
551 /// \brief Retrieve the external source for preprocessed entities.
552 ExternalPreprocessingRecordSource *getExternalSource() const {
553 return ExternalSource;
556 /// \brief Retrieve the macro definition that corresponds to the given
558 MacroDefinition *findMacroDefinition(const MacroInfo *MI);
561 virtual void MacroExpands(const Token &Id, const MacroDirective *MD,
562 SourceRange Range, const MacroArgs *Args);
563 virtual void MacroDefined(const Token &Id, const MacroDirective *MD);
564 virtual void MacroUndefined(const Token &Id, const MacroDirective *MD);
565 virtual void InclusionDirective(SourceLocation HashLoc,
566 const Token &IncludeTok,
569 CharSourceRange FilenameRange,
570 const FileEntry *File,
571 StringRef SearchPath,
572 StringRef RelativePath,
573 const Module *Imported);
574 virtual void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
575 const MacroDirective *MD);
576 virtual void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
577 const MacroDirective *MD);
578 /// \brief Hook called whenever the 'defined' operator is seen.
579 virtual void Defined(const Token &MacroNameTok, const MacroDirective *MD);
581 void addMacroExpansion(const Token &Id, const MacroInfo *MI,
584 /// \brief Cached result of the last \see getPreprocessedEntitiesInRange
588 std::pair<int, int> Result;
591 std::pair<int, int> getPreprocessedEntitiesInRangeSlow(SourceRange R);
593 friend class ASTReader;
594 friend class ASTWriter;
596 } // end namespace clang
598 inline void* operator new(size_t bytes, clang::PreprocessingRecord& PR,
599 unsigned alignment) throw() {
600 return PR.Allocate(bytes, alignment);
603 inline void operator delete(void* ptr, clang::PreprocessingRecord& PR,
608 #endif // LLVM_CLANG_LEX_PREPROCESSINGRECORD_H