Coverage Report

Created: 2022-01-25 06:29

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Basic/SourceManager.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- SourceManager.cpp - Track and cache source files -------------------===//
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
//  This file implements the SourceManager interface.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/Basic/SourceManager.h"
14
#include "clang/Basic/Diagnostic.h"
15
#include "clang/Basic/FileManager.h"
16
#include "clang/Basic/LLVM.h"
17
#include "clang/Basic/SourceLocation.h"
18
#include "clang/Basic/SourceManagerInternals.h"
19
#include "llvm/ADT/DenseMap.h"
20
#include "llvm/ADT/None.h"
21
#include "llvm/ADT/Optional.h"
22
#include "llvm/ADT/STLExtras.h"
23
#include "llvm/ADT/SmallVector.h"
24
#include "llvm/ADT/StringRef.h"
25
#include "llvm/ADT/StringSwitch.h"
26
#include "llvm/Support/Allocator.h"
27
#include "llvm/Support/Capacity.h"
28
#include "llvm/Support/Compiler.h"
29
#include "llvm/Support/Endian.h"
30
#include "llvm/Support/ErrorHandling.h"
31
#include "llvm/Support/FileSystem.h"
32
#include "llvm/Support/MathExtras.h"
33
#include "llvm/Support/MemoryBuffer.h"
34
#include "llvm/Support/Path.h"
35
#include "llvm/Support/raw_ostream.h"
36
#include <algorithm>
37
#include <cassert>
38
#include <cstddef>
39
#include <cstdint>
40
#include <memory>
41
#include <tuple>
42
#include <utility>
43
#include <vector>
44
45
using namespace clang;
46
using namespace SrcMgr;
47
using llvm::MemoryBuffer;
48
49
//===----------------------------------------------------------------------===//
50
// SourceManager Helper Classes
51
//===----------------------------------------------------------------------===//
52
53
/// getSizeBytesMapped - Returns the number of bytes actually mapped for this
54
/// ContentCache. This can be 0 if the MemBuffer was not actually expanded.
55
8
unsigned ContentCache::getSizeBytesMapped() const {
56
8
  return Buffer ? Buffer->getBufferSize() : 
00
;
57
8
}
58
59
/// Returns the kind of memory used to back the memory buffer for
60
/// this content cache.  This is used for performance analysis.
61
1
llvm::MemoryBuffer::BufferKind ContentCache::getMemoryBufferKind() const {
62
1
  if (Buffer == nullptr) {
63
0
    assert(0 && "Buffer should never be null");
64
0
    return llvm::MemoryBuffer::MemoryBuffer_Malloc;
65
0
  }
66
1
  return Buffer->getBufferKind();
67
1
}
68
69
/// getSize - Returns the size of the content encapsulated by this ContentCache.
70
///  This can be the size of the source file or the size of an arbitrary
71
///  scratch buffer.  If the ContentCache encapsulates a source file, that
72
///  file is not lazily brought in from disk to satisfy this query.
73
984k
unsigned ContentCache::getSize() const {
74
984k
  return Buffer ? 
(unsigned)Buffer->getBufferSize()307k
75
984k
                : 
(unsigned)ContentsEntry->getSize()676k
;
76
984k
}
77
78
719k
const char *ContentCache::getInvalidBOM(StringRef BufStr) {
79
  // If the buffer is valid, check to see if it has a UTF Byte Order Mark
80
  // (BOM).  We only support UTF-8 with and without a BOM right now.  See
81
  // http://en.wikipedia.org/wiki/Byte_order_mark for more information.
82
719k
  const char *InvalidBOM =
83
719k
      llvm::StringSwitch<const char *>(BufStr)
84
719k
          .StartsWith(llvm::StringLiteral::withInnerNUL("\x00\x00\xFE\xFF"),
85
719k
                      "UTF-32 (BE)")
86
719k
          .StartsWith(llvm::StringLiteral::withInnerNUL("\xFF\xFE\x00\x00"),
87
719k
                      "UTF-32 (LE)")
88
719k
          .StartsWith("\xFE\xFF", "UTF-16 (BE)")
89
719k
          .StartsWith("\xFF\xFE", "UTF-16 (LE)")
90
719k
          .StartsWith("\x2B\x2F\x76", "UTF-7")
91
719k
          .StartsWith("\xF7\x64\x4C", "UTF-1")
92
719k
          .StartsWith("\xDD\x73\x66\x73", "UTF-EBCDIC")
93
719k
          .StartsWith("\x0E\xFE\xFF", "SCSU")
94
719k
          .StartsWith("\xFB\xEE\x28", "BOCU-1")
95
719k
          .StartsWith("\x84\x31\x95\x33", "GB-18030")
96
719k
          .Default(nullptr);
97
98
719k
  return InvalidBOM;
99
719k
}
100
101
llvm::Optional<llvm::MemoryBufferRef>
102
ContentCache::getBufferOrNone(DiagnosticsEngine &Diag, FileManager &FM,
103
268M
                              SourceLocation Loc) const {
104
  // Lazily create the Buffer for ContentCaches that wrap files.  If we already
105
  // computed it, just return what we have.
106
268M
  if (IsBufferInvalid)
107
2
    return None;
108
268M
  if (Buffer)
109
267M
    return Buffer->getMemBufferRef();
110
719k
  if (!ContentsEntry)
111
0
    return None;
112
113
  // Start with the assumption that the buffer is invalid to simplify early
114
  // return paths.
115
719k
  IsBufferInvalid = true;
116
117
719k
  auto BufferOrError = FM.getBufferForFile(ContentsEntry, IsFileVolatile);
118
119
  // If we were unable to open the file, then we are in an inconsistent
120
  // situation where the content cache referenced a file which no longer
121
  // exists. Most likely, we were using a stat cache with an invalid entry but
122
  // the file could also have been removed during processing. Since we can't
123
  // really deal with this situation, just create an empty buffer.
124
719k
  if (!BufferOrError) {
125
1
    if (Diag.isDiagnosticInFlight())
126
0
      Diag.SetDelayedDiagnostic(diag::err_cannot_open_file,
127
0
                                ContentsEntry->getName(),
128
0
                                BufferOrError.getError().message());
129
1
    else
130
1
      Diag.Report(Loc, diag::err_cannot_open_file)
131
1
          << ContentsEntry->getName() << BufferOrError.getError().message();
132
133
1
    return None;
134
1
  }
135
136
719k
  Buffer = std::move(*BufferOrError);
137
138
  // Check that the file's size fits in an 'unsigned' (with room for a
139
  // past-the-end value). This is deeply regrettable, but various parts of
140
  // Clang (including elsewhere in this file!) use 'unsigned' to represent file
141
  // offsets, line numbers, string literal lengths, and so on, and fail
142
  // miserably on large source files.
143
  //
144
  // Note: ContentsEntry could be a named pipe, in which case
145
  // ContentsEntry::getSize() could have the wrong size. Use
146
  // MemoryBuffer::getBufferSize() instead.
147
719k
  if (Buffer->getBufferSize() >= std::numeric_limits<unsigned>::max()) {
148
0
    if (Diag.isDiagnosticInFlight())
149
0
      Diag.SetDelayedDiagnostic(diag::err_file_too_large,
150
0
                                ContentsEntry->getName());
151
0
    else
152
0
      Diag.Report(Loc, diag::err_file_too_large)
153
0
        << ContentsEntry->getName();
154
155
0
    return None;
156
0
  }
157
158
  // Unless this is a named pipe (in which case we can handle a mismatch),
159
  // check that the file's size is the same as in the file entry (which may
160
  // have come from a stat cache).
161
719k
  if (!ContentsEntry->isNamedPipe() &&
162
719k
      
Buffer->getBufferSize() != (size_t)ContentsEntry->getSize()718k
) {
163
0
    if (Diag.isDiagnosticInFlight())
164
0
      Diag.SetDelayedDiagnostic(diag::err_file_modified,
165
0
                                ContentsEntry->getName());
166
0
    else
167
0
      Diag.Report(Loc, diag::err_file_modified)
168
0
        << ContentsEntry->getName();
169
170
0
    return None;
171
0
  }
172
173
  // If the buffer is valid, check to see if it has a UTF Byte Order Mark
174
  // (BOM).  We only support UTF-8 with and without a BOM right now.  See
175
  // http://en.wikipedia.org/wiki/Byte_order_mark for more information.
176
719k
  StringRef BufStr = Buffer->getBuffer();
177
719k
  const char *InvalidBOM = getInvalidBOM(BufStr);
178
179
719k
  if (InvalidBOM) {
180
1
    Diag.Report(Loc, diag::err_unsupported_bom)
181
1
      << InvalidBOM << ContentsEntry->getName();
182
1
    return None;
183
1
  }
184
185
  // Buffer has been validated.
186
719k
  IsBufferInvalid = false;
187
719k
  return Buffer->getMemBufferRef();
188
719k
}
189
190
964k
unsigned LineTableInfo::getLineTableFilenameID(StringRef Name) {
191
964k
  auto IterBool = FilenameIDs.try_emplace(Name, FilenamesByID.size());
192
964k
  if (IterBool.second)
193
856k
    FilenamesByID.push_back(&*IterBool.first);
194
964k
  return IterBool.first->second;
195
964k
}
196
197
/// Add a line note to the line table that indicates that there is a \#line or
198
/// GNU line marker at the specified FID/Offset location which changes the
199
/// presumed location to LineNo/FilenameID. If EntryExit is 0, then this doesn't
200
/// change the presumed \#include stack.  If it is 1, this is a file entry, if
201
/// it is 2 then this is a file exit. FileKind specifies whether this is a
202
/// system header or extern C system header.
203
void LineTableInfo::AddLineNote(FileID FID, unsigned Offset, unsigned LineNo,
204
                                int FilenameID, unsigned EntryExit,
205
422k
                                SrcMgr::CharacteristicKind FileKind) {
206
422k
  std::vector<LineEntry> &Entries = LineEntries[FID];
207
208
422k
  assert((Entries.empty() || Entries.back().FileOffset < Offset) &&
209
422k
         "Adding line entries out of order!");
210
211
0
  unsigned IncludeOffset = 0;
212
422k
  if (EntryExit == 1) {
213
    // Push #include
214
84.2k
    IncludeOffset = Offset-1;
215
338k
  } else {
216
338k
    const auto *PrevEntry = Entries.empty() ? 
nullptr230k
:
&Entries.back()108k
;
217
338k
    if (EntryExit == 2) {
218
      // Pop #include
219
84.2k
      assert(PrevEntry && PrevEntry->IncludeOffset &&
220
84.2k
             "PPDirectives should have caught case when popping empty include "
221
84.2k
             "stack");
222
0
      PrevEntry = FindNearestLineEntry(FID, PrevEntry->IncludeOffset);
223
84.2k
    }
224
338k
    if (PrevEntry) {
225
108k
      IncludeOffset = PrevEntry->IncludeOffset;
226
108k
      if (FilenameID == -1) {
227
        // An unspecified FilenameID means use the previous (or containing)
228
        // filename if available, or the main source file otherwise.
229
1.73k
        FilenameID = PrevEntry->FilenameID;
230
1.73k
      }
231
108k
    }
232
338k
  }
233
234
0
  Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, FileKind,
235
422k
                                   IncludeOffset));
236
422k
}
237
238
/// FindNearestLineEntry - Find the line entry nearest to FID that is before
239
/// it.  If there is no line entry before Offset in FID, return null.
240
const LineEntry *LineTableInfo::FindNearestLineEntry(FileID FID,
241
122M
                                                     unsigned Offset) {
242
122M
  const std::vector<LineEntry> &Entries = LineEntries[FID];
243
122M
  assert(!Entries.empty() && "No #line entries for this FID after all!");
244
245
  // It is very common for the query to be after the last #line, check this
246
  // first.
247
122M
  if (Entries.back().FileOffset <= Offset)
248
121M
    return &Entries.back();
249
250
  // Do a binary search to find the maximal element that is still before Offset.
251
1.55M
  std::vector<LineEntry>::const_iterator I = llvm::upper_bound(Entries, Offset);
252
1.55M
  if (I == Entries.begin())
253
1.42M
    return nullptr;
254
131k
  return &*--I;
255
1.55M
}
256
257
/// Add a new line entry that has already been encoded into
258
/// the internal representation of the line table.
259
void LineTableInfo::AddEntry(FileID FID,
260
526k
                             const std::vector<LineEntry> &Entries) {
261
526k
  LineEntries[FID] = Entries;
262
526k
}
263
264
/// getLineTableFilenameID - Return the uniqued ID for the specified filename.
265
420k
unsigned SourceManager::getLineTableFilenameID(StringRef Name) {
266
420k
  return getLineTable().getLineTableFilenameID(Name);
267
420k
}
268
269
/// AddLineNote - Add a line note to the line table for the FileID and offset
270
/// specified by Loc.  If FilenameID is -1, it is considered to be
271
/// unspecified.
272
void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo,
273
                                int FilenameID, bool IsFileEntry,
274
                                bool IsFileExit,
275
422k
                                SrcMgr::CharacteristicKind FileKind) {
276
422k
  std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc);
277
278
422k
  bool Invalid = false;
279
422k
  const SLocEntry &Entry = getSLocEntry(LocInfo.first, &Invalid);
280
422k
  if (!Entry.isFile() || 
Invalid422k
)
281
0
    return;
282
283
422k
  const SrcMgr::FileInfo &FileInfo = Entry.getFile();
284
285
  // Remember that this file has #line directives now if it doesn't already.
286
422k
  const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives();
287
288
422k
  (void) getLineTable();
289
290
422k
  unsigned EntryExit = 0;
291
422k
  if (IsFileEntry)
292
84.2k
    EntryExit = 1;
293
338k
  else if (IsFileExit)
294
84.2k
    EntryExit = 2;
295
296
422k
  LineTable->AddLineNote(LocInfo.first, LocInfo.second, LineNo, FilenameID,
297
422k
                         EntryExit, FileKind);
298
422k
}
299
300
867k
LineTableInfo &SourceManager::getLineTable() {
301
867k
  if (!LineTable)
302
87.6k
    LineTable.reset(new LineTableInfo());
303
867k
  return *LineTable;
304
867k
}
305
306
//===----------------------------------------------------------------------===//
307
// Private 'Create' methods.
308
//===----------------------------------------------------------------------===//
309
310
SourceManager::SourceManager(DiagnosticsEngine &Diag, FileManager &FileMgr,
311
                             bool UserFilesAreVolatile)
