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/Lex/PPCallbacks.h"
18 #include "clang/Basic/SourceLocation.h"
19 #include "clang/Basic/IdentifierTable.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/Support/Allocator.h"
26 class PreprocessingRecord;
29 /// \brief Allocates memory within a Clang preprocessing record.
30 void* operator new(size_t bytes, clang::PreprocessingRecord& PR,
31 unsigned alignment = 8) throw();
33 /// \brief Frees memory allocated in a Clang preprocessing record.
34 void operator delete(void* ptr, clang::PreprocessingRecord& PR,
38 class MacroDefinition;
41 /// \brief Base class that describes a preprocessed entity, which may be a
42 /// preprocessor directive or macro expansion.
43 class PreprocessedEntity {
45 /// \brief The kind of preprocessed entity an object describes.
47 /// \brief Indicates a problem trying to load the preprocessed entity.
50 /// \brief A macro expansion.
53 /// \defgroup Preprocessing directives
56 /// \brief A macro definition.
59 /// \brief An inclusion directive, such as \c #include, \c
60 /// #import, or \c #include_next.
61 InclusionDirectiveKind,
65 FirstPreprocessingDirective = MacroDefinitionKind,
66 LastPreprocessingDirective = InclusionDirectiveKind
70 /// \brief The kind of preprocessed entity that this object describes.
73 /// \brief The source range that covers this preprocessed entity.
77 PreprocessedEntity(EntityKind Kind, SourceRange Range)
78 : Kind(Kind), Range(Range) { }
80 friend class PreprocessingRecord;
83 /// \brief Retrieve the kind of preprocessed entity stored in this object.
84 EntityKind getKind() const { return Kind; }
86 /// \brief Retrieve the source range that covers this entire preprocessed
88 SourceRange getSourceRange() const { return Range; }
90 /// \brief Returns true if there was a problem loading the preprocessed
92 bool isInvalid() const { return Kind == InvalidKind; }
94 // Implement isa/cast/dyncast/etc.
95 static bool classof(const PreprocessedEntity *) { return true; }
97 // Only allow allocation of preprocessed entities using the allocator
98 // in PreprocessingRecord or by doing a placement new.
99 void* operator new(size_t bytes, PreprocessingRecord& PR,
100 unsigned alignment = 8) throw() {
101 return ::operator new(bytes, PR, alignment);
104 void* operator new(size_t bytes, void* mem) throw() {
108 void operator delete(void* ptr, PreprocessingRecord& PR,
109 unsigned alignment) throw() {
110 return ::operator delete(ptr, PR, alignment);
113 void operator delete(void*, std::size_t) throw() { }
114 void operator delete(void*, void*) throw() { }
117 // Make vanilla 'new' and 'delete' illegal for preprocessed entities.
118 void* operator new(size_t bytes) throw();
119 void operator delete(void* data) throw();
122 /// \brief Records the presence of a preprocessor directive.
123 class PreprocessingDirective : public PreprocessedEntity {
125 PreprocessingDirective(EntityKind Kind, SourceRange Range)
126 : PreprocessedEntity(Kind, Range) { }
128 // Implement isa/cast/dyncast/etc.
129 static bool classof(const PreprocessedEntity *PD) {
130 return PD->getKind() >= FirstPreprocessingDirective &&
131 PD->getKind() <= LastPreprocessingDirective;
133 static bool classof(const PreprocessingDirective *) { return true; }
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;
155 static bool classof(const MacroDefinition *) { return true; }
158 /// \brief Records the location of a macro expansion.
159 class MacroExpansion : public PreprocessedEntity {
160 /// \brief The definition of this macro or the name of the macro if it is
162 llvm::PointerUnion<IdentifierInfo *, MacroDefinition *> NameOrDef;
165 MacroExpansion(IdentifierInfo *BuiltinName, SourceRange Range)
166 : PreprocessedEntity(MacroExpansionKind, Range),
167 NameOrDef(BuiltinName) { }
169 MacroExpansion(MacroDefinition *Definition, SourceRange Range)
170 : PreprocessedEntity(MacroExpansionKind, Range),
171 NameOrDef(Definition) { }
173 /// \brief True if it is a builtin macro.
174 bool isBuiltinMacro() const { return NameOrDef.is<IdentifierInfo *>(); }
176 /// \brief The name of the macro being expanded.
177 const IdentifierInfo *getName() const {
178 if (MacroDefinition *Def = getDefinition())
179 return Def->getName();
180 return NameOrDef.get<IdentifierInfo*>();
183 /// \brief The definition of the macro being expanded. May return null if
184 /// this is a builtin macro.
185 MacroDefinition *getDefinition() const {
186 return NameOrDef.dyn_cast<MacroDefinition *>();
189 // Implement isa/cast/dyncast/etc.
190 static bool classof(const PreprocessedEntity *PE) {
191 return PE->getKind() == MacroExpansionKind;
193 static bool classof(const MacroExpansion *) { return true; }
196 /// \brief Record the location of an inclusion directive, such as an
197 /// \c #include or \c #import statement.
198 class InclusionDirective : public PreprocessingDirective {
200 /// \brief The kind of inclusion directives known to the
203 /// \brief An \c #include directive.
205 /// \brief An Objective-C \c #import directive.
207 /// \brief A GNU \c #include_next directive.
209 /// \brief A Clang \c #__include_macros directive.
214 /// \brief The name of the file that was included, as written in
218 /// \brief Whether the file name was in quotation marks; otherwise, it was
219 /// in angle brackets.
220 unsigned InQuotes : 1;
222 /// \brief The kind of inclusion directive we have.
224 /// This is a value of type InclusionKind.
227 /// \brief The file that was included.
228 const FileEntry *File;
231 InclusionDirective(PreprocessingRecord &PPRec,
232 InclusionKind Kind, StringRef FileName,
233 bool InQuotes, const FileEntry *File, SourceRange Range);
235 /// \brief Determine what kind of inclusion directive this is.
236 InclusionKind getKind() const { return static_cast<InclusionKind>(Kind); }
238 /// \brief Retrieve the included file name as it was written in the source.
239 StringRef getFileName() const { return FileName; }
241 /// \brief Determine whether the included file name was written in quotes;
242 /// otherwise, it was written in angle brackets.
243 bool wasInQuotes() const { return InQuotes; }
245 /// \brief Retrieve the file entry for the actual file that was included
246 /// by this directive.
247 const FileEntry *getFile() const { return File; }
249 // Implement isa/cast/dyncast/etc.
250 static bool classof(const PreprocessedEntity *PE) {
251 return PE->getKind() == InclusionDirectiveKind;
253 static bool classof(const InclusionDirective *) { return true; }
256 /// \brief An abstract class that should be subclassed by any external source
257 /// of preprocessing record entries.
258 class ExternalPreprocessingRecordSource {
260 virtual ~ExternalPreprocessingRecordSource();
262 /// \brief Read a preallocated preprocessed entity from the external source.
264 /// \returns null if an error occurred that prevented the preprocessed
265 /// entity from being loaded.
266 virtual PreprocessedEntity *ReadPreprocessedEntity(unsigned Index) = 0;
268 /// \brief Returns a pair of [Begin, End) indices of preallocated
269 /// preprocessed entities that \arg Range encompasses.
270 virtual std::pair<unsigned, unsigned>
271 findPreprocessedEntitiesInRange(SourceRange Range) = 0;
274 /// \brief A record of the steps taken while preprocessing a source file,
275 /// including the various preprocessing directives processed, macros
277 class PreprocessingRecord : public PPCallbacks {
278 SourceManager &SourceMgr;
280 /// \brief Whether we should include nested macro expansions in
281 /// the preprocessing record.
282 bool IncludeNestedMacroExpansions;
284 /// \brief Allocator used to store preprocessing objects.
285 llvm::BumpPtrAllocator BumpAlloc;
287 /// \brief The set of preprocessed entities in this record, in order they
289 std::vector<PreprocessedEntity *> PreprocessedEntities;
291 /// \brief The set of preprocessed entities in this record that have been
292 /// loaded from external sources.
294 /// The entries in this vector are loaded lazily from the external source,
295 /// and are referenced by the iterator using negative indices.
296 std::vector<PreprocessedEntity *> LoadedPreprocessedEntities;
298 /// \brief Global (loaded or local) ID for a preprocessed entity.
299 /// Negative values are used to indicate preprocessed entities
300 /// loaded from the external source while non-negative values are used to
301 /// indicate preprocessed entities introduced by the current preprocessor.
302 /// If M is the number of loaded preprocessed entities, value -M
303 /// corresponds to element 0 in the loaded entities vector, position -M+1
304 /// corresponds to element 1 in the loaded entities vector, etc.
305 typedef int PPEntityID;
307 PPEntityID getPPEntityID(unsigned Index, bool isLoaded) const {
308 return isLoaded ? PPEntityID(Index) - LoadedPreprocessedEntities.size()
312 /// \brief Mapping from MacroInfo structures to their definitions.
313 llvm::DenseMap<const MacroInfo *, PPEntityID> MacroDefinitions;
315 /// \brief External source of preprocessed entities.
316 ExternalPreprocessingRecordSource *ExternalSource;
318 /// \brief Retrieve the preprocessed entity at the given ID.
319 PreprocessedEntity *getPreprocessedEntity(PPEntityID PPID);
321 /// \brief Retrieve the loaded preprocessed entity at the given index.
322 PreprocessedEntity *getLoadedPreprocessedEntity(unsigned Index);
324 /// \brief Determine the number of preprocessed entities that were
325 /// loaded (or can be loaded) from an external source.
326 unsigned getNumLoadedPreprocessedEntities() const {
327 return LoadedPreprocessedEntities.size();
330 /// \brief Returns a pair of [Begin, End) indices of local preprocessed
331 /// entities that \arg Range encompasses.
332 std::pair<unsigned, unsigned>
333 findLocalPreprocessedEntitiesInRange(SourceRange Range) const;
334 unsigned findBeginLocalPreprocessedEntity(SourceLocation Loc) const;
335 unsigned findEndLocalPreprocessedEntity(SourceLocation Loc) const;
337 /// \brief Allocate space for a new set of loaded preprocessed entities.
339 /// \returns The index into the set of loaded preprocessed entities, which
340 /// corresponds to the first newly-allocated entity.
341 unsigned allocateLoadedEntities(unsigned NumEntities);
343 /// \brief Register a new macro definition.
344 void RegisterMacroDefinition(MacroInfo *Macro, PPEntityID PPID);
347 /// \brief Construct a new preprocessing record.
348 PreprocessingRecord(SourceManager &SM, bool IncludeNestedMacroExpansions);
350 /// \brief Allocate memory in the preprocessing record.
351 void *Allocate(unsigned Size, unsigned Align = 8) {
352 return BumpAlloc.Allocate(Size, Align);
355 /// \brief Deallocate memory in the preprocessing record.
356 void Deallocate(void *Ptr) { }
358 size_t getTotalMemory() const;
360 SourceManager &getSourceManager() const { return SourceMgr; }
362 // Iteration over the preprocessed entities.
364 PreprocessingRecord *Self;
366 /// \brief Position within the preprocessed entity sequence.
368 /// In a complete iteration, the Position field walks the range [-M, N),
369 /// where negative values are used to indicate preprocessed entities
370 /// loaded from the external source while non-negative values are used to
371 /// indicate preprocessed entities introduced by the current preprocessor.
372 /// However, to provide iteration in source order (for, e.g., chained
373 /// precompiled headers), dereferencing the iterator flips the negative
374 /// values (corresponding to loaded entities), so that position -M
375 /// corresponds to element 0 in the loaded entities vector, position -M+1
376 /// corresponds to element 1 in the loaded entities vector, etc. This
377 /// gives us a reasonably efficient, source-order walk.
381 typedef PreprocessedEntity *value_type;
382 typedef value_type& reference;
383 typedef value_type* pointer;
384 typedef std::random_access_iterator_tag iterator_category;
385 typedef int difference_type;
387 iterator() : Self(0), Position(0) { }
389 iterator(PreprocessingRecord *Self, int Position)
390 : Self(Self), Position(Position) { }
392 value_type operator*() const {
393 return Self->getPreprocessedEntity(Position);
396 value_type operator[](difference_type D) {
400 iterator &operator++() {
405 iterator operator++(int) {
406 iterator Prev(*this);
411 iterator &operator--() {
416 iterator operator--(int) {
417 iterator Prev(*this);
422 friend bool operator==(const iterator &X, const iterator &Y) {
423 return X.Position == Y.Position;
426 friend bool operator!=(const iterator &X, const iterator &Y) {
427 return X.Position != Y.Position;
430 friend bool operator<(const iterator &X, const iterator &Y) {
431 return X.Position < Y.Position;
434 friend bool operator>(const iterator &X, const iterator &Y) {
435 return X.Position > Y.Position;
438 friend bool operator<=(const iterator &X, const iterator &Y) {
439 return X.Position < Y.Position;
442 friend bool operator>=(const iterator &X, const iterator &Y) {
443 return X.Position > Y.Position;
446 friend iterator& operator+=(iterator &X, difference_type D) {
451 friend iterator& operator-=(iterator &X, difference_type D) {
456 friend iterator operator+(iterator X, difference_type D) {
461 friend iterator operator+(difference_type D, iterator X) {
466 friend difference_type operator-(const iterator &X, const iterator &Y) {
467 return X.Position - Y.Position;
470 friend iterator operator-(iterator X, difference_type D) {
475 friend class iterator;
477 /// \brief Begin iterator for all preprocessed entities.
479 return iterator(this, -(int)LoadedPreprocessedEntities.size());
482 /// \brief End iterator for all preprocessed entities.
484 return iterator(this, PreprocessedEntities.size());
487 /// \brief Begin iterator for local, non-loaded, preprocessed entities.
488 iterator local_begin() {
489 return iterator(this, 0);
492 /// \brief End iterator for local, non-loaded, preprocessed entities.
493 iterator local_end() {
494 return iterator(this, PreprocessedEntities.size());
497 /// \brief Returns a pair of [Begin, End) iterators of preprocessed entities
498 /// that source range \arg R encompasses.
499 std::pair<iterator, iterator> getPreprocessedEntitiesInRange(SourceRange R);
501 /// \brief Add a new preprocessed entity to this record.
502 void addPreprocessedEntity(PreprocessedEntity *Entity);
504 /// \brief Set the external source for preprocessed entities.
505 void SetExternalSource(ExternalPreprocessingRecordSource &Source);
507 /// \brief Retrieve the external source for preprocessed entities.
508 ExternalPreprocessingRecordSource *getExternalSource() const {
509 return ExternalSource;
512 /// \brief Retrieve the macro definition that corresponds to the given
514 MacroDefinition *findMacroDefinition(const MacroInfo *MI);
516 virtual void MacroExpands(const Token &Id, const MacroInfo* MI,
518 virtual void MacroDefined(const Token &Id, const MacroInfo *MI);
519 virtual void MacroUndefined(const Token &Id, const MacroInfo *MI);
520 virtual void InclusionDirective(SourceLocation HashLoc,
521 const Token &IncludeTok,
524 const FileEntry *File,
525 SourceLocation EndLoc,
526 StringRef SearchPath,
527 StringRef RelativePath);
529 friend class ASTReader;
530 friend class ASTWriter;
532 } // end namespace clang
534 inline void* operator new(size_t bytes, clang::PreprocessingRecord& PR,
535 unsigned alignment) throw() {
536 return PR.Allocate(bytes, alignment);
539 inline void operator delete(void* ptr, clang::PreprocessingRecord& PR,
544 #endif // LLVM_CLANG_LEX_PREPROCESSINGRECORD_H