PreprocessingRecord.h revision 2dbaca748bc3eb6539f417bd8354c930bdf88fa4
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    /// \brief The location of the macro name in the macro definition.
143    SourceLocation Location;
144
145  public:
146    explicit MacroDefinition(const IdentifierInfo *Name, SourceLocation Location,
147                             SourceRange Range)
148      : PreprocessingDirective(MacroDefinitionKind, Range), Name(Name),
149        Location(Location) { }
150
151    /// \brief Retrieve the name of the macro being defined.
152    const IdentifierInfo *getName() const { return Name; }
153
154    /// \brief Retrieve the location of the macro name in the definition.
155    SourceLocation getLocation() const { return Location; }
156
157    // Implement isa/cast/dyncast/etc.
158    static bool classof(const PreprocessedEntity *PE) {
159      return PE->getKind() == MacroDefinitionKind;
160    }
161    static bool classof(const MacroDefinition *) { return true; }
162  };
163
164  /// \brief Records the location of a macro expansion.
165  class MacroExpansion : public PreprocessedEntity {
166    /// \brief The definition of this macro or the name of the macro if it is
167    /// a builtin macro.
168    llvm::PointerUnion<IdentifierInfo *, MacroDefinition *> NameOrDef;
169
170  public:
171    MacroExpansion(IdentifierInfo *BuiltinName, SourceRange Range)
172      : PreprocessedEntity(MacroExpansionKind, Range),
173        NameOrDef(BuiltinName) { }
174
175    MacroExpansion(MacroDefinition *Definition, SourceRange Range)
176      : PreprocessedEntity(MacroExpansionKind, Range),
177        NameOrDef(Definition) { }
178
179    /// \brief True if it is a builtin macro.
180    bool isBuiltinMacro() const { return NameOrDef.is<IdentifierInfo *>(); }
181
182    /// \brief The name of the macro being expanded.
183    const IdentifierInfo *getName() const {
184      if (MacroDefinition *Def = getDefinition())
185        return Def->getName();
186      return NameOrDef.get<IdentifierInfo*>();
187    }
188
189    /// \brief The definition of the macro being expanded. May return null if
190    /// this is a builtin macro.
191    MacroDefinition *getDefinition() const {
192      return NameOrDef.dyn_cast<MacroDefinition *>();
193    }
194
195    // Implement isa/cast/dyncast/etc.
196    static bool classof(const PreprocessedEntity *PE) {
197      return PE->getKind() == MacroExpansionKind;
198    }
199    static bool classof(const MacroExpansion *) { return true; }
200  };
201
202  /// \brief Record the location of an inclusion directive, such as an
203  /// \c #include or \c #import statement.
204  class InclusionDirective : public PreprocessingDirective {
205  public:
206    /// \brief The kind of inclusion directives known to the
207    /// preprocessor.
208    enum InclusionKind {
209      /// \brief An \c #include directive.
210      Include,
211      /// \brief An Objective-C \c #import directive.
212      Import,
213      /// \brief A GNU \c #include_next directive.
214      IncludeNext,
215      /// \brief A Clang \c #__include_macros directive.
216      IncludeMacros
217    };
218
219  private:
220    /// \brief The name of the file that was included, as written in
221    /// the source.
222    StringRef FileName;
223
224    /// \brief Whether the file name was in quotation marks; otherwise, it was
225    /// in angle brackets.
226    unsigned InQuotes : 1;
227
228    /// \brief The kind of inclusion directive we have.
229    ///
230    /// This is a value of type InclusionKind.
231    unsigned Kind : 2;
232
233    /// \brief The file that was included.
234    const FileEntry *File;
235
236  public:
237    InclusionDirective(PreprocessingRecord &PPRec,
238                       InclusionKind Kind, StringRef FileName,
239                       bool InQuotes, const FileEntry *File, SourceRange Range);
240
241    /// \brief Determine what kind of inclusion directive this is.
242    InclusionKind getKind() const { return static_cast<InclusionKind>(Kind); }
243
244    /// \brief Retrieve the included file name as it was written in the source.
245    StringRef getFileName() const { return FileName; }
246
247    /// \brief Determine whether the included file name was written in quotes;
248    /// otherwise, it was written in angle brackets.
249    bool wasInQuotes() const { return InQuotes; }
250
251    /// \brief Retrieve the file entry for the actual file that was included
252    /// by this directive.
253    const FileEntry *getFile() const { return File; }
254
255    // Implement isa/cast/dyncast/etc.
256    static bool classof(const PreprocessedEntity *PE) {
257      return PE->getKind() == InclusionDirectiveKind;
258    }
259    static bool classof(const InclusionDirective *) { return true; }
260  };
261
262  /// \brief An abstract class that should be subclassed by any external source
263  /// of preprocessing record entries.
264  class ExternalPreprocessingRecordSource {
265  public:
266    virtual ~ExternalPreprocessingRecordSource();
267
268    /// \brief Read a preallocated preprocessed entity from the external source.
269    ///
270    /// \returns null if an error occurred that prevented the preprocessed
271    /// entity from being loaded.
272    virtual PreprocessedEntity *ReadPreprocessedEntity(unsigned Index) = 0;
273
274    /// \brief Returns a pair of [Begin, End) indices of preallocated
275    /// preprocessed entities that \arg Range encompasses.
276    virtual std::pair<unsigned, unsigned>
277        findPreprocessedEntitiesInRange(SourceRange Range) = 0;
278
279    /// \brief Read the preprocessed entity at the given offset.
280    virtual PreprocessedEntity *
281    ReadPreprocessedEntityAtOffset(uint64_t Offset) = 0;
282  };
283
284  /// \brief A record of the steps taken while preprocessing a source file,
285  /// including the various preprocessing directives processed, macros
286  /// expanded, etc.
287  class PreprocessingRecord : public PPCallbacks {
288    SourceManager &SourceMgr;
289
290    /// \brief Whether we should include nested macro expansions in
291    /// the preprocessing record.
292    bool IncludeNestedMacroExpansions;
293
294    /// \brief Allocator used to store preprocessing objects.
295    llvm::BumpPtrAllocator BumpAlloc;
296
297    /// \brief The set of preprocessed entities in this record, in order they
298    /// were seen.
299    std::vector<PreprocessedEntity *> PreprocessedEntities;
300
301    /// \brief The set of preprocessed entities in this record that have been
302    /// loaded from external sources.
303    ///
304    /// The entries in this vector are loaded lazily from the external source,
305    /// and are referenced by the iterator using negative indices.
306    std::vector<PreprocessedEntity *> LoadedPreprocessedEntities;
307
308    /// \brief Global (loaded or local) ID for a preprocessed entity.
309    /// Negative values are used to indicate preprocessed entities
310    /// loaded from the external source while non-negative values are used to
311    /// indicate preprocessed entities introduced by the current preprocessor.
312    /// If M is the number of loaded preprocessed entities, value -M
313    /// corresponds to element 0 in the loaded entities vector, position -M+1
314    /// corresponds to element 1 in the loaded entities vector, etc.
315    typedef int PPEntityID;
316
317    PPEntityID getPPEntityID(unsigned Index, bool isLoaded) const {
318      return isLoaded ? PPEntityID(Index) - LoadedPreprocessedEntities.size()
319                      : Index;
320    }
321
322    /// \brief Mapping from MacroInfo structures to their definitions.
323    llvm::DenseMap<const MacroInfo *, PPEntityID> MacroDefinitions;
324
325    /// \brief External source of preprocessed entities.
326    ExternalPreprocessingRecordSource *ExternalSource;
327
328    /// \brief Retrieve the preprocessed entity at the given ID.
329    PreprocessedEntity *getPreprocessedEntity(PPEntityID PPID);
330
331    /// \brief Retrieve the loaded preprocessed entity at the given index.
332    PreprocessedEntity *getLoadedPreprocessedEntity(unsigned Index);
333
334    /// \brief Determine the number of preprocessed entities that were
335    /// loaded (or can be loaded) from an external source.
336    unsigned getNumLoadedPreprocessedEntities() const {
337      return LoadedPreprocessedEntities.size();
338    }
339
340    /// \brief Returns a pair of [Begin, End) indices of local preprocessed
341    /// entities that \arg Range encompasses.
342    std::pair<unsigned, unsigned>
343      findLocalPreprocessedEntitiesInRange(SourceRange Range) const;
344    unsigned findBeginLocalPreprocessedEntity(SourceLocation Loc) const;
345    unsigned findEndLocalPreprocessedEntity(SourceLocation Loc) const;
346
347    /// \brief Allocate space for a new set of loaded preprocessed entities.
348    ///
349    /// \returns The index into the set of loaded preprocessed entities, which
350    /// corresponds to the first newly-allocated entity.
351    unsigned allocateLoadedEntities(unsigned NumEntities);
352
353    /// \brief Register a new macro definition.
354    void RegisterMacroDefinition(MacroInfo *Macro, PPEntityID PPID);
355
356  public:
357    /// \brief Construct a new preprocessing record.
358    PreprocessingRecord(SourceManager &SM, bool IncludeNestedMacroExpansions);
359
360    /// \brief Allocate memory in the preprocessing record.
361    void *Allocate(unsigned Size, unsigned Align = 8) {
362      return BumpAlloc.Allocate(Size, Align);
363    }
364
365    /// \brief Deallocate memory in the preprocessing record.
366    void Deallocate(void *Ptr) { }
367
368    size_t getTotalMemory() const;
369
370    SourceManager &getSourceManager() const { return SourceMgr; }
371
372    // Iteration over the preprocessed entities.
373    class iterator {
374      PreprocessingRecord *Self;
375
376      /// \brief Position within the preprocessed entity sequence.
377      ///
378      /// In a complete iteration, the Position field walks the range [-M, N),
379      /// where negative values are used to indicate preprocessed entities
380      /// loaded from the external source while non-negative values are used to
381      /// indicate preprocessed entities introduced by the current preprocessor.
382      /// However, to provide iteration in source order (for, e.g., chained
383      /// precompiled headers), dereferencing the iterator flips the negative
384      /// values (corresponding to loaded entities), so that position -M
385      /// corresponds to element 0 in the loaded entities vector, position -M+1
386      /// corresponds to element 1 in the loaded entities vector, etc. This
387      /// gives us a reasonably efficient, source-order walk.
388      PPEntityID Position;
389
390    public:
391      typedef PreprocessedEntity *value_type;
392      typedef value_type&         reference;
393      typedef value_type*         pointer;
394      typedef std::random_access_iterator_tag iterator_category;
395      typedef int                 difference_type;
396
397      iterator() : Self(0), Position(0) { }
398
399      iterator(PreprocessingRecord *Self, int Position)
400        : Self(Self), Position(Position) { }
401
402      value_type operator*() const {
403        return Self->getPreprocessedEntity(Position);
404      }
405
406      value_type operator[](difference_type D) {
407        return *(*this + D);
408      }
409
410      iterator &operator++() {
411        ++Position;
412        return *this;
413      }
414
415      iterator operator++(int) {
416        iterator Prev(*this);
417        ++Position;
418        return Prev;
419      }
420
421      iterator &operator--() {
422        --Position;
423        return *this;
424      }
425
426      iterator operator--(int) {
427        iterator Prev(*this);
428        --Position;
429        return Prev;
430      }
431
432      friend bool operator==(const iterator &X, const iterator &Y) {
433        return X.Position == Y.Position;
434      }
435
436      friend bool operator!=(const iterator &X, const iterator &Y) {
437        return X.Position != Y.Position;
438      }
439
440      friend bool operator<(const iterator &X, const iterator &Y) {
441        return X.Position < Y.Position;
442      }
443
444      friend bool operator>(const iterator &X, const iterator &Y) {
445        return X.Position > Y.Position;
446      }
447
448      friend bool operator<=(const iterator &X, const iterator &Y) {
449        return X.Position < Y.Position;
450      }
451
452      friend bool operator>=(const iterator &X, const iterator &Y) {
453        return X.Position > Y.Position;
454      }
455
456      friend iterator& operator+=(iterator &X, difference_type D) {
457        X.Position += D;
458        return X;
459      }
460
461      friend iterator& operator-=(iterator &X, difference_type D) {
462        X.Position -= D;
463        return X;
464      }
465
466      friend iterator operator+(iterator X, difference_type D) {
467        X.Position += D;
468        return X;
469      }
470
471      friend iterator operator+(difference_type D, iterator X) {
472        X.Position += D;
473        return X;
474      }
475
476      friend difference_type operator-(const iterator &X, const iterator &Y) {
477        return X.Position - Y.Position;
478      }
479
480      friend iterator operator-(iterator X, difference_type D) {
481        X.Position -= D;
482        return X;
483      }
484    };
485    friend class iterator;
486
487    iterator begin(bool OnlyLocalEntities = false);
488    iterator end(bool OnlyLocalEntities = false);
489
490    /// \brief Returns a pair of [Begin, End) iterators of preprocessed entities
491    /// that source range \arg R encompasses.
492    std::pair<iterator, iterator> getPreprocessedEntitiesInRange(SourceRange R);
493
494    /// \brief Add a new preprocessed entity to this record.
495    void addPreprocessedEntity(PreprocessedEntity *Entity);
496
497    /// \brief Set the external source for preprocessed entities.
498    void SetExternalSource(ExternalPreprocessingRecordSource &Source);
499
500    /// \brief Retrieve the external source for preprocessed entities.
501    ExternalPreprocessingRecordSource *getExternalSource() const {
502      return ExternalSource;
503    }
504
505    /// \brief Retrieve the macro definition that corresponds to the given
506    /// \c MacroInfo.
507    MacroDefinition *findMacroDefinition(const MacroInfo *MI);
508
509    virtual void MacroExpands(const Token &Id, const MacroInfo* MI,
510                              SourceRange Range);
511    virtual void MacroDefined(const Token &Id, const MacroInfo *MI);
512    virtual void MacroUndefined(const Token &Id, const MacroInfo *MI);
513    virtual void InclusionDirective(SourceLocation HashLoc,
514                                    const Token &IncludeTok,
515                                    StringRef FileName,
516                                    bool IsAngled,
517                                    const FileEntry *File,
518                                    SourceLocation EndLoc,
519                                    StringRef SearchPath,
520                                    StringRef RelativePath);
521
522    friend class ASTReader;
523    friend class ASTWriter;
524  };
525} // end namespace clang
526
527inline void* operator new(size_t bytes, clang::PreprocessingRecord& PR,
528                          unsigned alignment) throw() {
529  return PR.Allocate(bytes, alignment);
530}
531
532inline void operator delete(void* ptr, clang::PreprocessingRecord& PR,
533                            unsigned) throw() {
534  PR.Deallocate(ptr);
535}
536
537#endif // LLVM_CLANG_LEX_PREPROCESSINGRECORD_H
538