]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/Lex/PreprocessingRecord.h
Upgrade to OpenSSH 7.3p1.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / include / clang / Lex / PreprocessingRecord.h
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/Basic/IdentifierTable.h"
18 #include "clang/Basic/SourceLocation.h"
19 #include "clang/Lex/PPCallbacks.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/Optional.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/iterator.h"
24 #include "llvm/Support/Allocator.h"
25 #include "llvm/Support/Compiler.h"
26 #include <vector>
27
28 namespace clang {
29   class IdentifierInfo;
30   class MacroInfo;
31   class PreprocessingRecord;
32 }
33
34 /// \brief Allocates memory within a Clang preprocessing record.
35 void *operator new(size_t bytes, clang::PreprocessingRecord &PR,
36                    unsigned alignment = 8) LLVM_NOEXCEPT;
37
38 /// \brief Frees memory allocated in a Clang preprocessing record.
39 void operator delete(void *ptr, clang::PreprocessingRecord &PR,
40                      unsigned) LLVM_NOEXCEPT;
41
42 namespace clang {
43   class MacroDefinitionRecord;
44   class FileEntry;
45
46   /// \brief Base class that describes a preprocessed entity, which may be a
47   /// preprocessor directive or macro expansion.
48   class PreprocessedEntity {
49   public:
50     /// \brief The kind of preprocessed entity an object describes.
51     enum EntityKind {
52       /// \brief Indicates a problem trying to load the preprocessed entity.
53       InvalidKind,
54
55       /// \brief A macro expansion.
56       MacroExpansionKind,
57       
58       /// \defgroup Preprocessing directives
59       /// @{
60       
61       /// \brief A macro definition.
62       MacroDefinitionKind,
63       
64       /// \brief An inclusion directive, such as \c \#include, \c
65       /// \#import, or \c \#include_next.
66       InclusionDirectiveKind,
67
68       /// @}
69
70       FirstPreprocessingDirective = MacroDefinitionKind,
71       LastPreprocessingDirective = InclusionDirectiveKind
72     };
73
74   private:
75     /// \brief The kind of preprocessed entity that this object describes.
76     EntityKind Kind;
77     
78     /// \brief The source range that covers this preprocessed entity.
79     SourceRange Range;
80     
81   protected:
82     PreprocessedEntity(EntityKind Kind, SourceRange Range)
83       : Kind(Kind), Range(Range) { }
84
85     friend class PreprocessingRecord;
86
87   public:
88     /// \brief Retrieve the kind of preprocessed entity stored in this object.
89     EntityKind getKind() const { return Kind; }
90     
91     /// \brief Retrieve the source range that covers this entire preprocessed 
92     /// entity.
93     SourceRange getSourceRange() const LLVM_READONLY { return Range; }
94
95     /// \brief Returns true if there was a problem loading the preprocessed
96     /// entity.
97     bool isInvalid() const { return Kind == InvalidKind; }
98
99     // Only allow allocation of preprocessed entities using the allocator 
100     // in PreprocessingRecord or by doing a placement new.
101     void *operator new(size_t bytes, PreprocessingRecord &PR,
102                        unsigned alignment = 8) LLVM_NOEXCEPT {
103       return ::operator new(bytes, PR, alignment);
104     }
105
106     void *operator new(size_t bytes, void *mem) LLVM_NOEXCEPT { return mem; }
107
108     void operator delete(void *ptr, PreprocessingRecord &PR,
109                          unsigned alignment) LLVM_NOEXCEPT {
110       return ::operator delete(ptr, PR, alignment);
111     }
112
113     void operator delete(void *, std::size_t) LLVM_NOEXCEPT {}
114     void operator delete(void *, void *) LLVM_NOEXCEPT {}
115
116   private:
117     // Make vanilla 'new' and 'delete' illegal for preprocessed entities.
118     void *operator new(size_t bytes) LLVM_NOEXCEPT;
119     void operator delete(void *data) LLVM_NOEXCEPT;
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 MacroDefinitionRecord : public PreprocessingDirective {
137     /// \brief The name of the macro being defined.
138     const IdentifierInfo *Name;
139
140   public:
141     explicit MacroDefinitionRecord(const IdentifierInfo *Name,
142                                    SourceRange Range)
143         : PreprocessingDirective(MacroDefinitionKind, Range), Name(Name) {}
144
145     /// \brief Retrieve the name of the macro being defined.
146     const IdentifierInfo *getName() const { return Name; }
147
148     /// \brief Retrieve the location of the macro name in the definition.
149     SourceLocation getLocation() const { return getSourceRange().getBegin(); }
150     
151     // Implement isa/cast/dyncast/etc.
152     static bool classof(const PreprocessedEntity *PE) {
153       return PE->getKind() == MacroDefinitionKind;
154     }
155   };
156   
157   /// \brief Records the location of a macro expansion.
158   class MacroExpansion : public PreprocessedEntity {
159     /// \brief The definition of this macro or the name of the macro if it is
160     /// a builtin macro.
161     llvm::PointerUnion<IdentifierInfo *, MacroDefinitionRecord *> NameOrDef;
162
163   public:
164     MacroExpansion(IdentifierInfo *BuiltinName, SourceRange Range)
165         : PreprocessedEntity(MacroExpansionKind, Range),
166           NameOrDef(BuiltinName) {}
167
168     MacroExpansion(MacroDefinitionRecord *Definition, SourceRange Range)
169         : PreprocessedEntity(MacroExpansionKind, Range), NameOrDef(Definition) {
170     }
171
172     /// \brief True if it is a builtin macro.
173     bool isBuiltinMacro() const { return NameOrDef.is<IdentifierInfo *>(); }
174
175     /// \brief The name of the macro being expanded.
176     const IdentifierInfo *getName() const {
177       if (MacroDefinitionRecord *Def = getDefinition())
178         return Def->getName();
179       return NameOrDef.get<IdentifierInfo *>();
180     }
181
182     /// \brief The definition of the macro being expanded. May return null if
183     /// this is a builtin macro.
184     MacroDefinitionRecord *getDefinition() const {
185       return NameOrDef.dyn_cast<MacroDefinitionRecord *>();
186     }
187
188     // Implement isa/cast/dyncast/etc.
189     static bool classof(const PreprocessedEntity *PE) {
190       return PE->getKind() == MacroExpansionKind;
191     }
192   };
193
194   /// \brief Record the location of an inclusion directive, such as an
195   /// \c \#include or \c \#import statement.
196   class InclusionDirective : public PreprocessingDirective {
197   public:
198     /// \brief The kind of inclusion directives known to the
199     /// preprocessor.
200     enum InclusionKind {
201       /// \brief An \c \#include directive.
202       Include,
203       /// \brief An Objective-C \c \#import directive.
204       Import,
205       /// \brief A GNU \c \#include_next directive.
206       IncludeNext,
207       /// \brief A Clang \c \#__include_macros directive.
208       IncludeMacros
209     };
210
211   private:
212     /// \brief The name of the file that was included, as written in
213     /// the source.
214     StringRef FileName;
215
216     /// \brief Whether the file name was in quotation marks; otherwise, it was
217     /// in angle brackets.
218     unsigned InQuotes : 1;
219
220     /// \brief The kind of inclusion directive we have.
221     ///
222     /// This is a value of type InclusionKind.
223     unsigned Kind : 2;
224
225     /// \brief Whether the inclusion directive was automatically turned into
226     /// a module import.
227     unsigned ImportedModule : 1;
228
229     /// \brief The file that was included.
230     const FileEntry *File;
231
232   public:
233     InclusionDirective(PreprocessingRecord &PPRec,
234                        InclusionKind Kind, StringRef FileName, 
235                        bool InQuotes, bool ImportedModule,
236                        const FileEntry *File, SourceRange Range);
237     
238     /// \brief Determine what kind of inclusion directive this is.
239     InclusionKind getKind() const { return static_cast<InclusionKind>(Kind); }
240     
241     /// \brief Retrieve the included file name as it was written in the source.
242     StringRef getFileName() const { return FileName; }
243     
244     /// \brief Determine whether the included file name was written in quotes;
245     /// otherwise, it was written in angle brackets.
246     bool wasInQuotes() const { return InQuotes; }
247
248     /// \brief Determine whether the inclusion directive was automatically
249     /// turned into a module import.
250     bool importedModule() const { return ImportedModule; }
251     
252     /// \brief Retrieve the file entry for the actual file that was included
253     /// by this directive.
254     const FileEntry *getFile() const { return File; }
255         
256     // Implement isa/cast/dyncast/etc.
257     static bool classof(const PreprocessedEntity *PE) {
258       return PE->getKind() == InclusionDirectiveKind;
259     }
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 \p Range encompasses.
276     virtual std::pair<unsigned, unsigned>
277         findPreprocessedEntitiesInRange(SourceRange Range) = 0;
278
279     /// \brief Optionally returns true or false if the preallocated preprocessed
280     /// entity with index \p Index came from file \p FID.
281     virtual Optional<bool> isPreprocessedEntityInFileID(unsigned Index,
282                                                         FileID FID) {
283       return None;
284     }
285   };
286   
287   /// \brief A record of the steps taken while preprocessing a source file,
288   /// including the various preprocessing directives processed, macros 
289   /// expanded, etc.
290   class PreprocessingRecord : public PPCallbacks {
291     SourceManager &SourceMgr;
292     
293     /// \brief Allocator used to store preprocessing objects.
294     llvm::BumpPtrAllocator BumpAlloc;
295
296     /// \brief The set of preprocessed entities in this record, in order they
297     /// were seen.
298     std::vector<PreprocessedEntity *> PreprocessedEntities;
299     
300     /// \brief The set of preprocessed entities in this record that have been
301     /// loaded from external sources.
302     ///
303     /// The entries in this vector are loaded lazily from the external source,
304     /// and are referenced by the iterator using negative indices.
305     std::vector<PreprocessedEntity *> LoadedPreprocessedEntities;
306
307     /// \brief The set of ranges that were skipped by the preprocessor,
308     std::vector<SourceRange> SkippedRanges;
309
310     /// \brief Global (loaded or local) ID for a preprocessed entity.
311     /// Negative values are used to indicate preprocessed entities
312     /// loaded from the external source while non-negative values are used to
313     /// indicate preprocessed entities introduced by the current preprocessor.
314     /// Value -1 corresponds to element 0 in the loaded entities vector,
315     /// value -2 corresponds to element 1 in the loaded entities vector, etc.
316     /// Value 0 is an invalid value, the index to local entities is 1-based,
317     /// value 1 corresponds to element 0 in the local entities vector,
318     /// value 2 corresponds to element 1 in the local entities vector, etc.
319     class PPEntityID {
320       int ID;
321       explicit PPEntityID(int ID) : ID(ID) {}
322       friend class PreprocessingRecord;
323     public:
324       PPEntityID() : ID(0) {}
325     };
326
327     static PPEntityID getPPEntityID(unsigned Index, bool isLoaded) {
328       return isLoaded ? PPEntityID(-int(Index)-1) : PPEntityID(Index+1);
329     }
330
331     /// \brief Mapping from MacroInfo structures to their definitions.
332     llvm::DenseMap<const MacroInfo *, MacroDefinitionRecord *> MacroDefinitions;
333
334     /// \brief External source of preprocessed entities.
335     ExternalPreprocessingRecordSource *ExternalSource;
336
337     /// \brief Retrieve the preprocessed entity at the given ID.
338     PreprocessedEntity *getPreprocessedEntity(PPEntityID PPID);
339
340     /// \brief Retrieve the loaded preprocessed entity at the given index.
341     PreprocessedEntity *getLoadedPreprocessedEntity(unsigned Index);
342     
343     /// \brief Determine the number of preprocessed entities that were
344     /// loaded (or can be loaded) from an external source.
345     unsigned getNumLoadedPreprocessedEntities() const {
346       return LoadedPreprocessedEntities.size();
347     }
348
349     /// \brief Returns a pair of [Begin, End) indices of local preprocessed
350     /// entities that \p Range encompasses.
351     std::pair<unsigned, unsigned>
352       findLocalPreprocessedEntitiesInRange(SourceRange Range) const;
353     unsigned findBeginLocalPreprocessedEntity(SourceLocation Loc) const;
354     unsigned findEndLocalPreprocessedEntity(SourceLocation Loc) const;
355
356     /// \brief Allocate space for a new set of loaded preprocessed entities.
357     ///
358     /// \returns The index into the set of loaded preprocessed entities, which
359     /// corresponds to the first newly-allocated entity.
360     unsigned allocateLoadedEntities(unsigned NumEntities);
361
362     /// \brief Register a new macro definition.
363     void RegisterMacroDefinition(MacroInfo *Macro, MacroDefinitionRecord *Def);
364
365   public:
366     /// \brief Construct a new preprocessing record.
367     explicit PreprocessingRecord(SourceManager &SM);
368
369     /// \brief Allocate memory in the preprocessing record.
370     void *Allocate(unsigned Size, unsigned Align = 8) {
371       return BumpAlloc.Allocate(Size, Align);
372     }
373     
374     /// \brief Deallocate memory in the preprocessing record.
375     void Deallocate(void *Ptr) { }
376
377     size_t getTotalMemory() const;
378
379     SourceManager &getSourceManager() const { return SourceMgr; }
380
381     /// Iteration over the preprocessed entities.
382     ///
383     /// In a complete iteration, the iterator walks the range [-M, N),
384     /// where negative values are used to indicate preprocessed entities
385     /// loaded from the external source while non-negative values are used to
386     /// indicate preprocessed entities introduced by the current preprocessor.
387     /// However, to provide iteration in source order (for, e.g., chained
388     /// precompiled headers), dereferencing the iterator flips the negative
389     /// values (corresponding to loaded entities), so that position -M
390     /// corresponds to element 0 in the loaded entities vector, position -M+1
391     /// corresponds to element 1 in the loaded entities vector, etc. This
392     /// gives us a reasonably efficient, source-order walk.
393     ///
394     /// We define this as a wrapping iterator around an int. The
395     /// iterator_adaptor_base class forwards the iterator methods to basic
396     /// integer arithmetic.
397     class iterator : public llvm::iterator_adaptor_base<
398                          iterator, int, std::random_access_iterator_tag,
399                          PreprocessedEntity *, int, PreprocessedEntity *,
400                          PreprocessedEntity *> {
401       PreprocessingRecord *Self;
402
403       iterator(PreprocessingRecord *Self, int Position)
404           : iterator::iterator_adaptor_base(Position), Self(Self) {}
405       friend class PreprocessingRecord;
406
407     public:
408       iterator() : iterator(nullptr, 0) {}
409
410       PreprocessedEntity *operator*() const {
411         bool isLoaded = this->I < 0;
412         unsigned Index = isLoaded ?
413             Self->LoadedPreprocessedEntities.size() + this->I : this->I;
414         PPEntityID ID = Self->getPPEntityID(Index, isLoaded);
415         return Self->getPreprocessedEntity(ID);
416       }
417       PreprocessedEntity *operator->() const { return **this; }
418     };
419
420     /// \brief Begin iterator for all preprocessed entities.
421     iterator begin() {
422       return iterator(this, -(int)LoadedPreprocessedEntities.size());
423     }
424
425     /// \brief End iterator for all preprocessed entities.
426     iterator end() {
427       return iterator(this, PreprocessedEntities.size());
428     }
429
430     /// \brief Begin iterator for local, non-loaded, preprocessed entities.
431     iterator local_begin() {
432       return iterator(this, 0);
433     }
434
435     /// \brief End iterator for local, non-loaded, preprocessed entities.
436     iterator local_end() {
437       return iterator(this, PreprocessedEntities.size());
438     }
439
440     /// \brief iterator range for the given range of loaded
441     /// preprocessed entities.
442     llvm::iterator_range<iterator> getIteratorsForLoadedRange(unsigned start,
443                                                               unsigned count) {
444       unsigned end = start + count;
445       assert(end <= LoadedPreprocessedEntities.size());
446       return llvm::make_range(
447           iterator(this, int(start) - LoadedPreprocessedEntities.size()),
448           iterator(this, int(end) - LoadedPreprocessedEntities.size()));
449     }
450
451     /// \brief Returns a range of preprocessed entities that source range \p R
452     /// encompasses.
453     ///
454     /// \param R the range to look for preprocessed entities.
455     ///
456     llvm::iterator_range<iterator>
457     getPreprocessedEntitiesInRange(SourceRange R);
458
459     /// \brief Returns true if the preprocessed entity that \p PPEI iterator
460     /// points to is coming from the file \p FID.
461     ///
462     /// Can be used to avoid implicit deserializations of preallocated
463     /// preprocessed entities if we only care about entities of a specific file
464     /// and not from files \#included in the range given at
465     /// \see getPreprocessedEntitiesInRange.
466     bool isEntityInFileID(iterator PPEI, FileID FID);
467
468     /// \brief Add a new preprocessed entity to this record.
469     PPEntityID addPreprocessedEntity(PreprocessedEntity *Entity);
470
471     /// \brief Set the external source for preprocessed entities.
472     void SetExternalSource(ExternalPreprocessingRecordSource &Source);
473
474     /// \brief Retrieve the external source for preprocessed entities.
475     ExternalPreprocessingRecordSource *getExternalSource() const {
476       return ExternalSource;
477     }
478
479     /// \brief Retrieve the macro definition that corresponds to the given
480     /// \c MacroInfo.
481     MacroDefinitionRecord *findMacroDefinition(const MacroInfo *MI);
482
483     /// \brief Retrieve all ranges that got skipped while preprocessing.
484     const std::vector<SourceRange> &getSkippedRanges() const {
485       return SkippedRanges;
486     }
487         
488   private:
489     void MacroExpands(const Token &Id, const MacroDefinition &MD,
490                       SourceRange Range, const MacroArgs *Args) override;
491     void MacroDefined(const Token &Id, const MacroDirective *MD) override;
492     void MacroUndefined(const Token &Id, const MacroDefinition &MD) override;
493     void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
494                             StringRef FileName, bool IsAngled,
495                             CharSourceRange FilenameRange,
496                             const FileEntry *File, StringRef SearchPath,
497                             StringRef RelativePath,
498                             const Module *Imported) override;
499     void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
500                const MacroDefinition &MD) override;
501     void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
502                 const MacroDefinition &MD) override;
503     /// \brief Hook called whenever the 'defined' operator is seen.
504     void Defined(const Token &MacroNameTok, const MacroDefinition &MD,
505                  SourceRange Range) override;
506
507     void SourceRangeSkipped(SourceRange Range) override;
508
509     void addMacroExpansion(const Token &Id, const MacroInfo *MI,
510                            SourceRange Range);
511
512     /// \brief Cached result of the last \see getPreprocessedEntitiesInRange
513     /// query.
514     struct {
515       SourceRange Range;
516       std::pair<int, int> Result;
517     } CachedRangeQuery;
518
519     std::pair<int, int> getPreprocessedEntitiesInRangeSlow(SourceRange R);
520
521     friend class ASTReader;
522     friend class ASTWriter;
523   };
524 } // end namespace clang
525
526 inline void *operator new(size_t bytes, clang::PreprocessingRecord &PR,
527                           unsigned alignment) LLVM_NOEXCEPT {
528   return PR.Allocate(bytes, alignment);
529 }
530
531 inline void operator delete(void *ptr, clang::PreprocessingRecord &PR,
532                             unsigned) LLVM_NOEXCEPT {
533   PR.Deallocate(ptr);
534 }
535
536 #endif // LLVM_CLANG_LEX_PREPROCESSINGRECORD_H