Coverage Report

Created: 2022-05-17 06:19

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/Basic/SourceManager.h
Line
Count
Source (jump to first uncovered line)
1
//===- SourceManager.h - Track and cache source files -----------*- 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
/// \file
10
/// Defines the SourceManager interface.
11
///
12
/// There are three different types of locations in a %file: a spelling
13
/// location, an expansion location, and a presumed location.
14
///
15
/// Given an example of:
16
/// \code
17
/// #define min(x, y) x < y ? x : y
18
/// \endcode
19
///
20
/// and then later on a use of min:
21
/// \code
22
/// #line 17
23
/// return min(a, b);
24
/// \endcode
25
///
26
/// The expansion location is the line in the source code where the macro
27
/// was expanded (the return statement), the spelling location is the
28
/// location in the source where the macro was originally defined,
29
/// and the presumed location is where the line directive states that
30
/// the line is 17, or any other line.
31
//
32
//===----------------------------------------------------------------------===//
33
34
#ifndef LLVM_CLANG_BASIC_SOURCEMANAGER_H
35
#define LLVM_CLANG_BASIC_SOURCEMANAGER_H
36
37
#include "clang/Basic/Diagnostic.h"
38
#include "clang/Basic/FileEntry.h"
39
#include "clang/Basic/FileManager.h"
40
#include "clang/Basic/SourceLocation.h"
41
#include "llvm/ADT/ArrayRef.h"
42
#include "llvm/ADT/BitVector.h"
43
#include "llvm/ADT/DenseMap.h"
44
#include "llvm/ADT/DenseSet.h"
45
#include "llvm/ADT/IntrusiveRefCntPtr.h"
46
#include "llvm/ADT/PointerIntPair.h"
47
#include "llvm/ADT/SmallVector.h"
48
#include "llvm/ADT/StringRef.h"
49
#include "llvm/Support/Allocator.h"
50
#include "llvm/Support/Compiler.h"
51
#include "llvm/Support/MemoryBuffer.h"
52
#include <cassert>
53
#include <cstddef>
54
#include <map>
55
#include <memory>
56
#include <string>
57
#include <utility>
58
#include <vector>
59
60
namespace clang {
61
62
class ASTReader;
63
class ASTWriter;
64
class FileManager;
65
class LineTableInfo;
66
class SourceManager;
67
68
/// Public enums and private classes that are part of the
69
/// SourceManager implementation.
70
namespace SrcMgr {
71
72
/// Indicates whether a file or directory holds normal user code,
73
/// system code, or system code which is implicitly 'extern "C"' in C++ mode.
74
///
75
/// Entire directories can be tagged with this (this is maintained by
76
/// DirectoryLookup and friends) as can specific FileInfos when a \#pragma
77
/// system_header is seen or in various other cases.
78
///
79
enum CharacteristicKind {
80
  C_User,
81
  C_System,
82
  C_ExternCSystem,
83
  C_User_ModuleMap,
84
  C_System_ModuleMap
85
};
86
87
/// Determine whether a file / directory characteristic is for system code.
88
206M
inline bool isSystem(CharacteristicKind CK) {
89
206M
  return CK != C_User && 
CK != C_User_ModuleMap83.1M
;
90
206M
}
91
92
/// Determine whether a file characteristic is for a module map.
93
215k
inline bool isModuleMap(CharacteristicKind CK) {
94
215k
  return CK == C_User_ModuleMap || 
CK == C_System_ModuleMap211k
;
95
215k
}
96
97
/// Mapping of line offsets into a source file. This does not own the storage
98
/// for the line numbers.
99
class LineOffsetMapping {
100
public:
101
265M
  explicit operator bool() const { return Storage; }
102
602M
  unsigned size() const {
103
602M
    assert(Storage);
104
0
    return Storage[0];
105
602M
  }
106
427M
  ArrayRef<unsigned> getLines() const {
107
427M
    assert(Storage);
108
0
    return ArrayRef<unsigned>(Storage + 1, Storage + 1 + size());
109
427M
  }
110
265M
  const unsigned *begin() const { return getLines().begin(); }
111
161M
  const unsigned *end() const { return getLines().end(); }
112
91.8k
  const unsigned &operator[](int I) const { return getLines()[I]; }
113
114
  static LineOffsetMapping get(llvm::MemoryBufferRef Buffer,
115
                               llvm::BumpPtrAllocator &Alloc);
116
117
9.21M
  LineOffsetMapping() = default;
118
  LineOffsetMapping(ArrayRef<unsigned> LineOffsets,
119
                    llvm::BumpPtrAllocator &Alloc);
120
121
private:
122
  /// First element is the size, followed by elements at off-by-one indexes.
123
  unsigned *Storage = nullptr;
124
};
125
126
/// One instance of this struct is kept for every file loaded or used.
127
///
128
/// This object owns the MemoryBuffer object.
129
class alignas(8) ContentCache {
130
  /// The actual buffer containing the characters from the input
131
  /// file.
132
  mutable std::unique_ptr<llvm::MemoryBuffer> Buffer;
133
134
public:
135
  /// Reference to the file entry representing this ContentCache.
136
  ///
137
  /// This reference does not own the FileEntry object.
138
  ///
139
  /// It is possible for this to be NULL if the ContentCache encapsulates
140
  /// an imaginary text buffer.
141
  ///
142
  /// FIXME: Turn this into a FileEntryRef and remove Filename.
143
  const FileEntry *OrigEntry;
144
145
  /// References the file which the contents were actually loaded from.
146
  ///
147
  /// Can be different from 'Entry' if we overridden the contents of one file
148
  /// with the contents of another file.
149
  const FileEntry *ContentsEntry;
150
151
  /// The filename that is used to access OrigEntry.
152
  ///
153
  /// FIXME: Remove this once OrigEntry is a FileEntryRef with a stable name.
154
  StringRef Filename;
155
156
  /// A bump pointer allocated array of offsets for each source line.
157
  ///
158
  /// This is lazily computed.  The lines are owned by the SourceManager
159
  /// BumpPointerAllocator object.
160
  mutable LineOffsetMapping SourceLineCache;
161
162
  /// Indicates whether the buffer itself was provided to override
163
  /// the actual file contents.
164
  ///
165
  /// When true, the original entry may be a virtual file that does not
166
  /// exist.
167
  unsigned BufferOverridden : 1;
168
169
  /// True if this content cache was initially created for a source file
170
  /// considered to be volatile (likely to change between stat and open).
171
  unsigned IsFileVolatile : 1;
172
173
  /// True if this file may be transient, that is, if it might not
174
  /// exist at some later point in time when this content entry is used,
175
  /// after serialization and deserialization.
176
  unsigned IsTransient : 1;
177
178
  mutable unsigned IsBufferInvalid : 1;
179
180
1.26M
  ContentCache(const FileEntry *Ent = nullptr) : ContentCache(Ent, Ent) {}
Unexecuted instantiation: clang::SrcMgr::ContentCache::ContentCache(clang::FileEntry const*)
clang::SrcMgr::ContentCache::ContentCache(clang::FileEntry const*)
Line
Count
Source
180
1.26M
  ContentCache(const FileEntry *Ent = nullptr) : ContentCache(Ent, Ent) {}
181
182
  ContentCache(const FileEntry *Ent, const FileEntry *contentEnt)
183
      : OrigEntry(Ent), ContentsEntry(contentEnt), BufferOverridden(false),
184
1.26M
        IsFileVolatile(false), IsTransient(false), IsBufferInvalid(false) {}
185
186
  /// The copy ctor does not allow copies where source object has either
187
  /// a non-NULL Buffer or SourceLineCache.  Ownership of allocated memory
188
  /// is not transferred, so this is a logical error.
189
  ContentCache(const ContentCache &RHS)
190
      : BufferOverridden(false), IsFileVolatile(false), IsTransient(false),
191
0
        IsBufferInvalid(false) {
192
0
    OrigEntry = RHS.OrigEntry;
193
0
    ContentsEntry = RHS.ContentsEntry;
194
0
195
0
    assert(!RHS.Buffer && !RHS.SourceLineCache &&
196
0
           "Passed ContentCache object cannot own a buffer.");
197
0
  }
198
199
  ContentCache &operator=(const ContentCache &RHS) = delete;
200
201
  /// Returns the memory buffer for the associated content.
202
  ///
203
  /// \param Diag Object through which diagnostics will be emitted if the
204
  ///   buffer cannot be retrieved.
205
  ///
206
  /// \param Loc If specified, is the location that invalid file diagnostics
207
  ///   will be emitted at.
208
  llvm::Optional<llvm::MemoryBufferRef>
209
  getBufferOrNone(DiagnosticsEngine &Diag, FileManager &FM,
210
                  SourceLocation Loc = SourceLocation()) const;
211
212
  /// Returns the size of the content encapsulated by this
213
  /// ContentCache.
214
  ///
215
  /// This can be the size of the source file or the size of an
216
  /// arbitrary scratch buffer.  If the ContentCache encapsulates a source
217
  /// file this size is retrieved from the file's FileEntry.
218
  unsigned getSize() const;
219
220
  /// Returns the number of bytes actually mapped for this
221
  /// ContentCache.
222
  ///
223
  /// This can be 0 if the MemBuffer was not actually expanded.
224
  unsigned getSizeBytesMapped() const;
225
226
  /// Returns the kind of memory used to back the memory buffer for
227
  /// this content cache.  This is used for performance analysis.
228
  llvm::MemoryBuffer::BufferKind getMemoryBufferKind() const;
229
230
  /// Return the buffer, only if it has been loaded.
231
152
  llvm::Optional<llvm::MemoryBufferRef> getBufferIfLoaded() const {
232
152
    if (Buffer)
233
14
      return Buffer->getMemBufferRef();
234
138
    return None;
235
152
  }
236
237
  /// Return a StringRef to the source buffer data, only if it has already
238
  /// been loaded.
239
0
  llvm::Optional<StringRef> getBufferDataIfLoaded() const {
240
0
    if (Buffer)
241
0
      return Buffer->getBuffer();
242
0
    return None;
243
0
  }
244
245
  /// Set the buffer.
246
235k
  void setBuffer(std::unique_ptr<llvm::MemoryBuffer> B) {
247
235k
    IsBufferInvalid = false;
248
235k
    Buffer = std::move(B);
249
235k
  }
250
251
  /// Set the buffer to one that's not owned (or to nullptr).
252
  ///
253
  /// \pre Buffer cannot already be set.
254
55
  void setUnownedBuffer(llvm::Optional<llvm::MemoryBufferRef> B) {
255
55
    assert(!Buffer && "Expected to be called right after construction");
256
55
    if (B)
257
3
      setBuffer(llvm::MemoryBuffer::getMemBuffer(*B));
258
55
  }
259
260
  // If BufStr has an invalid BOM, returns the BOM name; otherwise, returns
261
  // nullptr
262
  static const char *getInvalidBOM(StringRef BufStr);
263
};
264
265
// Assert that the \c ContentCache objects will always be 8-byte aligned so
266
// that we can pack 3 bits of integer into pointers to such objects.
267
static_assert(alignof(ContentCache) >= 8,
268
              "ContentCache must be 8-byte aligned.");
269
270
/// Information about a FileID, basically just the logical file
271
/// that it represents and include stack information.
272
///
273
/// Each FileInfo has include stack information, indicating where it came
274
/// from. This information encodes the \#include chain that a token was
275
/// expanded from. The main include file has an invalid IncludeLoc.
276
///
277
/// FileInfo should not grow larger than ExpansionInfo. Doing so will
278
/// cause memory to bloat in compilations with many unloaded macro
279
/// expansions, since the two data structurs are stored in a union in
280
/// SLocEntry. Extra fields should instead go in "ContentCache *", which
281
/// stores file contents and other bits on the side.
282
///
283
class FileInfo {
284
  friend class clang::SourceManager;
285
  friend class clang::ASTWriter;
286
  friend class clang::ASTReader;
287
288
  /// The location of the \#include that brought in this file.
289
  ///
290
  /// This is an invalid SLOC for the main file (top of the \#include chain).
291
  SourceLocation IncludeLoc;
292
293
  /// Number of FileIDs (files and macros) that were created during
294
  /// preprocessing of this \#include, including this SLocEntry.
295
  ///
296
  /// Zero means the preprocessor didn't provide such info for this SLocEntry.
297
  unsigned NumCreatedFIDs : 31;
298
299
  /// Whether this FileInfo has any \#line directives.
300
  unsigned HasLineDirectives : 1;
301
302
  /// The content cache and the characteristic of the file.
303
  llvm::PointerIntPair<const ContentCache *, 3, CharacteristicKind>
304
      ContentAndKind;
305
306
public:
307
  /// Return a FileInfo object.
308
  static FileInfo get(SourceLocation IL, ContentCache &Con,
309
1.94M
                      CharacteristicKind FileCharacter, StringRef Filename) {
310
1.94M
    FileInfo X;
311
1.94M
    X.IncludeLoc = IL;
312
1.94M
    X.NumCreatedFIDs = 0;
313
1.94M
    X.HasLineDirectives = false;
314
1.94M
    X.ContentAndKind.setPointer(&Con);
315
1.94M
    X.ContentAndKind.setInt(FileCharacter);
316
1.94M
    Con.Filename = Filename;
317
1.94M
    return X;
318
1.94M
  }
319
320
187M
  SourceLocation getIncludeLoc() const {
321
187M
    return IncludeLoc;
322
187M
  }
323
324
397M
  const ContentCache &getContentCache() const {
325
397M
    return *ContentAndKind.getPointer();
326
397M
  }
327
328
  /// Return whether this is a system header or not.
329
170M
  CharacteristicKind getFileCharacteristic() const {
330
170M
    return ContentAndKind.getInt();
331
170M
  }
332
333
  /// Return true if this FileID has \#line directives in it.
334
377M
  bool hasLineDirectives() const { return HasLineDirectives; }
335
336
  /// Set the flag that indicates that this FileID has
337
  /// line table entries associated with it.
338
563k
  void setHasLineDirectives() { HasLineDirectives = true; }
339
340
  /// Returns the name of the file that was used when the file was loaded from
341
  /// the underlying file system.
342
419k
  StringRef getName() const { return getContentCache().Filename; }
343
};
344
345
/// Each ExpansionInfo encodes the expansion location - where
346
/// the token was ultimately expanded, and the SpellingLoc - where the actual
347
/// character data for the token came from.
348
class ExpansionInfo {
349
  // Really these are all SourceLocations.
350
351
  /// Where the spelling for the token can be found.
352
  SourceLocation SpellingLoc;
353
354
  /// In a macro expansion, ExpansionLocStart and ExpansionLocEnd
355
  /// indicate the start and end of the expansion. In object-like macros,
356
  /// they will be the same. In a function-like macro expansion, the start
357
  /// will be the identifier and the end will be the ')'. Finally, in
358
  /// macro-argument instantiations, the end will be 'SourceLocation()', an
359
  /// invalid location.
360
  SourceLocation ExpansionLocStart, ExpansionLocEnd;
361
362
  /// Whether the expansion range is a token range.
363
  bool ExpansionIsTokenRange;
364
365
public:
366
78.6M
  SourceLocation getSpellingLoc() const {
367
78.6M
    return SpellingLoc.isInvalid() ? 
getExpansionLocStart()1
:
SpellingLoc78.6M
;
368
78.6M
  }
369
370
152M
  SourceLocation getExpansionLocStart() const {
371
152M
    return ExpansionLocStart;
372
152M
  }
373
374
59.8M
  SourceLocation getExpansionLocEnd() const {
375
59.8M
    return ExpansionLocEnd.isInvalid() ? 
getExpansionLocStart()6.20M
376
59.8M
                                       : 
ExpansionLocEnd53.6M
;
377
59.8M
  }
378
379
23.4M
  bool isExpansionTokenRange() const { return ExpansionIsTokenRange; }
380
381
18.7M
  CharSourceRange getExpansionLocRange() const {
382
18.7M
    return CharSourceRange(
383
18.7M
        SourceRange(getExpansionLocStart(), getExpansionLocEnd()),
384
18.7M
        isExpansionTokenRange());
385
18.7M
  }
386
387
67.1M
  bool isMacroArgExpansion() const {
388
    // Note that this needs to return false for default constructed objects.
389
67.1M
    return getExpansionLocStart().isValid() && ExpansionLocEnd.isInvalid();
390
67.1M
  }
391
392
193k
  bool isMacroBodyExpansion() const {
393
193k
    return getExpansionLocStart().isValid() && ExpansionLocEnd.isValid();
394
193k
  }
395
396
3.07k
  bool isFunctionMacroExpansion() const {
397
3.07k
    return getExpansionLocStart().isValid() &&
398
3.07k
           getExpansionLocStart() != getExpansionLocEnd();
399
3.07k
  }
400
401
  /// Return a ExpansionInfo for an expansion.
402
  ///
403
  /// Start and End specify the expansion range (where the macro is
404
  /// expanded), and SpellingLoc specifies the spelling location (where
405
  /// the characters from the token come from). All three can refer to
406
  /// normal File SLocs or expansion locations.
407
  static ExpansionInfo create(SourceLocation SpellingLoc, SourceLocation Start,
408
                              SourceLocation End,
409
108M
                              bool ExpansionIsTokenRange = true) {
410
108M
    ExpansionInfo X;
411
108M
    X.SpellingLoc = SpellingLoc;
412
108M
    X.ExpansionLocStart = Start;
413
108M
    X.ExpansionLocEnd = End;
414
108M
    X.ExpansionIsTokenRange = ExpansionIsTokenRange;
415
108M
    return X;
416
108M
  }
417
418
  /// Return a special ExpansionInfo for the expansion of
419
  /// a macro argument into a function-like macro's body.
420
  ///
421
  /// ExpansionLoc specifies the expansion location (where the macro is
422
  /// expanded). This doesn't need to be a range because a macro is always
423
  /// expanded at a macro parameter reference, and macro parameters are
424
  /// always exactly one token. SpellingLoc specifies the spelling location
425
  /// (where the characters from the token come from). ExpansionLoc and
426
  /// SpellingLoc can both refer to normal File SLocs or expansion locations.
427
  ///
428
  /// Given the code:
429
  /// \code
430
  ///   #define F(x) f(x)
431
  ///   F(42);
432
  /// \endcode
433
  ///
434
  /// When expanding '\c F(42)', the '\c x' would call this with an
435
  /// SpellingLoc pointing at '\c 42' and an ExpansionLoc pointing at its
436
  /// location in the definition of '\c F'.
437
  static ExpansionInfo createForMacroArg(SourceLocation SpellingLoc,
438
34.9M
                                         SourceLocation ExpansionLoc) {
439
    // We store an intentionally invalid source location for the end of the
440
    // expansion range to mark that this is a macro argument location rather
441
    // than a normal one.
442
34.9M
    return create(SpellingLoc, ExpansionLoc, SourceLocation());
443
34.9M
  }
444
445
  /// Return a special ExpansionInfo representing a token that ends
446
  /// prematurely. This is used to model a '>>' token that has been split
447
  /// into '>' tokens and similar cases. Unlike for the other forms of
448
  /// expansion, the expansion range in this case is a character range, not
449
  /// a token range.
450
  static ExpansionInfo createForTokenSplit(SourceLocation SpellingLoc,
451
                                           SourceLocation Start,
452
20.6k
                                           SourceLocation End) {
453
20.6k
    return create(SpellingLoc, Start, End, false);
454
20.6k
  }
455
};
456
457
// Assert that the \c FileInfo objects are no bigger than \c ExpansionInfo
458
// objects. This controls the size of \c SLocEntry, of which we have one for
459
// each macro expansion. The number of (unloaded) macro expansions can be
460
// very large. Any other fields needed in FileInfo should go in ContentCache.
461
static_assert(sizeof(FileInfo) <= sizeof(ExpansionInfo),
462
              "FileInfo must be no larger than ExpansionInfo.");
463
464
/// This is a discriminated union of FileInfo and ExpansionInfo.
465
///
466
/// SourceManager keeps an array of these objects, and they are uniquely
467
/// identified by the FileID datatype.
468
class SLocEntry {
469
  static constexpr int OffsetBits = 8 * sizeof(SourceLocation::UIntTy) - 1;
470
  SourceLocation::UIntTy Offset : OffsetBits;
471
  SourceLocation::UIntTy IsExpansion : 1;
472
  union {
473
    FileInfo File;
474
    ExpansionInfo Expansion;
475
  };
476
477
public:
478
254M
  SLocEntry() : Offset(), IsExpansion(), File() {}
479
480
8.60G
  SourceLocation::UIntTy getOffset() const { return Offset; }
481
482
1.65G
  bool isExpansion() const { return IsExpansion; }
483
1.46G
  bool isFile() const { return !isExpansion(); }
484
485
689M
  const FileInfo &getFile() const {
486
689M
    assert(isFile() && "Not a file SLocEntry!");
487
0
    return File;
488
689M
  }
489
490
190M
  const ExpansionInfo &getExpansion() const {
491
190M
    assert(isExpansion() && "Not a macro expansion SLocEntry!");
492
0
    return Expansion;
493
190M
  }
494
495
1.94M
  static SLocEntry get(SourceLocation::UIntTy Offset, const FileInfo &FI) {
496
1.94M
    assert(!(Offset & (1ULL << OffsetBits)) && "Offset is too large");
497
0
    SLocEntry E;
498
1.94M
    E.Offset = Offset;
499
1.94M
    E.IsExpansion = false;
500
1.94M
    E.File = FI;
501
1.94M
    return E;
502
1.94M
  }
503
504
  static SLocEntry get(SourceLocation::UIntTy Offset,
505
108M
                       const ExpansionInfo &Expansion) {
506
108M
    assert(!(Offset & (1ULL << OffsetBits)) && "Offset is too large");
507
0
    SLocEntry E;
508
108M
    E.Offset = Offset;
509
108M
    E.IsExpansion = true;
510
108M
    new (&E.Expansion) ExpansionInfo(Expansion);
511
108M
    return E;
512
108M
  }
513
};
514
515
} // namespace SrcMgr
516
517
/// External source of source location entries.
518
class ExternalSLocEntrySource {
519
public:
520
  virtual ~ExternalSLocEntrySource();
521
522
  /// Read the source location entry with index ID, which will always be
523
  /// less than -1.
524
  ///
525
  /// \returns true if an error occurred that prevented the source-location
526
  /// entry from being loaded.
527
  virtual bool ReadSLocEntry(int ID) = 0;
528
529
  /// Retrieve the module import location and name for the given ID, if
530
  /// in fact it was loaded from a module (rather than, say, a precompiled
531
  /// header).
532
  virtual std::pair<SourceLocation, StringRef> getModuleImportLoc(int ID) = 0;
533
};
534
535
/// Holds the cache used by isBeforeInTranslationUnit.
536
///
537
/// The cache structure is complex enough to be worth breaking out of
538
/// SourceManager.
539
class InBeforeInTUCacheEntry {
540
  /// The FileID's of the cached query.
541
  ///
542
  /// If these match up with a subsequent query, the result can be reused.
543
  FileID LQueryFID, RQueryFID;
544
545
  /// True if LQueryFID was created before RQueryFID.
546
  ///
547
  /// This is used to compare macro expansion locations.
548
  bool IsLQFIDBeforeRQFID;
549
550
  /// The file found in common between the two \#include traces, i.e.,
551
  /// the nearest common ancestor of the \#include tree.
552
  FileID CommonFID;
553
554
  /// The offset of the previous query in CommonFID.
555
  ///
556
  /// Usually, this represents the location of the \#include for QueryFID, but
557
  /// if LQueryFID is a parent of RQueryFID (or vice versa) then these can be a
558
  /// random token in the parent.
559
  unsigned LCommonOffset, RCommonOffset;
560
561
public:
562
  /// Return true if the currently cached values match up with
563
  /// the specified LHS/RHS query.
564
  ///
565
  /// If not, we can't use the cache.
566
148k
  bool isCacheValid(FileID LHS, FileID RHS) const {
567
148k
    return LQueryFID == LHS && 
RQueryFID == RHS16.7k
;
568
148k
  }
569
570
  /// If the cache is valid, compute the result given the
571
  /// specified offsets in the LHS/RHS FileID's.
572
20.2k
  bool getCachedResult(unsigned LOffset, unsigned ROffset) const {
573
    // If one of the query files is the common file, use the offset.  Otherwise,
574
    // use the #include loc in the common file.
575
20.2k
    if (LQueryFID != CommonFID) 
LOffset = LCommonOffset12.0k
;
576
20.2k
    if (RQueryFID != CommonFID) 
ROffset = RCommonOffset12.6k
;
577
578
    // It is common for multiple macro expansions to be "included" from the same
579
    // location (expansion location), in which case use the order of the FileIDs
580
    // to determine which came first. This will also take care the case where
581
    // one of the locations points at the inclusion/expansion point of the other
582
    // in which case its FileID will come before the other.
583
20.2k
    if (LOffset == ROffset)
584
281
      return IsLQFIDBeforeRQFID;
585
586
19.9k
    return LOffset < ROffset;
587
20.2k
  }
588
589
  /// Set up a new query.
590
132k
  void setQueryFIDs(FileID LHS, FileID RHS, bool isLFIDBeforeRFID) {
591
132k
    assert(LHS != RHS);
592
0
    LQueryFID = LHS;
593
132k
    RQueryFID = RHS;
594
132k
    IsLQFIDBeforeRQFID = isLFIDBeforeRFID;
595
132k
  }
596
597
128k
  void clear() {
598
128k
    LQueryFID = RQueryFID = FileID();
599
128k
    IsLQFIDBeforeRQFID = false;
600
128k
  }
601
602
  void setCommonLoc(FileID commonFID, unsigned lCommonOffset,
603
3.44k
                    unsigned rCommonOffset) {
604
3.44k
    CommonFID = commonFID;
605
3.44k
    LCommonOffset = lCommonOffset;
606
3.44k
    RCommonOffset = rCommonOffset;
607
3.44k
  }
608
};
609
610
/// The stack used when building modules on demand, which is used
611
/// to provide a link between the source managers of the different compiler
612
/// instances.
613
using ModuleBuildStack = ArrayRef<std::pair<std::string, FullSourceLoc>>;
614
615
/// This class handles loading and caching of source files into memory.
616
///
617
/// This object owns the MemoryBuffer objects for all of the loaded
618
/// files and assigns unique FileID's for each unique \#include chain.
619
///
620
/// The SourceManager can be queried for information about SourceLocation
621
/// objects, turning them into either spelling or expansion locations. Spelling
622
/// locations represent where the bytes corresponding to a token came from and
623
/// expansion locations represent where the location is in the user's view. In
624
/// the case of a macro expansion, for example, the spelling location indicates
625
/// where the expanded token came from and the expansion location specifies
626
/// where it was expanded.
627
class SourceManager : public RefCountedBase<SourceManager> {
628
  /// DiagnosticsEngine object.
629
  DiagnosticsEngine &Diag;
630
631
  FileManager &FileMgr;
632
633
  mutable llvm::BumpPtrAllocator ContentCacheAlloc;
634
635
  /// Memoized information about all of the files tracked by this
636
  /// SourceManager.
637
  ///
638
  /// This map allows us to merge ContentCache entries based
639
  /// on their FileEntry*.  All ContentCache objects will thus have unique,
640
  /// non-null, FileEntry pointers.
641
  llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos;
642
643
  /// True if the ContentCache for files that are overridden by other
644
  /// files, should report the original file name. Defaults to true.
645
  bool OverridenFilesKeepOriginalName = true;
646
647
  /// True if non-system source files should be treated as volatile
648
  /// (likely to change while trying to use them). Defaults to false.
649
  bool UserFilesAreVolatile;
650
651
  /// True if all files read during this compilation should be treated
652
  /// as transient (may not be present in later compilations using a module
653
  /// file created from this compilation). Defaults to false.
654
  bool FilesAreTransient = false;
655
656
  struct OverriddenFilesInfoTy {
657
    /// Files that have been overridden with the contents from another
658
    /// file.
659
    llvm::DenseMap<const FileEntry *, const FileEntry *> OverriddenFiles;
660
661
    /// Files that were overridden with a memory buffer.
662
    llvm::DenseSet<const FileEntry *> OverriddenFilesWithBuffer;
663
  };
664
665
  /// Lazily create the object keeping overridden files info, since
666
  /// it is uncommonly used.
667
  std::unique_ptr<OverriddenFilesInfoTy> OverriddenFilesInfo;
668
669
5.75k
  OverriddenFilesInfoTy &getOverriddenFilesInfo() {
670
5.75k
    if (!OverriddenFilesInfo)
671
5.55k
      OverriddenFilesInfo.reset(new OverriddenFilesInfoTy);
672
5.75k
    return *OverriddenFilesInfo;
673
5.75k
  }
674
675
  /// Information about various memory buffers that we have read in.
676
  ///
677
  /// All FileEntry* within the stored ContentCache objects are NULL,
678
  /// as they do not refer to a file.
679
  std::vector<SrcMgr::ContentCache*> MemBufferInfos;
680
681
  /// The table of SLocEntries that are local to this module.
682
  ///
683
  /// Positive FileIDs are indexes into this table. Entry 0 indicates an invalid
684
  /// expansion.
685
  SmallVector<SrcMgr::SLocEntry, 0> LocalSLocEntryTable;
686
687
  /// The table of SLocEntries that are loaded from other modules.
688
  ///
689
  /// Negative FileIDs are indexes into this table. To get from ID to an index,
690
  /// use (-ID - 2).
691
  SmallVector<SrcMgr::SLocEntry, 0> LoadedSLocEntryTable;
692
693
  /// The starting offset of the next local SLocEntry.
694
  ///
695
  /// This is LocalSLocEntryTable.back().Offset + the size of that entry.
696
  SourceLocation::UIntTy NextLocalOffset;
697
698
  /// The starting offset of the latest batch of loaded SLocEntries.
699
  ///
700
  /// This is LoadedSLocEntryTable.back().Offset, except that that entry might
701
  /// not have been loaded, so that value would be unknown.
702
  SourceLocation::UIntTy CurrentLoadedOffset;
703
704
  /// The highest possible offset is 2^32-1 (2^63-1 for 64-bit source
705
  /// locations), so CurrentLoadedOffset starts at 2^31 (2^63 resp.).
706
  static const SourceLocation::UIntTy MaxLoadedOffset =
707
      1ULL << (8 * sizeof(SourceLocation::UIntTy) - 1);
708
709
  /// A bitmap that indicates whether the entries of LoadedSLocEntryTable
710
  /// have already been loaded from the external source.
711
  ///
712
  /// Same indexing as LoadedSLocEntryTable.
713
  llvm::BitVector SLocEntryLoaded;
714
715
  /// An external source for source location entries.
716
  ExternalSLocEntrySource *ExternalSLocEntries = nullptr;
717
718
  /// A one-entry cache to speed up getFileID.
719
  ///
720
  /// LastFileIDLookup records the last FileID looked up or created, because it
721
  /// is very common to look up many tokens from the same file.
722
  mutable FileID LastFileIDLookup;
723
724
  /// Holds information for \#line directives.
725
  ///
726
  /// This is referenced by indices from SLocEntryTable.
727
  std::unique_ptr<LineTableInfo> LineTable;
728
729
  /// These ivars serve as a cache used in the getLineNumber
730
  /// method which is used to speedup getLineNumber calls to nearby locations.
731
  mutable FileID LastLineNoFileIDQuery;
732
  mutable const SrcMgr::ContentCache *LastLineNoContentCache;
733
  mutable unsigned LastLineNoFilePos;
734
  mutable unsigned LastLineNoResult;
735
736
  /// The file ID for the main source file of the translation unit.
737
  FileID MainFileID;
738
739
  /// The file ID for the precompiled preamble there is one.
740
  FileID PreambleFileID;
741
742
  // Statistics for -print-stats.
743
  mutable unsigned NumLinearScans = 0;
744
  mutable unsigned NumBinaryProbes = 0;
745
746
  /// Associates a FileID with its "included/expanded in" decomposed
747
  /// location.
748
  ///
749
  /// Used to cache results from and speed-up \c getDecomposedIncludedLoc
750
  /// function.
751
  mutable llvm::DenseMap<FileID, std::pair<FileID, unsigned>> IncludedLocMap;
752
753
  /// The key value into the IsBeforeInTUCache table.
754
  using IsBeforeInTUCacheKey = std::pair<FileID, FileID>;
755
756
  /// The IsBeforeInTranslationUnitCache is a mapping from FileID pairs
757
  /// to cache results.
758
  using InBeforeInTUCache =
759
      llvm::DenseMap<IsBeforeInTUCacheKey, InBeforeInTUCacheEntry>;
760
761
  /// Cache results for the isBeforeInTranslationUnit method.
762
  mutable InBeforeInTUCache IBTUCache;
763
  mutable InBeforeInTUCacheEntry IBTUCacheOverflow;
764
765
  /// Return the cache entry for comparing the given file IDs
766
  /// for isBeforeInTranslationUnit.
767
  InBeforeInTUCacheEntry &getInBeforeInTUCache(FileID LFID, FileID RFID) const;
768
769
  // Cache for the "fake" buffer used for error-recovery purposes.
770
  mutable std::unique_ptr<llvm::MemoryBuffer> FakeBufferForRecovery;
771
772
  mutable std::unique_ptr<SrcMgr::ContentCache> FakeContentCacheForRecovery;
773
774
  mutable std::unique_ptr<SrcMgr::SLocEntry> FakeSLocEntryForRecovery;
775
776
  /// Lazily computed map of macro argument chunks to their expanded
777
  /// source location.
778
  using MacroArgsMap = std::map<unsigned, SourceLocation>;
779
780
  mutable llvm::DenseMap<FileID, std::unique_ptr<MacroArgsMap>>
781
      MacroArgsCacheMap;
782
783
  /// The stack of modules being built, which is used to detect
784
  /// cycles in the module dependency graph as modules are being built, as
785
  /// well as to describe why we're rebuilding a particular module.
786
  ///
787
  /// There is no way to set this value from the command line. If we ever need
788
  /// to do so (e.g., if on-demand module construction moves out-of-process),
789
  /// we can add a cc1-level option to do so.
790
  SmallVector<std::pair<std::string, FullSourceLoc>, 2> StoredModuleBuildStack;
791
792
public:
793
  SourceManager(DiagnosticsEngine &Diag, FileManager &FileMgr,
794
                bool UserFilesAreVolatile = false);
795
  explicit SourceManager(const SourceManager &) = delete;
796
  SourceManager &operator=(const SourceManager &) = delete;
797
  ~SourceManager();
798
799
  void clearIDTables();
800
801
  /// Initialize this source manager suitably to replay the compilation
802
  /// described by \p Old. Requires that \p Old outlive \p *this.
803
  void initializeForReplay(const SourceManager &Old);
804
805
10.4M
  DiagnosticsEngine &getDiagnostics() const { return Diag; }
806
807
276M
  FileManager &getFileManager() const { return FileMgr; }
808
809
  /// Set true if the SourceManager should report the original file name
810
  /// for contents of files that were overridden by other files. Defaults to
811
  /// true.
812
88.9k
  void setOverridenFilesKeepOriginalName(bool value) {
813
88.9k
    OverridenFilesKeepOriginalName = value;
814
88.9k
  }
815
816
  /// True if non-system source files should be treated as volatile
817
  /// (likely to change while trying to use them).
818
0
  bool userFilesAreVolatile() const { return UserFilesAreVolatile; }
819
820
  /// Retrieve the module build stack.
821
16.4k
  ModuleBuildStack getModuleBuildStack() const {
822
16.4k
    return StoredModuleBuildStack;
823
16.4k
  }
824
825
  /// Set the module build stack.
826
1.79k
  void setModuleBuildStack(ModuleBuildStack stack) {
827
1.79k
    StoredModuleBuildStack.clear();
828
1.79k
    StoredModuleBuildStack.append(stack.begin(), stack.end());
829
1.79k
  }
830
831
  /// Push an entry to the module build stack.
832
2.06k
  void pushModuleBuildStack(StringRef moduleName, FullSourceLoc importLoc) {
833
2.06k
    StoredModuleBuildStack.push_back(std::make_pair(moduleName.str(),importLoc));
834
2.06k
  }
835
836
  //===--------------------------------------------------------------------===//
837
  // MainFileID creation and querying methods.
838
  //===--------------------------------------------------------------------===//
839
840
  /// Returns the FileID of the main source file.
841
2.17M
  FileID getMainFileID() const { return MainFileID; }
842
843
  /// Set the file ID for the main source file.
844
155k
  void setMainFileID(FileID FID) {
845
155k
    MainFileID = FID;
846
155k
  }
847
848
  /// Returns true when the given FileEntry corresponds to the main file.
849
  ///
850
  /// The main file should be set prior to calling this function.
851
  bool isMainFile(const FileEntry &SourceFile);
852
853
  /// Set the file ID for the precompiled preamble.
854
339
  void setPreambleFileID(FileID Preamble) {
855
339
    assert(PreambleFileID.isInvalid() && "PreambleFileID already set!");
856
0
    PreambleFileID = Preamble;
857
339
  }
858
859
  /// Get the file ID for the precompiled preamble if there is one.
860
574k
  FileID getPreambleFileID() const { return PreambleFileID; }
861
862
  //===--------------------------------------------------------------------===//
863
  // Methods to create new FileID's and macro expansions.
864
  //===--------------------------------------------------------------------===//
865
866
  /// Create a new FileID that represents the specified file
867
  /// being \#included from the specified IncludePosition.
868
  ///
869
  /// This translates NULL into standard input.
870
  FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
871
                      SrcMgr::CharacteristicKind FileCharacter,
872
                      int LoadedID = 0,
873
                      SourceLocation::UIntTy LoadedOffset = 0);
874
875
  FileID createFileID(FileEntryRef SourceFile, SourceLocation IncludePos,
876
                      SrcMgr::CharacteristicKind FileCharacter,
877
                      int LoadedID = 0,
878
                      SourceLocation::UIntTy LoadedOffset = 0);
879
880
  /// Create a new FileID that represents the specified memory buffer.
881
  ///
882
  /// This does no caching of the buffer and takes ownership of the
883
  /// MemoryBuffer, so only pass a MemoryBuffer to this once.
884
  FileID createFileID(std::unique_ptr<llvm::MemoryBuffer> Buffer,
885
                      SrcMgr::CharacteristicKind FileCharacter = SrcMgr::C_User,
886
                      int LoadedID = 0, SourceLocation::UIntTy LoadedOffset = 0,
887
                      SourceLocation IncludeLoc = SourceLocation());
888
889
  /// Create a new FileID that represents the specified memory buffer.
890
  ///
891
  /// This does not take ownership of the MemoryBuffer. The memory buffer must
892
  /// outlive the SourceManager.
893
  FileID createFileID(const llvm::MemoryBufferRef &Buffer,
894
                      SrcMgr::CharacteristicKind FileCharacter = SrcMgr::C_User,
895
                      int LoadedID = 0, SourceLocation::UIntTy LoadedOffset = 0,
896
                      SourceLocation IncludeLoc = SourceLocation());
897
898
  /// Get the FileID for \p SourceFile if it exists. Otherwise, create a
899
  /// new FileID for the \p SourceFile.
900
  FileID getOrCreateFileID(const FileEntry *SourceFile,
901
                           SrcMgr::CharacteristicKind FileCharacter);
902
903
  /// Creates an expansion SLocEntry for the substitution of an argument into a
904
  /// function-like macro's body. Returns the start of the expansion.
905
  ///
906
  /// The macro argument was written at \p SpellingLoc with length \p Length.
907
  /// \p ExpansionLoc is the parameter name in the (expanded) macro body.
908
  SourceLocation createMacroArgExpansionLoc(SourceLocation SpellingLoc,
909
                                            SourceLocation ExpansionLoc,
910
                                            unsigned Length);
911
912
  /// Creates an expansion SLocEntry for a macro use. Returns its start.
913
  ///
914
  /// The macro body begins at \p SpellingLoc with length \p Length.
915
  /// The macro use spans [ExpansionLocStart, ExpansionLocEnd].
916
  SourceLocation createExpansionLoc(SourceLocation SpellingLoc,
917
                                    SourceLocation ExpansionLocStart,
918
                                    SourceLocation ExpansionLocEnd,
919
                                    unsigned Length,
920
                                    bool ExpansionIsTokenRange = true,
921
                                    int LoadedID = 0,
922
                                    SourceLocation::UIntTy LoadedOffset = 0);
923
924
  /// Return a new SourceLocation that encodes that the token starting
925
  /// at \p TokenStart ends prematurely at \p TokenEnd.
926
  SourceLocation createTokenSplitLoc(SourceLocation SpellingLoc,
927
                                     SourceLocation TokenStart,
928
                                     SourceLocation TokenEnd);
929
930
  /// Retrieve the memory buffer associated with the given file.
931
  ///
932
  /// Returns None if the buffer is not valid.
933
  llvm::Optional<llvm::MemoryBufferRef>
934
  getMemoryBufferForFileOrNone(const FileEntry *File);
935
936
  /// Retrieve the memory buffer associated with the given file.
937
  ///
938
  /// Returns a fake buffer if there isn't a real one.
939
10
  llvm::MemoryBufferRef getMemoryBufferForFileOrFake(const FileEntry *File) {
940
10
    if (auto B = getMemoryBufferForFileOrNone(File))
941
10
      return *B;
942
0
    return getFakeBufferForRecovery();
943
10
  }
944
945
  /// Override the contents of the given source file by providing an
946
  /// already-allocated buffer.
947
  ///
948
  /// \param SourceFile the source file whose contents will be overridden.
949
  ///
950
  /// \param Buffer the memory buffer whose contents will be used as the
951
  /// data in the given source file.
952
  void overrideFileContents(const FileEntry *SourceFile,
953
565
                            const llvm::MemoryBufferRef &Buffer) {
954
565
    overrideFileContents(SourceFile, llvm::MemoryBuffer::getMemBuffer(Buffer));
955
565
  }
956
957
  /// Override the contents of the given source file by providing an
958
  /// already-allocated buffer.
959
  ///
960
  /// \param SourceFile the source file whose contents will be overridden.
961
  ///
962
  /// \param Buffer the memory buffer whose contents will be used as the
963
  /// data in the given source file.
964
  void overrideFileContents(const FileEntry *SourceFile,
965
                            std::unique_ptr<llvm::MemoryBuffer> Buffer);
966
  void overrideFileContents(FileEntryRef SourceFile,
967
87
                            std::unique_ptr<llvm::MemoryBuffer> Buffer) {
968
87
    overrideFileContents(&SourceFile.getFileEntry(), std::move(Buffer));
969
87
  }
970
971
  /// Override the given source file with another one.
972
  ///
973
  /// \param SourceFile the source file which will be overridden.
974
  ///
975
  /// \param NewFile the file whose contents will be used as the
976
  /// data instead of the contents of the given source file.
977
  void overrideFileContents(const FileEntry *SourceFile,
978
                            const FileEntry *NewFile);
979
980
  /// Returns true if the file contents have been overridden.
981
1.04M
  bool isFileOverridden(const FileEntry *File) const {
982
1.04M
    if (OverriddenFilesInfo) {
983
173k
      if (OverriddenFilesInfo->OverriddenFilesWithBuffer.count(File))
984
0
        return true;
985
173k
      if (OverriddenFilesInfo->OverriddenFiles.find(File) !=
986
173k
          OverriddenFilesInfo->OverriddenFiles.end())
987
2
        return true;
988
173k
    }
989
1.04M
    return false;
990
1.04M
  }
991
992
  /// Bypass the overridden contents of a file.  This creates a new FileEntry
993
  /// and initializes the content cache for it.  Returns None if there is no
994
  /// such file in the filesystem.
995
  ///
996
  /// This should be called before parsing has begun.
997
  Optional<FileEntryRef> bypassFileContentsOverride(FileEntryRef File);
998
999
  /// Specify that a file is transient.
1000
  void setFileIsTransient(const FileEntry *SourceFile);
1001
1002
  /// Specify that all files that are read during this compilation are
1003
  /// transient.
1004
10
  void setAllFilesAreTransient(bool Transient) {
1005
10
    FilesAreTransient = Transient;
1006
10
  }
1007
1008
  //===--------------------------------------------------------------------===//
1009
  // FileID manipulation methods.
1010
  //===--------------------------------------------------------------------===//
1011
1012
  /// Return the buffer for the specified FileID.
1013
  ///
1014
  /// If there is an error opening this buffer the first time, return None.
1015
  llvm::Optional<llvm::MemoryBufferRef>
1016
115M
  getBufferOrNone(FileID FID, SourceLocation Loc = SourceLocation()) const {
1017
115M
    if (auto *Entry = getSLocEntryForFile(FID))
1018
115M
      return Entry->getFile().getContentCache().getBufferOrNone(
1019
115M
          Diag, getFileManager(), Loc);
1020
985
    return None;
1021
115M
  }
1022
1023
  /// Return the buffer for the specified FileID.
1024
  ///
1025
  /// If there is an error opening this buffer the first time, this
1026
  /// manufactures a temporary buffer and returns it.
1027
  llvm::MemoryBufferRef
1028
918k
  getBufferOrFake(FileID FID, SourceLocation Loc = SourceLocation()) const {
1029
918k
    if (auto B = getBufferOrNone(FID, Loc))
1030
918k
      return *B;
1031
3
    return getFakeBufferForRecovery();
1032
918k
  }
1033
1034
  /// Returns the FileEntry record for the provided FileID.
1035
4.99M
  const FileEntry *getFileEntryForID(FileID FID) const {
1036
4.99M
    if (auto *Entry = getSLocEntryForFile(FID))
1037
4.99M
      return Entry->getFile().getContentCache().OrigEntry;
1038
214
    return nullptr;
1039
4.99M
  }
1040
1041
  /// Returns the FileEntryRef for the provided FileID.
1042
570k
  Optional<FileEntryRef> getFileEntryRefForID(FileID FID) const {
1043
570k
    if (auto *Entry = getFileEntryForID(FID))
1044
569k
      return Entry->getLastRef();
1045
1.48k
    return None;
1046
570k
  }
1047
1048
  /// Returns the filename for the provided FileID, unless it's a built-in
1049
  /// buffer that's not represented by a filename.
1050
  ///
1051
  /// Returns None for non-files and built-in files.
1052
  Optional<StringRef> getNonBuiltinFilenameForID(FileID FID) const;
1053
1054
  /// Returns the FileEntry record for the provided SLocEntry.
1055
  const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const
1056
63.5k
  {
1057
63.5k
    return sloc.getFile().getContentCache().OrigEntry;
1058
63.5k
  }
1059
1060
  /// Return a StringRef to the source buffer data for the
1061
  /// specified FileID.
1062
  ///
1063
  /// \param FID The file ID whose contents will be returned.
1064
  /// \param Invalid If non-NULL, will be set true if an error occurred.
1065
  StringRef getBufferData(FileID FID, bool *Invalid = nullptr) const;
1066
1067
  /// Return a StringRef to the source buffer data for the
1068
  /// specified FileID, returning None if invalid.
1069
  ///
1070
  /// \param FID The file ID whose contents will be returned.
1071
  llvm::Optional<StringRef> getBufferDataOrNone(FileID FID) const;
1072
1073
  /// Return a StringRef to the source buffer data for the
1074
  /// specified FileID, returning None if it's not yet loaded.
1075
  ///
1076
  /// \param FID The file ID whose contents will be returned.
1077
  llvm::Optional<StringRef> getBufferDataIfLoaded(FileID FID) const;
1078
1079
  /// Get the number of FileIDs (files and macros) that were created
1080
  /// during preprocessing of \p FID, including it.
1081
  unsigned getNumCreatedFIDsForFileID(FileID FID) const {
1082
    if (auto *Entry = getSLocEntryForFile(FID))
1083
      return Entry->getFile().NumCreatedFIDs;
1084
    return 0;
1085
  }
1086
1087
  /// Set the number of FileIDs (files and macros) that were created
1088
  /// during preprocessing of \p FID, including it.
1089
  void setNumCreatedFIDsForFileID(FileID FID, unsigned NumFIDs,
1090
668k
                                  bool Force = false) const {
1091
668k
    auto *Entry = getSLocEntryForFile(FID);
1092
668k
    if (!Entry)
1093
0
      return;
1094
668k
    assert((Force || Entry->getFile().NumCreatedFIDs == 0) && "Already set!");
1095
0
    const_cast<SrcMgr::FileInfo &>(Entry->getFile()).NumCreatedFIDs = NumFIDs;
1096
668k
  }
1097
1098
  //===--------------------------------------------------------------------===//
1099
  // SourceLocation manipulation methods.
1100
  //===--------------------------------------------------------------------===//
1101
1102
  /// Return the FileID for a SourceLocation.
1103
  ///
1104
  /// This is a very hot method that is used for all SourceManager queries
1105
  /// that start with a SourceLocation object.  It is responsible for finding
1106
  /// the entry in SLocEntryTable which contains the specified location.
1107
  ///
1108
1.15G
  FileID getFileID(SourceLocation SpellingLoc) const {
1109
1.15G
    SourceLocation::UIntTy SLocOffset = SpellingLoc.getOffset();
1110
1111
    // If our one-entry cache covers this offset, just return it.
1112
1.15G
    if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
1113
813M
      return LastFileIDLookup;
1114
1115
340M
    return getFileIDSlow(SLocOffset);
1116
1.15G
  }
1117
1118
  /// Return the filename of the file containing a SourceLocation.
1119
  StringRef getFilename(SourceLocation SpellingLoc) const;
1120
1121
  /// Return the source location corresponding to the first byte of
1122
  /// the specified file.
1123
50.5M
  SourceLocation getLocForStartOfFile(FileID FID) const {
1124
50.5M
    if (auto *Entry = getSLocEntryForFile(FID))
1125
50.5M
      return SourceLocation::getFileLoc(Entry->getOffset());
1126
588
    return SourceLocation();
1127
50.5M
  }
1128
1129
  /// Return the source location corresponding to the last byte of the
1130
  /// specified file.
1131
99.2k
  SourceLocation getLocForEndOfFile(FileID FID) const {
1132
99.2k
    if (auto *Entry = getSLocEntryForFile(FID))
1133
99.2k
      return SourceLocation::getFileLoc(Entry->getOffset() +
1134
99.2k
                                        getFileIDSize(FID));
1135
0
    return SourceLocation();
1136
99.2k
  }
1137
1138
  /// Returns the include location if \p FID is a \#include'd file
1139
  /// otherwise it returns an invalid location.
1140
1.51M
  SourceLocation getIncludeLoc(FileID FID) const {
1141
1.51M
    if (auto *Entry = getSLocEntryForFile(FID))
1142
1.51M
      return Entry->getFile().getIncludeLoc();
1143
1
    return SourceLocation();
1144
1.51M
  }
1145
1146
  // Returns the import location if the given source location is
1147
  // located within a module, or an invalid location if the source location
1148
  // is within the current translation unit.
1149
  std::pair<SourceLocation, StringRef>
1150
1.16k
  getModuleImportLoc(SourceLocation Loc) const {
1151
1.16k
    FileID FID = getFileID(Loc);
1152
1153
    // Positive file IDs are in the current translation unit, and -1 is a
1154
    // placeholder.
1155
1.16k
    if (FID.ID >= -1)
1156
1.12k
      return std::make_pair(SourceLocation(), "");
1157
1158
42
    return ExternalSLocEntries->getModuleImportLoc(FID.ID);
1159
1.16k
  }
1160
1161
  /// Given a SourceLocation object \p Loc, return the expansion
1162
  /// location referenced by the ID.
1163
134M
  SourceLocation getExpansionLoc(SourceLocation Loc) const {
1164
    // Handle the non-mapped case inline, defer to out of line code to handle
1165
    // expansions.
1166
134M
    if (Loc.isFileID()) 
return Loc125M
;
1167
8.54M
    return getExpansionLocSlowCase(Loc);
1168
134M
  }
1169
1170
  /// Given \p Loc, if it is a macro location return the expansion
1171
  /// location or the spelling location, depending on if it comes from a
1172
  /// macro argument or not.
1173
1.13M
  SourceLocation getFileLoc(SourceLocation Loc) const {
1174
1.13M
    if (Loc.isFileID()) 
return Loc1.10M
;
1175
29.8k
    return getFileLocSlowCase(Loc);
1176
1.13M
  }
1177
1178
  /// Return the start/end of the expansion information for an
1179
  /// expansion location.
1180
  ///
1181
  /// \pre \p Loc is required to be an expansion location.
1182
  CharSourceRange getImmediateExpansionRange(SourceLocation Loc) const;
1183
1184
  /// Given a SourceLocation object, return the range of
1185
  /// tokens covered by the expansion in the ultimate file.
1186
  CharSourceRange getExpansionRange(SourceLocation Loc) const;
1187
1188
  /// Given a SourceRange object, return the range of
1189
  /// tokens or characters covered by the expansion in the ultimate file.
1190
2.40k
  CharSourceRange getExpansionRange(SourceRange Range) const {
1191
2.40k
    SourceLocation Begin = getExpansionRange(Range.getBegin()).getBegin();
1192
2.40k
    CharSourceRange End = getExpansionRange(Range.getEnd());
1193
2.40k
    return CharSourceRange(SourceRange(Begin, End.getEnd()),
1194
2.40k
                           End.isTokenRange());
1195
2.40k
  }
1196
1197
  /// Given a CharSourceRange object, return the range of
1198
  /// tokens or characters covered by the expansion in the ultimate file.
1199
457
  CharSourceRange getExpansionRange(CharSourceRange Range) const {
1200
457
    CharSourceRange Expansion = getExpansionRange(Range.getAsRange());
1201
457
    if (Expansion.getEnd() == Range.getEnd())
1202
454
      Expansion.setTokenRange(Range.isTokenRange());
1203
457
    return Expansion;
1204
457
  }
1205
1206
  /// Given a SourceLocation object, return the spelling
1207
  /// location referenced by the ID.
1208
  ///
1209
  /// This is the place where the characters that make up the lexed token
1210
  /// can be found.
1211
92.2M
  SourceLocation getSpellingLoc(SourceLocation Loc) const {
1212
    // Handle the non-mapped case inline, defer to out of line code to handle
1213
    // expansions.
1214
92.2M
    if (Loc.isFileID()) 
return Loc19.4M
;
1215
72.8M
    return getSpellingLocSlowCase(Loc);
1216
92.2M
  }
1217
1218
  /// Given a SourceLocation object, return the spelling location
1219
  /// referenced by the ID.
1220
  ///
1221
  /// This is the first level down towards the place where the characters
1222
  /// that make up the lexed token can be found.  This should not generally
1223
  /// be used by clients.
1224
  SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const;
1225
1226
  /// Form a SourceLocation from a FileID and Offset pair.
1227
31.7M
  SourceLocation getComposedLoc(FileID FID, unsigned Offset) const {
1228
31.7M
    auto *Entry = getSLocEntryOrNull(FID);
1229
31.7M
    if (!Entry)
1230
0
      return SourceLocation();
1231
1232
31.7M
    SourceLocation::UIntTy GlobalOffset = Entry->getOffset() + Offset;
1233
31.7M
    return Entry->isFile() ? 
SourceLocation::getFileLoc(GlobalOffset)26.5M
1234
31.7M
                           : 
SourceLocation::getMacroLoc(GlobalOffset)5.19M
;
1235
31.7M
  }
1236
1237
  /// Decompose the specified location into a raw FileID + Offset pair.
1238
  ///
1239
  /// The first element is the FileID, the second is the offset from the
1240
  /// start of the buffer of the location.
1241
428M
  std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const {
1242
428M
    FileID FID = getFileID(Loc);
1243
428M
    auto *Entry = getSLocEntryOrNull(FID);
1244
428M
    if (!Entry)
1245
1.20M
      return std::make_pair(FileID(), 0);
1246
427M
    return std::make_pair(FID, Loc.getOffset() - Entry->getOffset());
1247
428M
  }
1248
1249
  /// Decompose the specified location into a raw FileID + Offset pair.
1250
  ///
1251
  /// If the location is an expansion record, walk through it until we find
1252
  /// the final location expanded.
1253
  std::pair<FileID, unsigned>
1254
385M
  getDecomposedExpansionLoc(SourceLocation Loc) const {
1255
385M
    FileID FID = getFileID(Loc);
1256
385M
    auto *E = getSLocEntryOrNull(FID);
1257
385M
    if (!E)
1258
21
      return std::make_pair(FileID(), 0);
1259
1260
385M
    unsigned Offset = Loc.getOffset()-E->getOffset();
1261
385M
    if (Loc.isFileID())
1262
379M
      return std::make_pair(FID, Offset);
1263
1264
5.43M
    return getDecomposedExpansionLocSlowCase(E);
1265
385M
  }
1266
1267
  /// Decompose the specified location into a raw FileID + Offset pair.
1268
  ///
1269
  /// If the location is an expansion record, walk through it until we find
1270
  /// its spelling record.
1271
  std::pair<FileID, unsigned>
1272
53.5M
  getDecomposedSpellingLoc(SourceLocation Loc) const {
1273
53.5M
    FileID FID = getFileID(Loc);
1274
53.5M
    auto *E = getSLocEntryOrNull(FID);
1275
53.5M
    if (!E)
1276
64
      return std::make_pair(FileID(), 0);
1277
1278
53.5M
    unsigned Offset = Loc.getOffset()-E->getOffset();
1279
53.5M
    if (Loc.isFileID())
1280
52.7M
      return std::make_pair(FID, Offset);
1281
797k
    return getDecomposedSpellingLocSlowCase(E, Offset);
1282
53.5M
  }
1283
1284
  /// Returns the "included/expanded in" decomposed location of the given
1285
  /// FileID.
1286
  std::pair<FileID, unsigned> getDecomposedIncludedLoc(FileID FID) const;
1287
1288
  /// Returns the offset from the start of the file that the
1289
  /// specified SourceLocation represents.
1290
  ///
1291
  /// This is not very meaningful for a macro ID.
1292
1.62M
  unsigned getFileOffset(SourceLocation SpellingLoc) const {
1293
1.62M
    return getDecomposedLoc(SpellingLoc).second;
1294
1.62M
  }
1295
1296
  /// Tests whether the given source location represents a macro
1297
  /// argument's expansion into the function-like macro definition.
1298
  ///
1299
  /// \param StartLoc If non-null and function returns true, it is set to the
1300
  /// start location of the macro argument expansion.
1301
  ///
1302
  /// Such source locations only appear inside of the expansion
1303
  /// locations representing where a particular function-like macro was
1304
  /// expanded.
1305
  bool isMacroArgExpansion(SourceLocation Loc,
1306
                           SourceLocation *StartLoc = nullptr) const;
1307
1308
  /// Tests whether the given source location represents the expansion of
1309
  /// a macro body.
1310
  ///
1311
  /// This is equivalent to testing whether the location is part of a macro
1312
  /// expansion but not the expansion of an argument to a function-like macro.
1313
  bool isMacroBodyExpansion(SourceLocation Loc) const;
1314
1315
  /// Returns true if the given MacroID location points at the beginning
1316
  /// of the immediate macro expansion.
1317
  ///
1318
  /// \param MacroBegin If non-null and function returns true, it is set to the
1319
  /// begin location of the immediate macro expansion.
1320
  bool isAtStartOfImmediateMacroExpansion(SourceLocation Loc,
1321
                                    SourceLocation *MacroBegin = nullptr) const;
1322
1323
  /// Returns true if the given MacroID location points at the character
1324
  /// end of the immediate macro expansion.
1325
  ///
1326
  /// \param MacroEnd If non-null and function returns true, it is set to the
1327
  /// character end location of the immediate macro expansion.
1328
  bool
1329
  isAtEndOfImmediateMacroExpansion(SourceLocation Loc,
1330
                                   SourceLocation *MacroEnd = nullptr) const;
1331
1332
  /// Returns true if \p Loc is inside the [\p Start, +\p Length)
1333
  /// chunk of the source location address space.
1334
  ///
1335
  /// If it's true and \p RelativeOffset is non-null, it will be set to the
1336
  /// relative offset of \p Loc inside the chunk.
1337
  bool
1338
  isInSLocAddrSpace(SourceLocation Loc, SourceLocation Start, unsigned Length,
1339
885M
                    SourceLocation::UIntTy *RelativeOffset = nullptr) const {
1340
885M
    assert(((Start.getOffset() < NextLocalOffset &&
1341
885M
               Start.getOffset()+Length <= NextLocalOffset) ||
1342
885M
            (Start.getOffset() >= CurrentLoadedOffset &&
1343
885M
                Start.getOffset()+Length < MaxLoadedOffset)) &&
1344
885M
           "Chunk is not valid SLoc address space");
1345
0
    SourceLocation::UIntTy LocOffs = Loc.getOffset();
1346
885M
    SourceLocation::UIntTy BeginOffs = Start.getOffset();
1347
885M
    SourceLocation::UIntTy EndOffs = BeginOffs + Length;
1348
885M
    if (LocOffs >= BeginOffs && LocOffs < EndOffs) {
1349
885M
      if (RelativeOffset)
1350
442M
        *RelativeOffset = LocOffs - BeginOffs;
1351
885M
      return true;
1352
885M
    }
1353
1354
0
    return false;
1355
885M
  }
1356
1357
  /// Return true if both \p LHS and \p RHS are in the local source
1358
  /// location address space or the loaded one.
1359
  ///
1360
  /// If it's true and \p RelativeOffset is non-null, it will be set to the
1361
  /// offset of \p RHS relative to \p LHS.
1362
  bool isInSameSLocAddrSpace(SourceLocation LHS, SourceLocation RHS,
1363
168M
                             SourceLocation::IntTy *RelativeOffset) const {
1364
168M
    SourceLocation::UIntTy LHSOffs = LHS.getOffset(), RHSOffs = RHS.getOffset();
1365
168M
    bool LHSLoaded = LHSOffs >= CurrentLoadedOffset;
1366
168M
    bool RHSLoaded = RHSOffs >= CurrentLoadedOffset;
1367
1368
168M
    if (LHSLoaded == RHSLoaded) {
1369
168M
      if (RelativeOffset)
1370
168M
        *RelativeOffset = RHSOffs - LHSOffs;
1371
168M
      return true;
1372
168M
    }
1373
1374
0
    return false;
1375
168M
  }
1376
1377
  //===--------------------------------------------------------------------===//
1378
  // Queries about the code at a SourceLocation.
1379
  //===--------------------------------------------------------------------===//
1380
1381
  /// Return a pointer to the start of the specified location
1382
  /// in the appropriate spelling MemoryBuffer.
1383
  ///
1384
  /// \param Invalid If non-NULL, will be set \c true if an error occurs.
1385
  const char *getCharacterData(SourceLocation SL,
1386
                               bool *Invalid = nullptr) const;
1387
1388
  /// Return the column # for the specified file position.
1389
  ///
1390
  /// This is significantly cheaper to compute than the line number.  This
1391
  /// returns zero if the column number isn't known.  This may only be called
1392
  /// on a file sloc, so you must choose a spelling or expansion location
1393
  /// before calling this method.
1394
  unsigned getColumnNumber(FileID FID, unsigned FilePos,
1395
                           bool *Invalid = nullptr) const;
1396
  unsigned getSpellingColumnNumber(SourceLocation Loc,
1397
                                   bool *Invalid = nullptr) const;
1398
  unsigned getExpansionColumnNumber(SourceLocation Loc,
1399
                                    bool *Invalid = nullptr) const;
1400
  unsigned getPresumedColumnNumber(SourceLocation Loc,
1401
                                   bool *Invalid = nullptr) const;
1402
1403
  /// Given a SourceLocation, return the spelling line number
1404
  /// for the position indicated.
1405
  ///
1406
  /// This requires building and caching a table of line offsets for the
1407
  /// MemoryBuffer, so this is not cheap: use only when about to emit a
1408
  /// diagnostic.
1409
  unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = nullptr) const;
