SourceManager.h revision dcb1d68f6ffa183f3919aee6b554aec3793bf13e
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"
199f8eb2032030482b1d3de86e9bee725d93564302Chandler Carruth#include "llvm/System/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
5378d85f53b093867bbb0123f016956178eea7343eTed Kremenek  /// ContentCache - Once instance of this struct is kept for every file
5406a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  /// loaded or used.  This object owns the MemoryBuffer object.
55c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek  class ContentCache {
56c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    /// Buffer - The actual buffer containing the characters from the input
57c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    /// file.  This is owned by the ContentCache object.
58c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor    /// The bit indicates whether the buffer is invalid.
59c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor    mutable llvm::PointerIntPair<const llvm::MemoryBuffer *, 1, bool> Buffer;
60c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek
61c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek  public:
6278d85f53b093867bbb0123f016956178eea7343eTed Kremenek    /// Reference to the file entry.  This reference does not own
6378d85f53b093867bbb0123f016956178eea7343eTed Kremenek    /// the FileEntry object.  It is possible for this to be NULL if
6478d85f53b093867bbb0123f016956178eea7343eTed Kremenek    /// the ContentCache encapsulates an imaginary text buffer.
6505816591ec488a933dfecc9ff9f3cbf3c32767c2Chris Lattner    const FileEntry *Entry;
661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
670d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner    /// SourceLineCache - A bump pointer allocated array of offsets for each
680d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner    /// source line.  This is lazily computed.  This is owned by the
690d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner    /// SourceManager BumpPointerAllocator object.
7005816591ec488a933dfecc9ff9f3cbf3c32767c2Chris Lattner    unsigned *SourceLineCache;
711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
72b6427f821de8cce1566fb6e755143ea0918d5543Ted Kremenek    /// NumLines - The number of lines in this ContentCache.  This is only valid
73b6427f821de8cce1566fb6e755143ea0918d5543Ted Kremenek    /// if SourceLineCache is non-null.
745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    unsigned NumLines;
7510b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis
7636c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor    /// getBuffer - Returns the memory buffer for the associated content.
7736c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor    ///
7836c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor    /// \param Diag Object through which diagnostics will be emitted it the
7936c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor    /// buffer cannot be retrieved.
8036c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor    ///
81e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner    /// \param Loc If specified, is the location that invalid file diagnostics
82e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner    ///     will be emitted at.
83e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner    ///
8436c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor    /// \param Invalid If non-NULL, will be set \c true if an error occurred.
85e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner    const llvm::MemoryBuffer *getBuffer(Diagnostic &Diag,
86e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner                                        const SourceManager &SM,
87e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner                                        SourceLocation Loc = SourceLocation(),
8836c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor                                        bool *Invalid = 0) const;
891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
90c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    /// getSize - Returns the size of the content encapsulated by this
91c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    ///  ContentCache. This can be the size of the source file or the size of an
92c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    ///  arbitrary scratch buffer.  If the ContentCache encapsulates a source
93c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    ///  file this size is retrieved from the file's FileEntry.
94c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    unsigned getSize() const;
951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
96c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    /// getSizeBytesMapped - Returns the number of bytes actually mapped for
97c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    ///  this ContentCache.  This can be 0 if the MemBuffer was not actually
98c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    ///  instantiated.
99c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    unsigned getSizeBytesMapped() const;
1001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10105816591ec488a933dfecc9ff9f3cbf3c32767c2Chris Lattner    void setBuffer(const llvm::MemoryBuffer *B) {
102c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor      assert(!Buffer.getPointer() && "MemoryBuffer already set.");
103c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor      Buffer.setPointer(B);
104c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor      Buffer.setInt(false);
105c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    }
1061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1072968442603b029949246467253eeac8139a5b6d8Douglas Gregor    /// \brief Replace the existing buffer (which will be deleted)
1082968442603b029949246467253eeac8139a5b6d8Douglas Gregor    /// with the given buffer.
1092968442603b029949246467253eeac8139a5b6d8Douglas Gregor    void replaceBuffer(const llvm::MemoryBuffer *B);
1102968442603b029949246467253eeac8139a5b6d8Douglas Gregor
1110d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner    ContentCache(const FileEntry *Ent = 0)
112c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor      : Buffer(0, false), Entry(Ent), SourceLineCache(0), NumLines(0) {}
11378d85f53b093867bbb0123f016956178eea7343eTed Kremenek
11478d85f53b093867bbb0123f016956178eea7343eTed Kremenek    ~ContentCache();
1151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1160d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek    /// The copy ctor does not allow copies where source object has either
1170d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek    ///  a non-NULL Buffer or SourceLineCache.  Ownership of allocated memory
1180d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek    ///  is not transfered, so this is a logical error.
119c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor    ContentCache(const ContentCache &RHS)
120c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor      : Buffer(0, false), SourceLineCache(0)
121c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor    {
1220d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek      Entry = RHS.Entry;
1230d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek
124c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor      assert (RHS.Buffer.getPointer() == 0 && RHS.SourceLineCache == 0
1250d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek              && "Passed ContentCache object cannot own a buffer.");
1261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      NumLines = RHS.NumLines;
1280d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek    }
1291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1300d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek  private:
1310d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek    // Disable assignments.
1321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    ContentCache &operator=(const ContentCache& RHS);
1331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  };
1345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
135de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// FileInfo - Information about a FileID, basically just the logical file
136de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// that it represents and include stack information.
1375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
138de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// Each FileInfo has include stack information, indicating where it came
139de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// from.  This information encodes the #include chain that a token was
140de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// instantiated from.  The main include file has an invalid IncludeLoc.
1415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
142de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// FileInfos contain a "ContentCache *", with the contents of the file.
1435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
144de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  class FileInfo {
1455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    /// IncludeLoc - The location of the #include that brought in this file.
146de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    /// This is an invalid SLOC for the main file (top of the #include chain).
147de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned IncludeLoc;  // Really a SourceLocation
1481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1496e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    /// Data - This contains the ContentCache* and the bits indicating the
1506e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    /// characteristic of the file and whether it has #line info, all bitmangled
1516e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    /// together.
1526e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    uintptr_t Data;
15378d85f53b093867bbb0123f016956178eea7343eTed Kremenek  public:
154de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    /// get - Return a FileInfo object.
155de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    static FileInfo get(SourceLocation IL, const ContentCache *Con,
156de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner                        CharacteristicKind FileCharacter) {
157de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      FileInfo X;
158de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      X.IncludeLoc = IL.getRawEncoding();
1596e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner      X.Data = (uintptr_t)Con;
16000282d6e1194655a2e89f940bd6fa8484b52e666Chris Lattner      assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned");
1616e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner      assert((unsigned)FileCharacter < 4 && "invalid file character");
1626e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner      X.Data |= (unsigned)FileCharacter;
1635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return X;
1645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
166de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    SourceLocation getIncludeLoc() const {
167de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return SourceLocation::getFromRawEncoding(IncludeLoc);
168de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
1696e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    const ContentCache* getContentCache() const {
17000282d6e1194655a2e89f940bd6fa8484b52e666Chris Lattner      return reinterpret_cast<const ContentCache*>(Data & ~7UL);
1716e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    }
1721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1730b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner    /// getCharacteristic - Return whether this is a system header or not.
1741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    CharacteristicKind getFileCharacteristic() const {
1756e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner      return (CharacteristicKind)(Data & 3);
1760b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner    }
177ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner
178ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    /// hasLineDirectives - Return true if this FileID has #line directives in
179ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    /// it.
180ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    bool hasLineDirectives() const { return (Data & 4) != 0; }
1811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
182ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    /// setHasLineDirectives - Set the flag that indicates that this FileID has
183ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    /// line table entries associated with it.
184ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    void setHasLineDirectives() {
185ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner      Data |= 4;
186ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    }
1879dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  };
1881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
189de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// InstantiationInfo - Each InstantiationInfo encodes the Instantiation
190de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// location - where the token was ultimately instantiated, and the
191de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// SpellingLoc - where the actual character data for the token came from.
192de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  class InstantiationInfo {
193e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner     // Really these are all SourceLocations.
1941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
195e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    /// SpellingLoc - Where the spelling for the token can be found.
196e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    unsigned SpellingLoc;
1971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
198e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    /// InstantiationLocStart/InstantiationLocEnd - In a macro expansion, these
19914f79002e58556798e86168c63e48d533287eda5Douglas Gregor    /// indicate the start and end of the instantiation.  In object-like macros,
200e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    /// these will be the same.  In a function-like macro instantiation, the
201e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    /// start will be the identifier and the end will be the ')'.
202e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    unsigned InstantiationLocStart, InstantiationLocEnd;
2039dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  public:
204de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    SourceLocation getSpellingLoc() const {
205de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return SourceLocation::getFromRawEncoding(SpellingLoc);
206de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
207e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    SourceLocation getInstantiationLocStart() const {
208e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner      return SourceLocation::getFromRawEncoding(InstantiationLocStart);
209e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    }
210e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    SourceLocation getInstantiationLocEnd() const {
211e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner      return SourceLocation::getFromRawEncoding(InstantiationLocEnd);
212e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    }
2131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
214e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    std::pair<SourceLocation,SourceLocation> getInstantiationLocRange() const {
215e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner      return std::make_pair(getInstantiationLocStart(),
216e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner                            getInstantiationLocEnd());
217e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    }
2181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
219e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    /// get - Return a InstantiationInfo for an expansion.  IL specifies
220df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner    /// the instantiation location (where the macro is expanded), and SL
221df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner    /// specifies the spelling location (where the characters from the token
222e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    /// come from).  IL and PL can both refer to normal File SLocs or
223e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    /// instantiation locations.
224e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    static InstantiationInfo get(SourceLocation ILStart, SourceLocation ILEnd,
225e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner                                 SourceLocation SL) {
226de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      InstantiationInfo X;
227de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      X.SpellingLoc = SL.getRawEncoding();
228e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner      X.InstantiationLocStart = ILStart.getRawEncoding();
229e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner      X.InstantiationLocEnd = ILEnd.getRawEncoding();
2309dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner      return X;
2315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
232de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  };
2331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
234de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// SLocEntry - This is a discriminated union of FileInfo and
235de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// InstantiationInfo.  SourceManager keeps an array of these objects, and
236de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// they are uniquely identified by the FileID datatype.
237de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  class SLocEntry {
238de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned Offset;   // low bit is set for instantiation info.
239de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    union {
240de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      FileInfo File;
241de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      InstantiationInfo Instantiation;
242de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    };
243de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  public:
244de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned getOffset() const { return Offset >> 1; }
2451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
246de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    bool isInstantiation() const { return Offset & 1; }
247de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    bool isFile() const { return !isInstantiation(); }
2481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
249de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    const FileInfo &getFile() const {
250de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      assert(isFile() && "Not a file SLocEntry!");
251de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return File;
252de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
253de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
254de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    const InstantiationInfo &getInstantiation() const {
255de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      assert(isInstantiation() && "Not an instantiation SLocEntry!");
256de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return Instantiation;
257de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
2581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
259de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    static SLocEntry get(unsigned Offset, const FileInfo &FI) {
260de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      SLocEntry E;
261de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      E.Offset = Offset << 1;
262de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      E.File = FI;
263de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return E;
264de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
265de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
266de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    static SLocEntry get(unsigned Offset, const InstantiationInfo &II) {
267de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      SLocEntry E;
268de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      E.Offset = (Offset << 1) | 1;
269de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      E.Instantiation = II;
270de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return E;
271de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
2725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  };
2735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}  // end SrcMgr namespace.
2747f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
2757f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor/// \brief External source of source location entries.
2767f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregorclass ExternalSLocEntrySource {
2777f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregorpublic:
2787f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  virtual ~ExternalSLocEntrySource();
2797f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
2807f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  /// \brief Read the source location entry with index ID.
2817f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  virtual void ReadSLocEntry(unsigned ID) = 0;
2827f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor};
283dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner
284dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner
285dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner/// IsBeforeInTranslationUnitCache - This class holds the cache used by
286dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner/// isBeforeInTranslationUnit.  The cache structure is complex enough to be
287dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner/// worth breaking out of SourceManager.
288dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattnerclass IsBeforeInTranslationUnitCache {
289dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// L/R QueryFID - These are the FID's of the cached query.  If these match up
290dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// with a subsequent query, the result can be reused.
291dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  FileID LQueryFID, RQueryFID;
292dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner
293dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// CommonFID - This is the file found in common between the two #include
294dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// traces.  It is the nearest common ancestor of the #include tree.
295dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  FileID CommonFID;
296dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner
297dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// L/R CommonOffset - This is the offset of the previous query in CommonFID.
298dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// Usually, this represents the location of the #include for QueryFID, but if
299dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// LQueryFID is a parent of RQueryFID (or vise versa) then these can be a
300dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// random token in the parent.
301dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  unsigned LCommonOffset, RCommonOffset;
302dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattnerpublic:
303dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner
304dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// isCacheValid - Return true if the currently cached values match up with
305dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// the specified LHS/RHS query.  If not, we can't use the cache.
306dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  bool isCacheValid(FileID LHS, FileID RHS) const {
307dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    return LQueryFID == LHS && RQueryFID == RHS;
308dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  }
309dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner
310dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// getCachedResult - If the cache is valid, compute the result given the
311dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// specified offsets in the LHS/RHS FID's.
312dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  bool getCachedResult(unsigned LOffset, unsigned ROffset) const {
313dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    // If one of the query files is the common file, use the offset.  Otherwise,
314dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    // use the #include loc in the common file.
315dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    if (LQueryFID != CommonFID) LOffset = LCommonOffset;
316dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    if (RQueryFID != CommonFID) ROffset = RCommonOffset;
317dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    return LOffset < ROffset;
318dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  }
319dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner
320dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  // Set up a new query.
321dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  void setQueryFIDs(FileID LHS, FileID RHS) {
322dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    LQueryFID = LHS;
323dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    RQueryFID = RHS;
324dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  }
325dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner
326dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  void setCommonLoc(FileID commonFID, unsigned lCommonOffset,
327dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner                    unsigned rCommonOffset) {
328dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    CommonFID = commonFID;
329dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    LCommonOffset = lCommonOffset;
330dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    RCommonOffset = rCommonOffset;
331dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  }
332dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner
333dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner};
3347f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
3355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SourceManager - This file handles loading and caching of source files into
3365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// memory.  This object owns the MemoryBuffer objects for all of the loaded
3375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// files and assigns unique FileID's for each unique #include chain.
3385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
3395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// The SourceManager can be queried for information about SourceLocation
340f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner/// objects, turning them into either spelling or instantiation locations.
341f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner/// Spelling locations represent where the bytes corresponding to a token came
342f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner/// from and instantiation locations represent where the location is in the
343f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner/// user's view.  In the case of a macro expansion, for example, the spelling
344f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner/// location indicates where the expanded token came from and the instantiation
345f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner/// location specifies where it was expanded.
3465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass SourceManager {
347f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor  /// \brief Diagnostic object.
348f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor  Diagnostic &Diag;
349f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor
3500d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  mutable llvm::BumpPtrAllocator ContentCacheAlloc;
3511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// FileInfos - Memoized information about all of the files tracked by this
3530d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek  /// SourceManager.  This set allows us to merge ContentCache entries based
3540d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek  /// on their FileEntry*.  All ContentCache objects will thus have unique,
3551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// non-null, FileEntry pointers.
3560d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos;
3571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// MemBufferInfos - Information about various memory buffers that we have
3590d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  /// read in.  All FileEntry* within the stored ContentCache objects are NULL,
3600d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  /// as they do not refer to a file.
3610d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  std::vector<SrcMgr::ContentCache*> MemBufferInfos;
3621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
363de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// SLocEntryTable - This is an array of SLocEntry's that we have created.
364de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// FileID is an index into this vector.  This array is sorted by the offset.
365de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::vector<SrcMgr::SLocEntry> SLocEntryTable;
366de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// NextOffset - This is the next available offset that a new SLocEntry can
367de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// start at.  It is SLocEntryTable.back().getOffset()+size of back() entry.
368de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  unsigned NextOffset;
3697f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
3707f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  /// \brief If source location entries are being lazily loaded from
3717f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  /// an external source, this vector indicates whether the Ith source
3727f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  /// location entry has already been loaded from the external storage.
3737f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  std::vector<bool> SLocEntryLoaded;
3747f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
3757f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  /// \brief An external source for source location entries.
3767f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  ExternalSLocEntrySource *ExternalSLocEntries;
3777f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
378de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// LastFileIDLookup - This is a one-entry cache to speed up getFileID.
379de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// LastFileIDLookup records the last FileID looked up or created, because it
380de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// is very common to look up many tokens from the same file.
381de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  mutable FileID LastFileIDLookup;
3821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3835b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  /// LineTable - This holds information for #line directives.  It is referenced
3845b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  /// by indices from SLocEntryTable.
3855b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  LineTableInfo *LineTable;
3861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3875e36a7a89da5d69ece23fc8228624a053f75c645Chris Lattner  /// LastLineNo - These ivars serve as a cache used in the getLineNumber
3885e36a7a89da5d69ece23fc8228624a053f75c645Chris Lattner  /// method which is used to speedup getLineNumber calls to nearby locations.
3892b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  mutable FileID LastLineNoFileIDQuery;
390f812a45dd93634c9300ed5533bd26b56374714a1Chris Lattner  mutable SrcMgr::ContentCache *LastLineNoContentCache;
391f812a45dd93634c9300ed5533bd26b56374714a1Chris Lattner  mutable unsigned LastLineNoFilePos;
392f812a45dd93634c9300ed5533bd26b56374714a1Chris Lattner  mutable unsigned LastLineNoResult;
3931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
39476edd0e4ae0592a7225d50d0bad6732ac64dca2aTed Kremenek  /// MainFileID - The file ID for the main source file of the translation unit.
3952b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID MainFileID;
39649c1f4aa2a6c360d25d605004ec3c4affd62db77Steve Naroff
397de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  // Statistics for -print-stats.
398de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  mutable unsigned NumLinearScans, NumBinaryProbes;
3991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4002aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis  // Cache results for the isBeforeInTranslationUnit method.
401dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  mutable IsBeforeInTranslationUnitCache IsBeforeInTUCache;
4021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
40349c1f4aa2a6c360d25d605004ec3c4affd62db77Steve Naroff  // SourceManager doesn't support copy construction.
40449c1f4aa2a6c360d25d605004ec3c4affd62db77Steve Naroff  explicit SourceManager(const SourceManager&);
4051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  void operator=(const SourceManager&);
4065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
407f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor  SourceManager(Diagnostic &Diag)
408f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor    : Diag(Diag), ExternalSLocEntries(0), LineTable(0), NumLinearScans(0),
4092968442603b029949246467253eeac8139a5b6d8Douglas Gregor      NumBinaryProbes(0) {
410de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    clearIDTables();
411de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
4125b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  ~SourceManager();
4131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4145b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  void clearIDTables();
4151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
41606a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
41706a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  // MainFileID creation and querying methods.
41806a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
41906a062dc784c609b75dca15fd97f468d0d846596Chris Lattner
42076edd0e4ae0592a7225d50d0bad6732ac64dca2aTed Kremenek  /// getMainFileID - Returns the FileID of the main source file.
4212b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID getMainFileID() const { return MainFileID; }
4221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
42306a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  /// createMainFileID - Create the FileID for the main source file.
42406a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  FileID createMainFileID(const FileEntry *SourceFile,
42506a062dc784c609b75dca15fd97f468d0d846596Chris Lattner                          SourceLocation IncludePos) {
42606a062dc784c609b75dca15fd97f468d0d846596Chris Lattner    assert(MainFileID.isInvalid() && "MainFileID already set!");
42706a062dc784c609b75dca15fd97f468d0d846596Chris Lattner    MainFileID = createFileID(SourceFile, IncludePos, SrcMgr::C_User);
42806a062dc784c609b75dca15fd97f468d0d846596Chris Lattner    return MainFileID;
42906a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  }
4301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
43106a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
432de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  // Methods to create new FileID's and instantiations.
43306a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
4341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// createFileID - Create a new FileID that represents the specified file
4365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// being #included from the specified IncludePosition.  This returns 0 on
4375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// error and translates NULL into standard input.
4381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// PreallocateID should be non-zero to specify which a pre-allocated,
4397f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  /// lazily computed source location is being filled in by this operation.
4402b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
4417f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                      SrcMgr::CharacteristicKind FileCharacter,
4427f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                      unsigned PreallocatedID = 0,
4437f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                      unsigned Offset = 0) {
444de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile);
4452b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner    if (IR == 0) return FileID();    // Error opening file?
4467f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    return createFileID(IR, IncludePos, FileCharacter, PreallocatedID, Offset);
4475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// createFileIDForMemBuffer - Create a new FileID that represents the
4505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// specified memory buffer.  This does no caching of the buffer and takes
4515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once.
4527f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer,
4537f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                                  unsigned PreallocatedID = 0,
4547f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                                  unsigned Offset = 0) {
4557bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber    return createFileID(createMemBufferContentCache(Buffer), SourceLocation(),
4567f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                        SrcMgr::C_User, PreallocatedID, Offset);
4575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4591036b68525f39cb69ac22c679ed440acd8392a16Ted Kremenek  /// createMainFileIDForMembuffer - Create the FileID for a memory buffer
4601036b68525f39cb69ac22c679ed440acd8392a16Ted Kremenek  ///  that will represent the FileID for the main source.  One example
4611036b68525f39cb69ac22c679ed440acd8392a16Ted Kremenek  ///  of when this would be used is when the main source is read from STDIN.
4622b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) {
4632b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner    assert(MainFileID.isInvalid() && "MainFileID already set!");
4647697b5c5a918729718da9ecd9e374bbd973a69e9Chris Lattner    MainFileID = createFileIDForMemBuffer(Buffer);
4651036b68525f39cb69ac22c679ed440acd8392a16Ted Kremenek    return MainFileID;
4661036b68525f39cb69ac22c679ed440acd8392a16Ted Kremenek  }
46706a062dc784c609b75dca15fd97f468d0d846596Chris Lattner
468de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// createInstantiationLoc - Return a new SourceLocation that encodes the fact
469de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// that a token at Loc should actually be referenced from InstantiationLoc.
470de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// TokLength is the length of the token being instantiated.
471de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  SourceLocation createInstantiationLoc(SourceLocation Loc,
472e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner                                        SourceLocation InstantiationLocStart,
473e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner                                        SourceLocation InstantiationLocEnd,
4747f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                                        unsigned TokLength,
4757f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                                        unsigned PreallocatedID = 0,
4767f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                                        unsigned Offset = 0);
4771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4782968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \brief Retrieve the memory buffer associated with the given file.
47950f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  ///
48050f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  /// \param Invalid If non-NULL, will be set \c true if an error
48150f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  /// occurs while retrieving the memory buffer.
48250f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File,
48350f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor                                                   bool *Invalid = 0);
4842968442603b029949246467253eeac8139a5b6d8Douglas Gregor
4852968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \brief Override the contents of the given source file by providing an
4862968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// already-allocated buffer.
4872968442603b029949246467253eeac8139a5b6d8Douglas Gregor  ///
4882968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \param SourceFile the source file whose contents will be override.
4892968442603b029949246467253eeac8139a5b6d8Douglas Gregor  ///
4902968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \param Buffer the memory buffer whose contents will be used as the
4912968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// data in the given source file.
4922968442603b029949246467253eeac8139a5b6d8Douglas Gregor  ///
4932968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \returns true if an error occurred, false otherwise.
4942968442603b029949246467253eeac8139a5b6d8Douglas Gregor  bool overrideFileContents(const FileEntry *SourceFile,
4952968442603b029949246467253eeac8139a5b6d8Douglas Gregor                            const llvm::MemoryBuffer *Buffer);
4962968442603b029949246467253eeac8139a5b6d8Douglas Gregor
49706a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
49806a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  // FileID manipulation methods.
49906a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
5001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5012ffb14f004affc363d86d6c7c63c356190db3679Daniel Dunbar  /// getBuffer - Return the buffer for the specified FileID. If there is an
5022ffb14f004affc363d86d6c7c63c356190db3679Daniel Dunbar  /// error opening this buffer the first time, this manufactures a temporary
5032ffb14f004affc363d86d6c7c63c356190db3679Daniel Dunbar  /// buffer and returns a non-empty error string.
504e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner  const llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc,
505e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner                                      bool *Invalid = 0) const {
506e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner    return getSLocEntry(FID).getFile().getContentCache()
507e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner       ->getBuffer(Diag, *this, Loc, Invalid);
50806a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  }
5091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
510e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner  const llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = 0) const {
511e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner    return getSLocEntry(FID).getFile().getContentCache()
512e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner       ->getBuffer(Diag, *this, SourceLocation(), Invalid);
513e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner  }
514e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner
51506a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  /// getFileEntryForID - Returns the FileEntry record for the provided FileID.
51606a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  const FileEntry *getFileEntryForID(FileID FID) const {
517de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return getSLocEntry(FID).getFile().getContentCache()->Entry;
51806a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  }
5191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
520ceafc4b63599d14f0b5b10ff92e22bf242682dceBenjamin Kramer  /// getBufferData - Return a StringRef to the source buffer data for the
521ceafc4b63599d14f0b5b10ff92e22bf242682dceBenjamin Kramer  /// specified FileID.
522ceafc4b63599d14f0b5b10ff92e22bf242682dceBenjamin Kramer  ///
523f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor  /// \param FID The file ID whose contents will be returned.
524f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor  /// \param Invalid If non-NULL, will be set true if an error occurred.
525f6ac97b101c8840efa92bf29166077ce4049e293Benjamin Kramer  llvm::StringRef getBufferData(FileID FID, bool *Invalid = 0) const;
526f6ac97b101c8840efa92bf29166077ce4049e293Benjamin Kramer
5271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
52806a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
52906a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  // SourceLocation manipulation methods.
53006a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
5311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
532668ab1a36d6a5731c71a75a5f388ecafd538a896Chris Lattner  /// getFileID - Return the FileID for a SourceLocation.  This is a very
533de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// hot method that is used for all SourceManager queries that start with a
534de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// SourceLocation object.  It is responsible for finding the entry in
535de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// SLocEntryTable which contains the specified location.
536de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  ///
537de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  FileID getFileID(SourceLocation SpellingLoc) const {
538de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned SLocOffset = SpellingLoc.getOffset();
5391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
540de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    // If our one-entry cache covers this offset, just return it.
541de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
542de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return LastFileIDLookup;
543de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
544de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return getFileIDSlow(SLocOffset);
545de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
5461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5472b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  /// getLocForStartOfFile - Return the source location corresponding to the
5482b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  /// first byte of the specified file.
5492b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  SourceLocation getLocForStartOfFile(FileID FID) const {
5507f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    assert(FID.ID < SLocEntryTable.size() && "FileID out of range");
5517f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    assert(getSLocEntry(FID).isFile() && "FileID is not a file");
5527f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    unsigned FileOffset = getSLocEntry(FID).getOffset();
553de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return SourceLocation::getFileLoc(FileOffset);
5542b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  }
5551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5566678133b8ce642f93e5141f056fa643112041ad0Chris Lattner  /// getInstantiationLoc - Given a SourceLocation object, return the
5576678133b8ce642f93e5141f056fa643112041ad0Chris Lattner  /// instantiation location referenced by the ID.
558de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  SourceLocation getInstantiationLoc(SourceLocation Loc) const {
559addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner    // Handle the non-mapped case inline, defer to out of line code to handle
560addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner    // instantiations.
561de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (Loc.isFileID()) return Loc;
562addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner    return getInstantiationLocSlowCase(Loc);
563de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
5641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
565e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  /// getImmediateInstantiationRange - Loc is required to be an instantiation
566e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  /// location.  Return the start/end of the instantiation information.
567e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  std::pair<SourceLocation,SourceLocation>
568e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  getImmediateInstantiationRange(SourceLocation Loc) const;
5691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5706678133b8ce642f93e5141f056fa643112041ad0Chris Lattner  /// getInstantiationRange - Given a SourceLocation object, return the
5716678133b8ce642f93e5141f056fa643112041ad0Chris Lattner  /// range of tokens covered by the instantiation in the ultimate file.
5726678133b8ce642f93e5141f056fa643112041ad0Chris Lattner  std::pair<SourceLocation,SourceLocation>
5736678133b8ce642f93e5141f056fa643112041ad0Chris Lattner  getInstantiationRange(SourceLocation Loc) const;
5741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
576de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// getSpellingLoc - Given a SourceLocation object, return the spelling
577de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// location referenced by the ID.  This is the place where the characters
578de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// that make up the lexed token can be found.
579de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  SourceLocation getSpellingLoc(SourceLocation Loc) const {
580addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner    // Handle the non-mapped case inline, defer to out of line code to handle
581addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner    // instantiations.
582de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (Loc.isFileID()) return Loc;
583addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner    return getSpellingLocSlowCase(Loc);
584de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
5851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
586387616edf98739f4a0dd234c907e2b913e6a535dChris Lattner  /// getImmediateSpellingLoc - Given a SourceLocation object, return the
587387616edf98739f4a0dd234c907e2b913e6a535dChris Lattner  /// spelling location referenced by the ID.  This is the first level down
588387616edf98739f4a0dd234c907e2b913e6a535dChris Lattner  /// towards the place where the characters that make up the lexed token can be
589387616edf98739f4a0dd234c907e2b913e6a535dChris Lattner  /// found.  This should not generally be used by clients.
5901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const;
591de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
592de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// getDecomposedLoc - Decompose the specified location into a raw FileID +
593de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// Offset pair.  The first element is the FileID, the second is the
594de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// offset from the start of the buffer of the location.
595de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const {
596de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    FileID FID = getFileID(Loc);
597de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return std::make_pair(FID, Loc.getOffset()-getSLocEntry(FID).getOffset());
598de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
5991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
600de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// getDecomposedInstantiationLoc - Decompose the specified location into a
601de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// raw FileID + Offset pair.  If the location is an instantiation record,
602de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// walk through it until we find the final location instantiated.
603de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned>
604de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  getDecomposedInstantiationLoc(SourceLocation Loc) const {
605de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    FileID FID = getFileID(Loc);
606de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
6071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
608de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned Offset = Loc.getOffset()-E->getOffset();
609de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (Loc.isFileID())
610de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return std::make_pair(FID, Offset);
6111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
612de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return getDecomposedInstantiationLocSlowCase(E, Offset);
613de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
614de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
615de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// getDecomposedSpellingLoc - Decompose the specified location into a raw
616de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// FileID + Offset pair.  If the location is an instantiation record, walk
617de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// through it until we find its spelling record.
618de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned>
619de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  getDecomposedSpellingLoc(SourceLocation Loc) const {
620de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    FileID FID = getFileID(Loc);
621de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
6221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
623de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned Offset = Loc.getOffset()-E->getOffset();
624de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (Loc.isFileID())
625de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return std::make_pair(FID, Offset);
626de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return getDecomposedSpellingLocSlowCase(E, Offset);
6271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
6281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
62952c29081281955d3db9e11d10573b2d38f709099Chris Lattner  /// getFileOffset - This method returns the offset from the start
63052c29081281955d3db9e11d10573b2d38f709099Chris Lattner  /// of the file that the specified SourceLocation represents. This is not very
63152c29081281955d3db9e11d10573b2d38f709099Chris Lattner  /// meaningful for a macro ID.
63252c29081281955d3db9e11d10573b2d38f709099Chris Lattner  unsigned getFileOffset(SourceLocation SpellingLoc) const {
633de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return getDecomposedLoc(SpellingLoc).second;
6345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
6351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
637de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  //===--------------------------------------------------------------------===//
638de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  // Queries about the code at a SourceLocation.
639de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  //===--------------------------------------------------------------------===//
6401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getCharacterData - Return a pointer to the start of the specified location
642de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// in the appropriate spelling MemoryBuffer.
64350f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  ///
64450f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  /// \param Invalid If non-NULL, will be set \c true if an error occurs.
64550f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  const char *getCharacterData(SourceLocation SL, bool *Invalid = 0) const;
6461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6479dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  /// getColumnNumber - Return the column # for the specified file position.
6489dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  /// This is significantly cheaper to compute than the line number.  This
6499dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  /// returns zero if the column number isn't known.  This may only be called on
650f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner  /// a file sloc, so you must choose a spelling or instantiation location
651f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner  /// before calling this method.
65250f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  unsigned getColumnNumber(FileID FID, unsigned FilePos,
65350f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor                           bool *Invalid = 0) const;
65450f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  unsigned getSpellingColumnNumber(SourceLocation Loc,
65550f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor                                   bool *Invalid = 0) const;
65650f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  unsigned getInstantiationColumnNumber(SourceLocation Loc,
65750f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor                                        bool *Invalid = 0) const;
6581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
660df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner  /// getLineNumber - Given a SourceLocation, return the spelling line number
6615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// for the position indicated.  This requires building and caching a table of
6625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// line offsets for the MemoryBuffer, so this is not cheap: use only when
6635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// about to emit a diagnostic.
66450f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = 0) const;
6651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
66650f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  unsigned getInstantiationLineNumber(SourceLocation Loc,
66750f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor                                      bool *Invalid = 0) const;
66850f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
6691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
670bff5c512af8ca7ac92e974e04c06ff4f820e4ee1Chris Lattner  /// Return the filename or buffer identifier of the buffer the location is in.
671bff5c512af8ca7ac92e974e04c06ff4f820e4ee1Chris Lattner  /// Note that this name does not respect #line directives.  Use getPresumedLoc
672bff5c512af8ca7ac92e974e04c06ff4f820e4ee1Chris Lattner  /// for normal clients.
67350f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  const char *getBufferName(SourceLocation Loc, bool *Invalid = 0) const;
6741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6756b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// getFileCharacteristic - return the file characteristic of the specified
6761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// source location, indicating whether this is a normal file, a system
6776b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// header, or an "implicit extern C" system header.
6786b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  ///
6796b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// This state can be modified with flags on GNU linemarker directives like:
6806b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  ///   # 4 "foo.h" 3
6816b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// which changes all source locations in the current file after that to be
6826b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// considered to be from a system header.
6836b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const;
6841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
685b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// getPresumedLoc - This method returns the "presumed" location of a
686b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// SourceLocation specifies.  A "presumed location" can be modified by #line
687b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// or GNU line marker directives.  This provides a view on the data that a
688b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// user should see in diagnostics, for example.
689b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  ///
690b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// Note that a presumed location is always given as the instantiation point
691b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// of an instantiation location, not at the spelling location.
692b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  PresumedLoc getPresumedLoc(SourceLocation Loc) const;
6931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6949fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  /// isFromSameFile - Returns true if both SourceLocations correspond to
6959fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  ///  the same file.
6969fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
697a11d61793341fea195c29a0dab3fbd74f2b39a8cChris Lattner    return getFileID(Loc1) == getFileID(Loc2);
6989fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  }
6991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7009fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  /// isFromMainFile - Returns true if the file of provided SourceLocation is
7019fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  ///   the main file.
7029fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  bool isFromMainFile(SourceLocation Loc) const {
703a11d61793341fea195c29a0dab3fbd74f2b39a8cChris Lattner    return getFileID(Loc) == getMainFileID();
7041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
7051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7067bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber  /// isInSystemHeader - Returns if a SourceLocation is in a system header.
7077bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber  bool isInSystemHeader(SourceLocation Loc) const {
7080b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner    return getFileCharacteristic(Loc) != SrcMgr::C_User;
709721818304ac462d8c6ce05eecd02884033db78f1Chris Lattner  }
7101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7110d456588acac0713a7c33063922d35a8cc8c658eChris Lattner  /// isInExternCSystemHeader - Returns if a SourceLocation is in an "extern C"
7120d456588acac0713a7c33063922d35a8cc8c658eChris Lattner  /// system header.
7130d456588acac0713a7c33063922d35a8cc8c658eChris Lattner  bool isInExternCSystemHeader(SourceLocation Loc) const {
7140d456588acac0713a7c33063922d35a8cc8c658eChris Lattner    return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem;
7150d456588acac0713a7c33063922d35a8cc8c658eChris Lattner  }
7161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
71706a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
7185b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  // Line Table Manipulation Routines
7195b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  //===--------------------------------------------------------------------===//
7201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7215b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  /// getLineTableFilenameID - Return the uniqued ID for the specified filename.
7221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ///
7235b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  unsigned getLineTableFilenameID(const char *Ptr, unsigned Len);
7241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7254c4ea17d7f991516c37a871dfa4bbe5723fa85f0Chris Lattner  /// AddLineNote - Add a line note to the line table for the FileID and offset
7264c4ea17d7f991516c37a871dfa4bbe5723fa85f0Chris Lattner  /// specified by Loc.  If FilenameID is -1, it is considered to be
7274c4ea17d7f991516c37a871dfa4bbe5723fa85f0Chris Lattner  /// unspecified.
7284c4ea17d7f991516c37a871dfa4bbe5723fa85f0Chris Lattner  void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID);
7299d79ebac47ffde6a1cb312f4c09b66b1b9a397fbChris Lattner  void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID,
7301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                   bool IsFileEntry, bool IsFileExit,
7319d79ebac47ffde6a1cb312f4c09b66b1b9a397fbChris Lattner                   bool IsSystemHeader, bool IsExternCHeader);
732bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
733bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  /// \brief Determine if the source manager has a line table.
734bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  bool hasLineTable() const { return LineTable != 0; }
735bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
736bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  /// \brief Retrieve the stored line table.
737bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  LineTableInfo &getLineTable();
738bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
7395b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  //===--------------------------------------------------------------------===//
74006a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  // Other miscellaneous methods.
74106a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
74210b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis
74310b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  /// \brief Get the source location for the given file:line:col triplet.
74410b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  ///
74510b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  /// If the source file is included multiple times, the source location will
74610b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  /// be based upon the first inclusion.
74710b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  SourceLocation getLocation(const FileEntry *SourceFile,
74810b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis                             unsigned Line, unsigned Col) const;
7491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7502aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis  /// \brief Determines the order of 2 source locations in the translation unit.
7512aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis  ///
7522aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis  /// \returns true if LHS source location comes before RHS, false otherwise.
7532aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis  bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const;
7542aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis
755c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner  // Iterators over FileInfos.
7560d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>
7570d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner      ::const_iterator fileinfo_iterator;
758c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner  fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); }
759c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner  fileinfo_iterator fileinfo_end() const { return FileInfos.end(); }
760d93256e55673a17d18543397ec462416acb13792Douglas Gregor  bool hasFileInfo(const FileEntry *File) const {
761d93256e55673a17d18543397ec462416acb13792Douglas Gregor    return FileInfos.find(File) != FileInfos.end();
762d93256e55673a17d18543397ec462416acb13792Douglas Gregor  }
763c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner
7645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// PrintStats - Print statistics to stderr.
7655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
7665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void PrintStats() const;
76778d85f53b093867bbb0123f016956178eea7343eTed Kremenek
76814f79002e58556798e86168c63e48d533287eda5Douglas Gregor  unsigned sloc_entry_size() const { return SLocEntryTable.size(); }
7692d684c2499bd80e28451c1db0e2e040ef00c53bcKovarththanan Rajaratnam
77016b55a71695a33c094383295cc7b7a2080e098daTed Kremenek  // FIXME: Exposing this is a little gross; what we want is a good way
77116b55a71695a33c094383295cc7b7a2080e098daTed Kremenek  //  to iterate the entries that were not defined in a PCH file (or
77216b55a71695a33c094383295cc7b7a2080e098daTed Kremenek  //  any other external source).
77316b55a71695a33c094383295cc7b7a2080e098daTed Kremenek  unsigned sloc_loaded_entry_size() const { return SLocEntryLoaded.size(); }
77414f79002e58556798e86168c63e48d533287eda5Douglas Gregor
775bdfe48ac80573e026595af91e541474dbf02565fDouglas Gregor  const SrcMgr::SLocEntry &getSLocEntry(unsigned ID) const {
776bdfe48ac80573e026595af91e541474dbf02565fDouglas Gregor    assert(ID < SLocEntryTable.size() && "Invalid id");
7771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (ExternalSLocEntries &&
778bdfe48ac80573e026595af91e541474dbf02565fDouglas Gregor        ID < SLocEntryLoaded.size() &&
779bdfe48ac80573e026595af91e541474dbf02565fDouglas Gregor        !SLocEntryLoaded[ID])
780bdfe48ac80573e026595af91e541474dbf02565fDouglas Gregor      ExternalSLocEntries->ReadSLocEntry(ID);
781bdfe48ac80573e026595af91e541474dbf02565fDouglas Gregor    return SLocEntryTable[ID];
782bdfe48ac80573e026595af91e541474dbf02565fDouglas Gregor  }
7832d684c2499bd80e28451c1db0e2e040ef00c53bcKovarththanan Rajaratnam
7842d684c2499bd80e28451c1db0e2e040ef00c53bcKovarththanan Rajaratnam  const SrcMgr::SLocEntry &getSLocEntry(FileID FID) const {
785bdfe48ac80573e026595af91e541474dbf02565fDouglas Gregor    return getSLocEntry(FID.ID);
786bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  }
787bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
788f60e9918690fcf02974bc1ebecd42c99d561855eDouglas Gregor  unsigned getNextOffset() const { return NextOffset; }
789f60e9918690fcf02974bc1ebecd42c99d561855eDouglas Gregor
7907f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  /// \brief Preallocate some number of source location entries, which
7917f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  /// will be loaded as needed from the given external source.
7927f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  void PreallocateSLocEntries(ExternalSLocEntrySource *Source,
7937f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                              unsigned NumSLocEntries,
7947f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                              unsigned NextOffset);
7957f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
7962bf1eb09f06a9792fa94dff0703f2aa2c4bace2aDouglas Gregor  /// \brief Clear out any preallocated source location entries that
7972bf1eb09f06a9792fa94dff0703f2aa2c4bace2aDouglas Gregor  /// haven't already been loaded.
7982bf1eb09f06a9792fa94dff0703f2aa2c4bace2aDouglas Gregor  void ClearPreallocatedSLocEntries();
7992bf1eb09f06a9792fa94dff0703f2aa2c4bace2aDouglas Gregor
8005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerprivate:
801de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// isOffsetInFileID - Return true if the specified FileID contains the
802de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// specified SourceLocation offset.  This is a very hot method.
803de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const {
804de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID);
805de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    // If the entry is after the offset, it can't contain it.
806de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (SLocOffset < Entry.getOffset()) return false;
8071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
808de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    // If this is the last entry than it does.  Otherwise, the entry after it
809de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    // has to not include it.
810de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (FID.ID+1 == SLocEntryTable.size()) return true;
8117f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
8127f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    return SLocOffset < getSLocEntry(FileID::get(FID.ID+1)).getOffset();
813de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
8141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
81578d85f53b093867bbb0123f016956178eea7343eTed Kremenek  /// createFileID - Create a new fileID for the specified ContentCache and
81678d85f53b093867bbb0123f016956178eea7343eTed Kremenek  ///  include position.  This works regardless of whether the ContentCache
81778d85f53b093867bbb0123f016956178eea7343eTed Kremenek  ///  corresponds to a file or some other input source.
8182b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID createFileID(const SrcMgr::ContentCache* File,
8192b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner                      SourceLocation IncludePos,
8207f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                      SrcMgr::CharacteristicKind DirCharacter,
8217f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                      unsigned PreallocatedID = 0,
8227f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                      unsigned Offset = 0);
8231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
824de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  const SrcMgr::ContentCache *
825de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    getOrCreateContentCache(const FileEntry *SourceFile);
826c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek
82778d85f53b093867bbb0123f016956178eea7343eTed Kremenek  /// createMemBufferContentCache - Create a new ContentCache for the specified
82878d85f53b093867bbb0123f016956178eea7343eTed Kremenek  ///  memory buffer.
8291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const SrcMgr::ContentCache*
8302b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  createMemBufferContentCache(const llvm::MemoryBuffer *Buf);
8311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
832de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  FileID getFileIDSlow(unsigned SLocOffset) const;
833de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
834addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner  SourceLocation getInstantiationLocSlowCase(SourceLocation Loc) const;
835addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner  SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;
836addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner
837de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned>
838de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E,
8391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        unsigned Offset) const;
840de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned>
841de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
842de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner                                   unsigned Offset) const;
8435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
8445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}  // end namespace clang
8475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif
849