Coverage Report

Created: 2021-09-21 08:58

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