SourceManager.h revision 642116259e8df6286063a17361c20e95b5017a0a
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- SourceManager.h - Track and cache source files ---------*- C++ -*-===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file defines the SourceManager interface.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifndef LLVM_CLANG_SOURCEMANAGER_H
155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#define LLVM_CLANG_SOURCEMANAGER_H
165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
17d47d3b0cfeb7e8564ff77f48130fe63282b6d127Chris Lattner#include "clang/Basic/LLVM.h"
185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/SourceLocation.h"
190d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner#include "llvm/Support/Allocator.h"
2003013fa9a0bf1ef4b907f5fec006c8f4000fdd21Michael J. Spencer#include "llvm/Support/DataTypes.h"
21c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor#include "llvm/ADT/PointerIntPair.h"
22aea67dbd653a2dd6dd5cc2159279e81e855b2482Douglas Gregor#include "llvm/ADT/PointerUnion.h"
234f32786ac45210143654390177105eb749b614e9Ted Kremenek#include "llvm/ADT/IntrusiveRefCntPtr.h"
240d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner#include "llvm/ADT/DenseMap.h"
25f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek#include "llvm/Support/MemoryBuffer.h"
265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include <vector>
279dc62f044a6ba21f503bd56607d94b32704e7945Chris Lattner#include <cassert>
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace clang {
301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
31aea67dbd653a2dd6dd5cc2159279e81e855b2482Douglas Gregorclass Diagnostic;
325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass SourceManager;
33099b4747042352f69184481a48508b599a8d3f73Ted Kremenekclass FileManager;
345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass FileEntry;
355b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattnerclass LineTableInfo;
36b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidisclass LangOptions;
37aea67dbd653a2dd6dd5cc2159279e81e855b2482Douglas Gregor
380b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner/// SrcMgr - Public enums and private classes that are part of the
390b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner/// SourceManager implementation.
405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace SrcMgr {
429d72851fec9e9c62570a027d42701562bbf29751Chris Lattner  /// CharacteristicKind - This is used to represent whether a file or directory
430b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner  /// holds normal user code, system code, or system code which is implicitly
440b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner  /// 'extern "C"' in C++ mode.  Entire directories can be tagged with this
450b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner  /// (this is maintained by DirectoryLookup and friends) as can specific
46f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// FileInfos when a #pragma system_header is seen or various other cases.
470b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner  ///
489d72851fec9e9c62570a027d42701562bbf29751Chris Lattner  enum CharacteristicKind {
490b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner    C_User, C_System, C_ExternCSystem
500b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner  };
511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
524710a8ea766b45079bf1c1dd36e29b59bb90829dDan Gohman  /// ContentCache - One instance of this struct is kept for every file
5306a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  /// loaded or used.  This object owns the MemoryBuffer object.
54c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek  class ContentCache {
55f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    enum CCFlags {
56f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor      /// \brief Whether the buffer is invalid.
57f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor      InvalidFlag = 0x01,
58f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor      /// \brief Whether the buffer should not be freed on destruction.
59f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor      DoNotFreeFlag = 0x02
60f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    };
61f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor
62c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    /// Buffer - The actual buffer containing the characters from the input
63c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    /// file.  This is owned by the ContentCache object.
64f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    /// The bits indicate indicates whether the buffer is invalid.
65f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    mutable llvm::PointerIntPair<const llvm::MemoryBuffer *, 2> Buffer;
66c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek
67c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek  public:
68b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    /// Reference to the file entry representing this ContentCache.
69b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    /// This reference does not own the FileEntry object.
70b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    /// It is possible for this to be NULL if
7178d85f53b093867bbb0123f016956178eea7343eTed Kremenek    /// the ContentCache encapsulates an imaginary text buffer.
72b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    const FileEntry *OrigEntry;
73b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis
74b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    /// \brief References the file which the contents were actually loaded from.
75b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    /// Can be different from 'Entry' if we overridden the contents of one file
76b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    /// with the contents of another file.
77b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    const FileEntry *ContentsEntry;
781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
790d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner    /// SourceLineCache - A bump pointer allocated array of offsets for each
800d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner    /// source line.  This is lazily computed.  This is owned by the
810d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner    /// SourceManager BumpPointerAllocator object.
8205816591ec488a933dfecc9ff9f3cbf3c32767c2Chris Lattner    unsigned *SourceLineCache;
831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
84b6427f821de8cce1566fb6e755143ea0918d5543Ted Kremenek    /// NumLines - The number of lines in this ContentCache.  This is only valid
85b6427f821de8cce1566fb6e755143ea0918d5543Ted Kremenek    /// if SourceLineCache is non-null.
865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    unsigned NumLines;
8710b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis
8836c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor    /// getBuffer - Returns the memory buffer for the associated content.
8936c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor    ///
90a92d7e7a55a35b28437103130904a6401bf35408Jonathan D. Turner    /// \param Diag Object through which diagnostics will be emitted if the
9136c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor    /// buffer cannot be retrieved.
9236c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor    ///
93e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner    /// \param Loc If specified, is the location that invalid file diagnostics
94e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner    ///     will be emitted at.
95e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner    ///
9636c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor    /// \param Invalid If non-NULL, will be set \c true if an error occurred.
97e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner    const llvm::MemoryBuffer *getBuffer(Diagnostic &Diag,
98e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner                                        const SourceManager &SM,
99e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner                                        SourceLocation Loc = SourceLocation(),
10036c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor                                        bool *Invalid = 0) const;
1011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
102c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    /// getSize - Returns the size of the content encapsulated by this
103c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    ///  ContentCache. This can be the size of the source file or the size of an
104c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    ///  arbitrary scratch buffer.  If the ContentCache encapsulates a source
105c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    ///  file this size is retrieved from the file's FileEntry.
106c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    unsigned getSize() const;
1071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
108c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    /// getSizeBytesMapped - Returns the number of bytes actually mapped for
109c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    ///  this ContentCache.  This can be 0 if the MemBuffer was not actually
110c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    ///  instantiated.
111c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    unsigned getSizeBytesMapped() const;
112f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek
113f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek    /// Returns the kind of memory used to back the memory buffer for
114f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek    /// this content cache.  This is used for performance analysis.
115f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek    llvm::MemoryBuffer::BufferKind getMemoryBufferKind() const;
1161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11705816591ec488a933dfecc9ff9f3cbf3c32767c2Chris Lattner    void setBuffer(const llvm::MemoryBuffer *B) {
118c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor      assert(!Buffer.getPointer() && "MemoryBuffer already set.");
119c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor      Buffer.setPointer(B);
120c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor      Buffer.setInt(false);
121c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    }
122cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor
123cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor    /// \brief Get the underlying buffer, returning NULL if the buffer is not
124cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor    /// yet available.
125cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor    const llvm::MemoryBuffer *getRawBuffer() const {
126cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor      return Buffer.getPointer();
127cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor    }
1281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1292968442603b029949246467253eeac8139a5b6d8Douglas Gregor    /// \brief Replace the existing buffer (which will be deleted)
1302968442603b029949246467253eeac8139a5b6d8Douglas Gregor    /// with the given buffer.
131f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    void replaceBuffer(const llvm::MemoryBuffer *B, bool DoNotFree = false);
1322968442603b029949246467253eeac8139a5b6d8Douglas Gregor
133f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    /// \brief Determine whether the buffer itself is invalid.
134f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    bool isBufferInvalid() const {
135f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor      return Buffer.getInt() & InvalidFlag;
136f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    }
137f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor
138f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    /// \brief Determine whether the buffer should be freed.
139f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    bool shouldFreeBuffer() const {
140f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor      return (Buffer.getInt() & DoNotFreeFlag) == 0;
141f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    }
142f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor
1430d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner    ContentCache(const FileEntry *Ent = 0)
144b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis      : Buffer(0, false), OrigEntry(Ent), ContentsEntry(Ent),
145b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis        SourceLineCache(0), NumLines(0) {}
146b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis
147b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    ContentCache(const FileEntry *Ent, const FileEntry *contentEnt)
148b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis      : Buffer(0, false), OrigEntry(Ent), ContentsEntry(contentEnt),
149b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis        SourceLineCache(0), NumLines(0) {}
15078d85f53b093867bbb0123f016956178eea7343eTed Kremenek
15178d85f53b093867bbb0123f016956178eea7343eTed Kremenek    ~ContentCache();
1521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1530d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek    /// The copy ctor does not allow copies where source object has either
1540d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek    ///  a non-NULL Buffer or SourceLineCache.  Ownership of allocated memory
155fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner    ///  is not transferred, so this is a logical error.
156c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor    ContentCache(const ContentCache &RHS)
157c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor      : Buffer(0, false), SourceLineCache(0)
158c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor    {
159b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis      OrigEntry = RHS.OrigEntry;
160b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis      ContentsEntry = RHS.ContentsEntry;
1610d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek
162c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor      assert (RHS.Buffer.getPointer() == 0 && RHS.SourceLineCache == 0
1630d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek              && "Passed ContentCache object cannot own a buffer.");
1641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      NumLines = RHS.NumLines;
1660d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek    }
1671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1680d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek  private:
1690d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek    // Disable assignments.
1701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    ContentCache &operator=(const ContentCache& RHS);
1711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  };
1725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
173de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// FileInfo - Information about a FileID, basically just the logical file
174de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// that it represents and include stack information.
1755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
176de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// Each FileInfo has include stack information, indicating where it came
177de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// from.  This information encodes the #include chain that a token was
178de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// instantiated from.  The main include file has an invalid IncludeLoc.
1795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
180de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// FileInfos contain a "ContentCache *", with the contents of the file.
1815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
182de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  class FileInfo {
1835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    /// IncludeLoc - The location of the #include that brought in this file.
184de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    /// This is an invalid SLOC for the main file (top of the #include chain).
185de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned IncludeLoc;  // Really a SourceLocation
1861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1876e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    /// Data - This contains the ContentCache* and the bits indicating the
1886e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    /// characteristic of the file and whether it has #line info, all bitmangled
1896e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    /// together.
1906e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    uintptr_t Data;
19178d85f53b093867bbb0123f016956178eea7343eTed Kremenek  public:
192de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    /// get - Return a FileInfo object.
193de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    static FileInfo get(SourceLocation IL, const ContentCache *Con,
194de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner                        CharacteristicKind FileCharacter) {
195de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      FileInfo X;
196de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      X.IncludeLoc = IL.getRawEncoding();
1976e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner      X.Data = (uintptr_t)Con;
19800282d6e1194655a2e89f940bd6fa8484b52e666Chris Lattner      assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned");
1996e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner      assert((unsigned)FileCharacter < 4 && "invalid file character");
2006e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner      X.Data |= (unsigned)FileCharacter;
2015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return X;
2025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
2031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
204de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    SourceLocation getIncludeLoc() const {
205de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return SourceLocation::getFromRawEncoding(IncludeLoc);
206de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
2076e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    const ContentCache* getContentCache() const {
20800282d6e1194655a2e89f940bd6fa8484b52e666Chris Lattner      return reinterpret_cast<const ContentCache*>(Data & ~7UL);
2096e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    }
2101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2110b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner    /// getCharacteristic - Return whether this is a system header or not.
2121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    CharacteristicKind getFileCharacteristic() const {
2136e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner      return (CharacteristicKind)(Data & 3);
2140b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner    }
215ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner
216ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    /// hasLineDirectives - Return true if this FileID has #line directives in
217ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    /// it.
218ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    bool hasLineDirectives() const { return (Data & 4) != 0; }
2191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
220ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    /// setHasLineDirectives - Set the flag that indicates that this FileID has
221ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    /// line table entries associated with it.
222ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    void setHasLineDirectives() {
223ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner      Data |= 4;
224ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    }
2259dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  };
2261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
227de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// InstantiationInfo - Each InstantiationInfo encodes the Instantiation
228de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// location - where the token was ultimately instantiated, and the
229de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// SpellingLoc - where the actual character data for the token came from.
230de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  class InstantiationInfo {
231e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner     // Really these are all SourceLocations.
2321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
233e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    /// SpellingLoc - Where the spelling for the token can be found.
234e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    unsigned SpellingLoc;
2351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
236e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    /// InstantiationLocStart/InstantiationLocEnd - In a macro expansion, these
23714f79002e58556798e86168c63e48d533287eda5Douglas Gregor    /// indicate the start and end of the instantiation.  In object-like macros,
238e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    /// these will be the same.  In a function-like macro instantiation, the
239c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    /// start will be the identifier and the end will be the ')'.  Finally, in
240c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    /// macro-argument instantitions, the end will be 'SourceLocation()', an
241c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    /// invalid location.
242e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    unsigned InstantiationLocStart, InstantiationLocEnd;
243c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth
2449dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  public:
245de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    SourceLocation getSpellingLoc() const {
246de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return SourceLocation::getFromRawEncoding(SpellingLoc);
247de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
248e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    SourceLocation getInstantiationLocStart() const {
249e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner      return SourceLocation::getFromRawEncoding(InstantiationLocStart);
250e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    }
251e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    SourceLocation getInstantiationLocEnd() const {
252c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth      SourceLocation EndLoc =
253c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth        SourceLocation::getFromRawEncoding(InstantiationLocEnd);
254c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth      return EndLoc.isInvalid() ? getInstantiationLocStart() : EndLoc;
255e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    }
2561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
257e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    std::pair<SourceLocation,SourceLocation> getInstantiationLocRange() const {
258e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner      return std::make_pair(getInstantiationLocStart(),
259e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner                            getInstantiationLocEnd());
260e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    }
2611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
262c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    bool isMacroArgInstantiation() const {
263c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth      // Note that this needs to return false for default constructed objects.
264c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth      return getInstantiationLocStart().isValid() &&
265c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth        SourceLocation::getFromRawEncoding(InstantiationLocEnd).isInvalid();
266c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    }
267c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth
268c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    /// create - Return a InstantiationInfo for an expansion. ILStart and
269c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    /// ILEnd specify the instantiation range (where the macro is expanded),
270c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    /// and SL specifies the spelling location (where the characters from the
271c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    /// token come from). All three can refer to normal File SLocs or
272e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    /// instantiation locations.
273c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    static InstantiationInfo create(SourceLocation SL,
274c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth                                    SourceLocation ILStart,
275c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth                                    SourceLocation ILEnd) {
276de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      InstantiationInfo X;
277de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      X.SpellingLoc = SL.getRawEncoding();
278e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner      X.InstantiationLocStart = ILStart.getRawEncoding();
279e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner      X.InstantiationLocEnd = ILEnd.getRawEncoding();
2809dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner      return X;
2815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
282c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth
283c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    /// createForMacroArg - Return a special InstantiationInfo for the
284c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    /// expansion of a macro argument into a function-like macro's body. IL
285c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    /// specifies the instantiation location (where the macro is expanded).
286c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    /// This doesn't need to be a range because a macro is always instantiated
287c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    /// at a macro parameter reference, and macro parameters are always exactly
288c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    /// one token. SL specifies the spelling location (where the characters
289c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    /// from the token come from). IL and SL can both refer to normal File
290c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    /// SLocs or instantiation locations.
291c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    ///
292c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    /// Given the code:
293c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    /// \code
294c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    ///   #define F(x) f(x)
295c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    ///   F(42);
296c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    /// \endcode
297c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    ///
298c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    /// When expanding '\c F(42)', the '\c x' would call this with an SL
299c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    /// pointing at '\c 42' anad an IL pointing at its location in the
300c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    /// definition of '\c F'.
301c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    static InstantiationInfo createForMacroArg(SourceLocation SL,
302c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth                                               SourceLocation IL) {
303c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth      // We store an intentionally invalid source location for the end of the
304c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth      // instantiation range to mark that this is a macro argument instantation
305c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth      // rather than a normal one.
306c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth      return create(SL, IL, SourceLocation());
307c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    }
308de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  };
3091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
310de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// SLocEntry - This is a discriminated union of FileInfo and
311de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// InstantiationInfo.  SourceManager keeps an array of these objects, and
312de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// they are uniquely identified by the FileID datatype.
313de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  class SLocEntry {
314de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned Offset;   // low bit is set for instantiation info.
315de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    union {
316de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      FileInfo File;
317de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      InstantiationInfo Instantiation;
318de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    };
319de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  public:
320de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned getOffset() const { return Offset >> 1; }
3211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
322de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    bool isInstantiation() const { return Offset & 1; }
323de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    bool isFile() const { return !isInstantiation(); }
3241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
325de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    const FileInfo &getFile() const {
326de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      assert(isFile() && "Not a file SLocEntry!");
327de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return File;
328de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
329de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
330de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    const InstantiationInfo &getInstantiation() const {
331de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      assert(isInstantiation() && "Not an instantiation SLocEntry!");
332de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return Instantiation;
333de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
3341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
335de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    static SLocEntry get(unsigned Offset, const FileInfo &FI) {
336de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      SLocEntry E;
337de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      E.Offset = Offset << 1;
338de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      E.File = FI;
339de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return E;
340de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
341de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
342de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    static SLocEntry get(unsigned Offset, const InstantiationInfo &II) {
343de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      SLocEntry E;
344de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      E.Offset = (Offset << 1) | 1;
345de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      E.Instantiation = II;
346de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return E;
347de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
3485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  };
3495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}  // end SrcMgr namespace.
3507f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
3517f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor/// \brief External source of source location entries.
3527f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregorclass ExternalSLocEntrySource {
3537f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregorpublic:
3547f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  virtual ~ExternalSLocEntrySource();
3557f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
356f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief Read the source location entry with index ID, which will always be
357f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// less than -1.
358e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor  ///
359e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor  /// \returns true if an error occurred that prevented the source-location
360e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor  /// entry from being loaded.
361f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  virtual bool ReadSLocEntry(int ID) = 0;
3627f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor};
363dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner
364dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner
365dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner/// IsBeforeInTranslationUnitCache - This class holds the cache used by
366dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner/// isBeforeInTranslationUnit.  The cache structure is complex enough to be
367dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner/// worth breaking out of SourceManager.
368dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattnerclass IsBeforeInTranslationUnitCache {
369dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// L/R QueryFID - These are the FID's of the cached query.  If these match up
370dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// with a subsequent query, the result can be reused.
371dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  FileID LQueryFID, RQueryFID;
372dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner
373dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// CommonFID - This is the file found in common between the two #include
374dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// traces.  It is the nearest common ancestor of the #include tree.
375dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  FileID CommonFID;
376dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner
377dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// L/R CommonOffset - This is the offset of the previous query in CommonFID.
378dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// Usually, this represents the location of the #include for QueryFID, but if
379dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// LQueryFID is a parent of RQueryFID (or vise versa) then these can be a
380dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// random token in the parent.
381dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  unsigned LCommonOffset, RCommonOffset;
382dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattnerpublic:
383dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner
384dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// isCacheValid - Return true if the currently cached values match up with
385dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// the specified LHS/RHS query.  If not, we can't use the cache.
386dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  bool isCacheValid(FileID LHS, FileID RHS) const {
387dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    return LQueryFID == LHS && RQueryFID == RHS;
388dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  }
389dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner
390dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// getCachedResult - If the cache is valid, compute the result given the
391dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// specified offsets in the LHS/RHS FID's.
392dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  bool getCachedResult(unsigned LOffset, unsigned ROffset) const {
393dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    // If one of the query files is the common file, use the offset.  Otherwise,
394dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    // use the #include loc in the common file.
395dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    if (LQueryFID != CommonFID) LOffset = LCommonOffset;
396dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    if (RQueryFID != CommonFID) ROffset = RCommonOffset;
397dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    return LOffset < ROffset;
398dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  }
399dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner
400dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  // Set up a new query.
401dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  void setQueryFIDs(FileID LHS, FileID RHS) {
402dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    LQueryFID = LHS;
403dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    RQueryFID = RHS;
404dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  }
405dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner
406dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  void setCommonLoc(FileID commonFID, unsigned lCommonOffset,
407dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner                    unsigned rCommonOffset) {
408dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    CommonFID = commonFID;
409dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    LCommonOffset = lCommonOffset;
410dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    RCommonOffset = rCommonOffset;
411dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  }
412dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner
413dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner};
4147f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
415f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor/// \brief This class handles loading and caching of source files into memory.
416f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor///
417f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor/// This object owns the MemoryBuffer objects for all of the loaded
4185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// files and assigns unique FileID's for each unique #include chain.
4195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
4205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// The SourceManager can be queried for information about SourceLocation
421f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner/// objects, turning them into either spelling or instantiation locations.
422f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner/// Spelling locations represent where the bytes corresponding to a token came
423f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner/// from and instantiation locations represent where the location is in the
424f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner/// user's view.  In the case of a macro expansion, for example, the spelling
425e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor/// location indicates  where the expanded token came from and the instantiation
426f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner/// location specifies where it was expanded.
4274f32786ac45210143654390177105eb749b614e9Ted Kremenekclass SourceManager : public llvm::RefCountedBase<SourceManager> {
428f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor  /// \brief Diagnostic object.
429f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor  Diagnostic &Diag;
430389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
431389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis  FileManager &FileMgr;
432389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
4330d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  mutable llvm::BumpPtrAllocator ContentCacheAlloc;
4341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// FileInfos - Memoized information about all of the files tracked by this
4360d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek  /// SourceManager.  This set allows us to merge ContentCache entries based
4370d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek  /// on their FileEntry*.  All ContentCache objects will thus have unique,
4381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// non-null, FileEntry pointers.
4390d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos;
4401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
441299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  /// \brief True if the ContentCache for files that are overriden by other
442299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  /// files, should report the original file name. Defaults to true.
443299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  bool OverridenFilesKeepOriginalName;
444299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis
445b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  /// \brief Files that have been overriden with the contents from another file.
446b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  llvm::DenseMap<const FileEntry *, const FileEntry *> OverriddenFiles;
447b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis
4485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// MemBufferInfos - Information about various memory buffers that we have
4490d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  /// read in.  All FileEntry* within the stored ContentCache objects are NULL,
4500d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  /// as they do not refer to a file.
4510d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  std::vector<SrcMgr::ContentCache*> MemBufferInfos;
4521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
453f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief The table of SLocEntries that are local to this module.
454f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///
455f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// Positive FileIDs are indexes into this table. Entry 0 indicates an invalid
456f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// instantiation.
457f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  std::vector<SrcMgr::SLocEntry> LocalSLocEntryTable;
458f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
459f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief The table of SLocEntries that are loaded from other modules.
460f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///
461f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// Negative FileIDs are indexes into this table. To get from ID to an index,
462f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// use (-ID - 2).
463f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  std::vector<SrcMgr::SLocEntry> LoadedSLocEntryTable;
4647f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
465f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief The starting offset of the next local SLocEntry.
466f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///
467f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// This is LocalSLocEntryTable.back().Offset + the size of that entry.
468f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  unsigned NextLocalOffset;
469f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
470f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief The starting offset of the latest batch of loaded SLocEntries.
471f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///
472f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// This is LoadedSLocEntryTable.back().Offset, except that that entry might
473f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// not have been loaded, so that value would be unknown.
474f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  unsigned CurrentLoadedOffset;
475f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
476f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief A bitmap that indicates whether the entries of LoadedSLocEntryTable
477f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// have already been loaded from the external source.
478f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///
479f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// Same indexing as LoadedSLocEntryTable.
4807f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  std::vector<bool> SLocEntryLoaded;
4817f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
4827f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  /// \brief An external source for source location entries.
4837f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  ExternalSLocEntrySource *ExternalSLocEntries;
4847f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
485de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// LastFileIDLookup - This is a one-entry cache to speed up getFileID.
486de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// LastFileIDLookup records the last FileID looked up or created, because it
487de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// is very common to look up many tokens from the same file.
488de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  mutable FileID LastFileIDLookup;
4891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4905b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  /// LineTable - This holds information for #line directives.  It is referenced
4915b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  /// by indices from SLocEntryTable.
4925b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  LineTableInfo *LineTable;
4931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4945e36a7a89da5d69ece23fc8228624a053f75c645Chris Lattner  /// LastLineNo - These ivars serve as a cache used in the getLineNumber
4955e36a7a89da5d69ece23fc8228624a053f75c645Chris Lattner  /// method which is used to speedup getLineNumber calls to nearby locations.
4962b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  mutable FileID LastLineNoFileIDQuery;
497f812a45dd93634c9300ed5533bd26b56374714a1Chris Lattner  mutable SrcMgr::ContentCache *LastLineNoContentCache;
498f812a45dd93634c9300ed5533bd26b56374714a1Chris Lattner  mutable unsigned LastLineNoFilePos;
499f812a45dd93634c9300ed5533bd26b56374714a1Chris Lattner  mutable unsigned LastLineNoResult;
5001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
50176edd0e4ae0592a7225d50d0bad6732ac64dca2aTed Kremenek  /// MainFileID - The file ID for the main source file of the translation unit.
5022b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID MainFileID;
50349c1f4aa2a6c360d25d605004ec3c4affd62db77Steve Naroff
504de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  // Statistics for -print-stats.
505de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  mutable unsigned NumLinearScans, NumBinaryProbes;
5061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5072aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis  // Cache results for the isBeforeInTranslationUnit method.
508dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  mutable IsBeforeInTranslationUnitCache IsBeforeInTUCache;
5091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
510e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor  // Cache for the "fake" buffer used for error-recovery purposes.
511e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor  mutable llvm::MemoryBuffer *FakeBufferForRecovery;
512e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor
51349c1f4aa2a6c360d25d605004ec3c4affd62db77Steve Naroff  // SourceManager doesn't support copy construction.
51449c1f4aa2a6c360d25d605004ec3c4affd62db77Steve Naroff  explicit SourceManager(const SourceManager&);
5151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  void operator=(const SourceManager&);
5165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
51739b49bcaaddb1049234fca9500c0ac02c088e23dChris Lattner  SourceManager(Diagnostic &Diag, FileManager &FileMgr);
5185b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  ~SourceManager();
5191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5205b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  void clearIDTables();
5211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
52278a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios Kyrtzidis  Diagnostic &getDiagnostics() const { return Diag; }
52378a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios Kyrtzidis
524389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis  FileManager &getFileManager() const { return FileMgr; }
525389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
526299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  /// \brief Set true if the SourceManager should report the original file name
527299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  /// for contents of files that were overriden by other files.Defaults to true.
528299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  void setOverridenFilesKeepOriginalName(bool value) {
529299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis    OverridenFilesKeepOriginalName = value;
530299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  }
531299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis
532f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// createMainFileIDForMembuffer - Create the FileID for a memory buffer
533f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///  that will represent the FileID for the main source.  One example
534f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///  of when this would be used is when the main source is read from STDIN.
535f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) {
536f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    assert(MainFileID.isInvalid() && "MainFileID already set!");
537f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    MainFileID = createFileIDForMemBuffer(Buffer);
538f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    return MainFileID;
539f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  }
540f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
54106a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
54206a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  // MainFileID creation and querying methods.
54306a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
54406a062dc784c609b75dca15fd97f468d0d846596Chris Lattner
54576edd0e4ae0592a7225d50d0bad6732ac64dca2aTed Kremenek  /// getMainFileID - Returns the FileID of the main source file.
5462b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID getMainFileID() const { return MainFileID; }
5471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
54806a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  /// createMainFileID - Create the FileID for the main source file.
549f155dfa4e284be7dca8f1abf002476b2aaee8546Dan Gohman  FileID createMainFileID(const FileEntry *SourceFile) {
55006a062dc784c609b75dca15fd97f468d0d846596Chris Lattner    assert(MainFileID.isInvalid() && "MainFileID already set!");
551f155dfa4e284be7dca8f1abf002476b2aaee8546Dan Gohman    MainFileID = createFileID(SourceFile, SourceLocation(), SrcMgr::C_User);
55206a062dc784c609b75dca15fd97f468d0d846596Chris Lattner    return MainFileID;
55306a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  }
5541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
555414cb64f09ce48a36377458ce5e5a90c3ad41d00Douglas Gregor  /// \brief Set the file ID for the precompiled preamble, which is also the
556414cb64f09ce48a36377458ce5e5a90c3ad41d00Douglas Gregor  /// main file.
557414cb64f09ce48a36377458ce5e5a90c3ad41d00Douglas Gregor  void SetPreambleFileID(FileID Preamble) {
558414cb64f09ce48a36377458ce5e5a90c3ad41d00Douglas Gregor    assert(MainFileID.isInvalid() && "MainFileID already set!");
559414cb64f09ce48a36377458ce5e5a90c3ad41d00Douglas Gregor    MainFileID = Preamble;
560414cb64f09ce48a36377458ce5e5a90c3ad41d00Douglas Gregor  }
561414cb64f09ce48a36377458ce5e5a90c3ad41d00Douglas Gregor
56206a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
563de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  // Methods to create new FileID's and instantiations.
56406a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
5651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// createFileID - Create a new FileID that represents the specified file
567d57b7ff9bebc4c45f325fc1be6f238cfcd4c3732Peter Collingbourne  /// being #included from the specified IncludePosition.  This translates NULL
568d57b7ff9bebc4c45f325fc1be6f238cfcd4c3732Peter Collingbourne  /// into standard input.
5692b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
5707f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                      SrcMgr::CharacteristicKind FileCharacter,
571f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor                      int LoadedID = 0, unsigned LoadedOffset = 0) {
572de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile);
5730d06e998910934e5ef070f53f4c272e7c6b846c6Dan Gohman    assert(IR && "getOrCreateContentCache() cannot return NULL");
574f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    return createFileID(IR, IncludePos, FileCharacter, LoadedID, LoadedOffset);
5755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
5761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// createFileIDForMemBuffer - Create a new FileID that represents the
5785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// specified memory buffer.  This does no caching of the buffer and takes
5795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once.
5807f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer,
581f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor                                  int LoadedID = 0, unsigned LoadedOffset = 0) {
5827bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber    return createFileID(createMemBufferContentCache(Buffer), SourceLocation(),
583f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor                        SrcMgr::C_User, LoadedID, LoadedOffset);
5841036b68525f39cb69ac22c679ed440acd8392a16Ted Kremenek  }
58506a062dc784c609b75dca15fd97f468d0d846596Chris Lattner
586c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth  /// createMacroArgInstantiationLoc - Return a new SourceLocation that encodes
587c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth  /// the fact that a token from SpellingLoc should actually be referenced from
588c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth  /// InstantiationLoc, and that it represents the instantiation of a macro
589c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth  /// argument into the function-like macro body.
590c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth  SourceLocation createMacroArgInstantiationLoc(SourceLocation Loc,
591c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth                                                SourceLocation InstantiationLoc,
592c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth                                                unsigned TokLength);
593c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth
594de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// createInstantiationLoc - Return a new SourceLocation that encodes the fact
595c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth  /// that a token from SpellingLoc should actually be referenced from
596c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth  /// InstantiationLoc.
597de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  SourceLocation createInstantiationLoc(SourceLocation Loc,
598e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner                                        SourceLocation InstantiationLocStart,
599e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner                                        SourceLocation InstantiationLocEnd,
6007f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                                        unsigned TokLength,
601f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor                                        int LoadedID = 0,
602f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor                                        unsigned LoadedOffset = 0);
6031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6042968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \brief Retrieve the memory buffer associated with the given file.
60550f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  ///
60650f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  /// \param Invalid If non-NULL, will be set \c true if an error
60750f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  /// occurs while retrieving the memory buffer.
60850f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File,
60950f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor                                                   bool *Invalid = 0);
6102968442603b029949246467253eeac8139a5b6d8Douglas Gregor
6112968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \brief Override the contents of the given source file by providing an
6122968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// already-allocated buffer.
6132968442603b029949246467253eeac8139a5b6d8Douglas Gregor  ///
614afbf5f810652f04c0bba235c0fa079885fb3caa8Dan Gohman  /// \param SourceFile the source file whose contents will be overriden.
6152968442603b029949246467253eeac8139a5b6d8Douglas Gregor  ///
6162968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \param Buffer the memory buffer whose contents will be used as the
6172968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// data in the given source file.
6182968442603b029949246467253eeac8139a5b6d8Douglas Gregor  ///
619f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// \param DoNotFree If true, then the buffer will not be freed when the
620f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// source manager is destroyed.
6210d06e998910934e5ef070f53f4c272e7c6b846c6Dan Gohman  void overrideFileContents(const FileEntry *SourceFile,
622f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor                            const llvm::MemoryBuffer *Buffer,
623f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor                            bool DoNotFree = false);
6242968442603b029949246467253eeac8139a5b6d8Douglas Gregor
625b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  /// \brief Override the the given source file with another one.
626b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  ///
627b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  /// \param SourceFile the source file which will be overriden.
628b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  ///
629b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  /// \param NewFile the file whose contents will be used as the
630b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  /// data instead of the contents of the given source file.
631b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  void overrideFileContents(const FileEntry *SourceFile,
632b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis                            const FileEntry *NewFile);
633b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis
63406a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
63506a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  // FileID manipulation methods.
63606a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
6371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6382ffb14f004affc363d86d6c7c63c356190db3679Daniel Dunbar  /// getBuffer - Return the buffer for the specified FileID. If there is an
6392ffb14f004affc363d86d6c7c63c356190db3679Daniel Dunbar  /// error opening this buffer the first time, this manufactures a temporary
6402ffb14f004affc363d86d6c7c63c356190db3679Daniel Dunbar  /// buffer and returns a non-empty error string.
641e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner  const llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc,
642e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner                                      bool *Invalid = 0) const {
643e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    bool MyInvalid = false;
644e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
645e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    if (MyInvalid || !Entry.isFile()) {
646e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor      if (Invalid)
647e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor        *Invalid = true;
648e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor
649e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor      return getFakeBufferForRecovery();
650e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    }
651e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor
652e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    return Entry.getFile().getContentCache()->getBuffer(Diag, *this, Loc,
653e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor                                                        Invalid);
65406a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  }
6551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
656e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner  const llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = 0) const {
657e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    bool MyInvalid = false;
658e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
659e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    if (MyInvalid || !Entry.isFile()) {
660e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor      if (Invalid)
661e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor        *Invalid = true;
662e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor
663e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor      return getFakeBufferForRecovery();
664e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    }
665e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor
666e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    return Entry.getFile().getContentCache()->getBuffer(Diag, *this,
667e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor                                                        SourceLocation(),
668e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor                                                        Invalid);
669e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner  }
670e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner
67106a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  /// getFileEntryForID - Returns the FileEntry record for the provided FileID.
67206a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  const FileEntry *getFileEntryForID(FileID FID) const {
673e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    bool MyInvalid = false;
674e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
675e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    if (MyInvalid || !Entry.isFile())
676e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor      return 0;
677e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor
678e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    return Entry.getFile().getContentCache()->OrigEntry;
67906a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  }
6801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6819d5a165d301cc9df68631e624322dd2a962f65b3Ted Kremenek  /// Returns the FileEntry record for the provided SLocEntry.
6829d5a165d301cc9df68631e624322dd2a962f65b3Ted Kremenek  const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const
6839d5a165d301cc9df68631e624322dd2a962f65b3Ted Kremenek  {
6849d5a165d301cc9df68631e624322dd2a962f65b3Ted Kremenek    return sloc.getFile().getContentCache()->OrigEntry;
6859d5a165d301cc9df68631e624322dd2a962f65b3Ted Kremenek  }
6869d5a165d301cc9df68631e624322dd2a962f65b3Ted Kremenek
687ceafc4b63599d14f0b5b10ff92e22bf242682dceBenjamin Kramer  /// getBufferData - Return a StringRef to the source buffer data for the
688ceafc4b63599d14f0b5b10ff92e22bf242682dceBenjamin Kramer  /// specified FileID.
689ceafc4b63599d14f0b5b10ff92e22bf242682dceBenjamin Kramer  ///
690f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor  /// \param FID The file ID whose contents will be returned.
691f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor  /// \param Invalid If non-NULL, will be set true if an error occurred.
692686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  StringRef getBufferData(FileID FID, bool *Invalid = 0) const;
693f6ac97b101c8840efa92bf29166077ce4049e293Benjamin Kramer
6941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
69506a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
69606a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  // SourceLocation manipulation methods.
69706a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
6981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
699668ab1a36d6a5731c71a75a5f388ecafd538a896Chris Lattner  /// getFileID - Return the FileID for a SourceLocation.  This is a very
700de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// hot method that is used for all SourceManager queries that start with a
701de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// SourceLocation object.  It is responsible for finding the entry in
702de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// SLocEntryTable which contains the specified location.
703de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  ///
704de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  FileID getFileID(SourceLocation SpellingLoc) const {
705de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned SLocOffset = SpellingLoc.getOffset();
7061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
707de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    // If our one-entry cache covers this offset, just return it.
708de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
709de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return LastFileIDLookup;
710de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
711de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return getFileIDSlow(SLocOffset);
712de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
7131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7142b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  /// getLocForStartOfFile - Return the source location corresponding to the
7152b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  /// first byte of the specified file.
7162b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  SourceLocation getLocForStartOfFile(FileID FID) const {
717e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    bool Invalid = false;
718e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
719e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    if (Invalid || !Entry.isFile())
720e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor      return SourceLocation();
721e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor
722e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    unsigned FileOffset = Entry.getOffset();
723de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return SourceLocation::getFileLoc(FileOffset);
7242b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  }
7251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
726402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth  /// getExpansionLoc - Given a SourceLocation object, return the expansion
727402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth  /// location referenced by the ID.
728402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth  SourceLocation getExpansionLoc(SourceLocation Loc) const {
729addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner    // Handle the non-mapped case inline, defer to out of line code to handle
730402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth    // expansions.
731de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (Loc.isFileID()) return Loc;
732f84ef95ecec34f27fd05eb4e0392ca6bd3bd0be0Chandler Carruth    return getExpansionLocSlowCase(Loc);
733de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
7341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
735999f739404edf2078cf9f9c28b4dc45c19765842Chandler Carruth  /// getImmediateExpansionRange - Loc is required to be an expansion location.
736999f739404edf2078cf9f9c28b4dc45c19765842Chandler Carruth  /// Return the start/end of the expansion information.
737e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  std::pair<SourceLocation,SourceLocation>
738999f739404edf2078cf9f9c28b4dc45c19765842Chandler Carruth  getImmediateExpansionRange(SourceLocation Loc) const;
7391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
740edc3dccece244a584f8ebdb81da6c962c08e79beChandler Carruth  /// getExpansionRange - Given a SourceLocation object, return the range of
741edc3dccece244a584f8ebdb81da6c962c08e79beChandler Carruth  /// tokens covered by the expansion the ultimate file.
7426678133b8ce642f93e5141f056fa643112041ad0Chris Lattner  std::pair<SourceLocation,SourceLocation>
743edc3dccece244a584f8ebdb81da6c962c08e79beChandler Carruth  getExpansionRange(SourceLocation Loc) const;
7441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
746de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// getSpellingLoc - Given a SourceLocation object, return the spelling
747de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// location referenced by the ID.  This is the place where the characters
748de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// that make up the lexed token can be found.
749de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  SourceLocation getSpellingLoc(SourceLocation Loc) const {
750addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner    // Handle the non-mapped case inline, defer to out of line code to handle
751addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner    // instantiations.
752de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (Loc.isFileID()) return Loc;
753addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner    return getSpellingLocSlowCase(Loc);
754de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
7551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
756387616edf98739f4a0dd234c907e2b913e6a535dChris Lattner  /// getImmediateSpellingLoc - Given a SourceLocation object, return the
757387616edf98739f4a0dd234c907e2b913e6a535dChris Lattner  /// spelling location referenced by the ID.  This is the first level down
758387616edf98739f4a0dd234c907e2b913e6a535dChris Lattner  /// towards the place where the characters that make up the lexed token can be
759387616edf98739f4a0dd234c907e2b913e6a535dChris Lattner  /// found.  This should not generally be used by clients.
7601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const;
761de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
762de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// getDecomposedLoc - Decompose the specified location into a raw FileID +
763de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// Offset pair.  The first element is the FileID, the second is the
764de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// offset from the start of the buffer of the location.
765de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const {
766de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    FileID FID = getFileID(Loc);
767de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return std::make_pair(FID, Loc.getOffset()-getSLocEntry(FID).getOffset());
768de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
7691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
770e7b2b6e87dbe5b1207f77b6ff9c210a02f95bb39Chandler Carruth  /// getDecomposedExpansionLoc - Decompose the specified location into a
771de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// raw FileID + Offset pair.  If the location is an instantiation record,
772de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// walk through it until we find the final location instantiated.
773de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned>
774e7b2b6e87dbe5b1207f77b6ff9c210a02f95bb39Chandler Carruth  getDecomposedExpansionLoc(SourceLocation Loc) const {
775de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    FileID FID = getFileID(Loc);
776de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
7771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
778de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned Offset = Loc.getOffset()-E->getOffset();
779de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (Loc.isFileID())
780de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return std::make_pair(FID, Offset);
7811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
782e7b2b6e87dbe5b1207f77b6ff9c210a02f95bb39Chandler Carruth    return getDecomposedExpansionLocSlowCase(E);
783de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
784de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
785de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// getDecomposedSpellingLoc - Decompose the specified location into a raw
786de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// FileID + Offset pair.  If the location is an instantiation record, walk
787de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// through it until we find its spelling record.
788de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned>
789de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  getDecomposedSpellingLoc(SourceLocation Loc) const {
790de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    FileID FID = getFileID(Loc);
791de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
7921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
793de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned Offset = Loc.getOffset()-E->getOffset();
794de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (Loc.isFileID())
795de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return std::make_pair(FID, Offset);
796de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return getDecomposedSpellingLocSlowCase(E, Offset);
7971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
7981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
79952c29081281955d3db9e11d10573b2d38f709099Chris Lattner  /// getFileOffset - This method returns the offset from the start
80052c29081281955d3db9e11d10573b2d38f709099Chris Lattner  /// of the file that the specified SourceLocation represents. This is not very
80152c29081281955d3db9e11d10573b2d38f709099Chris Lattner  /// meaningful for a macro ID.
80252c29081281955d3db9e11d10573b2d38f709099Chris Lattner  unsigned getFileOffset(SourceLocation SpellingLoc) const {
803de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return getDecomposedLoc(SpellingLoc).second;
8045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
8051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
806c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth  /// isMacroArgInstantiation - This method tests whether the given source
807c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth  /// location represents a macro argument's instantiation into the
808c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth  /// function-like macro definition. Such source locations only appear inside
809c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth  /// of the instantiation locations representing where a particular
810c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth  /// function-like macro was expanded.
811c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth  bool isMacroArgInstantiation(SourceLocation Loc) const;
8121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
813de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  //===--------------------------------------------------------------------===//
814de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  // Queries about the code at a SourceLocation.
815de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  //===--------------------------------------------------------------------===//
8161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getCharacterData - Return a pointer to the start of the specified location
818de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// in the appropriate spelling MemoryBuffer.
81950f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  ///
82050f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  /// \param Invalid If non-NULL, will be set \c true if an error occurs.
82150f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  const char *getCharacterData(SourceLocation SL, bool *Invalid = 0) const;
8221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8239dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  /// getColumnNumber - Return the column # for the specified file position.
8249dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  /// This is significantly cheaper to compute than the line number.  This
8259dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  /// returns zero if the column number isn't known.  This may only be called on
826f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner  /// a file sloc, so you must choose a spelling or instantiation location
827f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner  /// before calling this method.
82850f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  unsigned getColumnNumber(FileID FID, unsigned FilePos,
82950f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor                           bool *Invalid = 0) const;
8305ef04ee40c3332d31b6d1439f50d0ddb45812929Chandler Carruth  unsigned getSpellingColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
831a77c031cb66f75d22672070052cc6e0205289ff8Chandler Carruth  unsigned getExpansionColumnNumber(SourceLocation Loc,
832b49dcd249c7f4f034493741f95394987a4ccf244Chandler Carruth                                    bool *Invalid = 0) const;
8335ef04ee40c3332d31b6d1439f50d0ddb45812929Chandler Carruth  unsigned getPresumedColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
8341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
836df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner  /// getLineNumber - Given a SourceLocation, return the spelling line number
8375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// for the position indicated.  This requires building and caching a table of
8385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// line offsets for the MemoryBuffer, so this is not cheap: use only when
8395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// about to emit a diagnostic.
84050f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = 0) const;
8415ef04ee40c3332d31b6d1439f50d0ddb45812929Chandler Carruth  unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
842642116259e8df6286063a17361c20e95b5017a0aChandler Carruth  unsigned getExpansionLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
8435ef04ee40c3332d31b6d1439f50d0ddb45812929Chandler Carruth  unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
8441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
845bff5c512af8ca7ac92e974e04c06ff4f820e4ee1Chris Lattner  /// Return the filename or buffer identifier of the buffer the location is in.
846bff5c512af8ca7ac92e974e04c06ff4f820e4ee1Chris Lattner  /// Note that this name does not respect #line directives.  Use getPresumedLoc
847bff5c512af8ca7ac92e974e04c06ff4f820e4ee1Chris Lattner  /// for normal clients.
84850f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  const char *getBufferName(SourceLocation Loc, bool *Invalid = 0) const;
8491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8506b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// getFileCharacteristic - return the file characteristic of the specified
8511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// source location, indicating whether this is a normal file, a system
8526b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// header, or an "implicit extern C" system header.
8536b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  ///
8546b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// This state can be modified with flags on GNU linemarker directives like:
8556b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  ///   # 4 "foo.h" 3
8566b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// which changes all source locations in the current file after that to be
8576b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// considered to be from a system header.
8586b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const;
8591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
860b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// getPresumedLoc - This method returns the "presumed" location of a
861b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// SourceLocation specifies.  A "presumed location" can be modified by #line
862b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// or GNU line marker directives.  This provides a view on the data that a
863b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// user should see in diagnostics, for example.
864b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  ///
865b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// Note that a presumed location is always given as the instantiation point
866b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// of an instantiation location, not at the spelling location.
867cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  ///
868cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  /// \returns The presumed location of the specified SourceLocation. If the
869cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  /// presumed location cannot be calculate (e.g., because \p Loc is invalid
870cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  /// or the file containing \p Loc has changed on disk), returns an invalid
871cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  /// presumed location.
872b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  PresumedLoc getPresumedLoc(SourceLocation Loc) const;
8731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8749fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  /// isFromSameFile - Returns true if both SourceLocations correspond to
8759fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  ///  the same file.
8769fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
877a11d61793341fea195c29a0dab3fbd74f2b39a8cChris Lattner    return getFileID(Loc1) == getFileID(Loc2);
8789fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  }
8791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8809fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  /// isFromMainFile - Returns true if the file of provided SourceLocation is
8819fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  ///   the main file.
8829fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  bool isFromMainFile(SourceLocation Loc) const {
883a11d61793341fea195c29a0dab3fbd74f2b39a8cChris Lattner    return getFileID(Loc) == getMainFileID();
8841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
8851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8867bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber  /// isInSystemHeader - Returns if a SourceLocation is in a system header.
8877bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber  bool isInSystemHeader(SourceLocation Loc) const {
8880b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner    return getFileCharacteristic(Loc) != SrcMgr::C_User;
889721818304ac462d8c6ce05eecd02884033db78f1Chris Lattner  }
8901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8910d456588acac0713a7c33063922d35a8cc8c658eChris Lattner  /// isInExternCSystemHeader - Returns if a SourceLocation is in an "extern C"
8920d456588acac0713a7c33063922d35a8cc8c658eChris Lattner  /// system header.
8930d456588acac0713a7c33063922d35a8cc8c658eChris Lattner  bool isInExternCSystemHeader(SourceLocation Loc) const {
8940d456588acac0713a7c33063922d35a8cc8c658eChris Lattner    return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem;
8950d456588acac0713a7c33063922d35a8cc8c658eChris Lattner  }
8961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
897b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis  /// \brief Given a specific chunk of a FileID (FileID with offset+length),
898b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis  /// returns true if \arg Loc is inside that chunk and sets relative offset
899b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis  /// (offset of \arg Loc from beginning of chunk) to \arg relativeOffset.
900b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis  bool isInFileID(SourceLocation Loc,
901b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis                  FileID FID, unsigned offset, unsigned length,
902b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis                  unsigned *relativeOffset = 0) const {
903b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis    assert(!FID.isInvalid());
904b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis    if (Loc.isInvalid())
905b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis      return false;
906b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis
907b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis    unsigned start = getSLocEntry(FID).getOffset() + offset;
908b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis    unsigned end = start + length;
909b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis
910b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis#ifndef NDEBUG
911b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis    // Make sure offset/length describe a chunk inside the given FileID.
912b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis    unsigned NextOffset;
913f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    if (FID.ID == -2)
914f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor      NextOffset = 1U << 31U;
915f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    else if (FID.ID+1 == (int)LocalSLocEntryTable.size())
916f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor      NextOffset = getNextLocalOffset();
917b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis    else
918f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor      NextOffset = getSLocEntryByID(FID.ID+1).getOffset();
919b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis    assert(start < NextOffset);
920b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis    assert(end   < NextOffset);
921b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis#endif
922b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis
923b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis    if (Loc.getOffset() >= start && Loc.getOffset() < end) {
924b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis      if (relativeOffset)
925b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis        *relativeOffset = Loc.getOffset() - start;
926b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis      return true;
927b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis    }
928b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis
929b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis    return false;
930b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis  }
931469244a322dd5d35cee1d02d70a2edbc12ac5ce7Argyrios Kyrtzidis
93206a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
9335b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  // Line Table Manipulation Routines
9345b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  //===--------------------------------------------------------------------===//
9351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9365b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  /// getLineTableFilenameID - Return the uniqued ID for the specified filename.
9371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ///
938686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  unsigned getLineTableFilenameID(StringRef Str);
9391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9404c4ea17d7f991516c37a871dfa4bbe5723fa85f0Chris Lattner  /// AddLineNote - Add a line note to the line table for the FileID and offset
9414c4ea17d7f991516c37a871dfa4bbe5723fa85f0Chris Lattner  /// specified by Loc.  If FilenameID is -1, it is considered to be
9424c4ea17d7f991516c37a871dfa4bbe5723fa85f0Chris Lattner  /// unspecified.
9434c4ea17d7f991516c37a871dfa4bbe5723fa85f0Chris Lattner  void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID);
9449d79ebac47ffde6a1cb312f4c09b66b1b9a397fbChris Lattner  void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID,
9451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                   bool IsFileEntry, bool IsFileExit,
9469d79ebac47ffde6a1cb312f4c09b66b1b9a397fbChris Lattner                   bool IsSystemHeader, bool IsExternCHeader);
947bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
948bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  /// \brief Determine if the source manager has a line table.
949bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  bool hasLineTable() const { return LineTable != 0; }
950bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
951bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  /// \brief Retrieve the stored line table.
952bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  LineTableInfo &getLineTable();
953bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
9545b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  //===--------------------------------------------------------------------===//
955457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek  // Queries for performance analysis.
956457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek  //===--------------------------------------------------------------------===//
957457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek
958457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek  /// Return the total amount of physical memory allocated by the
959457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek  /// ContentCache allocator.
960457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek  size_t getContentCacheSize() const {
961457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek    return ContentCacheAlloc.getTotalMemory();
962457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek  }
963f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek
964f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek  struct MemoryBufferSizes {
965f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek    const size_t malloc_bytes;
966f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek    const size_t mmap_bytes;
967f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek
968f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek    MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)
969f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek      : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {}
970f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek  };
971f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek
972f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek  /// Return the amount of memory used by memory buffers, breaking down
973f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek  /// by heap-backed versus mmap'ed memory.
974f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek  MemoryBufferSizes getMemoryBufferSizes() const;
975457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek
976457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek  //===--------------------------------------------------------------------===//
97706a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  // Other miscellaneous methods.
97806a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
97910b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis
98010b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  /// \brief Get the source location for the given file:line:col triplet.
98110b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  ///
98210b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  /// If the source file is included multiple times, the source location will
98310b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  /// be based upon the first inclusion.
98410b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  SourceLocation getLocation(const FileEntry *SourceFile,
98586a4d0dd6a630639aab7715323ed068940e650afDouglas Gregor                             unsigned Line, unsigned Col);
9861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9872aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis  /// \brief Determines the order of 2 source locations in the translation unit.
9882aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis  ///
9892aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis  /// \returns true if LHS source location comes before RHS, false otherwise.
9902aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis  bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const;
9912aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis
992b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis  /// \brief Determines the order of 2 source locations in the "source location
993b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis  /// address space".
994f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  bool isBeforeInSourceLocationOffset(SourceLocation LHS,
995f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor                                      SourceLocation RHS) const {
996b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis    return isBeforeInSourceLocationOffset(LHS, RHS.getOffset());
997b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis  }
998b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis
999b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis  /// \brief Determines the order of a source location and a source location
1000b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis  /// offset in the "source location address space".
1001f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///
1002f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// Note that we always consider source locations loaded from
1003f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  bool isBeforeInSourceLocationOffset(SourceLocation LHS, unsigned RHS) const {
1004f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    unsigned LHSOffset = LHS.getOffset();
1005f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    bool LHSLoaded = LHSOffset >= CurrentLoadedOffset;
1006f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    bool RHSLoaded = RHS >= CurrentLoadedOffset;
1007f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    if (LHSLoaded == RHSLoaded)
1008f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor      return LHS.getOffset() < RHS;
1009f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
1010f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    return LHSLoaded;
1011b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis  }
1012b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis
1013c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner  // Iterators over FileInfos.
10140d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>
10150d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner      ::const_iterator fileinfo_iterator;
1016c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner  fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); }
1017c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner  fileinfo_iterator fileinfo_end() const { return FileInfos.end(); }
1018d93256e55673a17d18543397ec462416acb13792Douglas Gregor  bool hasFileInfo(const FileEntry *File) const {
1019d93256e55673a17d18543397ec462416acb13792Douglas Gregor    return FileInfos.find(File) != FileInfos.end();
1020d93256e55673a17d18543397ec462416acb13792Douglas Gregor  }
1021c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner
10225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// PrintStats - Print statistics to stderr.
10235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
10245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void PrintStats() const;
102578d85f53b093867bbb0123f016956178eea7343eTed Kremenek
1026f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief Get the number of local SLocEntries we have.
1027f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  unsigned local_sloc_entry_size() const { return LocalSLocEntryTable.size(); }
1028f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
1029f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief Get a local SLocEntry. This is exposed for indexing.
1030f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  const SrcMgr::SLocEntry &getLocalSLocEntry(unsigned Index,
1031f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor                                             bool *Invalid = 0) const {
1032f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    assert(Index < LocalSLocEntryTable.size() && "Invalid index");
1033f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    return LocalSLocEntryTable[Index];
1034bdfe48ac80573e026595af91e541474dbf02565fDouglas Gregor  }
1035f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
1036f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief Get the number of loaded SLocEntries we have.
1037f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  unsigned loaded_sloc_entry_size() const { return LoadedSLocEntryTable.size();}
1038f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
1039f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief Get a loaded SLocEntry. This is exposed for indexing.
1040f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  const SrcMgr::SLocEntry &getLoadedSLocEntry(unsigned Index, bool *Invalid=0) const {
1041f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    assert(Index < LoadedSLocEntryTable.size() && "Invalid index");
1042f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    if (!SLocEntryLoaded[Index])
1043f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor      ExternalSLocEntries->ReadSLocEntry(-(static_cast<int>(Index) + 2));
1044f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    return LoadedSLocEntryTable[Index];
1045f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  }
1046f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
1047e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor  const SrcMgr::SLocEntry &getSLocEntry(FileID FID, bool *Invalid = 0) const {
1048f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    return getSLocEntryByID(FID.ID);
1049bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  }
1050bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
1051f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  unsigned getNextLocalOffset() const { return NextLocalOffset; }
1052f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
1053f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  void setExternalSLocEntrySource(ExternalSLocEntrySource *Source) {
1054f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    assert(LoadedSLocEntryTable.empty() &&
1055f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor           "Invalidating existing loaded entries");
1056f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    ExternalSLocEntries = Source;
1057f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  }
1058f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
1059f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief Allocate a number of loaded SLocEntries, which will be actually
1060f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// loaded on demand from the external source.
1061f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///
1062f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// NumSLocEntries will be allocated, which occupy a total of TotalSize space
1063f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// in the global source view. The lowest ID and the base offset of the
1064f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// entries will be returned.
1065f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  std::pair<int, unsigned>
1066f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  AllocateLoadedSLocEntries(unsigned NumSLocEntries, unsigned TotalSize);
1067f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
10685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerprivate:
1069e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor  const llvm::MemoryBuffer *getFakeBufferForRecovery() const;
1070e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor
1071f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief Get the entry with the given unwrapped FileID.
1072f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  const SrcMgr::SLocEntry &getSLocEntryByID(int ID) const {
1073f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    assert(ID != -1 && "Using FileID sentinel value");
1074f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    if (ID < 0)
1075f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor      return getLoadedSLocEntryByID(ID);
1076f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    return getLocalSLocEntry(static_cast<unsigned>(ID));
1077f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  }
1078f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
1079f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  const SrcMgr::SLocEntry &getLoadedSLocEntryByID(int ID) const {
1080f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    return getLoadedSLocEntry(static_cast<unsigned>(-ID - 2));
1081f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  }
1082f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
1083c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth  /// createInstantiationLoc - Implements the common elements of storing an
1084c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth  /// instantiation info struct into the SLocEntry table and producing a source
1085c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth  /// location that refers to it.
1086c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth  SourceLocation createInstantiationLocImpl(const SrcMgr::InstantiationInfo &II,
1087c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth                                            unsigned TokLength,
1088f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor                                            int LoadedID = 0,
1089f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor                                            unsigned LoadedOffset = 0);
1090c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth
1091de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// isOffsetInFileID - Return true if the specified FileID contains the
1092de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// specified SourceLocation offset.  This is a very hot method.
1093de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const {
1094de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID);
1095de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    // If the entry is after the offset, it can't contain it.
1096de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (SLocOffset < Entry.getOffset()) return false;
10971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1098f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    // If this is the very last entry then it does.
1099f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    if (FID.ID == -2)
1100f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor      return true;
1101f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
1102f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    // If it is the last local entry, then it does if the location is local.
1103f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    if (static_cast<unsigned>(FID.ID+1) == LocalSLocEntryTable.size()) {
1104f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor      return SLocOffset < NextLocalOffset;
1105f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    }
11067f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
1107f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    // Otherwise, the entry after it has to not include it. This works for both
1108f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    // local and loaded entries.
11097f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    return SLocOffset < getSLocEntry(FileID::get(FID.ID+1)).getOffset();
1110de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
11111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
111278d85f53b093867bbb0123f016956178eea7343eTed Kremenek  /// createFileID - Create a new fileID for the specified ContentCache and
111378d85f53b093867bbb0123f016956178eea7343eTed Kremenek  ///  include position.  This works regardless of whether the ContentCache
111478d85f53b093867bbb0123f016956178eea7343eTed Kremenek  ///  corresponds to a file or some other input source.
11152b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID createFileID(const SrcMgr::ContentCache* File,
11162b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner                      SourceLocation IncludePos,
11177f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                      SrcMgr::CharacteristicKind DirCharacter,
1118f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor                      int LoadedID, unsigned LoadedOffset);
11191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1120de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  const SrcMgr::ContentCache *
1121de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    getOrCreateContentCache(const FileEntry *SourceFile);
1122c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek
112378d85f53b093867bbb0123f016956178eea7343eTed Kremenek  /// createMemBufferContentCache - Create a new ContentCache for the specified
112478d85f53b093867bbb0123f016956178eea7343eTed Kremenek  ///  memory buffer.
11251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const SrcMgr::ContentCache*
11262b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  createMemBufferContentCache(const llvm::MemoryBuffer *Buf);
11271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1128de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  FileID getFileIDSlow(unsigned SLocOffset) const;
1129f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  FileID getFileIDLocal(unsigned SLocOffset) const;
1130f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  FileID getFileIDLoaded(unsigned SLocOffset) const;
1131de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
1132f84ef95ecec34f27fd05eb4e0392ca6bd3bd0be0Chandler Carruth  SourceLocation getExpansionLocSlowCase(SourceLocation Loc) const;
1133addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner  SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;
1134addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner
1135de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned>
1136e7b2b6e87dbe5b1207f77b6ff9c210a02f95bb39Chandler Carruth  getDecomposedExpansionLocSlowCase(const SrcMgr::SLocEntry *E) const;
1137de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned>
1138de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
1139de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner                                   unsigned Offset) const;
11405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
11415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}  // end namespace clang
11445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif
1146