SourceManager.cpp 51 KB

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