SourceManager.h revision 299a4a967b02c9f0d0d94ad8560e3ced893f9116
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
175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/SourceLocation.h"
180d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner#include "llvm/Support/Allocator.h"
1903013fa9a0bf1ef4b907f5fec006c8f4000fdd21Michael J. Spencer#include "llvm/Support/DataTypes.h"
20c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor#include "llvm/ADT/PointerIntPair.h"
21aea67dbd653a2dd6dd5cc2159279e81e855b2482Douglas Gregor#include "llvm/ADT/PointerUnion.h"
220d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner#include "llvm/ADT/DenseMap.h"
235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include <vector>
249dc62f044a6ba21f503bd56607d94b32704e7945Chris Lattner#include <cassert>
255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace llvm {
275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass MemoryBuffer;
28aea67dbd653a2dd6dd5cc2159279e81e855b2482Douglas Gregorclass StringRef;
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace clang {
321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
33aea67dbd653a2dd6dd5cc2159279e81e855b2482Douglas Gregorclass Diagnostic;
345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass SourceManager;
35099b4747042352f69184481a48508b599a8d3f73Ted Kremenekclass FileManager;
365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass FileEntry;
375b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattnerclass LineTableInfo;
38aea67dbd653a2dd6dd5cc2159279e81e855b2482Douglas Gregor
390b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner/// SrcMgr - Public enums and private classes that are part of the
400b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner/// SourceManager implementation.
415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace SrcMgr {
439d72851fec9e9c62570a027d42701562bbf29751Chris Lattner  /// CharacteristicKind - This is used to represent whether a file or directory
440b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner  /// holds normal user code, system code, or system code which is implicitly
450b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner  /// 'extern "C"' in C++ mode.  Entire directories can be tagged with this
460b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner  /// (this is maintained by DirectoryLookup and friends) as can specific
470b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner  /// FileIDInfos when a #pragma system_header is seen or various other cases.
480b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner  ///
499d72851fec9e9c62570a027d42701562bbf29751Chris Lattner  enum CharacteristicKind {
500b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner    C_User, C_System, C_ExternCSystem
510b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner  };
521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
534710a8ea766b45079bf1c1dd36e29b59bb90829dDan Gohman  /// ContentCache - One instance of this struct is kept for every file
5406a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  /// loaded or used.  This object owns the MemoryBuffer object.
55c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek  class ContentCache {
56f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    enum CCFlags {
57f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor      /// \brief Whether the buffer is invalid.
58f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor      InvalidFlag = 0x01,
59f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor      /// \brief Whether the buffer should not be freed on destruction.
60f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor      DoNotFreeFlag = 0x02
61f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    };
62f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor
63c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    /// Buffer - The actual buffer containing the characters from the input
64c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    /// file.  This is owned by the ContentCache object.
65f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    /// The bits indicate indicates whether the buffer is invalid.
66f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    mutable llvm::PointerIntPair<const llvm::MemoryBuffer *, 2> Buffer;
67c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek
68c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek  public:
69b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    /// Reference to the file entry representing this ContentCache.
70b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    /// This reference does not own the FileEntry object.
71b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    /// It is possible for this to be NULL if
7278d85f53b093867bbb0123f016956178eea7343eTed Kremenek    /// the ContentCache encapsulates an imaginary text buffer.
73b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    const FileEntry *OrigEntry;
74b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis
75b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    /// \brief References the file which the contents were actually loaded from.
76b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    /// Can be different from 'Entry' if we overridden the contents of one file
77b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    /// with the contents of another file.
78b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    const FileEntry *ContentsEntry;
791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
800d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner    /// SourceLineCache - A bump pointer allocated array of offsets for each
810d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner    /// source line.  This is lazily computed.  This is owned by the
820d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner    /// SourceManager BumpPointerAllocator object.
8305816591ec488a933dfecc9ff9f3cbf3c32767c2Chris Lattner    unsigned *SourceLineCache;
841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
85b6427f821de8cce1566fb6e755143ea0918d5543Ted Kremenek    /// NumLines - The number of lines in this ContentCache.  This is only valid
86b6427f821de8cce1566fb6e755143ea0918d5543Ted Kremenek    /// if SourceLineCache is non-null.
875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    unsigned NumLines;
8810b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis
8936c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor    /// getBuffer - Returns the memory buffer for the associated content.
9036c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor    ///
9136c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor    /// \param Diag Object through which diagnostics will be emitted it the
9236c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor    /// buffer cannot be retrieved.
9336c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor    ///
94e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner    /// \param Loc If specified, is the location that invalid file diagnostics
95e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner    ///     will be emitted at.
96e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner    ///
9736c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor    /// \param Invalid If non-NULL, will be set \c true if an error occurred.
98e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner    const llvm::MemoryBuffer *getBuffer(Diagnostic &Diag,
99e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner                                        const SourceManager &SM,
100e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner                                        SourceLocation Loc = SourceLocation(),
10136c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor                                        bool *Invalid = 0) const;
1021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
103c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    /// getSize - Returns the size of the content encapsulated by this
104c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    ///  ContentCache. This can be the size of the source file or the size of an
105c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    ///  arbitrary scratch buffer.  If the ContentCache encapsulates a source
106c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    ///  file this size is retrieved from the file's FileEntry.
107c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    unsigned getSize() const;
1081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
109c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    /// getSizeBytesMapped - Returns the number of bytes actually mapped for
110c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    ///  this ContentCache.  This can be 0 if the MemBuffer was not actually
111c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    ///  instantiated.
112c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    unsigned getSizeBytesMapped() const;
1131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11405816591ec488a933dfecc9ff9f3cbf3c32767c2Chris Lattner    void setBuffer(const llvm::MemoryBuffer *B) {
115c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor      assert(!Buffer.getPointer() && "MemoryBuffer already set.");
116c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor      Buffer.setPointer(B);
117c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor      Buffer.setInt(false);
118c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    }
119cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor
120cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor    /// \brief Get the underlying buffer, returning NULL if the buffer is not
121cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor    /// yet available.
122cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor    const llvm::MemoryBuffer *getRawBuffer() const {
123cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor      return Buffer.getPointer();
124cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor    }
1251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1262968442603b029949246467253eeac8139a5b6d8Douglas Gregor    /// \brief Replace the existing buffer (which will be deleted)
1272968442603b029949246467253eeac8139a5b6d8Douglas Gregor    /// with the given buffer.
128f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    void replaceBuffer(const llvm::MemoryBuffer *B, bool DoNotFree = false);
1292968442603b029949246467253eeac8139a5b6d8Douglas Gregor
130f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    /// \brief Determine whether the buffer itself is invalid.
131f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    bool isBufferInvalid() const {
132f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor      return Buffer.getInt() & InvalidFlag;
133f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    }
134f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor
135f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    /// \brief Determine whether the buffer should be freed.
136f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    bool shouldFreeBuffer() const {
137f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor      return (Buffer.getInt() & DoNotFreeFlag) == 0;
138f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    }
139f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor
1400d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner    ContentCache(const FileEntry *Ent = 0)
141b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis      : Buffer(0, false), OrigEntry(Ent), ContentsEntry(Ent),
142b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis        SourceLineCache(0), NumLines(0) {}
143b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis
144b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    ContentCache(const FileEntry *Ent, const FileEntry *contentEnt)
145b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis      : Buffer(0, false), OrigEntry(Ent), ContentsEntry(contentEnt),
146b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis        SourceLineCache(0), NumLines(0) {}
14778d85f53b093867bbb0123f016956178eea7343eTed Kremenek
14878d85f53b093867bbb0123f016956178eea7343eTed Kremenek    ~ContentCache();
1491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1500d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek    /// The copy ctor does not allow copies where source object has either
1510d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek    ///  a non-NULL Buffer or SourceLineCache.  Ownership of allocated memory
1520d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek    ///  is not transfered, so this is a logical error.
153c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor    ContentCache(const ContentCache &RHS)
154c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor      : Buffer(0, false), SourceLineCache(0)
155c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor    {
156b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis      OrigEntry = RHS.OrigEntry;
157b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis      ContentsEntry = RHS.ContentsEntry;
1580d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek
159c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor      assert (RHS.Buffer.getPointer() == 0 && RHS.SourceLineCache == 0
1600d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek              && "Passed ContentCache object cannot own a buffer.");
1611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      NumLines = RHS.NumLines;
1630d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek    }
1641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1650d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek  private:
1660d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek    // Disable assignments.
1671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    ContentCache &operator=(const ContentCache& RHS);
1681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  };
1695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
170de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// FileInfo - Information about a FileID, basically just the logical file
171de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// that it represents and include stack information.
1725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
173de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// Each FileInfo has include stack information, indicating where it came
174de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// from.  This information encodes the #include chain that a token was
175de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// instantiated from.  The main include file has an invalid IncludeLoc.
1765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
177de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// FileInfos contain a "ContentCache *", with the contents of the file.
1785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
179de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  class FileInfo {
1805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    /// IncludeLoc - The location of the #include that brought in this file.
181de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    /// This is an invalid SLOC for the main file (top of the #include chain).
182de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned IncludeLoc;  // Really a SourceLocation
1831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1846e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    /// Data - This contains the ContentCache* and the bits indicating the
1856e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    /// characteristic of the file and whether it has #line info, all bitmangled
1866e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    /// together.
1876e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    uintptr_t Data;
18878d85f53b093867bbb0123f016956178eea7343eTed Kremenek  public:
189de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    /// get - Return a FileInfo object.
190de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    static FileInfo get(SourceLocation IL, const ContentCache *Con,
191de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner                        CharacteristicKind FileCharacter) {
192de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      FileInfo X;
193de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      X.IncludeLoc = IL.getRawEncoding();
1946e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner      X.Data = (uintptr_t)Con;
19500282d6e1194655a2e89f940bd6fa8484b52e666Chris Lattner      assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned");
1966e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner      assert((unsigned)FileCharacter < 4 && "invalid file character");
1976e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner      X.Data |= (unsigned)FileCharacter;
1985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return X;
1995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
2001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
201de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    SourceLocation getIncludeLoc() const {
202de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return SourceLocation::getFromRawEncoding(IncludeLoc);
203de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
2046e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    const ContentCache* getContentCache() const {
20500282d6e1194655a2e89f940bd6fa8484b52e666Chris Lattner      return reinterpret_cast<const ContentCache*>(Data & ~7UL);
2066e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    }
2071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2080b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner    /// getCharacteristic - Return whether this is a system header or not.
2091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    CharacteristicKind getFileCharacteristic() const {
2106e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner      return (CharacteristicKind)(Data & 3);
2110b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner    }
212ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner
213ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    /// hasLineDirectives - Return true if this FileID has #line directives in
214ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    /// it.
215ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    bool hasLineDirectives() const { return (Data & 4) != 0; }
2161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
217ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    /// setHasLineDirectives - Set the flag that indicates that this FileID has
218ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    /// line table entries associated with it.
219ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    void setHasLineDirectives() {
220ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner      Data |= 4;
221ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    }
2229dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  };
2231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
224de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// InstantiationInfo - Each InstantiationInfo encodes the Instantiation
225de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// location - where the token was ultimately instantiated, and the
226de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// SpellingLoc - where the actual character data for the token came from.
227de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  class InstantiationInfo {
228e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner     // Really these are all SourceLocations.
2291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
230e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    /// SpellingLoc - Where the spelling for the token can be found.
231e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    unsigned SpellingLoc;
2321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
233e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    /// InstantiationLocStart/InstantiationLocEnd - In a macro expansion, these
23414f79002e58556798e86168c63e48d533287eda5Douglas Gregor    /// indicate the start and end of the instantiation.  In object-like macros,
235e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    /// these will be the same.  In a function-like macro instantiation, the
236e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    /// start will be the identifier and the end will be the ')'.
237e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    unsigned InstantiationLocStart, InstantiationLocEnd;
2389dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  public:
239de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    SourceLocation getSpellingLoc() const {
240de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return SourceLocation::getFromRawEncoding(SpellingLoc);
241de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
242e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    SourceLocation getInstantiationLocStart() const {
243e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner      return SourceLocation::getFromRawEncoding(InstantiationLocStart);
244e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    }
245e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    SourceLocation getInstantiationLocEnd() const {
246e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner      return SourceLocation::getFromRawEncoding(InstantiationLocEnd);
247e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    }
2481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
249e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    std::pair<SourceLocation,SourceLocation> getInstantiationLocRange() const {
250e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner      return std::make_pair(getInstantiationLocStart(),
251e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner                            getInstantiationLocEnd());
252e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    }
2531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
254e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    /// get - Return a InstantiationInfo for an expansion.  IL specifies
255df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner    /// the instantiation location (where the macro is expanded), and SL
256df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner    /// specifies the spelling location (where the characters from the token
257e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    /// come from).  IL and PL can both refer to normal File SLocs or
258e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    /// instantiation locations.
259e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    static InstantiationInfo get(SourceLocation ILStart, SourceLocation ILEnd,
260e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner                                 SourceLocation SL) {
261de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      InstantiationInfo X;
262de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      X.SpellingLoc = SL.getRawEncoding();
263e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner      X.InstantiationLocStart = ILStart.getRawEncoding();
264e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner      X.InstantiationLocEnd = ILEnd.getRawEncoding();
2659dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner      return X;
2665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
267de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  };
2681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
269de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// SLocEntry - This is a discriminated union of FileInfo and
270de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// InstantiationInfo.  SourceManager keeps an array of these objects, and
271de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// they are uniquely identified by the FileID datatype.
272de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  class SLocEntry {
273de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned Offset;   // low bit is set for instantiation info.
274de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    union {
275de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      FileInfo File;
276de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      InstantiationInfo Instantiation;
277de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    };
278de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  public:
279de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned getOffset() const { return Offset >> 1; }
2801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
281de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    bool isInstantiation() const { return Offset & 1; }
282de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    bool isFile() const { return !isInstantiation(); }
2831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
284de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    const FileInfo &getFile() const {
285de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      assert(isFile() && "Not a file SLocEntry!");
286de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return File;
287de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
288de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
289de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    const InstantiationInfo &getInstantiation() const {
290de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      assert(isInstantiation() && "Not an instantiation SLocEntry!");
291de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return Instantiation;
292de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
2931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
294de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    static SLocEntry get(unsigned Offset, const FileInfo &FI) {
295de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      SLocEntry E;
296de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      E.Offset = Offset << 1;
297de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      E.File = FI;
298de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return E;
299de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
300de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
301de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    static SLocEntry get(unsigned Offset, const InstantiationInfo &II) {
302de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      SLocEntry E;
303de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      E.Offset = (Offset << 1) | 1;
304de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      E.Instantiation = II;
305de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return E;
306de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
3075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  };
3085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}  // end SrcMgr namespace.
3097f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
3107f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor/// \brief External source of source location entries.
3117f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregorclass ExternalSLocEntrySource {
3127f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregorpublic:
3137f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  virtual ~ExternalSLocEntrySource();
3147f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
3157f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  /// \brief Read the source location entry with index ID.
3167f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  virtual void ReadSLocEntry(unsigned ID) = 0;
3177f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor};
318dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner
319dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner
320dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner/// IsBeforeInTranslationUnitCache - This class holds the cache used by
321dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner/// isBeforeInTranslationUnit.  The cache structure is complex enough to be
322dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner/// worth breaking out of SourceManager.
323dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattnerclass IsBeforeInTranslationUnitCache {
324dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// L/R QueryFID - These are the FID's of the cached query.  If these match up
325dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// with a subsequent query, the result can be reused.
326dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  FileID LQueryFID, RQueryFID;
327dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner
328dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// CommonFID - This is the file found in common between the two #include
329dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// traces.  It is the nearest common ancestor of the #include tree.
330dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  FileID CommonFID;
331dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner
332dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// L/R CommonOffset - This is the offset of the previous query in CommonFID.
333dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// Usually, this represents the location of the #include for QueryFID, but if
334dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// LQueryFID is a parent of RQueryFID (or vise versa) then these can be a
335dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// random token in the parent.
336dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  unsigned LCommonOffset, RCommonOffset;
337dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattnerpublic:
338dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner
339dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// isCacheValid - Return true if the currently cached values match up with
340dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// the specified LHS/RHS query.  If not, we can't use the cache.
341dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  bool isCacheValid(FileID LHS, FileID RHS) const {
342dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    return LQueryFID == LHS && RQueryFID == RHS;
343dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  }
344dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner
345dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// getCachedResult - If the cache is valid, compute the result given the
346dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// specified offsets in the LHS/RHS FID's.
347dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  bool getCachedResult(unsigned LOffset, unsigned ROffset) const {
348dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    // If one of the query files is the common file, use the offset.  Otherwise,
349dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    // use the #include loc in the common file.
350dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    if (LQueryFID != CommonFID) LOffset = LCommonOffset;
351dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    if (RQueryFID != CommonFID) ROffset = RCommonOffset;
352dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    return LOffset < ROffset;
353dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  }
354dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner
355dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  // Set up a new query.
356dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  void setQueryFIDs(FileID LHS, FileID RHS) {
357dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    LQueryFID = LHS;
358dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    RQueryFID = RHS;
359dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  }
360dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner
361dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  void setCommonLoc(FileID commonFID, unsigned lCommonOffset,
362dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner                    unsigned rCommonOffset) {
363dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    CommonFID = commonFID;
364dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    LCommonOffset = lCommonOffset;
365dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    RCommonOffset = rCommonOffset;
366dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  }
367dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner
368dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner};
3697f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
3705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SourceManager - This file handles loading and caching of source files into
3715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// memory.  This object owns the MemoryBuffer objects for all of the loaded
3725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// files and assigns unique FileID's for each unique #include chain.
3735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
3745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// The SourceManager can be queried for information about SourceLocation
375f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner/// objects, turning them into either spelling or instantiation locations.
376f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner/// Spelling locations represent where the bytes corresponding to a token came
377f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner/// from and instantiation locations represent where the location is in the
378f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner/// user's view.  In the case of a macro expansion, for example, the spelling
379f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner/// location indicates where the expanded token came from and the instantiation
380f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner/// location specifies where it was expanded.
3815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass SourceManager {
382f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor  /// \brief Diagnostic object.
383f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor  Diagnostic &Diag;
384389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
385389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis  FileManager &FileMgr;
386389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
3870d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  mutable llvm::BumpPtrAllocator ContentCacheAlloc;
3881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// FileInfos - Memoized information about all of the files tracked by this
3900d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek  /// SourceManager.  This set allows us to merge ContentCache entries based
3910d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek  /// on their FileEntry*.  All ContentCache objects will thus have unique,
3921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// non-null, FileEntry pointers.
3930d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos;
3941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
395299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  /// \brief True if the ContentCache for files that are overriden by other
396299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  /// files, should report the original file name. Defaults to true.
397299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  bool OverridenFilesKeepOriginalName;
398299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis
399b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  /// \brief Files that have been overriden with the contents from another file.
400b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  llvm::DenseMap<const FileEntry *, const FileEntry *> OverriddenFiles;
401b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis
4025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// MemBufferInfos - Information about various memory buffers that we have
4030d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  /// read in.  All FileEntry* within the stored ContentCache objects are NULL,
4040d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  /// as they do not refer to a file.
4050d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  std::vector<SrcMgr::ContentCache*> MemBufferInfos;
4061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
407de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// SLocEntryTable - This is an array of SLocEntry's that we have created.
408de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// FileID is an index into this vector.  This array is sorted by the offset.
409de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::vector<SrcMgr::SLocEntry> SLocEntryTable;
410de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// NextOffset - This is the next available offset that a new SLocEntry can
411de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// start at.  It is SLocEntryTable.back().getOffset()+size of back() entry.
412de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  unsigned NextOffset;
4137f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
4147f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  /// \brief If source location entries are being lazily loaded from
4157f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  /// an external source, this vector indicates whether the Ith source
4167f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  /// location entry has already been loaded from the external storage.
4177f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  std::vector<bool> SLocEntryLoaded;
4187f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
4197f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  /// \brief An external source for source location entries.
4207f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  ExternalSLocEntrySource *ExternalSLocEntries;
4217f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
422de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// LastFileIDLookup - This is a one-entry cache to speed up getFileID.
423de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// LastFileIDLookup records the last FileID looked up or created, because it
424de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// is very common to look up many tokens from the same file.
425de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  mutable FileID LastFileIDLookup;
4261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4275b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  /// LineTable - This holds information for #line directives.  It is referenced
4285b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  /// by indices from SLocEntryTable.
4295b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  LineTableInfo *LineTable;
4301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4315e36a7a89da5d69ece23fc8228624a053f75c645Chris Lattner  /// LastLineNo - These ivars serve as a cache used in the getLineNumber
4325e36a7a89da5d69ece23fc8228624a053f75c645Chris Lattner  /// method which is used to speedup getLineNumber calls to nearby locations.
4332b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  mutable FileID LastLineNoFileIDQuery;
434f812a45dd93634c9300ed5533bd26b56374714a1Chris Lattner  mutable SrcMgr::ContentCache *LastLineNoContentCache;
435f812a45dd93634c9300ed5533bd26b56374714a1Chris Lattner  mutable unsigned LastLineNoFilePos;
436f812a45dd93634c9300ed5533bd26b56374714a1Chris Lattner  mutable unsigned LastLineNoResult;
4371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
43876edd0e4ae0592a7225d50d0bad6732ac64dca2aTed Kremenek  /// MainFileID - The file ID for the main source file of the translation unit.
4392b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID MainFileID;
44049c1f4aa2a6c360d25d605004ec3c4affd62db77Steve Naroff
441de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  // Statistics for -print-stats.
442de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  mutable unsigned NumLinearScans, NumBinaryProbes;
4431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4442aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis  // Cache results for the isBeforeInTranslationUnit method.
445dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  mutable IsBeforeInTranslationUnitCache IsBeforeInTUCache;
4461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
44749c1f4aa2a6c360d25d605004ec3c4affd62db77Steve Naroff  // SourceManager doesn't support copy construction.
44849c1f4aa2a6c360d25d605004ec3c4affd62db77Steve Naroff  explicit SourceManager(const SourceManager&);
4491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  void operator=(const SourceManager&);
4505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
45139b49bcaaddb1049234fca9500c0ac02c088e23dChris Lattner  SourceManager(Diagnostic &Diag, FileManager &FileMgr);
4525b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  ~SourceManager();
4531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4545b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  void clearIDTables();
4551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
45678a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios Kyrtzidis  Diagnostic &getDiagnostics() const { return Diag; }
45778a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios Kyrtzidis
458389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis  FileManager &getFileManager() const { return FileMgr; }
459389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
460299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  /// \brief Set true if the SourceManager should report the original file name
461299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  /// for contents of files that were overriden by other files.Defaults to true.
462299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  void setOverridenFilesKeepOriginalName(bool value) {
463299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis    OverridenFilesKeepOriginalName = value;
464299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  }
465299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis
46606a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
46706a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  // MainFileID creation and querying methods.
46806a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
46906a062dc784c609b75dca15fd97f468d0d846596Chris Lattner
47076edd0e4ae0592a7225d50d0bad6732ac64dca2aTed Kremenek  /// getMainFileID - Returns the FileID of the main source file.
4712b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID getMainFileID() const { return MainFileID; }
4721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
47306a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  /// createMainFileID - Create the FileID for the main source file.
474f155dfa4e284be7dca8f1abf002476b2aaee8546Dan Gohman  FileID createMainFileID(const FileEntry *SourceFile) {
47506a062dc784c609b75dca15fd97f468d0d846596Chris Lattner    assert(MainFileID.isInvalid() && "MainFileID already set!");
476f155dfa4e284be7dca8f1abf002476b2aaee8546Dan Gohman    MainFileID = createFileID(SourceFile, SourceLocation(), SrcMgr::C_User);
47706a062dc784c609b75dca15fd97f468d0d846596Chris Lattner    return MainFileID;
47806a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  }
4791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
480414cb64f09ce48a36377458ce5e5a90c3ad41d00Douglas Gregor  /// \brief Set the file ID for the precompiled preamble, which is also the
481414cb64f09ce48a36377458ce5e5a90c3ad41d00Douglas Gregor  /// main file.
482414cb64f09ce48a36377458ce5e5a90c3ad41d00Douglas Gregor  void SetPreambleFileID(FileID Preamble) {
483414cb64f09ce48a36377458ce5e5a90c3ad41d00Douglas Gregor    assert(MainFileID.isInvalid() && "MainFileID already set!");
484414cb64f09ce48a36377458ce5e5a90c3ad41d00Douglas Gregor    MainFileID = Preamble;
485414cb64f09ce48a36377458ce5e5a90c3ad41d00Douglas Gregor  }
486414cb64f09ce48a36377458ce5e5a90c3ad41d00Douglas Gregor
48706a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
488de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  // Methods to create new FileID's and instantiations.
48906a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
4901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// createFileID - Create a new FileID that represents the specified file
4925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// being #included from the specified IncludePosition.  This returns 0 on
4935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// error and translates NULL into standard input.
494190faf7c30890479925193b074571e5dc30c3f53Sebastian Redl  /// PreallocateID should be non-zero to specify which pre-allocated,
4957f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  /// lazily computed source location is being filled in by this operation.
4962b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
4977f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                      SrcMgr::CharacteristicKind FileCharacter,
4987f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                      unsigned PreallocatedID = 0,
4997f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                      unsigned Offset = 0) {
500de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile);
5010d06e998910934e5ef070f53f4c272e7c6b846c6Dan Gohman    assert(IR && "getOrCreateContentCache() cannot return NULL");
5027f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    return createFileID(IR, IncludePos, FileCharacter, PreallocatedID, Offset);
5035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
5041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// createFileIDForMemBuffer - Create a new FileID that represents the
5065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// specified memory buffer.  This does no caching of the buffer and takes
5075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once.
5087f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer,
5097f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                                  unsigned PreallocatedID = 0,
5107f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                                  unsigned Offset = 0) {
5117bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber    return createFileID(createMemBufferContentCache(Buffer), SourceLocation(),
5127f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                        SrcMgr::C_User, PreallocatedID, Offset);
5135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
5141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5151036b68525f39cb69ac22c679ed440acd8392a16Ted Kremenek  /// createMainFileIDForMembuffer - Create the FileID for a memory buffer
5161036b68525f39cb69ac22c679ed440acd8392a16Ted Kremenek  ///  that will represent the FileID for the main source.  One example
5171036b68525f39cb69ac22c679ed440acd8392a16Ted Kremenek  ///  of when this would be used is when the main source is read from STDIN.
5182b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) {
5192b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner    assert(MainFileID.isInvalid() && "MainFileID already set!");
5207697b5c5a918729718da9ecd9e374bbd973a69e9Chris Lattner    MainFileID = createFileIDForMemBuffer(Buffer);
5211036b68525f39cb69ac22c679ed440acd8392a16Ted Kremenek    return MainFileID;
5221036b68525f39cb69ac22c679ed440acd8392a16Ted Kremenek  }
52306a062dc784c609b75dca15fd97f468d0d846596Chris Lattner
524de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// createInstantiationLoc - Return a new SourceLocation that encodes the fact
525de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// that a token at Loc should actually be referenced from InstantiationLoc.
526de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// TokLength is the length of the token being instantiated.
527de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  SourceLocation createInstantiationLoc(SourceLocation Loc,
528e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner                                        SourceLocation InstantiationLocStart,
529e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner                                        SourceLocation InstantiationLocEnd,
5307f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                                        unsigned TokLength,
5317f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                                        unsigned PreallocatedID = 0,
5327f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                                        unsigned Offset = 0);
5331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5342968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \brief Retrieve the memory buffer associated with the given file.
53550f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  ///
53650f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  /// \param Invalid If non-NULL, will be set \c true if an error
53750f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  /// occurs while retrieving the memory buffer.
53850f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File,
53950f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor                                                   bool *Invalid = 0);
5402968442603b029949246467253eeac8139a5b6d8Douglas Gregor
5412968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \brief Override the contents of the given source file by providing an
5422968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// already-allocated buffer.
5432968442603b029949246467253eeac8139a5b6d8Douglas Gregor  ///
544afbf5f810652f04c0bba235c0fa079885fb3caa8Dan Gohman  /// \param SourceFile the source file whose contents will be overriden.
5452968442603b029949246467253eeac8139a5b6d8Douglas Gregor  ///
5462968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \param Buffer the memory buffer whose contents will be used as the
5472968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// data in the given source file.
5482968442603b029949246467253eeac8139a5b6d8Douglas Gregor  ///
549f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// \param DoNotFree If true, then the buffer will not be freed when the
550f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// source manager is destroyed.
5510d06e998910934e5ef070f53f4c272e7c6b846c6Dan Gohman  void overrideFileContents(const FileEntry *SourceFile,
552f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor                            const llvm::MemoryBuffer *Buffer,
553f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor                            bool DoNotFree = false);
5542968442603b029949246467253eeac8139a5b6d8Douglas Gregor
555b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  /// \brief Override the the given source file with another one.
556b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  ///
557b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  /// \param SourceFile the source file which will be overriden.
558b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  ///
559b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  /// \param NewFile the file whose contents will be used as the
560b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  /// data instead of the contents of the given source file.
561b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  void overrideFileContents(const FileEntry *SourceFile,
562b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis                            const FileEntry *NewFile);
563b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis
56406a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
56506a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  // FileID manipulation methods.
56606a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
5671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5682ffb14f004affc363d86d6c7c63c356190db3679Daniel Dunbar  /// getBuffer - Return the buffer for the specified FileID. If there is an
5692ffb14f004affc363d86d6c7c63c356190db3679Daniel Dunbar  /// error opening this buffer the first time, this manufactures a temporary
5702ffb14f004affc363d86d6c7c63c356190db3679Daniel Dunbar  /// buffer and returns a non-empty error string.
571e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner  const llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc,
572e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner                                      bool *Invalid = 0) const {
573e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner    return getSLocEntry(FID).getFile().getContentCache()
574e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner       ->getBuffer(Diag, *this, Loc, Invalid);
57506a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  }
5761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
577e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner  const llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = 0) const {
578e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner    return getSLocEntry(FID).getFile().getContentCache()
579e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner       ->getBuffer(Diag, *this, SourceLocation(), Invalid);
580e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner  }
581e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner
58206a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  /// getFileEntryForID - Returns the FileEntry record for the provided FileID.
58306a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  const FileEntry *getFileEntryForID(FileID FID) const {
584b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    return getSLocEntry(FID).getFile().getContentCache()->OrigEntry;
58506a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  }
5861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
587ceafc4b63599d14f0b5b10ff92e22bf242682dceBenjamin Kramer  /// getBufferData - Return a StringRef to the source buffer data for the
588ceafc4b63599d14f0b5b10ff92e22bf242682dceBenjamin Kramer  /// specified FileID.
589ceafc4b63599d14f0b5b10ff92e22bf242682dceBenjamin Kramer  ///
590f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor  /// \param FID The file ID whose contents will be returned.
591f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor  /// \param Invalid If non-NULL, will be set true if an error occurred.
592f6ac97b101c8840efa92bf29166077ce4049e293Benjamin Kramer  llvm::StringRef getBufferData(FileID FID, bool *Invalid = 0) const;
593f6ac97b101c8840efa92bf29166077ce4049e293Benjamin Kramer
5941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
59506a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
59606a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  // SourceLocation manipulation methods.
59706a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
5981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
599668ab1a36d6a5731c71a75a5f388ecafd538a896Chris Lattner  /// getFileID - Return the FileID for a SourceLocation.  This is a very
600de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// hot method that is used for all SourceManager queries that start with a
601de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// SourceLocation object.  It is responsible for finding the entry in
602de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// SLocEntryTable which contains the specified location.
603de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  ///
604de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  FileID getFileID(SourceLocation SpellingLoc) const {
605de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned SLocOffset = SpellingLoc.getOffset();
6061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
607de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    // If our one-entry cache covers this offset, just return it.
608de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
609de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return LastFileIDLookup;
610de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
611de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return getFileIDSlow(SLocOffset);
612de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
6131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6142b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  /// getLocForStartOfFile - Return the source location corresponding to the
6152b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  /// first byte of the specified file.
6162b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  SourceLocation getLocForStartOfFile(FileID FID) const {
6177f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    assert(FID.ID < SLocEntryTable.size() && "FileID out of range");
6187f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    assert(getSLocEntry(FID).isFile() && "FileID is not a file");
6197f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    unsigned FileOffset = getSLocEntry(FID).getOffset();
620de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return SourceLocation::getFileLoc(FileOffset);
6212b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  }
6221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6236678133b8ce642f93e5141f056fa643112041ad0Chris Lattner  /// getInstantiationLoc - Given a SourceLocation object, return the
6246678133b8ce642f93e5141f056fa643112041ad0Chris Lattner  /// instantiation location referenced by the ID.
625de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  SourceLocation getInstantiationLoc(SourceLocation Loc) const {
626addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner    // Handle the non-mapped case inline, defer to out of line code to handle
627addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner    // instantiations.
628de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (Loc.isFileID()) return Loc;
629addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner    return getInstantiationLocSlowCase(Loc);
630de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
6311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
632e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  /// getImmediateInstantiationRange - Loc is required to be an instantiation
633e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  /// location.  Return the start/end of the instantiation information.
634e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  std::pair<SourceLocation,SourceLocation>
635e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  getImmediateInstantiationRange(SourceLocation Loc) const;
6361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6376678133b8ce642f93e5141f056fa643112041ad0Chris Lattner  /// getInstantiationRange - Given a SourceLocation object, return the
6386678133b8ce642f93e5141f056fa643112041ad0Chris Lattner  /// range of tokens covered by the instantiation in the ultimate file.
6396678133b8ce642f93e5141f056fa643112041ad0Chris Lattner  std::pair<SourceLocation,SourceLocation>
6406678133b8ce642f93e5141f056fa643112041ad0Chris Lattner  getInstantiationRange(SourceLocation Loc) const;
6411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
643de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// getSpellingLoc - Given a SourceLocation object, return the spelling
644de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// location referenced by the ID.  This is the place where the characters
645de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// that make up the lexed token can be found.
646de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  SourceLocation getSpellingLoc(SourceLocation Loc) const {
647addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner    // Handle the non-mapped case inline, defer to out of line code to handle
648addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner    // instantiations.
649de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (Loc.isFileID()) return Loc;
650addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner    return getSpellingLocSlowCase(Loc);
651de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
6521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
653387616edf98739f4a0dd234c907e2b913e6a535dChris Lattner  /// getImmediateSpellingLoc - Given a SourceLocation object, return the
654387616edf98739f4a0dd234c907e2b913e6a535dChris Lattner  /// spelling location referenced by the ID.  This is the first level down
655387616edf98739f4a0dd234c907e2b913e6a535dChris Lattner  /// towards the place where the characters that make up the lexed token can be
656387616edf98739f4a0dd234c907e2b913e6a535dChris Lattner  /// found.  This should not generally be used by clients.
6571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const;
658de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
659de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// getDecomposedLoc - Decompose the specified location into a raw FileID +
660de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// Offset pair.  The first element is the FileID, the second is the
661de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// offset from the start of the buffer of the location.
662de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const {
663de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    FileID FID = getFileID(Loc);
664de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return std::make_pair(FID, Loc.getOffset()-getSLocEntry(FID).getOffset());
665de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
6661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
667de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// getDecomposedInstantiationLoc - Decompose the specified location into a
668de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// raw FileID + Offset pair.  If the location is an instantiation record,
669de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// walk through it until we find the final location instantiated.
670de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned>
671de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  getDecomposedInstantiationLoc(SourceLocation Loc) const {
672de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    FileID FID = getFileID(Loc);
673de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
6741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
675de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned Offset = Loc.getOffset()-E->getOffset();
676de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (Loc.isFileID())
677de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return std::make_pair(FID, Offset);
6781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
679de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return getDecomposedInstantiationLocSlowCase(E, Offset);
680de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
681de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
682de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// getDecomposedSpellingLoc - Decompose the specified location into a raw
683de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// FileID + Offset pair.  If the location is an instantiation record, walk
684de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// through it until we find its spelling record.
685de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned>
686de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  getDecomposedSpellingLoc(SourceLocation Loc) const {
687de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    FileID FID = getFileID(Loc);
688de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
6891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
690de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned Offset = Loc.getOffset()-E->getOffset();
691de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (Loc.isFileID())
692de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return std::make_pair(FID, Offset);
693de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return getDecomposedSpellingLocSlowCase(E, Offset);
6941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
6951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
69652c29081281955d3db9e11d10573b2d38f709099Chris Lattner  /// getFileOffset - This method returns the offset from the start
69752c29081281955d3db9e11d10573b2d38f709099Chris Lattner  /// of the file that the specified SourceLocation represents. This is not very
69852c29081281955d3db9e11d10573b2d38f709099Chris Lattner  /// meaningful for a macro ID.
69952c29081281955d3db9e11d10573b2d38f709099Chris Lattner  unsigned getFileOffset(SourceLocation SpellingLoc) const {
700de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return getDecomposedLoc(SpellingLoc).second;
7015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
7021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
704de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  //===--------------------------------------------------------------------===//
705de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  // Queries about the code at a SourceLocation.
706de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  //===--------------------------------------------------------------------===//
7071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getCharacterData - Return a pointer to the start of the specified location
709de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// in the appropriate spelling MemoryBuffer.
71050f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  ///
71150f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  /// \param Invalid If non-NULL, will be set \c true if an error occurs.
71250f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  const char *getCharacterData(SourceLocation SL, bool *Invalid = 0) const;
7131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7149dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  /// getColumnNumber - Return the column # for the specified file position.
7159dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  /// This is significantly cheaper to compute than the line number.  This
7169dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  /// returns zero if the column number isn't known.  This may only be called on
717f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner  /// a file sloc, so you must choose a spelling or instantiation location
718f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner  /// before calling this method.
71950f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  unsigned getColumnNumber(FileID FID, unsigned FilePos,
72050f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor                           bool *Invalid = 0) const;
7215ef04ee40c3332d31b6d1439f50d0ddb45812929Chandler Carruth  unsigned getSpellingColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
72250f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  unsigned getInstantiationColumnNumber(SourceLocation Loc,
72350f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor                                        bool *Invalid = 0) const;
7245ef04ee40c3332d31b6d1439f50d0ddb45812929Chandler Carruth  unsigned getPresumedColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
7251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
727df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner  /// getLineNumber - Given a SourceLocation, return the spelling line number
7285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// for the position indicated.  This requires building and caching a table of
7295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// line offsets for the MemoryBuffer, so this is not cheap: use only when
7305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// about to emit a diagnostic.
73150f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = 0) const;
7325ef04ee40c3332d31b6d1439f50d0ddb45812929Chandler Carruth  unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
73350f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  unsigned getInstantiationLineNumber(SourceLocation Loc,
73450f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor                                      bool *Invalid = 0) const;
7355ef04ee40c3332d31b6d1439f50d0ddb45812929Chandler Carruth  unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
7361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
737bff5c512af8ca7ac92e974e04c06ff4f820e4ee1Chris Lattner  /// Return the filename or buffer identifier of the buffer the location is in.
738bff5c512af8ca7ac92e974e04c06ff4f820e4ee1Chris Lattner  /// Note that this name does not respect #line directives.  Use getPresumedLoc
739bff5c512af8ca7ac92e974e04c06ff4f820e4ee1Chris Lattner  /// for normal clients.
74050f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  const char *getBufferName(SourceLocation Loc, bool *Invalid = 0) const;
7411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7426b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// getFileCharacteristic - return the file characteristic of the specified
7431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// source location, indicating whether this is a normal file, a system
7446b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// header, or an "implicit extern C" system header.
7456b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  ///
7466b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// This state can be modified with flags on GNU linemarker directives like:
7476b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  ///   # 4 "foo.h" 3
7486b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// which changes all source locations in the current file after that to be
7496b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// considered to be from a system header.
7506b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const;
7511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
752b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// getPresumedLoc - This method returns the "presumed" location of a
753b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// SourceLocation specifies.  A "presumed location" can be modified by #line
754b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// or GNU line marker directives.  This provides a view on the data that a
755b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// user should see in diagnostics, for example.
756b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  ///
757b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// Note that a presumed location is always given as the instantiation point
758b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// of an instantiation location, not at the spelling location.
759cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  ///
760cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  /// \returns The presumed location of the specified SourceLocation. If the
761cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  /// presumed location cannot be calculate (e.g., because \p Loc is invalid
762cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  /// or the file containing \p Loc has changed on disk), returns an invalid
763cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  /// presumed location.
764b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  PresumedLoc getPresumedLoc(SourceLocation Loc) const;
7651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7669fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  /// isFromSameFile - Returns true if both SourceLocations correspond to
7679fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  ///  the same file.
7689fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
769a11d61793341fea195c29a0dab3fbd74f2b39a8cChris Lattner    return getFileID(Loc1) == getFileID(Loc2);
7709fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  }
7711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7729fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  /// isFromMainFile - Returns true if the file of provided SourceLocation is
7739fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  ///   the main file.
7749fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  bool isFromMainFile(SourceLocation Loc) const {
775a11d61793341fea195c29a0dab3fbd74f2b39a8cChris Lattner    return getFileID(Loc) == getMainFileID();
7761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
7771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7787bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber  /// isInSystemHeader - Returns if a SourceLocation is in a system header.
7797bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber  bool isInSystemHeader(SourceLocation Loc) const {
7800b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner    return getFileCharacteristic(Loc) != SrcMgr::C_User;
781721818304ac462d8c6ce05eecd02884033db78f1Chris Lattner  }
7821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7830d456588acac0713a7c33063922d35a8cc8c658eChris Lattner  /// isInExternCSystemHeader - Returns if a SourceLocation is in an "extern C"
7840d456588acac0713a7c33063922d35a8cc8c658eChris Lattner  /// system header.
7850d456588acac0713a7c33063922d35a8cc8c658eChris Lattner  bool isInExternCSystemHeader(SourceLocation Loc) const {
7860d456588acac0713a7c33063922d35a8cc8c658eChris Lattner    return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem;
7870d456588acac0713a7c33063922d35a8cc8c658eChris Lattner  }
7881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
78906a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
7905b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  // Line Table Manipulation Routines
7915b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  //===--------------------------------------------------------------------===//
7921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7935b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  /// getLineTableFilenameID - Return the uniqued ID for the specified filename.
7941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ///
7955b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  unsigned getLineTableFilenameID(const char *Ptr, unsigned Len);
7961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7974c4ea17d7f991516c37a871dfa4bbe5723fa85f0Chris Lattner  /// AddLineNote - Add a line note to the line table for the FileID and offset
7984c4ea17d7f991516c37a871dfa4bbe5723fa85f0Chris Lattner  /// specified by Loc.  If FilenameID is -1, it is considered to be
7994c4ea17d7f991516c37a871dfa4bbe5723fa85f0Chris Lattner  /// unspecified.
8004c4ea17d7f991516c37a871dfa4bbe5723fa85f0Chris Lattner  void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID);
8019d79ebac47ffde6a1cb312f4c09b66b1b9a397fbChris Lattner  void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID,
8021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                   bool IsFileEntry, bool IsFileExit,
8039d79ebac47ffde6a1cb312f4c09b66b1b9a397fbChris Lattner                   bool IsSystemHeader, bool IsExternCHeader);
804bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
805bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  /// \brief Determine if the source manager has a line table.
806bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  bool hasLineTable() const { return LineTable != 0; }
807bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
808bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  /// \brief Retrieve the stored line table.
809bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  LineTableInfo &getLineTable();
810bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
8115b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  //===--------------------------------------------------------------------===//
81206a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  // Other miscellaneous methods.
81306a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
81410b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis
81510b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  /// \brief Get the source location for the given file:line:col triplet.
81610b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  ///
81710b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  /// If the source file is included multiple times, the source location will
81810b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  /// be based upon the first inclusion.
81910b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  SourceLocation getLocation(const FileEntry *SourceFile,
82086a4d0dd6a630639aab7715323ed068940e650afDouglas Gregor                             unsigned Line, unsigned Col);
8211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8222aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis  /// \brief Determines the order of 2 source locations in the translation unit.
8232aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis  ///
8242aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis  /// \returns true if LHS source location comes before RHS, false otherwise.
8252aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis  bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const;
8262aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis
827c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner  // Iterators over FileInfos.
8280d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>
8290d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner      ::const_iterator fileinfo_iterator;
830c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner  fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); }
831c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner  fileinfo_iterator fileinfo_end() const { return FileInfos.end(); }
832d93256e55673a17d18543397ec462416acb13792Douglas Gregor  bool hasFileInfo(const FileEntry *File) const {
833d93256e55673a17d18543397ec462416acb13792Douglas Gregor    return FileInfos.find(File) != FileInfos.end();
834d93256e55673a17d18543397ec462416acb13792Douglas Gregor  }
835c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner
8365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// PrintStats - Print statistics to stderr.
8375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
8385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void PrintStats() const;
83978d85f53b093867bbb0123f016956178eea7343eTed Kremenek
84014f79002e58556798e86168c63e48d533287eda5Douglas Gregor  unsigned sloc_entry_size() const { return SLocEntryTable.size(); }
8412d684c2499bd80e28451c1db0e2e040ef00c53bcKovarththanan Rajaratnam
84216b55a71695a33c094383295cc7b7a2080e098daTed Kremenek  // FIXME: Exposing this is a little gross; what we want is a good way
8433c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  //  to iterate the entries that were not defined in an AST file (or
84416b55a71695a33c094383295cc7b7a2080e098daTed Kremenek  //  any other external source).
84516b55a71695a33c094383295cc7b7a2080e098daTed Kremenek  unsigned sloc_loaded_entry_size() const { return SLocEntryLoaded.size(); }
84614f79002e58556798e86168c63e48d533287eda5Douglas Gregor
847bdfe48ac80573e026595af91e541474dbf02565fDouglas Gregor  const SrcMgr::SLocEntry &getSLocEntry(unsigned ID) const {
848bdfe48ac80573e026595af91e541474dbf02565fDouglas Gregor    assert(ID < SLocEntryTable.size() && "Invalid id");
8491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (ExternalSLocEntries &&
850bdfe48ac80573e026595af91e541474dbf02565fDouglas Gregor        ID < SLocEntryLoaded.size() &&
851bdfe48ac80573e026595af91e541474dbf02565fDouglas Gregor        !SLocEntryLoaded[ID])
852bdfe48ac80573e026595af91e541474dbf02565fDouglas Gregor      ExternalSLocEntries->ReadSLocEntry(ID);
853bdfe48ac80573e026595af91e541474dbf02565fDouglas Gregor    return SLocEntryTable[ID];
854bdfe48ac80573e026595af91e541474dbf02565fDouglas Gregor  }
8552d684c2499bd80e28451c1db0e2e040ef00c53bcKovarththanan Rajaratnam
8562d684c2499bd80e28451c1db0e2e040ef00c53bcKovarththanan Rajaratnam  const SrcMgr::SLocEntry &getSLocEntry(FileID FID) const {
857bdfe48ac80573e026595af91e541474dbf02565fDouglas Gregor    return getSLocEntry(FID.ID);
858bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  }
859bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
860f60e9918690fcf02974bc1ebecd42c99d561855eDouglas Gregor  unsigned getNextOffset() const { return NextOffset; }
861f60e9918690fcf02974bc1ebecd42c99d561855eDouglas Gregor
8627f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  /// \brief Preallocate some number of source location entries, which
8637f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  /// will be loaded as needed from the given external source.
8647f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  void PreallocateSLocEntries(ExternalSLocEntrySource *Source,
8657f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                              unsigned NumSLocEntries,
8667f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                              unsigned NextOffset);
8677f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
8682bf1eb09f06a9792fa94dff0703f2aa2c4bace2aDouglas Gregor  /// \brief Clear out any preallocated source location entries that
8692bf1eb09f06a9792fa94dff0703f2aa2c4bace2aDouglas Gregor  /// haven't already been loaded.
8702bf1eb09f06a9792fa94dff0703f2aa2c4bace2aDouglas Gregor  void ClearPreallocatedSLocEntries();
8712bf1eb09f06a9792fa94dff0703f2aa2c4bace2aDouglas Gregor
8725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerprivate:
873de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// isOffsetInFileID - Return true if the specified FileID contains the
874de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// specified SourceLocation offset.  This is a very hot method.
875de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const {
876de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID);
877de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    // If the entry is after the offset, it can't contain it.
878de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (SLocOffset < Entry.getOffset()) return false;
8791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
880de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    // If this is the last entry than it does.  Otherwise, the entry after it
881de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    // has to not include it.
882de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (FID.ID+1 == SLocEntryTable.size()) return true;
8837f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
8847f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    return SLocOffset < getSLocEntry(FileID::get(FID.ID+1)).getOffset();
885de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
8861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
88778d85f53b093867bbb0123f016956178eea7343eTed Kremenek  /// createFileID - Create a new fileID for the specified ContentCache and
88878d85f53b093867bbb0123f016956178eea7343eTed Kremenek  ///  include position.  This works regardless of whether the ContentCache
88978d85f53b093867bbb0123f016956178eea7343eTed Kremenek  ///  corresponds to a file or some other input source.
8902b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID createFileID(const SrcMgr::ContentCache* File,
8912b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner                      SourceLocation IncludePos,
8927f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                      SrcMgr::CharacteristicKind DirCharacter,
8937f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                      unsigned PreallocatedID = 0,
8947f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                      unsigned Offset = 0);
8951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
896de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  const SrcMgr::ContentCache *
897de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    getOrCreateContentCache(const FileEntry *SourceFile);
898c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek
89978d85f53b093867bbb0123f016956178eea7343eTed Kremenek  /// createMemBufferContentCache - Create a new ContentCache for the specified
90078d85f53b093867bbb0123f016956178eea7343eTed Kremenek  ///  memory buffer.
9011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const SrcMgr::ContentCache*
9022b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  createMemBufferContentCache(const llvm::MemoryBuffer *Buf);
9031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
904de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  FileID getFileIDSlow(unsigned SLocOffset) const;
905de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
906addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner  SourceLocation getInstantiationLocSlowCase(SourceLocation Loc) const;
907addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner  SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;
908addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner
909de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned>
910de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E,
9111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        unsigned Offset) const;
912de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned>
913de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
914de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner                                   unsigned Offset) const;
9155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
9165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}  // end namespace clang
9195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif
921