Coverage Report

Created: 2020-02-15 09:57

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