Coverage Report

Created: 2021-01-23 06:44

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