SourceManager.h revision ac50e3427cb9eb3dc9f13f29a78f00ef3122433d
14e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)//===--- SourceManager.h - Track and cache source files ---------*- C++ -*-===//
2c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//
3c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//                     The LLVM Compiler Infrastructure
4c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//
5c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// This file is distributed under the University of Illinois Open Source
6c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// License. See LICENSE.TXT for details.
7c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//
8010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)//===----------------------------------------------------------------------===//
94e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)//
109ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch//  This file defines the SourceManager interface.
11010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)//
12116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch//===----------------------------------------------------------------------===//
13010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#ifndef LLVM_CLANG_SOURCEMANAGER_H
15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#define LLVM_CLANG_SOURCEMANAGER_H
164e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
17b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "clang/Basic/SourceLocation.h"
18010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "llvm/Bitcode/SerializationFwd.h"
19010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "llvm/Support/Allocator.h"
20010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "llvm/Support/DataTypes.h"
21010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "llvm/ADT/DenseMap.h"
22c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include <vector>
23c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include <cassert>
244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)namespace llvm {
268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)class MemoryBuffer;
274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)namespace clang {
30010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
31010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)class SourceManager;
32010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)class FileManager;
33010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)class FileEntry;
34010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)class IdentifierTokenInfo;
35c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class LineTableInfo;
364e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
37c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)/// SrcMgr - Public enums and private classes that are part of the
384e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)/// SourceManager implementation.
39c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)///
40c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace SrcMgr {
414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// CharacteristicKind - This is used to represent whether a file or directory
424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// holds normal user code, system code, or system code which is implicitly
434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// 'extern "C"' in C++ mode.  Entire directories can be tagged with this
444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// (this is maintained by DirectoryLookup and friends) as can specific
454e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// FileIDInfos when a #pragma system_header is seen or various other cases.
46c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ///
47c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  enum CharacteristicKind {
484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    C_User, C_System, C_ExternCSystem
494e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  };
504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
51424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  /// ContentCache - Once instance of this struct is kept for every file
52424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  /// loaded or used.  This object owns the MemoryBuffer object.
538bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  class ContentCache {
548bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    /// Buffer - The actual buffer containing the characters from the input
558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    /// file.  This is owned by the ContentCache object.
568bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    mutable const llvm::MemoryBuffer *Buffer;
578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  public:
598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    /// Reference to the file entry.  This reference does not own
608bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    /// the FileEntry object.  It is possible for this to be NULL if
618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    /// the ContentCache encapsulates an imaginary text buffer.
628bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    const FileEntry *Entry;
634e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
64c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    /// SourceLineCache - A bump pointer allocated array of offsets for each
654e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    /// source line.  This is lazily computed.  This is owned by the
664e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    /// SourceManager BumpPointerAllocator object.
67c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    unsigned *SourceLineCache;
68c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
69c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    /// NumLines - The number of lines in this ContentCache.  This is only valid
70b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    /// if SourceLineCache is non-null.
71b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    unsigned NumLines;
72c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
73c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    /// getBuffer - Returns the memory buffer for the associated content.
744e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    const llvm::MemoryBuffer *getBuffer() const;
754e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
764e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    /// getSize - Returns the size of the content encapsulated by this
774e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    ///  ContentCache. This can be the size of the source file or the size of an
784e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    ///  arbitrary scratch buffer.  If the ContentCache encapsulates a source
79c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    ///  file this size is retrieved from the file's FileEntry.
80c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    unsigned getSize() const;
814e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
824e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    /// getSizeBytesMapped - Returns the number of bytes actually mapped for
834e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    ///  this ContentCache.  This can be 0 if the MemBuffer was not actually
844e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    ///  instantiated.
85c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    unsigned getSizeBytesMapped() const;
864e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
87c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    void setBuffer(const llvm::MemoryBuffer *B) {
88c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      assert(!Buffer && "MemoryBuffer already set.");
894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      Buffer = B;
904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    }
914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
924e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    ContentCache(const FileEntry *Ent = 0)
93c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      : Buffer(0), Entry(Ent), SourceLineCache(0), NumLines(0) {}
94c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
951e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    ~ContentCache();
961e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
971e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    /// The copy ctor does not allow copies where source object has either
981e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    ///  a non-NULL Buffer or SourceLineCache.  Ownership of allocated memory
991e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    ///  is not transfered, so this is a logical error.
100c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    ContentCache(const ContentCache &RHS) : Buffer(0), SourceLineCache(0) {
101c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      Entry = RHS.Entry;
1024e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
103c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      assert (RHS.Buffer == 0 && RHS.SourceLineCache == 0
1044e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)              && "Passed ContentCache object cannot own a buffer.");
1054e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1064e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      NumLines = RHS.NumLines;
1074e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    }
1084e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1094e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    /// Emit - Emit this ContentCache to Bitcode.
1104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    void Emit(llvm::Serializer &S) const;
111c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
112c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    /// ReadToSourceManager - Reconstitute a ContentCache from Bitcode
1134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    //   and store it in the specified SourceManager.
1144e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    static void ReadToSourceManager(llvm::Deserializer &D, SourceManager &SM,
1154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                    FileManager *FMgr, std::vector<char> &Buf);
116c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
117c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  private:
1184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    // Disable assignments.
1194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    ContentCache &operator=(const ContentCache& RHS);
1204e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  };
1214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// FileInfo - Information about a FileID, basically just the logical file
123c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// that it represents and include stack information.
124c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ///
1254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// Each FileInfo has include stack information, indicating where it came
1264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// from.  This information encodes the #include chain that a token was
1274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// instantiated from.  The main include file has an invalid IncludeLoc.
1284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  ///
1294e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// FileInfos contain a "ContentCache *", with the contents of the file.
1304e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  ///
131c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  class FileInfo {
132c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    /// IncludeLoc - The location of the #include that brought in this file.
133c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    /// This is an invalid SLOC for the main file (top of the #include chain).
134c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    unsigned IncludeLoc;  // Really a SourceLocation
1354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
136c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    /// Data - This contains the ContentCache* and the bits indicating the
137c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    /// characteristic of the file and whether it has #line info, all bitmangled
138a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    /// together.
139a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    uintptr_t Data;
140a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  public:
141a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    /// get - Return a FileInfo object.
1424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    static FileInfo get(SourceLocation IL, const ContentCache *Con,
143a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                        CharacteristicKind FileCharacter) {
144a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      FileInfo X;
1454e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      X.IncludeLoc = IL.getRawEncoding();
1464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      X.Data = (uintptr_t)Con;
1474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned");
1484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      assert((unsigned)FileCharacter < 4 && "invalid file character");
149c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      X.Data |= (unsigned)FileCharacter;
150c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return X;
1514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    }
152c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1534e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    SourceLocation getIncludeLoc() const {
1544e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      return SourceLocation::getFromRawEncoding(IncludeLoc);
155c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
156c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const ContentCache* getContentCache() const {
1574e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      return reinterpret_cast<const ContentCache*>(Data & ~7UL);
1584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    }
1594e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    /// getCharacteristic - Return whether this is a system header or not.
1614e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    CharacteristicKind getFileCharacteristic() const {
1624e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      return (CharacteristicKind)(Data & 3);
1634e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    }
1644e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1654e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    /// hasLineDirectives - Return true if this FileID has #line directives in
1664e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    /// it.
1674e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    bool hasLineDirectives() const { return (Data & 4) != 0; }
1684e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1694e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    /// setHasLineDirectives - Set the flag that indicates that this FileID has
1704e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    /// line table entries associated with it.
1714e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    void setHasLineDirectives() {
1724e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      Data |= 4;
1734e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    }
1744e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  };
1754e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1764e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// InstantiationInfo - Each InstantiationInfo encodes the Instantiation
1774e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// location - where the token was ultimately instantiated, and the
1784e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// SpellingLoc - where the actual character data for the token came from.
1794e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  class InstantiationInfo {
1804e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    unsigned InstantiationLoc, SpellingLoc; // Really these are SourceLocations.
1814e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  public:
1824e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    SourceLocation getInstantiationLoc() const {
1834e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      return SourceLocation::getFromRawEncoding(InstantiationLoc);
1844e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    }
1854e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    SourceLocation getSpellingLoc() const {
1864e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      return SourceLocation::getFromRawEncoding(SpellingLoc);
1874e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    }
1884e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    /// get - Return a InstantiationInfo for an expansion.  VL specifies
1904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    /// the instantiation location (where the macro is expanded), and SL
191c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    /// specifies the spelling location (where the characters from the token
192c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    /// come from).  Both VL and PL refer to normal File SLocs.
193c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    static InstantiationInfo get(SourceLocation IL, SourceLocation SL) {
194c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      InstantiationInfo X;
1954e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      X.InstantiationLoc = IL.getRawEncoding();
1964e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      X.SpellingLoc = SL.getRawEncoding();
197c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return X;
198c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
1994e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  };
2004e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2014e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// SLocEntry - This is a discriminated union of FileInfo and
2024e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// InstantiationInfo.  SourceManager keeps an array of these objects, and
2034e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// they are uniquely identified by the FileID datatype.
2044e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  class SLocEntry {
2054e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    unsigned Offset;   // low bit is set for instantiation info.
206c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    union {
207c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FileInfo File;
208c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      InstantiationInfo Instantiation;
2094e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    };
2104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  public:
2114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    unsigned getOffset() const { return Offset >> 1; }
2124e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
213c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    bool isInstantiation() const { return Offset & 1; }
214c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    bool isFile() const { return !isInstantiation(); }
2154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2164e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    const FileInfo &getFile() const {
2174e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      assert(isFile() && "Not a file SLocEntry!");
2184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      return File;
2194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    }
2204e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    const InstantiationInfo &getInstantiation() const {
2224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      assert(isInstantiation() && "Not an instantiation SLocEntry!");
2234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      return Instantiation;
2244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    }
2254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
226c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    static SLocEntry get(unsigned Offset, const FileInfo &FI) {
227c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      SLocEntry E;
2284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      E.Offset = Offset << 1;
2294e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      E.File = FI;
2304e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      return E;
2314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    }
232c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
233c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    static SLocEntry get(unsigned Offset, const InstantiationInfo &II) {
2344e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      SLocEntry E;
2354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      E.Offset = (Offset << 1) | 1;
2364e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      E.Instantiation = II;
2374e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      return E;
2384e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    }
2394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  };
2404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}  // end SrcMgr namespace.
2414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)/// SourceManager - This file handles loading and caching of source files into
2434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)/// memory.  This object owns the MemoryBuffer objects for all of the loaded
2444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)/// files and assigns unique FileID's for each unique #include chain.
245c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)///
246c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)/// The SourceManager can be queried for information about SourceLocation
247c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)/// objects, turning them into either spelling or instantiation locations.
248c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)/// Spelling locations represent where the bytes corresponding to a token came
249c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)/// from and instantiation locations represent where the location is in the
250c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)/// user's view.  In the case of a macro expansion, for example, the spelling
2514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)/// location indicates where the expanded token came from and the instantiation
2524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)/// location specifies where it was expanded.
2534e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)class SourceManager {
254c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  mutable llvm::BumpPtrAllocator ContentCacheAlloc;
255c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2564e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// FileInfos - Memoized information about all of the files tracked by this
2574e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// SourceManager.  This set allows us to merge ContentCache entries based
2584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// on their FileEntry*.  All ContentCache objects will thus have unique,
259010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  /// non-null, FileEntry pointers.
260010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos;
261010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
262010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  /// MemBufferInfos - Information about various memory buffers that we have
263010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  /// read in.  All FileEntry* within the stored ContentCache objects are NULL,
264010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  /// as they do not refer to a file.
265010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  std::vector<SrcMgr::ContentCache*> MemBufferInfos;
266010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
267010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  /// SLocEntryTable - This is an array of SLocEntry's that we have created.
268010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  /// FileID is an index into this vector.  This array is sorted by the offset.
269010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  std::vector<SrcMgr::SLocEntry> SLocEntryTable;
270010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  /// NextOffset - This is the next available offset that a new SLocEntry can
271010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  /// start at.  It is SLocEntryTable.back().getOffset()+size of back() entry.
272010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  unsigned NextOffset;
273010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
274010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  /// LastFileIDLookup - This is a one-entry cache to speed up getFileID.
275010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  /// LastFileIDLookup records the last FileID looked up or created, because it
276010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  /// is very common to look up many tokens from the same file.
277010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  mutable FileID LastFileIDLookup;
278010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
279010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  /// LineTable - This holds information for #line directives.  It is referenced
280010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  /// by indices from SLocEntryTable.
281010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  LineTableInfo *LineTable;
282010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
283010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  /// LastLineNo - These ivars serve as a cache used in the getLineNumber
284010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  /// method which is used to speedup getLineNumber calls to nearby locations.
285010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  mutable FileID LastLineNoFileIDQuery;
286010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  mutable SrcMgr::ContentCache *LastLineNoContentCache;
287010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  mutable unsigned LastLineNoFilePos;
288010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  mutable unsigned LastLineNoResult;
289010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
290010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  /// MainFileID - The file ID for the main source file of the translation unit.
291010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  FileID MainFileID;
292010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
293010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // Statistics for -print-stats.
294010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  mutable unsigned NumLinearScans, NumBinaryProbes;
295010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
296010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // SourceManager doesn't support copy construction.
297010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  explicit SourceManager(const SourceManager&);
298010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  void operator=(const SourceManager&);
299010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)public:
300010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  SourceManager() : LineTable(0), NumLinearScans(0), NumBinaryProbes(0) {
301010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    clearIDTables();
302010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  }
303010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  ~SourceManager();
304010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
305010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  void clearIDTables();
306010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
307010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  //===--------------------------------------------------------------------===//
308116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // MainFileID creation and querying methods.
309116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  //===--------------------------------------------------------------------===//
310116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
311116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// getMainFileID - Returns the FileID of the main source file.
312010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  FileID getMainFileID() const { return MainFileID; }
3134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
314c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// createMainFileID - Create the FileID for the main source file.
315c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  FileID createMainFileID(const FileEntry *SourceFile,
316c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                          SourceLocation IncludePos) {
317c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    assert(MainFileID.isInvalid() && "MainFileID already set!");
3184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    MainFileID = createFileID(SourceFile, IncludePos, SrcMgr::C_User);
3194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    return MainFileID;
320c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
321c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  //===--------------------------------------------------------------------===//
3234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Methods to create new FileID's and instantiations.
3244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  //===--------------------------------------------------------------------===//
325c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
326c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// createFileID - Create a new FileID that represents the specified file
3274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// being #included from the specified IncludePosition.  This returns 0 on
3284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// error and translates NULL into standard input.
3294e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
3304e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                      SrcMgr::CharacteristicKind FileCharacter) {
331c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile);
332c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (IR == 0) return FileID();    // Error opening file?
3334e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    return createFileID(IR, IncludePos, FileCharacter);
3344e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
3354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
3364e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// createFileIDForMemBuffer - Create a new FileID that represents the
337c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// specified memory buffer.  This does no caching of the buffer and takes
338c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once.
3394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) {
3404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    return createFileID(createMemBufferContentCache(Buffer), SourceLocation(),
3414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                        SrcMgr::C_User);
3424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
343c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
344c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// createMainFileIDForMembuffer - Create the FileID for a memory buffer
3454e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  ///  that will represent the FileID for the main source.  One example
346a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ///  of when this would be used is when the main source is read from STDIN.
3474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) {
3484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    assert(MainFileID.isInvalid() && "MainFileID already set!");
349c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    MainFileID = createFileIDForMemBuffer(Buffer);
350c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return MainFileID;
3514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
352a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
3534e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// createInstantiationLoc - Return a new SourceLocation that encodes the fact
3544e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// that a token at Loc should actually be referenced from InstantiationLoc.
3554e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// TokLength is the length of the token being instantiated.
356c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SourceLocation createInstantiationLoc(SourceLocation Loc,
357c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                        SourceLocation InstantiationLoc,
3584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                        unsigned TokLength);
359a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
3604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  //===--------------------------------------------------------------------===//
3618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // FileID manipulation methods.
3624e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  //===--------------------------------------------------------------------===//
363c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3644e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// getBuffer - Return the buffer for the specified FileID.
365c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ///
366c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const llvm::MemoryBuffer *getBuffer(FileID FID) const {
3674e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    return getSLocEntry(FID).getFile().getContentCache()->getBuffer();
3684e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
3694e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
3708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  /// getFileEntryForID - Returns the FileEntry record for the provided FileID.
3714e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  const FileEntry *getFileEntryForID(FileID FID) const {
3724e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    return getSLocEntry(FID).getFile().getContentCache()->Entry;
3734e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
374c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
375c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// getBufferData - Return a pointer to the start and end of the source buffer
376c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// data for the specified FileID.
377c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  std::pair<const char*, const char*> getBufferData(FileID FID) const;
3788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
379c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
380c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  //===--------------------------------------------------------------------===//
3814e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // SourceLocation manipulation methods.
3824e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  //===--------------------------------------------------------------------===//
383c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
384c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// getFileIDSlow - Return the FileID for a SourceLocation.  This is a very
3854e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// hot method that is used for all SourceManager queries that start with a
386c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// SourceLocation object.  It is responsible for finding the entry in
3878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  /// SLocEntryTable which contains the specified location.
388c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ///
3894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  FileID getFileID(SourceLocation SpellingLoc) const {
3904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    unsigned SLocOffset = SpellingLoc.getOffset();
3914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
3924e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    // If our one-entry cache covers this offset, just return it.
393c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
394c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return LastFileIDLookup;
3954e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
3964e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    return getFileIDSlow(SLocOffset);
3978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
3984e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
3994e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// getLocForStartOfFile - Return the source location corresponding to the
400868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  /// first byte of the specified file.
401b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  SourceLocation getLocForStartOfFile(FileID FID) const {
4024e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    assert(FID.ID < SLocEntryTable.size() && SLocEntryTable[FID.ID].isFile());
403c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    unsigned FileOffset = SLocEntryTable[FID.ID].getOffset();
404c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return SourceLocation::getFileLoc(FileOffset);
4054e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
4064e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
4078bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  /// Given a SourceLocation object, return the instantiation location
4084e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// referenced by the ID.
4094e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  SourceLocation getInstantiationLoc(SourceLocation Loc) const {
4104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    // Handle the non-mapped case inline, defer to out of line code to handle
411bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    // instantiations.
412bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    if (Loc.isFileID()) return Loc;
413c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return getInstantiationLocSlowCase(Loc);
414c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
4158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
416c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// getSpellingLoc - Given a SourceLocation object, return the spelling
417c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// location referenced by the ID.  This is the place where the characters
418c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// that make up the lexed token can be found.
419c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SourceLocation getSpellingLoc(SourceLocation Loc) const {
420c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Handle the non-mapped case inline, defer to out of line code to handle
421c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // instantiations.
4224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    if (Loc.isFileID()) return Loc;
423c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return getSpellingLocSlowCase(Loc);
424c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
4254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
4264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// getDecomposedLoc - Decompose the specified location into a raw FileID +
4278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  /// Offset pair.  The first element is the FileID, the second is the
4284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// offset from the start of the buffer of the location.
4294e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const {
4304e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    FileID FID = getFileID(Loc);
4314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    return std::make_pair(FID, Loc.getOffset()-getSLocEntry(FID).getOffset());
432c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
433c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
4344e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// getDecomposedInstantiationLoc - Decompose the specified location into a
435c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// raw FileID + Offset pair.  If the location is an instantiation record,
4368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  /// walk through it until we find the final location instantiated.
437c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  std::pair<FileID, unsigned>
4384e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  getDecomposedInstantiationLoc(SourceLocation Loc) const {
4394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    FileID FID = getFileID(Loc);
4404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
4414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
442c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    unsigned Offset = Loc.getOffset()-E->getOffset();
443c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (Loc.isFileID())
4444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      return std::make_pair(FID, Offset);
4454e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
4468bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return getDecomposedInstantiationLocSlowCase(E, Offset);
4474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
4484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
4494e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// getDecomposedSpellingLoc - Decompose the specified location into a raw
4504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// FileID + Offset pair.  If the location is an instantiation record, walk
4514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// through it until we find its spelling record.
452c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  std::pair<FileID, unsigned>
453c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  getDecomposedSpellingLoc(SourceLocation Loc) const {
4540f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    FileID FID = getFileID(Loc);
4550f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
4560f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
4570f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    unsigned Offset = Loc.getOffset()-E->getOffset();
4580f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    if (Loc.isFileID())
4590f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      return std::make_pair(FID, Offset);
4600f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    return getDecomposedSpellingLocSlowCase(E, Offset);
4610f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  }
4620f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
463a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  /// getFileOffset - This method returns the offset from the start
464a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  /// of the file that the specified SourceLocation represents. This is not very
465a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  /// meaningful for a macro ID.
466a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  unsigned getFileOffset(SourceLocation SpellingLoc) const {
467a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return getDecomposedLoc(SpellingLoc).second;
468116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
469116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
470a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
471a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  //===--------------------------------------------------------------------===//
472a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Queries about the code at a SourceLocation.
473a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  //===--------------------------------------------------------------------===//
474a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
475a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  /// getCharacterData - Return a pointer to the start of the specified location
476a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  /// in the appropriate spelling MemoryBuffer.
477116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  const char *getCharacterData(SourceLocation SL) const;
478116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
479116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// getColumnNumber - Return the column # for the specified file position.
480116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// This is significantly cheaper to compute than the line number.  This
481116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// returns zero if the column number isn't known.  This may only be called on
482a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  /// a file sloc, so you must choose a spelling or instantiation location
483a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  /// before calling this method.
484a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  unsigned getColumnNumber(SourceLocation Loc) const;
485a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
486a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  unsigned getSpellingColumnNumber(SourceLocation Loc) const {
487a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return getColumnNumber(getSpellingLoc(Loc));
488a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
489116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  unsigned getInstantiationColumnNumber(SourceLocation Loc) const {
490116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return getColumnNumber(getInstantiationLoc(Loc));
491a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
492a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
493effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
494effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  /// getLineNumber - Given a SourceLocation, return the spelling line number
495effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  /// for the position indicated.  This requires building and caching a table of
496effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  /// line offsets for the MemoryBuffer, so this is not cheap: use only when
497effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  /// about to emit a diagnostic.
498116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  unsigned getLineNumber(SourceLocation Loc) const;
499116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
500effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  unsigned getInstantiationLineNumber(SourceLocation Loc) const {
501effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return getLineNumber(getInstantiationLoc(Loc));
502a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
503a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  unsigned getSpellingLineNumber(SourceLocation Loc) const {
504a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return getLineNumber(getSpellingLoc(Loc));
505a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
506a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
507116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // FIXME: This should handle #line.
508116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const {
509116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    FileID FID = getFileID(getSpellingLoc(Loc));
510116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return getSLocEntry(FID).getFile().getFileCharacteristic();
511116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
512116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
513116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// getPresumedLoc - This method returns the "presumed" location of a
514116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// SourceLocation specifies.  A "presumed location" can be modified by #line
515116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// or GNU line marker directives.  This provides a view on the data that a
516116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// user should see in diagnostics, for example.
517116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ///
518116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// Note that a presumed location is always given as the instantiation point
519116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// of an instantiation location, not at the spelling location.
520116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  PresumedLoc getPresumedLoc(SourceLocation Loc) const;
521116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
522116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
523116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
524116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// isFromSameFile - Returns true if both SourceLocations correspond to
525116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ///  the same file.
526116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
527116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return getFileID(Loc1) == getFileID(Loc2);
528116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
529116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
530116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// isFromMainFile - Returns true if the file of provided SourceLocation is
531116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ///   the main file.
532116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  bool isFromMainFile(SourceLocation Loc) const {
533a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return getFileID(Loc) == getMainFileID();
534a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
5358bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
5368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  /// isInSystemHeader - Returns if a SourceLocation is in a system header.
5378bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  bool isInSystemHeader(SourceLocation Loc) const {
5388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return getFileCharacteristic(Loc) != SrcMgr::C_User;
5398bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
5408bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
5418bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  //===--------------------------------------------------------------------===//
5428bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Line Table Manipulation Routines
5438bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  //===--------------------------------------------------------------------===//
5448bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
5454e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// getLineTableFilenameID - Return the uniqued ID for the specified filename.
5464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  ///
5474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  unsigned getLineTableFilenameID(const char *Ptr, unsigned Len);
5484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
5494e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// AddLineNote - Add a line note to the line table for the FileID and offset
5504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// specified by Loc.  If FilenameID is -1, it is considered to be
5514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// unspecified.
552a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID);
553116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
554a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  //===--------------------------------------------------------------------===//
555a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Other miscellaneous methods.
556a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  //===--------------------------------------------------------------------===//
557a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
558a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Iterators over FileInfos.
559a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>
560a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      ::const_iterator fileinfo_iterator;
561a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); }
562a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  fileinfo_iterator fileinfo_end() const { return FileInfos.end(); }
5634e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
5644e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// PrintStats - Print statistics to stderr.
5654e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  ///
5664e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  void PrintStats() const;
5674e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
5684e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// Emit - Emit this SourceManager to Bitcode.
5694e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  void Emit(llvm::Serializer& S) const;
5704e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
5714e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// Read - Reconstitute a SourceManager from Bitcode.
5724e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  static SourceManager* CreateAndRegister(llvm::Deserializer& S,
5734e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                          FileManager &FMgr);
5744e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
5754e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)private:
5764e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  friend struct SrcMgr::ContentCache; // Used for deserialization.
5774e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
5784e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// isOffsetInFileID - Return true if the specified FileID contains the
5794e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// specified SourceLocation offset.  This is a very hot method.
5804e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const {
5814e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID);
5824e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    // If the entry is after the offset, it can't contain it.
5834e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    if (SLocOffset < Entry.getOffset()) return false;
5844e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
5854e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    // If this is the last entry than it does.  Otherwise, the entry after it
5864e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    // has to not include it.
5874e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    if (FID.ID+1 == SLocEntryTable.size()) return true;
5884e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    return SLocOffset < SLocEntryTable[FID.ID+1].getOffset();
5894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
5904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
5914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  /// createFileID - Create a new fileID for the specified ContentCache and
5924e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  ///  include position.  This works regardless of whether the ContentCache
5934e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  ///  corresponds to a file or some other input source.
594c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  FileID createFileID(const SrcMgr::ContentCache* File,
595c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                      SourceLocation IncludePos,
5964e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                      SrcMgr::CharacteristicKind DirCharacter);
597
598  const SrcMgr::ContentCache *
599    getOrCreateContentCache(const FileEntry *SourceFile);
600
601  /// createMemBufferContentCache - Create a new ContentCache for the specified
602  ///  memory buffer.
603  const SrcMgr::ContentCache*
604  createMemBufferContentCache(const llvm::MemoryBuffer *Buf);
605
606  const SrcMgr::SLocEntry &getSLocEntry(FileID FID) const {
607    assert(FID.ID < SLocEntryTable.size() && "Invalid id");
608    return SLocEntryTable[FID.ID];
609  }
610
611  FileID getFileIDSlow(unsigned SLocOffset) const;
612
613  SourceLocation getInstantiationLocSlowCase(SourceLocation Loc) const;
614  SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;
615
616  std::pair<FileID, unsigned>
617  getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E,
618                                        unsigned Offset) const;
619  std::pair<FileID, unsigned>
620  getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
621                                   unsigned Offset) const;
622};
623
624
625}  // end namespace clang
626
627#endif
628