clang API Documentation
00001 //===--- PreprocessingRecord.h - Record of Preprocessing --------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the PreprocessingRecord class, which maintains a record 00011 // of what occurred during preprocessing. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 #ifndef LLVM_CLANG_LEX_PREPROCESSINGRECORD_H 00015 #define LLVM_CLANG_LEX_PREPROCESSINGRECORD_H 00016 00017 #include "clang/Basic/IdentifierTable.h" 00018 #include "clang/Basic/SourceLocation.h" 00019 #include "clang/Lex/PPCallbacks.h" 00020 #include "llvm/ADT/DenseMap.h" 00021 #include "llvm/ADT/Optional.h" 00022 #include "llvm/ADT/SmallVector.h" 00023 #include "llvm/Support/Allocator.h" 00024 #include "llvm/Support/Compiler.h" 00025 #include <vector> 00026 00027 namespace clang { 00028 class IdentifierInfo; 00029 class MacroInfo; 00030 class PreprocessingRecord; 00031 } 00032 00033 /// \brief Allocates memory within a Clang preprocessing record. 00034 void* operator new(size_t bytes, clang::PreprocessingRecord& PR, 00035 unsigned alignment = 8) throw(); 00036 00037 /// \brief Frees memory allocated in a Clang preprocessing record. 00038 void operator delete(void* ptr, clang::PreprocessingRecord& PR, 00039 unsigned) throw(); 00040 00041 namespace clang { 00042 class MacroDefinition; 00043 class FileEntry; 00044 00045 /// \brief Base class that describes a preprocessed entity, which may be a 00046 /// preprocessor directive or macro expansion. 00047 class PreprocessedEntity { 00048 public: 00049 /// \brief The kind of preprocessed entity an object describes. 00050 enum EntityKind { 00051 /// \brief Indicates a problem trying to load the preprocessed entity. 00052 InvalidKind, 00053 00054 /// \brief A macro expansion. 00055 MacroExpansionKind, 00056 00057 /// \defgroup Preprocessing directives 00058 /// @{ 00059 00060 /// \brief A macro definition. 00061 MacroDefinitionKind, 00062 00063 /// \brief An inclusion directive, such as \c \#include, \c 00064 /// \#import, or \c \#include_next. 00065 InclusionDirectiveKind, 00066 00067 /// @} 00068 00069 FirstPreprocessingDirective = MacroDefinitionKind, 00070 LastPreprocessingDirective = InclusionDirectiveKind 00071 }; 00072 00073 private: 00074 /// \brief The kind of preprocessed entity that this object describes. 00075 EntityKind Kind; 00076 00077 /// \brief The source range that covers this preprocessed entity. 00078 SourceRange Range; 00079 00080 protected: 00081 PreprocessedEntity(EntityKind Kind, SourceRange Range) 00082 : Kind(Kind), Range(Range) { } 00083 00084 friend class PreprocessingRecord; 00085 00086 public: 00087 /// \brief Retrieve the kind of preprocessed entity stored in this object. 00088 EntityKind getKind() const { return Kind; } 00089 00090 /// \brief Retrieve the source range that covers this entire preprocessed 00091 /// entity. 00092 SourceRange getSourceRange() const LLVM_READONLY { return Range; } 00093 00094 /// \brief Returns true if there was a problem loading the preprocessed 00095 /// entity. 00096 bool isInvalid() const { return Kind == InvalidKind; } 00097 00098 // Only allow allocation of preprocessed entities using the allocator 00099 // in PreprocessingRecord or by doing a placement new. 00100 void* operator new(size_t bytes, PreprocessingRecord& PR, 00101 unsigned alignment = 8) throw() { 00102 return ::operator new(bytes, PR, alignment); 00103 } 00104 00105 void* operator new(size_t bytes, void* mem) throw() { 00106 return mem; 00107 } 00108 00109 void operator delete(void* ptr, PreprocessingRecord& PR, 00110 unsigned alignment) throw() { 00111 return ::operator delete(ptr, PR, alignment); 00112 } 00113 00114 void operator delete(void*, std::size_t) throw() { } 00115 void operator delete(void*, void*) throw() { } 00116 00117 private: 00118 // Make vanilla 'new' and 'delete' illegal for preprocessed entities. 00119 void* operator new(size_t bytes) throw(); 00120 void operator delete(void* data) throw(); 00121 }; 00122 00123 /// \brief Records the presence of a preprocessor directive. 00124 class PreprocessingDirective : public PreprocessedEntity { 00125 public: 00126 PreprocessingDirective(EntityKind Kind, SourceRange Range) 00127 : PreprocessedEntity(Kind, Range) { } 00128 00129 // Implement isa/cast/dyncast/etc. 00130 static bool classof(const PreprocessedEntity *PD) { 00131 return PD->getKind() >= FirstPreprocessingDirective && 00132 PD->getKind() <= LastPreprocessingDirective; 00133 } 00134 }; 00135 00136 /// \brief Record the location of a macro definition. 00137 class MacroDefinition : public PreprocessingDirective { 00138 /// \brief The name of the macro being defined. 00139 const IdentifierInfo *Name; 00140 00141 public: 00142 explicit MacroDefinition(const IdentifierInfo *Name, SourceRange Range) 00143 : PreprocessingDirective(MacroDefinitionKind, Range), Name(Name) { } 00144 00145 /// \brief Retrieve the name of the macro being defined. 00146 const IdentifierInfo *getName() const { return Name; } 00147 00148 /// \brief Retrieve the location of the macro name in the definition. 00149 SourceLocation getLocation() const { return getSourceRange().getBegin(); } 00150 00151 // Implement isa/cast/dyncast/etc. 00152 static bool classof(const PreprocessedEntity *PE) { 00153 return PE->getKind() == MacroDefinitionKind; 00154 } 00155 }; 00156 00157 /// \brief Records the location of a macro expansion. 00158 class MacroExpansion : public PreprocessedEntity { 00159 /// \brief The definition of this macro or the name of the macro if it is 00160 /// a builtin macro. 00161 llvm::PointerUnion<IdentifierInfo *, MacroDefinition *> NameOrDef; 00162 00163 public: 00164 MacroExpansion(IdentifierInfo *BuiltinName, SourceRange Range) 00165 : PreprocessedEntity(MacroExpansionKind, Range), 00166 NameOrDef(BuiltinName) { } 00167 00168 MacroExpansion(MacroDefinition *Definition, SourceRange Range) 00169 : PreprocessedEntity(MacroExpansionKind, Range), 00170 NameOrDef(Definition) { } 00171 00172 /// \brief True if it is a builtin macro. 00173 bool isBuiltinMacro() const { return NameOrDef.is<IdentifierInfo *>(); } 00174 00175 /// \brief The name of the macro being expanded. 00176 const IdentifierInfo *getName() const { 00177 if (MacroDefinition *Def = getDefinition()) 00178 return Def->getName(); 00179 return NameOrDef.get<IdentifierInfo*>(); 00180 } 00181 00182 /// \brief The definition of the macro being expanded. May return null if 00183 /// this is a builtin macro. 00184 MacroDefinition *getDefinition() const { 00185 return NameOrDef.dyn_cast<MacroDefinition *>(); 00186 } 00187 00188 // Implement isa/cast/dyncast/etc. 00189 static bool classof(const PreprocessedEntity *PE) { 00190 return PE->getKind() == MacroExpansionKind; 00191 } 00192 }; 00193 00194 /// \brief Record the location of an inclusion directive, such as an 00195 /// \c \#include or \c \#import statement. 00196 class InclusionDirective : public PreprocessingDirective { 00197 public: 00198 /// \brief The kind of inclusion directives known to the 00199 /// preprocessor. 00200 enum InclusionKind { 00201 /// \brief An \c \#include directive. 00202 Include, 00203 /// \brief An Objective-C \c \#import directive. 00204 Import, 00205 /// \brief A GNU \c \#include_next directive. 00206 IncludeNext, 00207 /// \brief A Clang \c \#__include_macros directive. 00208 IncludeMacros 00209 }; 00210 00211 private: 00212 /// \brief The name of the file that was included, as written in 00213 /// the source. 00214 StringRef FileName; 00215 00216 /// \brief Whether the file name was in quotation marks; otherwise, it was 00217 /// in angle brackets. 00218 unsigned InQuotes : 1; 00219 00220 /// \brief The kind of inclusion directive we have. 00221 /// 00222 /// This is a value of type InclusionKind. 00223 unsigned Kind : 2; 00224 00225 /// \brief Whether the inclusion directive was automatically turned into 00226 /// a module import. 00227 unsigned ImportedModule : 1; 00228 00229 /// \brief The file that was included. 00230 const FileEntry *File; 00231 00232 public: 00233 InclusionDirective(PreprocessingRecord &PPRec, 00234 InclusionKind Kind, StringRef FileName, 00235 bool InQuotes, bool ImportedModule, 00236 const FileEntry *File, SourceRange Range); 00237 00238 /// \brief Determine what kind of inclusion directive this is. 00239 InclusionKind getKind() const { return static_cast<InclusionKind>(Kind); } 00240 00241 /// \brief Retrieve the included file name as it was written in the source. 00242 StringRef getFileName() const { return FileName; } 00243 00244 /// \brief Determine whether the included file name was written in quotes; 00245 /// otherwise, it was written in angle brackets. 00246 bool wasInQuotes() const { return InQuotes; } 00247 00248 /// \brief Determine whether the inclusion directive was automatically 00249 /// turned into a module import. 00250 bool importedModule() const { return ImportedModule; } 00251 00252 /// \brief Retrieve the file entry for the actual file that was included 00253 /// by this directive. 00254 const FileEntry *getFile() const { return File; } 00255 00256 // Implement isa/cast/dyncast/etc. 00257 static bool classof(const PreprocessedEntity *PE) { 00258 return PE->getKind() == InclusionDirectiveKind; 00259 } 00260 }; 00261 00262 /// \brief An abstract class that should be subclassed by any external source 00263 /// of preprocessing record entries. 00264 class ExternalPreprocessingRecordSource { 00265 public: 00266 virtual ~ExternalPreprocessingRecordSource(); 00267 00268 /// \brief Read a preallocated preprocessed entity from the external source. 00269 /// 00270 /// \returns null if an error occurred that prevented the preprocessed 00271 /// entity from being loaded. 00272 virtual PreprocessedEntity *ReadPreprocessedEntity(unsigned Index) = 0; 00273 00274 /// \brief Returns a pair of [Begin, End) indices of preallocated 00275 /// preprocessed entities that \p Range encompasses. 00276 virtual std::pair<unsigned, unsigned> 00277 findPreprocessedEntitiesInRange(SourceRange Range) = 0; 00278 00279 /// \brief Optionally returns true or false if the preallocated preprocessed 00280 /// entity with index \p Index came from file \p FID. 00281 virtual Optional<bool> isPreprocessedEntityInFileID(unsigned Index, 00282 FileID FID) { 00283 return None; 00284 } 00285 }; 00286 00287 /// \brief A record of the steps taken while preprocessing a source file, 00288 /// including the various preprocessing directives processed, macros 00289 /// expanded, etc. 00290 class PreprocessingRecord : public PPCallbacks { 00291 SourceManager &SourceMgr; 00292 00293 /// \brief Allocator used to store preprocessing objects. 00294 llvm::BumpPtrAllocator BumpAlloc; 00295 00296 /// \brief The set of preprocessed entities in this record, in order they 00297 /// were seen. 00298 std::vector<PreprocessedEntity *> PreprocessedEntities; 00299 00300 /// \brief The set of preprocessed entities in this record that have been 00301 /// loaded from external sources. 00302 /// 00303 /// The entries in this vector are loaded lazily from the external source, 00304 /// and are referenced by the iterator using negative indices. 00305 std::vector<PreprocessedEntity *> LoadedPreprocessedEntities; 00306 00307 /// \brief The set of ranges that were skipped by the preprocessor, 00308 std::vector<SourceRange> SkippedRanges; 00309 00310 /// \brief Global (loaded or local) ID for a preprocessed entity. 00311 /// Negative values are used to indicate preprocessed entities 00312 /// loaded from the external source while non-negative values are used to 00313 /// indicate preprocessed entities introduced by the current preprocessor. 00314 /// Value -1 corresponds to element 0 in the loaded entities vector, 00315 /// value -2 corresponds to element 1 in the loaded entities vector, etc. 00316 /// Value 0 is an invalid value, the index to local entities is 1-based, 00317 /// value 1 corresponds to element 0 in the local entities vector, 00318 /// value 2 corresponds to element 1 in the local entities vector, etc. 00319 class PPEntityID { 00320 int ID; 00321 explicit PPEntityID(int ID) : ID(ID) {} 00322 friend class PreprocessingRecord; 00323 public: 00324 PPEntityID() : ID(0) {} 00325 }; 00326 00327 static PPEntityID getPPEntityID(unsigned Index, bool isLoaded) { 00328 return isLoaded ? PPEntityID(-int(Index)-1) : PPEntityID(Index+1); 00329 } 00330 00331 /// \brief Mapping from MacroInfo structures to their definitions. 00332 llvm::DenseMap<const MacroInfo *, MacroDefinition *> MacroDefinitions; 00333 00334 /// \brief External source of preprocessed entities. 00335 ExternalPreprocessingRecordSource *ExternalSource; 00336 00337 /// \brief Retrieve the preprocessed entity at the given ID. 00338 PreprocessedEntity *getPreprocessedEntity(PPEntityID PPID); 00339 00340 /// \brief Retrieve the loaded preprocessed entity at the given index. 00341 PreprocessedEntity *getLoadedPreprocessedEntity(unsigned Index); 00342 00343 /// \brief Determine the number of preprocessed entities that were 00344 /// loaded (or can be loaded) from an external source. 00345 unsigned getNumLoadedPreprocessedEntities() const { 00346 return LoadedPreprocessedEntities.size(); 00347 } 00348 00349 /// \brief Returns a pair of [Begin, End) indices of local preprocessed 00350 /// entities that \p Range encompasses. 00351 std::pair<unsigned, unsigned> 00352 findLocalPreprocessedEntitiesInRange(SourceRange Range) const; 00353 unsigned findBeginLocalPreprocessedEntity(SourceLocation Loc) const; 00354 unsigned findEndLocalPreprocessedEntity(SourceLocation Loc) const; 00355 00356 /// \brief Allocate space for a new set of loaded preprocessed entities. 00357 /// 00358 /// \returns The index into the set of loaded preprocessed entities, which 00359 /// corresponds to the first newly-allocated entity. 00360 unsigned allocateLoadedEntities(unsigned NumEntities); 00361 00362 /// \brief Register a new macro definition. 00363 void RegisterMacroDefinition(MacroInfo *Macro, MacroDefinition *Def); 00364 00365 public: 00366 /// \brief Construct a new preprocessing record. 00367 explicit PreprocessingRecord(SourceManager &SM); 00368 00369 /// \brief Allocate memory in the preprocessing record. 00370 void *Allocate(unsigned Size, unsigned Align = 8) { 00371 return BumpAlloc.Allocate(Size, Align); 00372 } 00373 00374 /// \brief Deallocate memory in the preprocessing record. 00375 void Deallocate(void *Ptr) { } 00376 00377 size_t getTotalMemory() const; 00378 00379 SourceManager &getSourceManager() const { return SourceMgr; } 00380 00381 // Iteration over the preprocessed entities. 00382 class iterator { 00383 PreprocessingRecord *Self; 00384 00385 /// \brief Position within the preprocessed entity sequence. 00386 /// 00387 /// In a complete iteration, the Position field walks the range [-M, N), 00388 /// where negative values are used to indicate preprocessed entities 00389 /// loaded from the external source while non-negative values are used to 00390 /// indicate preprocessed entities introduced by the current preprocessor. 00391 /// However, to provide iteration in source order (for, e.g., chained 00392 /// precompiled headers), dereferencing the iterator flips the negative 00393 /// values (corresponding to loaded entities), so that position -M 00394 /// corresponds to element 0 in the loaded entities vector, position -M+1 00395 /// corresponds to element 1 in the loaded entities vector, etc. This 00396 /// gives us a reasonably efficient, source-order walk. 00397 int Position; 00398 00399 public: 00400 typedef PreprocessedEntity *value_type; 00401 typedef value_type& reference; 00402 typedef value_type* pointer; 00403 typedef std::random_access_iterator_tag iterator_category; 00404 typedef int difference_type; 00405 00406 iterator() : Self(nullptr), Position(0) { } 00407 00408 iterator(PreprocessingRecord *Self, int Position) 00409 : Self(Self), Position(Position) { } 00410 00411 value_type operator*() const { 00412 bool isLoaded = Position < 0; 00413 unsigned Index = isLoaded ? 00414 Self->LoadedPreprocessedEntities.size() + Position : Position; 00415 PPEntityID ID = Self->getPPEntityID(Index, isLoaded); 00416 return Self->getPreprocessedEntity(ID); 00417 } 00418 00419 value_type operator[](difference_type D) { 00420 return *(*this + D); 00421 } 00422 00423 iterator &operator++() { 00424 ++Position; 00425 return *this; 00426 } 00427 00428 iterator operator++(int) { 00429 iterator Prev(*this); 00430 ++Position; 00431 return Prev; 00432 } 00433 00434 iterator &operator--() { 00435 --Position; 00436 return *this; 00437 } 00438 00439 iterator operator--(int) { 00440 iterator Prev(*this); 00441 --Position; 00442 return Prev; 00443 } 00444 00445 friend bool operator==(const iterator &X, const iterator &Y) { 00446 return X.Position == Y.Position; 00447 } 00448 00449 friend bool operator!=(const iterator &X, const iterator &Y) { 00450 return X.Position != Y.Position; 00451 } 00452 00453 friend bool operator<(const iterator &X, const iterator &Y) { 00454 return X.Position < Y.Position; 00455 } 00456 00457 friend bool operator>(const iterator &X, const iterator &Y) { 00458 return X.Position > Y.Position; 00459 } 00460 00461 friend bool operator<=(const iterator &X, const iterator &Y) { 00462 return X.Position < Y.Position; 00463 } 00464 00465 friend bool operator>=(const iterator &X, const iterator &Y) { 00466 return X.Position > Y.Position; 00467 } 00468 00469 friend iterator& operator+=(iterator &X, difference_type D) { 00470 X.Position += D; 00471 return X; 00472 } 00473 00474 friend iterator& operator-=(iterator &X, difference_type D) { 00475 X.Position -= D; 00476 return X; 00477 } 00478 00479 friend iterator operator+(iterator X, difference_type D) { 00480 X.Position += D; 00481 return X; 00482 } 00483 00484 friend iterator operator+(difference_type D, iterator X) { 00485 X.Position += D; 00486 return X; 00487 } 00488 00489 friend difference_type operator-(const iterator &X, const iterator &Y) { 00490 return X.Position - Y.Position; 00491 } 00492 00493 friend iterator operator-(iterator X, difference_type D) { 00494 X.Position -= D; 00495 return X; 00496 } 00497 friend class PreprocessingRecord; 00498 }; 00499 friend class iterator; 00500 00501 /// \brief Begin iterator for all preprocessed entities. 00502 iterator begin() { 00503 return iterator(this, -(int)LoadedPreprocessedEntities.size()); 00504 } 00505 00506 /// \brief End iterator for all preprocessed entities. 00507 iterator end() { 00508 return iterator(this, PreprocessedEntities.size()); 00509 } 00510 00511 /// \brief Begin iterator for local, non-loaded, preprocessed entities. 00512 iterator local_begin() { 00513 return iterator(this, 0); 00514 } 00515 00516 /// \brief End iterator for local, non-loaded, preprocessed entities. 00517 iterator local_end() { 00518 return iterator(this, PreprocessedEntities.size()); 00519 } 00520 00521 /// \brief begin/end iterator pair for the given range of loaded 00522 /// preprocessed entities. 00523 std::pair<iterator, iterator> 00524 getIteratorsForLoadedRange(unsigned start, unsigned count) { 00525 unsigned end = start + count; 00526 assert(end <= LoadedPreprocessedEntities.size()); 00527 return std::make_pair( 00528 iterator(this, int(start)-LoadedPreprocessedEntities.size()), 00529 iterator(this, int(end)-LoadedPreprocessedEntities.size())); 00530 } 00531 00532 /// \brief Returns a pair of [Begin, End) iterators of preprocessed entities 00533 /// that source range \p R encompasses. 00534 /// 00535 /// \param R the range to look for preprocessed entities. 00536 /// 00537 std::pair<iterator, iterator> getPreprocessedEntitiesInRange(SourceRange R); 00538 00539 /// \brief Returns true if the preprocessed entity that \p PPEI iterator 00540 /// points to is coming from the file \p FID. 00541 /// 00542 /// Can be used to avoid implicit deserializations of preallocated 00543 /// preprocessed entities if we only care about entities of a specific file 00544 /// and not from files \#included in the range given at 00545 /// \see getPreprocessedEntitiesInRange. 00546 bool isEntityInFileID(iterator PPEI, FileID FID); 00547 00548 /// \brief Add a new preprocessed entity to this record. 00549 PPEntityID addPreprocessedEntity(PreprocessedEntity *Entity); 00550 00551 /// \brief Set the external source for preprocessed entities. 00552 void SetExternalSource(ExternalPreprocessingRecordSource &Source); 00553 00554 /// \brief Retrieve the external source for preprocessed entities. 00555 ExternalPreprocessingRecordSource *getExternalSource() const { 00556 return ExternalSource; 00557 } 00558 00559 /// \brief Retrieve the macro definition that corresponds to the given 00560 /// \c MacroInfo. 00561 MacroDefinition *findMacroDefinition(const MacroInfo *MI); 00562 00563 /// \brief Retrieve all ranges that got skipped while preprocessing. 00564 const std::vector<SourceRange> &getSkippedRanges() const { 00565 return SkippedRanges; 00566 } 00567 00568 private: 00569 void MacroExpands(const Token &Id, const MacroDirective *MD, 00570 SourceRange Range, const MacroArgs *Args) override; 00571 void MacroDefined(const Token &Id, const MacroDirective *MD) override; 00572 void MacroUndefined(const Token &Id, const MacroDirective *MD) override; 00573 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, 00574 StringRef FileName, bool IsAngled, 00575 CharSourceRange FilenameRange, 00576 const FileEntry *File, StringRef SearchPath, 00577 StringRef RelativePath, 00578 const Module *Imported) override; 00579 void Ifdef(SourceLocation Loc, const Token &MacroNameTok, 00580 const MacroDirective *MD) override; 00581 void Ifndef(SourceLocation Loc, const Token &MacroNameTok, 00582 const MacroDirective *MD) override; 00583 /// \brief Hook called whenever the 'defined' operator is seen. 00584 void Defined(const Token &MacroNameTok, const MacroDirective *MD, 00585 SourceRange Range) override; 00586 00587 void SourceRangeSkipped(SourceRange Range) override; 00588 00589 void addMacroExpansion(const Token &Id, const MacroInfo *MI, 00590 SourceRange Range); 00591 00592 /// \brief Cached result of the last \see getPreprocessedEntitiesInRange 00593 /// query. 00594 struct { 00595 SourceRange Range; 00596 std::pair<int, int> Result; 00597 } CachedRangeQuery; 00598 00599 std::pair<int, int> getPreprocessedEntitiesInRangeSlow(SourceRange R); 00600 00601 friend class ASTReader; 00602 friend class ASTWriter; 00603 }; 00604 } // end namespace clang 00605 00606 inline void* operator new(size_t bytes, clang::PreprocessingRecord& PR, 00607 unsigned alignment) throw() { 00608 return PR.Allocate(bytes, alignment); 00609 } 00610 00611 inline void operator delete(void* ptr, clang::PreprocessingRecord& PR, 00612 unsigned) throw() { 00613 PR.Deallocate(ptr); 00614 } 00615 00616 #endif // LLVM_CLANG_LEX_PREPROCESSINGRECORD_H