1410
  unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = nullptr) const;
1411
  unsigned getExpansionLineNumber(SourceLocation Loc, bool *Invalid = nullptr) const;
1412
  unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = nullptr) const;
1413
1414
  /// Return the filename or buffer identifier of the buffer the
1415
  /// location is in.
1416
  ///
1417
  /// Note that this name does not respect \#line directives.  Use
1418
  /// getPresumedLoc for normal clients.
1419
  StringRef getBufferName(SourceLocation Loc, bool *Invalid = nullptr) const;
1420
1421
  /// Return the file characteristic of the specified source
1422
  /// location, indicating whether this is a normal file, a system
1423
  /// header, or an "implicit extern C" system header.
1424
  ///
1425
  /// This state can be modified with flags on GNU linemarker directives like:
1426
  /// \code
1427
  ///   # 4 "foo.h" 3
1428
  /// \endcode
1429
  /// which changes all source locations in the current file after that to be
1430
  /// considered to be from a system header.
1431
  SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const;
1432
1433
  /// Returns the "presumed" location of a SourceLocation specifies.
1434
  ///
1435
  /// A "presumed location" can be modified by \#line or GNU line marker
1436
  /// directives.  This provides a view on the data that a user should see
1437
  /// in diagnostics, for example.
1438
  ///
