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