PreprocessingRecord.h revision e1d4330adaaa7faf093e725c9c993207eb2d778a
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/ADT/Optional.h"
22#include "llvm/Support/Allocator.h"
23#include <vector>
24
25namespace clang {
26  class IdentifierInfo;
27  class PreprocessingRecord;
28}
29
30/// \brief Allocates memory within a Clang preprocessing record.
31void* operator new(size_t bytes, clang::PreprocessingRecord& PR,
32                   unsigned alignment = 8) throw();
33
34/// \brief Frees memory allocated in a Clang preprocessing record.
35void operator delete(void* ptr, clang::PreprocessingRecord& PR,
36                     unsigned) throw();
37
38namespace clang {
39  class MacroDefinition;
40  class FileEntry;
41
42  /// \brief Base class that describes a preprocessed entity, which may be a
43  /// preprocessor directive or macro expansion.
44  class PreprocessedEntity {
45  public:
46    /// \brief The kind of preprocessed entity an object describes.
47    enum EntityKind {
48      /// \brief Indicates a problem trying to load the preprocessed entity.
49      InvalidKind,
50
51      /// \brief A macro expansion.
52      MacroExpansionKind,
53
54      /// \defgroup Preprocessing directives
55      /// @{
56
57      /// \brief A macro definition.
58      MacroDefinitionKind,
59
60      /// \brief An inclusion directive, such as \c #include, \c
61      /// #import, or \c #include_next.
62      InclusionDirectiveKind,
63
64      /// @}
65
66      FirstPreprocessingDirective = MacroDefinitionKind,
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    /// \brief Optionally returns true or false if the preallocated preprocessed
275    /// entity with index \arg Index came from file \arg FID.
276    virtual llvm::Optional<bool> isPreprocessedEntityInFileID(unsigned Index,
277                                                              FileID FID) {
278      return llvm::Optional<bool>();
279    }
280  };
281
282  /// \brief A record of the steps taken while preprocessing a source file,
283  /// including the various preprocessing directives processed, macros
284  /// expanded, etc.
285  class PreprocessingRecord : public PPCallbacks {
286    SourceManager &SourceMgr;
287
288    /// \brief Allocator used to store preprocessing objects.
289    llvm::BumpPtrAllocator BumpAlloc;
290
291    /// \brief The set of preprocessed entities in this record, in order they
292    /// were seen.
293    std::vector<PreprocessedEntity *> PreprocessedEntities;
294
295    /// \brief The set of preprocessed entities in this record that have been
296    /// loaded from external sources.
297    ///
298    /// The entries in this vector are loaded lazily from the external source,
299    /// and are referenced by the iterator using negative indices.
300    std::vector<PreprocessedEntity *> LoadedPreprocessedEntities;
301
302    /// \brief Global (loaded or local) ID for a preprocessed entity.
303    /// Negative values are used to indicate preprocessed entities
304    /// loaded from the external source while non-negative values are used to
305    /// indicate preprocessed entities introduced by the current preprocessor.
306    /// If M is the number of loaded preprocessed entities, value -M
307    /// corresponds to element 0 in the loaded entities vector, position -M+1
308    /// corresponds to element 1 in the loaded entities vector, etc.
309    typedef int PPEntityID;
310
311    PPEntityID getPPEntityID(unsigned Index, bool isLoaded) const {
312      return isLoaded ? PPEntityID(Index) - LoadedPreprocessedEntities.size()
313                      : Index;
314    }
315
316    /// \brief Mapping from MacroInfo structures to their definitions.
317    llvm::DenseMap<const MacroInfo *, PPEntityID> MacroDefinitions;
318
319    /// \brief External source of preprocessed entities.
320    ExternalPreprocessingRecordSource *ExternalSource;
321
322    /// \brief Retrieve the preprocessed entity at the given ID.
323    PreprocessedEntity *getPreprocessedEntity(PPEntityID PPID);
324
325    /// \brief Retrieve the loaded preprocessed entity at the given index.
326    PreprocessedEntity *getLoadedPreprocessedEntity(unsigned Index);
327
328    /// \brief Determine the number of preprocessed entities that were
329    /// loaded (or can be loaded) from an external source.
330    unsigned getNumLoadedPreprocessedEntities() const {
331      return LoadedPreprocessedEntities.size();
332    }
333
334    /// \brief Returns a pair of [Begin, End) indices of local preprocessed
335    /// entities that \arg Range encompasses.
336    std::pair<unsigned, unsigned>
337      findLocalPreprocessedEntitiesInRange(SourceRange Range) const;
338    unsigned findBeginLocalPreprocessedEntity(SourceLocation Loc) const;
339    unsigned findEndLocalPreprocessedEntity(SourceLocation Loc) const;
340
341    /// \brief Allocate space for a new set of loaded preprocessed entities.
342    ///
343    /// \returns The index into the set of loaded preprocessed entities, which
344    /// corresponds to the first newly-allocated entity.
345    unsigned allocateLoadedEntities(unsigned NumEntities);
346
347    /// \brief Register a new macro definition.
348    void RegisterMacroDefinition(MacroInfo *Macro, PPEntityID PPID);
349
350  public:
351    /// \brief Construct a new preprocessing record.
352    explicit PreprocessingRecord(SourceManager &SM);
353
354    /// \brief Allocate memory in the preprocessing record.
355    void *Allocate(unsigned Size, unsigned Align = 8) {
356      return BumpAlloc.Allocate(Size, Align);
357    }
358
359    /// \brief Deallocate memory in the preprocessing record.
360    void Deallocate(void *Ptr) { }
361
362    size_t getTotalMemory() const;
363
364    SourceManager &getSourceManager() const { return SourceMgr; }
365
366    // Iteration over the preprocessed entities.
367    class iterator {
368      PreprocessingRecord *Self;
369
370      /// \brief Position within the preprocessed entity sequence.
371      ///
372      /// In a complete iteration, the Position field walks the range [-M, N),
373      /// where negative values are used to indicate preprocessed entities
374      /// loaded from the external source while non-negative values are used to
375      /// indicate preprocessed entities introduced by the current preprocessor.
376      /// However, to provide iteration in source order (for, e.g., chained
377      /// precompiled headers), dereferencing the iterator flips the negative
378      /// values (corresponding to loaded entities), so that position -M
379      /// corresponds to element 0 in the loaded entities vector, position -M+1
380      /// corresponds to element 1 in the loaded entities vector, etc. This
381      /// gives us a reasonably efficient, source-order walk.
382      PPEntityID Position;
383
384    public:
385      typedef PreprocessedEntity *value_type;
386      typedef value_type&         reference;
387      typedef value_type*         pointer;
388      typedef std::random_access_iterator_tag iterator_category;
389      typedef int                 difference_type;
390
391      iterator() : Self(0), Position(0) { }
392
393      iterator(PreprocessingRecord *Self, PPEntityID Position)
394        : Self(Self), Position(Position) { }
395
396      value_type operator*() const {
397        return Self->getPreprocessedEntity(Position);
398      }
399
400      value_type operator[](difference_type D) {
401        return *(*this + D);
402      }
403
404      iterator &operator++() {
405        ++Position;
406        return *this;
407      }
408
409      iterator operator++(int) {
410        iterator Prev(*this);
411        ++Position;
412        return Prev;
413      }
414
415      iterator &operator--() {
416        --Position;
417        return *this;
418      }
419
420      iterator operator--(int) {
421        iterator Prev(*this);
422        --Position;
423        return Prev;
424      }
425
426      friend bool operator==(const iterator &X, const iterator &Y) {
427        return X.Position == Y.Position;
428      }
429
430      friend bool operator!=(const iterator &X, const iterator &Y) {
431        return X.Position != Y.Position;
432      }
433
434      friend bool operator<(const iterator &X, const iterator &Y) {
435        return X.Position < Y.Position;
436      }
437
438      friend bool operator>(const iterator &X, const iterator &Y) {
439        return X.Position > Y.Position;
440      }
441
442      friend bool operator<=(const iterator &X, const iterator &Y) {
443        return X.Position < Y.Position;
444      }
445
446      friend bool operator>=(const iterator &X, const iterator &Y) {
447        return X.Position > Y.Position;
448      }
449
450      friend iterator& operator+=(iterator &X, difference_type D) {
451        X.Position += D;
452        return X;
453      }
454
455      friend iterator& operator-=(iterator &X, difference_type D) {
456        X.Position -= D;
457        return X;
458      }
459
460      friend iterator operator+(iterator X, difference_type D) {
461        X.Position += D;
462        return X;
463      }
464
465      friend iterator operator+(difference_type D, iterator X) {
466        X.Position += D;
467        return X;
468      }
469
470      friend difference_type operator-(const iterator &X, const iterator &Y) {
471        return X.Position - Y.Position;
472      }
473
474      friend iterator operator-(iterator X, difference_type D) {
475        X.Position -= D;
476        return X;
477      }
478      friend class PreprocessingRecord;
479    };
480    friend class iterator;
481
482    /// \brief Begin iterator for all preprocessed entities.
483    iterator begin() {
484      return iterator(this, -(int)LoadedPreprocessedEntities.size());
485    }
486
487    /// \brief End iterator for all preprocessed entities.
488    iterator end() {
489      return iterator(this, PreprocessedEntities.size());
490    }
491
492    /// \brief Begin iterator for local, non-loaded, preprocessed entities.
493    iterator local_begin() {
494      return iterator(this, 0);
495    }
496
497    /// \brief End iterator for local, non-loaded, preprocessed entities.
498    iterator local_end() {
499      return iterator(this, PreprocessedEntities.size());
500    }
501
502    /// \brief Returns a pair of [Begin, End) iterators of preprocessed entities
503    /// that source range \arg R encompasses.
504    ///
505    /// \param R the range to look for preprocessed entities.
506    ///
507    std::pair<iterator, iterator> getPreprocessedEntitiesInRange(SourceRange R);
508
509    /// \brief Returns true if the preprocessed entity that \arg PPEI iterator
510    /// points to is coming from the file \arg FID.
511    ///
512    /// Can be used to avoid implicit deserializations of preallocated
513    /// preprocessed entities if we only care about entities of a specific file
514    /// and not from files #included in the range given at
515    /// \see getPreprocessedEntitiesInRange.
516    bool isEntityInFileID(iterator PPEI, FileID FID);
517
518    /// \brief Add a new preprocessed entity to this record.
519    void addPreprocessedEntity(PreprocessedEntity *Entity);
520
521    /// \brief Set the external source for preprocessed entities.
522    void SetExternalSource(ExternalPreprocessingRecordSource &Source);
523
524    /// \brief Retrieve the external source for preprocessed entities.
525    ExternalPreprocessingRecordSource *getExternalSource() const {
526      return ExternalSource;
527    }
528
529    /// \brief Retrieve the macro definition that corresponds to the given
530    /// \c MacroInfo.
531    MacroDefinition *findMacroDefinition(const MacroInfo *MI);
532
533    virtual void MacroExpands(const Token &Id, const MacroInfo* MI,
534                              SourceRange Range);
535    virtual void MacroDefined(const Token &Id, const MacroInfo *MI);
536    virtual void MacroUndefined(const Token &Id, const MacroInfo *MI);
537    virtual void InclusionDirective(SourceLocation HashLoc,
538                                    const Token &IncludeTok,
539                                    StringRef FileName,
540                                    bool IsAngled,
541                                    const FileEntry *File,
542                                    SourceLocation EndLoc,
543                                    StringRef SearchPath,
544                                    StringRef RelativePath);
545
546  private:
547    /// \brief Cached result of the last \see getPreprocessedEntitiesInRange
548    /// query.
549    struct {
550      SourceRange Range;
551      std::pair<PPEntityID, PPEntityID> Result;
552    } CachedRangeQuery;
553
554    std::pair<PPEntityID, PPEntityID>
555      getPreprocessedEntitiesInRangeSlow(SourceRange R);
556
557    friend class ASTReader;
558    friend class ASTWriter;
559  };
560} // end namespace clang
561
562inline void* operator new(size_t bytes, clang::PreprocessingRecord& PR,
563                          unsigned alignment) throw() {
564  return PR.Allocate(bytes, alignment);
565}
566
567inline void operator delete(void* ptr, clang::PreprocessingRecord& PR,
568                            unsigned) throw() {
569  PR.Deallocate(ptr);
570}
571
572#endif // LLVM_CLANG_LEX_PREPROCESSINGRECORD_H
573