1439
  /// Note that a presumed location is always given as the expansion point of
1440
  /// an expansion location, not at the spelling location.
1441
  ///
1442
  /// \returns The presumed location of the specified SourceLocation. If the
1443
  /// presumed location cannot be calculated (e.g., because \p Loc is invalid
1444
  /// or the file containing \p Loc has changed on disk), returns an invalid
1445
  /// presumed location.
1446
  PresumedLoc getPresumedLoc(SourceLocation Loc,
1447
                             bool UseLineDirectives = true) const;
1448
1449
  /// Returns whether the PresumedLoc for a given SourceLocation is
1450
  /// in the main file.
1451
  ///
1452
  /// This computes the "presumed" location for a SourceLocation, then checks
1453
  /// whether it came from a file other than the main file. This is different
1454
  /// from isWrittenInMainFile() because it takes line marker directives into
1455
  /// account.
1456
  bool isInMainFile(SourceLocation Loc) const;
1457
1458
  /// Returns true if the spelling locations for both SourceLocations
1459
  /// are part of the same file buffer.
1460
  ///
1461
  /// This check ignores line marker directives.
1462
37.1M
  bool isWrittenInSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
1463
37.1M
    return getFileID(Loc1) == getFileID(Loc2);
1464
37.1M
  }
1465
1466
  /// Returns true if the spelling location for the given location