312
211k
  : Diag(Diag), FileMgr(FileMgr), UserFilesAreVolatile(UserFilesAreVolatile) {
313
211k
  clearIDTables();
314
211k
  Diag.setSourceManager(this);
315
211k
}
316
317
206k
SourceManager::~SourceManager() {
318
  // Delete FileEntry objects corresponding to content caches.  Since the actual
319
  // content cache objects are bump pointer allocated, we just have to run the
320
  // dtors, but we call the deallocate method for completeness.
321
360k
  for (unsigned i = 0, e = MemBufferInfos.size(); i != e; 
++i153k
) {
322
153k
    if (
MemBufferInfos[i]153k
) {
323
153k
      MemBufferInfos[i]->~ContentCache();
324
153k
      ContentCacheAlloc.Deallocate(MemBufferInfos[i]);
325
153k
    }
326
153k
  }
327
206k
  for (llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>::iterator
328
678k
       I = FileInfos.begin(), E = FileInfos.end(); I != E; 
++I472k
) {
329
472k
    if (
I->second472k
) {
330
472k
      I->second->~ContentCache();
331
472k
      ContentCacheAlloc.Deallocate(I->second);
332
472k
    }
333
472k
  }
334
206k
}
335
336
233k
void SourceManager::clearIDTables() {
337
233k
  MainFileID = FileID();
338
233k
  LocalSLocEntryTable.clear();
339
233k
  LoadedSLocEntryTable.clear();
340
233k
  SLocEntryLoaded.clear();
341
233k
  LastLineNoFileIDQuery = FileID();
342
233k
  LastLineNoContentCache = nullptr;
343
233k
  LastFileIDLookup = FileID();
344
345
233k
  if (LineTable)
346
46
    LineTable->clear();
347
348
  // Use up FileID #0 as an invalid expansion.
349
233k
  NextLocalOffset = 0;
350
233k
  CurrentLoadedOffset = MaxLoadedOffset;
351
233k
  createExpansionLoc(SourceLocation(), SourceLocation(), SourceLocation(), 1);
352
233k
}
353
354
78
bool SourceManager::isMainFile(const FileEntry &SourceFile) {
355
78
  assert(MainFileID.isValid() && "expected initialized SourceManager");
356
78
  if (auto *FE = getFileEntryForID(MainFileID))
357
77
    return FE->getUID() == SourceFile.getUID();
358
1
  return false;
359
78
}
360
361
15
void SourceManager::initializeForReplay(const SourceManager &Old) {
362
15
  assert(MainFileID.isInvalid() && "expected uninitialized SourceManager");
363
364
52
  auto CloneContentCache = [&](const ContentCache *Cache) -> ContentCache * {
365
52
    auto *Clone = new (ContentCacheAlloc.Allocate<ContentCache>()) ContentCache;
366
52
    Clone->OrigEntry = Cache->OrigEntry;
367
52
    Clone->ContentsEntry = Cache->ContentsEntry;
368
52
    Clone->BufferOverridden = Cache->BufferOverridden;
369
52
    Clone->IsFileVolatile = Cache->IsFileVolatile;
370
52
    Clone->IsTransient = Cache->IsTransient;
371
52
    Clone->setUnownedBuffer(Cache->getBufferIfLoaded());
372
52
    return Clone;
373
52
  };
374
375
  // Ensure all SLocEntries are loaded from the external source.
376
142
  for (unsigned I = 0, N = Old.LoadedSLocEntryTable.size(); I != N; 
++I127
)
377
127
    if (!Old.SLocEntryLoaded[I])
378
102
      Old.loadSLocEntry(I, nullptr);
379
380
  // Inherit any content cache data from the old source manager.
381
52
  for (auto &FileInfo : Old.FileInfos) {
382
52
    SrcMgr::ContentCache *&Slot = FileInfos[FileInfo.first];
383
52
    if (Slot)
384
0
      continue;
385
52
    Slot = CloneContentCache(FileInfo.second);
386
52
  }
387
15
}
388
389
ContentCache &SourceManager::getOrCreateContentCache(FileEntryRef FileEnt,
390
2.06M
                                                     bool isSystemFile) {
391
  // Do we already have information about this file?
392
2.06M
  ContentCache *&Entry = FileInfos[FileEnt];
393
2.06M
  if (Entry)
394
1.11M
    return *Entry;
395
396
  // Nope, create a new Cache entry.
397
958k
  Entry = ContentCacheAlloc.Allocate<ContentCache>();
398
399
958k
  if (OverriddenFilesInfo) {
400
    // If the file contents are overridden with contents from another file,
401
    // pass that file to ContentCache.
402
83.8k
    llvm::DenseMap<const FileEntry *, const FileEntry *>::iterator
403
83.8k
        overI = OverriddenFilesInfo->OverriddenFiles.find(FileEnt);
404
83.8k
    if (overI == OverriddenFilesInfo->OverriddenFiles.end())
405
83.8k
      new (Entry) ContentCache(FileEnt);
406
11
    else
407
11
      new (Entry) ContentCache(OverridenFilesKeepOriginalName ? 
FileEnt7
408
11
                                                              : 
overI->second4
,
409
11
                               overI->second);
410
874k
  } else {
411
874k
    new (Entry) ContentCache(FileEnt);
412
874k
  }
413
414
958k
  Entry->IsFileVolatile = UserFilesAreVolatile && 
!isSystemFile2.70k
;
415
958k
  Entry->IsTransient = FilesAreTransient;
416
958k
  Entry->BufferOverridden |= FileEnt.isNamedPipe();
417
418
958k
  return *Entry;
419
2.06M
}
420
421
/// Create a new ContentCache for the specified memory buffer.
422
/// This does no caching.
423
ContentCache &SourceManager::createMemBufferContentCache(
424
225k
    std::unique_ptr<llvm::MemoryBuffer> Buffer) {
425
  // Add a new ContentCache to the MemBufferInfos list and return it.
426
225k
  ContentCache *Entry = ContentCacheAlloc.Allocate<ContentCache>();
427
225k
  new (Entry) ContentCache();
428
225k
  MemBufferInfos.push_back(Entry);
429
225k
  Entry->setBuffer(std::move(Buffer));
430
225k
  return *Entry;
431
225k
}
432
433
const SrcMgr::SLocEntry &SourceManager::loadSLocEntry(unsigned Index,
434
9.56M
                                                      bool *Invalid) const {
435
9.56M
  assert(!SLocEntryLoaded[Index]);
436
9.56M
  if (ExternalSLocEntries->ReadSLocEntry(-(static_cast<int>(Index) + 2))) {
437
94
    if (Invalid)
438
44
      *Invalid = true;
439
    // If the file of the SLocEntry changed we could still have loaded it.
440
94
    if (!SLocEntryLoaded[Index]) {
441
      // Try to recover; create a SLocEntry so the rest of clang can handle it.
442
94
      if (!FakeSLocEntryForRecovery)
443
2
        FakeSLocEntryForRecovery = std::make_unique<SLocEntry>(SLocEntry::get(
444
2
            0, FileInfo::get(SourceLocation(), getFakeContentCacheForRecovery(),
445
2
                             SrcMgr::C_User, "")));
446
94
      return *FakeSLocEntryForRecovery;
447
94
    }
448
94
  }
449
450
9.56M
  return LoadedSLocEntryTable[Index];
451
9.56M
}
452
453
std::pair<int, SourceLocation::UIntTy>
454
SourceManager::AllocateLoadedSLocEntries(unsigned NumSLocEntries,
455
17.9k
                                         SourceLocation::UIntTy TotalSize) {
456
17.9k
  assert(ExternalSLocEntries && "Don't have an external sloc source");
457
  // Make sure we're not about to run out of source locations.
458
17.9k
  if (CurrentLoadedOffset - TotalSize < NextLocalOffset)
459
0
    return std::make_pair(0, 0);
460
17.9k
  LoadedSLocEntryTable.resize(LoadedSLocEntryTable.size() + NumSLocEntries);
461
17.9k
  SLocEntryLoaded.resize(LoadedSLocEntryTable.size());
462
17.9k
  CurrentLoadedOffset -= TotalSize;
463
17.9k
  int ID = LoadedSLocEntryTable.size();
464
17.9k
  return std::make_pair(-ID - 1, CurrentLoadedOffset);
465
17.9k
}
466
467
/// As part of recovering from missing or changed content, produce a
468
/// fake, non-empty buffer.
469
5
llvm::MemoryBufferRef SourceManager::getFakeBufferForRecovery() const {
470
5
  if (!FakeBufferForRecovery)
471
5
    FakeBufferForRecovery =
472
5
        llvm::MemoryBuffer::getMemBuffer("<<<INVALID BUFFER>>");
473
474
5
  return *FakeBufferForRecovery;
475
5
}
476
477
/// As part of recovering from missing or changed content, produce a
478
/// fake content cache.
479
2
SrcMgr::ContentCache &SourceManager::getFakeContentCacheForRecovery() const {
480
2
  if (!FakeContentCacheForRecovery) {
481
2
    FakeContentCacheForRecovery = std::make_unique<SrcMgr::ContentCache>();
482
2
    FakeContentCacheForRecovery->setUnownedBuffer(getFakeBufferForRecovery());
483
2
  }
484
2
  return *FakeContentCacheForRecovery;
485
2
}
486
487
/// Returns the previous in-order FileID or an invalid FileID if there
488
/// is no previous one.
489
2.24M
FileID SourceManager::getPreviousFileID(FileID FID) const {
490
2.24M
  if (FID.isInvalid())
491
0
    return FileID();
492
493
2.24M
  int ID = FID.ID;
494
2.24M
  if (ID == -1)
495
0
    return FileID();
496
497
2.24M
  if (ID > 0) {
498
2.24M
    if (ID-1 == 0)
499
0
      return FileID();
500
2.24M
  } else 
if (0
unsigned(-(ID-1) - 2) >= LoadedSLocEntryTable.size()0
) {
501
0
    return FileID();
502
0
  }
503
504
2.24M
  return FileID::get(ID-1);
505
2.24M
}
506
507
/// Returns the next in-order FileID or an invalid FileID if there is
508
/// no next one.
509
47.6k
FileID SourceManager::getNextFileID(FileID FID) const {
510
47.6k
  if (FID.isInvalid())
511
0
    return FileID();
512
513
47.6k
  int ID = FID.ID;
514
47.6k
  if (ID > 0) {
515
47.6k
    if (unsigned(ID+1) >= local_sloc_entry_size())
516
46
      return FileID();
517
47.6k
  } else 
if (0
ID+1 >= -10
) {
518
0
    return FileID();
519
0
  }
520
521
47.5k
  return FileID::get(ID+1);
522
47.6k
}
523
524
//===----------------------------------------------------------------------===//
525
// Methods to create new FileID's and macro expansions.
526
//===----------------------------------------------------------------------===//
527
528
/// Create a new FileID that represents the specified file
529
/// being \#included from the specified IncludePosition.
530
///
531
/// This translates NULL into standard input.
532
FileID SourceManager::createFileID(const FileEntry *SourceFile,
533
                                   SourceLocation IncludePos,
534
                                   SrcMgr::CharacteristicKind FileCharacter,
535
                                   int LoadedID,
536
63.9k
                                   SourceLocation::UIntTy LoadedOffset) {
537
63.9k
  return createFileID(SourceFile->getLastRef(), IncludePos, FileCharacter,
538
63.9k
                      LoadedID, LoadedOffset);
539
63.9k
}
540
541
FileID SourceManager::createFileID(FileEntryRef SourceFile,
542
                                   SourceLocation IncludePos,
543
                                   SrcMgr::CharacteristicKind FileCharacter,
544
                                   int LoadedID,
545
1.42M
                                   SourceLocation::UIntTy LoadedOffset) {
546
1.42M
  SrcMgr::ContentCache &IR = getOrCreateContentCache(SourceFile,
547
1.42M
                                                     isSystem(FileCharacter));
548
549
  // If this is a named pipe, immediately load the buffer to ensure subsequent
550
  // calls to ContentCache::getSize() are accurate.
551
1.42M
  if (IR.ContentsEntry->isNamedPipe())
552
661
    (void)IR.getBufferOrNone(Diag, getFileManager(), SourceLocation());
553
554
1.42M
  return createFileIDImpl(IR, SourceFile.getName(), IncludePos, FileCharacter,
555
1.42M
                          LoadedID, LoadedOffset);
556
1.42M
}
557
558
/// Create a new FileID that represents the specified memory buffer.
559
///
560
/// This does no caching of the buffer and takes ownership of the
561
/// MemoryBuffer, so only pass a MemoryBuffer to this once.
562
FileID SourceManager::createFileID(std::unique_ptr<llvm::MemoryBuffer> Buffer,
563
                                   SrcMgr::CharacteristicKind FileCharacter,
564
                                   int LoadedID,
565
                                   SourceLocation::UIntTy LoadedOffset,
566
225k
                                   SourceLocation IncludeLoc) {
567
225k
  StringRef Name = Buffer->getBufferIdentifier();
568
225k
  return createFileIDImpl(createMemBufferContentCache(std::move(Buffer)), Name,
569
225k
                          IncludeLoc, FileCharacter, LoadedID, LoadedOffset);
570
225k
}
571
572
/// Create a new FileID that represents the specified memory buffer.
573
///
574
/// This does not take ownership of the MemoryBuffer. The memory buffer must
575
/// outlive the SourceManager.
576
FileID SourceManager::createFileID(const llvm::MemoryBufferRef &Buffer,
577
                                   SrcMgr::CharacteristicKind FileCharacter,
578
                                   int LoadedID,
579
                                   SourceLocation::UIntTy LoadedOffset,
580
21.9k
                                   SourceLocation IncludeLoc) {
581
21.9k
  return createFileID(llvm::MemoryBuffer::getMemBuffer(Buffer), FileCharacter,
582
21.9k
                      LoadedID, LoadedOffset, IncludeLoc);
583
21.9k
}
584
585
/// Get the FileID for \p SourceFile if it exists. Otherwise, create a
586
/// new FileID for the \p SourceFile.
587
FileID
588
SourceManager::getOrCreateFileID(const FileEntry *SourceFile,
589
60.0k
                                 SrcMgr::CharacteristicKind FileCharacter) {
590
60.0k
  FileID ID = translateFile(SourceFile);
591
60.0k
  return ID.isValid() ? 
ID60.0k
: createFileID(SourceFile, SourceLocation(),
592
73
            FileCharacter);
593
60.0k
}
594
595
/// createFileID - Create a new FileID for the specified ContentCache and
596
/// include position.  This works regardless of whether the ContentCache
597
/// corresponds to a file or some other input source.
598
FileID SourceManager::createFileIDImpl(ContentCache &File, StringRef Filename,
599
                                       SourceLocation IncludePos,
600
                                       SrcMgr::CharacteristicKind FileCharacter,
601
                                       int LoadedID,
602
1.65M
                                       SourceLocation::UIntTy LoadedOffset) {
603
1.65M
  if (LoadedID < 0) {
604
667k
    assert(LoadedID != -1 && "Loading sentinel FileID");
605
0
    unsigned Index = unsigned(-LoadedID) - 2;
606
667k
    assert(Index < LoadedSLocEntryTable.size() && "FileID out of range");
607
0
    assert(!SLocEntryLoaded[Index] && "FileID already loaded");
608
0
    LoadedSLocEntryTable[Index] = SLocEntry::get(
609
667k
        LoadedOffset, FileInfo::get(IncludePos, File, FileCharacter, Filename));
610
667k
    SLocEntryLoaded[Index] = true;
611
667k
    return FileID::get(LoadedID);
612
667k
  }
613
984k
  unsigned FileSize = File.getSize();
614
984k
  if (!(NextLocalOffset + FileSize + 1 > NextLocalOffset &&
615
984k
        NextLocalOffset + FileSize + 1 <= CurrentLoadedOffset)) {
616
1
    Diag.Report(IncludePos, diag::err_include_too_large);
617
1
    return FileID();
618
1
  }
619
984k
  LocalSLocEntryTable.push_back(
620
984k
      SLocEntry::get(NextLocalOffset,
621
984k
                     FileInfo::get(IncludePos, File, FileCharacter, Filename)));
622
  // We do a +1 here because we want a SourceLocation that means "the end of the
623
  // file", e.g. for the "no newline at the end of the file" diagnostic.
624
984k
  NextLocalOffset += FileSize + 1;
625
626
  // Set LastFileIDLookup to the newly created file.  The next getFileID call is
627
  // almost guaranteed to be from that file.
628
984k
  FileID FID = FileID::get(LocalSLocEntryTable.size()-1);
629
984k
  return LastFileIDLookup = FID;
630
984k
}
631
632
SourceLocation
633
SourceManager::createMacroArgExpansionLoc(SourceLocation SpellingLoc,
634
                                          SourceLocation ExpansionLoc,
635
36.3M
                                          unsigned TokLength) {
636
36.3M
  ExpansionInfo Info = ExpansionInfo::createForMacroArg(SpellingLoc,
637
36.3M
                                                        ExpansionLoc);
638
36.3M
  return createExpansionLocImpl(Info, TokLength);
639
36.3M
}
640
641
SourceLocation SourceManager::createExpansionLoc(
642
    SourceLocation SpellingLoc, SourceLocation ExpansionLocStart,
643
    SourceLocation ExpansionLocEnd, unsigned TokLength,
644
    bool ExpansionIsTokenRange, int LoadedID,
645
76.4M
    SourceLocation::UIntTy LoadedOffset) {
646
76.4M
  ExpansionInfo Info = ExpansionInfo::create(
647
76.4M
      SpellingLoc, ExpansionLocStart, ExpansionLocEnd, ExpansionIsTokenRange);
648
76.4M
  return createExpansionLocImpl(Info, TokLength, LoadedID, LoadedOffset);
649
76.4M
}
650
651
SourceLocation SourceManager::createTokenSplitLoc(SourceLocation Spelling,
652
                                                  SourceLocation TokenStart,
653
17.5k
                                                  SourceLocation TokenEnd) {
654
17.5k
  assert(getFileID(TokenStart) == getFileID(TokenEnd) &&
655
17.5k
         "token spans multiple files");
656
0
  return createExpansionLocImpl(
657
17.5k
      ExpansionInfo::createForTokenSplit(Spelling, TokenStart, TokenEnd),
658
17.5k
      TokenEnd.getOffset() - TokenStart.getOffset());
659
17.5k
}
660
661
SourceLocation
662
SourceManager::createExpansionLocImpl(const ExpansionInfo &Info,
663
                                      unsigned TokLength, int LoadedID,
664
112M
                                      SourceLocation::UIntTy LoadedOffset) {
665
112M
  if (LoadedID < 0) {
666
8.89M
    assert(LoadedID != -1 && "Loading sentinel FileID");
667
0
    unsigned Index = unsigned(-LoadedID) - 2;
668
8.89M
    assert(Index < LoadedSLocEntryTable.size() && "FileID out of range");
669
0
    assert(!SLocEntryLoaded[Index] && "FileID already loaded");
670
0
    LoadedSLocEntryTable[Index] = SLocEntry::get(LoadedOffset, Info);
671
8.89M
    SLocEntryLoaded[Index] = true;
672
8.89M
    return SourceLocation::getMacroLoc(LoadedOffset);
673
8.89M
  }
674
103M
  LocalSLocEntryTable.push_back(SLocEntry::get(NextLocalOffset, Info));
675
103M
  assert(NextLocalOffset + TokLength + 1 > NextLocalOffset &&
676
103M
         NextLocalOffset + TokLength + 1 <= CurrentLoadedOffset &&
677
103M
         "Ran out of source locations!");
678
  // See createFileID for that +1.
679
0
  NextLocalOffset += TokLength + 1;
680
103M
  return SourceLocation::getMacroLoc(NextLocalOffset - (TokLength + 1));
681
112M
}
682
683
llvm::Optional<llvm::MemoryBufferRef>
684
1.29k
SourceManager::getMemoryBufferForFileOrNone(const FileEntry *File) {
685
1.29k
  SrcMgr::ContentCache &IR = getOrCreateContentCache(File->getLastRef());
686
1.29k
  return IR.getBufferOrNone(Diag, getFileManager(), SourceLocation());
687
1.29k
}
688
689
void SourceManager::overrideFileContents(
690
5.73k
    const FileEntry *SourceFile, std::unique_ptr<llvm::MemoryBuffer> Buffer) {
691
5.73k
  SrcMgr::ContentCache &IR = getOrCreateContentCache(SourceFile->getLastRef());
692
693
5.73k
  IR.setBuffer(std::move(Buffer));
694
5.73k
  IR.BufferOverridden = true;
695
696
5.73k
  getOverriddenFilesInfo().OverriddenFilesWithBuffer.insert(SourceFile);
697
5.73k
}
698
699
void SourceManager::overrideFileContents(const FileEntry *SourceFile,
700
18
                                         const FileEntry *NewFile) {
701
18
  assert(SourceFile->getSize() == NewFile->getSize() &&
702
18
         "Different sizes, use the FileManager to create a virtual file with "
703
18
         "the correct size");
704
0
  assert(FileInfos.count(SourceFile) == 0 &&
705
18
         "This function should be called at the initialization stage, before "
706
18
         "any parsing occurs.");
707
0
  getOverriddenFilesInfo().OverriddenFiles[SourceFile] = NewFile;
708
18
}
709
710
Optional<FileEntryRef>
711
1
SourceManager::bypassFileContentsOverride(FileEntryRef File) {
712
1
  assert(isFileOverridden(&File.getFileEntry()));
713
0
  llvm::Optional<FileEntryRef> BypassFile = FileMgr.getBypassFile(File);
714
715
  // If the file can't be found in the FS, give up.
716
1
  if (!BypassFile)
717
0
    return None;
718
719
1
  (void)getOrCreateContentCache(*BypassFile);
720
1
  return BypassFile;
721
1
}
722
723
2
void SourceManager::setFileIsTransient(const FileEntry *File) {
724
2
  getOrCreateContentCache(File->getLastRef()).IsTransient = true;
725
2
}
726
727
Optional<StringRef>
728
456k
SourceManager::getNonBuiltinFilenameForID(FileID FID) const {
729
456k
  if (const SrcMgr::SLocEntry *Entry = getSLocEntryForFile(FID))
730
456k
    if (Entry->getFile().getContentCache().OrigEntry)
731
449k
      return Entry->getFile().getName();
732
7.05k
  return None;
733
456k
}
734
735
108M
StringRef SourceManager::getBufferData(FileID FID, bool *Invalid) const {
736
108M
  auto B = getBufferDataOrNone(FID);
737
108M
  if (Invalid)
738
108M
    *Invalid = !B;
739
108M
  return B ? 
*B108M
:
"<<<<<INVALID SOURCE LOCATION>>>>>"27
;
740
108M
}
741
742
llvm::Optional<StringRef>
743
0
SourceManager::getBufferDataIfLoaded(FileID FID) const {
744
0
  if (const SrcMgr::SLocEntry *Entry = getSLocEntryForFile(FID))
745
0
    return Entry->getFile().getContentCache().getBufferDataIfLoaded();
746
0
  return None;
747
0
}
748
749
108M
llvm::Optional<StringRef> SourceManager::getBufferDataOrNone(FileID FID) const {
750
108M
  if (const SrcMgr::SLocEntry *Entry = getSLocEntryForFile(FID))
751
108M
    if (auto B = Entry->getFile().getContentCache().getBufferOrNone(
752
108M
            Diag, getFileManager(), SourceLocation()))
753
108M
      return B->getBuffer();
754
27
  return None;
755
108M
}
756
757
//===----------------------------------------------------------------------===//
758
// SourceLocation manipulation methods.
759
//===----------------------------------------------------------------------===//
760
761
/// Return the FileID for a SourceLocation.
762
///
763
/// This is the cache-miss path of getFileID. Not as hot as that function, but
764
/// still very important. It is responsible for finding the entry in the
765
/// SLocEntry tables that contains the specified location.
766
357M
FileID SourceManager::getFileIDSlow(SourceLocation::UIntTy SLocOffset) const {
767
357M
  if (!SLocOffset)
768
1.22M
    return FileID::get(0);
769
770
  // Now it is time to search for the correct file. See where the SLocOffset
771
  // sits in the global view and consult local or loaded buffers for it.
772
356M
  if (SLocOffset < NextLocalOffset)
773
336M
    return getFileIDLocal(SLocOffset);
774
19.9M
  return getFileIDLoaded(SLocOffset);
775
356M
}
776
777
/// Return the FileID for a SourceLocation with a low offset.
778
///
779
/// This function knows that the SourceLocation is in a local buffer, not a
780
/// loaded one.
781
336M
FileID SourceManager::getFileIDLocal(SourceLocation::UIntTy SLocOffset) const {
782
336M
  assert(SLocOffset < NextLocalOffset && "Bad function choice");
783
784
  // After the first and second level caches, I see two common sorts of
785
  // behavior: 1) a lot of searched FileID's are "near" the cached file
786
  // location or are "near" the cached expansion location. 2) others are just
787
  // completely random and may be a very long way away.
788
  //
789
  // To handle this, we do a linear search for up to 8 steps to catch #1 quickly
790
  // then we fall back to a less cache efficient, but more scalable, binary
791
  // search to find the location.
792
793
  // See if this is near the file point - worst case we start scanning from the
794
  // most newly created FileID.
795
0
  const SrcMgr::SLocEntry *I;
796
797
336M
  if (LastFileIDLookup.ID < 0 ||
798
336M
      
LocalSLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset335M
) {
799
    // Neither loc prunes our search.
800
156M
    I = LocalSLocEntryTable.end();
801
180M
  } else {
802
    // Perhaps it is near the file point.
803
180M
    I = LocalSLocEntryTable.begin()+LastFileIDLookup.ID;
804
180M
  }
805
806
  // Find the FileID that contains this.  "I" is an iterator that points to a
807
  // FileID whose offset is known to be larger than SLocOffset.
808
336M
  unsigned NumProbes = 0;
809
1.59G
  while (true) {
810
1.59G
    --I;
811
1.59G
    if (I->getOffset() <= SLocOffset) {
812
187M
      FileID Res = FileID::get(int(I - LocalSLocEntryTable.begin()));
813
      // Remember it.  We have good locality across FileID lookups.
814
187M
      LastFileIDLookup = Res;
815
187M
      NumLinearScans += NumProbes+1;
816
187M
      return Res;
817
187M
    }
818
1.41G
    if (++NumProbes == 8)
819
148M
      break;
820
1.41G
  }
821
822
  // Convert "I" back into an index.  We know that it is an entry whose index is
823
  // larger than the offset we are looking for.
824
148M
  unsigned GreaterIndex = I - LocalSLocEntryTable.begin();
825
  // LessIndex - This is the lower bound of the range that we're searching.
826
  // We know that the offset corresponding to the FileID is is less than
827
  // SLocOffset.
828
148M
  unsigned LessIndex = 0;
829
148M
  NumProbes = 0;
830
1.90G
  while (true) {
831
1.90G
    unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex;
832
1.90G
    SourceLocation::UIntTy MidOffset =
833
1.90G
        getLocalSLocEntry(MiddleIndex).getOffset();
834
835
1.90G
    ++NumProbes;
836
837
    // If the offset of the midpoint is too large, chop the high side of the
838
    // range to the midpoint.
839
1.90G
    if (MidOffset > SLocOffset) {
840
1.05G
      GreaterIndex = MiddleIndex;
841
1.05G
      continue;
842
1.05G
    }
843
844
    // If the middle index contains the value, succeed and return.
845
850M
    if (MiddleIndex + 1 == LocalSLocEntryTable.size() ||
846
850M
        SLocOffset < getLocalSLocEntry(MiddleIndex + 1).getOffset()) {
847
148M
      FileID Res = FileID::get(MiddleIndex);
848
849
      // Remember it.  We have good locality across FileID lookups.
850
148M
      LastFileIDLookup = Res;
851
148M
      NumBinaryProbes += NumProbes;
852
148M
      return Res;
853
148M
    }
854
855
    // Otherwise, move the low-side up to the middle index.
856
701M
    LessIndex = MiddleIndex;
857
701M
  }
858
148M
}
859
860
/// Return the FileID for a SourceLocation with a high offset.
861
///
862
/// This function knows that the SourceLocation is in a loaded buffer, not a
863
/// local one.
864
19.9M
FileID SourceManager::getFileIDLoaded(SourceLocation::UIntTy SLocOffset) const {
865
19.9M
  if (SLocOffset < CurrentLoadedOffset) {
866
0
    assert(0 && "Invalid SLocOffset or bad function choice");
867
0
    return FileID();
868
0
  }
869
870
  // Essentially the same as the local case, but the loaded array is sorted
871
  // in the other direction.
872
873
  // First do a linear scan from the last lookup position, if possible.
874
19.9M
  unsigned I;
875
19.9M
  int LastID = LastFileIDLookup.ID;
876
19.9M
  if (LastID >= 0 || 
getLoadedSLocEntryByID(LastID).getOffset() < SLocOffset18.7M
)
877
9.39M
    I = 0;
878
10.5M
  else
879
10.5M
    I = (-LastID - 2) + 1;
880
881
19.9M
  unsigned NumProbes;
882
146M
  for (NumProbes = 0; NumProbes < 8; 
++NumProbes, ++I126M
) {
883
    // Make sure the entry is loaded!
884
131M
    const SrcMgr::SLocEntry &E = getLoadedSLocEntry(I);
885
131M
    if (E.getOffset() <= SLocOffset) {
886
4.35M
      FileID Res = FileID::get(-int(I) - 2);
887
4.35M
      LastFileIDLookup = Res;
888
4.35M
      NumLinearScans += NumProbes + 1;
889
4.35M
      return Res;
890
4.35M
    }
891
131M
  }
892
893
  // Linear scan failed. Do the binary search. Note the reverse sorting of the
894
  // table: GreaterIndex is the one where the offset is greater, which is
895
  // actually a lower index!
896
15.6M
  unsigned GreaterIndex = I;
897
15.6M
  unsigned LessIndex = LoadedSLocEntryTable.size();
898
15.6M
  NumProbes = 0;
899
234M
  while (true) {
900
234M
    ++NumProbes;
901
234M
    unsigned MiddleIndex = (LessIndex - GreaterIndex) / 2 + GreaterIndex;
902
234M
    const SrcMgr::SLocEntry &E = getLoadedSLocEntry(MiddleIndex);
903
234M
    if (E.getOffset() == 0)
904
0
      return FileID(); // invalid entry.
905
906
234M
    ++NumProbes;
907
908
234M
    if (E.getOffset() > SLocOffset) {
909
100M
      if (GreaterIndex == MiddleIndex) {
910
0
        assert(0 && "binary search missed the entry");
911
0
        return FileID();
912
0
      }
913
100M
      GreaterIndex = MiddleIndex;
914
100M
      continue;
915
100M
    }
916
917
133M
    if (isOffsetInFileID(FileID::get(-int(MiddleIndex) - 2), SLocOffset)) {
918
15.6M
      FileID Res = FileID::get(-int(MiddleIndex) - 2);
919
15.6M
      LastFileIDLookup = Res;
920
15.6M
      NumBinaryProbes += NumProbes;
921
15.6M
      return Res;
922
15.6M
    }
923
924
117M
    if (LessIndex == MiddleIndex) {
925
0
      assert(0 && "binary search missed the entry");
926
0
      return FileID();
927
0
    }
928
117M
    LessIndex = MiddleIndex;
929
117M
  }
930
15.6M
}
931
932
SourceLocation SourceManager::
933
8.44M
getExpansionLocSlowCase(SourceLocation Loc) const {
934
14.3M
  do {
935
    // Note: If Loc indicates an offset into a token that came from a macro
936
    // expansion (e.g. the 5th character of the token) we do not want to add
937
    // this offset when going to the expansion location.  The expansion
938
    // location is the macro invocation, which the offset has nothing to do
939
    // with.  This is unlike when we get the spelling loc, because the offset
940
    // directly correspond to the token whose spelling we're inspecting.
941
14.3M
    Loc = getSLocEntry(getFileID(Loc)).getExpansion().getExpansionLocStart();
942
14.3M
  } while (!Loc.isFileID());
943
944
8.44M
  return Loc;
945
8.44M
}
946
947
80.3M
SourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const {
948
80.4M
  do {
949
80.4M
    std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
950
80.4M
    Loc = getSLocEntry(LocInfo.first).getExpansion().getSpellingLoc();
951
80.4M
    Loc = Loc.getLocWithOffset(LocInfo.second);
952
80.4M
  } while (!Loc.isFileID());
953
80.3M
  return Loc;
954
80.3M
}
955
956
26.1k
SourceLocation SourceManager::getFileLocSlowCase(SourceLocation Loc) const {
957
120k
  do {
958
120k
    if (isMacroArgExpansion(Loc))
959
21.4k
      Loc = getImmediateSpellingLoc(Loc);
960
99.4k
    else
961
99.4k
      Loc = getImmediateExpansionRange(Loc).getBegin();
962
120k
  } while (!Loc.isFileID());
963
26.1k
  return Loc;
964
26.1k
}
965
966
967
std::pair<FileID, unsigned>
968
SourceManager::getDecomposedExpansionLocSlowCase(
969
3.89M
                                             const SrcMgr::SLocEntry *E) const {
970
  // If this is an expansion record, walk through all the expansion points.
971
3.89M
  FileID FID;
972
3.89M
  SourceLocation Loc;
973
3.89M
  unsigned Offset;
974
8.58M
  do {
975
8.58M
    Loc = E->getExpansion().getExpansionLocStart();
976
977
8.58M
    FID = getFileID(Loc);
978
8.58M
    E = &getSLocEntry(FID);
979
8.58M
    Offset = Loc.getOffset()-E->getOffset();
980
8.58M
  } while (!Loc.isFileID());
981
982
3.89M
  return std::make_pair(FID, Offset);
983
3.89M
}
984
985
std::pair<FileID, unsigned>
986
SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
987
708k
                                                unsigned Offset) const {
988
  // If this is an expansion record, walk through all the expansion points.
989
708k
  FileID FID;
990
708k
  SourceLocation Loc;
991
796k
  do {
992
796k
    Loc = E->getExpansion().getSpellingLoc();
993
796k
    Loc = Loc.getLocWithOffset(Offset);
994
995
796k
    FID = getFileID(Loc);
996
796k
    E = &getSLocEntry(FID);
997
796k
    Offset = Loc.getOffset()-E->getOffset();
998
796k
  } while (!Loc.isFileID());
999
1000
708k
  return std::make_pair(FID, Offset);
1001
708k
}
1002
1003
/// getImmediateSpellingLoc - Given a SourceLocation object, return the
1004
/// spelling location referenced by the ID.  This is the first level down
1005
/// towards the place where the characters that make up the lexed token can be
1006
/// found.  This should not generally be used by clients.
1007
37.2k
SourceLocation SourceManager::getImmediateSpellingLoc(SourceLocation Loc) const{
1008
37.2k
  if (Loc.isFileID()) 
return Loc0
;
1009
37.2k
  std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
1010
37.2k
  Loc = getSLocEntry(LocInfo.first).getExpansion().getSpellingLoc();
1011
37.2k
  return Loc.getLocWithOffset(LocInfo.second);
1012
37.2k
}
1013
1014
/// Return the filename of the file containing a SourceLocation.
1015
11.2k
StringRef SourceManager::getFilename(SourceLocation SpellingLoc) const {
1016
11.2k
  if (const FileEntry *F = getFileEntryForID(getFileID(SpellingLoc)))
1017
11.1k
    return F->getName();
1018
156
  return StringRef();
1019
11.2k
}
1020
1021
/// getImmediateExpansionRange - Loc is required to be an expansion location.
1022
/// Return the start/end of the expansion information.
1023
CharSourceRange
1024
19.4M
SourceManager::getImmediateExpansionRange(SourceLocation Loc) const {
1025
19.4M
  assert(Loc.isMacroID() && "Not a macro expansion loc!");
1026
0
  const ExpansionInfo &Expansion = getSLocEntry(getFileID(Loc)).getExpansion();
1027
19.4M
  return Expansion.getExpansionLocRange();
1028
19.4M
}
1029
1030
5.45k
SourceLocation SourceManager::getTopMacroCallerLoc(SourceLocation Loc) const {
1031
5.61k
  while (isMacroArgExpansion(Loc))
1032
160
    Loc = getImmediateSpellingLoc(Loc);
1033
5.45k
  return Loc;
1034
5.45k
}
1035
1036
/// getExpansionRange - Given a SourceLocation object, return the range of
1037
/// tokens covered by the expansion in the ultimate file.
1038
3.90M
CharSourceRange SourceManager::getExpansionRange(SourceLocation Loc) const {
1039
3.90M
  if (Loc.isFileID())
1040
8.74k
    return CharSourceRange(SourceRange(Loc, Loc), true);
1041
1042
3.89M
  CharSourceRange Res = getImmediateExpansionRange(Loc);
1043
1044
  // Fully resolve the start and end locations to their ultimate expansion
1045
  // points.
1046
7.71M
  while (!Res.getBegin().isFileID())
1047
3.81M
    Res.setBegin(getImmediateExpansionRange(Res.getBegin()).getBegin());
1048
6.84M
  while (!Res.getEnd().isFileID()) {
1049
2.95M
    CharSourceRange EndRange = getImmediateExpansionRange(Res.getEnd());
1050
2.95M
    Res.setEnd(EndRange.getEnd());
1051
2.95M
    Res.setTokenRange(EndRange.isTokenRange());
1052
2.95M
  }
1053
3.89M
  return Res;
1054
3.90M
}
1055
1056
bool SourceManager::isMacroArgExpansion(SourceLocation Loc,
1057
306k
                                        SourceLocation *StartLoc) const {
1058
306k
  if (!Loc.isMacroID()) 
return false145k
;
1059
1060
160k
  FileID FID = getFileID(Loc);
1061
160k
  const SrcMgr::ExpansionInfo &Expansion = getSLocEntry(FID).getExpansion();
1062
160k
  if (!Expansion.isMacroArgExpansion()) 
return false124k
;
1063
1064
36.7k
  if (StartLoc)
1065
236
    *StartLoc = Expansion.getExpansionLocStart();
1066
36.7k
  return true;
1067
160k
}
1068
1069
2.48M
bool SourceManager::isMacroBodyExpansion(SourceLocation Loc) const {
1070
2.48M
  if (!Loc.isMacroID()) 
return false2.25M
;
1071
1072
225k
  FileID FID = getFileID(Loc);
1073
225k
  const SrcMgr::ExpansionInfo &Expansion = getSLocEntry(FID).getExpansion();
1074
225k
  return Expansion.isMacroBodyExpansion();
1075
2.48M
}
1076
1077
bool SourceManager::isAtStartOfImmediateMacroExpansion(SourceLocation Loc,
1078
50.3M
                                             SourceLocation *MacroBegin) const {
1079
50.3M
  assert(Loc.isValid() && Loc.isMacroID() && "Expected a valid macro loc");
1080
1081
0
  std::pair<FileID, unsigned> DecompLoc = getDecomposedLoc(Loc);
1082
50.3M
  if (DecompLoc.second > 0)
1083
25.8M
    return false; // Does not point at the start of expansion range.
1084
1085
24.4M
  bool Invalid = false;
1086
24.4M
  const SrcMgr::ExpansionInfo &ExpInfo =
1087
24.4M
      getSLocEntry(DecompLoc.first, &Invalid).getExpansion();
1088
24.4M
  if (Invalid)
1089
0
    return false;
1090
24.4M
  SourceLocation ExpLoc = ExpInfo.getExpansionLocStart();
1091
1092
24.4M
  if (ExpInfo.isMacroArgExpansion()) {
1093
    // For macro argument expansions, check if the previous FileID is part of
1094
    // the same argument expansion, in which case this Loc is not at the
1095
    // beginning of the expansion.
1096
2.24M
    FileID PrevFID = getPreviousFileID(DecompLoc.first);
1097
2.24M
    if (!PrevFID.isInvalid()) {
1098
2.24M
      const SrcMgr::SLocEntry &PrevEntry = getSLocEntry(PrevFID, &Invalid);
1099
2.24M
      if (Invalid)
1100
0
        return false;
1101
2.24M
      if (PrevEntry.isExpansion() &&
1102
2.24M
          PrevEntry.getExpansion().getExpansionLocStart() == ExpLoc)
1103
4.39k
        return false;
1104
2.24M
    }
1105
2.24M
  }
1106
1107
24.4M
  if (MacroBegin)
1108
24.4M
    *MacroBegin = ExpLoc;
1109
24.4M
  return true;
1110
24.4M
}
1111
1112
bool SourceManager::isAtEndOfImmediateMacroExpansion(SourceLocation Loc,
1113
45.8M
                                               SourceLocation *MacroEnd) const {
1114
45.8M
  assert(Loc.isValid() && Loc.isMacroID() && "Expected a valid macro loc");
1115
1116
0
  FileID FID = getFileID(Loc);
1117
45.8M
  SourceLocation NextLoc = Loc.getLocWithOffset(1);
1118
45.8M
  if (isInFileID(NextLoc, FID))
1119
4.29M
    return false; // Does not point at the end of expansion range.
1120
1121
41.6M
  bool Invalid = false;
1122
41.6M
  const SrcMgr::ExpansionInfo &ExpInfo =
1123
41.6M
      getSLocEntry(FID, &Invalid).getExpansion();
1124
41.6M
  if (Invalid)
1125
0
    return false;
1126
1127
41.6M
  if (ExpInfo.isMacroArgExpansion()) {
1128
    // For macro argument expansions, check if the next FileID is part of the
1129
    // same argument expansion, in which case this Loc is not at the end of the
1130
    // expansion.
1131
47.6k
    FileID NextFID = getNextFileID(FID);
1132
47.6k
    if (!NextFID.isInvalid()) {
1133
47.5k
      const SrcMgr::SLocEntry &NextEntry = getSLocEntry(NextFID, &Invalid);
1134
47.5k
      if (Invalid)
1135
0
        return false;
1136
47.5k
      if (NextEntry.isExpansion() &&
1137
47.5k
          NextEntry.getExpansion().getExpansionLocStart() ==
1138
47.5k
              ExpInfo.getExpansionLocStart())
1139
47.4k
        return false;
1140
47.5k
    }
1141
47.6k
  }
1142
1143
41.5M
  if (MacroEnd)
1144
41.5M
    *MacroEnd = ExpInfo.getExpansionLocEnd();
1145
41.5M
  return true;
1146
41.6M
}
1147
1148
//===----------------------------------------------------------------------===//
1149
// Queries about the code at a SourceLocation.
1150
//===----------------------------------------------------------------------===//
1151
1152
/// getCharacterData - Return a pointer to the start of the specified location
1153
/// in the appropriate MemoryBuffer.
1154
const char *SourceManager::getCharacterData(SourceLocation SL,
1155
52.9M
                                            bool *Invalid) const {
1156
  // Note that this is a hot function in the getSpelling() path, which is
1157
  // heavily used by -E mode.
1158
52.9M
  std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL);
