Coverage Report

Created: 2022-05-14 11:35

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