1467
  /// is in the main file buffer.
1468
  ///
1469
  /// This check ignores line marker directives.
1470
120k
  bool isWrittenInMainFile(SourceLocation Loc) const {
1471
120k
    return getFileID(Loc) == getMainFileID();
1472
120k
  }
1473
1474
  /// Returns whether \p Loc is located in a <built-in> file.
1475
79.0M
  bool isWrittenInBuiltinFile(SourceLocation Loc) const {
1476
79.0M
    StringRef Filename(getPresumedLoc(Loc).getFilename());
1477
79.0M
    return Filename.equals("<built-in>");
1478
79.0M
  }
1479
1480
  /// Returns whether \p Loc is located in a <command line> file.
1481
86.2k
  bool isWrittenInCommandLineFile(SourceLocation Loc) const {
1482
86.2k
    StringRef Filename(getPresumedLoc(Loc).getFilename());
1483
86.2k
    return Filename.equals("<command line>");
1484
86.2k
  }
1485
1486
  /// Returns whether \p Loc is located in a <scratch space> file.
1487
16.6k
  bool isWrittenInScratchSpace(SourceLocation Loc) const {
1488
16.6k
    StringRef Filename(getPresumedLoc(Loc).getFilename());
1489
16.6k
    return Filename.equals("<scratch space>");
1490
16.6k
  }