1159
1160
  // Note that calling 'getBuffer()' may lazily page in a source file.
1161
52.9M
  bool CharDataInvalid = false;
1162
52.9M
  const SLocEntry &Entry = getSLocEntry(LocInfo.first, &CharDataInvalid);
1163
52.9M
  if (CharDataInvalid || 
!Entry.isFile()52.9M
) {
1164
4
    if (Invalid)
1165
0
      *Invalid = true;
1166
1167
4
    return "<<<<INVALID BUFFER>>>>";
1168
4
  }
1169
52.9M
  llvm::Optional<llvm::MemoryBufferRef> Buffer =
1170
52.9M
      Entry.getFile().getContentCache().getBufferOrNone(Diag, getFileManager(),
1171
52.9M
                                                        SourceLocation());
1172
52.9M
  if (Invalid)
1173
6.68M
    *Invalid = !Buffer;
1174
52.9M
  return Buffer ? Buffer->getBufferStart() + LocInfo.second
1175
52.9M
                : 
"<<<<INVALID BUFFER>>>>"0
;
1176
52.9M
}
1177
1178
/// getColumnNumber - Return the column # for the specified file position.
1179
/// this is significantly cheaper to compute than the line number.
1180
unsigned SourceManager::getColumnNumber(FileID FID, unsigned FilePos,
1181
96.0M
                                        bool *Invalid) const {
1182
96.0M
  llvm::Optional<llvm::MemoryBufferRef> MemBuf = getBufferOrNone(FID);
1183
96.0M
  if (Invalid)
1184
95.6M
    *Invalid = !MemBuf;
1185
1186
96.0M
  if (!MemBuf)
1187
61
    return 1;
1188
1189
  // It is okay to request a position just past the end of the buffer.
1190
96.0M
  if (FilePos > MemBuf->getBufferSize()) {
1191
1
    if (Invalid)
1192
1
      *Invalid = true;
1193
1
    return 1;
1194
1
  }
1195
1196
96.0M
  const char *Buf = MemBuf->getBufferStart();
1197
  // See if we just calculated the line number for this FilePos and can use
1198
  // that to lookup the start of the line instead of searching for it.
1199
96.0M
  if (LastLineNoFileIDQuery == FID && 
LastLineNoContentCache->SourceLineCache96.0M
&&
1200
96.0M
      
LastLineNoResult < LastLineNoContentCache->SourceLineCache.size()96.0M
) {
1201
95.8M
    const unsigned *SourceLineCache =
1202
95.8M
        LastLineNoContentCache->SourceLineCache.begin();
1203
95.8M
    unsigned LineStart = SourceLineCache[LastLineNoResult - 1];
1204
95.8M
    unsigned LineEnd = SourceLineCache[LastLineNoResult];
1205
95.8M
    if (FilePos >= LineStart && 
FilePos < LineEnd95.8M
) {
1206
      // LineEnd is the LineStart of the next line.
1207
      // A line ends with separator LF or CR+LF on Windows.
1208
      // FilePos might point to the last separator,
1209
      // but we need a column number at most 1 + the last column.
1210
95.8M
      if (FilePos + 1 == LineEnd && 
FilePos > LineStart370k
) {
1211
364k
        if (Buf[FilePos - 1] == '\r' || Buf[FilePos - 1] == '\n')
1212
0
          --FilePos;
1213
364k
      }
1214
95.8M
      return FilePos - LineStart + 1;
1215
95.8M
    }
1216
95.8M
  }
1217
1218
189k
  unsigned LineStart = FilePos;
1219
34.7M
  while (LineStart && 
Buf[LineStart-1] != '\n'34.5M
&&
Buf[LineStart-1] != '\r'34.5M
)
1220
34.5M
    --LineStart;
1221
189k
  return FilePos-LineStart+1;
1222
96.0M
}
1223
1224
// isInvalid - Return the result of calling loc.isInvalid(), and
1225
// if Invalid is not null, set its value to same.
1226
template<typename LocType>
1227
17.1M
static bool isInvalid(LocType Loc, bool *Invalid) {
1228
17.1M
  bool MyInvalid = Loc.isInvalid();
1229
17.1M
  if (Invalid)
1230
294k
    *Invalid = MyInvalid;
1231
17.1M
  return MyInvalid;
1232
17.1M
}
SourceManager.cpp:bool isInvalid<clang::SourceLocation>(clang::SourceLocation, bool*)
Line
Count
Source
1227
14.5M
static bool isInvalid(LocType Loc, bool *Invalid) {
1228
14.5M
  bool MyInvalid = Loc.isInvalid();
1229
14.5M
  if (Invalid)
1230
292k
    *Invalid = MyInvalid;
1231
14.5M
  return MyInvalid;
1232
14.5M
}
SourceManager.cpp:bool isInvalid<clang::PresumedLoc>(clang::PresumedLoc, bool*)
Line
Count
Source
1227
2.56M
static bool isInvalid(LocType Loc, bool *Invalid) {
1228
2.56M
  bool MyInvalid = Loc.isInvalid();
1229
2.56M
  if (Invalid)
1230
1.99k
    *Invalid = MyInvalid;
1231
2.56M
  return MyInvalid;
1232
2.56M
}
1233
1234
unsigned SourceManager::getSpellingColumnNumber(SourceLocation Loc,
1235
162k
                                                bool *Invalid) const {
1236
162k
  if (isInvalid(Loc, Invalid)) 
return 0224
;
1237
161k
  std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
1238
161k
  return getColumnNumber(LocInfo.first, LocInfo.second, Invalid);
1239
162k
}
1240
1241
unsigned SourceManager::getExpansionColumnNumber(SourceLocation Loc,
1242
90.1k
                                                 bool *Invalid) const {
1243
90.1k
  if (isInvalid(Loc, Invalid)) 
return 00
;
1244
90.1k
  std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc);
