Coverage Report

Created: 2019-06-16 23:17

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