1491
1492
  /// Returns if a SourceLocation is in a system header.
1493
202M
  bool isInSystemHeader(SourceLocation Loc) const {
1494
202M
    return isSystem(getFileCharacteristic(Loc));
1495
202M
  }
1496
1497
  /// Returns if a SourceLocation is in an "extern C" system header.
1498
15
  bool isInExternCSystemHeader(SourceLocation Loc) const {
1499
15
    return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem;
1500
15
  }
1501
1502
  /// Returns whether \p Loc is expanded from a macro in a system header.
1503
2.32M
  bool isInSystemMacro(SourceLocation loc) const {
1504
2.32M
    if (!loc.isMacroID())
1505
2.30M
      return false;
1506
1507
    // This happens when the macro is the result of a paste, in that case
1508
    // its spelling is the scratch memory, so we take the parent context.
1509
    // There can be several level of token pasting.
1510
16.2k
    if (isWrittenInScratchSpace(getSpellingLoc(loc))) {
1511
397
      do {
1512
397
        loc = getImmediateMacroCallerLoc(loc);
1513
397
      } while (isWrittenInScratchSpace(getSpellingLoc(loc)));
1514
396
      return isInSystemMacro(loc);
1515
396
    }
1516
1517
15.8k
    return isInSystemHeader(getSpellingLoc(loc));
1518
16.2k
  }