1245
90.1k
  return getColumnNumber(LocInfo.first, LocInfo.second, Invalid);
1246
90.1k
}
1247
1248
unsigned SourceManager::getPresumedColumnNumber(SourceLocation Loc,
1249
932
                                                bool *Invalid) const {
1250
932
  PresumedLoc PLoc = getPresumedLoc(Loc);
1251
932
  if (isInvalid(PLoc, Invalid)) 
return 00
;
1252
932
  return PLoc.getColumn();
1253
932
}
1254
1255
// Check if mutli-byte word x has bytes between m and n, included. This may also
1256
// catch bytes equal to n + 1.
1257
// The returned value holds a 0x80 at each byte position that holds a match.
1258
// see http://graphics.stanford.edu/~seander/bithacks.html#HasBetweenInWord
1259
template <class T>
1260
static constexpr inline T likelyhasbetween(T x, unsigned char m,
1261
1.42G
                                           unsigned char n) {
1262
1.42G
  return ((x - ~static_cast<T>(0) / 255 * (n + 1)) & ~x &
1263
1.42G
          ((x & ~static_cast<T>(0) / 255 * 127) +
1264
1.42G
           (~static_cast<T>(0) / 255 * (127 - (m - 1))))) &
1265
1.42G
         ~static_cast<T>(0) / 255 * 128;
1266
1.42G
}
1267
1268
LineOffsetMapping LineOffsetMapping::get(llvm::MemoryBufferRef Buffer,
1269
385k
                                         llvm::BumpPtrAllocator &Alloc) {
1270
1271
  // Find the file offsets of all of the *physical* source lines.  This does
1272
  // not look at trigraphs, escaped newlines, or anything else tricky.
1273
385k
  SmallVector<unsigned, 256> LineOffsets;
1274
1275
  // Line #1 starts at char 0.
1276
385k
  LineOffsets.push_back(0);
1277
1278
385k
  const unsigned char *Buf = (const unsigned char *)Buffer.getBufferStart();
1279
385k
  const unsigned char *End = (const unsigned char *)Buffer.getBufferEnd();
1280
385k
  const std::size_t BufLen = End - Buf;
1281
1282
385k
  unsigned I = 0;
1283
385k
  uint64_t Word;
1284
1285
  // scan sizeof(Word) bytes at a time for new lines.
1286
  // This is much faster than scanning each byte independently.
1287
385k
  if (BufLen > sizeof(Word)) {
1288
1.42G
    do {
1289
1.42G
      Word = llvm::support::endian::read64(Buf + I, llvm::support::little);
1290
      // no new line => jump over sizeof(Word) bytes.
1291
1.42G
      auto Mask = likelyhasbetween(Word, '\n', '\r');
1292
1.42G
      if (!Mask) {
1293
1.20G
        I += sizeof(Word);
1294
1.20G
        continue;
1295
1.20G
      }
1296
1297
      // At that point, Mask contains 0x80 set at each byte that holds a value
1298
      // in [\n, \r + 1 [
1299
1300
      // Scan for the next newline - it's very likely there's one.
1301
219M
      unsigned N =
1302
219M
          llvm::countTrailingZeros(Mask) - 7; // -7 because 0x80 is the marker
1303
219M
      Word >>= N;
1304
219M
      I += N / 8 + 1;
1305
219M
      unsigned char Byte = Word;
1306
219M
      if (
Byte == '\n'219M
) {
1307
219M
        LineOffsets.push_back(I);
1308
18.4E
      } else if (Byte == '\r') {
1309
        // If this is \r\n, skip both characters.
1310
1.67k
        if (Buf[I] == '\n')
1311
1.67k
          ++I;
1312
1.67k
        LineOffsets.push_back(I);
1313
1.67k
      }
1314
1.42G
    } while (I < BufLen - sizeof(Word) - 1);
1315
380k
  }
1316
1317
  // Handle tail using a regular check.
1318
2.37M
  while (I < BufLen) {
1319
1.99M
    if (Buf[I] == '\n') {
1320
407k
      LineOffsets.push_back(I + 1);
1321
1.58M
    } else if (Buf[I] == '\r') {
1322
      // If this is \r\n, skip both characters.
1323
34
      if (I + 1 < BufLen && Buf[I + 1] == '\n')
1324
34
        ++I;
1325
34
      LineOffsets.push_back(I + 1);
1326
34
    }
1327
1.99M
    ++I;
1328
1.99M
  }
1329
1330
385k
  return LineOffsetMapping(LineOffsets, Alloc);
1331
385k
}
1332
1333
LineOffsetMapping::LineOffsetMapping(ArrayRef<unsigned> LineOffsets,
1334
                                     llvm::BumpPtrAllocator &Alloc)
