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/SmallVector.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/Optional.h"
23 #include "llvm/Support/Allocator.h"
24 #include "llvm/Support/Compiler.h"
29 class PreprocessingRecord;
32 /// \brief Allocates memory within a Clang preprocessing record.
33 void* operator new(size_t bytes, clang::PreprocessingRecord& PR,
34 unsigned alignment = 8) throw();
36 /// \brief Frees memory allocated in a Clang preprocessing record.
37 void operator delete(void* ptr, clang::PreprocessingRecord& PR,
41 class MacroDefinition;
44 /// \brief Base class that describes a preprocessed entity, which may be a
45 /// preprocessor directive or macro expansion.
46 class PreprocessedEntity {
48 /// \brief The kind of preprocessed entity an object describes.
50 /// \brief Indicates a problem trying to load the preprocessed entity.
53 /// \brief A macro expansion.
56 /// \defgroup Preprocessing directives
59 /// \brief A macro definition.
62 /// \brief An inclusion directive, such as \c #include, \c
63 /// #import, or \c #include_next.
64 InclusionDirectiveKind,
68 FirstPreprocessingDirective = MacroDefinitionKind,
69 LastPreprocessingDirective = InclusionDirectiveKind
73 /// \brief The kind of preprocessed entity that this object describes.
76 /// \brief The source range that covers this preprocessed entity.
80 PreprocessedEntity(EntityKind Kind, SourceRange Range)
81 : Kind(Kind), Range(Range) { }
83 friend class PreprocessingRecord;
86 /// \brief Retrieve the kind of preprocessed entity stored in this object.
87 EntityKind getKind() const { return Kind; }
89 /// \brief Retrieve the source range that covers this entire preprocessed
91 SourceRange getSourceRange() const LLVM_READONLY { return Range; }
93 /// \brief Returns true if there was a problem loading the preprocessed
95 bool isInvalid() const { return Kind == InvalidKind; }
97 // Implement isa/cast/dyncast/etc.
98 static bool classof(const PreprocessedEntity *) { return true; }
100 // Only allow allocation of preprocessed entities using the allocator
101 // in PreprocessingRecord or by doing a placement new.
102 void* operator new(size_t bytes, PreprocessingRecord& PR,
103 unsigned alignment = 8) throw() {
104 return ::operator new(bytes, PR, alignment);
107 void* operator new(size_t bytes, void* mem) throw() {
111 void operator delete(void* ptr, PreprocessingRecord& PR,
112 unsigned alignment) throw() {
113 return ::operator delete(ptr, PR, alignment);
116 void operator delete(void*, std::size_t) throw() { }
117 void operator delete(void*, void*) throw() { }
120 // Make vanilla 'new' and 'delete' illegal for preprocessed entities.
121 void* operator new(size_t bytes) throw();
122 void operator delete(void* data) throw();
125 /// \brief Records the presence of a preprocessor directive.
126 class PreprocessingDirective : public PreprocessedEntity {
128 PreprocessingDirective(EntityKind Kind, SourceRange Range)
129 : PreprocessedEntity(Kind, Range) { }
131 // Implement isa/cast/dyncast/etc.
132 static bool classof(const PreprocessedEntity *PD) {
133 return PD->getKind() >= FirstPreprocessingDirective &&
134 PD->getKind() <= LastPreprocessingDirective;
136 static bool classof(const PreprocessingDirective *) { return true; }
139 /// \brief Record the location of a macro definition.
140 class MacroDefinition : public PreprocessingDirective {
141 /// \brief The name of the macro being defined.
142 const IdentifierInfo *Name;
145 explicit MacroDefinition(const IdentifierInfo *Name, SourceRange Range)
146 : PreprocessingDirective(MacroDefinitionKind, Range), Name(Name) { }
148 /// \brief Retrieve the name of the macro being defined.
149 const IdentifierInfo *getName() const { return Name; }
151 /// \brief Retrieve the location of the macro name in the definition.
152 SourceLocation getLocation() const { return getSourceRange().getBegin(); }
154 // Implement isa/cast/dyncast/etc.
155 static bool classof(const PreprocessedEntity *PE) {
156 return PE->getKind() == MacroDefinitionKind;
158 static bool classof(const MacroDefinition *) { return true; }
161 /// \brief Records the location of a macro expansion.
162 class MacroExpansion : public PreprocessedEntity {
163 /// \brief The definition of this macro or the name of the macro if it is
165 llvm::PointerUnion<IdentifierInfo *, MacroDefinition *> NameOrDef;
168 MacroExpansion(IdentifierInfo *BuiltinName, SourceRange Range)
169 : PreprocessedEntity(MacroExpansionKind, Range),
170 NameOrDef(BuiltinName) { }
172 MacroExpansion(MacroDefinition *Definition, SourceRange Range)
173 : PreprocessedEntity(MacroExpansionKind, Range),
174 NameOrDef(Definition) { }
176 /// \brief True if it is a builtin macro.
177 bool isBuiltinMacro() const { return NameOrDef.is<IdentifierInfo *>(); }
179 /// \brief The name of the macro being expanded.
180 const IdentifierInfo *getName() const {
181 if (MacroDefinition *Def = getDefinition())
182 return Def->getName();
183 return NameOrDef.get<IdentifierInfo*>();
186 /// \brief The definition of the macro being expanded. May return null if
187 /// this is a builtin macro.
188 MacroDefinition *getDefinition() const {
189 return NameOrDef.dyn_cast<MacroDefinition *>();
192 // Implement isa/cast/dyncast/etc.
193 static bool classof(const PreprocessedEntity *PE) {
194 return PE->getKind() == MacroExpansionKind;
196 static bool classof(const MacroExpansion *) { return true; }
199 /// \brief Record the location of an inclusion directive, such as an
200 /// \c #include or \c #import statement.
201 class InclusionDirective : public PreprocessingDirective {
203 /// \brief The kind of inclusion directives known to the
206 /// \brief An \c #include directive.
208 /// \brief An Objective-C \c #import directive.
210 /// \brief A GNU \c #include_next directive.
212 /// \brief A Clang \c #__include_macros directive.
217 /// \brief The name of the file that was included, as written in
221 /// \brief Whether the file name was in quotation marks; otherwise, it was
222 /// in angle brackets.
223 unsigned InQuotes : 1;
225 /// \brief The kind of inclusion directive we have.
227 /// This is a value of type InclusionKind.
230 /// \brief The file that was included.
231 const FileEntry *File;
234 InclusionDirective(PreprocessingRecord &PPRec,
235 InclusionKind Kind, StringRef FileName,
236 bool InQuotes, 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 Retrieve the file entry for the actual file that was included
249 /// by this directive.
250 const FileEntry *getFile() const { return File; }
252 // Implement isa/cast/dyncast/etc.
253 static bool classof(const PreprocessedEntity *PE) {
254 return PE->getKind() == InclusionDirectiveKind;
256 static bool classof(const InclusionDirective *) { return true; }
259 /// \brief An abstract class that should be subclassed by any external source
260 /// of preprocessing record entries.
261 class ExternalPreprocessingRecordSource {
263 virtual ~ExternalPreprocessingRecordSource();
265 /// \brief Read a preallocated preprocessed entity from the external source.
267 /// \returns null if an error occurred that prevented the preprocessed
268 /// entity from being loaded.
269 virtual PreprocessedEntity *ReadPreprocessedEntity(unsigned Index) = 0;
271 /// \brief Returns a pair of [Begin, End) indices of preallocated
272 /// preprocessed entities that \arg Range encompasses.
273 virtual std::pair<unsigned, unsigned>
274 findPreprocessedEntitiesInRange(SourceRange Range) = 0;
276 /// \brief Optionally returns true or false if the preallocated preprocessed
277 /// entity with index \arg Index came from file \arg FID.
278 virtual llvm::Optional<bool> isPreprocessedEntityInFileID(unsigned Index,
280 return llvm::Optional<bool>();
284 /// \brief A record of the steps taken while preprocessing a source file,
285 /// including the various preprocessing directives processed, macros
287 class PreprocessingRecord : public PPCallbacks {
288 SourceManager &SourceMgr;
290 /// \brief Allocator used to store preprocessing objects.
291 llvm::BumpPtrAllocator BumpAlloc;
293 /// \brief The set of preprocessed entities in this record, in order they
295 std::vector<PreprocessedEntity *> PreprocessedEntities;
297 /// \brief The set of preprocessed entities in this record that have been
298 /// loaded from external sources.
300 /// The entries in this vector are loaded lazily from the external source,
301 /// and are referenced by the iterator using negative indices.
302 std::vector<PreprocessedEntity *> LoadedPreprocessedEntities;
304 bool RecordCondDirectives;
305 unsigned CondDirectiveNextIdx;
306 SmallVector<unsigned, 6> CondDirectiveStack;
308 class CondDirectiveLoc {
313 CondDirectiveLoc(SourceLocation Loc, unsigned Idx) : Loc(Loc), Idx(Idx) {}
315 SourceLocation getLoc() const { return Loc; }
316 unsigned getIdx() const { return Idx; }
321 explicit Comp(SourceManager &SM) : SM(SM) {}
322 bool operator()(const CondDirectiveLoc &LHS,
323 const CondDirectiveLoc &RHS) {
324 return SM.isBeforeInTranslationUnit(LHS.getLoc(), RHS.getLoc());
326 bool operator()(const CondDirectiveLoc &LHS, SourceLocation RHS) {
327 return SM.isBeforeInTranslationUnit(LHS.getLoc(), RHS);
329 bool operator()(SourceLocation LHS, const CondDirectiveLoc &RHS) {
330 return SM.isBeforeInTranslationUnit(LHS, RHS.getLoc());
335 typedef std::vector<CondDirectiveLoc> CondDirectiveLocsTy;
336 /// \brief The locations of conditional directives in source order.
337 CondDirectiveLocsTy CondDirectiveLocs;
339 void addCondDirectiveLoc(CondDirectiveLoc DirLoc);
340 unsigned findCondDirectiveIdx(SourceLocation Loc) const;
342 /// \brief Global (loaded or local) ID for a preprocessed entity.
343 /// Negative values are used to indicate preprocessed entities
344 /// loaded from the external source while non-negative values are used to
345 /// indicate preprocessed entities introduced by the current preprocessor.
346 /// If M is the number of loaded preprocessed entities, value -M
347 /// corresponds to element 0 in the loaded entities vector, position -M+1
348 /// corresponds to element 1 in the loaded entities vector, etc.
349 typedef int PPEntityID;
351 PPEntityID getPPEntityID(unsigned Index, bool isLoaded) const {
352 return isLoaded ? PPEntityID(Index) - LoadedPreprocessedEntities.size()
356 /// \brief Mapping from MacroInfo structures to their definitions.
357 llvm::DenseMap<const MacroInfo *, PPEntityID> MacroDefinitions;
359 /// \brief External source of preprocessed entities.
360 ExternalPreprocessingRecordSource *ExternalSource;
362 /// \brief Retrieve the preprocessed entity at the given ID.
363 PreprocessedEntity *getPreprocessedEntity(PPEntityID PPID);
365 /// \brief Retrieve the loaded preprocessed entity at the given index.
366 PreprocessedEntity *getLoadedPreprocessedEntity(unsigned Index);
368 /// \brief Determine the number of preprocessed entities that were
369 /// loaded (or can be loaded) from an external source.
370 unsigned getNumLoadedPreprocessedEntities() const {
371 return LoadedPreprocessedEntities.size();
374 /// \brief Returns a pair of [Begin, End) indices of local preprocessed
375 /// entities that \arg Range encompasses.
376 std::pair<unsigned, unsigned>
377 findLocalPreprocessedEntitiesInRange(SourceRange Range) const;
378 unsigned findBeginLocalPreprocessedEntity(SourceLocation Loc) const;
379 unsigned findEndLocalPreprocessedEntity(SourceLocation Loc) const;
381 /// \brief Allocate space for a new set of loaded preprocessed entities.
383 /// \returns The index into the set of loaded preprocessed entities, which
384 /// corresponds to the first newly-allocated entity.
385 unsigned allocateLoadedEntities(unsigned NumEntities);
387 /// \brief Register a new macro definition.
388 void RegisterMacroDefinition(MacroInfo *Macro, PPEntityID PPID);
391 /// \brief Construct a new preprocessing record.
392 PreprocessingRecord(SourceManager &SM, bool RecordConditionalDirectives);
394 /// \brief Allocate memory in the preprocessing record.
395 void *Allocate(unsigned Size, unsigned Align = 8) {
396 return BumpAlloc.Allocate(Size, Align);
399 /// \brief Deallocate memory in the preprocessing record.
400 void Deallocate(void *Ptr) { }
402 size_t getTotalMemory() const;
404 SourceManager &getSourceManager() const { return SourceMgr; }
406 // Iteration over the preprocessed entities.
408 PreprocessingRecord *Self;
410 /// \brief Position within the preprocessed entity sequence.
412 /// In a complete iteration, the Position field walks the range [-M, N),
413 /// where negative values are used to indicate preprocessed entities
414 /// loaded from the external source while non-negative values are used to
415 /// indicate preprocessed entities introduced by the current preprocessor.
416 /// However, to provide iteration in source order (for, e.g., chained
417 /// precompiled headers), dereferencing the iterator flips the negative
418 /// values (corresponding to loaded entities), so that position -M
419 /// corresponds to element 0 in the loaded entities vector, position -M+1
420 /// corresponds to element 1 in the loaded entities vector, etc. This
421 /// gives us a reasonably efficient, source-order walk.
425 typedef PreprocessedEntity *value_type;
426 typedef value_type& reference;
427 typedef value_type* pointer;
428 typedef std::random_access_iterator_tag iterator_category;
429 typedef int difference_type;
431 iterator() : Self(0), Position(0) { }
433 iterator(PreprocessingRecord *Self, PPEntityID Position)
434 : Self(Self), Position(Position) { }
436 value_type operator*() const {
437 return Self->getPreprocessedEntity(Position);
440 value_type operator[](difference_type D) {
444 iterator &operator++() {
449 iterator operator++(int) {
450 iterator Prev(*this);
455 iterator &operator--() {
460 iterator operator--(int) {
461 iterator Prev(*this);
466 friend bool operator==(const iterator &X, const iterator &Y) {
467 return X.Position == Y.Position;
470 friend bool operator!=(const iterator &X, const iterator &Y) {
471 return X.Position != Y.Position;
474 friend bool operator<(const iterator &X, const iterator &Y) {
475 return X.Position < Y.Position;
478 friend bool operator>(const iterator &X, const iterator &Y) {
479 return X.Position > Y.Position;
482 friend bool operator<=(const iterator &X, const iterator &Y) {
483 return X.Position < Y.Position;
486 friend bool operator>=(const iterator &X, const iterator &Y) {
487 return X.Position > Y.Position;
490 friend iterator& operator+=(iterator &X, difference_type D) {
495 friend iterator& operator-=(iterator &X, difference_type D) {
500 friend iterator operator+(iterator X, difference_type D) {
505 friend iterator operator+(difference_type D, iterator X) {
510 friend difference_type operator-(const iterator &X, const iterator &Y) {
511 return X.Position - Y.Position;
514 friend iterator operator-(iterator X, difference_type D) {
518 friend class PreprocessingRecord;
520 friend class iterator;
522 /// \brief Begin iterator for all preprocessed entities.
524 return iterator(this, -(int)LoadedPreprocessedEntities.size());
527 /// \brief End iterator for all preprocessed entities.
529 return iterator(this, PreprocessedEntities.size());
532 /// \brief Begin iterator for local, non-loaded, preprocessed entities.
533 iterator local_begin() {
534 return iterator(this, 0);
537 /// \brief End iterator for local, non-loaded, preprocessed entities.
538 iterator local_end() {
539 return iterator(this, PreprocessedEntities.size());
542 /// \brief Returns a pair of [Begin, End) iterators of preprocessed entities
543 /// that source range \arg R encompasses.
545 /// \param R the range to look for preprocessed entities.
547 std::pair<iterator, iterator> getPreprocessedEntitiesInRange(SourceRange R);
549 /// \brief Returns true if the preprocessed entity that \arg PPEI iterator
550 /// points to is coming from the file \arg FID.
552 /// Can be used to avoid implicit deserializations of preallocated
553 /// preprocessed entities if we only care about entities of a specific file
554 /// and not from files #included in the range given at
555 /// \see getPreprocessedEntitiesInRange.
556 bool isEntityInFileID(iterator PPEI, FileID FID);
558 /// \brief Add a new preprocessed entity to this record.
559 PPEntityID addPreprocessedEntity(PreprocessedEntity *Entity);
561 /// \brief Returns true if this PreprocessingRecord is keeping track of
562 /// conditional directives locations.
563 bool isRecordingConditionalDirectives() const {
564 return RecordCondDirectives;
567 /// \brief Returns true if the given range intersects with a conditional
568 /// directive. if a #if/#endif block is fully contained within the range,
569 /// this function will return false.
570 bool rangeIntersectsConditionalDirective(SourceRange Range) const;
572 /// \brief Returns true if the given locations are in different regions,
573 /// separated by conditional directive blocks.
574 bool areInDifferentConditionalDirectiveRegion(SourceLocation LHS,
575 SourceLocation RHS) const {
576 return findCondDirectiveIdx(LHS) != findCondDirectiveIdx(RHS);
579 /// \brief Set the external source for preprocessed entities.
580 void SetExternalSource(ExternalPreprocessingRecordSource &Source);
582 /// \brief Retrieve the external source for preprocessed entities.
583 ExternalPreprocessingRecordSource *getExternalSource() const {
584 return ExternalSource;
587 /// \brief Retrieve the macro definition that corresponds to the given
589 MacroDefinition *findMacroDefinition(const MacroInfo *MI);
592 virtual void MacroExpands(const Token &Id, const MacroInfo* MI,
594 virtual void MacroDefined(const Token &Id, const MacroInfo *MI);
595 virtual void MacroUndefined(const Token &Id, const MacroInfo *MI);
596 virtual void InclusionDirective(SourceLocation HashLoc,
597 const Token &IncludeTok,
600 const FileEntry *File,
601 SourceLocation EndLoc,
602 StringRef SearchPath,
603 StringRef RelativePath);
604 virtual void If(SourceLocation Loc, SourceRange ConditionRange);
605 virtual void Elif(SourceLocation Loc, SourceRange ConditionRange,
606 SourceLocation IfLoc);
607 virtual void Ifdef(SourceLocation Loc, const Token &MacroNameTok);
608 virtual void Ifndef(SourceLocation Loc, const Token &MacroNameTok);
609 virtual void Else(SourceLocation Loc, SourceLocation IfLoc);
610 virtual void Endif(SourceLocation Loc, SourceLocation IfLoc);
612 /// \brief Cached result of the last \see getPreprocessedEntitiesInRange
616 std::pair<PPEntityID, PPEntityID> Result;
619 std::pair<PPEntityID, PPEntityID>
620 getPreprocessedEntitiesInRangeSlow(SourceRange R);
622 friend class ASTReader;
623 friend class ASTWriter;
625 } // end namespace clang
627 inline void* operator new(size_t bytes, clang::PreprocessingRecord& PR,
628 unsigned alignment) throw() {
629 return PR.Allocate(bytes, alignment);
632 inline void operator delete(void* ptr, clang::PreprocessingRecord& PR,
637 #endif // LLVM_CLANG_LEX_PREPROCESSINGRECORD_H