1519
1520
  /// The size of the SLocEntry that \p FID represents.
1521
  unsigned getFileIDSize(FileID FID) const;
1522
1523
  /// Given a specific FileID, returns true if \p Loc is inside that
1524
  /// FileID chunk and sets relative offset (offset of \p Loc from beginning
1525
  /// of FileID) to \p relativeOffset.
1526
  bool isInFileID(SourceLocation Loc, FileID FID,
1527
50.1M
                  unsigned *RelativeOffset = nullptr) const {
1528
50.1M
    SourceLocation::UIntTy Offs = Loc.getOffset();
1529
50.1M
    if (isOffsetInFileID(FID, Offs)) {
1530
12.3M
      if (RelativeOffset)
1531
8.04M
        *RelativeOffset = Offs - getSLocEntry(FID).getOffset();
1532
12.3M
      return true;
1533
12.3M
    }
1534
1535
37.8M
    return false;
1536
50.1M
  }
1537
1538
  //===--------------------------------------------------------------------===//
1539
  // Line Table Manipulation Routines
1540
  //===--------------------------------------------------------------------===//
1541
1542
  /// Return the uniqued ID for the specified filename.
1543
  unsigned getLineTableFilenameID(StringRef Str);
1544
1545
  /// Add a line note to the line table for the FileID and offset