1335
385k
    : Storage(Alloc.Allocate<unsigned>(LineOffsets.size() + 1)) {
1336
385k
  Storage[0] = LineOffsets.size();
1337
385k
  std::copy(LineOffsets.begin(), LineOffsets.end(), Storage + 1);
1338
385k
}
1339
1340
/// getLineNumber - Given a SourceLocation, return the spelling line number
1341
/// for the position indicated.  This requires building and caching a table of
1342
/// line offsets for the MemoryBuffer, so this is not cheap: use only when
1343
/// about to emit a diagnostic.
1344
unsigned SourceManager::getLineNumber(FileID FID, unsigned FilePos,
1345
137M
                                      bool *Invalid) const {
1346
137M
  if (FID.isInvalid()) {
1347
50
    if (Invalid)
1348
0
      *Invalid = true;
1349
50
    return 1;
1350
50
  }
1351
1352
137M
  const ContentCache *Content;
1353
137M
  if (LastLineNoFileIDQuery == FID)
1354
132M
    Content = LastLineNoContentCache;
1355
4.75M
  else {
1356
4.75M
    bool MyInvalid = false;
1357
4.75M
    const SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
1358
4.75M
    if (MyInvalid || !Entry.isFile()) {
1359
9
      if (Invalid)
1360
0
        *Invalid = true;
1361
9
      return 1;
1362
9
    }
1363
1364
4.75M
    Content = &Entry.getFile().getContentCache();
1365
4.75M
  }
1366
1367
  // If this is the first use of line information for this buffer, compute the
1368
  /// SourceLineCache for it on demand.
1369
137M
  if (!Content->SourceLineCache) {
1370
384k
    llvm::Optional<llvm::MemoryBufferRef> Buffer =
1371
384k
        Content->getBufferOrNone(Diag, getFileManager(), SourceLocation());
1372
384k
    if (Invalid)
1373
366k
      *Invalid = !Buffer;
1374
384k
    if (!Buffer)
1375
0
      return 1;
1376
1377
384k
    Content->SourceLineCache =
1378
384k
        LineOffsetMapping::get(*Buffer, ContentCacheAlloc);
1379
137M
  } else if (Invalid)
1380
95.1M
    *Invalid = false;
1381
1382
  // Okay, we know we have a line number table.  Do a binary search to find the
1383
  // line number that this character position lands on.
1384
137M
  const unsigned *SourceLineCache = Content->SourceLineCache.begin();
1385
137M
  const unsigned *SourceLineCacheStart = SourceLineCache;
1386
137M
  const unsigned *SourceLineCacheEnd = Content->SourceLineCache.end();
1387
1388
137M
  unsigned QueriedFilePos = FilePos+1;
1389
1390
  // FIXME: I would like to be convinced that this code is worth being as
1391
  // complicated as it is, binary search isn't that slow.
1392
  //
1393
  // If it is worth being optimized, then in my opinion it could be more
1394
  // performant, simpler, and more obviously correct by just "galloping" outward
1395
  // from the queried file position. In fact, this could be incorporated into a
1396
  // generic algorithm such as lower_bound_with_hint.
1397
  //
1398
  // If someone gives me a test case where this matters, and I will do it! - DWD
1399
1400
  // If the previous query was to the same file, we know both the file pos from
1401
  // that query and the line number returned.  This allows us to narrow the
1402
  // search space from the entire file to something near the match.
1403
137M
  if (LastLineNoFileIDQuery == FID) {
1404
132M
    if (QueriedFilePos >= LastLineNoFilePos) {
1405
      // FIXME: Potential overflow?
1406
73.8M
      SourceLineCache = SourceLineCache+LastLineNoResult-1;
1407
1408
      // The query is likely to be nearby the previous one.  Here we check to
1409
      // see if it is within 5, 10 or 20 lines.  It can be far away in cases
1410
      // where big comment blocks and vertical whitespace eat up lines but
1411
      // contribute no tokens.
1412
73.8M
      if (SourceLineCache+5 < SourceLineCacheEnd) {
1413
73.0M
        if (SourceLineCache[5] > QueriedFilePos)
1414
18.1M
          SourceLineCacheEnd = SourceLineCache+5;
1415
54.8M
        else if (SourceLineCache+10 < SourceLineCacheEnd) {
1416
54.8M
          if (SourceLineCache[10] > QueriedFilePos)
1417
992k
            SourceLineCacheEnd = SourceLineCache+10;
1418
53.8M
          else if (SourceLineCache+20 < SourceLineCacheEnd) {
1419
53.8M
            if (SourceLineCache[20] > QueriedFilePos)
1420
821k
              SourceLineCacheEnd = SourceLineCache+20;
1421
53.8M
          }
1422
54.8M
        }
1423
73.0M
      }
1424
73.8M
    } else {
1425
59.0M
      if (LastLineNoResult < Content->SourceLineCache.size())
1426
58.9M
        SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1;
1427
59.0M
    }
1428
132M
  }
1429
1430
137M
  const unsigned *Pos =
1431
137M
      std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos);
1432
137M
  unsigned LineNo = Pos-SourceLineCacheStart;
1433
1434
137M
  LastLineNoFileIDQuery = FID;
1435
137M
  LastLineNoContentCache = Content;
1436
137M
  LastLineNoFilePos = QueriedFilePos;
1437
137M
  LastLineNoResult = LineNo;
1438
137M
  return LineNo;
