SourceManager.cpp 83 KB

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