1546
  /// specified by Loc.
1547
  ///
1548
  /// If FilenameID is -1, it is considered to be unspecified.
1549
  void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID,
1550
                   bool IsFileEntry, bool IsFileExit,
1551
                   SrcMgr::CharacteristicKind FileKind);
1552
1553
  /// Determine if the source manager has a line table.
1554
5.56k
  bool hasLineTable() const { return LineTable != nullptr; }
1555
1556
  /// Retrieve the stored line table.
1557
  LineTableInfo &getLineTable();
1558
1559
  //===--------------------------------------------------------------------===//
1560
  // Queries for performance analysis.
1561
  //===--------------------------------------------------------------------===//
1562
1563
  /// Return the total amount of physical memory allocated by the
1564
  /// ContentCache allocator.
1565
1
  size_t getContentCacheSize() const {
1566
1
    return ContentCacheAlloc.getTotalMemory();
1567
1
  }
1568
1569
  struct MemoryBufferSizes {
1570
    const size_t malloc_bytes;
1571
    const size_t mmap_bytes;
1572
1573
    MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)
1574
1
      : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {}
1575
  };
1576
1577
  /// Return the amount of memory used by memory buffers, breaking down
1578
  /// by heap-backed versus mmap'ed memory.
1579
  MemoryBufferSizes getMemoryBufferSizes() const;
1580
1581
  /// Return the amount of memory used for various side tables and
1582
  /// data structures in the SourceManager.
1583
  size_t getDataStructureSizes() const;
1584
1585
  //===--------------------------------------------------------------------===//
1586
  // Other miscellaneous methods.
1587
  //===--------------------------------------------------------------------===//
1588
1589
  /// Get the source location for the given file:line:col triplet.
1590
  ///
1591
  /// If the source file is included multiple times, the source location will
1592
  /// be based upon the first inclusion.
1593
  SourceLocation translateFileLineCol(const FileEntry *SourceFile,
1594
                                      unsigned Line, unsigned Col) const;
1595
1596
  /// Get the FileID for the given file.
1597
  ///
1598
  /// If the source file is included multiple times, the FileID will be the
1599
  /// first inclusion.
1600
  FileID translateFile(const FileEntry *SourceFile) const;
1601
1.30k
  FileID translateFile(FileEntryRef SourceFile) const {
1602
1.30k
    return translateFile(&SourceFile.getFileEntry());
1603
1.30k
  }
1604
1605
  /// Get the source location in \p FID for the given line:col.
1606
  /// Returns null location if \p FID is not a file SLocEntry.
1607
  SourceLocation translateLineCol(FileID FID,
1608
                                  unsigned Line, unsigned Col) const;
1609
1610
  /// If \p Loc points inside a function macro argument, the returned
1611
  /// location will be the macro location in which the argument was expanded.
1612
  /// If a macro argument is used multiple times, the expanded location will
1613
  /// be at the first expansion of the argument.
1614
  /// e.g.
1615
  ///   MY_MACRO(foo);
1616
  ///             ^
1617
  /// Passing a file location pointing at 'foo', will yield a macro location
1618
  /// where 'foo' was expanded into.
1619
  SourceLocation getMacroArgExpandedLocation(SourceLocation Loc) const;
1620
1621
  /// Determines the order of 2 source locations in the translation unit.
1622
  ///
1623
  /// \returns true if LHS source location comes before RHS, false otherwise.
1624
  bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const;
1625
1626
  /// Determines whether the two decomposed source location is in the
1627
  ///        same translation unit. As a byproduct, it also calculates the order
1628
  ///        of the source locations in case they are in the same TU.
1629
  ///
1630
  /// \returns Pair of bools the first component is true if the two locations
1631
  ///          are in the same TU. The second bool is true if the first is true
1632
  ///          and \p LOffs is before \p ROffs.
1633
  std::pair<bool, bool>
1634
  isInTheSameTranslationUnit(std::pair<FileID, unsigned> &LOffs,
1635
                             std::pair<FileID, unsigned> &ROffs) const;
1636
1637
  /// Determines the order of 2 source locations in the "source location
1638
  /// address space".
1639
0
  bool isBeforeInSLocAddrSpace(SourceLocation LHS, SourceLocation RHS) const {
1640
0
    return isBeforeInSLocAddrSpace(LHS, RHS.getOffset());
1641
0
  }
1642
1643
  /// Determines the order of a source location and a source location
1644
  /// offset in the "source location address space".
1645
  ///
1646
  /// Note that we always consider source locations loaded from
1647
  bool isBeforeInSLocAddrSpace(SourceLocation LHS,
1648
506M
                               SourceLocation::UIntTy RHS) const {
1649
506M
    SourceLocation::UIntTy LHSOffset = LHS.getOffset();
1650
506M
    bool LHSLoaded = LHSOffset >= CurrentLoadedOffset;
1651
506M
    bool RHSLoaded = RHS >= CurrentLoadedOffset;
1652
506M
    if (LHSLoaded == RHSLoaded)
1653
500M
      return LHSOffset < RHS;
1654
1655
6.20M
    return LHSLoaded;
1656
506M
  }
1657
1658
  /// Return true if the Point is within Start and End.
1659
  bool isPointWithin(SourceLocation Location, SourceLocation Start,
1660
9.54k
                     SourceLocation End) const {
1661
9.54k
    return Location == Start || 
Location == End9.38k
||
1662
9.54k
           
(9.18k
isBeforeInTranslationUnit(Start, Location)9.18k
&&
1663
9.18k
            
isBeforeInTranslationUnit(Location, End)6.08k
);
1664
9.54k
  }
1665
1666
  // Iterators over FileInfos.
1667
  using fileinfo_iterator =
1668
      llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>::const_iterator;
1669
1670
11.5k
  fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); }
1671
11.5k
  fileinfo_iterator fileinfo_end() const { return FileInfos.end(); }
1672
680
  bool hasFileInfo(const FileEntry *File) const {
1673
680
    return FileInfos.find(File) != FileInfos.end();
1674
680
  }
1675
1676
  /// Print statistics to stderr.
1677
  void PrintStats() const;
1678
1679
  void dump() const;
1680
1681
  /// Get the number of local SLocEntries we have.
1682
2.09M
  unsigned local_sloc_entry_size() const { return LocalSLocEntryTable.size(); }
1683
1684
  /// Get a local SLocEntry. This is exposed for indexing.
1685
6.10G
  const SrcMgr::SLocEntry &getLocalSLocEntry(unsigned Index) const {
1686
6.10G
    assert(Index < LocalSLocEntryTable.size() && "Invalid index");
1687
0
    return LocalSLocEntryTable[Index];
1688
6.10G
  }
1689
1690
  /// Get the number of loaded SLocEntries we have.
1691
1.27k
  unsigned loaded_sloc_entry_size() const { return LoadedSLocEntryTable.size();}
1692
1693
  /// Get a loaded SLocEntry. This is exposed for indexing.
1694
  const SrcMgr::SLocEntry &getLoadedSLocEntry(unsigned Index,
1695
1.24G
                                              bool *Invalid = nullptr) const {
1696
1.24G
    assert(Index < LoadedSLocEntryTable.size() && "Invalid index");
1697
1.24G
    if (SLocEntryLoaded[Index])
1698
1.23G
      return LoadedSLocEntryTable[Index];
1699
12.5M
    return loadSLocEntry(Index, Invalid);
1700
1.24G
  }