1439
137M
}
1440
1441
unsigned SourceManager::getSpellingLineNumber(SourceLocation Loc,
1442
171k
                                              bool *Invalid) const {
1443
171k
  if (isInvalid(Loc, Invalid)) 
return 01.46k
;
1444
170k
  std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
1445
170k
  return getLineNumber(LocInfo.first, LocInfo.second);
1446
171k
}
1447
unsigned SourceManager::getExpansionLineNumber(SourceLocation Loc,
1448
6.04M
                                               bool *Invalid) const {
1449
6.04M
  if (isInvalid(Loc, Invalid)) 
return 00
;
1450
6.04M
  std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc);
1451
6.04M
  return getLineNumber(LocInfo.first, LocInfo.second);
1452
6.04M
}
1453
unsigned SourceManager::getPresumedLineNumber(SourceLocation Loc,
1454
2.56M
                                              bool *Invalid) const {
1455
2.56M
  PresumedLoc PLoc = getPresumedLoc(Loc);
1456
2.56M
  if (isInvalid(PLoc, Invalid)) 
return 0458
;
1457
2.56M
  return PLoc.getLine();
1458
2.56M
}
1459
1460
/// getFileCharacteristic - return the file characteristic of the specified
1461
/// source location, indicating whether this is a normal file, a system
1462
/// header, or an "implicit extern C" system header.
1463
///
1464
/// This state can be modified with flags on GNU linemarker directives like:
1465
///   # 4 "foo.h" 3
1466
/// which changes all source locations in the current file after that to be
1467
/// considered to be from a system header.
1468
SrcMgr::CharacteristicKind
1469
205M
SourceManager::getFileCharacteristic(SourceLocation Loc) const {
1470
205M
  assert(Loc.isValid() && "Can't get file characteristic of invalid loc!");
1471
0
  std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc);
1472
205M
  const SLocEntry *SEntry = getSLocEntryForFile(LocInfo.first);
1473
205M
  if (!SEntry)
1474
6
    return C_User;
1475
1476
205M
  const SrcMgr::FileInfo &FI = SEntry->getFile();
1477
1478
  // If there are no #line directives in this file, just return the whole-file
1479
  // state.
1480
205M
  if (!FI.hasLineDirectives())
1481
153M
    return FI.getFileCharacteristic();
1482
1483
51.1M
  assert(LineTable && "Can't have linetable entries without a LineTable!");
1484
  // See if there is a #line directive before the location.
1485
0
  const LineEntry *Entry =
1486
51.1M
    LineTable->FindNearestLineEntry(LocInfo.first, LocInfo.second);
1487
1488
  // If this is before the first line marker, use the file characteristic.
1489
51.1M
  if (!Entry)
1490
1.39M
    return FI.getFileCharacteristic();
1491
1492
49.7M
  return Entry->FileKind;
1493
51.1M
}
1494
1495
/// Return the filename or buffer identifier of the buffer the location is in.
1496
/// Note that this name does not respect \#line directives.  Use getPresumedLoc
1497
/// for normal clients.
1498
StringRef SourceManager::getBufferName(SourceLocation Loc,
1499
8.08M
                                       bool *Invalid) const {
1500
8.08M
  if (isInvalid(Loc, Invalid)) 
return "<invalid loc>"1.26k
;
1501
1502
8.08M
  auto B = getBufferOrNone(getFileID(Loc));
1503
8.08M
  if (Invalid)
1504
0
    *Invalid = !B;
1505
8.08M
  return B ? 
B->getBufferIdentifier()8.08M
:
"<invalid buffer>"895
;
1506
8.08M
}
1507
1508
/// getPresumedLoc - This method returns the "presumed" location of a
1509
/// SourceLocation specifies.  A "presumed location" can be modified by \#line
1510
/// or GNU line marker directives.  This provides a view on the data that a
1511
/// user should see in diagnostics, for example.
1512
///
1513
/// Note that a presumed location is always given as the expansion point of an
1514
/// expansion location, not at the spelling location.
1515
PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc,
1516
95.5M
                                          bool UseLineDirectives) const {
1517
95.5M
  if (Loc.isInvalid()) 
return PresumedLoc()30.3k
;
1518
1519
  // Presumed locations are always for expansion points.
1520
95.4M
  std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc);
1521
1522
95.4M
  bool Invalid = false;
1523
95.4M
  const SLocEntry &Entry = getSLocEntry(LocInfo.first, &Invalid);
1524
95.4M
  if (Invalid || 
!Entry.isFile()95.4M
)
1525
4
    return PresumedLoc();
1526
1527
95.4M
  const SrcMgr::FileInfo &FI = Entry.getFile();
1528
95.4M
  const SrcMgr::ContentCache *C = &FI.getContentCache();
1529
1530
  // To get the source name, first consult the FileEntry (if one exists)
1531
  // before the MemBuffer as this will avoid unnecessarily paging in the
1532
  // MemBuffer.
1533
95.4M
  FileID FID = LocInfo.first;
1534
95.4M
  StringRef Filename;
1535
95.4M
  if (C->OrigEntry)
1536
95.2M
    Filename = C->OrigEntry->getName();
1537
198k
  else if (auto Buffer = C->getBufferOrNone(Diag, getFileManager()))
1538
198k
    Filename = Buffer->getBufferIdentifier();
1539
1540
95.4M
  unsigned LineNo = getLineNumber(LocInfo.first, LocInfo.second, &Invalid);
1541
95.4M
  if (Invalid)
1542
0
    return PresumedLoc();
1543
95.4M
  unsigned ColNo  = getColumnNumber(LocInfo.first, LocInfo.second, &Invalid);
1544
95.4M
  if (Invalid)
1545
0
    return PresumedLoc();
1546
1547
95.4M
  SourceLocation IncludeLoc = FI.getIncludeLoc();
1548
1549
  // If we have #line directives in this file, update and overwrite the physical
1550
  // location info if appropriate.
1551
95.4M
  if (UseLineDirectives && 
FI.hasLineDirectives()95.4M
) {
1552
35.5M
    assert(LineTable && "Can't have linetable entries without a LineTable!");
1553
    // See if there is a #line directive before this.  If so, get it.
1554
35.5M
    if (const LineEntry *Entry =
1555
35.5M
          LineTable->FindNearestLineEntry(LocInfo.first, LocInfo.second)) {
1556
      // If the LineEntry indicates a filename, use it.
1557
35.4M
      if (Entry->FilenameID != -1) {
1558
35.4M
        Filename = LineTable->getFilename(Entry->FilenameID);
1559
        // The contents of files referenced by #line are not in the
1560
        // SourceManager
1561
35.4M
        FID = FileID::get(0);
1562
35.4M
      }
1563
1564
      // Use the line number specified by the LineEntry.  This line number may
1565
      // be multiple lines down from the line entry.  Add the difference in
1566
      // physical line numbers from the query point and the line marker to the
1567
      // total.
1568
35.4M
      unsigned MarkerLineNo = getLineNumber(LocInfo.first, Entry->FileOffset);
1569
35.4M
      LineNo = Entry->LineNo + (LineNo-MarkerLineNo-1);
1570
1571
      // Note that column numbers are not molested by line markers.
1572
1573
      // Handle virtual #include manipulation.
1574
35.4M
      if (Entry->IncludeOffset) {
1575
89.9k
        IncludeLoc = getLocForStartOfFile(LocInfo.first);
1576
89.9k
        IncludeLoc = IncludeLoc.getLocWithOffset(Entry->IncludeOffset);
1577
89.9k
      }
1578
35.4M
    }
1579
35.5M
  }
1580
1581
0
  return PresumedLoc(Filename.data(), FID, LineNo, ColNo, IncludeLoc);
1582
95.4M
}
1583
1584
/// Returns whether the PresumedLoc for a given SourceLocation is
1585
/// in the main file.
1586
///
1587
/// This computes the "presumed" location for a SourceLocation, then checks
1588
/// whether it came from a file other than the main file. This is different
1589
/// from isWrittenInMainFile() because it takes line marker directives into
1590
/// account.
1591
70.4M
bool SourceManager::isInMainFile(SourceLocation Loc) const {
1592
70.4M
  if (Loc.isInvalid()) 
return false63
;
1593
1594
  // Presumed locations are always for expansion points.
1595
70.4M
  std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc);
1596
1597
70.4M
  const SLocEntry *Entry = getSLocEntryForFile(LocInfo.first);
1598
70.4M
  if (!Entry)
1599
2
    return false;
1600
1601
70.4M
  const SrcMgr::FileInfo &FI = Entry->getFile();
1602
1603
  // Check if there is a line directive for this location.
1604
70.4M
  if (FI.hasLineDirectives())
1605
36.1M
    if (const LineEntry *Entry =
1606
36.1M
            LineTable->FindNearestLineEntry(LocInfo.first, LocInfo.second))
1607
36.1M
      if (Entry->IncludeOffset)
1608
351k
        return false;
1609
1610
70.1M
  return FI.getIncludeLoc().isInvalid();
