SourceManager.cpp 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  1. //===--- SourceManager.cpp - Track and cache source files -----------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the SourceManager interface.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Basic/SourceManager.h"
  14. #include "clang/Basic/SourceManagerInternals.h"
  15. #include "clang/Basic/Diagnostic.h"
  16. #include "clang/Basic/FileManager.h"
  17. #include "llvm/ADT/StringSwitch.h"
  18. #include "llvm/Support/Compiler.h"
  19. #include "llvm/Support/MemoryBuffer.h"
  20. #include "llvm/Support/raw_ostream.h"
  21. #include "llvm/System/Path.h"
  22. #include <algorithm>
  23. #include <string>
  24. #include <cstring>
  25. using namespace clang;
  26. using namespace SrcMgr;
  27. using llvm::MemoryBuffer;
  28. //===----------------------------------------------------------------------===//
  29. // SourceManager Helper Classes
  30. //===----------------------------------------------------------------------===//
  31. ContentCache::~ContentCache() {
  32. if (shouldFreeBuffer())
  33. delete Buffer.getPointer();
  34. }
  35. /// getSizeBytesMapped - Returns the number of bytes actually mapped for
  36. /// this ContentCache. This can be 0 if the MemBuffer was not actually
  37. /// instantiated.
  38. unsigned ContentCache::getSizeBytesMapped() const {
  39. return Buffer.getPointer() ? Buffer.getPointer()->getBufferSize() : 0;
  40. }
  41. /// getSize - Returns the size of the content encapsulated by this ContentCache.
  42. /// This can be the size of the source file or the size of an arbitrary
  43. /// scratch buffer. If the ContentCache encapsulates a source file, that
  44. /// file is not lazily brought in from disk to satisfy this query.
  45. unsigned ContentCache::getSize() const {
  46. return Buffer.getPointer() ? (unsigned) Buffer.getPointer()->getBufferSize()
  47. : (unsigned) Entry->getSize();
  48. }
  49. void ContentCache::replaceBuffer(const llvm::MemoryBuffer *B,
  50. bool DoNotFree) {
  51. assert(B != Buffer.getPointer());
  52. if (shouldFreeBuffer())
  53. delete Buffer.getPointer();
  54. Buffer.setPointer(B);
  55. Buffer.setInt(DoNotFree? DoNotFreeFlag : 0);
  56. }
  57. const llvm::MemoryBuffer *ContentCache::getBuffer(Diagnostic &Diag,
  58. const SourceManager &SM,
  59. SourceLocation Loc,
  60. bool *Invalid) const {
  61. if (Invalid)
  62. *Invalid = false;
  63. // Lazily create the Buffer for ContentCaches that wrap files.
  64. if (!Buffer.getPointer() && Entry) {
  65. std::string ErrorStr;
  66. Buffer.setPointer(SM.getFileManager().getBufferForFile(Entry, &ErrorStr));
  67. // If we were unable to open the file, then we are in an inconsistent
  68. // situation where the content cache referenced a file which no longer
  69. // exists. Most likely, we were using a stat cache with an invalid entry but
  70. // the file could also have been removed during processing. Since we can't
  71. // really deal with this situation, just create an empty buffer.
  72. //
  73. // FIXME: This is definitely not ideal, but our immediate clients can't
  74. // currently handle returning a null entry here. Ideally we should detect
  75. // that we are in an inconsistent situation and error out as quickly as
  76. // possible.
  77. if (!Buffer.getPointer()) {
  78. const llvm::StringRef FillStr("<<<MISSING SOURCE FILE>>>\n");
  79. Buffer.setPointer(MemoryBuffer::getNewMemBuffer(Entry->getSize(),
  80. "<invalid>"));
  81. char *Ptr = const_cast<char*>(Buffer.getPointer()->getBufferStart());
  82. for (unsigned i = 0, e = Entry->getSize(); i != e; ++i)
  83. Ptr[i] = FillStr[i % FillStr.size()];
  84. if (Diag.isDiagnosticInFlight())
  85. Diag.SetDelayedDiagnostic(diag::err_cannot_open_file,
  86. Entry->getName(), ErrorStr);
  87. else
  88. Diag.Report(Loc, diag::err_cannot_open_file)
  89. << Entry->getName() << ErrorStr;
  90. Buffer.setInt(Buffer.getInt() | InvalidFlag);
  91. } else if (getRawBuffer()->getBufferSize() != (size_t)Entry->getSize()) {
  92. // Check that the file's size is the same as in the file entry (which may
  93. // have come from a stat cache).
  94. if (Diag.isDiagnosticInFlight())
  95. Diag.SetDelayedDiagnostic(diag::err_file_modified,
  96. Entry->getName());
  97. else
  98. Diag.Report(Loc, diag::err_file_modified)
  99. << Entry->getName();
  100. Buffer.setInt(Buffer.getInt() | InvalidFlag);
  101. }
  102. // If the buffer is valid, check to see if it has a UTF Byte Order Mark
  103. // (BOM). We only support UTF-8 without a BOM right now. See
  104. // http://en.wikipedia.org/wiki/Byte_order_mark for more information.
  105. if (!isBufferInvalid()) {
  106. llvm::StringRef BufStr = Buffer.getPointer()->getBuffer();
  107. const char *BOM = llvm::StringSwitch<const char *>(BufStr)
  108. .StartsWith("\xEF\xBB\xBF", "UTF-8")
  109. .StartsWith("\xFE\xFF", "UTF-16 (BE)")
  110. .StartsWith("\xFF\xFE", "UTF-16 (LE)")
  111. .StartsWith("\x00\x00\xFE\xFF", "UTF-32 (BE)")
  112. .StartsWith("\xFF\xFE\x00\x00", "UTF-32 (LE)")
  113. .StartsWith("\x2B\x2F\x76", "UTF-7")
  114. .StartsWith("\xF7\x64\x4C", "UTF-1")
  115. .StartsWith("\xDD\x73\x66\x73", "UTF-EBCDIC")
  116. .StartsWith("\x0E\xFE\xFF", "SDSU")
  117. .StartsWith("\xFB\xEE\x28", "BOCU-1")
  118. .StartsWith("\x84\x31\x95\x33", "GB-18030")
  119. .Default(0);
  120. if (BOM) {
  121. Diag.Report(Loc, diag::err_unsupported_bom)
  122. << BOM << Entry->getName();
  123. Buffer.setInt(1);
  124. }
  125. }
  126. }
  127. if (Invalid)
  128. *Invalid = isBufferInvalid();
  129. return Buffer.getPointer();
  130. }
  131. unsigned LineTableInfo::getLineTableFilenameID(const char *Ptr, unsigned Len) {
  132. // Look up the filename in the string table, returning the pre-existing value
  133. // if it exists.
  134. llvm::StringMapEntry<unsigned> &Entry =
  135. FilenameIDs.GetOrCreateValue(Ptr, Ptr+Len, ~0U);
  136. if (Entry.getValue() != ~0U)
  137. return Entry.getValue();
  138. // Otherwise, assign this the next available ID.
  139. Entry.setValue(FilenamesByID.size());
  140. FilenamesByID.push_back(&Entry);
  141. return FilenamesByID.size()-1;
  142. }
  143. /// AddLineNote - Add a line note to the line table that indicates that there
  144. /// is a #line at the specified FID/Offset location which changes the presumed
  145. /// location to LineNo/FilenameID.
  146. void LineTableInfo::AddLineNote(unsigned FID, unsigned Offset,
  147. unsigned LineNo, int FilenameID) {
  148. std::vector<LineEntry> &Entries = LineEntries[FID];
  149. assert((Entries.empty() || Entries.back().FileOffset < Offset) &&
  150. "Adding line entries out of order!");
  151. SrcMgr::CharacteristicKind Kind = SrcMgr::C_User;
  152. unsigned IncludeOffset = 0;
  153. if (!Entries.empty()) {
  154. // If this is a '#line 4' after '#line 42 "foo.h"', make sure to remember
  155. // that we are still in "foo.h".
  156. if (FilenameID == -1)
  157. FilenameID = Entries.back().FilenameID;
  158. // If we are after a line marker that switched us to system header mode, or
  159. // that set #include information, preserve it.
  160. Kind = Entries.back().FileKind;
  161. IncludeOffset = Entries.back().IncludeOffset;
  162. }
  163. Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, Kind,
  164. IncludeOffset));
  165. }
  166. /// AddLineNote This is the same as the previous version of AddLineNote, but is
  167. /// used for GNU line markers. If EntryExit is 0, then this doesn't change the
  168. /// presumed #include stack. If it is 1, this is a file entry, if it is 2 then
  169. /// this is a file exit. FileKind specifies whether this is a system header or
  170. /// extern C system header.
  171. void LineTableInfo::AddLineNote(unsigned FID, unsigned Offset,
  172. unsigned LineNo, int FilenameID,
  173. unsigned EntryExit,
  174. SrcMgr::CharacteristicKind FileKind) {
  175. assert(FilenameID != -1 && "Unspecified filename should use other accessor");
  176. std::vector<LineEntry> &Entries = LineEntries[FID];
  177. assert((Entries.empty() || Entries.back().FileOffset < Offset) &&
  178. "Adding line entries out of order!");
  179. unsigned IncludeOffset = 0;
  180. if (EntryExit == 0) { // No #include stack change.
  181. IncludeOffset = Entries.empty() ? 0 : Entries.back().IncludeOffset;
  182. } else if (EntryExit == 1) {
  183. IncludeOffset = Offset-1;
  184. } else if (EntryExit == 2) {
  185. assert(!Entries.empty() && Entries.back().IncludeOffset &&
  186. "PPDirectives should have caught case when popping empty include stack");
  187. // Get the include loc of the last entries' include loc as our include loc.
  188. IncludeOffset = 0;
  189. if (const LineEntry *PrevEntry =
  190. FindNearestLineEntry(FID, Entries.back().IncludeOffset))
  191. IncludeOffset = PrevEntry->IncludeOffset;
  192. }
  193. Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, FileKind,
  194. IncludeOffset));
  195. }
  196. /// FindNearestLineEntry - Find the line entry nearest to FID that is before
  197. /// it. If there is no line entry before Offset in FID, return null.
  198. const LineEntry *LineTableInfo::FindNearestLineEntry(unsigned FID,
  199. unsigned Offset) {
  200. const std::vector<LineEntry> &Entries = LineEntries[FID];
  201. assert(!Entries.empty() && "No #line entries for this FID after all!");
  202. // It is very common for the query to be after the last #line, check this
  203. // first.
  204. if (Entries.back().FileOffset <= Offset)
  205. return &Entries.back();
  206. // Do a binary search to find the maximal element that is still before Offset.
  207. std::vector<LineEntry>::const_iterator I =
  208. std::upper_bound(Entries.begin(), Entries.end(), Offset);
  209. if (I == Entries.begin()) return 0;
  210. return &*--I;
  211. }
  212. /// \brief Add a new line entry that has already been encoded into
  213. /// the internal representation of the line table.
  214. void LineTableInfo::AddEntry(unsigned FID,
  215. const std::vector<LineEntry> &Entries) {
  216. LineEntries[FID] = Entries;
  217. }
  218. /// getLineTableFilenameID - Return the uniqued ID for the specified filename.
  219. ///
  220. unsigned SourceManager::getLineTableFilenameID(const char *Ptr, unsigned Len) {
  221. if (LineTable == 0)
  222. LineTable = new LineTableInfo();
  223. return LineTable->getLineTableFilenameID(Ptr, Len);
  224. }
  225. /// AddLineNote - Add a line note to the line table for the FileID and offset
  226. /// specified by Loc. If FilenameID is -1, it is considered to be
  227. /// unspecified.
  228. void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo,
  229. int FilenameID) {
  230. std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
  231. const SrcMgr::FileInfo &FileInfo = getSLocEntry(LocInfo.first).getFile();
  232. // Remember that this file has #line directives now if it doesn't already.
  233. const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives();
  234. if (LineTable == 0)
  235. LineTable = new LineTableInfo();
  236. LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID);
  237. }
  238. /// AddLineNote - Add a GNU line marker to the line table.
  239. void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo,
  240. int FilenameID, bool IsFileEntry,
  241. bool IsFileExit, bool IsSystemHeader,
  242. bool IsExternCHeader) {
  243. // If there is no filename and no flags, this is treated just like a #line,
  244. // which does not change the flags of the previous line marker.
  245. if (FilenameID == -1) {
  246. assert(!IsFileEntry && !IsFileExit && !IsSystemHeader && !IsExternCHeader &&
  247. "Can't set flags without setting the filename!");
  248. return AddLineNote(Loc, LineNo, FilenameID);
  249. }
  250. std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
  251. const SrcMgr::FileInfo &FileInfo = getSLocEntry(LocInfo.first).getFile();
  252. // Remember that this file has #line directives now if it doesn't already.
  253. const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives();
  254. if (LineTable == 0)
  255. LineTable = new LineTableInfo();
  256. SrcMgr::CharacteristicKind FileKind;
  257. if (IsExternCHeader)
  258. FileKind = SrcMgr::C_ExternCSystem;
  259. else if (IsSystemHeader)
  260. FileKind = SrcMgr::C_System;
  261. else
  262. FileKind = SrcMgr::C_User;
  263. unsigned EntryExit = 0;
  264. if (IsFileEntry)
  265. EntryExit = 1;
  266. else if (IsFileExit)
  267. EntryExit = 2;
  268. LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID,
  269. EntryExit, FileKind);
  270. }
  271. LineTableInfo &SourceManager::getLineTable() {
  272. if (LineTable == 0)
  273. LineTable = new LineTableInfo();
  274. return *LineTable;
  275. }
  276. //===----------------------------------------------------------------------===//
  277. // Private 'Create' methods.
  278. //===----------------------------------------------------------------------===//
  279. SourceManager::SourceManager(Diagnostic &Diag, FileManager &FileMgr)
  280. : Diag(Diag), FileMgr(FileMgr),
  281. ExternalSLocEntries(0), LineTable(0), NumLinearScans(0),
  282. NumBinaryProbes(0) {
  283. clearIDTables();
  284. Diag.setSourceManager(this);
  285. }
  286. SourceManager::~SourceManager() {
  287. delete LineTable;
  288. // Delete FileEntry objects corresponding to content caches. Since the actual
  289. // content cache objects are bump pointer allocated, we just have to run the
  290. // dtors, but we call the deallocate method for completeness.
  291. for (unsigned i = 0, e = MemBufferInfos.size(); i != e; ++i) {
  292. MemBufferInfos[i]->~ContentCache();
  293. ContentCacheAlloc.Deallocate(MemBufferInfos[i]);
  294. }
  295. for (llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>::iterator
  296. I = FileInfos.begin(), E = FileInfos.end(); I != E; ++I) {
  297. I->second->~ContentCache();
  298. ContentCacheAlloc.Deallocate(I->second);
  299. }
  300. }
  301. void SourceManager::clearIDTables() {
  302. MainFileID = FileID();
  303. SLocEntryTable.clear();
  304. LastLineNoFileIDQuery = FileID();
  305. LastLineNoContentCache = 0;
  306. LastFileIDLookup = FileID();
  307. if (LineTable)
  308. LineTable->clear();
  309. // Use up FileID #0 as an invalid instantiation.
  310. NextOffset = 0;
  311. createInstantiationLoc(SourceLocation(),SourceLocation(),SourceLocation(), 1);
  312. }
  313. /// getOrCreateContentCache - Create or return a cached ContentCache for the
  314. /// specified file.
  315. const ContentCache *
  316. SourceManager::getOrCreateContentCache(const FileEntry *FileEnt) {
  317. assert(FileEnt && "Didn't specify a file entry to use?");
  318. // Do we already have information about this file?
  319. ContentCache *&Entry = FileInfos[FileEnt];
  320. if (Entry) return Entry;
  321. // Nope, create a new Cache entry. Make sure it is at least 8-byte aligned
  322. // so that FileInfo can use the low 3 bits of the pointer for its own
  323. // nefarious purposes.
  324. unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment;
  325. EntryAlign = std::max(8U, EntryAlign);
  326. Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign);
  327. new (Entry) ContentCache(FileEnt);
  328. return Entry;
  329. }
  330. /// createMemBufferContentCache - Create a new ContentCache for the specified
  331. /// memory buffer. This does no caching.
  332. const ContentCache*
  333. SourceManager::createMemBufferContentCache(const MemoryBuffer *Buffer) {
  334. // Add a new ContentCache to the MemBufferInfos list and return it. Make sure
  335. // it is at least 8-byte aligned so that FileInfo can use the low 3 bits of
  336. // the pointer for its own nefarious purposes.
  337. unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment;
  338. EntryAlign = std::max(8U, EntryAlign);
  339. ContentCache *Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign);
  340. new (Entry) ContentCache();
  341. MemBufferInfos.push_back(Entry);
  342. Entry->setBuffer(Buffer);
  343. return Entry;
  344. }
  345. void SourceManager::PreallocateSLocEntries(ExternalSLocEntrySource *Source,
  346. unsigned NumSLocEntries,
  347. unsigned NextOffset) {
  348. ExternalSLocEntries = Source;
  349. this->NextOffset = NextOffset;
  350. unsigned CurPrealloc = SLocEntryLoaded.size();
  351. // If we've ever preallocated, we must not count the dummy entry.
  352. if (CurPrealloc) --CurPrealloc;
  353. SLocEntryLoaded.resize(NumSLocEntries + 1);
  354. SLocEntryLoaded[0] = true;
  355. SLocEntryTable.resize(SLocEntryTable.size() + NumSLocEntries - CurPrealloc);
  356. }
  357. void SourceManager::ClearPreallocatedSLocEntries() {
  358. unsigned I = 0;
  359. for (unsigned N = SLocEntryLoaded.size(); I != N; ++I)
  360. if (!SLocEntryLoaded[I])
  361. break;
  362. // We've already loaded all preallocated source location entries.
  363. if (I == SLocEntryLoaded.size())
  364. return;
  365. // Remove everything from location I onward.
  366. SLocEntryTable.resize(I);
  367. SLocEntryLoaded.clear();
  368. ExternalSLocEntries = 0;
  369. }
  370. //===----------------------------------------------------------------------===//
  371. // Methods to create new FileID's and instantiations.
  372. //===----------------------------------------------------------------------===//
  373. /// createFileID - Create a new FileID for the specified ContentCache and
  374. /// include position. This works regardless of whether the ContentCache
  375. /// corresponds to a file or some other input source.
  376. FileID SourceManager::createFileID(const ContentCache *File,
  377. SourceLocation IncludePos,
  378. SrcMgr::CharacteristicKind FileCharacter,
  379. unsigned PreallocatedID,
  380. unsigned Offset) {
  381. if (PreallocatedID) {
  382. // If we're filling in a preallocated ID, just load in the file
  383. // entry and return.
  384. assert(PreallocatedID < SLocEntryLoaded.size() &&
  385. "Preallocate ID out-of-range");
  386. assert(!SLocEntryLoaded[PreallocatedID] &&
  387. "Source location entry already loaded");
  388. assert(Offset && "Preallocate source location cannot have zero offset");
  389. SLocEntryTable[PreallocatedID]
  390. = SLocEntry::get(Offset, FileInfo::get(IncludePos, File, FileCharacter));
  391. SLocEntryLoaded[PreallocatedID] = true;
  392. FileID FID = FileID::get(PreallocatedID);
  393. return FID;
  394. }
  395. SLocEntryTable.push_back(SLocEntry::get(NextOffset,
  396. FileInfo::get(IncludePos, File,
  397. FileCharacter)));
  398. unsigned FileSize = File->getSize();
  399. assert(NextOffset+FileSize+1 > NextOffset && "Ran out of source locations!");
  400. NextOffset += FileSize+1;
  401. // Set LastFileIDLookup to the newly created file. The next getFileID call is
  402. // almost guaranteed to be from that file.
  403. FileID FID = FileID::get(SLocEntryTable.size()-1);
  404. return LastFileIDLookup = FID;
  405. }
  406. /// createInstantiationLoc - Return a new SourceLocation that encodes the fact
  407. /// that a token from SpellingLoc should actually be referenced from
  408. /// InstantiationLoc.
  409. SourceLocation SourceManager::createInstantiationLoc(SourceLocation SpellingLoc,
  410. SourceLocation ILocStart,
  411. SourceLocation ILocEnd,
  412. unsigned TokLength,
  413. unsigned PreallocatedID,
  414. unsigned Offset) {
  415. InstantiationInfo II = InstantiationInfo::get(ILocStart,ILocEnd, SpellingLoc);
  416. if (PreallocatedID) {
  417. // If we're filling in a preallocated ID, just load in the
  418. // instantiation entry and return.
  419. assert(PreallocatedID < SLocEntryLoaded.size() &&
  420. "Preallocate ID out-of-range");
  421. assert(!SLocEntryLoaded[PreallocatedID] &&
  422. "Source location entry already loaded");
  423. assert(Offset && "Preallocate source location cannot have zero offset");
  424. SLocEntryTable[PreallocatedID] = SLocEntry::get(Offset, II);
  425. SLocEntryLoaded[PreallocatedID] = true;
  426. return SourceLocation::getMacroLoc(Offset);
  427. }
  428. SLocEntryTable.push_back(SLocEntry::get(NextOffset, II));
  429. assert(NextOffset+TokLength+1 > NextOffset && "Ran out of source locations!");
  430. NextOffset += TokLength+1;
  431. return SourceLocation::getMacroLoc(NextOffset-(TokLength+1));
  432. }
  433. const llvm::MemoryBuffer *
  434. SourceManager::getMemoryBufferForFile(const FileEntry *File,
  435. bool *Invalid) {
  436. const SrcMgr::ContentCache *IR = getOrCreateContentCache(File);
  437. assert(IR && "getOrCreateContentCache() cannot return NULL");
  438. return IR->getBuffer(Diag, *this, SourceLocation(), Invalid);
  439. }
  440. void SourceManager::overrideFileContents(const FileEntry *SourceFile,
  441. const llvm::MemoryBuffer *Buffer,
  442. bool DoNotFree) {
  443. const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile);
  444. assert(IR && "getOrCreateContentCache() cannot return NULL");
  445. const_cast<SrcMgr::ContentCache *>(IR)->replaceBuffer(Buffer, DoNotFree);
  446. }
  447. llvm::StringRef SourceManager::getBufferData(FileID FID, bool *Invalid) const {
  448. bool MyInvalid = false;
  449. const llvm::MemoryBuffer *Buf = getBuffer(FID, &MyInvalid);
  450. if (Invalid)
  451. *Invalid = MyInvalid;
  452. if (MyInvalid)
  453. return "";
  454. return Buf->getBuffer();
  455. }
  456. //===----------------------------------------------------------------------===//
  457. // SourceLocation manipulation methods.
  458. //===----------------------------------------------------------------------===//
  459. /// getFileIDSlow - Return the FileID for a SourceLocation. This is a very hot
  460. /// method that is used for all SourceManager queries that start with a
  461. /// SourceLocation object. It is responsible for finding the entry in
  462. /// SLocEntryTable which contains the specified location.
  463. ///
  464. FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const {
  465. assert(SLocOffset && "Invalid FileID");
  466. // After the first and second level caches, I see two common sorts of
  467. // behavior: 1) a lot of searched FileID's are "near" the cached file location
  468. // or are "near" the cached instantiation location. 2) others are just
  469. // completely random and may be a very long way away.
  470. //
  471. // To handle this, we do a linear search for up to 8 steps to catch #1 quickly
  472. // then we fall back to a less cache efficient, but more scalable, binary
  473. // search to find the location.
  474. // See if this is near the file point - worst case we start scanning from the
  475. // most newly created FileID.
  476. std::vector<SrcMgr::SLocEntry>::const_iterator I;
  477. if (SLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) {
  478. // Neither loc prunes our search.
  479. I = SLocEntryTable.end();
  480. } else {
  481. // Perhaps it is near the file point.
  482. I = SLocEntryTable.begin()+LastFileIDLookup.ID;
  483. }
  484. // Find the FileID that contains this. "I" is an iterator that points to a
  485. // FileID whose offset is known to be larger than SLocOffset.
  486. unsigned NumProbes = 0;
  487. while (1) {
  488. --I;
  489. if (ExternalSLocEntries)
  490. getSLocEntry(FileID::get(I - SLocEntryTable.begin()));
  491. if (I->getOffset() <= SLocOffset) {
  492. #if 0
  493. printf("lin %d -> %d [%s] %d %d\n", SLocOffset,
  494. I-SLocEntryTable.begin(),
  495. I->isInstantiation() ? "inst" : "file",
  496. LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
  497. #endif
  498. FileID Res = FileID::get(I-SLocEntryTable.begin());
  499. // If this isn't an instantiation, remember it. We have good locality
  500. // across FileID lookups.
  501. if (!I->isInstantiation())
  502. LastFileIDLookup = Res;
  503. NumLinearScans += NumProbes+1;
  504. return Res;
  505. }
  506. if (++NumProbes == 8)
  507. break;
  508. }
  509. // Convert "I" back into an index. We know that it is an entry whose index is
  510. // larger than the offset we are looking for.
  511. unsigned GreaterIndex = I-SLocEntryTable.begin();
  512. // LessIndex - This is the lower bound of the range that we're searching.
  513. // We know that the offset corresponding to the FileID is is less than
  514. // SLocOffset.
  515. unsigned LessIndex = 0;
  516. NumProbes = 0;
  517. while (1) {
  518. unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex;
  519. unsigned MidOffset = getSLocEntry(FileID::get(MiddleIndex)).getOffset();
  520. ++NumProbes;
  521. // If the offset of the midpoint is too large, chop the high side of the
  522. // range to the midpoint.
  523. if (MidOffset > SLocOffset) {
  524. GreaterIndex = MiddleIndex;
  525. continue;
  526. }
  527. // If the middle index contains the value, succeed and return.
  528. if (isOffsetInFileID(FileID::get(MiddleIndex), SLocOffset)) {
  529. #if 0
  530. printf("bin %d -> %d [%s] %d %d\n", SLocOffset,
  531. I-SLocEntryTable.begin(),
  532. I->isInstantiation() ? "inst" : "file",
  533. LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
  534. #endif
  535. FileID Res = FileID::get(MiddleIndex);
  536. // If this isn't an instantiation, remember it. We have good locality
  537. // across FileID lookups.
  538. if (!I->isInstantiation())
  539. LastFileIDLookup = Res;
  540. NumBinaryProbes += NumProbes;
  541. return Res;
  542. }
  543. // Otherwise, move the low-side up to the middle index.
  544. LessIndex = MiddleIndex;
  545. }
  546. }
  547. SourceLocation SourceManager::
  548. getInstantiationLocSlowCase(SourceLocation Loc) const {
  549. do {
  550. // Note: If Loc indicates an offset into a token that came from a macro
  551. // expansion (e.g. the 5th character of the token) we do not want to add
  552. // this offset when going to the instantiation location. The instatiation
  553. // location is the macro invocation, which the offset has nothing to do
  554. // with. This is unlike when we get the spelling loc, because the offset
  555. // directly correspond to the token whose spelling we're inspecting.
  556. Loc = getSLocEntry(getFileID(Loc)).getInstantiation()
  557. .getInstantiationLocStart();
  558. } while (!Loc.isFileID());
  559. return Loc;
  560. }
  561. SourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const {
  562. do {
  563. std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
  564. Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc();
  565. Loc = Loc.getFileLocWithOffset(LocInfo.second);
  566. } while (!Loc.isFileID());
  567. return Loc;
  568. }
  569. std::pair<FileID, unsigned>
  570. SourceManager::getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E,
  571. unsigned Offset) const {
  572. // If this is an instantiation record, walk through all the instantiation
  573. // points.
  574. FileID FID;
  575. SourceLocation Loc;
  576. do {
  577. Loc = E->getInstantiation().getInstantiationLocStart();
  578. FID = getFileID(Loc);
  579. E = &getSLocEntry(FID);
  580. Offset += Loc.getOffset()-E->getOffset();
  581. } while (!Loc.isFileID());
  582. return std::make_pair(FID, Offset);
  583. }
  584. std::pair<FileID, unsigned>
  585. SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
  586. unsigned Offset) const {
  587. // If this is an instantiation record, walk through all the instantiation
  588. // points.
  589. FileID FID;
  590. SourceLocation Loc;
  591. do {
  592. Loc = E->getInstantiation().getSpellingLoc();
  593. FID = getFileID(Loc);
  594. E = &getSLocEntry(FID);
  595. Offset += Loc.getOffset()-E->getOffset();
  596. } while (!Loc.isFileID());
  597. return std::make_pair(FID, Offset);
  598. }
  599. /// getImmediateSpellingLoc - Given a SourceLocation object, return the
  600. /// spelling location referenced by the ID. This is the first level down
  601. /// towards the place where the characters that make up the lexed token can be
  602. /// found. This should not generally be used by clients.
  603. SourceLocation SourceManager::getImmediateSpellingLoc(SourceLocation Loc) const{
  604. if (Loc.isFileID()) return Loc;
  605. std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
  606. Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc();
  607. return Loc.getFileLocWithOffset(LocInfo.second);
  608. }
  609. /// getImmediateInstantiationRange - Loc is required to be an instantiation
  610. /// location. Return the start/end of the instantiation information.
  611. std::pair<SourceLocation,SourceLocation>
  612. SourceManager::getImmediateInstantiationRange(SourceLocation Loc) const {
  613. assert(Loc.isMacroID() && "Not an instantiation loc!");
  614. const InstantiationInfo &II = getSLocEntry(getFileID(Loc)).getInstantiation();
  615. return II.getInstantiationLocRange();
  616. }
  617. /// getInstantiationRange - Given a SourceLocation object, return the
  618. /// range of tokens covered by the instantiation in the ultimate file.
  619. std::pair<SourceLocation,SourceLocation>
  620. SourceManager::getInstantiationRange(SourceLocation Loc) const {
  621. if (Loc.isFileID()) return std::make_pair(Loc, Loc);
  622. std::pair<SourceLocation,SourceLocation> Res =
  623. getImmediateInstantiationRange(Loc);
  624. // Fully resolve the start and end locations to their ultimate instantiation
  625. // points.
  626. while (!Res.first.isFileID())
  627. Res.first = getImmediateInstantiationRange(Res.first).first;
  628. while (!Res.second.isFileID())
  629. Res.second = getImmediateInstantiationRange(Res.second).second;
  630. return Res;
  631. }
  632. //===----------------------------------------------------------------------===//
  633. // Queries about the code at a SourceLocation.
  634. //===----------------------------------------------------------------------===//
  635. /// getCharacterData - Return a pointer to the start of the specified location
  636. /// in the appropriate MemoryBuffer.
  637. const char *SourceManager::getCharacterData(SourceLocation SL,
  638. bool *Invalid) const {
  639. // Note that this is a hot function in the getSpelling() path, which is
  640. // heavily used by -E mode.
  641. std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL);
  642. // Note that calling 'getBuffer()' may lazily page in a source file.
  643. bool CharDataInvalid = false;
  644. const llvm::MemoryBuffer *Buffer
  645. = getSLocEntry(LocInfo.first).getFile().getContentCache()
  646. ->getBuffer(Diag, *this, SourceLocation(), &CharDataInvalid);
  647. if (Invalid)
  648. *Invalid = CharDataInvalid;
  649. return Buffer->getBufferStart() + (CharDataInvalid? 0 : LocInfo.second);
  650. }
  651. /// getColumnNumber - Return the column # for the specified file position.
  652. /// this is significantly cheaper to compute than the line number.
  653. unsigned SourceManager::getColumnNumber(FileID FID, unsigned FilePos,
  654. bool *Invalid) const {
  655. bool MyInvalid = false;
  656. const char *Buf = getBuffer(FID, &MyInvalid)->getBufferStart();
  657. if (Invalid)
  658. *Invalid = MyInvalid;
  659. if (MyInvalid)
  660. return 1;
  661. unsigned LineStart = FilePos;
  662. while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r')
  663. --LineStart;
  664. return FilePos-LineStart+1;
  665. }
  666. // isInvalid - Return the result of calling loc.isInvalid(), and
  667. // if Invalid is not null, set its value to same.
  668. static bool isInvalid(SourceLocation Loc, bool *Invalid) {
  669. bool MyInvalid = Loc.isInvalid();
  670. if (Invalid)
  671. *Invalid = MyInvalid;
  672. return MyInvalid;
  673. }
  674. unsigned SourceManager::getSpellingColumnNumber(SourceLocation Loc,
  675. bool *Invalid) const {
  676. if (isInvalid(Loc, Invalid)) return 0;
  677. std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
  678. return getColumnNumber(LocInfo.first, LocInfo.second, Invalid);
  679. }
  680. unsigned SourceManager::getInstantiationColumnNumber(SourceLocation Loc,
  681. bool *Invalid) const {
  682. if (isInvalid(Loc, Invalid)) return 0;
  683. std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
  684. return getColumnNumber(LocInfo.first, LocInfo.second, Invalid);
  685. }
  686. static LLVM_ATTRIBUTE_NOINLINE void
  687. ComputeLineNumbers(Diagnostic &Diag, ContentCache *FI,
  688. llvm::BumpPtrAllocator &Alloc,
  689. const SourceManager &SM, bool &Invalid);
  690. static void ComputeLineNumbers(Diagnostic &Diag, ContentCache *FI,
  691. llvm::BumpPtrAllocator &Alloc,
  692. const SourceManager &SM, bool &Invalid) {
  693. // Note that calling 'getBuffer()' may lazily page in the file.
  694. const MemoryBuffer *Buffer = FI->getBuffer(Diag, SM, SourceLocation(),
  695. &Invalid);
  696. if (Invalid)
  697. return;
  698. // Find the file offsets of all of the *physical* source lines. This does
  699. // not look at trigraphs, escaped newlines, or anything else tricky.
  700. std::vector<unsigned> LineOffsets;
  701. // Line #1 starts at char 0.
  702. LineOffsets.push_back(0);
  703. const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart();
  704. const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd();
  705. unsigned Offs = 0;
  706. while (1) {
  707. // Skip over the contents of the line.
  708. // TODO: Vectorize this? This is very performance sensitive for programs
  709. // with lots of diagnostics and in -E mode.
  710. const unsigned char *NextBuf = (const unsigned char *)Buf;
  711. while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0')
  712. ++NextBuf;
  713. Offs += NextBuf-Buf;
  714. Buf = NextBuf;
  715. if (Buf[0] == '\n' || Buf[0] == '\r') {
  716. // If this is \n\r or \r\n, skip both characters.
  717. if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1])
  718. ++Offs, ++Buf;
  719. ++Offs, ++Buf;
  720. LineOffsets.push_back(Offs);
  721. } else {
  722. // Otherwise, this is a null. If end of file, exit.
  723. if (Buf == End) break;
  724. // Otherwise, skip the null.
  725. ++Offs, ++Buf;
  726. }
  727. }
  728. // Copy the offsets into the FileInfo structure.
  729. FI->NumLines = LineOffsets.size();
  730. FI->SourceLineCache = Alloc.Allocate<unsigned>(LineOffsets.size());
  731. std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache);
  732. }
  733. /// getLineNumber - Given a SourceLocation, return the spelling line number
  734. /// for the position indicated. This requires building and caching a table of
  735. /// line offsets for the MemoryBuffer, so this is not cheap: use only when
  736. /// about to emit a diagnostic.
  737. unsigned SourceManager::getLineNumber(FileID FID, unsigned FilePos,
  738. bool *Invalid) const {
  739. ContentCache *Content;
  740. if (LastLineNoFileIDQuery == FID)
  741. Content = LastLineNoContentCache;
  742. else
  743. Content = const_cast<ContentCache*>(getSLocEntry(FID)
  744. .getFile().getContentCache());
  745. // If this is the first use of line information for this buffer, compute the
  746. /// SourceLineCache for it on demand.
  747. if (Content->SourceLineCache == 0) {
  748. bool MyInvalid = false;
  749. ComputeLineNumbers(Diag, Content, ContentCacheAlloc, *this, MyInvalid);
  750. if (Invalid)
  751. *Invalid = MyInvalid;
  752. if (MyInvalid)
  753. return 1;
  754. } else if (Invalid)
  755. *Invalid = false;
  756. // Okay, we know we have a line number table. Do a binary search to find the
  757. // line number that this character position lands on.
  758. unsigned *SourceLineCache = Content->SourceLineCache;
  759. unsigned *SourceLineCacheStart = SourceLineCache;
  760. unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines;
  761. unsigned QueriedFilePos = FilePos+1;
  762. // FIXME: I would like to be convinced that this code is worth being as
  763. // complicated as it is, binary search isn't that slow.
  764. //
  765. // If it is worth being optimized, then in my opinion it could be more
  766. // performant, simpler, and more obviously correct by just "galloping" outward
  767. // from the queried file position. In fact, this could be incorporated into a
  768. // generic algorithm such as lower_bound_with_hint.
  769. //
  770. // If someone gives me a test case where this matters, and I will do it! - DWD
  771. // If the previous query was to the same file, we know both the file pos from
  772. // that query and the line number returned. This allows us to narrow the
  773. // search space from the entire file to something near the match.
  774. if (LastLineNoFileIDQuery == FID) {
  775. if (QueriedFilePos >= LastLineNoFilePos) {
  776. // FIXME: Potential overflow?
  777. SourceLineCache = SourceLineCache+LastLineNoResult-1;
  778. // The query is likely to be nearby the previous one. Here we check to
  779. // see if it is within 5, 10 or 20 lines. It can be far away in cases
  780. // where big comment blocks and vertical whitespace eat up lines but
  781. // contribute no tokens.
  782. if (SourceLineCache+5 < SourceLineCacheEnd) {
  783. if (SourceLineCache[5] > QueriedFilePos)
  784. SourceLineCacheEnd = SourceLineCache+5;
  785. else if (SourceLineCache+10 < SourceLineCacheEnd) {
  786. if (SourceLineCache[10] > QueriedFilePos)
  787. SourceLineCacheEnd = SourceLineCache+10;
  788. else if (SourceLineCache+20 < SourceLineCacheEnd) {
  789. if (SourceLineCache[20] > QueriedFilePos)
  790. SourceLineCacheEnd = SourceLineCache+20;
  791. }
  792. }
  793. }
  794. } else {
  795. if (LastLineNoResult < Content->NumLines)
  796. SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1;
  797. }
  798. }
  799. // If the spread is large, do a "radix" test as our initial guess, based on
  800. // the assumption that lines average to approximately the same length.
  801. // NOTE: This is currently disabled, as it does not appear to be profitable in
  802. // initial measurements.
  803. if (0 && SourceLineCacheEnd-SourceLineCache > 20) {
  804. unsigned FileLen = Content->SourceLineCache[Content->NumLines-1];
  805. // Take a stab at guessing where it is.
  806. unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen;
  807. // Check for -10 and +10 lines.
  808. unsigned LowerBound = std::max(int(ApproxPos-10), 0);
  809. unsigned UpperBound = std::min(ApproxPos+10, FileLen);
  810. // If the computed lower bound is less than the query location, move it in.
  811. if (SourceLineCache < SourceLineCacheStart+LowerBound &&
  812. SourceLineCacheStart[LowerBound] < QueriedFilePos)
  813. SourceLineCache = SourceLineCacheStart+LowerBound;
  814. // If the computed upper bound is greater than the query location, move it.
  815. if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound &&
  816. SourceLineCacheStart[UpperBound] >= QueriedFilePos)
  817. SourceLineCacheEnd = SourceLineCacheStart+UpperBound;
  818. }
  819. unsigned *Pos
  820. = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos);
  821. unsigned LineNo = Pos-SourceLineCacheStart;
  822. LastLineNoFileIDQuery = FID;
  823. LastLineNoContentCache = Content;
  824. LastLineNoFilePos = QueriedFilePos;
  825. LastLineNoResult = LineNo;
  826. return LineNo;
  827. }
  828. unsigned SourceManager::getInstantiationLineNumber(SourceLocation Loc,
  829. bool *Invalid) const {
  830. if (isInvalid(Loc, Invalid)) return 0;
  831. std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
  832. return getLineNumber(LocInfo.first, LocInfo.second);
  833. }
  834. unsigned SourceManager::getSpellingLineNumber(SourceLocation Loc,
  835. bool *Invalid) const {
  836. if (isInvalid(Loc, Invalid)) return 0;
  837. std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
  838. return getLineNumber(LocInfo.first, LocInfo.second);
  839. }
  840. /// getFileCharacteristic - return the file characteristic of the specified
  841. /// source location, indicating whether this is a normal file, a system
  842. /// header, or an "implicit extern C" system header.
  843. ///
  844. /// This state can be modified with flags on GNU linemarker directives like:
  845. /// # 4 "foo.h" 3
  846. /// which changes all source locations in the current file after that to be
  847. /// considered to be from a system header.
  848. SrcMgr::CharacteristicKind
  849. SourceManager::getFileCharacteristic(SourceLocation Loc) const {
  850. assert(!Loc.isInvalid() && "Can't get file characteristic of invalid loc!");
  851. std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
  852. const SrcMgr::FileInfo &FI = getSLocEntry(LocInfo.first).getFile();
  853. // If there are no #line directives in this file, just return the whole-file
  854. // state.
  855. if (!FI.hasLineDirectives())
  856. return FI.getFileCharacteristic();
  857. assert(LineTable && "Can't have linetable entries without a LineTable!");
  858. // See if there is a #line directive before the location.
  859. const LineEntry *Entry =
  860. LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second);
  861. // If this is before the first line marker, use the file characteristic.
  862. if (!Entry)
  863. return FI.getFileCharacteristic();
  864. return Entry->FileKind;
  865. }
  866. /// Return the filename or buffer identifier of the buffer the location is in.
  867. /// Note that this name does not respect #line directives. Use getPresumedLoc
  868. /// for normal clients.
  869. const char *SourceManager::getBufferName(SourceLocation Loc,
  870. bool *Invalid) const {
  871. if (isInvalid(Loc, Invalid)) return "<invalid loc>";
  872. return getBuffer(getFileID(Loc), Invalid)->getBufferIdentifier();
  873. }
  874. /// getPresumedLoc - This method returns the "presumed" location of a
  875. /// SourceLocation specifies. A "presumed location" can be modified by #line
  876. /// or GNU line marker directives. This provides a view on the data that a
  877. /// user should see in diagnostics, for example.
  878. ///
  879. /// Note that a presumed location is always given as the instantiation point
  880. /// of an instantiation location, not at the spelling location.
  881. PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc) const {
  882. if (Loc.isInvalid()) return PresumedLoc();
  883. // Presumed locations are always for instantiation points.
  884. std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
  885. const SrcMgr::FileInfo &FI = getSLocEntry(LocInfo.first).getFile();
  886. const SrcMgr::ContentCache *C = FI.getContentCache();
  887. // To get the source name, first consult the FileEntry (if one exists)
  888. // before the MemBuffer as this will avoid unnecessarily paging in the
  889. // MemBuffer.
  890. const char *Filename;
  891. if (C->Entry)
  892. Filename = C->Entry->getName();
  893. else
  894. Filename = C->getBuffer(Diag, *this)->getBufferIdentifier();
  895. bool Invalid = false;
  896. unsigned LineNo = getLineNumber(LocInfo.first, LocInfo.second, &Invalid);
  897. if (Invalid)
  898. return PresumedLoc();
  899. unsigned ColNo = getColumnNumber(LocInfo.first, LocInfo.second, &Invalid);
  900. if (Invalid)
  901. return PresumedLoc();
  902. SourceLocation IncludeLoc = FI.getIncludeLoc();
  903. // If we have #line directives in this file, update and overwrite the physical
  904. // location info if appropriate.
  905. if (FI.hasLineDirectives()) {
  906. assert(LineTable && "Can't have linetable entries without a LineTable!");
  907. // See if there is a #line directive before this. If so, get it.
  908. if (const LineEntry *Entry =
  909. LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second)) {
  910. // If the LineEntry indicates a filename, use it.
  911. if (Entry->FilenameID != -1)
  912. Filename = LineTable->getFilename(Entry->FilenameID);
  913. // Use the line number specified by the LineEntry. This line number may
  914. // be multiple lines down from the line entry. Add the difference in
  915. // physical line numbers from the query point and the line marker to the
  916. // total.
  917. unsigned MarkerLineNo = getLineNumber(LocInfo.first, Entry->FileOffset);
  918. LineNo = Entry->LineNo + (LineNo-MarkerLineNo-1);
  919. // Note that column numbers are not molested by line markers.
  920. // Handle virtual #include manipulation.
  921. if (Entry->IncludeOffset) {
  922. IncludeLoc = getLocForStartOfFile(LocInfo.first);
  923. IncludeLoc = IncludeLoc.getFileLocWithOffset(Entry->IncludeOffset);
  924. }
  925. }
  926. }
  927. return PresumedLoc(Filename, LineNo, ColNo, IncludeLoc);
  928. }
  929. //===----------------------------------------------------------------------===//
  930. // Other miscellaneous methods.
  931. //===----------------------------------------------------------------------===//
  932. /// \brief Get the source location for the given file:line:col triplet.
  933. ///
  934. /// If the source file is included multiple times, the source location will
  935. /// be based upon the first inclusion.
  936. SourceLocation SourceManager::getLocation(const FileEntry *SourceFile,
  937. unsigned Line, unsigned Col) const {
  938. assert(SourceFile && "Null source file!");
  939. assert(Line && Col && "Line and column should start from 1!");
  940. fileinfo_iterator FI = FileInfos.find(SourceFile);
  941. if (FI == FileInfos.end())
  942. return SourceLocation();
  943. ContentCache *Content = FI->second;
  944. // If this is the first use of line information for this buffer, compute the
  945. /// SourceLineCache for it on demand.
  946. if (Content->SourceLineCache == 0) {
  947. bool MyInvalid = false;
  948. ComputeLineNumbers(Diag, Content, ContentCacheAlloc, *this, MyInvalid);
  949. if (MyInvalid)
  950. return SourceLocation();
  951. }
  952. // Find the first file ID that corresponds to the given file.
  953. FileID FirstFID;
  954. // First, check the main file ID, since it is common to look for a
  955. // location in the main file.
  956. if (!MainFileID.isInvalid()) {
  957. const SLocEntry &MainSLoc = getSLocEntry(MainFileID);
  958. if (MainSLoc.isFile() && MainSLoc.getFile().getContentCache() == Content)
  959. FirstFID = MainFileID;
  960. }
  961. if (FirstFID.isInvalid()) {
  962. // The location we're looking for isn't in the main file; look
  963. // through all of the source locations.
  964. for (unsigned I = 0, N = sloc_entry_size(); I != N; ++I) {
  965. const SLocEntry &SLoc = getSLocEntry(I);
  966. if (SLoc.isFile() && SLoc.getFile().getContentCache() == Content) {
  967. FirstFID = FileID::get(I);
  968. break;
  969. }
  970. }
  971. }
  972. if (FirstFID.isInvalid())
  973. return SourceLocation();
  974. if (Line > Content->NumLines) {
  975. unsigned Size = Content->getBuffer(Diag, *this)->getBufferSize();
  976. if (Size > 0)
  977. --Size;
  978. return getLocForStartOfFile(FirstFID).getFileLocWithOffset(Size);
  979. }
  980. unsigned FilePos = Content->SourceLineCache[Line - 1];
  981. const char *Buf = Content->getBuffer(Diag, *this)->getBufferStart() + FilePos;
  982. unsigned BufLength = Content->getBuffer(Diag, *this)->getBufferEnd() - Buf;
  983. unsigned i = 0;
  984. // Check that the given column is valid.
  985. while (i < BufLength-1 && i < Col-1 && Buf[i] != '\n' && Buf[i] != '\r')
  986. ++i;
  987. if (i < Col-1)
  988. return getLocForStartOfFile(FirstFID).getFileLocWithOffset(FilePos + i);
  989. return getLocForStartOfFile(FirstFID).getFileLocWithOffset(FilePos + Col - 1);
  990. }
  991. /// Given a decomposed source location, move it up the include/instantiation
  992. /// stack to the parent source location. If this is possible, return the
  993. /// decomposed version of the parent in Loc and return false. If Loc is the
  994. /// top-level entry, return true and don't modify it.
  995. static bool MoveUpIncludeHierarchy(std::pair<FileID, unsigned> &Loc,
  996. const SourceManager &SM) {
  997. SourceLocation UpperLoc;
  998. const SrcMgr::SLocEntry &Entry = SM.getSLocEntry(Loc.first);
  999. if (Entry.isInstantiation())
  1000. UpperLoc = Entry.getInstantiation().getInstantiationLocStart();
  1001. else
  1002. UpperLoc = Entry.getFile().getIncludeLoc();
  1003. if (UpperLoc.isInvalid())
  1004. return true; // We reached the top.
  1005. Loc = SM.getDecomposedLoc(UpperLoc);
  1006. return false;
  1007. }
  1008. /// \brief Determines the order of 2 source locations in the translation unit.
  1009. ///
  1010. /// \returns true if LHS source location comes before RHS, false otherwise.
  1011. bool SourceManager::isBeforeInTranslationUnit(SourceLocation LHS,
  1012. SourceLocation RHS) const {
  1013. assert(LHS.isValid() && RHS.isValid() && "Passed invalid source location!");
  1014. if (LHS == RHS)
  1015. return false;
  1016. std::pair<FileID, unsigned> LOffs = getDecomposedLoc(LHS);
  1017. std::pair<FileID, unsigned> ROffs = getDecomposedLoc(RHS);
  1018. // If the source locations are in the same file, just compare offsets.
  1019. if (LOffs.first == ROffs.first)
  1020. return LOffs.second < ROffs.second;
  1021. // If we are comparing a source location with multiple locations in the same
  1022. // file, we get a big win by caching the result.
  1023. if (IsBeforeInTUCache.isCacheValid(LOffs.first, ROffs.first))
  1024. return IsBeforeInTUCache.getCachedResult(LOffs.second, ROffs.second);
  1025. // Okay, we missed in the cache, start updating the cache for this query.
  1026. IsBeforeInTUCache.setQueryFIDs(LOffs.first, ROffs.first);
  1027. // "Traverse" the include/instantiation stacks of both locations and try to
  1028. // find a common "ancestor". FileIDs build a tree-like structure that
  1029. // reflects the #include hierarchy, and this algorithm needs to find the
  1030. // nearest common ancestor between the two locations. For example, if you
  1031. // have a.c that includes b.h and c.h, and are comparing a location in b.h to
  1032. // a location in c.h, we need to find that their nearest common ancestor is
  1033. // a.c, and compare the locations of the two #includes to find their relative
  1034. // ordering.
  1035. //
  1036. // SourceManager assigns FileIDs in order of parsing. This means that an
  1037. // includee always has a larger FileID than an includer. While you might
  1038. // think that we could just compare the FileID's here, that doesn't work to
  1039. // compare a point at the end of a.c with a point within c.h. Though c.h has
  1040. // a larger FileID, we have to compare the include point of c.h to the
  1041. // location in a.c.
  1042. //
  1043. // Despite not being able to directly compare FileID's, we can tell that a
  1044. // larger FileID is necessarily more deeply nested than a lower one and use
  1045. // this information to walk up the tree to the nearest common ancestor.
  1046. do {
  1047. // If LOffs is larger than ROffs, then LOffs must be more deeply nested than
  1048. // ROffs, walk up the #include chain.
  1049. if (LOffs.first.ID > ROffs.first.ID) {
  1050. if (MoveUpIncludeHierarchy(LOffs, *this))
  1051. break; // We reached the top.
  1052. } else {
  1053. // Otherwise, ROffs is larger than LOffs, so ROffs must be more deeply
  1054. // nested than LOffs, walk up the #include chain.
  1055. if (MoveUpIncludeHierarchy(ROffs, *this))
  1056. break; // We reached the top.
  1057. }
  1058. } while (LOffs.first != ROffs.first);
  1059. // If we exited because we found a nearest common ancestor, compare the
  1060. // locations within the common file and cache them.
  1061. if (LOffs.first == ROffs.first) {
  1062. IsBeforeInTUCache.setCommonLoc(LOffs.first, LOffs.second, ROffs.second);
  1063. return IsBeforeInTUCache.getCachedResult(LOffs.second, ROffs.second);
  1064. }
  1065. // There is no common ancestor, most probably because one location is in the
  1066. // predefines buffer or an AST file.
  1067. // FIXME: We should rearrange the external interface so this simply never
  1068. // happens; it can't conceptually happen. Also see PR5662.
  1069. IsBeforeInTUCache.setQueryFIDs(FileID(), FileID()); // Don't try caching.
  1070. // Zip both entries up to the top level record.
  1071. while (!MoveUpIncludeHierarchy(LOffs, *this)) /*empty*/;
  1072. while (!MoveUpIncludeHierarchy(ROffs, *this)) /*empty*/;
  1073. // If exactly one location is a memory buffer, assume it preceeds the other.
  1074. // Strip off macro instantation locations, going up to the top-level File
  1075. // SLocEntry.
  1076. bool LIsMB = getFileEntryForID(LOffs.first) == 0;
  1077. bool RIsMB = getFileEntryForID(ROffs.first) == 0;
  1078. if (LIsMB != RIsMB)
  1079. return LIsMB;
  1080. // Otherwise, just assume FileIDs were created in order.
  1081. return LOffs.first < ROffs.first;
  1082. }
  1083. /// PrintStats - Print statistics to stderr.
  1084. ///
  1085. void SourceManager::PrintStats() const {
  1086. llvm::errs() << "\n*** Source Manager Stats:\n";
  1087. llvm::errs() << FileInfos.size() << " files mapped, " << MemBufferInfos.size()
  1088. << " mem buffers mapped.\n";
  1089. llvm::errs() << SLocEntryTable.size() << " SLocEntry's allocated, "
  1090. << NextOffset << "B of Sloc address space used.\n";
  1091. unsigned NumLineNumsComputed = 0;
  1092. unsigned NumFileBytesMapped = 0;
  1093. for (fileinfo_iterator I = fileinfo_begin(), E = fileinfo_end(); I != E; ++I){
  1094. NumLineNumsComputed += I->second->SourceLineCache != 0;
  1095. NumFileBytesMapped += I->second->getSizeBytesMapped();
  1096. }
  1097. llvm::errs() << NumFileBytesMapped << " bytes of files mapped, "
  1098. << NumLineNumsComputed << " files with line #'s computed.\n";
  1099. llvm::errs() << "FileID scans: " << NumLinearScans << " linear, "
  1100. << NumBinaryProbes << " binary.\n";
  1101. }
  1102. ExternalSLocEntrySource::~ExternalSLocEntrySource() { }