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