1611
70.4M
}
1612
1613
/// The size of the SLocEntry that \p FID represents.
1614
1.18M
unsigned SourceManager::getFileIDSize(FileID FID) const {
1615
1.18M
  bool Invalid = false;
1616
1.18M
  const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
1617
1.18M
  if (Invalid)
1618
0
    return 0;
1619
1620
1.18M
  int ID = FID.ID;
1621
1.18M
  SourceLocation::UIntTy NextOffset;
1622
1.18M
  if ((ID > 0 && 
unsigned(ID+1) == local_sloc_entry_size()88.9k
))
1623
63.8k
    NextOffset = getNextLocalOffset();
1624
1.11M
  else if (ID+1 == -1)
1625
7
    NextOffset = MaxLoadedOffset;
1626
1.11M
  else
1627
1.11M
    NextOffset = getSLocEntry(FileID::get(ID+1)).getOffset();
1628
1629
1.18M
  return NextOffset - Entry.getOffset() - 1;
1630
1.18M
}
1631
1632
//===----------------------------------------------------------------------===//
1633
// Other miscellaneous methods.
1634
//===----------------------------------------------------------------------===//
1635
1636
/// Get the source location for the given file:line:col triplet.
1637
///
1638
/// If the source file is included multiple times, the source location will
1639
/// be based upon an arbitrary inclusion.
1640
SourceLocation SourceManager::translateFileLineCol(const FileEntry *SourceFile,
1641
                                                  unsigned Line,
1642
9.71k
                                                  unsigned Col) const {
1643
9.71k
  assert(SourceFile && "Null source file!");
1644
0
  assert(Line && Col && "Line and column should start from 1!");
1645
1646
0
  FileID FirstFID = translateFile(SourceFile);
1647
9.71k
  return translateLineCol(FirstFID, Line, Col);
1648
9.71k
}
1649
1650
/// Get the FileID for the given file.
1651
///
1652
/// If the source file is included multiple times, the FileID will be the
1653
/// first inclusion.
1654
71.7k
FileID SourceManager::translateFile(const FileEntry *SourceFile) const {
1655
71.7k
  assert(SourceFile && "Null source file!");
1656
1657
  // First, check the main file ID, since it is common to look for a
1658
  // location in the main file.
1659
71.7k
  if (MainFileID.isValid()) {
1660
11.5k
    bool Invalid = false;
1661
11.5k
    const SLocEntry &MainSLoc = getSLocEntry(MainFileID, &Invalid);
1662
11.5k
    if (Invalid)
1663
0
      return FileID();
1664
1665
11.5k
    if (MainSLoc.isFile()) {
1666
11.5k
      if (MainSLoc.getFile().getContentCache().OrigEntry == SourceFile)
1667
9.71k
        return MainFileID;
1668
11.5k
    }
1669
11.5k
  }
1670
1671
  // The location we're looking for isn't in the main file; look
1672
  // through all of the local source locations.
1673
1.49M
  
for (unsigned I = 0, N = local_sloc_entry_size(); 62.0k
I != N;
++I1.43M
) {
1674
1.49M
    const SLocEntry &SLoc = getLocalSLocEntry(I);
1675
1.49M
    if (SLoc.isFile() &&
1676
1.49M
        
SLoc.getFile().getContentCache().OrigEntry == SourceFile113k
)
1677
60.7k
      return FileID::get(I);
1678
1.49M
  }
1679
1680
  // If that still didn't help, try the modules.
1681
65.2k
  
for (unsigned I = 0, N = loaded_sloc_entry_size(); 1.25k
I != N;
++I64.0k
) {
1682
65.1k
    const SLocEntry &SLoc = getLoadedSLocEntry(I);
1683
65.1k
    if (SLoc.isFile() &&
1684
65.1k
        
SLoc.getFile().getContentCache().OrigEntry == SourceFile4.75k
)
1685
1.12k
      return FileID::get(-int(I) - 2);
1686
65.1k
  }
1687
1688
123
  return FileID();
1689
1.25k
}
1690
1691
/// Get the source location in \arg FID for the given line:col.
1692
/// Returns null location if \arg FID is not a file SLocEntry.
1693
SourceLocation SourceManager::translateLineCol(FileID FID,
1694
                                               unsigned Line,
1695
94.7k
                                               unsigned Col) const {
1696
  // Lines are used as a one-based index into a zero-based array. This assert
1697
  // checks for possible buffer underruns.
1698
94.7k
  assert(Line && Col && "Line and column should start from 1!");
1699
1700
94.7k
  if (FID.isInvalid())
1701
0
    return SourceLocation();
1702
1703
94.7k
  bool Invalid = false;
1704
94.7k
  const SLocEntry &Entry = getSLocEntry(FID, &Invalid);
1705
94.7k
  if (Invalid)
1706
0
    return SourceLocation();
1707
1708
94.7k
  if (!Entry.isFile())
1709
0
    return SourceLocation();
1710
1711
94.7k
  SourceLocation FileLoc = SourceLocation::getFileLoc(Entry.getOffset());
1712
1713
94.7k
  if (Line == 1 && 
Col == 13.36k
)
1714
3.05k
    return FileLoc;
1715
1716
91.6k
  const ContentCache *Content = &Entry.getFile().getContentCache();
1717
1718
  // If this is the first use of line information for this buffer, compute the
1719
  // SourceLineCache for it on demand.
1720
91.6k
  llvm::Optional<llvm::MemoryBufferRef> Buffer =
1721
91.6k
      Content->getBufferOrNone(Diag, getFileManager());
1722
91.6k
  if (!Buffer)
1723
0
    return SourceLocation();
1724
91.6k
  if (!Content->SourceLineCache)
1725
577
    Content->SourceLineCache =
1726
577
        LineOffsetMapping::get(*Buffer, ContentCacheAlloc);
1727
1728
91.6k
  if (Line > Content->SourceLineCache.size()) {
1729
3
    unsigned Size = Buffer->getBufferSize();
1730
3
    if (Size > 0)
1731
3
      --Size;
1732
3
    return FileLoc.getLocWithOffset(Size);
1733
3
  }
1734
1735
91.6k
  unsigned FilePos = Content->SourceLineCache[Line - 1];
1736
91.6k
  const char *Buf = Buffer->getBufferStart() + FilePos;
1737
91.6k
  unsigned BufLength = Buffer->getBufferSize() - FilePos;
1738
91.6k
  if (BufLength == 0)
1739
29
    return FileLoc.getLocWithOffset(FilePos);
1740
1741
91.6k
  unsigned i = 0;
1742
1743
  // Check that the given column is valid.
1744
352k
  while (i < BufLength-1 && 
i < Col-1352k
&&
Buf[i] != '\n'260k
&&
Buf[i] != '\r'260k
)
1745
260k
    ++i;
1746
91.6k
  return FileLoc.getLocWithOffset(FilePos + i);
1747
91.6k
}
1748
1749
/// Compute a map of macro argument chunks to their expanded source
1750
/// location. Chunks that are not part of a macro argument will map to an
1751
/// invalid source location. e.g. if a file contains one macro argument at
1752
/// offset 100 with length 10, this is how the map will be formed:
1753
///     0   -> SourceLocation()
1754
///     100 -> Expanded macro arg location
1755
///     110 -> SourceLocation()
1756
void SourceManager::computeMacroArgsCache(MacroArgsMap &MacroArgsCache,
1757
274
                                          FileID FID) const {
1758
274
  assert(FID.isValid());
1759
1760
  // Initially no macro argument chunk is present.
1761
0
  MacroArgsCache.insert(std::make_pair(0, SourceLocation()));
1762
1763
274
  int ID = FID.ID;
1764
1.58k
  while (true) {
1765
1.58k
    ++ID;
1766
    // Stop if there are no more FileIDs to check.
1767
1.58k
    if (ID > 0) {
1768
1.43k
      if (unsigned(ID) >= local_sloc_entry_size())
1769
208
        return;
1770
1.43k
    } else 
if (145
ID == -1145
) {
1771
37
      return;
1772
37
    }
1773
1774
1.33k
    bool Invalid = false;
1775
1.33k
    const SrcMgr::SLocEntry &Entry = getSLocEntryByID(ID, &Invalid);
1776
1.33k
    if (Invalid)
1777
0
      return;
1778
1.33k
    if (Entry.isFile()) {
1779
402
      auto& File = Entry.getFile();
1780
402
      if (File.getFileCharacteristic() == C_User_ModuleMap ||
1781
402
          
File.getFileCharacteristic() == C_System_ModuleMap393
)
1782
9
        continue;
1783
1784
393
      SourceLocation IncludeLoc = File.getIncludeLoc();
1785
393
      bool IncludedInFID =
1786
393
          (IncludeLoc.isValid() && 
isInFileID(IncludeLoc, FID)106
) ||
1787
          // Predefined header doesn't have a valid include location in main
1788
          // file, but any files created by it should still be skipped when
1789
          // computing macro args expanded in the main file.
1790
393
          
(315
FID == MainFileID315
&&
Entry.getFile().getName() == "<built-in>"267
);
1791
393
      if (IncludedInFID) {
1792
        // Skip the files/macros of the #include'd file, we only care about
1793
        // macros that lexed macro arguments from our file.
1794
284
        if (Entry.getFile().NumCreatedFIDs)
1795
281
          ID += Entry.getFile().NumCreatedFIDs - 1 /*because of next ++ID*/;
1796
284
        continue;
1797
284
      } else 
if (109
IncludeLoc.isValid()109
) {
1798
        // If file was included but not from FID, there is no more files/macros
1799
        // that may be "contained" in this file.
1800
28
        return;
1801
28
      }
1802
81
      continue;
1803
393
    }
1804
1805
934
    const ExpansionInfo &ExpInfo = Entry.getExpansion();
1806
1807
934
    if (ExpInfo.getExpansionLocStart().isFileID()) {
1808
364
      if (!isInFileID(ExpInfo.getExpansionLocStart(), FID))
1809
1
        return; // No more files/macros that may be "contained" in this file.
1810
364
    }
1811
1812
933
    if (!ExpInfo.isMacroArgExpansion())
1813
430
      continue;
1814
1815
503
    associateFileChunkWithMacroArgExp(MacroArgsCache, FID,
1816
503
                                 ExpInfo.getSpellingLoc(),
1817
503
                                 SourceLocation::getMacroLoc(Entry.getOffset()),
1818
503
                                 getFileIDSize(FileID::get(ID)));
1819
503
  }
1820
274
}
1821
1822
void SourceManager::associateFileChunkWithMacroArgExp(
1823
                                         MacroArgsMap &MacroArgsCache,
1824
                                         FileID FID,
1825
                                         SourceLocation SpellLoc,
1826
                                         SourceLocation ExpansionLoc,
1827
546
                                         unsigned ExpansionLength) const {
1828
546
  if (!SpellLoc.isFileID()) {
1829
48
    SourceLocation::UIntTy SpellBeginOffs = SpellLoc.getOffset();
1830
48
    SourceLocation::UIntTy SpellEndOffs = SpellBeginOffs + ExpansionLength;
1831
1832
    // The spelling range for this macro argument expansion can span multiple
1833
    // consecutive FileID entries. Go through each entry contained in the
1834
    // spelling range and if one is itself a macro argument expansion, recurse
1835
    // and associate the file chunk that it represents.
1836
1837
48
    FileID SpellFID; // Current FileID in the spelling range.
1838
48
    unsigned SpellRelativeOffs;
1839
48
    std::tie(SpellFID, SpellRelativeOffs) = getDecomposedLoc(SpellLoc);
1840
48
    while (true) {
1841
48
      const SLocEntry &Entry = getSLocEntry(SpellFID);
1842
48
      SourceLocation::UIntTy SpellFIDBeginOffs = Entry.getOffset();
1843
48
      unsigned SpellFIDSize = getFileIDSize(SpellFID);
1844
48
      SourceLocation::UIntTy SpellFIDEndOffs = SpellFIDBeginOffs + SpellFIDSize;
1845
48
      const ExpansionInfo &Info = Entry.getExpansion();
1846
48
      if (Info.isMacroArgExpansion()) {
1847
43
        unsigned CurrSpellLength;
1848
43
        if (SpellFIDEndOffs < SpellEndOffs)
1849
0
          CurrSpellLength = SpellFIDSize - SpellRelativeOffs;
1850
43
        else
1851
43
          CurrSpellLength = ExpansionLength;
1852
43
        associateFileChunkWithMacroArgExp(MacroArgsCache, FID,
1853
43
                      Info.getSpellingLoc().getLocWithOffset(SpellRelativeOffs),
1854
43
                      ExpansionLoc, CurrSpellLength);
1855
43
      }
1856
1857
48
      if (SpellFIDEndOffs >= SpellEndOffs)
1858
48
        return; // we covered all FileID entries in the spelling range.
1859
1860
      // Move to the next FileID entry in the spelling range.
1861
0
      unsigned advance = SpellFIDSize - SpellRelativeOffs + 1;
1862
0
      ExpansionLoc = ExpansionLoc.getLocWithOffset(advance);
1863
0
      ExpansionLength -= advance;
1864
0
      ++SpellFID.ID;
1865
0
      SpellRelativeOffs = 0;
1866
0
    }
1867
48
  }
1868
1869
498
  assert(SpellLoc.isFileID());
1870
1871
0
  unsigned BeginOffs;
1872
498
  if (!isInFileID(SpellLoc, FID, &BeginOffs))
1873
0
    return;
1874
1875
498
  unsigned EndOffs = BeginOffs + ExpansionLength;
1876
1877
  // Add a new chunk for this macro argument. A previous macro argument chunk
1878
  // may have been lexed again, so e.g. if the map is
1879
  //     0   -> SourceLocation()
1880
  //     100 -> Expanded loc #1
1881
  //     110 -> SourceLocation()
1882
  // and we found a new macro FileID that lexed from offset 105 with length 3,
1883
  // the new map will be:
1884
  //     0   -> SourceLocation()
1885
  //     100 -> Expanded loc #1
1886
  //     105 -> Expanded loc #2
1887
  //     108 -> Expanded loc #1
1888
  //     110 -> SourceLocation()
1889
  //
1890
  // Since re-lexed macro chunks will always be the same size or less of
1891
  // previous chunks, we only need to find where the ending of the new macro
1892
  // chunk is mapped to and update the map with new begin/end mappings.
1893
1894
498
  MacroArgsMap::iterator I = MacroArgsCache.upper_bound(EndOffs);
1895
498
  --I;
1896
498
  SourceLocation EndOffsMappedLoc = I->second;
1897
498
  MacroArgsCache[BeginOffs] = ExpansionLoc;
1898
498
  MacroArgsCache[EndOffs] = EndOffsMappedLoc;
1899
498
}
1900
1901
/// If \arg Loc points inside a function macro argument, the returned
1902
/// location will be the macro location in which the argument was expanded.
1903
/// If a macro argument is used multiple times, the expanded location will
1904
/// be at the first expansion of the argument.
1905
/// e.g.
1906
///   MY_MACRO(foo);
1907
///             ^
1908
/// Passing a file location pointing at 'foo', will yield a macro location
1909
/// where 'foo' was expanded into.
1910
SourceLocation
1911
9.93k
SourceManager::getMacroArgExpandedLocation(SourceLocation Loc) const {
1912
9.93k
  if (Loc.isInvalid() || !Loc.isFileID())
1913
0
    return Loc;
1914
1915
9.93k
  FileID FID;
1916
9.93k
  unsigned Offset;
1917
9.93k
  std::tie(FID, Offset) = getDecomposedLoc(Loc);
1918
9.93k
  if (FID.isInvalid())
1919
0
    return Loc;
1920
1921
9.93k
  std::unique_ptr<MacroArgsMap> &MacroArgsCache = MacroArgsCacheMap[FID];
1922
9.93k
  if (!MacroArgsCache) {
1923
274
    MacroArgsCache = std::make_unique<MacroArgsMap>();
1924
274
    computeMacroArgsCache(*MacroArgsCache, FID);
1925
274
  }
1926
1927
9.93k
  assert(!MacroArgsCache->empty());
1928
0
  MacroArgsMap::iterator I = MacroArgsCache->upper_bound(Offset);
1929
  // In case every element in MacroArgsCache is greater than Offset we can't
1930
  // decrement the iterator.
1931
9.93k
  if (I == MacroArgsCache->begin())
1932
0
    return Loc;
1933
1934
9.93k
  --I;
1935
1936
9.93k
  SourceLocation::UIntTy MacroArgBeginOffs = I->first;
1937
9.93k
  SourceLocation MacroArgExpandedLoc = I->second;
1938
9.93k
  if (MacroArgExpandedLoc.isValid())
1939
129
    return MacroArgExpandedLoc.getLocWithOffset(Offset - MacroArgBeginOffs);
1940
1941
9.80k
  return Loc;
1942
9.93k
}
1943
1944
std::pair<FileID, unsigned>
1945
3.20M
SourceManager::getDecomposedIncludedLoc(FileID FID) const {
1946
3.20M
  if (FID.isInvalid())
1947
0
    return std::make_pair(FileID(), 0);
1948
1949
  // Uses IncludedLocMap to retrieve/cache the decomposed loc.
1950
1951
3.20M
  using DecompTy = std::pair<FileID, unsigned>;
1952
3.20M
  auto InsertOp = IncludedLocMap.try_emplace(FID);
1953
3.20M
  DecompTy &DecompLoc = InsertOp.first->second;
1954
3.20M
  if (!InsertOp.second)
1955
266k
    return DecompLoc; // already in map.
1956
1957
2.93M
  SourceLocation UpperLoc;
1958
2.93M
  bool Invalid = false;
1959
2.93M
  const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
1960
2.93M
  if (!Invalid) {
1961
2.93M
    if (Entry.isExpansion())
1962
2.59M
      UpperLoc = Entry.getExpansion().getExpansionLocStart();
1963
337k
    else
1964
337k
      UpperLoc = Entry.getFile().getIncludeLoc();
1965
2.93M
  }
1966
1967
2.93M
  if (UpperLoc.isValid())
1968
2.92M
    DecompLoc = getDecomposedLoc(UpperLoc);
1969
1970
2.93M
  return DecompLoc;
1971
3.20M
}
1972
1973
/// Given a decomposed source location, move it up the include/expansion stack
1974
/// to the parent source location.  If this is possible, return the decomposed
1975
/// version of the parent in Loc and return false.  If Loc is the top-level
1976
/// entry, return true and don't modify it.
1977
static bool MoveUpIncludeHierarchy(std::pair<FileID, unsigned> &Loc,
1978
270k
                                   const SourceManager &SM) {
1979
270k
  std::pair<FileID, unsigned> UpperLoc = SM.getDecomposedIncludedLoc(Loc.first);
1980
270k
  if (UpperLoc.first.isInvalid())
1981
253k
    return true; // We reached the top.
1982
1983
17.1k
  Loc = UpperLoc;
1984
17.1k
  return false;
1985
270k
}
1986
1987
/// Return the cache entry for comparing the given file IDs
1988
/// for isBeforeInTranslationUnit.
1989
InBeforeInTUCacheEntry &SourceManager::getInBeforeInTUCache(FileID LFID,
1990
145k
                                                            FileID RFID) const {
1991
  // This is a magic number for limiting the cache size.  It was experimentally
1992
  // derived from a small Objective-C project (where the cache filled
1993
  // out to ~250 items).  We can make it larger if necessary.
1994
145k
  enum { MagicCacheSize = 300 };
1995
145k
  IsBeforeInTUCacheKey Key(LFID, RFID);
1996
1997
  // If the cache size isn't too large, do a lookup and if necessary default
1998
  // construct an entry.  We can then return it to the caller for direct
1999
  // use.  When they update the value, the cache will get automatically
2000
  // updated as well.
2001
145k
  if (IBTUCache.size() < MagicCacheSize)
2002
145k
    return IBTUCache[Key];
2003
2004
  // Otherwise, do a lookup that will not construct a new value.
2005
0
  InBeforeInTUCache::iterator I = IBTUCache.find(Key);
2006
0
  if (I != IBTUCache.end())
2007
0
    return I->second;
2008
2009
  // Fall back to the overflow value.
2010
0
  return IBTUCacheOverflow;
2011
0
}
2012
2013
/// Determines the order of 2 source locations in the translation unit.
2014
///
2015
/// \returns true if LHS source location comes before RHS, false otherwise.
2016
bool SourceManager::isBeforeInTranslationUnit(SourceLocation LHS,
2017
27.5M
                                              SourceLocation RHS) const {
2018
27.5M
  assert(LHS.isValid() && RHS.isValid() && "Passed invalid source location!");
2019
27.5M
  if (LHS == RHS)
2020
184k
    return false;
2021
2022
27.3M
  std::pair<FileID, unsigned> LOffs = getDecomposedLoc(LHS);
2023
27.3M
  std::pair<FileID, unsigned> ROffs = getDecomposedLoc(RHS);
2024
2025
  // getDecomposedLoc may have failed to return a valid FileID because, e.g. it
2026
  // is a serialized one referring to a file that was removed after we loaded
2027
  // the PCH.
2028
27.3M
  if (LOffs.first.isInvalid() || ROffs.first.isInvalid())
2029
0
    return LOffs.first.isInvalid() && !ROffs.first.isInvalid();
2030
2031
27.3M
  std::pair<bool, bool> InSameTU = isInTheSameTranslationUnit(LOffs, ROffs);
2032
27.3M
  if (InSameTU.first)
2033
27.1M
    return InSameTU.second;
2034
2035
  // If we arrived here, the location is either in a built-ins buffer or
2036
  // associated with global inline asm. PR5662 and PR22576 are examples.
2037
2038
125k
  StringRef LB = getBufferOrFake(LOffs.first).getBufferIdentifier();
2039
125k
  StringRef RB = getBufferOrFake(ROffs.first).getBufferIdentifier();
2040
125k
  bool LIsBuiltins = LB == "<built-in>";
2041
125k
  bool RIsBuiltins = RB == "<built-in>";
2042
  // Sort built-in before non-built-in.
2043
125k
  if (LIsBuiltins || 
RIsBuiltins8.11k
) {
2044
125k
    if (LIsBuiltins != RIsBuiltins)
2045
125k
      return LIsBuiltins;
2046
    // Both are in built-in buffers, but from different files. We just claim that
2047
    // lower IDs come first.
2048
0
    return LOffs.first < ROffs.first;
2049
125k
  }
2050
0
  bool LIsAsm = LB == "<inline asm>";
2051
0
  bool RIsAsm = RB == "<inline asm>";
2052
  // Sort assembler after built-ins, but before the rest.
2053
0
  if (LIsAsm || RIsAsm) {
2054
0
    if (LIsAsm != RIsAsm)
2055
0
      return RIsAsm;
2056
0
    assert(LOffs.first == ROffs.first);
2057
0
    return false;
2058
0
  }
2059
0
  bool LIsScratch = LB == "<scratch space>";
2060
0
  bool RIsScratch = RB == "<scratch space>";
2061
  // Sort scratch after inline asm, but before the rest.
2062
0
  if (LIsScratch || RIsScratch) {
2063
0
    if (LIsScratch != RIsScratch)
2064
0
      return LIsScratch;
2065
0
    return LOffs.second < ROffs.second;
2066
0
  }
2067
0
  llvm_unreachable("Unsortable locations found");
2068
0
}
2069
2070
std::pair<bool, bool> SourceManager::isInTheSameTranslationUnit(
2071
    std::pair<FileID, unsigned> &LOffs,
2072
27.5M
    std::pair<FileID, unsigned> &ROffs) const {
2073
  // If the source locations are in the same file, just compare offsets.
2074
27.5M
  if (LOffs.first == ROffs.first)
2075
27.3M
    return std::make_pair(true, LOffs.second < ROffs.second);
2076
2077
  // If we are comparing a source location with multiple locations in the same
2078
  // file, we get a big win by caching the result.
2079
145k
  InBeforeInTUCacheEntry &IsBeforeInTUCache =
2080
145k
    getInBeforeInTUCache(LOffs.first, ROffs.first);
2081
2082
  // If we are comparing a source location with multiple locations in the same
2083
  // file, we get a big win by caching the result.
2084
145k
  if (IsBeforeInTUCache.isCacheValid(LOffs.first, ROffs.first))
2085
16.1k
    return std::make_pair(
2086
16.1k
        true, IsBeforeInTUCache.getCachedResult(LOffs.second, ROffs.second));
2087
2088
  // Okay, we missed in the cache, start updating the cache for this query.
2089
129k
  IsBeforeInTUCache.setQueryFIDs(LOffs.first, ROffs.first,
2090
129k
                          /*isLFIDBeforeRFID=*/LOffs.first.ID < ROffs.first.ID);
2091
2092
  // We need to find the common ancestor. The only way of doing this is to
2093
  // build the complete include chain for one and then walking up the chain
2094
  // of the other looking for a match.
2095
  // We use a map from FileID to Offset to store the chain. Easier than writing
2096
  // a custom set hash info that only depends on the first part of a pair.
2097
129k
  using LocSet = llvm::SmallDenseMap<FileID, unsigned, 16>;
2098
129k
  LocSet LChain;
2099
137k
  do {
2100
137k
    LChain.insert(LOffs);
2101
    // We catch the case where LOffs is in a file included by ROffs and
2102
    // quit early. The other way round unfortunately remains suboptimal.
2103
137k
  } while (LOffs.first != ROffs.first && 
!MoveUpIncludeHierarchy(LOffs, *this)136k
);
2104
129k
  LocSet::iterator I;
2105
138k
  while((I = LChain.find(ROffs.first)) == LChain.end()) {
2106
134k
    if (MoveUpIncludeHierarchy(ROffs, *this))
2107
125k
      break; // Met at topmost file.
2108
134k
  }
2109
129k
  if (I != LChain.end())
2110
3.42k
    LOffs = *I;
2111
2112
  // If we exited because we found a nearest common ancestor, compare the
2113
  // locations within the common file and cache them.
2114
129k
  if (LOffs.first == ROffs.first) {
2115
3.42k
    IsBeforeInTUCache.setCommonLoc(LOffs.first, LOffs.second, ROffs.second);
2116
3.42k
    return std::make_pair(
2117
3.42k
        true, IsBeforeInTUCache.getCachedResult(LOffs.second, ROffs.second));
2118
3.42k
  }
2119
  // Clear the lookup cache, it depends on a common location.
2120
125k
  IsBeforeInTUCache.clear();
2121
125k
  return std::make_pair(false, false);
2122
129k
}
2123
2124
4
void SourceManager::PrintStats() const {
2125
4
  llvm::errs() << "\n*** Source Manager Stats:\n";
2126
4
  llvm::errs() << FileInfos.size() << " files mapped, " << MemBufferInfos.size()
2127
4
               << " mem buffers mapped.\n";
2128
4
  llvm::errs() << LocalSLocEntryTable.size() << " local SLocEntry's allocated ("
2129
4
               << llvm::capacity_in_bytes(LocalSLocEntryTable)
2130
4
               << " bytes of capacity), "
2131
4
               << NextLocalOffset << "B of Sloc address space used.\n";
2132
4
  llvm::errs() << LoadedSLocEntryTable.size()
2133
4
               << " loaded SLocEntries allocated, "
2134
4
               << MaxLoadedOffset - CurrentLoadedOffset
2135
4
               << "B of Sloc address space used.\n";
2136
2137
4
  unsigned NumLineNumsComputed = 0;
2138
4
  unsigned NumFileBytesMapped = 0;
2139
11
  for (fileinfo_iterator I = fileinfo_begin(), E = fileinfo_end(); I != E; 
++I7
){
2140
7
    NumLineNumsComputed += bool(I->second->SourceLineCache);
2141
7
    NumFileBytesMapped  += I->second->getSizeBytesMapped();
2142
7
  }
2143
4
  unsigned NumMacroArgsComputed = MacroArgsCacheMap.size();
2144
2145
4
  llvm::errs() << NumFileBytesMapped << " bytes of files mapped, "
2146
4
               << NumLineNumsComputed << " files with line #'s computed, "
2147
4
               << NumMacroArgsComputed << " files with macro args computed.\n";
2148
4
  llvm::errs() << "FileID scans: " << NumLinearScans << " linear, "
2149
4
               << NumBinaryProbes << " binary.\n";
2150
4
}
2151
2152
0
LLVM_DUMP_METHOD void SourceManager::dump() const {
2153
0
  llvm::raw_ostream &out = llvm::errs();
2154
2155
0
  auto DumpSLocEntry = [&](int ID, const SrcMgr::SLocEntry &Entry,
2156
0
                           llvm::Optional<SourceLocation::UIntTy> NextStart) {
2157
0
    out << "SLocEntry <FileID " << ID << "> " << (Entry.isFile() ? "file" : "expansion")
2158
0
        << " <SourceLocation " << Entry.getOffset() << ":";
2159
0
    if (NextStart)
2160
0
      out << *NextStart << ">\n";
2161
0
    else
2162
0
      out << "???\?>\n";
2163
0
    if (Entry.isFile()) {
2164
0
      auto &FI = Entry.getFile();
2165
0
      if (FI.NumCreatedFIDs)
2166
0
        out << "  covers <FileID " << ID << ":" << int(ID + FI.NumCreatedFIDs)
2167
0
            << ">\n";
2168
0
      if (FI.getIncludeLoc().isValid())
2169
0
        out << "  included from " << FI.getIncludeLoc().getOffset() << "\n";
2170
0
      auto &CC = FI.getContentCache();
2171
0
      out << "  for " << (CC.OrigEntry ? CC.OrigEntry->getName() : "<none>")
2172
0
          << "\n";
2173
0
      if (CC.BufferOverridden)
2174
0
        out << "  contents overridden\n";
2175
0
      if (CC.ContentsEntry != CC.OrigEntry) {
2176
0
        out << "  contents from "
2177
0
            << (CC.ContentsEntry ? CC.ContentsEntry->getName() : "<none>")
2178
0
            << "\n";
2179
0
      }
2180
0
    } else {
2181
0
      auto &EI = Entry.getExpansion();
2182
0
      out << "  spelling from " << EI.getSpellingLoc().getOffset() << "\n";
2183
0
      out << "  macro " << (EI.isMacroArgExpansion() ? "arg" : "body")
2184
0
          << " range <" << EI.getExpansionLocStart().getOffset() << ":"
2185
0
          << EI.getExpansionLocEnd().getOffset() << ">\n";
2186
0
    }
2187
0
  };
2188
2189
  // Dump local SLocEntries.
2190
0
  for (unsigned ID = 0, NumIDs = LocalSLocEntryTable.size(); ID != NumIDs; ++ID) {
2191
0
    DumpSLocEntry(ID, LocalSLocEntryTable[ID],
2192
0
                  ID == NumIDs - 1 ? NextLocalOffset
2193
0
                                   : LocalSLocEntryTable[ID + 1].getOffset());
2194
0
  }
2195
  // Dump loaded SLocEntries.
2196
0
  llvm::Optional<SourceLocation::UIntTy> NextStart;
2197
0
  for (unsigned Index = 0; Index != LoadedSLocEntryTable.size(); ++Index) {
2198
0
    int ID = -(int)Index - 2;
2199
0
    if (SLocEntryLoaded[Index]) {
2200
0
      DumpSLocEntry(ID, LoadedSLocEntryTable[Index], NextStart);
2201
0
      NextStart = LoadedSLocEntryTable[Index].getOffset();
2202
0
    } else {
2203
0
      NextStart = None;
2204
0
    }
2205
0
  }
2206
0
}
2207
2208
12.7k
ExternalSLocEntrySource::~ExternalSLocEntrySource() = default;
2209
2210
/// Return the amount of memory used by memory buffers, breaking down
2211
/// by heap-backed versus mmap'ed memory.
2212
1
SourceManager::MemoryBufferSizes SourceManager::getMemoryBufferSizes() const {
2213
1
  size_t malloc_bytes = 0;
2214
1
  size_t mmap_bytes = 0;
2215
2216
2
  for (unsigned i = 0, e = MemBufferInfos.size(); i != e; 
++i1
)
2217
1
    if (size_t sized_mapped = MemBufferInfos[i]->getSizeBytesMapped())
2218
1
      switch (MemBufferInfos[i]->getMemoryBufferKind()) {
2219
0
        case llvm::MemoryBuffer::MemoryBuffer_MMap:
2220
0
          mmap_bytes += sized_mapped;
2221
0
          break;
2222
1
        case llvm::MemoryBuffer::MemoryBuffer_Malloc:
2223
1
          malloc_bytes += sized_mapped;
2224
1
          break;
2225
1
      }
2226
2227
1
  return MemoryBufferSizes(malloc_bytes, mmap_bytes);
2228
1
}
2229
2230
1
size_t SourceManager::getDataStructureSizes() const {
2231
1
  size_t size = llvm::capacity_in_bytes(MemBufferInfos)
2232
1
    + llvm::capacity_in_bytes(LocalSLocEntryTable)
2233
1
    + llvm::capacity_in_bytes(LoadedSLocEntryTable)
2234
1
    + llvm::capacity_in_bytes(SLocEntryLoaded)
2235
1
    + llvm::capacity_in_bytes(FileInfos);
2236
2237
1
  if (OverriddenFilesInfo)
2238
0
    size += llvm::capacity_in_bytes(OverriddenFilesInfo->OverriddenFiles);
2239
2240
1
  return size;
2241
1
}
2242
2243
SourceManagerForFile::SourceManagerForFile(StringRef FileName,
2244
50.0k
                                           StringRef Content) {
2245
  // This is referenced by `FileMgr` and will be released by `FileMgr` when it
2246
  // is deleted.
2247
50.0k
  IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
2248
50.0k
      new llvm::vfs::InMemoryFileSystem);
