PreprocessingRecord.h revision de4e0a8e57e643bbe78ad37ad6023c45a8a9f7e2
1//===--- PreprocessingRecord.h - Record of Preprocessing --------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines the PreprocessingRecord class, which maintains a record 11// of what occurred during preprocessing. 12// 13//===----------------------------------------------------------------------===// 14#ifndef LLVM_CLANG_LEX_PREPROCESSINGRECORD_H 15#define LLVM_CLANG_LEX_PREPROCESSINGRECORD_H 16 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" 22#include <vector> 23 24namespace clang { 25 class IdentifierInfo; 26 class PreprocessingRecord; 27} 28 29/// \brief Allocates memory within a Clang preprocessing record. 30void* operator new(size_t bytes, clang::PreprocessingRecord& PR, 31 unsigned alignment = 8) throw(); 32 33/// \brief Frees memory allocated in a Clang preprocessing record. 34void operator delete(void* ptr, clang::PreprocessingRecord& PR, 35 unsigned) throw(); 36 37namespace clang { 38 class MacroDefinition; 39 class FileEntry; 40 41 /// \brief Base class that describes a preprocessed entity, which may be a 42 /// preprocessor directive or macro expansion. 43 class PreprocessedEntity { 44 public: 45 /// \brief The kind of preprocessed entity an object describes. 46 enum EntityKind { 47 /// \brief Indicates a problem trying to load the preprocessed entity. 48 InvalidKind, 49 50 /// \brief A macro expansion. 51 MacroExpansionKind, 52 53 /// \brief A preprocessing directive whose kind is not specified. 54 /// 55 /// This kind will be used for any preprocessing directive that does not 56 /// have a more specific kind within the \c DirectiveKind enumeration. 57 PreprocessingDirectiveKind, 58 59 /// \brief A macro definition. 60 MacroDefinitionKind, 61 62 /// \brief An inclusion directive, such as \c #include, \c 63 /// #import, or \c #include_next. 64 InclusionDirectiveKind, 65 66 FirstPreprocessingDirective = PreprocessingDirectiveKind, 67 LastPreprocessingDirective = InclusionDirectiveKind 68 }; 69 70 private: 71 /// \brief The kind of preprocessed entity that this object describes. 72 EntityKind Kind; 73 74 /// \brief The source range that covers this preprocessed entity. 75 SourceRange Range; 76 77 protected: 78 PreprocessedEntity(EntityKind Kind, SourceRange Range) 79 : Kind(Kind), Range(Range) { } 80 81 friend class PreprocessingRecord; 82 83 public: 84 /// \brief Retrieve the kind of preprocessed entity stored in this object. 85 EntityKind getKind() const { return Kind; } 86 87 /// \brief Retrieve the source range that covers this entire preprocessed 88 /// entity. 89 SourceRange getSourceRange() const { return Range; } 90 91 /// \brief Returns true if there was a problem loading the preprocessed 92 /// entity. 93 bool isInvalid() const { return Kind == InvalidKind; } 94 95 // Implement isa/cast/dyncast/etc. 96 static bool classof(const PreprocessedEntity *) { return true; } 97 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); 103 } 104 105 void* operator new(size_t bytes, void* mem) throw() { 106 return mem; 107 } 108 109 void operator delete(void* ptr, PreprocessingRecord& PR, 110 unsigned alignment) throw() { 111 return ::operator delete(ptr, PR, alignment); 112 } 113 114 void operator delete(void*, std::size_t) throw() { } 115 void operator delete(void*, void*) throw() { } 116 117 private: 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(); 121 }; 122 123 /// \brief Records the presence of a preprocessor directive. 124 class PreprocessingDirective : public PreprocessedEntity { 125 public: 126 PreprocessingDirective(EntityKind Kind, SourceRange Range) 127 : PreprocessedEntity(Kind, Range) { } 128 129 // Implement isa/cast/dyncast/etc. 130 static bool classof(const PreprocessedEntity *PD) { 131 return PD->getKind() >= FirstPreprocessingDirective && 132 PD->getKind() <= LastPreprocessingDirective; 133 } 134 static bool classof(const PreprocessingDirective *) { return true; } 135 }; 136 137 /// \brief Record the location of a macro definition. 138 class MacroDefinition : public PreprocessingDirective { 139 /// \brief The name of the macro being defined. 140 const IdentifierInfo *Name; 141 142 public: 143 explicit MacroDefinition(const IdentifierInfo *Name, SourceRange Range) 144 : PreprocessingDirective(MacroDefinitionKind, Range), Name(Name) { } 145 146 /// \brief Retrieve the name of the macro being defined. 147 const IdentifierInfo *getName() const { return Name; } 148 149 /// \brief Retrieve the location of the macro name in the definition. 150 SourceLocation getLocation() const { return getSourceRange().getBegin(); } 151 152 // Implement isa/cast/dyncast/etc. 153 static bool classof(const PreprocessedEntity *PE) { 154 return PE->getKind() == MacroDefinitionKind; 155 } 156 static bool classof(const MacroDefinition *) { return true; } 157 }; 158 159 /// \brief Records the location of a macro expansion. 160 class MacroExpansion : public PreprocessedEntity { 161 /// \brief The definition of this macro or the name of the macro if it is 162 /// a builtin macro. 163 llvm::PointerUnion<IdentifierInfo *, MacroDefinition *> NameOrDef; 164 165 public: 166 MacroExpansion(IdentifierInfo *BuiltinName, SourceRange Range) 167 : PreprocessedEntity(MacroExpansionKind, Range), 168 NameOrDef(BuiltinName) { } 169 170 MacroExpansion(MacroDefinition *Definition, SourceRange Range) 171 : PreprocessedEntity(MacroExpansionKind, Range), 172 NameOrDef(Definition) { } 173 174 /// \brief True if it is a builtin macro. 175 bool isBuiltinMacro() const { return NameOrDef.is<IdentifierInfo *>(); } 176 177 /// \brief The name of the macro being expanded. 178 const IdentifierInfo *getName() const { 179 if (MacroDefinition *Def = getDefinition()) 180 return Def->getName(); 181 return NameOrDef.get<IdentifierInfo*>(); 182 } 183 184 /// \brief The definition of the macro being expanded. May return null if 185 /// this is a builtin macro. 186 MacroDefinition *getDefinition() const { 187 return NameOrDef.dyn_cast<MacroDefinition *>(); 188 } 189 190 // Implement isa/cast/dyncast/etc. 191 static bool classof(const PreprocessedEntity *PE) { 192 return PE->getKind() == MacroExpansionKind; 193 } 194 static bool classof(const MacroExpansion *) { return true; } 195 }; 196 197 /// \brief Record the location of an inclusion directive, such as an 198 /// \c #include or \c #import statement. 199 class InclusionDirective : public PreprocessingDirective { 200 public: 201 /// \brief The kind of inclusion directives known to the 202 /// preprocessor. 203 enum InclusionKind { 204 /// \brief An \c #include directive. 205 Include, 206 /// \brief An Objective-C \c #import directive. 207 Import, 208 /// \brief A GNU \c #include_next directive. 209 IncludeNext, 210 /// \brief A Clang \c #__include_macros directive. 211 IncludeMacros 212 }; 213 214 private: 215 /// \brief The name of the file that was included, as written in 216 /// the source. 217 StringRef FileName; 218 219 /// \brief Whether the file name was in quotation marks; otherwise, it was 220 /// in angle brackets. 221 unsigned InQuotes : 1; 222 223 /// \brief The kind of inclusion directive we have. 224 /// 225 /// This is a value of type InclusionKind. 226 unsigned Kind : 2; 227 228 /// \brief The file that was included. 229 const FileEntry *File; 230 231 public: 232 InclusionDirective(PreprocessingRecord &PPRec, 233 InclusionKind Kind, StringRef FileName, 234 bool InQuotes, const FileEntry *File, SourceRange Range); 235 236 /// \brief Determine what kind of inclusion directive this is. 237 InclusionKind getKind() const { return static_cast<InclusionKind>(Kind); } 238 239 /// \brief Retrieve the included file name as it was written in the source. 240 StringRef getFileName() const { return FileName; } 241 242 /// \brief Determine whether the included file name was written in quotes; 243 /// otherwise, it was written in angle brackets. 244 bool wasInQuotes() const { return InQuotes; } 245 246 /// \brief Retrieve the file entry for the actual file that was included 247 /// by this directive. 248 const FileEntry *getFile() const { return File; } 249 250 // Implement isa/cast/dyncast/etc. 251 static bool classof(const PreprocessedEntity *PE) { 252 return PE->getKind() == InclusionDirectiveKind; 253 } 254 static bool classof(const InclusionDirective *) { return true; } 255 }; 256 257 /// \brief An abstract class that should be subclassed by any external source 258 /// of preprocessing record entries. 259 class ExternalPreprocessingRecordSource { 260 public: 261 virtual ~ExternalPreprocessingRecordSource(); 262 263 /// \brief Read a preallocated preprocessed entity from the external source. 264 /// 265 /// \returns null if an error occurred that prevented the preprocessed 266 /// entity from being loaded. 267 virtual PreprocessedEntity *ReadPreprocessedEntity(unsigned Index) = 0; 268 269 /// \brief Returns a pair of [Begin, End) indices of preallocated 270 /// preprocessed entities that \arg Range encompasses. 271 virtual std::pair<unsigned, unsigned> 272 findPreprocessedEntitiesInRange(SourceRange Range) = 0; 273 }; 274 275 /// \brief A record of the steps taken while preprocessing a source file, 276 /// including the various preprocessing directives processed, macros 277 /// expanded, etc. 278 class PreprocessingRecord : public PPCallbacks { 279 SourceManager &SourceMgr; 280 281 /// \brief Whether we should include nested macro expansions in 282 /// the preprocessing record. 283 bool IncludeNestedMacroExpansions; 284 285 /// \brief Allocator used to store preprocessing objects. 286 llvm::BumpPtrAllocator BumpAlloc; 287 288 /// \brief The set of preprocessed entities in this record, in order they 289 /// were seen. 290 std::vector<PreprocessedEntity *> PreprocessedEntities; 291 292 /// \brief The set of preprocessed entities in this record that have been 293 /// loaded from external sources. 294 /// 295 /// The entries in this vector are loaded lazily from the external source, 296 /// and are referenced by the iterator using negative indices. 297 std::vector<PreprocessedEntity *> LoadedPreprocessedEntities; 298 299 /// \brief Global (loaded or local) ID for a preprocessed entity. 300 /// Negative values are used to indicate preprocessed entities 301 /// loaded from the external source while non-negative values are used to 302 /// indicate preprocessed entities introduced by the current preprocessor. 303 /// If M is the number of loaded preprocessed entities, value -M 304 /// corresponds to element 0 in the loaded entities vector, position -M+1 305 /// corresponds to element 1 in the loaded entities vector, etc. 306 typedef int PPEntityID; 307 308 PPEntityID getPPEntityID(unsigned Index, bool isLoaded) const { 309 return isLoaded ? PPEntityID(Index) - LoadedPreprocessedEntities.size() 310 : Index; 311 } 312 313 /// \brief Mapping from MacroInfo structures to their definitions. 314 llvm::DenseMap<const MacroInfo *, PPEntityID> MacroDefinitions; 315 316 /// \brief External source of preprocessed entities. 317 ExternalPreprocessingRecordSource *ExternalSource; 318 319 /// \brief Retrieve the preprocessed entity at the given ID. 320 PreprocessedEntity *getPreprocessedEntity(PPEntityID PPID); 321 322 /// \brief Retrieve the loaded preprocessed entity at the given index. 323 PreprocessedEntity *getLoadedPreprocessedEntity(unsigned Index); 324 325 /// \brief Determine the number of preprocessed entities that were 326 /// loaded (or can be loaded) from an external source. 327 unsigned getNumLoadedPreprocessedEntities() const { 328 return LoadedPreprocessedEntities.size(); 329 } 330 331 /// \brief Returns a pair of [Begin, End) indices of local preprocessed 332 /// entities that \arg Range encompasses. 333 std::pair<unsigned, unsigned> 334 findLocalPreprocessedEntitiesInRange(SourceRange Range) const; 335 unsigned findBeginLocalPreprocessedEntity(SourceLocation Loc) const; 336 unsigned findEndLocalPreprocessedEntity(SourceLocation Loc) const; 337 338 /// \brief Allocate space for a new set of loaded preprocessed entities. 339 /// 340 /// \returns The index into the set of loaded preprocessed entities, which 341 /// corresponds to the first newly-allocated entity. 342 unsigned allocateLoadedEntities(unsigned NumEntities); 343 344 /// \brief Register a new macro definition. 345 void RegisterMacroDefinition(MacroInfo *Macro, PPEntityID PPID); 346 347 public: 348 /// \brief Construct a new preprocessing record. 349 PreprocessingRecord(SourceManager &SM, bool IncludeNestedMacroExpansions); 350 351 /// \brief Allocate memory in the preprocessing record. 352 void *Allocate(unsigned Size, unsigned Align = 8) { 353 return BumpAlloc.Allocate(Size, Align); 354 } 355 356 /// \brief Deallocate memory in the preprocessing record. 357 void Deallocate(void *Ptr) { } 358 359 size_t getTotalMemory() const; 360 361 SourceManager &getSourceManager() const { return SourceMgr; } 362 363 // Iteration over the preprocessed entities. 364 class iterator { 365 PreprocessingRecord *Self; 366 367 /// \brief Position within the preprocessed entity sequence. 368 /// 369 /// In a complete iteration, the Position field walks the range [-M, N), 370 /// where negative values are used to indicate preprocessed entities 371 /// loaded from the external source while non-negative values are used to 372 /// indicate preprocessed entities introduced by the current preprocessor. 373 /// However, to provide iteration in source order (for, e.g., chained 374 /// precompiled headers), dereferencing the iterator flips the negative 375 /// values (corresponding to loaded entities), so that position -M 376 /// corresponds to element 0 in the loaded entities vector, position -M+1 377 /// corresponds to element 1 in the loaded entities vector, etc. This 378 /// gives us a reasonably efficient, source-order walk. 379 PPEntityID Position; 380 381 public: 382 typedef PreprocessedEntity *value_type; 383 typedef value_type& reference; 384 typedef value_type* pointer; 385 typedef std::random_access_iterator_tag iterator_category; 386 typedef int difference_type; 387 388 iterator() : Self(0), Position(0) { } 389 390 iterator(PreprocessingRecord *Self, int Position) 391 : Self(Self), Position(Position) { } 392 393 value_type operator*() const { 394 return Self->getPreprocessedEntity(Position); 395 } 396 397 value_type operator[](difference_type D) { 398 return *(*this + D); 399 } 400 401 iterator &operator++() { 402 ++Position; 403 return *this; 404 } 405 406 iterator operator++(int) { 407 iterator Prev(*this); 408 ++Position; 409 return Prev; 410 } 411 412 iterator &operator--() { 413 --Position; 414 return *this; 415 } 416 417 iterator operator--(int) { 418 iterator Prev(*this); 419 --Position; 420 return Prev; 421 } 422 423 friend bool operator==(const iterator &X, const iterator &Y) { 424 return X.Position == Y.Position; 425 } 426 427 friend bool operator!=(const iterator &X, const iterator &Y) { 428 return X.Position != Y.Position; 429 } 430 431 friend bool operator<(const iterator &X, const iterator &Y) { 432 return X.Position < Y.Position; 433 } 434 435 friend bool operator>(const iterator &X, const iterator &Y) { 436 return X.Position > Y.Position; 437 } 438 439 friend bool operator<=(const iterator &X, const iterator &Y) { 440 return X.Position < Y.Position; 441 } 442 443 friend bool operator>=(const iterator &X, const iterator &Y) { 444 return X.Position > Y.Position; 445 } 446 447 friend iterator& operator+=(iterator &X, difference_type D) { 448 X.Position += D; 449 return X; 450 } 451 452 friend iterator& operator-=(iterator &X, difference_type D) { 453 X.Position -= D; 454 return X; 455 } 456 457 friend iterator operator+(iterator X, difference_type D) { 458 X.Position += D; 459 return X; 460 } 461 462 friend iterator operator+(difference_type D, iterator X) { 463 X.Position += D; 464 return X; 465 } 466 467 friend difference_type operator-(const iterator &X, const iterator &Y) { 468 return X.Position - Y.Position; 469 } 470 471 friend iterator operator-(iterator X, difference_type D) { 472 X.Position -= D; 473 return X; 474 } 475 }; 476 friend class iterator; 477 478 /// \brief Begin iterator for all preprocessed entities. 479 iterator begin() { 480 return iterator(this, -(int)LoadedPreprocessedEntities.size()); 481 } 482 483 /// \brief End iterator for all preprocessed entities. 484 iterator end() { 485 return iterator(this, PreprocessedEntities.size()); 486 } 487 488 /// \brief Begin iterator for local, non-loaded, preprocessed entities. 489 iterator local_begin() { 490 return iterator(this, 0); 491 } 492 493 /// \brief End iterator for local, non-loaded, preprocessed entities. 494 iterator local_end() { 495 return iterator(this, PreprocessedEntities.size()); 496 } 497 498 /// \brief Returns a pair of [Begin, End) iterators of preprocessed entities 499 /// that source range \arg R encompasses. 500 std::pair<iterator, iterator> getPreprocessedEntitiesInRange(SourceRange R); 501 502 /// \brief Add a new preprocessed entity to this record. 503 void addPreprocessedEntity(PreprocessedEntity *Entity); 504 505 /// \brief Set the external source for preprocessed entities. 506 void SetExternalSource(ExternalPreprocessingRecordSource &Source); 507 508 /// \brief Retrieve the external source for preprocessed entities. 509 ExternalPreprocessingRecordSource *getExternalSource() const { 510 return ExternalSource; 511 } 512 513 /// \brief Retrieve the macro definition that corresponds to the given 514 /// \c MacroInfo. 515 MacroDefinition *findMacroDefinition(const MacroInfo *MI); 516 517 virtual void MacroExpands(const Token &Id, const MacroInfo* MI, 518 SourceRange Range); 519 virtual void MacroDefined(const Token &Id, const MacroInfo *MI); 520 virtual void MacroUndefined(const Token &Id, const MacroInfo *MI); 521 virtual void InclusionDirective(SourceLocation HashLoc, 522 const Token &IncludeTok, 523 StringRef FileName, 524 bool IsAngled, 525 const FileEntry *File, 526 SourceLocation EndLoc, 527 StringRef SearchPath, 528 StringRef RelativePath); 529 530 friend class ASTReader; 531 friend class ASTWriter; 532 }; 533} // end namespace clang 534 535inline void* operator new(size_t bytes, clang::PreprocessingRecord& PR, 536 unsigned alignment) throw() { 537 return PR.Allocate(bytes, alignment); 538} 539 540inline void operator delete(void* ptr, clang::PreprocessingRecord& PR, 541 unsigned) throw() { 542 PR.Deallocate(ptr); 543} 544 545#endif // LLVM_CLANG_LEX_PREPROCESSINGRECORD_H 546