Coverage Report

Created: 2021-01-23 06:44

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