2249
50.0k
  InMemoryFileSystem->addFile(
2250
50.0k
      FileName, 0,
2251
50.0k
      llvm::MemoryBuffer::getMemBuffer(Content, FileName,
2252
50.0k
                                       /*RequiresNullTerminator=*/false));
2253
  // This is passed to `SM` as reference, so the pointer has to be referenced
2254
  // in `Environment` so that `FileMgr` can out-live this function scope.
2255
50.0k
  FileMgr =
2256
50.0k
      std::make_unique<FileManager>(FileSystemOptions(), InMemoryFileSystem);
2257
  // This is passed to `SM` as reference, so the pointer has to be referenced
2258
  // by `Environment` due to the same reason above.
2259
50.0k
  Diagnostics = std::make_unique<DiagnosticsEngine>(
2260
50.0k
      IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
2261
50.0k
      new DiagnosticOptions);
2262
50.0k
  SourceMgr = std::make_unique<SourceManager>(*Diagnostics, *FileMgr);
2263
50.0k
  FileID ID = SourceMgr->createFileID(*FileMgr->getFile(FileName),
2264
50.0k
                                      SourceLocation(), clang::SrcMgr::C_User);
2265
50.0k
  assert(ID.isValid());
2266
0
  SourceMgr->setMainFileID(ID);
2267
50.0k
}