PreprocessingRecord.h revision df1059cac42d613547d86b4e44c5e364bfc03073
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/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"
25#include <vector>
26
27namespace clang {
28  class IdentifierInfo;
29  class PreprocessingRecord;
30}
31
32/// \brief Allocates memory within a Clang preprocessing record.
33void* operator new(size_t bytes, clang::PreprocessingRecord& PR,
34                   unsigned alignment = 8) throw();
35
36/// \brief Frees memory allocated in a Clang preprocessing record.
37void operator delete(void* ptr, clang::PreprocessingRecord& PR,
38                     unsigned) throw();
39
40namespace clang {
41  class MacroDefinition;
42  class FileEntry;
43
44  /// \brief Base class that describes a preprocessed entity, which may be a
45  /// preprocessor directive or macro expansion.
46  class PreprocessedEntity {
47  public:
48    /// \brief The kind of preprocessed entity an object describes.
49    enum EntityKind {
50      /// \brief Indicates a problem trying to load the preprocessed entity.
51      InvalidKind,
52
53      /// \brief A macro expansion.
54      MacroExpansionKind,
55
56      /// \defgroup Preprocessing directives
57      /// @{
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      /// @}
67
68      FirstPreprocessingDirective = MacroDefinitionKind,
69      LastPreprocessingDirective = InclusionDirectiveKind
70    };
71
72  private:
73    /// \brief The kind of preprocessed entity that this object describes.
74    EntityKind Kind;
75
76    /// \brief The source range that covers this preprocessed entity.
77    SourceRange Range;
78
79  protected:
80    PreprocessedEntity(EntityKind Kind, SourceRange Range)
81      : Kind(Kind), Range(Range) { }
82
83    friend class PreprocessingRecord;
84
85  public:
86    /// \brief Retrieve the kind of preprocessed entity stored in this object.
87    EntityKind getKind() const { return Kind; }
88
89    /// \brief Retrieve the source range that covers this entire preprocessed
90    /// entity.
91    SourceRange getSourceRange() const LLVM_READONLY { return Range; }
92
93    /// \brief Returns true if there was a problem loading the preprocessed
94    /// entity.
95    bool isInvalid() const { return Kind == InvalidKind; }
96
97    // Only allow allocation of preprocessed entities using the allocator
98    // in PreprocessingRecord or by doing a placement new.
99    void* operator new(size_t bytes, PreprocessingRecord& PR,
100                       unsigned alignment = 8) throw() {
101      return ::operator new(bytes, PR, alignment);
102    }
103
104    void* operator new(size_t bytes, void* mem) throw() {
105      return mem;
106    }
107
108    void operator delete(void* ptr, PreprocessingRecord& PR,
109                         unsigned alignment) throw() {
110      return ::operator delete(ptr, PR, alignment);
111    }
112
113    void operator delete(void*, std::size_t) throw() { }
114    void operator delete(void*, void*) throw() { }
115
116  private:
117    // Make vanilla 'new' and 'delete' illegal for preprocessed entities.
118    void* operator new(size_t bytes) throw();
119    void operator delete(void* data) throw();
120  };
121
122  /// \brief Records the presence of a preprocessor directive.
123  class PreprocessingDirective : public PreprocessedEntity {
124  public:
125    PreprocessingDirective(EntityKind Kind, SourceRange Range)
126      : PreprocessedEntity(Kind, Range) { }
127
128    // Implement isa/cast/dyncast/etc.
129    static bool classof(const PreprocessedEntity *PD) {
130      return PD->getKind() >= FirstPreprocessingDirective &&
131             PD->getKind() <= LastPreprocessingDirective;
132    }
133  };
134
135  /// \brief Record the location of a macro definition.
136  class MacroDefinition : public PreprocessingDirective {
137    /// \brief The name of the macro being defined.
138    const IdentifierInfo *Name;
139
140  public:
141    explicit MacroDefinition(const IdentifierInfo *Name, SourceRange Range)
142      : PreprocessingDirective(MacroDefinitionKind, Range), Name(Name) { }
143
144    /// \brief Retrieve the name of the macro being defined.
145    const IdentifierInfo *getName() const { return Name; }
146
147    /// \brief Retrieve the location of the macro name in the definition.
148    SourceLocation getLocation() const { return getSourceRange().getBegin(); }
149
150    // Implement isa/cast/dyncast/etc.
151    static bool classof(const PreprocessedEntity *PE) {
152      return PE->getKind() == MacroDefinitionKind;
153    }
154  };
155
156  /// \brief Records the location of a macro expansion.
157  class MacroExpansion : public PreprocessedEntity {
158    /// \brief The definition of this macro or the name of the macro if it is
159    /// a builtin macro.
160    llvm::PointerUnion<IdentifierInfo *, MacroDefinition *> NameOrDef;
161
162  public:
163    MacroExpansion(IdentifierInfo *BuiltinName, SourceRange Range)
164      : PreprocessedEntity(MacroExpansionKind, Range),
165        NameOrDef(BuiltinName) { }
166
167    MacroExpansion(MacroDefinition *Definition, SourceRange Range)
168      : PreprocessedEntity(MacroExpansionKind, Range),
169        NameOrDef(Definition) { }
170
171    /// \brief True if it is a builtin macro.
172    bool isBuiltinMacro() const { return NameOrDef.is<IdentifierInfo *>(); }
173
174    /// \brief The name of the macro being expanded.
175    const IdentifierInfo *getName() const {
176      if (MacroDefinition *Def = getDefinition())
177        return Def->getName();
178      return NameOrDef.get<IdentifierInfo*>();
179    }
180
181    /// \brief The definition of the macro being expanded. May return null if
182    /// this is a builtin macro.
183    MacroDefinition *getDefinition() const {
184      return NameOrDef.dyn_cast<MacroDefinition *>();
185    }
186
187    // Implement isa/cast/dyncast/etc.
188    static bool classof(const PreprocessedEntity *PE) {
189      return PE->getKind() == MacroExpansionKind;
190    }
191  };
192
193  /// \brief Record the location of an inclusion directive, such as an
194  /// \c \#include or \c \#import statement.
195  class InclusionDirective : public PreprocessingDirective {
196  public:
197    /// \brief The kind of inclusion directives known to the
198    /// preprocessor.
199    enum InclusionKind {
200      /// \brief An \c \#include directive.
201      Include,
202      /// \brief An Objective-C \c \#import directive.
203      Import,
204      /// \brief A GNU \c \#include_next directive.
205      IncludeNext,
206      /// \brief A Clang \c \#__include_macros directive.
207      IncludeMacros
208    };
209
210  private:
211    /// \brief The name of the file that was included, as written in
212    /// the source.
213    StringRef FileName;
214
215    /// \brief Whether the file name was in quotation marks; otherwise, it was
216    /// in angle brackets.
217    unsigned InQuotes : 1;
218
219    /// \brief The kind of inclusion directive we have.
220    ///
221    /// This is a value of type InclusionKind.
222    unsigned Kind : 2;
223
224    /// \brief Whether the inclusion directive was automatically turned into
225    /// a module import.
226    unsigned ImportedModule : 1;
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, bool ImportedModule,
235                       const FileEntry *File, SourceRange Range);
236
237    /// \brief Determine what kind of inclusion directive this is.
238    InclusionKind getKind() const { return static_cast<InclusionKind>(Kind); }
239
240    /// \brief Retrieve the included file name as it was written in the source.
241    StringRef getFileName() const { return FileName; }
242
243    /// \brief Determine whether the included file name was written in quotes;
244    /// otherwise, it was written in angle brackets.
245    bool wasInQuotes() const { return InQuotes; }
246
247    /// \brief Determine whether the inclusion directive was automatically
248    /// turned into a module import.
249    bool importedModule() const { return ImportedModule; }
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  };
260
261  /// \brief An abstract class that should be subclassed by any external source
262  /// of preprocessing record entries.
263  class ExternalPreprocessingRecordSource {
264  public:
265    virtual ~ExternalPreprocessingRecordSource();
266
267    /// \brief Read a preallocated preprocessed entity from the external source.
268    ///
269    /// \returns null if an error occurred that prevented the preprocessed
270    /// entity from being loaded.
271    virtual PreprocessedEntity *ReadPreprocessedEntity(unsigned Index) = 0;
272
273    /// \brief Returns a pair of [Begin, End) indices of preallocated
274    /// preprocessed entities that \p Range encompasses.
275    virtual std::pair<unsigned, unsigned>
276        findPreprocessedEntitiesInRange(SourceRange Range) = 0;
277
278    /// \brief Optionally returns true or false if the preallocated preprocessed
279    /// entity with index \p Index came from file \p FID.
280    virtual llvm::Optional<bool> isPreprocessedEntityInFileID(unsigned Index,
281                                                              FileID FID) {
282      return llvm::Optional<bool>();
283    }
284  };
285
286  /// \brief A record of the steps taken while preprocessing a source file,
287  /// including the various preprocessing directives processed, macros
288  /// expanded, etc.
289  class PreprocessingRecord : public PPCallbacks {
290    SourceManager &SourceMgr;
291
292    /// \brief Allocator used to store preprocessing objects.
293    llvm::BumpPtrAllocator BumpAlloc;
294
295    /// \brief The set of preprocessed entities in this record, in order they
296    /// were seen.
297    std::vector<PreprocessedEntity *> PreprocessedEntities;
298
299    /// \brief The set of preprocessed entities in this record that have been
300    /// loaded from external sources.
301    ///
302    /// The entries in this vector are loaded lazily from the external source,
303    /// and are referenced by the iterator using negative indices.
304    std::vector<PreprocessedEntity *> LoadedPreprocessedEntities;
305
306    bool RecordCondDirectives;
307    SmallVector<SourceLocation, 6> CondDirectiveStack;
308
309    class CondDirectiveLoc {
310      SourceLocation Loc;
311      SourceLocation RegionLoc;
312
313    public:
314      CondDirectiveLoc(SourceLocation Loc, SourceLocation RegionLoc)
315        : Loc(Loc), RegionLoc(RegionLoc) {}
316
317      SourceLocation getLoc() const { return Loc; }
318      SourceLocation getRegionLoc() const { return RegionLoc; }
319
320      class Comp {
321        SourceManager &SM;
322      public:
323        explicit Comp(SourceManager &SM) : SM(SM) {}
324        bool operator()(const CondDirectiveLoc &LHS,
325                        const CondDirectiveLoc &RHS) {
326          return SM.isBeforeInTranslationUnit(LHS.getLoc(), RHS.getLoc());
327        }
328        bool operator()(const CondDirectiveLoc &LHS, SourceLocation RHS) {
329          return SM.isBeforeInTranslationUnit(LHS.getLoc(), RHS);
330        }
331        bool operator()(SourceLocation LHS, const CondDirectiveLoc &RHS) {
332          return SM.isBeforeInTranslationUnit(LHS, RHS.getLoc());
333        }
334      };
335    };
336
337    typedef std::vector<CondDirectiveLoc> CondDirectiveLocsTy;
338    /// \brief The locations of conditional directives in source order.
339    CondDirectiveLocsTy CondDirectiveLocs;
340
341    void addCondDirectiveLoc(CondDirectiveLoc DirLoc);
342    SourceLocation findCondDirectiveRegionLoc(SourceLocation Loc) const;
343
344    /// \brief Global (loaded or local) ID for a preprocessed entity.
345    /// Negative values are used to indicate preprocessed entities
346    /// loaded from the external source while non-negative values are used to
347    /// indicate preprocessed entities introduced by the current preprocessor.
348    /// Value -1 corresponds to element 0 in the loaded entities vector,
349    /// value -2 corresponds to element 1 in the loaded entities vector, etc.
350    /// Value 0 is an invalid value, the index to local entities is 1-based,
351    /// value 1 corresponds to element 0 in the local entities vector,
352    /// value 2 corresponds to element 1 in the local entities vector, etc.
353    class PPEntityID {
354      int ID;
355      explicit PPEntityID(int ID) : ID(ID) {}
356      friend class PreprocessingRecord;
357    public:
358      PPEntityID() : ID(0) {}
359    };
360
361    static PPEntityID getPPEntityID(unsigned Index, bool isLoaded) {
362      return isLoaded ? PPEntityID(-int(Index)-1) : PPEntityID(Index+1);
363    }
364
365    /// \brief Mapping from MacroInfo structures to their definitions.
366    llvm::DenseMap<const MacroInfo *, PPEntityID> MacroDefinitions;
367
368    /// \brief External source of preprocessed entities.
369    ExternalPreprocessingRecordSource *ExternalSource;
370
371    /// \brief Retrieve the preprocessed entity at the given ID.
372    PreprocessedEntity *getPreprocessedEntity(PPEntityID PPID);
373
374    /// \brief Retrieve the loaded preprocessed entity at the given index.
375    PreprocessedEntity *getLoadedPreprocessedEntity(unsigned Index);
376
377    /// \brief Determine the number of preprocessed entities that were
378    /// loaded (or can be loaded) from an external source.
379    unsigned getNumLoadedPreprocessedEntities() const {
380      return LoadedPreprocessedEntities.size();
381    }
382
383    /// \brief Returns a pair of [Begin, End) indices of local preprocessed
384    /// entities that \p Range encompasses.
385    std::pair<unsigned, unsigned>
386      findLocalPreprocessedEntitiesInRange(SourceRange Range) const;
387    unsigned findBeginLocalPreprocessedEntity(SourceLocation Loc) const;
388    unsigned findEndLocalPreprocessedEntity(SourceLocation Loc) const;
389
390    /// \brief Allocate space for a new set of loaded preprocessed entities.
391    ///
392    /// \returns The index into the set of loaded preprocessed entities, which
393    /// corresponds to the first newly-allocated entity.
394    unsigned allocateLoadedEntities(unsigned NumEntities);
395
396    /// \brief Register a new macro definition.
397    void RegisterMacroDefinition(MacroInfo *Macro, PPEntityID PPID);
398
399  public:
400    /// \brief Construct a new preprocessing record.
401    PreprocessingRecord(SourceManager &SM, bool RecordConditionalDirectives);
402
403    /// \brief Allocate memory in the preprocessing record.
404    void *Allocate(unsigned Size, unsigned Align = 8) {
405      return BumpAlloc.Allocate(Size, Align);
406    }
407
408    /// \brief Deallocate memory in the preprocessing record.
409    void Deallocate(void *Ptr) { }
410
411    size_t getTotalMemory() const;
412
413    SourceManager &getSourceManager() const { return SourceMgr; }
414
415    // Iteration over the preprocessed entities.
416    class iterator {
417      PreprocessingRecord *Self;
418
419      /// \brief Position within the preprocessed entity sequence.
420      ///
421      /// In a complete iteration, the Position field walks the range [-M, N),
422      /// where negative values are used to indicate preprocessed entities
423      /// loaded from the external source while non-negative values are used to
424      /// indicate preprocessed entities introduced by the current preprocessor.
425      /// However, to provide iteration in source order (for, e.g., chained
426      /// precompiled headers), dereferencing the iterator flips the negative
427      /// values (corresponding to loaded entities), so that position -M
428      /// corresponds to element 0 in the loaded entities vector, position -M+1
429      /// corresponds to element 1 in the loaded entities vector, etc. This
430      /// gives us a reasonably efficient, source-order walk.
431      int Position;
432
433    public:
434      typedef PreprocessedEntity *value_type;
435      typedef value_type&         reference;
436      typedef value_type*         pointer;
437      typedef std::random_access_iterator_tag iterator_category;
438      typedef int                 difference_type;
439
440      iterator() : Self(0), Position(0) { }
441
442      iterator(PreprocessingRecord *Self, int Position)
443        : Self(Self), Position(Position) { }
444
445      value_type operator*() const {
446        bool isLoaded = Position < 0;
447        unsigned Index = isLoaded ?
448            Self->LoadedPreprocessedEntities.size() + Position : Position;
449        PPEntityID ID = Self->getPPEntityID(Index, isLoaded);
450        return Self->getPreprocessedEntity(ID);
451      }
452
453      value_type operator[](difference_type D) {
454        return *(*this + D);
455      }
456
457      iterator &operator++() {
458        ++Position;
459        return *this;
460      }
461
462      iterator operator++(int) {
463        iterator Prev(*this);
464        ++Position;
465        return Prev;
466      }
467
468      iterator &operator--() {
469        --Position;
470        return *this;
471      }
472
473      iterator operator--(int) {
474        iterator Prev(*this);
475        --Position;
476        return Prev;
477      }
478
479      friend bool operator==(const iterator &X, const iterator &Y) {
480        return X.Position == Y.Position;
481      }
482
483      friend bool operator!=(const iterator &X, const iterator &Y) {
484        return X.Position != Y.Position;
485      }
486
487      friend bool operator<(const iterator &X, const iterator &Y) {
488        return X.Position < Y.Position;
489      }
490
491      friend bool operator>(const iterator &X, const iterator &Y) {
492        return X.Position > Y.Position;
493      }
494
495      friend bool operator<=(const iterator &X, const iterator &Y) {
496        return X.Position < Y.Position;
497      }
498
499      friend bool operator>=(const iterator &X, const iterator &Y) {
500        return X.Position > Y.Position;
501      }
502
503      friend iterator& operator+=(iterator &X, difference_type D) {
504        X.Position += D;
505        return X;
506      }
507
508      friend iterator& operator-=(iterator &X, difference_type D) {
509        X.Position -= D;
510        return X;
511      }
512
513      friend iterator operator+(iterator X, difference_type D) {
514        X.Position += D;
515        return X;
516      }
517
518      friend iterator operator+(difference_type D, iterator X) {
519        X.Position += D;
520        return X;
521      }
522
523      friend difference_type operator-(const iterator &X, const iterator &Y) {
524        return X.Position - Y.Position;
525      }
526
527      friend iterator operator-(iterator X, difference_type D) {
528        X.Position -= D;
529        return X;
530      }
531      friend class PreprocessingRecord;
532    };
533    friend class iterator;
534
535    /// \brief Begin iterator for all preprocessed entities.
536    iterator begin() {
537      return iterator(this, -(int)LoadedPreprocessedEntities.size());
538    }
539
540    /// \brief End iterator for all preprocessed entities.
541    iterator end() {
542      return iterator(this, PreprocessedEntities.size());
543    }
544
545    /// \brief Begin iterator for local, non-loaded, preprocessed entities.
546    iterator local_begin() {
547      return iterator(this, 0);
548    }
549
550    /// \brief End iterator for local, non-loaded, preprocessed entities.
551    iterator local_end() {
552      return iterator(this, PreprocessedEntities.size());
553    }
554
555    /// \brief begin/end iterator pair for the given range of loaded
556    /// preprocessed entities.
557    std::pair<iterator, iterator>
558    getIteratorsForLoadedRange(unsigned start, unsigned count) {
559      unsigned end = start + count;
560      assert(end <= LoadedPreprocessedEntities.size());
561      return std::make_pair(
562                   iterator(this, int(start)-LoadedPreprocessedEntities.size()),
563                   iterator(this, int(end)-LoadedPreprocessedEntities.size()));
564    }
565
566    /// \brief Returns a pair of [Begin, End) iterators of preprocessed entities
567    /// that source range \p R encompasses.
568    ///
569    /// \param R the range to look for preprocessed entities.
570    ///
571    std::pair<iterator, iterator> getPreprocessedEntitiesInRange(SourceRange R);
572
573    /// \brief Returns true if the preprocessed entity that \p PPEI iterator
574    /// points to is coming from the file \p FID.
575    ///
576    /// Can be used to avoid implicit deserializations of preallocated
577    /// preprocessed entities if we only care about entities of a specific file
578    /// and not from files \#included in the range given at
579    /// \see getPreprocessedEntitiesInRange.
580    bool isEntityInFileID(iterator PPEI, FileID FID);
581
582    /// \brief Add a new preprocessed entity to this record.
583    PPEntityID addPreprocessedEntity(PreprocessedEntity *Entity);
584
585    /// \brief Returns true if this PreprocessingRecord is keeping track of
586    /// conditional directives locations.
587    bool isRecordingConditionalDirectives() const {
588      return RecordCondDirectives;
589    }
590
591    /// \brief Returns true if the given range intersects with a conditional
592    /// directive. if a \#if/\#endif block is fully contained within the range,
593    /// this function will return false.
594    bool rangeIntersectsConditionalDirective(SourceRange Range) const;
595
596    /// \brief Returns true if the given locations are in different regions,
597    /// separated by conditional directive blocks.
598    bool areInDifferentConditionalDirectiveRegion(SourceLocation LHS,
599                                                  SourceLocation RHS) const {
600      return findCondDirectiveRegionLoc(LHS) != findCondDirectiveRegionLoc(RHS);
601    }
602
603    /// \brief Set the external source for preprocessed entities.
604    void SetExternalSource(ExternalPreprocessingRecordSource &Source);
605
606    /// \brief Retrieve the external source for preprocessed entities.
607    ExternalPreprocessingRecordSource *getExternalSource() const {
608      return ExternalSource;
609    }
610
611    /// \brief Retrieve the macro definition that corresponds to the given
612    /// \c MacroInfo.
613    MacroDefinition *findMacroDefinition(const MacroInfo *MI);
614
615  private:
616    virtual void MacroExpands(const Token &Id, const MacroInfo* MI,
617                              SourceRange Range);
618    virtual void MacroDefined(const Token &Id, const MacroInfo *MI);
619    virtual void MacroUndefined(const Token &Id, const MacroInfo *MI);
620    virtual void InclusionDirective(SourceLocation HashLoc,
621                                    const Token &IncludeTok,
622                                    StringRef FileName,
623                                    bool IsAngled,
624                                    CharSourceRange FilenameRange,
625                                    const FileEntry *File,
626                                    StringRef SearchPath,
627                                    StringRef RelativePath,
628                                    const Module *Imported);
629    virtual void If(SourceLocation Loc, SourceRange ConditionRange);
630    virtual void Elif(SourceLocation Loc, SourceRange ConditionRange,
631                      SourceLocation IfLoc);
632    virtual void Ifdef(SourceLocation Loc, const Token &MacroNameTok);
633    virtual void Ifndef(SourceLocation Loc, const Token &MacroNameTok);
634    virtual void Else(SourceLocation Loc, SourceLocation IfLoc);
635    virtual void Endif(SourceLocation Loc, SourceLocation IfLoc);
636
637    /// \brief Cached result of the last \see getPreprocessedEntitiesInRange
638    /// query.
639    struct {
640      SourceRange Range;
641      std::pair<int, int> Result;
642    } CachedRangeQuery;
643
644    std::pair<int, int> getPreprocessedEntitiesInRangeSlow(SourceRange R);
645
646    friend class ASTReader;
647    friend class ASTWriter;
648  };
649} // end namespace clang
650
651inline void* operator new(size_t bytes, clang::PreprocessingRecord& PR,
652                          unsigned alignment) throw() {
653  return PR.Allocate(bytes, alignment);
654}
655
656inline void operator delete(void* ptr, clang::PreprocessingRecord& PR,
657                            unsigned) throw() {
658  PR.Deallocate(ptr);
659}
660
661#endif // LLVM_CLANG_LEX_PREPROCESSINGRECORD_H
662