Coverage Report

Created: 2020-09-19 12:23

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/Serialization/ModuleFile.h
Line
Count
Source
1
//===- ModuleFile.h - Module file description -------------------*- 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 Module class, which describes a module that has
10
//  been loaded from an AST file.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#ifndef LLVM_CLANG_SERIALIZATION_MODULEFILE_H
15
#define LLVM_CLANG_SERIALIZATION_MODULEFILE_H
16
17
#include "clang/Basic/FileManager.h"
18
#include "clang/Basic/Module.h"
19
#include "clang/Basic/SourceLocation.h"
20
#include "clang/Serialization/ASTBitCodes.h"
21
#include "clang/Serialization/ContinuousRangeMap.h"
22
#include "clang/Serialization/ModuleFileExtension.h"
23
#include "llvm/ADT/DenseMap.h"
24
#include "llvm/ADT/PointerIntPair.h"
25
#include "llvm/ADT/SetVector.h"
26
#include "llvm/ADT/SmallVector.h"
27
#include "llvm/ADT/StringRef.h"
28
#include "llvm/Bitstream/BitstreamReader.h"
29
#include "llvm/Support/Endian.h"
30
#include <cassert>
31
#include <cstdint>
32
#include <memory>
33
#include <string>
34
#include <vector>
35
36
namespace clang {
37
38
namespace serialization {
39
40
/// Specifies the kind of module that has been loaded.
41
enum ModuleKind {
42
  /// File is an implicitly-loaded module.
43
  MK_ImplicitModule,
44
45
  /// File is an explicitly-loaded module.
46
  MK_ExplicitModule,
47
48
  /// File is a PCH file treated as such.
49
  MK_PCH,
50
51
  /// File is a PCH file treated as the preamble.
52
  MK_Preamble,
53
54
  /// File is a PCH file treated as the actual main file.
55
  MK_MainFile,
56
57
  /// File is from a prebuilt module path.
58
  MK_PrebuiltModule
59
};
60
61
/// The input file that has been loaded from this AST file, along with
62
/// bools indicating whether this was an overridden buffer or if it was
63
/// out-of-date or not-found.
64
class InputFile {
65
  enum {
66
    Overridden = 1,
67
    OutOfDate = 2,
68
    NotFound = 3
69
  };
70
  llvm::PointerIntPair<const FileEntry *, 2, unsigned> Val;
71
72
public:
73
1.55M
  InputFile() = default;
74
75
  InputFile(const FileEntry *File,
76
1.22M
            bool isOverridden = false, bool isOutOfDate = false) {
77
1.22M
    assert(!(isOverridden && isOutOfDate) &&
78
1.22M
           "an overridden cannot be out-of-date");
79
1.22M
    unsigned intVal = 0;
80
1.22M
    if (isOverridden)
81
808
      intVal = Overridden;
82
1.22M
    else if (isOutOfDate)
83
34
      intVal = OutOfDate;
84
1.22M
    Val.setPointerAndInt(File, intVal);
85
1.22M
  }
86
87
3
  static InputFile getNotFound() {
88
3
    InputFile File;
89
3
    File.Val.setInt(NotFound);
90
3
    return File;
91
3
  }
92
93
3.00M
  const FileEntry *getFile() const { return Val.getPointer(); }
94
299k
  bool isOverridden() const { return Val.getInt() == Overridden; }
95
1.20M
  bool isOutOfDate() const { return Val.getInt() == OutOfDate; }
96
1.22M
  bool isNotFound() const { return Val.getInt() == NotFound; }
97
};
98
99
/// Information about a module that has been loaded by the ASTReader.
100
///
101
/// Each instance of the Module class corresponds to a single AST file, which
102
/// may be a precompiled header, precompiled preamble, a module, or an AST file
103
/// of some sort loaded as the main file, all of which are specific formulations
104
/// of the general notion of a "module". A module may depend on any number of
105
/// other modules.
106
class ModuleFile {
107
public:
108
  ModuleFile(ModuleKind Kind, unsigned Generation)
109
22.0k
      : Kind(Kind), Generation(Generation) {}
110
  ~ModuleFile();
111
112
  // === General information ===
113
114
  /// The index of this module in the list of modules.
115
  unsigned Index = 0;
116
117
  /// The type of this module.
118
  ModuleKind Kind;
119
120
  /// The file name of the module file.
121
  std::string FileName;
122
123
  /// The name of the module.
124
  std::string ModuleName;
125
126
  /// The base directory of the module.
127
  std::string BaseDirectory;
128
129
16.9k
  std::string getTimestampFilename() const {
130
16.9k
    return FileName + ".timestamp";
131
16.9k
  }
132
133
  /// The original source file name that was used to build the
134
  /// primary AST file, which may have been modified for
135
  /// relocatable-pch support.
136
  std::string OriginalSourceFileName;
137
138
  /// The actual original source file name that was used to
139
  /// build this AST file.
140
  std::string ActualOriginalSourceFileName;
141
142
  /// The file ID for the original source file that was used to
143
  /// build this AST file.
144
  FileID OriginalSourceFileID;
145
146
  /// The directory that the PCH was originally created in. Used to
147
  /// allow resolving headers even after headers+PCH was moved to a new path.
148
  std::string OriginalDir;
149
150
  std::string ModuleMapPath;
151
152
  /// Whether this precompiled header is a relocatable PCH file.
153
  bool RelocatablePCH = false;
154
155
  /// Whether timestamps are included in this module file.
156
  bool HasTimestamps = false;
157
158
  /// Whether the top-level module has been read from the AST file.
159
  bool DidReadTopLevelSubmodule = false;
160
161
  /// The file entry for the module file.
162
  const FileEntry *File = nullptr;
163
164
  /// The signature of the module file, which may be used instead of the size
165
  /// and modification time to identify this particular file.
166
  ASTFileSignature Signature;
167
168
  /// The signature of the AST block of the module file, this can be used to
169
  /// unique module files based on AST contents.
170
  ASTFileSignature ASTBlockHash;
171
172
  /// Whether this module has been directly imported by the
173
  /// user.
174
  bool DirectlyImported = false;
175
176
  /// The generation of which this module file is a part.
177
  unsigned Generation;
178
179
  /// The memory buffer that stores the data associated with
180
  /// this AST file, owned by the InMemoryModuleCache.
181
  llvm::MemoryBuffer *Buffer;
182
183
  /// The size of this file, in bits.
184
  uint64_t SizeInBits = 0;
185
186
  /// The global bit offset (or base) of this module
187
  uint64_t GlobalBitOffset = 0;
188
189
  /// The bit offset of the AST block of this module.
190
  uint64_t ASTBlockStartOffset = 0;
191
192
  /// The serialized bitstream data for this file.
193
  StringRef Data;
194
195
  /// The main bitstream cursor for the main block.
196
  llvm::BitstreamCursor Stream;
197
198
  /// The source location where the module was explicitly or implicitly
199
  /// imported in the local translation unit.
200
  ///
201
  /// If module A depends on and imports module B, both modules will have the
202
  /// same DirectImportLoc, but different ImportLoc (B's ImportLoc will be a
203
  /// source location inside module A).
204
  ///
205
  /// WARNING: This is largely useless. It doesn't tell you when a module was
206
  /// made visible, just when the first submodule of that module was imported.
207
  SourceLocation DirectImportLoc;
208
209
  /// The source location where this module was first imported.
210
  SourceLocation ImportLoc;
211
212
  /// The first source location in this module.
213
  SourceLocation FirstLoc;
214
215
  /// The list of extension readers that are attached to this module
216
  /// file.
217
  std::vector<std::unique_ptr<ModuleFileExtensionReader>> ExtensionReaders;
218
219
  /// The module offset map data for this file. If non-empty, the various
220
  /// ContinuousRangeMaps described below have not yet been populated.
221
  StringRef ModuleOffsetMap;
222
223
  // === Input Files ===
224
225
  /// The cursor to the start of the input-files block.
226
  llvm::BitstreamCursor InputFilesCursor;
227
228
  /// Offsets for all of the input file entries in the AST file.
229
  const llvm::support::unaligned_uint64_t *InputFileOffsets = nullptr;
230
231
  /// The input files that have been loaded from this AST file.
232
  std::vector<InputFile> InputFilesLoaded;
233
234
  // All user input files reside at the index range [0, NumUserInputFiles), and
235
  // system input files reside at [NumUserInputFiles, InputFilesLoaded.size()).
236
  unsigned NumUserInputFiles = 0;
237
238
  /// If non-zero, specifies the time when we last validated input
239
  /// files.  Zero means we never validated them.
240
  ///
241
  /// The time is specified in seconds since the start of the Epoch.
242
  uint64_t InputFilesValidationTimestamp = 0;
243
244
  // === Source Locations ===
245
246
  /// Cursor used to read source location entries.
247
  llvm::BitstreamCursor SLocEntryCursor;
248
249
  /// The bit offset to the start of the SOURCE_MANAGER_BLOCK.
250
  uint64_t SourceManagerBlockStartOffset = 0;
251
252
  /// The number of source location entries in this AST file.
253
  unsigned LocalNumSLocEntries = 0;
254
255
  /// The base ID in the source manager's view of this module.
256
  int SLocEntryBaseID = 0;
257
258
  /// The base offset in the source manager's view of this module.
259
  unsigned SLocEntryBaseOffset = 0;
260
261
  /// Base file offset for the offsets in SLocEntryOffsets. Real file offset
262
  /// for the entry is SLocEntryOffsetsBase + SLocEntryOffsets[i].
263
  uint64_t SLocEntryOffsetsBase = 0;
264
265
  /// Offsets for all of the source location entries in the
266
  /// AST file.
267
  const uint32_t *SLocEntryOffsets = nullptr;
268
269
  /// SLocEntries that we're going to preload.
270
  SmallVector<uint64_t, 4> PreloadSLocEntries;
271
272
  /// Remapping table for source locations in this module.
273
  ContinuousRangeMap<uint32_t, int, 2> SLocRemap;
274
275
  // === Identifiers ===
276
277
  /// The number of identifiers in this AST file.
278
  unsigned LocalNumIdentifiers = 0;
279
280
  /// Offsets into the identifier table data.
281
  ///
282
  /// This array is indexed by the identifier ID (-1), and provides
283
  /// the offset into IdentifierTableData where the string data is
284
  /// stored.
285
  const uint32_t *IdentifierOffsets = nullptr;
286
287
  /// Base identifier ID for identifiers local to this module.
288
  serialization::IdentID BaseIdentifierID = 0;
289
290
  /// Remapping table for identifier IDs in this module.
291
  ContinuousRangeMap<uint32_t, int, 2> IdentifierRemap;
292
293
  /// Actual data for the on-disk hash table of identifiers.
294
  ///
295
  /// This pointer points into a memory buffer, where the on-disk hash
296
  /// table for identifiers actually lives.
297
  const char *IdentifierTableData = nullptr;
298
299
  /// A pointer to an on-disk hash table of opaque type
300
  /// IdentifierHashTable.
301
  void *IdentifierLookupTable = nullptr;
302
303
  /// Offsets of identifiers that we're going to preload within
304
  /// IdentifierTableData.
305
  std::vector<unsigned> PreloadIdentifierOffsets;
306
307
  // === Macros ===
308
309
  /// The cursor to the start of the preprocessor block, which stores
310
  /// all of the macro definitions.
311
  llvm::BitstreamCursor MacroCursor;
312
313
  /// The number of macros in this AST file.
314
  unsigned LocalNumMacros = 0;
315
316
  /// Base file offset for the offsets in MacroOffsets. Real file offset for
317
  /// the entry is MacroOffsetsBase + MacroOffsets[i].
318
  uint64_t MacroOffsetsBase = 0;
319
320
  /// Offsets of macros in the preprocessor block.
321
  ///
322
  /// This array is indexed by the macro ID (-1), and provides
323
  /// the offset into the preprocessor block where macro definitions are
324
  /// stored.
325
  const uint32_t *MacroOffsets = nullptr;
326
327
  /// Base macro ID for macros local to this module.
328
  serialization::MacroID BaseMacroID = 0;
329
330
  /// Remapping table for macro IDs in this module.
331
  ContinuousRangeMap<uint32_t, int, 2> MacroRemap;
332
333
  /// The offset of the start of the set of defined macros.
334
  uint64_t MacroStartOffset = 0;
335
336
  // === Detailed PreprocessingRecord ===
337
338
  /// The cursor to the start of the (optional) detailed preprocessing
339
  /// record block.
340
  llvm::BitstreamCursor PreprocessorDetailCursor;
341
342
  /// The offset of the start of the preprocessor detail cursor.
343
  uint64_t PreprocessorDetailStartOffset = 0;
344
345
  /// Base preprocessed entity ID for preprocessed entities local to
346
  /// this module.
347
  serialization::PreprocessedEntityID BasePreprocessedEntityID = 0;
348
349
  /// Remapping table for preprocessed entity IDs in this module.
350
  ContinuousRangeMap<uint32_t, int, 2> PreprocessedEntityRemap;
351
352
  const PPEntityOffset *PreprocessedEntityOffsets = nullptr;
353
  unsigned NumPreprocessedEntities = 0;
354
355
  /// Base ID for preprocessed skipped ranges local to this module.
356
  unsigned BasePreprocessedSkippedRangeID = 0;
357
358
  const PPSkippedRange *PreprocessedSkippedRangeOffsets = nullptr;
359
  unsigned NumPreprocessedSkippedRanges = 0;
360
361
  // === Header search information ===
362
363
  /// The number of local HeaderFileInfo structures.
364
  unsigned LocalNumHeaderFileInfos = 0;
365
366
  /// Actual data for the on-disk hash table of header file
367
  /// information.
368
  ///
369
  /// This pointer points into a memory buffer, where the on-disk hash
370
  /// table for header file information actually lives.
371
  const char *HeaderFileInfoTableData = nullptr;
372
373
  /// The on-disk hash table that contains information about each of
374
  /// the header files.
375
  void *HeaderFileInfoTable = nullptr;
376
377
  // === Submodule information ===
378
379
  /// The number of submodules in this module.
380
  unsigned LocalNumSubmodules = 0;
381
382
  /// Base submodule ID for submodules local to this module.
383
  serialization::SubmoduleID BaseSubmoduleID = 0;
384
385
  /// Remapping table for submodule IDs in this module.
386
  ContinuousRangeMap<uint32_t, int, 2> SubmoduleRemap;
387
388
  // === Selectors ===
389
390
  /// The number of selectors new to this file.
391
  ///
392
  /// This is the number of entries in SelectorOffsets.
393
  unsigned LocalNumSelectors = 0;
394
395
  /// Offsets into the selector lookup table's data array
396
  /// where each selector resides.
397
  const uint32_t *SelectorOffsets = nullptr;
398
399
  /// Base selector ID for selectors local to this module.
400
  serialization::SelectorID BaseSelectorID = 0;
401
402
  /// Remapping table for selector IDs in this module.
403
  ContinuousRangeMap<uint32_t, int, 2> SelectorRemap;
404
405
  /// A pointer to the character data that comprises the selector table
406
  ///
407
  /// The SelectorOffsets table refers into this memory.
408
  const unsigned char *SelectorLookupTableData = nullptr;
409
410
  /// A pointer to an on-disk hash table of opaque type
411
  /// ASTSelectorLookupTable.
412
  ///
413
  /// This hash table provides the IDs of all selectors, and the associated
414
  /// instance and factory methods.
415
  void *SelectorLookupTable = nullptr;
416
417
  // === Declarations ===
418
419
  /// DeclsCursor - This is a cursor to the start of the DECLTYPES_BLOCK block.
420
  /// It has read all the abbreviations at the start of the block and is ready
421
  /// to jump around with these in context.
422
  llvm::BitstreamCursor DeclsCursor;
423
424
  /// The offset to the start of the DECLTYPES_BLOCK block.
425
  uint64_t DeclsBlockStartOffset = 0;
426
427
  /// The number of declarations in this AST file.
428
  unsigned LocalNumDecls = 0;
429
430
  /// Offset of each declaration within the bitstream, indexed
431
  /// by the declaration ID (-1).
432
  const DeclOffset *DeclOffsets = nullptr;
433
434
  /// Base declaration ID for declarations local to this module.
435
  serialization::DeclID BaseDeclID = 0;
436
437
  /// Remapping table for declaration IDs in this module.
438
  ContinuousRangeMap<uint32_t, int, 2> DeclRemap;
439
440
  /// Mapping from the module files that this module file depends on
441
  /// to the base declaration ID for that module as it is understood within this
442
  /// module.
443
  ///
444
  /// This is effectively a reverse global-to-local mapping for declaration
445
  /// IDs, so that we can interpret a true global ID (for this translation unit)
446
  /// as a local ID (for this module file).
447
  llvm::DenseMap<ModuleFile *, serialization::DeclID> GlobalToLocalDeclIDs;
448
449
  /// Array of file-level DeclIDs sorted by file.
450
  const serialization::DeclID *FileSortedDecls = nullptr;
451
  unsigned NumFileSortedDecls = 0;
452
453
  /// Array of category list location information within this
454
  /// module file, sorted by the definition ID.
455
  const serialization::ObjCCategoriesInfo *ObjCCategoriesMap = nullptr;
456
457
  /// The number of redeclaration info entries in ObjCCategoriesMap.
458
  unsigned LocalNumObjCCategoriesInMap = 0;
459
460
  /// The Objective-C category lists for categories known to this
461
  /// module.
462
  SmallVector<uint64_t, 1> ObjCCategories;
463
464
  // === Types ===
465
466
  /// The number of types in this AST file.
467
  unsigned LocalNumTypes = 0;
468
469
  /// Offset of each type within the bitstream, indexed by the
470
  /// type ID, or the representation of a Type*.
471
  const UnderalignedInt64 *TypeOffsets = nullptr;
472
473
  /// Base type ID for types local to this module as represented in
474
  /// the global type ID space.
475
  serialization::TypeID BaseTypeIndex = 0;
476
477
  /// Remapping table for type IDs in this module.
478
  ContinuousRangeMap<uint32_t, int, 2> TypeRemap;
479
480
  // === Miscellaneous ===
481
482
  /// Diagnostic IDs and their mappings that the user changed.
483
  SmallVector<uint64_t, 8> PragmaDiagMappings;
484
485
  /// List of modules which depend on this module
486
  llvm::SetVector<ModuleFile *> ImportedBy;
487
488
  /// List of modules which this module depends on
489
  llvm::SetVector<ModuleFile *> Imports;
490
491
  /// Determine whether this module was directly imported at
492
  /// any point during translation.
493
3.66k
  bool isDirectlyImported() const { return DirectlyImported; }
494
495
  /// Is this a module file for a module (rather than a PCH or similar).
496
7.54M
  bool isModule() const {
497
7.54M
    return Kind == MK_ImplicitModule || 
Kind == MK_ExplicitModule618k
||
498
610k
           Kind == MK_PrebuiltModule;
499
7.54M
  }
500
501
  /// Dump debugging output for this module.
502
  void dump();
503
};
504
505
} // namespace serialization
506
507
} // namespace clang
508
509
#endif // LLVM_CLANG_SERIALIZATION_MODULEFILE_H