SourceManager.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. //===--- SourceManager.h - Track and cache source files ---------*- C++ -*-===//
  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 defines the SourceManager interface.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CLANG_SOURCEMANAGER_H
  14. #define LLVM_CLANG_SOURCEMANAGER_H
  15. #include "clang/Basic/SourceLocation.h"
  16. #include "llvm/Support/Allocator.h"
  17. #include "llvm/System/DataTypes.h"
  18. #include "llvm/ADT/PointerIntPair.h"
  19. #include "llvm/ADT/PointerUnion.h"
  20. #include "llvm/ADT/DenseMap.h"
  21. #include <vector>
  22. #include <cassert>
  23. namespace llvm {
  24. class MemoryBuffer;
  25. class StringRef;
  26. }
  27. namespace clang {
  28. class Diagnostic;
  29. class SourceManager;
  30. class FileManager;
  31. class FileSystemOptions;
  32. class FileEntry;
  33. class LineTableInfo;
  34. /// SrcMgr - Public enums and private classes that are part of the
  35. /// SourceManager implementation.
  36. ///
  37. namespace SrcMgr {
  38. /// CharacteristicKind - This is used to represent whether a file or directory
  39. /// holds normal user code, system code, or system code which is implicitly
  40. /// 'extern "C"' in C++ mode. Entire directories can be tagged with this
  41. /// (this is maintained by DirectoryLookup and friends) as can specific
  42. /// FileIDInfos when a #pragma system_header is seen or various other cases.
  43. ///
  44. enum CharacteristicKind {
  45. C_User, C_System, C_ExternCSystem
  46. };
  47. /// ContentCache - One instance of this struct is kept for every file
  48. /// loaded or used. This object owns the MemoryBuffer object.
  49. class ContentCache {
  50. enum CCFlags {
  51. /// \brief Whether the buffer is invalid.
  52. InvalidFlag = 0x01,
  53. /// \brief Whether the buffer should not be freed on destruction.
  54. DoNotFreeFlag = 0x02
  55. };
  56. /// Buffer - The actual buffer containing the characters from the input
  57. /// file. This is owned by the ContentCache object.
  58. /// The bits indicate indicates whether the buffer is invalid.
  59. mutable llvm::PointerIntPair<const llvm::MemoryBuffer *, 2> Buffer;
  60. public:
  61. /// Reference to the file entry. This reference does not own
  62. /// the FileEntry object. It is possible for this to be NULL if
  63. /// the ContentCache encapsulates an imaginary text buffer.
  64. const FileEntry *Entry;
  65. /// SourceLineCache - A bump pointer allocated array of offsets for each
  66. /// source line. This is lazily computed. This is owned by the
  67. /// SourceManager BumpPointerAllocator object.
  68. unsigned *SourceLineCache;
  69. /// NumLines - The number of lines in this ContentCache. This is only valid
  70. /// if SourceLineCache is non-null.
  71. unsigned NumLines;
  72. /// getBuffer - Returns the memory buffer for the associated content.
  73. ///
  74. /// \param Diag Object through which diagnostics will be emitted it the
  75. /// buffer cannot be retrieved.
  76. ///
  77. /// \param Loc If specified, is the location that invalid file diagnostics
  78. /// will be emitted at.
  79. ///
  80. /// \param Invalid If non-NULL, will be set \c true if an error occurred.
  81. const llvm::MemoryBuffer *getBuffer(Diagnostic &Diag,
  82. const SourceManager &SM,
  83. SourceLocation Loc = SourceLocation(),
  84. bool *Invalid = 0) const;
  85. /// getSize - Returns the size of the content encapsulated by this
  86. /// ContentCache. This can be the size of the source file or the size of an
  87. /// arbitrary scratch buffer. If the ContentCache encapsulates a source
  88. /// file this size is retrieved from the file's FileEntry.
  89. unsigned getSize() const;
  90. /// getSizeBytesMapped - Returns the number of bytes actually mapped for
  91. /// this ContentCache. This can be 0 if the MemBuffer was not actually
  92. /// instantiated.
  93. unsigned getSizeBytesMapped() const;
  94. void setBuffer(const llvm::MemoryBuffer *B) {
  95. assert(!Buffer.getPointer() && "MemoryBuffer already set.");
  96. Buffer.setPointer(B);
  97. Buffer.setInt(false);
  98. }
  99. /// \brief Get the underlying buffer, returning NULL if the buffer is not
  100. /// yet available.
  101. const llvm::MemoryBuffer *getRawBuffer() const {
  102. return Buffer.getPointer();
  103. }
  104. /// \brief Replace the existing buffer (which will be deleted)
  105. /// with the given buffer.
  106. void replaceBuffer(const llvm::MemoryBuffer *B, bool DoNotFree = false);
  107. /// \brief Determine whether the buffer itself is invalid.
  108. bool isBufferInvalid() const {
  109. return Buffer.getInt() & InvalidFlag;
  110. }
  111. /// \brief Determine whether the buffer should be freed.
  112. bool shouldFreeBuffer() const {
  113. return (Buffer.getInt() & DoNotFreeFlag) == 0;
  114. }
  115. ContentCache(const FileEntry *Ent = 0)
  116. : Buffer(0, false), Entry(Ent), SourceLineCache(0), NumLines(0) {}
  117. ~ContentCache();
  118. /// The copy ctor does not allow copies where source object has either
  119. /// a non-NULL Buffer or SourceLineCache. Ownership of allocated memory
  120. /// is not transfered, so this is a logical error.
  121. ContentCache(const ContentCache &RHS)
  122. : Buffer(0, false), SourceLineCache(0)
  123. {
  124. Entry = RHS.Entry;
  125. assert (RHS.Buffer.getPointer() == 0 && RHS.SourceLineCache == 0
  126. && "Passed ContentCache object cannot own a buffer.");
  127. NumLines = RHS.NumLines;
  128. }
  129. private:
  130. // Disable assignments.
  131. ContentCache &operator=(const ContentCache& RHS);
  132. };
  133. /// FileInfo - Information about a FileID, basically just the logical file
  134. /// that it represents and include stack information.
  135. ///
  136. /// Each FileInfo has include stack information, indicating where it came
  137. /// from. This information encodes the #include chain that a token was
  138. /// instantiated from. The main include file has an invalid IncludeLoc.
  139. ///
  140. /// FileInfos contain a "ContentCache *", with the contents of the file.
  141. ///
  142. class FileInfo {
  143. /// IncludeLoc - The location of the #include that brought in this file.
  144. /// This is an invalid SLOC for the main file (top of the #include chain).
  145. unsigned IncludeLoc; // Really a SourceLocation
  146. /// Data - This contains the ContentCache* and the bits indicating the
  147. /// characteristic of the file and whether it has #line info, all bitmangled
  148. /// together.
  149. uintptr_t Data;
  150. public:
  151. /// get - Return a FileInfo object.
  152. static FileInfo get(SourceLocation IL, const ContentCache *Con,
  153. CharacteristicKind FileCharacter) {
  154. FileInfo X;
  155. X.IncludeLoc = IL.getRawEncoding();
  156. X.Data = (uintptr_t)Con;
  157. assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned");
  158. assert((unsigned)FileCharacter < 4 && "invalid file character");
  159. X.Data |= (unsigned)FileCharacter;
  160. return X;
  161. }
  162. SourceLocation getIncludeLoc() const {
  163. return SourceLocation::getFromRawEncoding(IncludeLoc);
  164. }
  165. const ContentCache* getContentCache() const {
  166. return reinterpret_cast<const ContentCache*>(Data & ~7UL);
  167. }
  168. /// getCharacteristic - Return whether this is a system header or not.
  169. CharacteristicKind getFileCharacteristic() const {
  170. return (CharacteristicKind)(Data & 3);
  171. }
  172. /// hasLineDirectives - Return true if this FileID has #line directives in
  173. /// it.
  174. bool hasLineDirectives() const { return (Data & 4) != 0; }
  175. /// setHasLineDirectives - Set the flag that indicates that this FileID has
  176. /// line table entries associated with it.
  177. void setHasLineDirectives() {
  178. Data |= 4;
  179. }
  180. };
  181. /// InstantiationInfo - Each InstantiationInfo encodes the Instantiation
  182. /// location - where the token was ultimately instantiated, and the
  183. /// SpellingLoc - where the actual character data for the token came from.
  184. class InstantiationInfo {
  185. // Really these are all SourceLocations.
  186. /// SpellingLoc - Where the spelling for the token can be found.
  187. unsigned SpellingLoc;
  188. /// InstantiationLocStart/InstantiationLocEnd - In a macro expansion, these
  189. /// indicate the start and end of the instantiation. In object-like macros,
  190. /// these will be the same. In a function-like macro instantiation, the
  191. /// start will be the identifier and the end will be the ')'.
  192. unsigned InstantiationLocStart, InstantiationLocEnd;
  193. public:
  194. SourceLocation getSpellingLoc() const {
  195. return SourceLocation::getFromRawEncoding(SpellingLoc);
  196. }
  197. SourceLocation getInstantiationLocStart() const {
  198. return SourceLocation::getFromRawEncoding(InstantiationLocStart);
  199. }
  200. SourceLocation getInstantiationLocEnd() const {
  201. return SourceLocation::getFromRawEncoding(InstantiationLocEnd);
  202. }
  203. std::pair<SourceLocation,SourceLocation> getInstantiationLocRange() const {
  204. return std::make_pair(getInstantiationLocStart(),
  205. getInstantiationLocEnd());
  206. }
  207. /// get - Return a InstantiationInfo for an expansion. IL specifies
  208. /// the instantiation location (where the macro is expanded), and SL
  209. /// specifies the spelling location (where the characters from the token
  210. /// come from). IL and PL can both refer to normal File SLocs or
  211. /// instantiation locations.
  212. static InstantiationInfo get(SourceLocation ILStart, SourceLocation ILEnd,
  213. SourceLocation SL) {
  214. InstantiationInfo X;
  215. X.SpellingLoc = SL.getRawEncoding();
  216. X.InstantiationLocStart = ILStart.getRawEncoding();
  217. X.InstantiationLocEnd = ILEnd.getRawEncoding();
  218. return X;
  219. }
  220. };
  221. /// SLocEntry - This is a discriminated union of FileInfo and
  222. /// InstantiationInfo. SourceManager keeps an array of these objects, and
  223. /// they are uniquely identified by the FileID datatype.
  224. class SLocEntry {
  225. unsigned Offset; // low bit is set for instantiation info.
  226. union {
  227. FileInfo File;
  228. InstantiationInfo Instantiation;
  229. };
  230. public:
  231. unsigned getOffset() const { return Offset >> 1; }
  232. bool isInstantiation() const { return Offset & 1; }
  233. bool isFile() const { return !isInstantiation(); }
  234. const FileInfo &getFile() const {
  235. assert(isFile() && "Not a file SLocEntry!");
  236. return File;
  237. }
  238. const InstantiationInfo &getInstantiation() const {
  239. assert(isInstantiation() && "Not an instantiation SLocEntry!");
  240. return Instantiation;
  241. }
  242. static SLocEntry get(unsigned Offset, const FileInfo &FI) {
  243. SLocEntry E;
  244. E.Offset = Offset << 1;
  245. E.File = FI;
  246. return E;
  247. }
  248. static SLocEntry get(unsigned Offset, const InstantiationInfo &II) {
  249. SLocEntry E;
  250. E.Offset = (Offset << 1) | 1;
  251. E.Instantiation = II;
  252. return E;
  253. }
  254. };
  255. } // end SrcMgr namespace.
  256. /// \brief External source of source location entries.
  257. class ExternalSLocEntrySource {
  258. public:
  259. virtual ~ExternalSLocEntrySource();
  260. /// \brief Read the source location entry with index ID.
  261. virtual void ReadSLocEntry(unsigned ID) = 0;
  262. };
  263. /// IsBeforeInTranslationUnitCache - This class holds the cache used by
  264. /// isBeforeInTranslationUnit. The cache structure is complex enough to be
  265. /// worth breaking out of SourceManager.
  266. class IsBeforeInTranslationUnitCache {
  267. /// L/R QueryFID - These are the FID's of the cached query. If these match up
  268. /// with a subsequent query, the result can be reused.
  269. FileID LQueryFID, RQueryFID;
  270. /// CommonFID - This is the file found in common between the two #include
  271. /// traces. It is the nearest common ancestor of the #include tree.
  272. FileID CommonFID;
  273. /// L/R CommonOffset - This is the offset of the previous query in CommonFID.
  274. /// Usually, this represents the location of the #include for QueryFID, but if
  275. /// LQueryFID is a parent of RQueryFID (or vise versa) then these can be a
  276. /// random token in the parent.
  277. unsigned LCommonOffset, RCommonOffset;
  278. public:
  279. /// isCacheValid - Return true if the currently cached values match up with
  280. /// the specified LHS/RHS query. If not, we can't use the cache.
  281. bool isCacheValid(FileID LHS, FileID RHS) const {
  282. return LQueryFID == LHS && RQueryFID == RHS;
  283. }
  284. /// getCachedResult - If the cache is valid, compute the result given the
  285. /// specified offsets in the LHS/RHS FID's.
  286. bool getCachedResult(unsigned LOffset, unsigned ROffset) const {
  287. // If one of the query files is the common file, use the offset. Otherwise,
  288. // use the #include loc in the common file.
  289. if (LQueryFID != CommonFID) LOffset = LCommonOffset;
  290. if (RQueryFID != CommonFID) ROffset = RCommonOffset;
  291. return LOffset < ROffset;
  292. }
  293. // Set up a new query.
  294. void setQueryFIDs(FileID LHS, FileID RHS) {
  295. LQueryFID = LHS;
  296. RQueryFID = RHS;
  297. }
  298. void setCommonLoc(FileID commonFID, unsigned lCommonOffset,
  299. unsigned rCommonOffset) {
  300. CommonFID = commonFID;
  301. LCommonOffset = lCommonOffset;
  302. RCommonOffset = rCommonOffset;
  303. }
  304. };
  305. /// SourceManager - This file handles loading and caching of source files into
  306. /// memory. This object owns the MemoryBuffer objects for all of the loaded
  307. /// files and assigns unique FileID's for each unique #include chain.
  308. ///
  309. /// The SourceManager can be queried for information about SourceLocation
  310. /// objects, turning them into either spelling or instantiation locations.
  311. /// Spelling locations represent where the bytes corresponding to a token came
  312. /// from and instantiation locations represent where the location is in the
  313. /// user's view. In the case of a macro expansion, for example, the spelling
  314. /// location indicates where the expanded token came from and the instantiation
  315. /// location specifies where it was expanded.
  316. class SourceManager {
  317. /// \brief Diagnostic object.
  318. Diagnostic &Diag;
  319. FileManager &FileMgr;
  320. const FileSystemOptions &FileSystemOpts;
  321. mutable llvm::BumpPtrAllocator ContentCacheAlloc;
  322. /// FileInfos - Memoized information about all of the files tracked by this
  323. /// SourceManager. This set allows us to merge ContentCache entries based
  324. /// on their FileEntry*. All ContentCache objects will thus have unique,
  325. /// non-null, FileEntry pointers.
  326. llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos;
  327. /// MemBufferInfos - Information about various memory buffers that we have
  328. /// read in. All FileEntry* within the stored ContentCache objects are NULL,
  329. /// as they do not refer to a file.
  330. std::vector<SrcMgr::ContentCache*> MemBufferInfos;
  331. /// SLocEntryTable - This is an array of SLocEntry's that we have created.
  332. /// FileID is an index into this vector. This array is sorted by the offset.
  333. std::vector<SrcMgr::SLocEntry> SLocEntryTable;
  334. /// NextOffset - This is the next available offset that a new SLocEntry can
  335. /// start at. It is SLocEntryTable.back().getOffset()+size of back() entry.
  336. unsigned NextOffset;
  337. /// \brief If source location entries are being lazily loaded from
  338. /// an external source, this vector indicates whether the Ith source
  339. /// location entry has already been loaded from the external storage.
  340. std::vector<bool> SLocEntryLoaded;
  341. /// \brief An external source for source location entries.
  342. ExternalSLocEntrySource *ExternalSLocEntries;
  343. /// LastFileIDLookup - This is a one-entry cache to speed up getFileID.
  344. /// LastFileIDLookup records the last FileID looked up or created, because it
  345. /// is very common to look up many tokens from the same file.
  346. mutable FileID LastFileIDLookup;
  347. /// LineTable - This holds information for #line directives. It is referenced
  348. /// by indices from SLocEntryTable.
  349. LineTableInfo *LineTable;
  350. /// LastLineNo - These ivars serve as a cache used in the getLineNumber
  351. /// method which is used to speedup getLineNumber calls to nearby locations.
  352. mutable FileID LastLineNoFileIDQuery;
  353. mutable SrcMgr::ContentCache *LastLineNoContentCache;
  354. mutable unsigned LastLineNoFilePos;
  355. mutable unsigned LastLineNoResult;
  356. /// MainFileID - The file ID for the main source file of the translation unit.
  357. FileID MainFileID;
  358. // Statistics for -print-stats.
  359. mutable unsigned NumLinearScans, NumBinaryProbes;
  360. // Cache results for the isBeforeInTranslationUnit method.
  361. mutable IsBeforeInTranslationUnitCache IsBeforeInTUCache;
  362. // SourceManager doesn't support copy construction.
  363. explicit SourceManager(const SourceManager&);
  364. void operator=(const SourceManager&);
  365. public:
  366. SourceManager(Diagnostic &Diag, FileManager &FileMgr,
  367. const FileSystemOptions &FSOpts)
  368. : Diag(Diag), FileMgr(FileMgr), FileSystemOpts(FSOpts),
  369. ExternalSLocEntries(0), LineTable(0), NumLinearScans(0),
  370. NumBinaryProbes(0) {
  371. clearIDTables();
  372. }
  373. ~SourceManager();
  374. void clearIDTables();
  375. Diagnostic &getDiagnostics() const { return Diag; }
  376. FileManager &getFileManager() const { return FileMgr; }
  377. const FileSystemOptions &getFileSystemOpts() const { return FileSystemOpts; }
  378. //===--------------------------------------------------------------------===//
  379. // MainFileID creation and querying methods.
  380. //===--------------------------------------------------------------------===//
  381. /// getMainFileID - Returns the FileID of the main source file.
  382. FileID getMainFileID() const { return MainFileID; }
  383. /// createMainFileID - Create the FileID for the main source file.
  384. FileID createMainFileID(const FileEntry *SourceFile) {
  385. assert(MainFileID.isInvalid() && "MainFileID already set!");
  386. MainFileID = createFileID(SourceFile, SourceLocation(), SrcMgr::C_User);
  387. return MainFileID;
  388. }
  389. //===--------------------------------------------------------------------===//
  390. // Methods to create new FileID's and instantiations.
  391. //===--------------------------------------------------------------------===//
  392. /// createFileID - Create a new FileID that represents the specified file
  393. /// being #included from the specified IncludePosition. This returns 0 on
  394. /// error and translates NULL into standard input.
  395. /// PreallocateID should be non-zero to specify which pre-allocated,
  396. /// lazily computed source location is being filled in by this operation.
  397. FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
  398. SrcMgr::CharacteristicKind FileCharacter,
  399. unsigned PreallocatedID = 0,
  400. unsigned Offset = 0) {
  401. const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile);
  402. assert(IR && "getOrCreateContentCache() cannot return NULL");
  403. return createFileID(IR, IncludePos, FileCharacter, PreallocatedID, Offset);
  404. }
  405. /// createFileIDForMemBuffer - Create a new FileID that represents the
  406. /// specified memory buffer. This does no caching of the buffer and takes
  407. /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once.
  408. FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer,
  409. unsigned PreallocatedID = 0,
  410. unsigned Offset = 0) {
  411. return createFileID(createMemBufferContentCache(Buffer), SourceLocation(),
  412. SrcMgr::C_User, PreallocatedID, Offset);
  413. }
  414. /// createMainFileIDForMembuffer - Create the FileID for a memory buffer
  415. /// that will represent the FileID for the main source. One example
  416. /// of when this would be used is when the main source is read from STDIN.
  417. FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) {
  418. assert(MainFileID.isInvalid() && "MainFileID already set!");
  419. MainFileID = createFileIDForMemBuffer(Buffer);
  420. return MainFileID;
  421. }
  422. /// createInstantiationLoc - Return a new SourceLocation that encodes the fact
  423. /// that a token at Loc should actually be referenced from InstantiationLoc.
  424. /// TokLength is the length of the token being instantiated.
  425. SourceLocation createInstantiationLoc(SourceLocation Loc,
  426. SourceLocation InstantiationLocStart,
  427. SourceLocation InstantiationLocEnd,
  428. unsigned TokLength,
  429. unsigned PreallocatedID = 0,
  430. unsigned Offset = 0);
  431. /// \brief Retrieve the memory buffer associated with the given file.
  432. ///
  433. /// \param Invalid If non-NULL, will be set \c true if an error
  434. /// occurs while retrieving the memory buffer.
  435. const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File,
  436. bool *Invalid = 0);
  437. /// \brief Override the contents of the given source file by providing an
  438. /// already-allocated buffer.
  439. ///
  440. /// \param SourceFile the source file whose contents will be overriden.
  441. ///
  442. /// \param Buffer the memory buffer whose contents will be used as the
  443. /// data in the given source file.
  444. ///
  445. /// \param DoNotFree If true, then the buffer will not be freed when the
  446. /// source manager is destroyed.
  447. void overrideFileContents(const FileEntry *SourceFile,
  448. const llvm::MemoryBuffer *Buffer,
  449. bool DoNotFree = false);
  450. //===--------------------------------------------------------------------===//
  451. // FileID manipulation methods.
  452. //===--------------------------------------------------------------------===//
  453. /// getBuffer - Return the buffer for the specified FileID. If there is an
  454. /// error opening this buffer the first time, this manufactures a temporary
  455. /// buffer and returns a non-empty error string.
  456. const llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc,
  457. bool *Invalid = 0) const {
  458. return getSLocEntry(FID).getFile().getContentCache()
  459. ->getBuffer(Diag, *this, Loc, Invalid);
  460. }
  461. const llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = 0) const {
  462. return getSLocEntry(FID).getFile().getContentCache()
  463. ->getBuffer(Diag, *this, SourceLocation(), Invalid);
  464. }
  465. /// getFileEntryForID - Returns the FileEntry record for the provided FileID.
  466. const FileEntry *getFileEntryForID(FileID FID) const {
  467. return getSLocEntry(FID).getFile().getContentCache()->Entry;
  468. }
  469. /// getBufferData - Return a StringRef to the source buffer data for the
  470. /// specified FileID.
  471. ///
  472. /// \param FID The file ID whose contents will be returned.
  473. /// \param Invalid If non-NULL, will be set true if an error occurred.
  474. llvm::StringRef getBufferData(FileID FID, bool *Invalid = 0) const;
  475. //===--------------------------------------------------------------------===//
  476. // SourceLocation manipulation methods.
  477. //===--------------------------------------------------------------------===//
  478. /// getFileID - Return the FileID for a SourceLocation. This is a very
  479. /// hot method that is used for all SourceManager queries that start with a
  480. /// SourceLocation object. It is responsible for finding the entry in
  481. /// SLocEntryTable which contains the specified location.
  482. ///
  483. FileID getFileID(SourceLocation SpellingLoc) const {
  484. unsigned SLocOffset = SpellingLoc.getOffset();
  485. // If our one-entry cache covers this offset, just return it.
  486. if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
  487. return LastFileIDLookup;
  488. return getFileIDSlow(SLocOffset);
  489. }
  490. /// getLocForStartOfFile - Return the source location corresponding to the
  491. /// first byte of the specified file.
  492. SourceLocation getLocForStartOfFile(FileID FID) const {
  493. assert(FID.ID < SLocEntryTable.size() && "FileID out of range");
  494. assert(getSLocEntry(FID).isFile() && "FileID is not a file");
  495. unsigned FileOffset = getSLocEntry(FID).getOffset();
  496. return SourceLocation::getFileLoc(FileOffset);
  497. }
  498. /// getInstantiationLoc - Given a SourceLocation object, return the
  499. /// instantiation location referenced by the ID.
  500. SourceLocation getInstantiationLoc(SourceLocation Loc) const {
  501. // Handle the non-mapped case inline, defer to out of line code to handle
  502. // instantiations.
  503. if (Loc.isFileID()) return Loc;
  504. return getInstantiationLocSlowCase(Loc);
  505. }
  506. /// getImmediateInstantiationRange - Loc is required to be an instantiation
  507. /// location. Return the start/end of the instantiation information.
  508. std::pair<SourceLocation,SourceLocation>
  509. getImmediateInstantiationRange(SourceLocation Loc) const;
  510. /// getInstantiationRange - Given a SourceLocation object, return the
  511. /// range of tokens covered by the instantiation in the ultimate file.
  512. std::pair<SourceLocation,SourceLocation>
  513. getInstantiationRange(SourceLocation Loc) const;
  514. /// getSpellingLoc - Given a SourceLocation object, return the spelling
  515. /// location referenced by the ID. This is the place where the characters
  516. /// that make up the lexed token can be found.
  517. SourceLocation getSpellingLoc(SourceLocation Loc) const {
  518. // Handle the non-mapped case inline, defer to out of line code to handle
  519. // instantiations.
  520. if (Loc.isFileID()) return Loc;
  521. return getSpellingLocSlowCase(Loc);
  522. }
  523. /// getImmediateSpellingLoc - Given a SourceLocation object, return the
  524. /// spelling location referenced by the ID. This is the first level down
  525. /// towards the place where the characters that make up the lexed token can be
  526. /// found. This should not generally be used by clients.
  527. SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const;
  528. /// getDecomposedLoc - Decompose the specified location into a raw FileID +
  529. /// Offset pair. The first element is the FileID, the second is the
  530. /// offset from the start of the buffer of the location.
  531. std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const {
  532. FileID FID = getFileID(Loc);
  533. return std::make_pair(FID, Loc.getOffset()-getSLocEntry(FID).getOffset());
  534. }
  535. /// getDecomposedInstantiationLoc - Decompose the specified location into a
  536. /// raw FileID + Offset pair. If the location is an instantiation record,
  537. /// walk through it until we find the final location instantiated.
  538. std::pair<FileID, unsigned>
  539. getDecomposedInstantiationLoc(SourceLocation Loc) const {
  540. FileID FID = getFileID(Loc);
  541. const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
  542. unsigned Offset = Loc.getOffset()-E->getOffset();
  543. if (Loc.isFileID())
  544. return std::make_pair(FID, Offset);
  545. return getDecomposedInstantiationLocSlowCase(E, Offset);
  546. }
  547. /// getDecomposedSpellingLoc - Decompose the specified location into a raw
  548. /// FileID + Offset pair. If the location is an instantiation record, walk
  549. /// through it until we find its spelling record.
  550. std::pair<FileID, unsigned>
  551. getDecomposedSpellingLoc(SourceLocation Loc) const {
  552. FileID FID = getFileID(Loc);
  553. const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
  554. unsigned Offset = Loc.getOffset()-E->getOffset();
  555. if (Loc.isFileID())
  556. return std::make_pair(FID, Offset);
  557. return getDecomposedSpellingLocSlowCase(E, Offset);
  558. }
  559. /// getFileOffset - This method returns the offset from the start
  560. /// of the file that the specified SourceLocation represents. This is not very
  561. /// meaningful for a macro ID.
  562. unsigned getFileOffset(SourceLocation SpellingLoc) const {
  563. return getDecomposedLoc(SpellingLoc).second;
  564. }
  565. //===--------------------------------------------------------------------===//
  566. // Queries about the code at a SourceLocation.
  567. //===--------------------------------------------------------------------===//
  568. /// getCharacterData - Return a pointer to the start of the specified location
  569. /// in the appropriate spelling MemoryBuffer.
  570. ///
  571. /// \param Invalid If non-NULL, will be set \c true if an error occurs.
  572. const char *getCharacterData(SourceLocation SL, bool *Invalid = 0) const;
  573. /// getColumnNumber - Return the column # for the specified file position.
  574. /// This is significantly cheaper to compute than the line number. This
  575. /// returns zero if the column number isn't known. This may only be called on
  576. /// a file sloc, so you must choose a spelling or instantiation location
  577. /// before calling this method.
  578. unsigned getColumnNumber(FileID FID, unsigned FilePos,
  579. bool *Invalid = 0) const;
  580. unsigned getSpellingColumnNumber(SourceLocation Loc,
  581. bool *Invalid = 0) const;
  582. unsigned getInstantiationColumnNumber(SourceLocation Loc,
  583. bool *Invalid = 0) const;
  584. /// getLineNumber - Given a SourceLocation, return the spelling line number
  585. /// for the position indicated. This requires building and caching a table of
  586. /// line offsets for the MemoryBuffer, so this is not cheap: use only when
  587. /// about to emit a diagnostic.
  588. unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = 0) const;
  589. unsigned getInstantiationLineNumber(SourceLocation Loc,
  590. bool *Invalid = 0) const;
  591. unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
  592. /// Return the filename or buffer identifier of the buffer the location is in.
  593. /// Note that this name does not respect #line directives. Use getPresumedLoc
  594. /// for normal clients.
  595. const char *getBufferName(SourceLocation Loc, bool *Invalid = 0) const;
  596. /// getFileCharacteristic - return the file characteristic of the specified
  597. /// source location, indicating whether this is a normal file, a system
  598. /// header, or an "implicit extern C" system header.
  599. ///
  600. /// This state can be modified with flags on GNU linemarker directives like:
  601. /// # 4 "foo.h" 3
  602. /// which changes all source locations in the current file after that to be
  603. /// considered to be from a system header.
  604. SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const;
  605. /// getPresumedLoc - This method returns the "presumed" location of a
  606. /// SourceLocation specifies. A "presumed location" can be modified by #line
  607. /// or GNU line marker directives. This provides a view on the data that a
  608. /// user should see in diagnostics, for example.
  609. ///
  610. /// Note that a presumed location is always given as the instantiation point
  611. /// of an instantiation location, not at the spelling location.
  612. PresumedLoc getPresumedLoc(SourceLocation Loc) const;
  613. /// isFromSameFile - Returns true if both SourceLocations correspond to
  614. /// the same file.
  615. bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
  616. return getFileID(Loc1) == getFileID(Loc2);
  617. }
  618. /// isFromMainFile - Returns true if the file of provided SourceLocation is
  619. /// the main file.
  620. bool isFromMainFile(SourceLocation Loc) const {
  621. return getFileID(Loc) == getMainFileID();
  622. }
  623. /// isInSystemHeader - Returns if a SourceLocation is in a system header.
  624. bool isInSystemHeader(SourceLocation Loc) const {
  625. return getFileCharacteristic(Loc) != SrcMgr::C_User;
  626. }
  627. /// isInExternCSystemHeader - Returns if a SourceLocation is in an "extern C"
  628. /// system header.
  629. bool isInExternCSystemHeader(SourceLocation Loc) const {
  630. return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem;
  631. }
  632. //===--------------------------------------------------------------------===//
  633. // Line Table Manipulation Routines
  634. //===--------------------------------------------------------------------===//
  635. /// getLineTableFilenameID - Return the uniqued ID for the specified filename.
  636. ///
  637. unsigned getLineTableFilenameID(const char *Ptr, unsigned Len);
  638. /// AddLineNote - Add a line note to the line table for the FileID and offset
  639. /// specified by Loc. If FilenameID is -1, it is considered to be
  640. /// unspecified.
  641. void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID);
  642. void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID,
  643. bool IsFileEntry, bool IsFileExit,
  644. bool IsSystemHeader, bool IsExternCHeader);
  645. /// \brief Determine if the source manager has a line table.
  646. bool hasLineTable() const { return LineTable != 0; }
  647. /// \brief Retrieve the stored line table.
  648. LineTableInfo &getLineTable();
  649. //===--------------------------------------------------------------------===//
  650. // Other miscellaneous methods.
  651. //===--------------------------------------------------------------------===//
  652. /// \brief Get the source location for the given file:line:col triplet.
  653. ///
  654. /// If the source file is included multiple times, the source location will
  655. /// be based upon the first inclusion.
  656. SourceLocation getLocation(const FileEntry *SourceFile,
  657. unsigned Line, unsigned Col) const;
  658. /// \brief Determines the order of 2 source locations in the translation unit.
  659. ///
  660. /// \returns true if LHS source location comes before RHS, false otherwise.
  661. bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const;
  662. // Iterators over FileInfos.
  663. typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>
  664. ::const_iterator fileinfo_iterator;
  665. fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); }
  666. fileinfo_iterator fileinfo_end() const { return FileInfos.end(); }
  667. bool hasFileInfo(const FileEntry *File) const {
  668. return FileInfos.find(File) != FileInfos.end();
  669. }
  670. /// PrintStats - Print statistics to stderr.
  671. ///
  672. void PrintStats() const;
  673. unsigned sloc_entry_size() const { return SLocEntryTable.size(); }
  674. // FIXME: Exposing this is a little gross; what we want is a good way
  675. // to iterate the entries that were not defined in an AST file (or
  676. // any other external source).
  677. unsigned sloc_loaded_entry_size() const { return SLocEntryLoaded.size(); }
  678. const SrcMgr::SLocEntry &getSLocEntry(unsigned ID) const {
  679. assert(ID < SLocEntryTable.size() && "Invalid id");
  680. if (ExternalSLocEntries &&
  681. ID < SLocEntryLoaded.size() &&
  682. !SLocEntryLoaded[ID])
  683. ExternalSLocEntries->ReadSLocEntry(ID);
  684. return SLocEntryTable[ID];
  685. }
  686. const SrcMgr::SLocEntry &getSLocEntry(FileID FID) const {
  687. return getSLocEntry(FID.ID);
  688. }
  689. unsigned getNextOffset() const { return NextOffset; }
  690. /// \brief Preallocate some number of source location entries, which
  691. /// will be loaded as needed from the given external source.
  692. void PreallocateSLocEntries(ExternalSLocEntrySource *Source,
  693. unsigned NumSLocEntries,
  694. unsigned NextOffset);
  695. /// \brief Clear out any preallocated source location entries that
  696. /// haven't already been loaded.
  697. void ClearPreallocatedSLocEntries();
  698. private:
  699. /// isOffsetInFileID - Return true if the specified FileID contains the
  700. /// specified SourceLocation offset. This is a very hot method.
  701. inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const {
  702. const SrcMgr::SLocEntry &Entry = getSLocEntry(FID);
  703. // If the entry is after the offset, it can't contain it.
  704. if (SLocOffset < Entry.getOffset()) return false;
  705. // If this is the last entry than it does. Otherwise, the entry after it
  706. // has to not include it.
  707. if (FID.ID+1 == SLocEntryTable.size()) return true;
  708. return SLocOffset < getSLocEntry(FileID::get(FID.ID+1)).getOffset();
  709. }
  710. /// createFileID - Create a new fileID for the specified ContentCache and
  711. /// include position. This works regardless of whether the ContentCache
  712. /// corresponds to a file or some other input source.
  713. FileID createFileID(const SrcMgr::ContentCache* File,
  714. SourceLocation IncludePos,
  715. SrcMgr::CharacteristicKind DirCharacter,
  716. unsigned PreallocatedID = 0,
  717. unsigned Offset = 0);
  718. const SrcMgr::ContentCache *
  719. getOrCreateContentCache(const FileEntry *SourceFile);
  720. /// createMemBufferContentCache - Create a new ContentCache for the specified
  721. /// memory buffer.
  722. const SrcMgr::ContentCache*
  723. createMemBufferContentCache(const llvm::MemoryBuffer *Buf);
  724. FileID getFileIDSlow(unsigned SLocOffset) const;
  725. SourceLocation getInstantiationLocSlowCase(SourceLocation Loc) const;
  726. SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;
  727. std::pair<FileID, unsigned>
  728. getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E,
  729. unsigned Offset) const;
  730. std::pair<FileID, unsigned>
  731. getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
  732. unsigned Offset) const;
  733. };
  734. } // end namespace clang
  735. #endif