1701
1702
  const SrcMgr::SLocEntry &getSLocEntry(FileID FID,
1703
3.23G
                                        bool *Invalid = nullptr) const {
1704
3.23G
    if (FID.ID == 0 || 
FID.ID == -13.23G
) {
1705
1.20M
      if (Invalid) 
*Invalid = true1.20M
;
1706
1.20M
      return LocalSLocEntryTable[0];
1707
1.20M
    }
1708
3.23G
    return getSLocEntryByID(FID.ID, Invalid);
1709
3.23G
  }
1710
1711
50.7M
  SourceLocation::UIntTy getNextLocalOffset() const { return NextLocalOffset; }
1712
1713
14.0k
  void setExternalSLocEntrySource(ExternalSLocEntrySource *Source) {
1714
14.0k
    assert(LoadedSLocEntryTable.empty() &&
1715
14.0k
           "Invalidating existing loaded entries");
1716
0
    ExternalSLocEntries = Source;
1717
14.0k
  }
1718
1719
  /// Allocate a number of loaded SLocEntries, which will be actually
1720
  /// loaded on demand from the external source.
1721
  ///
1722
  /// NumSLocEntries will be allocated, which occupy a total of TotalSize space
1723
  /// in the global source view. The lowest ID and the base offset of the
1724
  /// entries will be returned.
1725
  std::pair<int, SourceLocation::UIntTy>
1726
  AllocateLoadedSLocEntries(unsigned NumSLocEntries,
1727
                            SourceLocation::UIntTy TotalSize);
1728
1729
  /// Returns true if \p Loc came from a PCH/Module.
1730
2.37k
  bool isLoadedSourceLocation(SourceLocation Loc) const {
1731
2.37k
    return Loc.getOffset() >= CurrentLoadedOffset;
1732
2.37k
  }
1733
1734
  /// Returns true if \p Loc did not come from a PCH/Module.
1735
3.78M
  bool isLocalSourceLocation(SourceLocation Loc) const {
1736
3.78M
    return Loc.getOffset() < NextLocalOffset;
1737
3.78M
  }
1738
1739
  /// Returns true if \p FID came from a PCH/Module.
1740
470k
  bool isLoadedFileID(FileID FID) const {
1741
470k
    assert(FID.ID != -1 && "Using FileID sentinel value");
1742
0
    return FID.ID < 0;
1743
470k
  }
1744
1745
  /// Returns true if \p FID did not come from a PCH/Module.
1746
0
  bool isLocalFileID(FileID FID) const {
1747
0
    return !isLoadedFileID(FID);
1748
0
  }
1749
1750
  /// Gets the location of the immediate macro caller, one level up the stack
1751
  /// toward the initial macro typed into the source.
1752
22.6k
  SourceLocation getImmediateMacroCallerLoc(SourceLocation Loc) const {
1753
22.6k
    if (!Loc.isMacroID()) 
return Loc0
;
1754
1755
    // When we have the location of (part of) an expanded parameter, its
1756
    // spelling location points to the argument as expanded in the macro call,
1757
    // and therefore is used to locate the macro caller.
1758
22.6k
    if (isMacroArgExpansion(Loc))
1759
10.7k
      return getImmediateSpellingLoc(Loc);
1760
1761
    // Otherwise, the caller of the macro is located where this macro is
1762
    // expanded (while the spelling is part of the macro definition).
1763
11.9k
    return getImmediateExpansionRange(Loc).getBegin();
1764
22.6k
  }
1765
1766
  /// \return Location of the top-level macro caller.
1767
  SourceLocation getTopMacroCallerLoc(SourceLocation Loc) const;
1768
1769
private:
1770
  friend class ASTReader;
1771
  friend class ASTWriter;
1772
1773
  llvm::MemoryBufferRef getFakeBufferForRecovery() const;
1774
  SrcMgr::ContentCache &getFakeContentCacheForRecovery() const;
1775
1776
  const SrcMgr::SLocEntry &loadSLocEntry(unsigned Index, bool *Invalid) const;
1777
1778
1.45G
  const SrcMgr::SLocEntry *getSLocEntryOrNull(FileID FID) const {
1779
1.45G
    bool Invalid = false;
1780
1.45G
    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
1781
1.45G
    return Invalid ? 
nullptr1.20M
:
&Entry1.45G
;
1782
1.45G
  }
1783
1784
551M
  const SrcMgr::SLocEntry *getSLocEntryForFile(FileID FID) const {
1785
551M
    if (auto *Entry = getSLocEntryOrNull(FID))
1786
551M
      if (Entry->isFile())
1787
551M
        return Entry;
1788
1.72k
    return nullptr;
1789
551M
  }
1790
1791
  /// Get the entry with the given unwrapped FileID.
1792
  /// Invalid will not be modified for Local IDs.
1793
  const SrcMgr::SLocEntry &getSLocEntryByID(int ID,
1794
4.17G
                                            bool *Invalid = nullptr) const {
1795
4.17G
    assert(ID != -1 && "Using FileID sentinel value");
1796
4.17G
    if (ID < 0)
1797
696M
      return getLoadedSLocEntryByID(ID, Invalid);
1798
3.47G
    return getLocalSLocEntry(static_cast<unsigned>(ID));
1799
4.17G
  }
1800
1801
  const SrcMgr::SLocEntry &
1802
723M
  getLoadedSLocEntryByID(int ID, bool *Invalid = nullptr) const {
1803
723M
    return getLoadedSLocEntry(static_cast<unsigned>(-ID - 2), Invalid);
1804
723M
  }
1805
1806
  /// Implements the common elements of storing an expansion info struct into
1807
  /// the SLocEntry table and producing a source location that refers to it.
1808
  SourceLocation
1809
  createExpansionLocImpl(const SrcMgr::ExpansionInfo &Expansion,
1810
                         unsigned Length, int LoadedID = 0,
1811
                         SourceLocation::UIntTy LoadedOffset = 0);
1812
1813
  /// Return true if the specified FileID contains the
1814
  /// specified SourceLocation offset.  This is a very hot method.
1815
  inline bool isOffsetInFileID(FileID FID,
1816
1.39G
                               SourceLocation::UIntTy SLocOffset) const {
1817
1.39G
    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID);
1818
    // If the entry is after the offset, it can't contain it.
1819
1.39G
    if (SLocOffset < Entry.getOffset()) 
return false184M
;
1820
1821
    // If this is the very last entry then it does.
1822
1.21G
    if (FID.ID == -2)
1823
44.5k
      return true;
1824
1825
    // If it is the last local entry, then it does if the location is local.
1826
1.21G
    if (FID.ID+1 == static_cast<int>(LocalSLocEntryTable.size()))
1827
274M
      return SLocOffset < NextLocalOffset;
1828
1829
    // Otherwise, the entry after it has to not include it. This works for both
1830
    // local and loaded entries.
1831
936M
    return SLocOffset < getSLocEntryByID(FID.ID+1).getOffset();
1832
1.21G
  }
1833
1834
  /// Returns the previous in-order FileID or an invalid FileID if there
1835
  /// is no previous one.
1836
  FileID getPreviousFileID(FileID FID) const;
1837
1838
  /// Returns the next in-order FileID or an invalid FileID if there is
1839
  /// no next one.
1840
  FileID getNextFileID(FileID FID) const;
1841
1842
  /// Create a new fileID for the specified ContentCache and
1843
  /// include position.
1844
  ///
1845
  /// This works regardless of whether the ContentCache corresponds to a
1846
  /// file or some other input source.
1847
  FileID createFileIDImpl(SrcMgr::ContentCache &File, StringRef Filename,
1848
                          SourceLocation IncludePos,
1849
                          SrcMgr::CharacteristicKind DirCharacter, int LoadedID,
1850
                          SourceLocation::UIntTy LoadedOffset);
1851
1852
  SrcMgr::ContentCache &getOrCreateContentCache(FileEntryRef SourceFile,
1853
                                                bool isSystemFile = false);
1854
1855
  /// Create a new ContentCache for the specified  memory buffer.
1856
  SrcMgr::ContentCache &
1857
  createMemBufferContentCache(std::unique_ptr<llvm::MemoryBuffer> Buf);
1858
1859
  FileID getFileIDSlow(SourceLocation::UIntTy SLocOffset) const;
1860
  FileID getFileIDLocal(SourceLocation::UIntTy SLocOffset) const;
1861
  FileID getFileIDLoaded(SourceLocation::UIntTy SLocOffset) const;
1862
1863
  SourceLocation getExpansionLocSlowCase(SourceLocation Loc) const;
1864
  SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;
1865
  SourceLocation getFileLocSlowCase(SourceLocation Loc) const;
1866
1867
  std::pair<FileID, unsigned>
1868
  getDecomposedExpansionLocSlowCase(const SrcMgr::SLocEntry *E) const;
1869
  std::pair<FileID, unsigned>
1870
  getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
1871
                                   unsigned Offset) const;
1872
  void computeMacroArgsCache(MacroArgsMap &MacroArgsCache, FileID FID) const;
1873
  void associateFileChunkWithMacroArgExp(MacroArgsMap &MacroArgsCache,
1874
                                         FileID FID,
1875
                                         SourceLocation SpellLoc,
1876
                                         SourceLocation ExpansionLoc,
1877
                                         unsigned ExpansionLength) const;
1878
};
1879
1880
/// Comparison function object.
1881
template<typename T>
1882
class BeforeThanCompare;
1883
1884
/// Compare two source locations.
1885
template<>
1886
class BeforeThanCompare<SourceLocation> {
1887
  SourceManager &SM;
1888
1889
public:
1890
168
  explicit BeforeThanCompare(SourceManager &SM) : SM(SM) {}
1891
1892
323
  bool operator()(SourceLocation LHS, SourceLocation RHS) const {
1893
323
    return SM.isBeforeInTranslationUnit(LHS, RHS);
1894
323
  }
1895
};
1896
1897
/// Compare two non-overlapping source ranges.
1898
template<>
1899
class BeforeThanCompare<SourceRange> {
1900
  SourceManager &SM;
1901
1902
public:
1903
0
  explicit BeforeThanCompare(SourceManager &SM) : SM(SM) {}
1904
1905
0
  bool operator()(SourceRange LHS, SourceRange RHS) const {
1906
0
    return SM.isBeforeInTranslationUnit(LHS.getBegin(), RHS.getBegin());
1907
0
  }
1908
};
1909
1910
/// SourceManager and necessary depdencies (e.g. VFS, FileManager) for a single
1911
/// in-memorty file.
1912
class SourceManagerForFile {
1913
public:
1914
  /// Creates SourceManager and necessary depdencies (e.g. VFS, FileManager).
1915
  /// The main file in the SourceManager will be \p FileName with \p Content.
1916
  SourceManagerForFile(StringRef FileName, StringRef Content);
1917
1918
128k
  SourceManager &get() {
1919
128k
    assert(SourceMgr);
1920
0
    return *SourceMgr;
1921
128k
  }
1922
1923
private:
1924
  // The order of these fields are important - they should be in the same order
1925
  // as they are created in `createSourceManagerForFile` so that they can be
1926
  // deleted in the reverse order as they are created.
1927
  std::unique_ptr<FileManager> FileMgr;
1928
  std::unique_ptr<DiagnosticsEngine> Diagnostics;
1929
  std::unique_ptr<SourceManager> SourceMgr;
1930
};
1931
1932
} // namespace clang
1933
1934
#endif // LLVM_CLANG_BASIC_SOURCEMANAGER_H