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//===----------------------------------------------------------------------===// 912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// 102f7f5b1f5ff023cb8c4008ae53a12b09e3ea2622James Dennett/// \file 112f7f5b1f5ff023cb8c4008ae53a12b09e3ea2622James Dennett/// \brief Defines the SourceManager interface. 1212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// 132f7f5b1f5ff023cb8c4008ae53a12b09e3ea2622James Dennett/// There are three different types of locations in a file: a spelling 1412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// location, an expansion location, and a presumed location. 1512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// 1612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// Given an example of: 1712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// \code 1812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// #define min(x, y) x < y ? x : y 1912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// \endcode 2012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// 2112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// and then later on a use of min: 2212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// \code 232f7f5b1f5ff023cb8c4008ae53a12b09e3ea2622James Dennett/// #line 17 2412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// return min(a, b); 2512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// \endcode 2612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// 2712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// The expansion location is the line in the source code where the macro 2812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// was expanded (the return statement), the spelling location is the 2912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// location in the source where the macro was originally defined, 3012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// and the presumed location is where the line directive states that 3112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// the line is 17, or any other line. 3212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// 335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifndef LLVM_CLANG_SOURCEMANAGER_H 365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#define LLVM_CLANG_SOURCEMANAGER_H 375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 38d47d3b0cfeb7e8564ff77f48130fe63282b6d127Chris Lattner#include "clang/Basic/LLVM.h" 39aa48fe80a1b2000809900a437f0819d929793002Jordan Rose#include "clang/Basic/FileManager.h" 405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/SourceLocation.h" 410d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner#include "llvm/Support/Allocator.h" 4203013fa9a0bf1ef4b907f5fec006c8f4000fdd21Michael J. Spencer#include "llvm/Support/DataTypes.h" 43c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor#include "llvm/ADT/PointerIntPair.h" 44aea67dbd653a2dd6dd5cc2159279e81e855b2482Douglas Gregor#include "llvm/ADT/PointerUnion.h" 454f32786ac45210143654390177105eb749b614e9Ted Kremenek#include "llvm/ADT/IntrusiveRefCntPtr.h" 46d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis#include "llvm/ADT/OwningPtr.h" 470d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner#include "llvm/ADT/DenseMap.h" 48d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis#include "llvm/ADT/DenseSet.h" 49f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek#include "llvm/Support/MemoryBuffer.h" 50d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis#include <map> 515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include <vector> 529dc62f044a6ba21f503bd56607d94b32704e7945Chris Lattner#include <cassert> 535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace clang { 551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 56d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikieclass DiagnosticsEngine; 575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass SourceManager; 58099b4747042352f69184481a48508b599a8d3f73Ted Kremenekclass FileManager; 595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass FileEntry; 605b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattnerclass LineTableInfo; 61b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidisclass LangOptions; 62d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidisclass ASTWriter; 63d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidisclass ASTReader; 645330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher 6512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// \namespace 66c351d9837ea1d0b04842497e76c76125426a982cJames Dennett/// \brief Public enums and private classes that are part of the 670b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner/// SourceManager implementation. 685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace SrcMgr { 70af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// \brief Indicates whether a file or directory holds normal user code, 71af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// system code, or system code which is implicitly 'extern "C"' in C++ mode. 72af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// 73af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// Entire directories can be tagged with this (this is maintained by 74af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// DirectoryLookup and friends) as can specific FileInfos when a \#pragma 75af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// system_header is seen or in various other cases. 760b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner /// 779d72851fec9e9c62570a027d42701562bbf29751Chris Lattner enum CharacteristicKind { 780b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner C_User, C_System, C_ExternCSystem 790b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner }; 801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 81af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// \brief One instance of this struct is kept for every file loaded or used. 82af50aab0c317462129d73ae8000c6394c718598dJames Dennett //// 83af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// This object owns the MemoryBuffer object. 84c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek class ContentCache { 85f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor enum CCFlags { 86f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor /// \brief Whether the buffer is invalid. 87f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor InvalidFlag = 0x01, 88f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor /// \brief Whether the buffer should not be freed on destruction. 89f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor DoNotFreeFlag = 0x02 90f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor }; 915330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher 9212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief The actual buffer containing the characters from the input 9312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// file. 9412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// 9512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// This is owned by the ContentCache object. The bits indicate 9612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// whether the buffer is invalid. 97f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor mutable llvm::PointerIntPair<const llvm::MemoryBuffer *, 2> Buffer; 98c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek 99c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek public: 10012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Reference to the file entry representing this ContentCache. 10112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// 102b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis /// This reference does not own the FileEntry object. 103af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// 104af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// It is possible for this to be NULL if the ContentCache encapsulates 105af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// an imaginary text buffer. 106b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis const FileEntry *OrigEntry; 107b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis 108b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis /// \brief References the file which the contents were actually loaded from. 10912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// 110b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis /// Can be different from 'Entry' if we overridden the contents of one file 111b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis /// with the contents of another file. 112b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis const FileEntry *ContentsEntry; 1131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief A bump pointer allocated array of offsets for each source line. 11512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// 11612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// This is lazily computed. This is owned by the SourceManager 11712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// BumpPointerAllocator object. 11805816591ec488a933dfecc9ff9f3cbf3c32767c2Chris Lattner unsigned *SourceLineCache; 1191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief The number of lines in this ContentCache. 12112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// 12212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// This is only valid if SourceLineCache is non-null. 123a081da5e44600d02983d6562bed1b4fd61e410fdDouglas Gregor unsigned NumLines : 31; 12410b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis 125a081da5e44600d02983d6562bed1b4fd61e410fdDouglas Gregor /// \brief Indicates whether the buffer itself was provided to override 126a081da5e44600d02983d6562bed1b4fd61e410fdDouglas Gregor /// the actual file contents. 127a081da5e44600d02983d6562bed1b4fd61e410fdDouglas Gregor /// 128a081da5e44600d02983d6562bed1b4fd61e410fdDouglas Gregor /// When true, the original entry may be a virtual file that does not 129a081da5e44600d02983d6562bed1b4fd61e410fdDouglas Gregor /// exist. 130a081da5e44600d02983d6562bed1b4fd61e410fdDouglas Gregor unsigned BufferOverridden : 1; 131ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis 132ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis /// \brief True if this content cache was initially created for a source 133ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis /// file considered as a system one. 134ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis unsigned IsSystemFile : 1; 135a081da5e44600d02983d6562bed1b4fd61e410fdDouglas Gregor 1367955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor ContentCache(const FileEntry *Ent = 0) 1377955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor : Buffer(0, false), OrigEntry(Ent), ContentsEntry(Ent), 138ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis SourceLineCache(0), NumLines(0), BufferOverridden(false), 139ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis IsSystemFile(false) {} 1407955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor 1417955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor ContentCache(const FileEntry *Ent, const FileEntry *contentEnt) 1427955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor : Buffer(0, false), OrigEntry(Ent), ContentsEntry(contentEnt), 143ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis SourceLineCache(0), NumLines(0), BufferOverridden(false), 144ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis IsSystemFile(false) {} 1457955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor 1467955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor ~ContentCache(); 1477955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor 1487955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor /// The copy ctor does not allow copies where source object has either 14912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// a non-NULL Buffer or SourceLineCache. Ownership of allocated memory 15012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// is not transferred, so this is a logical error. 1517955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor ContentCache(const ContentCache &RHS) 152ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis : Buffer(0, false), SourceLineCache(0), BufferOverridden(false), 153ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis IsSystemFile(false) 1547955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor { 1557955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor OrigEntry = RHS.OrigEntry; 1567955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor ContentsEntry = RHS.ContentsEntry; 1577955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor 1587955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor assert (RHS.Buffer.getPointer() == 0 && RHS.SourceLineCache == 0 && 1597955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor "Passed ContentCache object cannot own a buffer."); 1607955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor 1617955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor NumLines = RHS.NumLines; 1627955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor } 1637955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor 16412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Returns the memory buffer for the associated content. 16536c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor /// 166a92d7e7a55a35b28437103130904a6401bf35408Jonathan D. Turner /// \param Diag Object through which diagnostics will be emitted if the 16712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// buffer cannot be retrieved. 1685330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher /// 169e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner /// \param Loc If specified, is the location that invalid file diagnostics 17012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// will be emitted at. 171e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner /// 17236c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor /// \param Invalid If non-NULL, will be set \c true if an error occurred. 173d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie const llvm::MemoryBuffer *getBuffer(DiagnosticsEngine &Diag, 174e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner const SourceManager &SM, 175e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner SourceLocation Loc = SourceLocation(), 17636c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor bool *Invalid = 0) const; 1771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 17812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Returns the size of the content encapsulated by this 17912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// ContentCache. 18012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// 18112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// This can be the size of the source file or the size of an 18212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// arbitrary scratch buffer. If the ContentCache encapsulates a source 18312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// file this size is retrieved from the file's FileEntry. 184c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek unsigned getSize() const; 1851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 18612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Returns the number of bytes actually mapped for this 18712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// ContentCache. 18812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// 18912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// This can be 0 if the MemBuffer was not actually expanded. 190c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek unsigned getSizeBytesMapped() const; 1915330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher 192f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek /// Returns the kind of memory used to back the memory buffer for 193f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek /// this content cache. This is used for performance analysis. 194f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek llvm::MemoryBuffer::BufferKind getMemoryBufferKind() const; 1951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 19605816591ec488a933dfecc9ff9f3cbf3c32767c2Chris Lattner void setBuffer(const llvm::MemoryBuffer *B) { 197c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor assert(!Buffer.getPointer() && "MemoryBuffer already set."); 198c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor Buffer.setPointer(B); 199c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor Buffer.setInt(false); 200c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek } 2015330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher 202cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor /// \brief Get the underlying buffer, returning NULL if the buffer is not 203cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor /// yet available. 204cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor const llvm::MemoryBuffer *getRawBuffer() const { 205cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor return Buffer.getPointer(); 206cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor } 2071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2082968442603b029949246467253eeac8139a5b6d8Douglas Gregor /// \brief Replace the existing buffer (which will be deleted) 2092968442603b029949246467253eeac8139a5b6d8Douglas Gregor /// with the given buffer. 210f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor void replaceBuffer(const llvm::MemoryBuffer *B, bool DoNotFree = false); 2112968442603b029949246467253eeac8139a5b6d8Douglas Gregor 212f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor /// \brief Determine whether the buffer itself is invalid. 213f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor bool isBufferInvalid() const { 214f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor return Buffer.getInt() & InvalidFlag; 215f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor } 2165330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher 217f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor /// \brief Determine whether the buffer should be freed. 218f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor bool shouldFreeBuffer() const { 219f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor return (Buffer.getInt() & DoNotFreeFlag) == 0; 220f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor } 2215330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher 2220d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek private: 2230d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek // Disable assignments. 2241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump ContentCache &operator=(const ContentCache& RHS); 2251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump }; 2265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 22712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Information about a FileID, basically just the logical file 228de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// that it represents and include stack information. 2295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// 230de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// Each FileInfo has include stack information, indicating where it came 231c351d9837ea1d0b04842497e76c76125426a982cJames Dennett /// from. This information encodes the \#include chain that a token was 2323201f382956ed9beee9fb31229c2835c1208889cChandler Carruth /// expanded from. The main include file has an invalid IncludeLoc. 2335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// 234de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// FileInfos contain a "ContentCache *", with the contents of the file. 2355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// 236de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner class FileInfo { 23712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief The location of the \#include that brought in this file. 23812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// 239c351d9837ea1d0b04842497e76c76125426a982cJames Dennett /// This is an invalid SLOC for the main file (top of the \#include chain). 240de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner unsigned IncludeLoc; // Really a SourceLocation 2411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 242d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis /// \brief Number of FileIDs (files and macros) that were created during 243c351d9837ea1d0b04842497e76c76125426a982cJames Dennett /// preprocessing of this \#include, including this SLocEntry. 24412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// 245d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis /// Zero means the preprocessor didn't provide such info for this SLocEntry. 246d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis unsigned NumCreatedFIDs; 247d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis 248c351d9837ea1d0b04842497e76c76125426a982cJames Dennett /// \brief Contains the ContentCache* and the bits indicating the 249c351d9837ea1d0b04842497e76c76125426a982cJames Dennett /// characteristic of the file and whether it has \#line info, all 250c351d9837ea1d0b04842497e76c76125426a982cJames Dennett /// bitmangled together. 2516e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner uintptr_t Data; 252d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis 25321032df7a0dfc129a8f0c5e004811b455baafb7aArgyrios Kyrtzidis friend class clang::SourceManager; 25421032df7a0dfc129a8f0c5e004811b455baafb7aArgyrios Kyrtzidis friend class clang::ASTWriter; 25521032df7a0dfc129a8f0c5e004811b455baafb7aArgyrios Kyrtzidis friend class clang::ASTReader; 25678d85f53b093867bbb0123f016956178eea7343eTed Kremenek public: 25712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Return a FileInfo object. 258de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner static FileInfo get(SourceLocation IL, const ContentCache *Con, 259de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner CharacteristicKind FileCharacter) { 260de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner FileInfo X; 261de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner X.IncludeLoc = IL.getRawEncoding(); 262d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis X.NumCreatedFIDs = 0; 2636e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner X.Data = (uintptr_t)Con; 26400282d6e1194655a2e89f940bd6fa8484b52e666Chris Lattner assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned"); 2656e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner assert((unsigned)FileCharacter < 4 && "invalid file character"); 2666e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner X.Data |= (unsigned)FileCharacter; 2675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return X; 2685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 2691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 270de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner SourceLocation getIncludeLoc() const { 271de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner return SourceLocation::getFromRawEncoding(IncludeLoc); 272de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner } 2736e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner const ContentCache* getContentCache() const { 27400282d6e1194655a2e89f940bd6fa8484b52e666Chris Lattner return reinterpret_cast<const ContentCache*>(Data & ~7UL); 2756e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner } 2761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 277c351d9837ea1d0b04842497e76c76125426a982cJames Dennett /// \brief Return whether this is a system header or not. 2781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump CharacteristicKind getFileCharacteristic() const { 2796e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner return (CharacteristicKind)(Data & 3); 2800b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner } 281ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner 282c351d9837ea1d0b04842497e76c76125426a982cJames Dennett /// \brief Return true if this FileID has \#line directives in it. 283ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner bool hasLineDirectives() const { return (Data & 4) != 0; } 2841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 285c351d9837ea1d0b04842497e76c76125426a982cJames Dennett /// \brief Set the flag that indicates that this FileID has 286ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner /// line table entries associated with it. 287ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner void setHasLineDirectives() { 288ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner Data |= 4; 289ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner } 2909dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner }; 2911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 29212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Each ExpansionInfo encodes the expansion location - where 29378df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth /// the token was ultimately expanded, and the SpellingLoc - where the actual 29478df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth /// character data for the token came from. 29578df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth class ExpansionInfo { 29678df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth // Really these are all SourceLocations. 2971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 298af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// \brief Where the spelling for the token can be found. 299e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner unsigned SpellingLoc; 3001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 301af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// In a macro expansion, ExpansionLocStart and ExpansionLocEnd 30278df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth /// indicate the start and end of the expansion. In object-like macros, 303af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// they will be the same. In a function-like macro expansion, the start 3043201f382956ed9beee9fb31229c2835c1208889cChandler Carruth /// will be the identifier and the end will be the ')'. Finally, in 30512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// macro-argument instantiations, the end will be 'SourceLocation()', an 306c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth /// invalid location. 30778df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth unsigned ExpansionLocStart, ExpansionLocEnd; 308c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth 3099dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner public: 310de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner SourceLocation getSpellingLoc() const { 311de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner return SourceLocation::getFromRawEncoding(SpellingLoc); 312de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner } 31378df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth SourceLocation getExpansionLocStart() const { 31478df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth return SourceLocation::getFromRawEncoding(ExpansionLocStart); 315e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner } 31678df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth SourceLocation getExpansionLocEnd() const { 317c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth SourceLocation EndLoc = 31878df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth SourceLocation::getFromRawEncoding(ExpansionLocEnd); 31978df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth return EndLoc.isInvalid() ? getExpansionLocStart() : EndLoc; 320e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner } 3211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 32278df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth std::pair<SourceLocation,SourceLocation> getExpansionLocRange() const { 32378df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth return std::make_pair(getExpansionLocStart(), getExpansionLocEnd()); 324e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner } 3251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 32696d3589e523a04f4ff2058a7919226ce60696ae8Chandler Carruth bool isMacroArgExpansion() const { 327c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth // Note that this needs to return false for default constructed objects. 32878df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth return getExpansionLocStart().isValid() && 32978df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth SourceLocation::getFromRawEncoding(ExpansionLocEnd).isInvalid(); 330c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth } 331c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth 332cee5ec9df479994e4ba27fb65b7ded5bb5a980ebArgyrios Kyrtzidis bool isFunctionMacroExpansion() const { 333cee5ec9df479994e4ba27fb65b7ded5bb5a980ebArgyrios Kyrtzidis return getExpansionLocStart().isValid() && 334cee5ec9df479994e4ba27fb65b7ded5bb5a980ebArgyrios Kyrtzidis getExpansionLocStart() != getExpansionLocEnd(); 335cee5ec9df479994e4ba27fb65b7ded5bb5a980ebArgyrios Kyrtzidis } 336cee5ec9df479994e4ba27fb65b7ded5bb5a980ebArgyrios Kyrtzidis 33712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Return a ExpansionInfo for an expansion. 33812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// 33912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// Start and End specify the expansion range (where the macro is 34012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// expanded), and SpellingLoc specifies the spelling location (where 34112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// the characters from the token come from). All three can refer to 34212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// normal File SLocs or expansion locations. 34378df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth static ExpansionInfo create(SourceLocation SpellingLoc, 34478df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth SourceLocation Start, SourceLocation End) { 34578df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth ExpansionInfo X; 34678df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth X.SpellingLoc = SpellingLoc.getRawEncoding(); 34778df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth X.ExpansionLocStart = Start.getRawEncoding(); 34878df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth X.ExpansionLocEnd = End.getRawEncoding(); 3499dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner return X; 3505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 351c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth 35212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Return a special ExpansionInfo for the expansion of 35312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// a macro argument into a function-like macro's body. 35412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// 35512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// ExpansionLoc specifies the expansion location (where the macro is 35612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// expanded). This doesn't need to be a range because a macro is always 35712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// expanded at a macro parameter reference, and macro parameters are 35812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// always exactly one token. SpellingLoc specifies the spelling location 35912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// (where the characters from the token come from). ExpansionLoc and 36012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// SpellingLoc can both refer to normal File SLocs or expansion locations. 361c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth /// 362c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth /// Given the code: 363c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth /// \code 364af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// #define F(x) f(x) 365c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth /// F(42); 366c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth /// \endcode 367c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth /// 36878df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth /// When expanding '\c F(42)', the '\c x' would call this with an 369af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// SpellingLoc pointing at '\c 42' and an ExpansionLoc pointing at its 37078df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth /// location in the definition of '\c F'. 37178df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth static ExpansionInfo createForMacroArg(SourceLocation SpellingLoc, 37278df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth SourceLocation ExpansionLoc) { 373c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth // We store an intentionally invalid source location for the end of the 37478df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth // expansion range to mark that this is a macro argument ion rather than 37578df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth // a normal one. 37678df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth return create(SpellingLoc, ExpansionLoc, SourceLocation()); 377c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth } 378de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner }; 3791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 38012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief This is a discriminated union of FileInfo and ExpansionInfo. 38112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// 38212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// SourceManager keeps an array of these objects, and they are uniquely 38312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// identified by the FileID datatype. 384de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner class SLocEntry { 3853201f382956ed9beee9fb31229c2835c1208889cChandler Carruth unsigned Offset; // low bit is set for expansion info. 386de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner union { 387de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner FileInfo File; 3881728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth ExpansionInfo Expansion; 389de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner }; 390de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner public: 391de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner unsigned getOffset() const { return Offset >> 1; } 3921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3931728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth bool isExpansion() const { return Offset & 1; } 3941728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth bool isFile() const { return !isExpansion(); } 3951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 396de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner const FileInfo &getFile() const { 397de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner assert(isFile() && "Not a file SLocEntry!"); 398de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner return File; 399de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner } 400de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner 4011728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth const ExpansionInfo &getExpansion() const { 4021728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth assert(isExpansion() && "Not a macro expansion SLocEntry!"); 4031728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth return Expansion; 404de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner } 4051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 406de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner static SLocEntry get(unsigned Offset, const FileInfo &FI) { 407de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner SLocEntry E; 408de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner E.Offset = Offset << 1; 409de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner E.File = FI; 410de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner return E; 411de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner } 412de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner 41378df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth static SLocEntry get(unsigned Offset, const ExpansionInfo &Expansion) { 414de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner SLocEntry E; 415de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner E.Offset = (Offset << 1) | 1; 4161728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth E.Expansion = Expansion; 417de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner return E; 418de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner } 4195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer }; 4205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} // end SrcMgr namespace. 4217f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor 4227f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor/// \brief External source of source location entries. 4237f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregorclass ExternalSLocEntrySource { 4247f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregorpublic: 4257f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor virtual ~ExternalSLocEntrySource(); 4267f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor 427f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// \brief Read the source location entry with index ID, which will always be 428f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// less than -1. 429e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor /// 430e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor /// \returns true if an error occurred that prevented the source-location 431e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor /// entry from being loaded. 432f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor virtual bool ReadSLocEntry(int ID) = 0; 4337f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor}; 4345330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher 435dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner 43612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// \brief Holds the cache used by isBeforeInTranslationUnit. 43712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// 43812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// The cache structure is complex enough to be worth breaking out of 43912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// SourceManager. 440dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattnerclass IsBeforeInTranslationUnitCache { 441af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// \brief The FileID's of the cached query. 442af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// 443af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// If these match up with a subsequent query, the result can be reused. 444dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner FileID LQueryFID, RQueryFID; 44537e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis 446af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// \brief True if LQueryFID was created before RQueryFID. 447af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// 448af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// This is used to compare macro expansion locations. 44937e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis bool IsLQFIDBeforeRQFID; 45037e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis 45112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief The file found in common between the two \#include traces, i.e., 45212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// the nearest common ancestor of the \#include tree. 453dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner FileID CommonFID; 4545330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher 455af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// \brief The offset of the previous query in CommonFID. 456af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// 457c351d9837ea1d0b04842497e76c76125426a982cJames Dennett /// Usually, this represents the location of the \#include for QueryFID, but 458c351d9837ea1d0b04842497e76c76125426a982cJames Dennett /// if LQueryFID is a parent of RQueryFID (or vice versa) then these can be a 459dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner /// random token in the parent. 460dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner unsigned LCommonOffset, RCommonOffset; 461dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattnerpublic: 4625330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher 46312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Return true if the currently cached values match up with 464af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// the specified LHS/RHS query. 465af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// 466af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// If not, we can't use the cache. 467dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner bool isCacheValid(FileID LHS, FileID RHS) const { 468dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner return LQueryFID == LHS && RQueryFID == RHS; 469dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner } 4705330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher 47112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief If the cache is valid, compute the result given the 472af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// specified offsets in the LHS/RHS FileID's. 473dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner bool getCachedResult(unsigned LOffset, unsigned ROffset) const { 474dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner // If one of the query files is the common file, use the offset. Otherwise, 475dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner // use the #include loc in the common file. 476dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner if (LQueryFID != CommonFID) LOffset = LCommonOffset; 477dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner if (RQueryFID != CommonFID) ROffset = RCommonOffset; 47837e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis 47937e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis // It is common for multiple macro expansions to be "included" from the same 48037e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis // location (expansion location), in which case use the order of the FileIDs 4814d1cbcf6b9aaae7f82c6e332f46b2ad2d8971bd0Argyrios Kyrtzidis // to determine which came first. This will also take care the case where 4824d1cbcf6b9aaae7f82c6e332f46b2ad2d8971bd0Argyrios Kyrtzidis // one of the locations points at the inclusion/expansion point of the other 4834d1cbcf6b9aaae7f82c6e332f46b2ad2d8971bd0Argyrios Kyrtzidis // in which case its FileID will come before the other. 484d7711ec430fde5706f85ba6c4b85283a8e743ff7Argyrios Kyrtzidis if (LOffset == ROffset) 48537e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis return IsLQFIDBeforeRQFID; 48637e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis 487dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner return LOffset < ROffset; 488dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner } 4895330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher 490af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// \brief Set up a new query. 49137e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis void setQueryFIDs(FileID LHS, FileID RHS, bool isLFIDBeforeRFID) { 49237e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis assert(LHS != RHS); 493dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner LQueryFID = LHS; 494dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner RQueryFID = RHS; 49537e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis IsLQFIDBeforeRQFID = isLFIDBeforeRFID; 49637e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis } 49737e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis 49837e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis void clear() { 49937e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis LQueryFID = RQueryFID = FileID(); 50037e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis IsLQFIDBeforeRQFID = false; 501dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner } 5025330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher 503dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner void setCommonLoc(FileID commonFID, unsigned lCommonOffset, 504dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner unsigned rCommonOffset) { 505dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner CommonFID = commonFID; 506dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner LCommonOffset = lCommonOffset; 507dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner RCommonOffset = rCommonOffset; 508dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner } 5095330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher 510dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner}; 5117f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor 512f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor/// \brief This class handles loading and caching of source files into memory. 513f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor/// 514f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor/// This object owns the MemoryBuffer objects for all of the loaded 515c351d9837ea1d0b04842497e76c76125426a982cJames Dennett/// files and assigns unique FileID's for each unique \#include chain. 5165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 5175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// The SourceManager can be queried for information about SourceLocation 5183201f382956ed9beee9fb31229c2835c1208889cChandler Carruth/// objects, turning them into either spelling or expansion locations. Spelling 5193201f382956ed9beee9fb31229c2835c1208889cChandler Carruth/// locations represent where the bytes corresponding to a token came from and 5203201f382956ed9beee9fb31229c2835c1208889cChandler Carruth/// expansion locations represent where the location is in the user's view. In 5213201f382956ed9beee9fb31229c2835c1208889cChandler Carruth/// the case of a macro expansion, for example, the spelling location indicates 5223201f382956ed9beee9fb31229c2835c1208889cChandler Carruth/// where the expanded token came from and the expansion location specifies 5233201f382956ed9beee9fb31229c2835c1208889cChandler Carruth/// where it was expanded. 524c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmithclass SourceManager : public RefCountedBase<SourceManager> { 525d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie /// \brief DiagnosticsEngine object. 526d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie DiagnosticsEngine &Diag; 527389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis 528389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis FileManager &FileMgr; 529389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis 5300d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner mutable llvm::BumpPtrAllocator ContentCacheAlloc; 5311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 53212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Memoized information about all of the files tracked by this 53312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// SourceManager. 53412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// 53512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// This map allows us to merge ContentCache entries based 5360d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek /// on their FileEntry*. All ContentCache objects will thus have unique, 5371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump /// non-null, FileEntry pointers. 5380d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos; 5391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 540299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis /// \brief True if the ContentCache for files that are overriden by other 541299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis /// files, should report the original file name. Defaults to true. 542299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis bool OverridenFilesKeepOriginalName; 543299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis 544ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis /// \brief True if non-system source files should be treated as volatile 545ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis /// (likely to change while trying to use them). Defaults to false. 546ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis bool UserFilesAreVolatile; 547ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis 548d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis struct OverriddenFilesInfoTy { 549d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis /// \brief Files that have been overriden with the contents from another 550d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis /// file. 551d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis llvm::DenseMap<const FileEntry *, const FileEntry *> OverriddenFiles; 552d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis /// \brief Files that were overridden with a memory buffer. 553d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis llvm::DenseSet<const FileEntry *> OverriddenFilesWithBuffer; 554d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis }; 555d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis 556d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis /// \brief Lazily create the object keeping overridden files info, since 557d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis /// it is uncommonly used. 558d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis OwningPtr<OverriddenFilesInfoTy> OverriddenFilesInfo; 559b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis 560d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis OverriddenFilesInfoTy &getOverriddenFilesInfo() { 561d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis if (!OverriddenFilesInfo) 562d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis OverriddenFilesInfo.reset(new OverriddenFilesInfoTy); 563d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis return *OverriddenFilesInfo; 564d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis } 56512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett 56612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Information about various memory buffers that we have read in. 56712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// 56812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// All FileEntry* within the stored ContentCache objects are NULL, 5690d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner /// as they do not refer to a file. 5700d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner std::vector<SrcMgr::ContentCache*> MemBufferInfos; 5711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 572f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// \brief The table of SLocEntries that are local to this module. 573f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// 574f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// Positive FileIDs are indexes into this table. Entry 0 indicates an invalid 5753201f382956ed9beee9fb31229c2835c1208889cChandler Carruth /// expansion. 576f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor std::vector<SrcMgr::SLocEntry> LocalSLocEntryTable; 577f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor 578f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// \brief The table of SLocEntries that are loaded from other modules. 579f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// 580f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// Negative FileIDs are indexes into this table. To get from ID to an index, 581f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// use (-ID - 2). 582a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis mutable std::vector<SrcMgr::SLocEntry> LoadedSLocEntryTable; 5837f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor 584f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// \brief The starting offset of the next local SLocEntry. 585f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// 586f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// This is LocalSLocEntryTable.back().Offset + the size of that entry. 587f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor unsigned NextLocalOffset; 588f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor 589f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// \brief The starting offset of the latest batch of loaded SLocEntries. 590f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// 591f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// This is LoadedSLocEntryTable.back().Offset, except that that entry might 592f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// not have been loaded, so that value would be unknown. 593f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor unsigned CurrentLoadedOffset; 594f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor 595ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis /// \brief The highest possible offset is 2^31-1, so CurrentLoadedOffset 596ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis /// starts at 2^31. 597ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis static const unsigned MaxLoadedOffset = 1U << 31U; 598ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis 599f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// \brief A bitmap that indicates whether the entries of LoadedSLocEntryTable 600f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// have already been loaded from the external source. 601f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// 602f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// Same indexing as LoadedSLocEntryTable. 6037f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor std::vector<bool> SLocEntryLoaded; 6047f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor 6057f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor /// \brief An external source for source location entries. 6067f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor ExternalSLocEntrySource *ExternalSLocEntries; 6077f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor 60812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief A one-entry cache to speed up getFileID. 609c351d9837ea1d0b04842497e76c76125426a982cJames Dennett /// 610de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// LastFileIDLookup records the last FileID looked up or created, because it 611de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// is very common to look up many tokens from the same file. 612de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner mutable FileID LastFileIDLookup; 6131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 614c351d9837ea1d0b04842497e76c76125426a982cJames Dennett /// \brief Holds information for \#line directives. 615c351d9837ea1d0b04842497e76c76125426a982cJames Dennett /// 616af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// This is referenced by indices from SLocEntryTable. 6175b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner LineTableInfo *LineTable; 6181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 61912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief These ivars serve as a cache used in the getLineNumber 6205e36a7a89da5d69ece23fc8228624a053f75c645Chris Lattner /// method which is used to speedup getLineNumber calls to nearby locations. 6212b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner mutable FileID LastLineNoFileIDQuery; 622f812a45dd93634c9300ed5533bd26b56374714a1Chris Lattner mutable SrcMgr::ContentCache *LastLineNoContentCache; 623f812a45dd93634c9300ed5533bd26b56374714a1Chris Lattner mutable unsigned LastLineNoFilePos; 624f812a45dd93634c9300ed5533bd26b56374714a1Chris Lattner mutable unsigned LastLineNoResult; 6251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 626c351d9837ea1d0b04842497e76c76125426a982cJames Dennett /// \brief The file ID for the main source file of the translation unit. 6272b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner FileID MainFileID; 62849c1f4aa2a6c360d25d605004ec3c4affd62db77Steve Naroff 629507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis /// \brief The file ID for the precompiled preamble there is one. 630507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis FileID PreambleFileID; 631507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis 632de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner // Statistics for -print-stats. 633de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner mutable unsigned NumLinearScans, NumBinaryProbes; 6341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6352aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis // Cache results for the isBeforeInTranslationUnit method. 636dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner mutable IsBeforeInTranslationUnitCache IsBeforeInTUCache; 6371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 638e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor // Cache for the "fake" buffer used for error-recovery purposes. 639e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor mutable llvm::MemoryBuffer *FakeBufferForRecovery; 6405330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher 641a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis mutable SrcMgr::ContentCache *FakeContentCacheForRecovery; 642a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis 643fb3612ef197cb8532c05f33889ec1aed7c26e5cbArgyrios Kyrtzidis /// \brief Lazily computed map of macro argument chunks to their expanded 644fb3612ef197cb8532c05f33889ec1aed7c26e5cbArgyrios Kyrtzidis /// source location. 645fb3612ef197cb8532c05f33889ec1aed7c26e5cbArgyrios Kyrtzidis typedef std::map<unsigned, SourceLocation> MacroArgsMap; 646fb3612ef197cb8532c05f33889ec1aed7c26e5cbArgyrios Kyrtzidis 64770042f5d1ca378138f90b7b9384023701f5d03d8David Blaikie mutable llvm::DenseMap<FileID, MacroArgsMap *> MacroArgsCacheMap; 648fb3612ef197cb8532c05f33889ec1aed7c26e5cbArgyrios Kyrtzidis 64949c1f4aa2a6c360d25d605004ec3c4affd62db77Steve Naroff // SourceManager doesn't support copy construction. 65049c1f4aa2a6c360d25d605004ec3c4affd62db77Steve Naroff explicit SourceManager(const SourceManager&); 6511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump void operator=(const SourceManager&); 6525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic: 653ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis SourceManager(DiagnosticsEngine &Diag, FileManager &FileMgr, 654ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis bool UserFilesAreVolatile = false); 6555b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner ~SourceManager(); 6561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6575b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner void clearIDTables(); 6581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 659d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie DiagnosticsEngine &getDiagnostics() const { return Diag; } 66078a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios Kyrtzidis 661389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis FileManager &getFileManager() const { return FileMgr; } 662389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis 663299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis /// \brief Set true if the SourceManager should report the original file name 664299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis /// for contents of files that were overriden by other files.Defaults to true. 665299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis void setOverridenFilesKeepOriginalName(bool value) { 666299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis OverridenFilesKeepOriginalName = value; 667299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis } 668299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis 669ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis /// \brief True if non-system source files should be treated as volatile 670ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis /// (likely to change while trying to use them). 671ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis bool userFilesAreVolatile() const { return UserFilesAreVolatile; } 672ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis 67312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Create the FileID for a memory buffer that will represent the 67412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// FileID for the main source. 67512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// 67612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// One example of when this would be used is when the main source is read 67712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// from STDIN. 678f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) { 679f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor assert(MainFileID.isInvalid() && "MainFileID already set!"); 680f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor MainFileID = createFileIDForMemBuffer(Buffer); 681f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor return MainFileID; 682f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor } 683f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor 68406a062dc784c609b75dca15fd97f468d0d846596Chris Lattner //===--------------------------------------------------------------------===// 68506a062dc784c609b75dca15fd97f468d0d846596Chris Lattner // MainFileID creation and querying methods. 68606a062dc784c609b75dca15fd97f468d0d846596Chris Lattner //===--------------------------------------------------------------------===// 68706a062dc784c609b75dca15fd97f468d0d846596Chris Lattner 68812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Returns the FileID of the main source file. 6892b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner FileID getMainFileID() const { return MainFileID; } 6901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 69112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Create the FileID for the main source file. 692a1f1fad8b60e1cb9d21a40a37f2e03150bcbeb6fDouglas Gregor FileID createMainFileID(const FileEntry *SourceFile, 693a1f1fad8b60e1cb9d21a40a37f2e03150bcbeb6fDouglas Gregor SrcMgr::CharacteristicKind Kind = SrcMgr::C_User) { 69406a062dc784c609b75dca15fd97f468d0d846596Chris Lattner assert(MainFileID.isInvalid() && "MainFileID already set!"); 695a1f1fad8b60e1cb9d21a40a37f2e03150bcbeb6fDouglas Gregor MainFileID = createFileID(SourceFile, SourceLocation(), Kind); 69606a062dc784c609b75dca15fd97f468d0d846596Chris Lattner return MainFileID; 69706a062dc784c609b75dca15fd97f468d0d846596Chris Lattner } 6981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 699b8c879a5363f36bdae8831112b563333e3c05acbArgyrios Kyrtzidis /// \brief Set the file ID for the main source file. 700b8c879a5363f36bdae8831112b563333e3c05acbArgyrios Kyrtzidis void setMainFileID(FileID FID) { 701b8c879a5363f36bdae8831112b563333e3c05acbArgyrios Kyrtzidis assert(MainFileID.isInvalid() && "MainFileID already set!"); 702b8c879a5363f36bdae8831112b563333e3c05acbArgyrios Kyrtzidis MainFileID = FID; 703b8c879a5363f36bdae8831112b563333e3c05acbArgyrios Kyrtzidis } 704b8c879a5363f36bdae8831112b563333e3c05acbArgyrios Kyrtzidis 705507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis /// \brief Set the file ID for the precompiled preamble. 706507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis void setPreambleFileID(FileID Preamble) { 707507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis assert(PreambleFileID.isInvalid() && "PreambleFileID already set!"); 708507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis PreambleFileID = Preamble; 709414cb64f09ce48a36377458ce5e5a90c3ad41d00Douglas Gregor } 7105330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher 711507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis /// \brief Get the file ID for the precompiled preamble if there is one. 712507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis FileID getPreambleFileID() const { return PreambleFileID; } 713507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis 71406a062dc784c609b75dca15fd97f468d0d846596Chris Lattner //===--------------------------------------------------------------------===// 7153201f382956ed9beee9fb31229c2835c1208889cChandler Carruth // Methods to create new FileID's and macro expansions. 71606a062dc784c609b75dca15fd97f468d0d846596Chris Lattner //===--------------------------------------------------------------------===// 7171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 71812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Create a new FileID that represents the specified file 71912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// being \#included from the specified IncludePosition. 72012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// 72112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// This translates NULL into standard input. 7222b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos, 7237f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor SrcMgr::CharacteristicKind FileCharacter, 724f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor int LoadedID = 0, unsigned LoadedOffset = 0) { 725ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis const SrcMgr::ContentCache * 726ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis IR = getOrCreateContentCache(SourceFile, 727ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis /*isSystemFile=*/FileCharacter != SrcMgr::C_User); 7280d06e998910934e5ef070f53f4c272e7c6b846c6Dan Gohman assert(IR && "getOrCreateContentCache() cannot return NULL"); 729f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor return createFileID(IR, IncludePos, FileCharacter, LoadedID, LoadedOffset); 7305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 7311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 73212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Create a new FileID that represents the specified memory buffer. 73312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// 73412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// This does no caching of the buffer and takes ownership of the 73512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// MemoryBuffer, so only pass a MemoryBuffer to this once. 7367f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer, 737f453cb9f677e16c00b358ec91eccf5f003765dc6Axel Naumann int LoadedID = 0, unsigned LoadedOffset = 0, 738f453cb9f677e16c00b358ec91eccf5f003765dc6Axel Naumann SourceLocation IncludeLoc = SourceLocation()) { 739f453cb9f677e16c00b358ec91eccf5f003765dc6Axel Naumann return createFileID(createMemBufferContentCache(Buffer), IncludeLoc, 740f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor SrcMgr::C_User, LoadedID, LoadedOffset); 7411036b68525f39cb69ac22c679ed440acd8392a16Ted Kremenek } 74206a062dc784c609b75dca15fd97f468d0d846596Chris Lattner 74312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Return a new SourceLocation that encodes the 744bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth /// fact that a token from SpellingLoc should actually be referenced from 7453201f382956ed9beee9fb31229c2835c1208889cChandler Carruth /// ExpansionLoc, and that it represents the expansion of a macro argument 7463201f382956ed9beee9fb31229c2835c1208889cChandler Carruth /// into the function-like macro body. 747bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth SourceLocation createMacroArgExpansionLoc(SourceLocation Loc, 748bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth SourceLocation ExpansionLoc, 749bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth unsigned TokLength); 750c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth 75112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Return a new SourceLocation that encodes the fact 752c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth /// that a token from SpellingLoc should actually be referenced from 753bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth /// ExpansionLoc. 754bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth SourceLocation createExpansionLoc(SourceLocation Loc, 755bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth SourceLocation ExpansionLocStart, 756bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth SourceLocation ExpansionLocEnd, 757bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth unsigned TokLength, 758bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth int LoadedID = 0, 759bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth unsigned LoadedOffset = 0); 7601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7612968442603b029949246467253eeac8139a5b6d8Douglas Gregor /// \brief Retrieve the memory buffer associated with the given file. 76250f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor /// 76350f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor /// \param Invalid If non-NULL, will be set \c true if an error 76450f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor /// occurs while retrieving the memory buffer. 76550f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File, 76650f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor bool *Invalid = 0); 7672968442603b029949246467253eeac8139a5b6d8Douglas Gregor 7682968442603b029949246467253eeac8139a5b6d8Douglas Gregor /// \brief Override the contents of the given source file by providing an 7692968442603b029949246467253eeac8139a5b6d8Douglas Gregor /// already-allocated buffer. 7702968442603b029949246467253eeac8139a5b6d8Douglas Gregor /// 771afbf5f810652f04c0bba235c0fa079885fb3caa8Dan Gohman /// \param SourceFile the source file whose contents will be overriden. 7722968442603b029949246467253eeac8139a5b6d8Douglas Gregor /// 7732968442603b029949246467253eeac8139a5b6d8Douglas Gregor /// \param Buffer the memory buffer whose contents will be used as the 7742968442603b029949246467253eeac8139a5b6d8Douglas Gregor /// data in the given source file. 7752968442603b029949246467253eeac8139a5b6d8Douglas Gregor /// 776f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor /// \param DoNotFree If true, then the buffer will not be freed when the 777f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor /// source manager is destroyed. 7780d06e998910934e5ef070f53f4c272e7c6b846c6Dan Gohman void overrideFileContents(const FileEntry *SourceFile, 779f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor const llvm::MemoryBuffer *Buffer, 780f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor bool DoNotFree = false); 7812968442603b029949246467253eeac8139a5b6d8Douglas Gregor 782bed28ac1d1463adca3ecf24fca5c30646fa9dbb2Sylvestre Ledru /// \brief Override the given source file with another one. 783b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis /// 784b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis /// \param SourceFile the source file which will be overriden. 785b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis /// 786b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis /// \param NewFile the file whose contents will be used as the 787b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis /// data instead of the contents of the given source file. 788b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis void overrideFileContents(const FileEntry *SourceFile, 789b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis const FileEntry *NewFile); 790b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis 791d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis /// \brief Returns true if the file contents have been overridden. 792d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis bool isFileOverridden(const FileEntry *File) { 793d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis if (OverriddenFilesInfo) { 794d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis if (OverriddenFilesInfo->OverriddenFilesWithBuffer.count(File)) 795d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis return true; 796d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis if (OverriddenFilesInfo->OverriddenFiles.find(File) != 797d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis OverriddenFilesInfo->OverriddenFiles.end()) 798d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis return true; 799d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis } 800d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis return false; 801d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis } 802d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis 803d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis /// \brief Disable overridding the contents of a file, previously enabled 804af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// with #overrideFileContents. 80512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// 806d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis /// This should be called before parsing has begun. 807d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis void disableFileContentsOverride(const FileEntry *File); 808d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis 80906a062dc784c609b75dca15fd97f468d0d846596Chris Lattner //===--------------------------------------------------------------------===// 81006a062dc784c609b75dca15fd97f468d0d846596Chris Lattner // FileID manipulation methods. 81106a062dc784c609b75dca15fd97f468d0d846596Chris Lattner //===--------------------------------------------------------------------===// 8121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 81312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Return the buffer for the specified FileID. 81412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// 81512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// If there is an error opening this buffer the first time, this 81612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// manufactures a temporary buffer and returns a non-empty error string. 817e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner const llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc, 818e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner bool *Invalid = 0) const { 819e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor bool MyInvalid = false; 820e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid); 821e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor if (MyInvalid || !Entry.isFile()) { 822e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor if (Invalid) 823e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor *Invalid = true; 8245330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher 825e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor return getFakeBufferForRecovery(); 826e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor } 8275330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher 8285330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher return Entry.getFile().getContentCache()->getBuffer(Diag, *this, Loc, 829e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor Invalid); 83006a062dc784c609b75dca15fd97f468d0d846596Chris Lattner } 8311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 832e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner const llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = 0) const { 833e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor bool MyInvalid = false; 834e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid); 835e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor if (MyInvalid || !Entry.isFile()) { 836e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor if (Invalid) 837e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor *Invalid = true; 8385330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher 839e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor return getFakeBufferForRecovery(); 840e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor } 841e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor 8425330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher return Entry.getFile().getContentCache()->getBuffer(Diag, *this, 8435330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher SourceLocation(), 844e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor Invalid); 845e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner } 8465330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher 84712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Returns the FileEntry record for the provided FileID. 84806a062dc784c609b75dca15fd97f468d0d846596Chris Lattner const FileEntry *getFileEntryForID(FileID FID) const { 849e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor bool MyInvalid = false; 850e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid); 851e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor if (MyInvalid || !Entry.isFile()) 852e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor return 0; 8535330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher 85439afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis const SrcMgr::ContentCache *Content = Entry.getFile().getContentCache(); 85539afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis if (!Content) 85639afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis return 0; 85739afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis return Content->OrigEntry; 85806a062dc784c609b75dca15fd97f468d0d846596Chris Lattner } 8591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 86012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Returns the FileEntry record for the provided SLocEntry. 8619d5a165d301cc9df68631e624322dd2a962f65b3Ted Kremenek const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const 8629d5a165d301cc9df68631e624322dd2a962f65b3Ted Kremenek { 86339afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis const SrcMgr::ContentCache *Content = sloc.getFile().getContentCache(); 86439afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis if (!Content) 86539afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis return 0; 86639afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis return Content->OrigEntry; 8679d5a165d301cc9df68631e624322dd2a962f65b3Ted Kremenek } 8689d5a165d301cc9df68631e624322dd2a962f65b3Ted Kremenek 86912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Return a StringRef to the source buffer data for the 870ceafc4b63599d14f0b5b10ff92e22bf242682dceBenjamin Kramer /// specified FileID. 871ceafc4b63599d14f0b5b10ff92e22bf242682dceBenjamin Kramer /// 872f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor /// \param FID The file ID whose contents will be returned. 873f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor /// \param Invalid If non-NULL, will be set true if an error occurred. 874686775deca8b8685eb90801495880e3abdd844c2Chris Lattner StringRef getBufferData(FileID FID, bool *Invalid = 0) const; 875f6ac97b101c8840efa92bf29166077ce4049e293Benjamin Kramer 876d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis /// \brief Get the number of FileIDs (files and macros) that were created 8772c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay /// during preprocessing of \p FID, including it. 878d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis unsigned getNumCreatedFIDsForFileID(FileID FID) const { 879d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis bool Invalid = false; 880d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); 881d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis if (Invalid || !Entry.isFile()) 882d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis return 0; 883d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis 884d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis return Entry.getFile().NumCreatedFIDs; 885d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis } 886d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis 887d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis /// \brief Set the number of FileIDs (files and macros) that were created 8882c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay /// during preprocessing of \p FID, including it. 889d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis void setNumCreatedFIDsForFileID(FileID FID, unsigned NumFIDs) const { 890d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis bool Invalid = false; 891d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); 892d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis if (Invalid || !Entry.isFile()) 893d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis return; 894d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis 895d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis assert(Entry.getFile().NumCreatedFIDs == 0 && "Already set!"); 896d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis const_cast<SrcMgr::FileInfo &>(Entry.getFile()).NumCreatedFIDs = NumFIDs; 897d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis } 8981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 89906a062dc784c609b75dca15fd97f468d0d846596Chris Lattner //===--------------------------------------------------------------------===// 90006a062dc784c609b75dca15fd97f468d0d846596Chris Lattner // SourceLocation manipulation methods. 90106a062dc784c609b75dca15fd97f468d0d846596Chris Lattner //===--------------------------------------------------------------------===// 9021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 90312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Return the FileID for a SourceLocation. 90412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// 90512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// This is a very hot method that is used for all SourceManager queries 90612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// that start with a SourceLocation object. It is responsible for finding 90712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// the entry in SLocEntryTable which contains the specified location. 908de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// 909de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner FileID getFileID(SourceLocation SpellingLoc) const { 910de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner unsigned SLocOffset = SpellingLoc.getOffset(); 9111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 912de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner // If our one-entry cache covers this offset, just return it. 913de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner if (isOffsetInFileID(LastFileIDLookup, SLocOffset)) 914de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner return LastFileIDLookup; 915de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner 916de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner return getFileIDSlow(SLocOffset); 917de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner } 9181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 919aa48fe80a1b2000809900a437f0819d929793002Jordan Rose /// \brief Return the filename of the file containing a SourceLocation. 920aa48fe80a1b2000809900a437f0819d929793002Jordan Rose StringRef getFilename(SourceLocation SpellingLoc) const { 921aa48fe80a1b2000809900a437f0819d929793002Jordan Rose if (const FileEntry *F = getFileEntryForID(getFileID(SpellingLoc))) 922aa48fe80a1b2000809900a437f0819d929793002Jordan Rose return F->getName(); 923aa48fe80a1b2000809900a437f0819d929793002Jordan Rose return StringRef(); 924aa48fe80a1b2000809900a437f0819d929793002Jordan Rose } 925aa48fe80a1b2000809900a437f0819d929793002Jordan Rose 92612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Return the source location corresponding to the first byte of 92712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// the specified file. 9282b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner SourceLocation getLocForStartOfFile(FileID FID) const { 929e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor bool Invalid = false; 930e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); 931e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor if (Invalid || !Entry.isFile()) 932e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor return SourceLocation(); 9335330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher 934e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor unsigned FileOffset = Entry.getOffset(); 935de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner return SourceLocation::getFileLoc(FileOffset); 9362b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner } 937f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis 938f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis /// \brief Return the source location corresponding to the last byte of the 939f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis /// specified file. 940f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis SourceLocation getLocForEndOfFile(FileID FID) const { 941f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis bool Invalid = false; 942f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); 943f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis if (Invalid || !Entry.isFile()) 944f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis return SourceLocation(); 945f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis 946f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis unsigned FileOffset = Entry.getOffset(); 947f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis return SourceLocation::getFileLoc(FileOffset + getFileIDSize(FID) - 1); 948f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis } 9491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 950809d1be9820039b4cf6efa48246a0d70ffa13394James Dennett /// \brief Returns the include location if \p FID is a \#include'd file 951d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis /// otherwise it returns an invalid location. 952d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis SourceLocation getIncludeLoc(FileID FID) const { 953d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis bool Invalid = false; 954d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); 955d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis if (Invalid || !Entry.isFile()) 956d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis return SourceLocation(); 9575330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher 958d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis return Entry.getFile().getIncludeLoc(); 959d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis } 960d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis 96112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Given a SourceLocation object \p Loc, return the expansion 962402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth /// location referenced by the ID. 963402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth SourceLocation getExpansionLoc(SourceLocation Loc) const { 964addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner // Handle the non-mapped case inline, defer to out of line code to handle 965402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth // expansions. 966de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner if (Loc.isFileID()) return Loc; 967f84ef95ecec34f27fd05eb4e0392ca6bd3bd0be0Chandler Carruth return getExpansionLocSlowCase(Loc); 968de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner } 9691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9702c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay /// \brief Given \p Loc, if it is a macro location return the expansion 971796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis /// location or the spelling location, depending on if it comes from a 972796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis /// macro argument or not. 973796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis SourceLocation getFileLoc(SourceLocation Loc) const { 974796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis if (Loc.isFileID()) return Loc; 975796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis return getFileLocSlowCase(Loc); 976796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis } 977796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis 97812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Return the start/end of the expansion information for an 97912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// expansion location. 98012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// 98112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \pre \p Loc is required to be an expansion location. 982e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner std::pair<SourceLocation,SourceLocation> 983999f739404edf2078cf9f9c28b4dc45c19765842Chandler Carruth getImmediateExpansionRange(SourceLocation Loc) const; 9841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 98512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Given a SourceLocation object, return the range of 986edc3dccece244a584f8ebdb81da6c962c08e79beChandler Carruth /// tokens covered by the expansion the ultimate file. 9876678133b8ce642f93e5141f056fa643112041ad0Chris Lattner std::pair<SourceLocation,SourceLocation> 988edc3dccece244a584f8ebdb81da6c962c08e79beChandler Carruth getExpansionRange(SourceLocation Loc) const; 9891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 99112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Given a SourceLocation object, return the spelling 99212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// location referenced by the ID. 99312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// 99412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// This is the place where the characters that make up the lexed token 99512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// can be found. 996de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner SourceLocation getSpellingLoc(SourceLocation Loc) const { 997addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner // Handle the non-mapped case inline, defer to out of line code to handle 9983201f382956ed9beee9fb31229c2835c1208889cChandler Carruth // expansions. 999de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner if (Loc.isFileID()) return Loc; 1000addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner return getSpellingLocSlowCase(Loc); 1001de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner } 10021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 100312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Given a SourceLocation object, return the spelling location 100412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// referenced by the ID. 100512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// 100612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// This is the first level down towards the place where the characters 100712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// that make up the lexed token can be found. This should not generally 100812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// be used by clients. 10091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const; 1010de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner 101112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Decompose the specified location into a raw FileID + Offset pair. 101212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// 101312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// The first element is the FileID, the second is the offset from the 101412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// start of the buffer of the location. 1015de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const { 1016de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner FileID FID = getFileID(Loc); 1017a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis bool Invalid = false; 1018a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis const SrcMgr::SLocEntry &E = getSLocEntry(FID, &Invalid); 1019a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis if (Invalid) 1020a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis return std::make_pair(FileID(), 0); 1021a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis return std::make_pair(FID, Loc.getOffset()-E.getOffset()); 1022de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner } 10231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 102412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Decompose the specified location into a raw FileID + Offset pair. 102512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// 102612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// If the location is an expansion record, walk through it until we find 102712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// the final location expanded. 1028de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner std::pair<FileID, unsigned> 1029e7b2b6e87dbe5b1207f77b6ff9c210a02f95bb39Chandler Carruth getDecomposedExpansionLoc(SourceLocation Loc) const { 1030de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner FileID FID = getFileID(Loc); 1031a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis bool Invalid = false; 1032a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis const SrcMgr::SLocEntry *E = &getSLocEntry(FID, &Invalid); 1033a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis if (Invalid) 1034a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis return std::make_pair(FileID(), 0); 10351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1036de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner unsigned Offset = Loc.getOffset()-E->getOffset(); 1037de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner if (Loc.isFileID()) 1038de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner return std::make_pair(FID, Offset); 10391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1040e7b2b6e87dbe5b1207f77b6ff9c210a02f95bb39Chandler Carruth return getDecomposedExpansionLocSlowCase(E); 1041de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner } 1042de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner 104312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Decompose the specified location into a raw FileID + Offset pair. 104412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// 104512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// If the location is an expansion record, walk through it until we find 104612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// its spelling record. 1047de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner std::pair<FileID, unsigned> 1048de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner getDecomposedSpellingLoc(SourceLocation Loc) const { 1049de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner FileID FID = getFileID(Loc); 1050a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis bool Invalid = false; 1051a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis const SrcMgr::SLocEntry *E = &getSLocEntry(FID, &Invalid); 1052a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis if (Invalid) 1053a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis return std::make_pair(FileID(), 0); 10541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1055de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner unsigned Offset = Loc.getOffset()-E->getOffset(); 1056de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner if (Loc.isFileID()) 1057de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner return std::make_pair(FID, Offset); 1058de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner return getDecomposedSpellingLocSlowCase(E, Offset); 10591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } 10601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 106112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Returns the offset from the start of the file that the 106212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// specified SourceLocation represents. 106312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// 106412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// This is not very meaningful for a macro ID. 106552c29081281955d3db9e11d10573b2d38f709099Chris Lattner unsigned getFileOffset(SourceLocation SpellingLoc) const { 1066de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner return getDecomposedLoc(SpellingLoc).second; 10675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 10681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 106912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Tests whether the given source location represents a macro 107012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// argument's expansion into the function-like macro definition. 107112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// 107212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// Such source locations only appear inside of the expansion 107396d3589e523a04f4ff2058a7919226ce60696ae8Chandler Carruth /// locations representing where a particular function-like macro was 107496d3589e523a04f4ff2058a7919226ce60696ae8Chandler Carruth /// expanded. 107596d3589e523a04f4ff2058a7919226ce60696ae8Chandler Carruth bool isMacroArgExpansion(SourceLocation Loc) const; 10761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10772c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay /// \brief Returns true if \p Loc is inside the [\p Start, +\p Length) 1078499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis /// chunk of the source location address space. 107912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// 10802c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay /// If it's true and \p RelativeOffset is non-null, it will be set to the 10812c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay /// relative offset of \p Loc inside the chunk. 1082499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis bool isInSLocAddrSpace(SourceLocation Loc, 1083499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis SourceLocation Start, unsigned Length, 1084499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis unsigned *RelativeOffset = 0) const { 1085499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis assert(((Start.getOffset() < NextLocalOffset && 1086499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis Start.getOffset()+Length <= NextLocalOffset) || 1087499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis (Start.getOffset() >= CurrentLoadedOffset && 1088499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis Start.getOffset()+Length < MaxLoadedOffset)) && 1089499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis "Chunk is not valid SLoc address space"); 1090499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis unsigned LocOffs = Loc.getOffset(); 1091499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis unsigned BeginOffs = Start.getOffset(); 1092499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis unsigned EndOffs = BeginOffs + Length; 1093499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis if (LocOffs >= BeginOffs && LocOffs < EndOffs) { 1094499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis if (RelativeOffset) 1095499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis *RelativeOffset = LocOffs - BeginOffs; 1096499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis return true; 1097499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis } 1098499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis 1099499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis return false; 1100499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis } 1101499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis 11022c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay /// \brief Return true if both \p LHS and \p RHS are in the local source 110312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// location address space or the loaded one. 110412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// 110512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// If it's true and \p RelativeOffset is non-null, it will be set to the 110612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// offset of \p RHS relative to \p LHS. 1107b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis bool isInSameSLocAddrSpace(SourceLocation LHS, SourceLocation RHS, 1108b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis int *RelativeOffset) const { 1109b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis unsigned LHSOffs = LHS.getOffset(), RHSOffs = RHS.getOffset(); 1110b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis bool LHSLoaded = LHSOffs >= CurrentLoadedOffset; 1111b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis bool RHSLoaded = RHSOffs >= CurrentLoadedOffset; 1112b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis 1113b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis if (LHSLoaded == RHSLoaded) { 1114b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis if (RelativeOffset) 1115b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis *RelativeOffset = RHSOffs - LHSOffs; 1116b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis return true; 1117b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis } 1118b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis 1119b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis return false; 1120b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis } 1121b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis 1122de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner //===--------------------------------------------------------------------===// 1123de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner // Queries about the code at a SourceLocation. 1124de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner //===--------------------------------------------------------------------===// 11251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 112612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Return a pointer to the start of the specified location 1127de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// in the appropriate spelling MemoryBuffer. 112850f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor /// 112950f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor /// \param Invalid If non-NULL, will be set \c true if an error occurs. 113050f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor const char *getCharacterData(SourceLocation SL, bool *Invalid = 0) const; 11311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 113212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Return the column # for the specified file position. 113312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// 11349dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner /// This is significantly cheaper to compute than the line number. This 11353201f382956ed9beee9fb31229c2835c1208889cChandler Carruth /// returns zero if the column number isn't known. This may only be called 11363201f382956ed9beee9fb31229c2835c1208889cChandler Carruth /// on a file sloc, so you must choose a spelling or expansion location 1137f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner /// before calling this method. 11385330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher unsigned getColumnNumber(FileID FID, unsigned FilePos, 113950f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor bool *Invalid = 0) const; 11405ef04ee40c3332d31b6d1439f50d0ddb45812929Chandler Carruth unsigned getSpellingColumnNumber(SourceLocation Loc, bool *Invalid = 0) const; 1141a77c031cb66f75d22672070052cc6e0205289ff8Chandler Carruth unsigned getExpansionColumnNumber(SourceLocation Loc, 1142b49dcd249c7f4f034493741f95394987a4ccf244Chandler Carruth bool *Invalid = 0) const; 11435ef04ee40c3332d31b6d1439f50d0ddb45812929Chandler Carruth unsigned getPresumedColumnNumber(SourceLocation Loc, bool *Invalid = 0) const; 11441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 114612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Given a SourceLocation, return the spelling line number 114712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// for the position indicated. 114812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// 114912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// This requires building and caching a table of line offsets for the 115012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// MemoryBuffer, so this is not cheap: use only when about to emit a 115112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// diagnostic. 115250f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = 0) const; 11535ef04ee40c3332d31b6d1439f50d0ddb45812929Chandler Carruth unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = 0) const; 1154642116259e8df6286063a17361c20e95b5017a0aChandler Carruth unsigned getExpansionLineNumber(SourceLocation Loc, bool *Invalid = 0) const; 11555ef04ee40c3332d31b6d1439f50d0ddb45812929Chandler Carruth unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = 0) const; 11561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1157c351d9837ea1d0b04842497e76c76125426a982cJames Dennett /// \brief Return the filename or buffer identifier of the buffer the 1158c351d9837ea1d0b04842497e76c76125426a982cJames Dennett /// location is in. 1159c351d9837ea1d0b04842497e76c76125426a982cJames Dennett /// 1160c351d9837ea1d0b04842497e76c76125426a982cJames Dennett /// Note that this name does not respect \#line directives. Use 1161c351d9837ea1d0b04842497e76c76125426a982cJames Dennett /// getPresumedLoc for normal clients. 116250f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor const char *getBufferName(SourceLocation Loc, bool *Invalid = 0) const; 11631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 116412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Return the file characteristic of the specified source 116512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// location, indicating whether this is a normal file, a system 11666b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner /// header, or an "implicit extern C" system header. 11676b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner /// 11686b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner /// This state can be modified with flags on GNU linemarker directives like: 1169c351d9837ea1d0b04842497e76c76125426a982cJames Dennett /// \code 11706b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner /// # 4 "foo.h" 3 1171c351d9837ea1d0b04842497e76c76125426a982cJames Dennett /// \endcode 11726b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner /// which changes all source locations in the current file after that to be 11736b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner /// considered to be from a system header. 11746b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const; 11751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 117612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Returns the "presumed" location of a SourceLocation specifies. 117712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// 117812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// A "presumed location" can be modified by \#line or GNU line marker 117912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// directives. This provides a view on the data that a user should see 118012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// in diagnostics, for example. 1181b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner /// 11823201f382956ed9beee9fb31229c2835c1208889cChandler Carruth /// Note that a presumed location is always given as the expansion point of 11833201f382956ed9beee9fb31229c2835c1208889cChandler Carruth /// an expansion location, not at the spelling location. 1184cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor /// 1185cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor /// \returns The presumed location of the specified SourceLocation. If the 1186cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor /// presumed location cannot be calculate (e.g., because \p Loc is invalid 1187cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor /// or the file containing \p Loc has changed on disk), returns an invalid 1188cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor /// presumed location. 1189b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner PresumedLoc getPresumedLoc(SourceLocation Loc) const; 11901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 119112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Returns true if both SourceLocations correspond to the same file. 11929fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const { 1193a11d61793341fea195c29a0dab3fbd74f2b39a8cChris Lattner return getFileID(Loc1) == getFileID(Loc2); 11949fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek } 11951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 119612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Returns true if the file of provided SourceLocation is the main 119712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// file. 11989fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek bool isFromMainFile(SourceLocation Loc) const { 1199a11d61793341fea195c29a0dab3fbd74f2b39a8cChris Lattner return getFileID(Loc) == getMainFileID(); 12001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } 12011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 120212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Returns if a SourceLocation is in a system header. 12037bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber bool isInSystemHeader(SourceLocation Loc) const { 12040b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner return getFileCharacteristic(Loc) != SrcMgr::C_User; 1205721818304ac462d8c6ce05eecd02884033db78f1Chris Lattner } 12061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 120712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Returns if a SourceLocation is in an "extern C" system header. 12080d456588acac0713a7c33063922d35a8cc8c658eChris Lattner bool isInExternCSystemHeader(SourceLocation Loc) const { 12090d456588acac0713a7c33063922d35a8cc8c658eChris Lattner return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem; 12100d456588acac0713a7c33063922d35a8cc8c658eChris Lattner } 12111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1212d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay /// \brief Returns whether \p Loc is expanded from a macro in a system header. 1213d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay bool isInSystemMacro(SourceLocation loc) { 1214d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay return loc.isMacroID() && isInSystemHeader(getSpellingLoc(loc)); 1215d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay } 1216d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay 12172c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay /// \brief The size of the SLocEnty that \p FID represents. 1218984e42ca1ff7775ce39372c314f1cb7d6862c4c7Argyrios Kyrtzidis unsigned getFileIDSize(FileID FID) const; 121954232ade44d31e98ea83f43ca066128e315dcbdaArgyrios Kyrtzidis 12202c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay /// \brief Given a specific FileID, returns true if \p Loc is inside that 12212c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay /// FileID chunk and sets relative offset (offset of \p Loc from beginning 12222c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay /// of FileID) to \p relativeOffset. 1223d60a34a4e514ec0dfddd05ef2744be104e111f45Argyrios Kyrtzidis bool isInFileID(SourceLocation Loc, FileID FID, 1224d60a34a4e514ec0dfddd05ef2744be104e111f45Argyrios Kyrtzidis unsigned *RelativeOffset = 0) const { 1225d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis unsigned Offs = Loc.getOffset(); 1226d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis if (isOffsetInFileID(FID, Offs)) { 1227d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis if (RelativeOffset) 1228d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis *RelativeOffset = Offs - getSLocEntry(FID).getOffset(); 1229d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis return true; 1230d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis } 1231d60a34a4e514ec0dfddd05ef2744be104e111f45Argyrios Kyrtzidis 1232d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis return false; 1233d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis } 1234469244a322dd5d35cee1d02d70a2edbc12ac5ce7Argyrios Kyrtzidis 123506a062dc784c609b75dca15fd97f468d0d846596Chris Lattner //===--------------------------------------------------------------------===// 12365b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner // Line Table Manipulation Routines 12375b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner //===--------------------------------------------------------------------===// 12381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 123912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Return the uniqued ID for the specified filename. 12401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump /// 1241686775deca8b8685eb90801495880e3abdd844c2Chris Lattner unsigned getLineTableFilenameID(StringRef Str); 12421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 124312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Add a line note to the line table for the FileID and offset 124412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// specified by Loc. 124512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// 124612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// If FilenameID is -1, it is considered to be unspecified. 12474c4ea17d7f991516c37a871dfa4bbe5723fa85f0Chris Lattner void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID); 12489d79ebac47ffde6a1cb312f4c09b66b1b9a397fbChris Lattner void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID, 12491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump bool IsFileEntry, bool IsFileExit, 12509d79ebac47ffde6a1cb312f4c09b66b1b9a397fbChris Lattner bool IsSystemHeader, bool IsExternCHeader); 1251bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor 1252bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor /// \brief Determine if the source manager has a line table. 1253bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor bool hasLineTable() const { return LineTable != 0; } 1254bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor 1255bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor /// \brief Retrieve the stored line table. 1256bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor LineTableInfo &getLineTable(); 1257bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor 12585b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner //===--------------------------------------------------------------------===// 1259457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek // Queries for performance analysis. 1260457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek //===--------------------------------------------------------------------===// 1261457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek 126212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Return the total amount of physical memory allocated by the 1263457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek /// ContentCache allocator. 1264457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek size_t getContentCacheSize() const { 1265457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek return ContentCacheAlloc.getTotalMemory(); 1266457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek } 12675330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher 1268f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek struct MemoryBufferSizes { 1269f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek const size_t malloc_bytes; 1270f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek const size_t mmap_bytes; 12715330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher 1272f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes) 1273f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {} 1274f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek }; 1275f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek 127612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Return the amount of memory used by memory buffers, breaking down 1277f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek /// by heap-backed versus mmap'ed memory. 1278f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek MemoryBufferSizes getMemoryBufferSizes() const; 12795330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher 128012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Return the amount of memory used for various side tables and 128112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// data structures in the SourceManager. 1282ca7dc2b755eb81ac95121ce1a1f1aa44a4a0fe12Ted Kremenek size_t getDataStructureSizes() const; 1283457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek 1284457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek //===--------------------------------------------------------------------===// 128506a062dc784c609b75dca15fd97f468d0d846596Chris Lattner // Other miscellaneous methods. 128606a062dc784c609b75dca15fd97f468d0d846596Chris Lattner //===--------------------------------------------------------------------===// 128710b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis 128810b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis /// \brief Get the source location for the given file:line:col triplet. 128910b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis /// 129010b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis /// If the source file is included multiple times, the source location will 129110b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis /// be based upon the first inclusion. 1292ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis SourceLocation translateFileLineCol(const FileEntry *SourceFile, 1293507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis unsigned Line, unsigned Col) const; 1294ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis 1295b201e16e0c331b0bdeae7b30f9f79aae32beb1b2Argyrios Kyrtzidis /// \brief Get the FileID for the given file. 1296b201e16e0c331b0bdeae7b30f9f79aae32beb1b2Argyrios Kyrtzidis /// 1297b201e16e0c331b0bdeae7b30f9f79aae32beb1b2Argyrios Kyrtzidis /// If the source file is included multiple times, the FileID will be the 1298b201e16e0c331b0bdeae7b30f9f79aae32beb1b2Argyrios Kyrtzidis /// first inclusion. 1299b201e16e0c331b0bdeae7b30f9f79aae32beb1b2Argyrios Kyrtzidis FileID translateFile(const FileEntry *SourceFile) const; 1300b201e16e0c331b0bdeae7b30f9f79aae32beb1b2Argyrios Kyrtzidis 13012c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay /// \brief Get the source location in \p FID for the given line:col. 13022c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay /// Returns null location if \p FID is not a file SLocEntry. 1303507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis SourceLocation translateLineCol(FileID FID, 1304507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis unsigned Line, unsigned Col) const; 1305efa2ff8603dae51f5f5ed7509a503f477498ad22Argyrios Kyrtzidis 13062c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay /// \brief If \p Loc points inside a function macro argument, the returned 1307ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis /// location will be the macro location in which the argument was expanded. 1308ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis /// If a macro argument is used multiple times, the expanded location will 1309ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis /// be at the first expansion of the argument. 1310ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis /// e.g. 1311ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis /// MY_MACRO(foo); 1312ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis /// ^ 1313ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis /// Passing a file location pointing at 'foo', will yield a macro location 1314ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis /// where 'foo' was expanded into. 1315507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis SourceLocation getMacroArgExpandedLocation(SourceLocation Loc) const; 13161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13172aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis /// \brief Determines the order of 2 source locations in the translation unit. 13182aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis /// 13192aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis /// \returns true if LHS source location comes before RHS, false otherwise. 13202aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const; 13212aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis 1322b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis /// \brief Determines the order of 2 source locations in the "source location 1323b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis /// address space". 13245d579e7b0463a2e65328df85e4fed8e07799fd9eArgyrios Kyrtzidis bool isBeforeInSLocAddrSpace(SourceLocation LHS, SourceLocation RHS) const { 13255d579e7b0463a2e65328df85e4fed8e07799fd9eArgyrios Kyrtzidis return isBeforeInSLocAddrSpace(LHS, RHS.getOffset()); 1326b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis } 1327b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis 1328b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis /// \brief Determines the order of a source location and a source location 1329b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis /// offset in the "source location address space". 1330f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// 13315330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher /// Note that we always consider source locations loaded from 13325d579e7b0463a2e65328df85e4fed8e07799fd9eArgyrios Kyrtzidis bool isBeforeInSLocAddrSpace(SourceLocation LHS, unsigned RHS) const { 1333f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor unsigned LHSOffset = LHS.getOffset(); 1334f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor bool LHSLoaded = LHSOffset >= CurrentLoadedOffset; 1335f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor bool RHSLoaded = RHS >= CurrentLoadedOffset; 1336f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor if (LHSLoaded == RHSLoaded) 13375d579e7b0463a2e65328df85e4fed8e07799fd9eArgyrios Kyrtzidis return LHSOffset < RHS; 13385330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher 1339f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor return LHSLoaded; 1340b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis } 1341b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis 1342c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner // Iterators over FileInfos. 13430d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> 13440d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner ::const_iterator fileinfo_iterator; 1345c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); } 1346c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner fileinfo_iterator fileinfo_end() const { return FileInfos.end(); } 1347d93256e55673a17d18543397ec462416acb13792Douglas Gregor bool hasFileInfo(const FileEntry *File) const { 1348d93256e55673a17d18543397ec462416acb13792Douglas Gregor return FileInfos.find(File) != FileInfos.end(); 1349d93256e55673a17d18543397ec462416acb13792Douglas Gregor } 1350c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner 135112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett /// \brief Print statistics to stderr. 13525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer /// 13535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer void PrintStats() const; 135478d85f53b093867bbb0123f016956178eea7343eTed Kremenek 1355f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// \brief Get the number of local SLocEntries we have. 1356f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor unsigned local_sloc_entry_size() const { return LocalSLocEntryTable.size(); } 13575330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher 1358f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// \brief Get a local SLocEntry. This is exposed for indexing. 13595330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher const SrcMgr::SLocEntry &getLocalSLocEntry(unsigned Index, 1360f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor bool *Invalid = 0) const { 1361f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor assert(Index < LocalSLocEntryTable.size() && "Invalid index"); 1362f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor return LocalSLocEntryTable[Index]; 1363bdfe48ac80573e026595af91e541474dbf02565fDouglas Gregor } 13645330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher 1365f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// \brief Get the number of loaded SLocEntries we have. 1366f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor unsigned loaded_sloc_entry_size() const { return LoadedSLocEntryTable.size();} 13675330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher 1368f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// \brief Get a loaded SLocEntry. This is exposed for indexing. 136970042f5d1ca378138f90b7b9384023701f5d03d8David Blaikie const SrcMgr::SLocEntry &getLoadedSLocEntry(unsigned Index, 137070042f5d1ca378138f90b7b9384023701f5d03d8David Blaikie bool *Invalid = 0) const { 1371f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor assert(Index < LoadedSLocEntryTable.size() && "Invalid index"); 1372a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis if (SLocEntryLoaded[Index]) 1373a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis return LoadedSLocEntryTable[Index]; 1374a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis return loadSLocEntry(Index, Invalid); 1375f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor } 13765330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher 1377e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor const SrcMgr::SLocEntry &getSLocEntry(FileID FID, bool *Invalid = 0) const { 1378c705d2520a51de1dc38d36efada8e9bc2d8b0d1fArgyrios Kyrtzidis if (FID.ID == 0 || FID.ID == -1) { 1379c705d2520a51de1dc38d36efada8e9bc2d8b0d1fArgyrios Kyrtzidis if (Invalid) *Invalid = true; 1380c705d2520a51de1dc38d36efada8e9bc2d8b0d1fArgyrios Kyrtzidis return LocalSLocEntryTable[0]; 1381c705d2520a51de1dc38d36efada8e9bc2d8b0d1fArgyrios Kyrtzidis } 1382f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor return getSLocEntryByID(FID.ID); 1383bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor } 1384bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor 1385f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor unsigned getNextLocalOffset() const { return NextLocalOffset; } 13865330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher 1387f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor void setExternalSLocEntrySource(ExternalSLocEntrySource *Source) { 1388f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor assert(LoadedSLocEntryTable.empty() && 1389f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor "Invalidating existing loaded entries"); 1390f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor ExternalSLocEntries = Source; 1391f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor } 13925330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher 1393f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// \brief Allocate a number of loaded SLocEntries, which will be actually 1394f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// loaded on demand from the external source. 1395f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// 1396f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// NumSLocEntries will be allocated, which occupy a total of TotalSize space 1397f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// in the global source view. The lowest ID and the base offset of the 1398f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// entries will be returned. 1399f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor std::pair<int, unsigned> 1400f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor AllocateLoadedSLocEntries(unsigned NumSLocEntries, unsigned TotalSize); 14015330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher 14022c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay /// \brief Returns true if \p Loc came from a PCH/Module. 1403aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis bool isLoadedSourceLocation(SourceLocation Loc) const { 1404aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis return Loc.getOffset() >= CurrentLoadedOffset; 1405aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis } 1406aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis 14072c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay /// \brief Returns true if \p Loc did not come from a PCH/Module. 1408aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis bool isLocalSourceLocation(SourceLocation Loc) const { 1409aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis return Loc.getOffset() < NextLocalOffset; 1410aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis } 1411aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis 14122c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay /// \brief Returns true if \p FID came from a PCH/Module. 1413718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis bool isLoadedFileID(FileID FID) const { 1414718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis assert(FID.ID != -1 && "Using FileID sentinel value"); 1415718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis return FID.ID < 0; 1416718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis } 1417718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis 14182c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay /// \brief Returns true if \p FID did not come from a PCH/Module. 1419718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis bool isLocalFileID(FileID FID) const { 1420718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis return !isLoadedFileID(FID); 1421718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis } 1422718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis 142329271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay /// Get a presumed location suitable for displaying in a diagnostic message, 142429271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay /// taking into account macro arguments and expansions. 142529271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay PresumedLoc getPresumedLocForDisplay(SourceLocation Loc) const { 142629271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay // This is a condensed form of the algorithm used by emitCaretDiagnostic to 142729271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay // walk to the top of the macro call stack. 142829271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay while (Loc.isMacroID()) { 142929271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay Loc = skipToMacroArgExpansion(Loc); 143029271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay Loc = getImmediateMacroCallerLoc(Loc); 143129271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay } 143229271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay 143329271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay return getPresumedLoc(Loc); 143429271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay } 143529271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay 143629271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay /// Look through spelling locations for a macro argument expansion, and if 143729271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay /// found skip to it so that we can trace the argument rather than the macros 143829271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay /// in which that argument is used. If no macro argument expansion is found, 143929271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay /// don't skip anything and return the starting location. 144029271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay SourceLocation skipToMacroArgExpansion(SourceLocation StartLoc) const { 144129271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay for (SourceLocation L = StartLoc; L.isMacroID(); 144229271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay L = getImmediateSpellingLoc(L)) { 144329271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay if (isMacroArgExpansion(L)) 144429271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay return L; 144529271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay } 144629271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay // Otherwise just return initial location, there's nothing to skip. 144729271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay return StartLoc; 144829271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay } 144929271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay 145029271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay /// Gets the location of the immediate macro caller, one level up the stack 145129271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay /// toward the initial macro typed into the source. 145229271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay SourceLocation getImmediateMacroCallerLoc(SourceLocation Loc) const { 145329271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay if (!Loc.isMacroID()) return Loc; 145429271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay 145529271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay // When we have the location of (part of) an expanded parameter, its 145629271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay // spelling location points to the argument as typed into the macro call, 145729271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay // and therefore is used to locate the macro caller. 145829271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay if (isMacroArgExpansion(Loc)) 145929271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay return getImmediateSpellingLoc(Loc); 146029271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay 146129271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay // Otherwise, the caller of the macro is located where this macro is 146229271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay // expanded (while the spelling is part of the macro definition). 146329271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay return getImmediateExpansionRange(Loc).first; 146429271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay } 146529271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay 146629271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay /// Gets the location of the immediate macro callee, one level down the stack 146729271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay /// toward the leaf macro. 146829271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay SourceLocation getImmediateMacroCalleeLoc(SourceLocation Loc) const { 146929271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay if (!Loc.isMacroID()) return Loc; 147029271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay 147129271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay // When we have the location of (part of) an expanded parameter, its 147229271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay // expansion location points to the unexpanded parameter reference within 147329271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay // the macro definition (or callee). 147429271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay if (isMacroArgExpansion(Loc)) 147529271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay return getImmediateExpansionRange(Loc).first; 147629271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay 147729271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay // Otherwise, the callee of the macro is located where this location was 147829271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay // spelled inside the macro definition. 147929271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay return getImmediateSpellingLoc(Loc); 148029271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay } 148129271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay 14825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerprivate: 1483e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor const llvm::MemoryBuffer *getFakeBufferForRecovery() const; 1484a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis const SrcMgr::ContentCache *getFakeContentCacheForRecovery() const; 1485a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis 1486a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis const SrcMgr::SLocEntry &loadSLocEntry(unsigned Index, bool *Invalid) const; 1487e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor 1488f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor /// \brief Get the entry with the given unwrapped FileID. 1489f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor const SrcMgr::SLocEntry &getSLocEntryByID(int ID) const { 1490f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor assert(ID != -1 && "Using FileID sentinel value"); 1491f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor if (ID < 0) 1492f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor return getLoadedSLocEntryByID(ID); 1493f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor return getLocalSLocEntry(static_cast<unsigned>(ID)); 1494f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor } 14955330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher 1496a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis const SrcMgr::SLocEntry &getLoadedSLocEntryByID(int ID, 1497a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis bool *Invalid = 0) const { 1498a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis return getLoadedSLocEntry(static_cast<unsigned>(-ID - 2), Invalid); 1499f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor } 15005330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher 1501af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// Implements the common elements of storing an expansion info struct into 1502af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// the SLocEntry table and producing a source location that refers to it. 150378df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth SourceLocation createExpansionLocImpl(const SrcMgr::ExpansionInfo &Expansion, 1504bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth unsigned TokLength, 1505bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth int LoadedID = 0, 1506bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth unsigned LoadedOffset = 0); 1507c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth 1508af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// \brief Return true if the specified FileID contains the 1509de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner /// specified SourceLocation offset. This is a very hot method. 1510de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const { 1511de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner const SrcMgr::SLocEntry &Entry = getSLocEntry(FID); 1512de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner // If the entry is after the offset, it can't contain it. 1513de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner if (SLocOffset < Entry.getOffset()) return false; 15141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1515f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor // If this is the very last entry then it does. 1516f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor if (FID.ID == -2) 1517f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor return true; 1518f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor 1519f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor // If it is the last local entry, then it does if the location is local. 1520b2efdf3e92f986956f0a755ba815f3870838583bBenjamin Kramer if (FID.ID+1 == static_cast<int>(LocalSLocEntryTable.size())) 1521f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor return SLocOffset < NextLocalOffset; 15227f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor 1523f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor // Otherwise, the entry after it has to not include it. This works for both 1524f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor // local and loaded entries. 1525b2efdf3e92f986956f0a755ba815f3870838583bBenjamin Kramer return SLocOffset < getSLocEntryByID(FID.ID+1).getOffset(); 1526de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner } 15271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1528af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// \brief Create a new fileID for the specified ContentCache and 1529af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// include position. 1530af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// 1531af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// This works regardless of whether the ContentCache corresponds to a 1532af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// file or some other input source. 15332b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner FileID createFileID(const SrcMgr::ContentCache* File, 15342b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner SourceLocation IncludePos, 15357f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor SrcMgr::CharacteristicKind DirCharacter, 1536f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor int LoadedID, unsigned LoadedOffset); 15371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1538de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner const SrcMgr::ContentCache * 1539ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis getOrCreateContentCache(const FileEntry *SourceFile, 1540ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis bool isSystemFile = false); 1541c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek 1542af50aab0c317462129d73ae8000c6394c718598dJames Dennett /// \brief Create a new ContentCache for the specified memory buffer. 15431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const SrcMgr::ContentCache* 15442b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner createMemBufferContentCache(const llvm::MemoryBuffer *Buf); 15451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1546de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner FileID getFileIDSlow(unsigned SLocOffset) const; 1547f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor FileID getFileIDLocal(unsigned SLocOffset) const; 1548f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor FileID getFileIDLoaded(unsigned SLocOffset) const; 1549de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner 1550f84ef95ecec34f27fd05eb4e0392ca6bd3bd0be0Chandler Carruth SourceLocation getExpansionLocSlowCase(SourceLocation Loc) const; 1551addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const; 1552796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis SourceLocation getFileLocSlowCase(SourceLocation Loc) const; 1553addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner 1554de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner std::pair<FileID, unsigned> 1555e7b2b6e87dbe5b1207f77b6ff9c210a02f95bb39Chandler Carruth getDecomposedExpansionLocSlowCase(const SrcMgr::SLocEntry *E) const; 1556de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner std::pair<FileID, unsigned> 1557de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E, 1558de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner unsigned Offset) const; 1559fb3612ef197cb8532c05f33889ec1aed7c26e5cbArgyrios Kyrtzidis void computeMacroArgsCache(MacroArgsMap *&MacroArgsCache, FileID FID) const; 1560ac1ffcc55b861737ba2466cd1ca1accd8eafceaaArgyrios Kyrtzidis 1561ac1ffcc55b861737ba2466cd1ca1accd8eafceaaArgyrios Kyrtzidis friend class ASTReader; 1562ac1ffcc55b861737ba2466cd1ca1accd8eafceaaArgyrios Kyrtzidis friend class ASTWriter; 15635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}; 15645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1565aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko/// \brief Comparison function object. 1566aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenkotemplate<typename T> 1567aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenkoclass BeforeThanCompare; 1568aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko 1569aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko/// \brief Compare two source locations. 1570aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenkotemplate<> 1571aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenkoclass BeforeThanCompare<SourceLocation> { 1572aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko SourceManager &SM; 1573aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko 1574aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenkopublic: 1575aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko explicit BeforeThanCompare(SourceManager &SM) : SM(SM) { } 1576aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko 1577aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko bool operator()(SourceLocation LHS, SourceLocation RHS) const { 1578aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko return SM.isBeforeInTranslationUnit(LHS, RHS); 1579aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko } 1580aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko}; 1581aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko 1582aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko/// \brief Compare two non-overlapping source ranges. 1583aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenkotemplate<> 1584aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenkoclass BeforeThanCompare<SourceRange> { 1585aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko SourceManager &SM; 1586aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko 1587aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenkopublic: 1588aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko explicit BeforeThanCompare(SourceManager &SM) : SM(SM) { } 1589aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko 1590aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko bool operator()(SourceRange LHS, SourceRange RHS) { 1591aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko return SM.isBeforeInTranslationUnit(LHS.getBegin(), RHS.getBegin()); 1592aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko } 1593aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko}; 15945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 15955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} // end namespace clang 15965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 15975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif 1598