SourceManager.h revision 1728762d5a8cfaf8d64385f47b311e84de1ae7a2
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- SourceManager.h - Track and cache source files ---------*- C++ -*-===// 25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// The LLVM Compiler Infrastructure 45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source 60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details. 75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// This file defines the SourceManager interface. 115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifndef LLVM_CLANG_SOURCEMANAGER_H 155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#define LLVM_CLANG_SOURCEMANAGER_H 165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 17d47d3b0cfeb7e8564ff77f48130fe63282b6d127Chris Lattner#include "clang/Basic/LLVM.h" 185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/SourceLocation.h" 190d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner#include "llvm/Support/Allocator.h" 2003013fa9a0bf1ef4b907f5fec006c8f4000fdd21Michael J. Spencer#include "llvm/Support/DataTypes.h" 21c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor#include "llvm/ADT/PointerIntPair.h" 22aea67dbd653a2dd6dd5cc2159279e81e855b2482Douglas Gregor#include "llvm/ADT/PointerUnion.h" 234f32786ac45210143654390177105eb749b614e9Ted Kremenek#include "llvm/ADT/IntrusiveRefCntPtr.h" 240d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner#include "llvm/ADT/DenseMap.h" 25f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek#include "llvm/Support/MemoryBuffer.h" 265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include <vector> 279dc62f044a6ba21f503bd56607d94b32704e7945Chris Lattner#include <cassert> 285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace clang { 301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 31aea67dbd653a2dd6dd5cc2159279e81e855b2482Douglas Gregorclass Diagnostic; 325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass SourceManager; 33099b4747042352f69184481a48508b599a8d3f73Ted Kremenekclass FileManager; 345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass FileEntry; 355b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattnerclass LineTableInfo; 36b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidisclass LangOptions; 37aea67dbd653a2dd6dd5cc2159279e81e855b2482Douglas Gregor 380b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner/// SrcMgr - Public enums and private classes that are part of the 390b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner/// SourceManager implementation. 405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace SrcMgr { 429d72851fec9e9c62570a027d42701562bbf29751Chris Lattner /// CharacteristicKind - This is used to represent whether a file or directory 430b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner /// holds normal user code, system code, or system code which is implicitly 440b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner /// 'extern "C"' in C++ mode. Entire directories can be tagged with this 450b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner /// (this is maintained by DirectoryLookup and friends) as can specific 46f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// FileInfos when a #pragma system_header is seen or various other cases. 470b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner /// 489d72851fec9e9c62570a027d42701562bbf29751Chris Lattner enum CharacteristicKind { 490b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner C_User, C_System, C_ExternCSystem 500b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner }; 511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 524710a8ea766b45079bf1c1dd36e29b59bb90829dDan Gohman /// ContentCache - One instance of this struct is kept for every file 5306a062dc784c609b75dca15fd97f468d0d846596Chris Lattner /// loaded or used. This object owns the MemoryBuffer object. 54c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek class ContentCache { 55f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor enum CCFlags { 56f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor /// \brief Whether the buffer is invalid. 57f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor InvalidFlag = 0x01, 58f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor /// \brief Whether the buffer should not be freed on destruction. 59f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor DoNotFreeFlag = 0x02 60f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor }; 61f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor 62c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek /// Buffer - The actual buffer containing the characters from the input 63c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek /// file. This is owned by the ContentCache object. 64f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor /// The bits indicate indicates whether the buffer is invalid. 65f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor mutable llvm::PointerIntPair<const llvm::MemoryBuffer *, 2> Buffer; 66c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek 67c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek public: 68b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis /// Reference to the file entry representing this ContentCache. 69b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis /// This reference does not own the FileEntry object. 70b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis /// It is possible for this to be NULL if 7178d85f53b093867bbb0123f016956178eea7343eTed Kremenek /// the ContentCache encapsulates an imaginary text buffer. 72b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis const FileEntry *OrigEntry; 73b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis 74b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis /// \brief References the file which the contents were actually loaded from. 75b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis /// Can be different from 'Entry' if we overridden the contents of one file 76b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis /// with the contents of another file. 77b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis const FileEntry *ContentsEntry; 781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 790d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner /// SourceLineCache - A bump pointer allocated array of offsets for each 800d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner /// source line. This is lazily computed. This is owned by the 810d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner /// SourceManager BumpPointerAllocator object. 8205816591ec488a933dfecc9ff9f3cbf3c32767c2Chris Lattner unsigned *SourceLineCache; 831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 84b6427f821de8cce1566fb6e755143ea0918d5543Ted Kremenek /// NumLines - The number of lines in this ContentCache. This is only valid 85b6427f821de8cce1566fb6e755143ea0918d5543Ted Kremenek /// if SourceLineCache is non-null. 865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer unsigned NumLines; 8710b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis 8836c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor /// getBuffer - Returns the memory buffer for the associated content. 8936c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor /// 90a92d7e7a55a35b28437103130904a6401bf35408Jonathan D. Turner /// \param Diag Object through which diagnostics will be emitted if the 9136c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor /// buffer cannot be retrieved. 9236c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor /// 93e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner /// \param Loc If specified, is the location that invalid file diagnostics 94e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner /// will be emitted at. 95e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner /// 9636c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor /// \param Invalid If non-NULL, will be set \c true if an error occurred. 97e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner const llvm::MemoryBuffer *getBuffer(Diagnostic &Diag, 98e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner const SourceManager &SM, 99e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner SourceLocation Loc = SourceLocation(), 10036c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor bool *Invalid = 0) const; 1011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 102c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek /// getSize - Returns the size of the content encapsulated by this 103c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek /// ContentCache. This can be the size of the source file or the size of an 104c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek /// arbitrary scratch buffer. If the ContentCache encapsulates a source 105c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek /// file this size is retrieved from the file's FileEntry. 106c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek unsigned getSize() const; 1071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 108c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek /// getSizeBytesMapped - Returns the number of bytes actually mapped for 109c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek /// this ContentCache. This can be 0 if the MemBuffer was not actually 110c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek /// instantiated. 111c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek unsigned getSizeBytesMapped() const; 112f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek 113f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek /// Returns the kind of memory used to back the memory buffer for 114f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek /// this content cache. This is used for performance analysis. 115f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek llvm::MemoryBuffer::BufferKind getMemoryBufferKind() const; 1161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11705816591ec488a933dfecc9ff9f3cbf3c32767c2Chris Lattner void setBuffer(const llvm::MemoryBuffer *B) { 118c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor assert(!Buffer.getPointer() && "MemoryBuffer already set."); 119c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor Buffer.setPointer(B); 120c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor Buffer.setInt(false); 121c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek } 122cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor 123cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor /// \brief Get the underlying buffer, returning NULL if the buffer is not 124cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor /// yet available. 125cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor const llvm::MemoryBuffer *getRawBuffer() const { 126cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor return Buffer.getPointer(); 127cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor } 1281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1292968442603b029949246467253eeac8139a5b6d8Douglas Gregor /// \brief Replace the existing buffer (which will be deleted) 1302968442603b029949246467253eeac8139a5b6d8Douglas Gregor /// with the given buffer. 131f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor void replaceBuffer(const llvm::MemoryBuffer *B, bool DoNotFree = false); 1322968442603b029949246467253eeac8139a5b6d8Douglas Gregor 133f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor /// \brief Determine whether the buffer itself is invalid. 134f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor bool isBufferInvalid() const { 135f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor return Buffer.getInt() & InvalidFlag; 136f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor } 137f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor 138f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor /// \brief Determine whether the buffer should be freed. 139f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor bool shouldFreeBuffer() const { 140f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor return (Buffer.getInt() & DoNotFreeFlag) == 0; 141f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor } 142f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor 1430d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner ContentCache(const FileEntry *Ent = 0) 144b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis : Buffer(0, false), OrigEntry(Ent), ContentsEntry(Ent), 145b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis SourceLineCache(0), NumLines(0) {} 146b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis 147b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis ContentCache(const FileEntry *Ent, const FileEntry *contentEnt) 148b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis : Buffer(0, false), OrigEntry(Ent), ContentsEntry(contentEnt), 149b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis SourceLineCache(0), NumLines(0) {} 15078d85f53b093867bbb0123f016956178eea7343eTed Kremenek 15178d85f53b093867bbb0123f016956178eea7343eTed Kremenek ~ContentCache(); 1521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1530d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek /// The copy ctor does not allow copies where source object has either 1540d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek /// a non-NULL Buffer or SourceLineCache. Ownership of allocated memory 155fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner /// is not transferred, so this is a logical error. 156c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor ContentCache(const ContentCache &RHS) 157c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor : Buffer(0, false), SourceLineCache(0) 158c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor { 159b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis OrigEntry = RHS.OrigEntry; 160b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis ContentsEntry = RHS.ContentsEntry; 1610d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek 162c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor assert (RHS.Buffer.getPointer() == 0 && RHS.SourceLineCache == 0 1630d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek && "Passed ContentCache object cannot own a buffer."); 1641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump NumLines = RHS.NumLines; 1660d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek } 1671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1680d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek private: 1690d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek // Disable assignments. 1701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump ContentCache &operator=(const ContentCache& RHS); 1711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump }; 1725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 173de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// FileInfo - Information about a FileID, basically just the logical file 174de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// that it represents and include stack information. 1755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// 176de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// Each FileInfo has include stack information, indicating where it came 177de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// from. This information encodes the #include chain that a token was 178de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// instantiated from. The main include file has an invalid IncludeLoc. 1795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// 180de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// FileInfos contain a "ContentCache *", with the contents of the file. 1815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// 182de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner class FileInfo { 1835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// IncludeLoc - The location of the #include that brought in this file. 184de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// This is an invalid SLOC for the main file (top of the #include chain). 185de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner unsigned IncludeLoc; // Really a SourceLocation 1861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1876e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner /// Data - This contains the ContentCache* and the bits indicating the 1886e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner /// characteristic of the file and whether it has #line info, all bitmangled 1896e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner /// together. 1906e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner uintptr_t Data; 19178d85f53b093867bbb0123f016956178eea7343eTed Kremenek public: 192de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// get - Return a FileInfo object. 193de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner static FileInfo get(SourceLocation IL, const ContentCache *Con, 194de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner CharacteristicKind FileCharacter) { 195de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner FileInfo X; 196de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner X.IncludeLoc = IL.getRawEncoding(); 1976e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner X.Data = (uintptr_t)Con; 19800282d6e1194655a2e89f940bd6fa8484b52e666Chris Lattner assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned"); 1996e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner assert((unsigned)FileCharacter < 4 && "invalid file character"); 2006e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner X.Data |= (unsigned)FileCharacter; 2015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return X; 2025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 2031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 204de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner SourceLocation getIncludeLoc() const { 205de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner return SourceLocation::getFromRawEncoding(IncludeLoc); 206de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner } 2076e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner const ContentCache* getContentCache() const { 20800282d6e1194655a2e89f940bd6fa8484b52e666Chris Lattner return reinterpret_cast<const ContentCache*>(Data & ~7UL); 2096e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner } 2101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2110b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner /// getCharacteristic - Return whether this is a system header or not. 2121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump CharacteristicKind getFileCharacteristic() const { 2136e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner return (CharacteristicKind)(Data & 3); 2140b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner } 215ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner 216ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner /// hasLineDirectives - Return true if this FileID has #line directives in 217ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner /// it. 218ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner bool hasLineDirectives() const { return (Data & 4) != 0; } 2191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 220ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner /// setHasLineDirectives - Set the flag that indicates that this FileID has 221ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner /// line table entries associated with it. 222ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner void setHasLineDirectives() { 223ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner Data |= 4; 224ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner } 2259dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner }; 2261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 22778df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth /// ExpansionInfo - Each ExpansionInfo encodes the expansion location - where 22878df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth /// the token was ultimately expanded, and the SpellingLoc - where the actual 22978df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth /// character data for the token came from. 23078df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth class ExpansionInfo { 23178df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth // Really these are all SourceLocations. 2321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 233e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner /// SpellingLoc - Where the spelling for the token can be found. 234e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner unsigned SpellingLoc; 2351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 23678df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth /// ExpansionLocStart/ExpansionLocEnd - In a macro expansion, these 23778df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth /// indicate the start and end of the expansion. In object-like macros, 23878df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth /// these will be the same. In a function-like macro instantiation, the 23978df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth /// start will be the identifier and the end will be the ')'. Finally, in 240c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth /// macro-argument instantitions, the end will be 'SourceLocation()', an 241c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth /// invalid location. 24278df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth unsigned ExpansionLocStart, ExpansionLocEnd; 243c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth 2449dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner public: 245de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner SourceLocation getSpellingLoc() const { 246de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner return SourceLocation::getFromRawEncoding(SpellingLoc); 247de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner } 24878df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth SourceLocation getExpansionLocStart() const { 24978df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth return SourceLocation::getFromRawEncoding(ExpansionLocStart); 250e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner } 25178df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth SourceLocation getExpansionLocEnd() const { 252c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth SourceLocation EndLoc = 25378df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth SourceLocation::getFromRawEncoding(ExpansionLocEnd); 25478df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth return EndLoc.isInvalid() ? getExpansionLocStart() : EndLoc; 255e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner } 2561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 25778df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth std::pair<SourceLocation,SourceLocation> getExpansionLocRange() const { 25878df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth return std::make_pair(getExpansionLocStart(), getExpansionLocEnd()); 259e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner } 2601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 26196d3589e523a04f4ff2058a7919226ce60696ae8Chandler Carruth bool isMacroArgExpansion() const { 262c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth // Note that this needs to return false for default constructed objects. 26378df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth return getExpansionLocStart().isValid() && 26478df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth SourceLocation::getFromRawEncoding(ExpansionLocEnd).isInvalid(); 265c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth } 266c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth 26778df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth /// create - Return a ExpansionInfo for an expansion. Start and End specify 26878df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth /// the expansion range (where the macro is expanded), and SpellingLoc 26978df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth /// specifies the spelling location (where the characters from the token 27078df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth /// come from). All three can refer to normal File SLocs or expansion 27178df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth /// locations. 27278df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth static ExpansionInfo create(SourceLocation SpellingLoc, 27378df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth SourceLocation Start, SourceLocation End) { 27478df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth ExpansionInfo X; 27578df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth X.SpellingLoc = SpellingLoc.getRawEncoding(); 27678df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth X.ExpansionLocStart = Start.getRawEncoding(); 27778df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth X.ExpansionLocEnd = End.getRawEncoding(); 2789dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner return X; 2795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 280c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth 28178df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth /// createForMacroArg - Return a special ExpansionInfo for the expansion of 28278df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth /// a macro argument into a function-like macro's body. ExpansionLoc 28378df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth /// specifies the expansion location (where the macro is expanded). This 28478df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth /// doesn't need to be a range because a macro is always expanded at 28578df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth /// a macro parameter reference, and macro parameters are always exactly 28678df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth /// one token. SpellingLoc specifies the spelling location (where the 28778df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth /// characters from the token come from). ExpansionLoc and SpellingLoc can 28878df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth /// both refer to normal File SLocs or expansion locations. 289c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth /// 290c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth /// Given the code: 291c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth /// \code 292c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth /// #define F(x) f(x) 293c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth /// F(42); 294c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth /// \endcode 295c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth /// 29678df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth /// When expanding '\c F(42)', the '\c x' would call this with an 29778df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth /// SpellingLoc pointing at '\c 42' anad an ExpansionLoc pointing at its 29878df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth /// location in the definition of '\c F'. 29978df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth static ExpansionInfo createForMacroArg(SourceLocation SpellingLoc, 30078df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth SourceLocation ExpansionLoc) { 301c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth // We store an intentionally invalid source location for the end of the 30278df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth // expansion range to mark that this is a macro argument ion rather than 30378df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth // a normal one. 30478df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth return create(SpellingLoc, ExpansionLoc, SourceLocation()); 305c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth } 306de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner }; 3071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 308de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// SLocEntry - This is a discriminated union of FileInfo and 30978df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth /// ExpansionInfo. SourceManager keeps an array of these objects, and 310de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// they are uniquely identified by the FileID datatype. 311de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner class SLocEntry { 312de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner unsigned Offset; // low bit is set for instantiation info. 313de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner union { 314de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner FileInfo File; 3151728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth ExpansionInfo Expansion; 316de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner }; 317de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner public: 318de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner unsigned getOffset() const { return Offset >> 1; } 3191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3201728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth bool isExpansion() const { return Offset & 1; } 3211728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth bool isFile() const { return !isExpansion(); } 3221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 323de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner const FileInfo &getFile() const { 324de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner assert(isFile() && "Not a file SLocEntry!"); 325de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner return File; 326de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner } 327de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner 3281728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth const ExpansionInfo &getExpansion() const { 3291728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth assert(isExpansion() && "Not a macro expansion SLocEntry!"); 3301728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth return Expansion; 331de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner } 3321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 333de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner static SLocEntry get(unsigned Offset, const FileInfo &FI) { 334de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner SLocEntry E; 335de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner E.Offset = Offset << 1; 336de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner E.File = FI; 337de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner return E; 338de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner } 339de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner 34078df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth static SLocEntry get(unsigned Offset, const ExpansionInfo &Expansion) { 341de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner SLocEntry E; 342de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner E.Offset = (Offset << 1) | 1; 3431728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth E.Expansion = Expansion; 344de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner return E; 345de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner } 3465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer }; 3475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} // end SrcMgr namespace. 3487f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor 3497f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor/// \brief External source of source location entries. 3507f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregorclass ExternalSLocEntrySource { 3517f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregorpublic: 3527f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor virtual ~ExternalSLocEntrySource(); 3537f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor 354f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// \brief Read the source location entry with index ID, which will always be 355f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// less than -1. 356e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor /// 357e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor /// \returns true if an error occurred that prevented the source-location 358e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor /// entry from being loaded. 359f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor virtual bool ReadSLocEntry(int ID) = 0; 3607f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor}; 361dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner 362dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner 363dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner/// IsBeforeInTranslationUnitCache - This class holds the cache used by 364dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner/// isBeforeInTranslationUnit. The cache structure is complex enough to be 365dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner/// worth breaking out of SourceManager. 366dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattnerclass IsBeforeInTranslationUnitCache { 367dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner /// L/R QueryFID - These are the FID's of the cached query. If these match up 368dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner /// with a subsequent query, the result can be reused. 369dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner FileID LQueryFID, RQueryFID; 370dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner 371dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner /// CommonFID - This is the file found in common between the two #include 372dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner /// traces. It is the nearest common ancestor of the #include tree. 373dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner FileID CommonFID; 374dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner 375dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner /// L/R CommonOffset - This is the offset of the previous query in CommonFID. 376dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner /// Usually, this represents the location of the #include for QueryFID, but if 377dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner /// LQueryFID is a parent of RQueryFID (or vise versa) then these can be a 378dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner /// random token in the parent. 379dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner unsigned LCommonOffset, RCommonOffset; 380dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattnerpublic: 381dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner 382dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner /// isCacheValid - Return true if the currently cached values match up with 383dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner /// the specified LHS/RHS query. If not, we can't use the cache. 384dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner bool isCacheValid(FileID LHS, FileID RHS) const { 385dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner return LQueryFID == LHS && RQueryFID == RHS; 386dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner } 387dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner 388dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner /// getCachedResult - If the cache is valid, compute the result given the 389dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner /// specified offsets in the LHS/RHS FID's. 390dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner bool getCachedResult(unsigned LOffset, unsigned ROffset) const { 391dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner // If one of the query files is the common file, use the offset. Otherwise, 392dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner // use the #include loc in the common file. 393dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner if (LQueryFID != CommonFID) LOffset = LCommonOffset; 394dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner if (RQueryFID != CommonFID) ROffset = RCommonOffset; 395dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner return LOffset < ROffset; 396dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner } 397dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner 398dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner // Set up a new query. 399dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner void setQueryFIDs(FileID LHS, FileID RHS) { 400dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner LQueryFID = LHS; 401dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner RQueryFID = RHS; 402dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner } 403dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner 404dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner void setCommonLoc(FileID commonFID, unsigned lCommonOffset, 405dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner unsigned rCommonOffset) { 406dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner CommonFID = commonFID; 407dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner LCommonOffset = lCommonOffset; 408dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner RCommonOffset = rCommonOffset; 409dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner } 410dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner 411dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner}; 4127f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor 413f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor/// \brief This class handles loading and caching of source files into memory. 414f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor/// 415f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor/// This object owns the MemoryBuffer objects for all of the loaded 4165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// files and assigns unique FileID's for each unique #include chain. 4175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 4185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// The SourceManager can be queried for information about SourceLocation 419f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner/// objects, turning them into either spelling or instantiation locations. 420f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner/// Spelling locations represent where the bytes corresponding to a token came 421f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner/// from and instantiation locations represent where the location is in the 422f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner/// user's view. In the case of a macro expansion, for example, the spelling 423e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor/// location indicates where the expanded token came from and the instantiation 424f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner/// location specifies where it was expanded. 4254f32786ac45210143654390177105eb749b614e9Ted Kremenekclass SourceManager : public llvm::RefCountedBase<SourceManager> { 426f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor /// \brief Diagnostic object. 427f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor Diagnostic &Diag; 428389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis 429389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis FileManager &FileMgr; 430389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis 4310d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner mutable llvm::BumpPtrAllocator ContentCacheAlloc; 4321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// FileInfos - Memoized information about all of the files tracked by this 4340d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek /// SourceManager. This set allows us to merge ContentCache entries based 4350d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek /// on their FileEntry*. All ContentCache objects will thus have unique, 4361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump /// non-null, FileEntry pointers. 4370d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos; 4381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 439299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis /// \brief True if the ContentCache for files that are overriden by other 440299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis /// files, should report the original file name. Defaults to true. 441299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis bool OverridenFilesKeepOriginalName; 442299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis 443b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis /// \brief Files that have been overriden with the contents from another file. 444b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis llvm::DenseMap<const FileEntry *, const FileEntry *> OverriddenFiles; 445b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis 4465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// MemBufferInfos - Information about various memory buffers that we have 4470d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner /// read in. All FileEntry* within the stored ContentCache objects are NULL, 4480d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner /// as they do not refer to a file. 4490d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner std::vector<SrcMgr::ContentCache*> MemBufferInfos; 4501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 451f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// \brief The table of SLocEntries that are local to this module. 452f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// 453f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// Positive FileIDs are indexes into this table. Entry 0 indicates an invalid 454f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// instantiation. 455f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor std::vector<SrcMgr::SLocEntry> LocalSLocEntryTable; 456f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor 457f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// \brief The table of SLocEntries that are loaded from other modules. 458f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// 459f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// Negative FileIDs are indexes into this table. To get from ID to an index, 460f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// use (-ID - 2). 461f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor std::vector<SrcMgr::SLocEntry> LoadedSLocEntryTable; 4627f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor 463f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// \brief The starting offset of the next local SLocEntry. 464f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// 465f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// This is LocalSLocEntryTable.back().Offset + the size of that entry. 466f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor unsigned NextLocalOffset; 467f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor 468f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// \brief The starting offset of the latest batch of loaded SLocEntries. 469f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// 470f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// This is LoadedSLocEntryTable.back().Offset, except that that entry might 471f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// not have been loaded, so that value would be unknown. 472f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor unsigned CurrentLoadedOffset; 473f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor 474f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// \brief A bitmap that indicates whether the entries of LoadedSLocEntryTable 475f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// have already been loaded from the external source. 476f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// 477f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// Same indexing as LoadedSLocEntryTable. 4787f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor std::vector<bool> SLocEntryLoaded; 4797f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor 4807f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor /// \brief An external source for source location entries. 4817f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor ExternalSLocEntrySource *ExternalSLocEntries; 4827f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor 483de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// LastFileIDLookup - This is a one-entry cache to speed up getFileID. 484de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// LastFileIDLookup records the last FileID looked up or created, because it 485de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// is very common to look up many tokens from the same file. 486de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner mutable FileID LastFileIDLookup; 4871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4885b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner /// LineTable - This holds information for #line directives. It is referenced 4895b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner /// by indices from SLocEntryTable. 4905b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner LineTableInfo *LineTable; 4911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4925e36a7a89da5d69ece23fc8228624a053f75c645Chris Lattner /// LastLineNo - These ivars serve as a cache used in the getLineNumber 4935e36a7a89da5d69ece23fc8228624a053f75c645Chris Lattner /// method which is used to speedup getLineNumber calls to nearby locations. 4942b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner mutable FileID LastLineNoFileIDQuery; 495f812a45dd93634c9300ed5533bd26b56374714a1Chris Lattner mutable SrcMgr::ContentCache *LastLineNoContentCache; 496f812a45dd93634c9300ed5533bd26b56374714a1Chris Lattner mutable unsigned LastLineNoFilePos; 497f812a45dd93634c9300ed5533bd26b56374714a1Chris Lattner mutable unsigned LastLineNoResult; 4981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 49976edd0e4ae0592a7225d50d0bad6732ac64dca2aTed Kremenek /// MainFileID - The file ID for the main source file of the translation unit. 5002b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner FileID MainFileID; 50149c1f4aa2a6c360d25d605004ec3c4affd62db77Steve Naroff 502de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner // Statistics for -print-stats. 503de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner mutable unsigned NumLinearScans, NumBinaryProbes; 5041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5052aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis // Cache results for the isBeforeInTranslationUnit method. 506dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner mutable IsBeforeInTranslationUnitCache IsBeforeInTUCache; 5071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 508e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor // Cache for the "fake" buffer used for error-recovery purposes. 509e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor mutable llvm::MemoryBuffer *FakeBufferForRecovery; 510e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor 51149c1f4aa2a6c360d25d605004ec3c4affd62db77Steve Naroff // SourceManager doesn't support copy construction. 51249c1f4aa2a6c360d25d605004ec3c4affd62db77Steve Naroff explicit SourceManager(const SourceManager&); 5131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump void operator=(const SourceManager&); 5145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic: 51539b49bcaaddb1049234fca9500c0ac02c088e23dChris Lattner SourceManager(Diagnostic &Diag, FileManager &FileMgr); 5165b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner ~SourceManager(); 5171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5185b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner void clearIDTables(); 5191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 52078a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios Kyrtzidis Diagnostic &getDiagnostics() const { return Diag; } 52178a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios Kyrtzidis 522389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis FileManager &getFileManager() const { return FileMgr; } 523389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis 524299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis /// \brief Set true if the SourceManager should report the original file name 525299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis /// for contents of files that were overriden by other files.Defaults to true. 526299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis void setOverridenFilesKeepOriginalName(bool value) { 527299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis OverridenFilesKeepOriginalName = value; 528299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis } 529299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis 530f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// createMainFileIDForMembuffer - Create the FileID for a memory buffer 531f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// that will represent the FileID for the main source. One example 532f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// of when this would be used is when the main source is read from STDIN. 533f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) { 534f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor assert(MainFileID.isInvalid() && "MainFileID already set!"); 535f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor MainFileID = createFileIDForMemBuffer(Buffer); 536f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor return MainFileID; 537f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor } 538f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor 53906a062dc784c609b75dca15fd97f468d0d846596Chris Lattner //===--------------------------------------------------------------------===// 54006a062dc784c609b75dca15fd97f468d0d846596Chris Lattner // MainFileID creation and querying methods. 54106a062dc784c609b75dca15fd97f468d0d846596Chris Lattner //===--------------------------------------------------------------------===// 54206a062dc784c609b75dca15fd97f468d0d846596Chris Lattner 54376edd0e4ae0592a7225d50d0bad6732ac64dca2aTed Kremenek /// getMainFileID - Returns the FileID of the main source file. 5442b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner FileID getMainFileID() const { return MainFileID; } 5451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 54606a062dc784c609b75dca15fd97f468d0d846596Chris Lattner /// createMainFileID - Create the FileID for the main source file. 547f155dfa4e284be7dca8f1abf002476b2aaee8546Dan Gohman FileID createMainFileID(const FileEntry *SourceFile) { 54806a062dc784c609b75dca15fd97f468d0d846596Chris Lattner assert(MainFileID.isInvalid() && "MainFileID already set!"); 549f155dfa4e284be7dca8f1abf002476b2aaee8546Dan Gohman MainFileID = createFileID(SourceFile, SourceLocation(), SrcMgr::C_User); 55006a062dc784c609b75dca15fd97f468d0d846596Chris Lattner return MainFileID; 55106a062dc784c609b75dca15fd97f468d0d846596Chris Lattner } 5521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 553414cb64f09ce48a36377458ce5e5a90c3ad41d00Douglas Gregor /// \brief Set the file ID for the precompiled preamble, which is also the 554414cb64f09ce48a36377458ce5e5a90c3ad41d00Douglas Gregor /// main file. 555414cb64f09ce48a36377458ce5e5a90c3ad41d00Douglas Gregor void SetPreambleFileID(FileID Preamble) { 556414cb64f09ce48a36377458ce5e5a90c3ad41d00Douglas Gregor assert(MainFileID.isInvalid() && "MainFileID already set!"); 557414cb64f09ce48a36377458ce5e5a90c3ad41d00Douglas Gregor MainFileID = Preamble; 558414cb64f09ce48a36377458ce5e5a90c3ad41d00Douglas Gregor } 559414cb64f09ce48a36377458ce5e5a90c3ad41d00Douglas Gregor 56006a062dc784c609b75dca15fd97f468d0d846596Chris Lattner //===--------------------------------------------------------------------===// 561de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner // Methods to create new FileID's and instantiations. 56206a062dc784c609b75dca15fd97f468d0d846596Chris Lattner //===--------------------------------------------------------------------===// 5631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// createFileID - Create a new FileID that represents the specified file 565d57b7ff9bebc4c45f325fc1be6f238cfcd4c3732Peter Collingbourne /// being #included from the specified IncludePosition. This translates NULL 566d57b7ff9bebc4c45f325fc1be6f238cfcd4c3732Peter Collingbourne /// into standard input. 5672b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos, 5687f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor SrcMgr::CharacteristicKind FileCharacter, 569f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor int LoadedID = 0, unsigned LoadedOffset = 0) { 570de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile); 5710d06e998910934e5ef070f53f4c272e7c6b846c6Dan Gohman assert(IR && "getOrCreateContentCache() cannot return NULL"); 572f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor return createFileID(IR, IncludePos, FileCharacter, LoadedID, LoadedOffset); 5735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 5741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// createFileIDForMemBuffer - Create a new FileID that represents the 5765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// specified memory buffer. This does no caching of the buffer and takes 5775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once. 5787f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer, 579f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor int LoadedID = 0, unsigned LoadedOffset = 0) { 5807bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber return createFileID(createMemBufferContentCache(Buffer), SourceLocation(), 581f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor SrcMgr::C_User, LoadedID, LoadedOffset); 5821036b68525f39cb69ac22c679ed440acd8392a16Ted Kremenek } 58306a062dc784c609b75dca15fd97f468d0d846596Chris Lattner 584bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth /// createMacroArgExpansionLoc - Return a new SourceLocation that encodes the 585bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth /// fact that a token from SpellingLoc should actually be referenced from 586bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth /// ExpansionLoc, and that it represents the instantiation of a macro 587c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth /// argument into the function-like macro body. 588bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth SourceLocation createMacroArgExpansionLoc(SourceLocation Loc, 589bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth SourceLocation ExpansionLoc, 590bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth unsigned TokLength); 591c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth 592bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth /// createExpansionLoc - Return a new SourceLocation that encodes the fact 593c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth /// that a token from SpellingLoc should actually be referenced from 594bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth /// ExpansionLoc. 595bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth SourceLocation createExpansionLoc(SourceLocation Loc, 596bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth SourceLocation ExpansionLocStart, 597bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth SourceLocation ExpansionLocEnd, 598bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth unsigned TokLength, 599bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth int LoadedID = 0, 600bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth unsigned LoadedOffset = 0); 6011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6022968442603b029949246467253eeac8139a5b6d8Douglas Gregor /// \brief Retrieve the memory buffer associated with the given file. 60350f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor /// 60450f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor /// \param Invalid If non-NULL, will be set \c true if an error 60550f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor /// occurs while retrieving the memory buffer. 60650f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File, 60750f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor bool *Invalid = 0); 6082968442603b029949246467253eeac8139a5b6d8Douglas Gregor 6092968442603b029949246467253eeac8139a5b6d8Douglas Gregor /// \brief Override the contents of the given source file by providing an 6102968442603b029949246467253eeac8139a5b6d8Douglas Gregor /// already-allocated buffer. 6112968442603b029949246467253eeac8139a5b6d8Douglas Gregor /// 612afbf5f810652f04c0bba235c0fa079885fb3caa8Dan Gohman /// \param SourceFile the source file whose contents will be overriden. 6132968442603b029949246467253eeac8139a5b6d8Douglas Gregor /// 6142968442603b029949246467253eeac8139a5b6d8Douglas Gregor /// \param Buffer the memory buffer whose contents will be used as the 6152968442603b029949246467253eeac8139a5b6d8Douglas Gregor /// data in the given source file. 6162968442603b029949246467253eeac8139a5b6d8Douglas Gregor /// 617f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor /// \param DoNotFree If true, then the buffer will not be freed when the 618f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor /// source manager is destroyed. 6190d06e998910934e5ef070f53f4c272e7c6b846c6Dan Gohman void overrideFileContents(const FileEntry *SourceFile, 620f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor const llvm::MemoryBuffer *Buffer, 621f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor bool DoNotFree = false); 6222968442603b029949246467253eeac8139a5b6d8Douglas Gregor 623b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis /// \brief Override the the given source file with another one. 624b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis /// 625b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis /// \param SourceFile the source file which will be overriden. 626b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis /// 627b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis /// \param NewFile the file whose contents will be used as the 628b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis /// data instead of the contents of the given source file. 629b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis void overrideFileContents(const FileEntry *SourceFile, 630b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis const FileEntry *NewFile); 631b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis 63206a062dc784c609b75dca15fd97f468d0d846596Chris Lattner //===--------------------------------------------------------------------===// 63306a062dc784c609b75dca15fd97f468d0d846596Chris Lattner // FileID manipulation methods. 63406a062dc784c609b75dca15fd97f468d0d846596Chris Lattner //===--------------------------------------------------------------------===// 6351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6362ffb14f004affc363d86d6c7c63c356190db3679Daniel Dunbar /// getBuffer - Return the buffer for the specified FileID. If there is an 6372ffb14f004affc363d86d6c7c63c356190db3679Daniel Dunbar /// error opening this buffer the first time, this manufactures a temporary 6382ffb14f004affc363d86d6c7c63c356190db3679Daniel Dunbar /// buffer and returns a non-empty error string. 639e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner const llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc, 640e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner bool *Invalid = 0) const { 641e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor bool MyInvalid = false; 642e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid); 643e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor if (MyInvalid || !Entry.isFile()) { 644e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor if (Invalid) 645e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor *Invalid = true; 646e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor 647e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor return getFakeBufferForRecovery(); 648e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor } 649e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor 650e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor return Entry.getFile().getContentCache()->getBuffer(Diag, *this, Loc, 651e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor Invalid); 65206a062dc784c609b75dca15fd97f468d0d846596Chris Lattner } 6531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 654e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner const llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = 0) const { 655e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor bool MyInvalid = false; 656e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid); 657e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor if (MyInvalid || !Entry.isFile()) { 658e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor if (Invalid) 659e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor *Invalid = true; 660e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor 661e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor return getFakeBufferForRecovery(); 662e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor } 663e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor 664e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor return Entry.getFile().getContentCache()->getBuffer(Diag, *this, 665e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor SourceLocation(), 666e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor Invalid); 667e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner } 668e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner 66906a062dc784c609b75dca15fd97f468d0d846596Chris Lattner /// getFileEntryForID - Returns the FileEntry record for the provided FileID. 67006a062dc784c609b75dca15fd97f468d0d846596Chris Lattner const FileEntry *getFileEntryForID(FileID FID) const { 671e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor bool MyInvalid = false; 672e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid); 673e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor if (MyInvalid || !Entry.isFile()) 674e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor return 0; 675e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor 676e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor return Entry.getFile().getContentCache()->OrigEntry; 67706a062dc784c609b75dca15fd97f468d0d846596Chris Lattner } 6781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6799d5a165d301cc9df68631e624322dd2a962f65b3Ted Kremenek /// Returns the FileEntry record for the provided SLocEntry. 6809d5a165d301cc9df68631e624322dd2a962f65b3Ted Kremenek const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const 6819d5a165d301cc9df68631e624322dd2a962f65b3Ted Kremenek { 6829d5a165d301cc9df68631e624322dd2a962f65b3Ted Kremenek return sloc.getFile().getContentCache()->OrigEntry; 6839d5a165d301cc9df68631e624322dd2a962f65b3Ted Kremenek } 6849d5a165d301cc9df68631e624322dd2a962f65b3Ted Kremenek 685ceafc4b63599d14f0b5b10ff92e22bf242682dceBenjamin Kramer /// getBufferData - Return a StringRef to the source buffer data for the 686ceafc4b63599d14f0b5b10ff92e22bf242682dceBenjamin Kramer /// specified FileID. 687ceafc4b63599d14f0b5b10ff92e22bf242682dceBenjamin Kramer /// 688f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor /// \param FID The file ID whose contents will be returned. 689f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor /// \param Invalid If non-NULL, will be set true if an error occurred. 690686775deca8b8685eb90801495880e3abdd844c2Chris Lattner StringRef getBufferData(FileID FID, bool *Invalid = 0) const; 691f6ac97b101c8840efa92bf29166077ce4049e293Benjamin Kramer 6921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 69306a062dc784c609b75dca15fd97f468d0d846596Chris Lattner //===--------------------------------------------------------------------===// 69406a062dc784c609b75dca15fd97f468d0d846596Chris Lattner // SourceLocation manipulation methods. 69506a062dc784c609b75dca15fd97f468d0d846596Chris Lattner //===--------------------------------------------------------------------===// 6961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 697668ab1a36d6a5731c71a75a5f388ecafd538a896Chris Lattner /// getFileID - Return the FileID for a SourceLocation. This is a very 698de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// hot method that is used for all SourceManager queries that start with a 699de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// SourceLocation object. It is responsible for finding the entry in 700de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// SLocEntryTable which contains the specified location. 701de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// 702de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner FileID getFileID(SourceLocation SpellingLoc) const { 703de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner unsigned SLocOffset = SpellingLoc.getOffset(); 7041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 705de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner // If our one-entry cache covers this offset, just return it. 706de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner if (isOffsetInFileID(LastFileIDLookup, SLocOffset)) 707de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner return LastFileIDLookup; 708de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner 709de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner return getFileIDSlow(SLocOffset); 710de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner } 7111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7122b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner /// getLocForStartOfFile - Return the source location corresponding to the 7132b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner /// first byte of the specified file. 7142b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner SourceLocation getLocForStartOfFile(FileID FID) const { 715e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor bool Invalid = false; 716e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); 717e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor if (Invalid || !Entry.isFile()) 718e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor return SourceLocation(); 719e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor 720e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor unsigned FileOffset = Entry.getOffset(); 721de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner return SourceLocation::getFileLoc(FileOffset); 7222b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner } 7231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 724402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth /// getExpansionLoc - Given a SourceLocation object, return the expansion 725402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth /// location referenced by the ID. 726402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth SourceLocation getExpansionLoc(SourceLocation Loc) const { 727addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner // Handle the non-mapped case inline, defer to out of line code to handle 728402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth // expansions. 729de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner if (Loc.isFileID()) return Loc; 730f84ef95ecec34f27fd05eb4e0392ca6bd3bd0be0Chandler Carruth return getExpansionLocSlowCase(Loc); 731de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner } 7321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 733999f739404edf2078cf9f9c28b4dc45c19765842Chandler Carruth /// getImmediateExpansionRange - Loc is required to be an expansion location. 734999f739404edf2078cf9f9c28b4dc45c19765842Chandler Carruth /// Return the start/end of the expansion information. 735e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner std::pair<SourceLocation,SourceLocation> 736999f739404edf2078cf9f9c28b4dc45c19765842Chandler Carruth getImmediateExpansionRange(SourceLocation Loc) const; 7371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 738edc3dccece244a584f8ebdb81da6c962c08e79beChandler Carruth /// getExpansionRange - Given a SourceLocation object, return the range of 739edc3dccece244a584f8ebdb81da6c962c08e79beChandler Carruth /// tokens covered by the expansion the ultimate file. 7406678133b8ce642f93e5141f056fa643112041ad0Chris Lattner std::pair<SourceLocation,SourceLocation> 741edc3dccece244a584f8ebdb81da6c962c08e79beChandler Carruth getExpansionRange(SourceLocation Loc) const; 7421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 744de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// getSpellingLoc - Given a SourceLocation object, return the spelling 745de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// location referenced by the ID. This is the place where the characters 746de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// that make up the lexed token can be found. 747de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner SourceLocation getSpellingLoc(SourceLocation Loc) const { 748addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner // Handle the non-mapped case inline, defer to out of line code to handle 749addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner // instantiations. 750de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner if (Loc.isFileID()) return Loc; 751addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner return getSpellingLocSlowCase(Loc); 752de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner } 7531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 754387616edf98739f4a0dd234c907e2b913e6a535dChris Lattner /// getImmediateSpellingLoc - Given a SourceLocation object, return the 755387616edf98739f4a0dd234c907e2b913e6a535dChris Lattner /// spelling location referenced by the ID. This is the first level down 756387616edf98739f4a0dd234c907e2b913e6a535dChris Lattner /// towards the place where the characters that make up the lexed token can be 757387616edf98739f4a0dd234c907e2b913e6a535dChris Lattner /// found. This should not generally be used by clients. 7581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const; 759de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner 760de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// getDecomposedLoc - Decompose the specified location into a raw FileID + 761de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// Offset pair. The first element is the FileID, the second is the 762de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// offset from the start of the buffer of the location. 763de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const { 764de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner FileID FID = getFileID(Loc); 765de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner return std::make_pair(FID, Loc.getOffset()-getSLocEntry(FID).getOffset()); 766de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner } 7671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 768e7b2b6e87dbe5b1207f77b6ff9c210a02f95bb39Chandler Carruth /// getDecomposedExpansionLoc - Decompose the specified location into a 769de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// raw FileID + Offset pair. If the location is an instantiation record, 770de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// walk through it until we find the final location instantiated. 771de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner std::pair<FileID, unsigned> 772e7b2b6e87dbe5b1207f77b6ff9c210a02f95bb39Chandler Carruth getDecomposedExpansionLoc(SourceLocation Loc) const { 773de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner FileID FID = getFileID(Loc); 774de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner const SrcMgr::SLocEntry *E = &getSLocEntry(FID); 7751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 776de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner unsigned Offset = Loc.getOffset()-E->getOffset(); 777de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner if (Loc.isFileID()) 778de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner return std::make_pair(FID, Offset); 7791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 780e7b2b6e87dbe5b1207f77b6ff9c210a02f95bb39Chandler Carruth return getDecomposedExpansionLocSlowCase(E); 781de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner } 782de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner 783de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// getDecomposedSpellingLoc - Decompose the specified location into a raw 784de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// FileID + Offset pair. If the location is an instantiation record, walk 785de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// through it until we find its spelling record. 786de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner std::pair<FileID, unsigned> 787de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner getDecomposedSpellingLoc(SourceLocation Loc) const { 788de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner FileID FID = getFileID(Loc); 789de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner const SrcMgr::SLocEntry *E = &getSLocEntry(FID); 7901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 791de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner unsigned Offset = Loc.getOffset()-E->getOffset(); 792de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner if (Loc.isFileID()) 793de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner return std::make_pair(FID, Offset); 794de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner return getDecomposedSpellingLocSlowCase(E, Offset); 7951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } 7961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 79752c29081281955d3db9e11d10573b2d38f709099Chris Lattner /// getFileOffset - This method returns the offset from the start 79852c29081281955d3db9e11d10573b2d38f709099Chris Lattner /// of the file that the specified SourceLocation represents. This is not very 79952c29081281955d3db9e11d10573b2d38f709099Chris Lattner /// meaningful for a macro ID. 80052c29081281955d3db9e11d10573b2d38f709099Chris Lattner unsigned getFileOffset(SourceLocation SpellingLoc) const { 801de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner return getDecomposedLoc(SpellingLoc).second; 8025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 8031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 80496d3589e523a04f4ff2058a7919226ce60696ae8Chandler Carruth /// isMacroArgExpansion - This method tests whether the given source location 80596d3589e523a04f4ff2058a7919226ce60696ae8Chandler Carruth /// represents a macro argument's expansion into the function-like macro 80696d3589e523a04f4ff2058a7919226ce60696ae8Chandler Carruth /// definition. Such source locations only appear inside of the expansion 80796d3589e523a04f4ff2058a7919226ce60696ae8Chandler Carruth /// locations representing where a particular function-like macro was 80896d3589e523a04f4ff2058a7919226ce60696ae8Chandler Carruth /// expanded. 80996d3589e523a04f4ff2058a7919226ce60696ae8Chandler Carruth bool isMacroArgExpansion(SourceLocation Loc) const; 8101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 811de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner //===--------------------------------------------------------------------===// 812de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner // Queries about the code at a SourceLocation. 813de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner //===--------------------------------------------------------------------===// 8141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 8155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// getCharacterData - Return a pointer to the start of the specified location 816de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// in the appropriate spelling MemoryBuffer. 81750f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor /// 81850f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor /// \param Invalid If non-NULL, will be set \c true if an error occurs. 81950f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor const char *getCharacterData(SourceLocation SL, bool *Invalid = 0) const; 8201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 8219dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner /// getColumnNumber - Return the column # for the specified file position. 8229dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner /// This is significantly cheaper to compute than the line number. This 8239dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner /// returns zero if the column number isn't known. This may only be called on 824f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner /// a file sloc, so you must choose a spelling or instantiation location 825f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner /// before calling this method. 82650f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor unsigned getColumnNumber(FileID FID, unsigned FilePos, 82750f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor bool *Invalid = 0) const; 8285ef04ee40c3332d31b6d1439f50d0ddb45812929Chandler Carruth unsigned getSpellingColumnNumber(SourceLocation Loc, bool *Invalid = 0) const; 829a77c031cb66f75d22672070052cc6e0205289ff8Chandler Carruth unsigned getExpansionColumnNumber(SourceLocation Loc, 830b49dcd249c7f4f034493741f95394987a4ccf244Chandler Carruth bool *Invalid = 0) const; 8315ef04ee40c3332d31b6d1439f50d0ddb45812929Chandler Carruth unsigned getPresumedColumnNumber(SourceLocation Loc, bool *Invalid = 0) const; 8321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 8331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 834df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner /// getLineNumber - Given a SourceLocation, return the spelling line number 8355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// for the position indicated. This requires building and caching a table of 8365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// line offsets for the MemoryBuffer, so this is not cheap: use only when 8375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// about to emit a diagnostic. 83850f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = 0) const; 8395ef04ee40c3332d31b6d1439f50d0ddb45812929Chandler Carruth unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = 0) const; 840642116259e8df6286063a17361c20e95b5017a0aChandler Carruth unsigned getExpansionLineNumber(SourceLocation Loc, bool *Invalid = 0) const; 8415ef04ee40c3332d31b6d1439f50d0ddb45812929Chandler Carruth unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = 0) const; 8421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 843bff5c512af8ca7ac92e974e04c06ff4f820e4ee1Chris Lattner /// Return the filename or buffer identifier of the buffer the location is in. 844bff5c512af8ca7ac92e974e04c06ff4f820e4ee1Chris Lattner /// Note that this name does not respect #line directives. Use getPresumedLoc 845bff5c512af8ca7ac92e974e04c06ff4f820e4ee1Chris Lattner /// for normal clients. 84650f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor const char *getBufferName(SourceLocation Loc, bool *Invalid = 0) const; 8471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 8486b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner /// getFileCharacteristic - return the file characteristic of the specified 8491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump /// source location, indicating whether this is a normal file, a system 8506b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner /// header, or an "implicit extern C" system header. 8516b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner /// 8526b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner /// This state can be modified with flags on GNU linemarker directives like: 8536b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner /// # 4 "foo.h" 3 8546b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner /// which changes all source locations in the current file after that to be 8556b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner /// considered to be from a system header. 8566b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const; 8571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 858b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner /// getPresumedLoc - This method returns the "presumed" location of a 859b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner /// SourceLocation specifies. A "presumed location" can be modified by #line 860b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner /// or GNU line marker directives. This provides a view on the data that a 861b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner /// user should see in diagnostics, for example. 862b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner /// 863b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner /// Note that a presumed location is always given as the instantiation point 864b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner /// of an instantiation location, not at the spelling location. 865cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor /// 866cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor /// \returns The presumed location of the specified SourceLocation. If the 867cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor /// presumed location cannot be calculate (e.g., because \p Loc is invalid 868cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor /// or the file containing \p Loc has changed on disk), returns an invalid 869cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor /// presumed location. 870b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner PresumedLoc getPresumedLoc(SourceLocation Loc) const; 8711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 8729fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek /// isFromSameFile - Returns true if both SourceLocations correspond to 8739fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek /// the same file. 8749fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const { 875a11d61793341fea195c29a0dab3fbd74f2b39a8cChris Lattner return getFileID(Loc1) == getFileID(Loc2); 8769fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek } 8771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 8789fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek /// isFromMainFile - Returns true if the file of provided SourceLocation is 8799fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek /// the main file. 8809fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek bool isFromMainFile(SourceLocation Loc) const { 881a11d61793341fea195c29a0dab3fbd74f2b39a8cChris Lattner return getFileID(Loc) == getMainFileID(); 8821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } 8831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 8847bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber /// isInSystemHeader - Returns if a SourceLocation is in a system header. 8857bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber bool isInSystemHeader(SourceLocation Loc) const { 8860b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner return getFileCharacteristic(Loc) != SrcMgr::C_User; 887721818304ac462d8c6ce05eecd02884033db78f1Chris Lattner } 8881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 8890d456588acac0713a7c33063922d35a8cc8c658eChris Lattner /// isInExternCSystemHeader - Returns if a SourceLocation is in an "extern C" 8900d456588acac0713a7c33063922d35a8cc8c658eChris Lattner /// system header. 8910d456588acac0713a7c33063922d35a8cc8c658eChris Lattner bool isInExternCSystemHeader(SourceLocation Loc) const { 8920d456588acac0713a7c33063922d35a8cc8c658eChris Lattner return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem; 8930d456588acac0713a7c33063922d35a8cc8c658eChris Lattner } 8941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 895b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis /// \brief Given a specific chunk of a FileID (FileID with offset+length), 896b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis /// returns true if \arg Loc is inside that chunk and sets relative offset 897b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis /// (offset of \arg Loc from beginning of chunk) to \arg relativeOffset. 898b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis bool isInFileID(SourceLocation Loc, 899b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis FileID FID, unsigned offset, unsigned length, 900b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis unsigned *relativeOffset = 0) const { 901b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis assert(!FID.isInvalid()); 902b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis if (Loc.isInvalid()) 903b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis return false; 904b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis 905b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis unsigned start = getSLocEntry(FID).getOffset() + offset; 906b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis unsigned end = start + length; 907b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis 908b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis#ifndef NDEBUG 909b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis // Make sure offset/length describe a chunk inside the given FileID. 910b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis unsigned NextOffset; 911f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor if (FID.ID == -2) 912f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor NextOffset = 1U << 31U; 913f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor else if (FID.ID+1 == (int)LocalSLocEntryTable.size()) 914f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor NextOffset = getNextLocalOffset(); 915b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis else 916f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor NextOffset = getSLocEntryByID(FID.ID+1).getOffset(); 917b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis assert(start < NextOffset); 918b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis assert(end < NextOffset); 919b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis#endif 920b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis 921b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis if (Loc.getOffset() >= start && Loc.getOffset() < end) { 922b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis if (relativeOffset) 923b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis *relativeOffset = Loc.getOffset() - start; 924b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis return true; 925b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis } 926b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis 927b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis return false; 928b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis } 929469244a322dd5d35cee1d02d70a2edbc12ac5ce7Argyrios Kyrtzidis 93006a062dc784c609b75dca15fd97f468d0d846596Chris Lattner //===--------------------------------------------------------------------===// 9315b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner // Line Table Manipulation Routines 9325b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner //===--------------------------------------------------------------------===// 9331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9345b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner /// getLineTableFilenameID - Return the uniqued ID for the specified filename. 9351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump /// 936686775deca8b8685eb90801495880e3abdd844c2Chris Lattner unsigned getLineTableFilenameID(StringRef Str); 9371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9384c4ea17d7f991516c37a871dfa4bbe5723fa85f0Chris Lattner /// AddLineNote - Add a line note to the line table for the FileID and offset 9394c4ea17d7f991516c37a871dfa4bbe5723fa85f0Chris Lattner /// specified by Loc. If FilenameID is -1, it is considered to be 9404c4ea17d7f991516c37a871dfa4bbe5723fa85f0Chris Lattner /// unspecified. 9414c4ea17d7f991516c37a871dfa4bbe5723fa85f0Chris Lattner void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID); 9429d79ebac47ffde6a1cb312f4c09b66b1b9a397fbChris Lattner void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID, 9431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump bool IsFileEntry, bool IsFileExit, 9449d79ebac47ffde6a1cb312f4c09b66b1b9a397fbChris Lattner bool IsSystemHeader, bool IsExternCHeader); 945bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor 946bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor /// \brief Determine if the source manager has a line table. 947bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor bool hasLineTable() const { return LineTable != 0; } 948bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor 949bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor /// \brief Retrieve the stored line table. 950bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor LineTableInfo &getLineTable(); 951bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor 9525b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner //===--------------------------------------------------------------------===// 953457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek // Queries for performance analysis. 954457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek //===--------------------------------------------------------------------===// 955457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek 956457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek /// Return the total amount of physical memory allocated by the 957457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek /// ContentCache allocator. 958457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek size_t getContentCacheSize() const { 959457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek return ContentCacheAlloc.getTotalMemory(); 960457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek } 961f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek 962f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek struct MemoryBufferSizes { 963f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek const size_t malloc_bytes; 964f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek const size_t mmap_bytes; 965f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek 966f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes) 967f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {} 968f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek }; 969f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek 970f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek /// Return the amount of memory used by memory buffers, breaking down 971f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek /// by heap-backed versus mmap'ed memory. 972f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek MemoryBufferSizes getMemoryBufferSizes() const; 973457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek 974457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek //===--------------------------------------------------------------------===// 97506a062dc784c609b75dca15fd97f468d0d846596Chris Lattner // Other miscellaneous methods. 97606a062dc784c609b75dca15fd97f468d0d846596Chris Lattner //===--------------------------------------------------------------------===// 97710b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis 97810b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis /// \brief Get the source location for the given file:line:col triplet. 97910b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis /// 98010b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis /// If the source file is included multiple times, the source location will 98110b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis /// be based upon the first inclusion. 98210b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis SourceLocation getLocation(const FileEntry *SourceFile, 98386a4d0dd6a630639aab7715323ed068940e650afDouglas Gregor unsigned Line, unsigned Col); 9841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9852aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis /// \brief Determines the order of 2 source locations in the translation unit. 9862aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis /// 9872aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis /// \returns true if LHS source location comes before RHS, false otherwise. 9882aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const; 9892aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis 990b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis /// \brief Determines the order of 2 source locations in the "source location 991b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis /// address space". 992f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor bool isBeforeInSourceLocationOffset(SourceLocation LHS, 993f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor SourceLocation RHS) const { 994b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis return isBeforeInSourceLocationOffset(LHS, RHS.getOffset()); 995b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis } 996b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis 997b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis /// \brief Determines the order of a source location and a source location 998b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis /// offset in the "source location address space". 999f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// 1000f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// Note that we always consider source locations loaded from 1001f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor bool isBeforeInSourceLocationOffset(SourceLocation LHS, unsigned RHS) const { 1002f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor unsigned LHSOffset = LHS.getOffset(); 1003f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor bool LHSLoaded = LHSOffset >= CurrentLoadedOffset; 1004f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor bool RHSLoaded = RHS >= CurrentLoadedOffset; 1005f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor if (LHSLoaded == RHSLoaded) 1006f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor return LHS.getOffset() < RHS; 1007f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor 1008f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor return LHSLoaded; 1009b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis } 1010b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis 1011c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner // Iterators over FileInfos. 10120d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> 10130d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner ::const_iterator fileinfo_iterator; 1014c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); } 1015c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner fileinfo_iterator fileinfo_end() const { return FileInfos.end(); } 1016d93256e55673a17d18543397ec462416acb13792Douglas Gregor bool hasFileInfo(const FileEntry *File) const { 1017d93256e55673a17d18543397ec462416acb13792Douglas Gregor return FileInfos.find(File) != FileInfos.end(); 1018d93256e55673a17d18543397ec462416acb13792Douglas Gregor } 1019c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner 10205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// PrintStats - Print statistics to stderr. 10215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// 10225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer void PrintStats() const; 102378d85f53b093867bbb0123f016956178eea7343eTed Kremenek 1024f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// \brief Get the number of local SLocEntries we have. 1025f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor unsigned local_sloc_entry_size() const { return LocalSLocEntryTable.size(); } 1026f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor 1027f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// \brief Get a local SLocEntry. This is exposed for indexing. 1028f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor const SrcMgr::SLocEntry &getLocalSLocEntry(unsigned Index, 1029f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor bool *Invalid = 0) const { 1030f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor assert(Index < LocalSLocEntryTable.size() && "Invalid index"); 1031f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor return LocalSLocEntryTable[Index]; 1032bdfe48ac80573e026595af91e541474dbf02565fDouglas Gregor } 1033f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor 1034f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// \brief Get the number of loaded SLocEntries we have. 1035f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor unsigned loaded_sloc_entry_size() const { return LoadedSLocEntryTable.size();} 1036f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor 1037f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// \brief Get a loaded SLocEntry. This is exposed for indexing. 1038f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor const SrcMgr::SLocEntry &getLoadedSLocEntry(unsigned Index, bool *Invalid=0) const { 1039f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor assert(Index < LoadedSLocEntryTable.size() && "Invalid index"); 1040f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor if (!SLocEntryLoaded[Index]) 1041f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor ExternalSLocEntries->ReadSLocEntry(-(static_cast<int>(Index) + 2)); 1042f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor return LoadedSLocEntryTable[Index]; 1043f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor } 1044f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor 1045e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor const SrcMgr::SLocEntry &getSLocEntry(FileID FID, bool *Invalid = 0) const { 1046f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor return getSLocEntryByID(FID.ID); 1047bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor } 1048bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor 1049f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor unsigned getNextLocalOffset() const { return NextLocalOffset; } 1050f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor 1051f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor void setExternalSLocEntrySource(ExternalSLocEntrySource *Source) { 1052f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor assert(LoadedSLocEntryTable.empty() && 1053f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor "Invalidating existing loaded entries"); 1054f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor ExternalSLocEntries = Source; 1055f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor } 1056f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor 1057f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// \brief Allocate a number of loaded SLocEntries, which will be actually 1058f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// loaded on demand from the external source. 1059f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// 1060f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// NumSLocEntries will be allocated, which occupy a total of TotalSize space 1061f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// in the global source view. The lowest ID and the base offset of the 1062f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// entries will be returned. 1063f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor std::pair<int, unsigned> 1064f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor AllocateLoadedSLocEntries(unsigned NumSLocEntries, unsigned TotalSize); 1065f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor 10665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerprivate: 1067e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor const llvm::MemoryBuffer *getFakeBufferForRecovery() const; 1068e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor 1069f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// \brief Get the entry with the given unwrapped FileID. 1070f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor const SrcMgr::SLocEntry &getSLocEntryByID(int ID) const { 1071f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor assert(ID != -1 && "Using FileID sentinel value"); 1072f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor if (ID < 0) 1073f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor return getLoadedSLocEntryByID(ID); 1074f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor return getLocalSLocEntry(static_cast<unsigned>(ID)); 1075f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor } 1076f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor 1077f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor const SrcMgr::SLocEntry &getLoadedSLocEntryByID(int ID) const { 1078f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor return getLoadedSLocEntry(static_cast<unsigned>(-ID - 2)); 1079f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor } 1080f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor 1081bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth /// createExpansionLoc - Implements the common elements of storing an 1082c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth /// instantiation info struct into the SLocEntry table and producing a source 1083c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth /// location that refers to it. 108478df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth SourceLocation createExpansionLocImpl(const SrcMgr::ExpansionInfo &Expansion, 1085bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth unsigned TokLength, 1086bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth int LoadedID = 0, 1087bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth unsigned LoadedOffset = 0); 1088c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth 1089de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// isOffsetInFileID - Return true if the specified FileID contains the 1090de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// specified SourceLocation offset. This is a very hot method. 1091de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const { 1092de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner const SrcMgr::SLocEntry &Entry = getSLocEntry(FID); 1093de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner // If the entry is after the offset, it can't contain it. 1094de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner if (SLocOffset < Entry.getOffset()) return false; 10951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1096f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor // If this is the very last entry then it does. 1097f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor if (FID.ID == -2) 1098f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor return true; 1099f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor 1100f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor // If it is the last local entry, then it does if the location is local. 1101f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor if (static_cast<unsigned>(FID.ID+1) == LocalSLocEntryTable.size()) { 1102f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor return SLocOffset < NextLocalOffset; 1103f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor } 11047f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor 1105f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor // Otherwise, the entry after it has to not include it. This works for both 1106f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor // local and loaded entries. 11077f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor return SLocOffset < getSLocEntry(FileID::get(FID.ID+1)).getOffset(); 1108de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner } 11091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 111078d85f53b093867bbb0123f016956178eea7343eTed Kremenek /// createFileID - Create a new fileID for the specified ContentCache and 111178d85f53b093867bbb0123f016956178eea7343eTed Kremenek /// include position. This works regardless of whether the ContentCache 111278d85f53b093867bbb0123f016956178eea7343eTed Kremenek /// corresponds to a file or some other input source. 11132b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner FileID createFileID(const SrcMgr::ContentCache* File, 11142b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner SourceLocation IncludePos, 11157f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor SrcMgr::CharacteristicKind DirCharacter, 1116f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor int LoadedID, unsigned LoadedOffset); 11171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1118de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner const SrcMgr::ContentCache * 1119de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner getOrCreateContentCache(const FileEntry *SourceFile); 1120c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek 112178d85f53b093867bbb0123f016956178eea7343eTed Kremenek /// createMemBufferContentCache - Create a new ContentCache for the specified 112278d85f53b093867bbb0123f016956178eea7343eTed Kremenek /// memory buffer. 11231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const SrcMgr::ContentCache* 11242b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner createMemBufferContentCache(const llvm::MemoryBuffer *Buf); 11251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1126de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner FileID getFileIDSlow(unsigned SLocOffset) const; 1127f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor FileID getFileIDLocal(unsigned SLocOffset) const; 1128f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor FileID getFileIDLoaded(unsigned SLocOffset) const; 1129de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner 1130f84ef95ecec34f27fd05eb4e0392ca6bd3bd0be0Chandler Carruth SourceLocation getExpansionLocSlowCase(SourceLocation Loc) const; 1131addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const; 1132addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner 1133de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner std::pair<FileID, unsigned> 1134e7b2b6e87dbe5b1207f77b6ff9c210a02f95bb39Chandler Carruth getDecomposedExpansionLocSlowCase(const SrcMgr::SLocEntry *E) const; 1135de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner std::pair<FileID, unsigned> 1136de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E, 1137de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner unsigned Offset) const; 11385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}; 11395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 11405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 11415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} // end namespace clang 11425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 11435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif 1144