Coverage Report

Created: 2020-02-15 09:57

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