SourceManager.h revision 686775deca8b8685eb90801495880e3abdd844c2
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===--- SourceManager.h - Track and cache source files ---------*- C++ -*-===//
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//                     The LLVM Compiler Infrastructure
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file is distributed under the University of Illinois Open Source
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// License. See LICENSE.TXT for details.
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//===----------------------------------------------------------------------===//
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
10c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//  This file defines the SourceManager interface.
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===//
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#ifndef LLVM_CLANG_SOURCEMANAGER_H
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LLVM_CLANG_SOURCEMANAGER_H
16c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/Basic/SourceLocation.h"
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/Support/Allocator.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Support/DataTypes.h"
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/ADT/PointerIntPair.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/ADT/PointerUnion.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/ADT/IntrusiveRefCntPtr.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/ADT/DenseMap.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Support/MemoryBuffer.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <cassert>
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace llvm {
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class StringRef;
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace clang {
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Diagnostic;
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class SourceManager;
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class FileManager;
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class FileEntry;
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class LineTableInfo;
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class LangOptions;
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// SrcMgr - Public enums and private classes that are part of the
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// SourceManager implementation.
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace SrcMgr {
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// CharacteristicKind - This is used to represent whether a file or directory
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// holds normal user code, system code, or system code which is implicitly
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// 'extern "C"' in C++ mode.  Entire directories can be tagged with this
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// (this is maintained by DirectoryLookup and friends) as can specific
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// FileInfos when a #pragma system_header is seen or various other cases.
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum CharacteristicKind {
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    C_User, C_System, C_ExternCSystem
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// ContentCache - One instance of this struct is kept for every file
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// loaded or used.  This object owns the MemoryBuffer object.
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class ContentCache {
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    enum CCFlags {
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      /// \brief Whether the buffer is invalid.
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      InvalidFlag = 0x01,
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      /// \brief Whether the buffer should not be freed on destruction.
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      DoNotFreeFlag = 0x02
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    };
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// Buffer - The actual buffer containing the characters from the input
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// file.  This is owned by the ContentCache object.
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// The bits indicate indicates whether the buffer is invalid.
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    mutable llvm::PointerIntPair<const llvm::MemoryBuffer *, 2> Buffer;
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  public:
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// Reference to the file entry representing this ContentCache.
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// This reference does not own the FileEntry object.
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// It is possible for this to be NULL if
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// the ContentCache encapsulates an imaginary text buffer.
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const FileEntry *OrigEntry;
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// \brief References the file which the contents were actually loaded from.
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// Can be different from 'Entry' if we overridden the contents of one file
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// with the contents of another file.
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const FileEntry *ContentsEntry;
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// SourceLineCache - A bump pointer allocated array of offsets for each
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// source line.  This is lazily computed.  This is owned by the
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// SourceManager BumpPointerAllocator object.
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned *SourceLineCache;
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// NumLines - The number of lines in this ContentCache.  This is only valid
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// if SourceLineCache is non-null.
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned NumLines;
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// getBuffer - Returns the memory buffer for the associated content.
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ///
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// \param Diag Object through which diagnostics will be emitted if the
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// buffer cannot be retrieved.
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ///
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// \param Loc If specified, is the location that invalid file diagnostics
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ///     will be emitted at.
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ///
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// \param Invalid If non-NULL, will be set \c true if an error occurred.
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const llvm::MemoryBuffer *getBuffer(Diagnostic &Diag,
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        const SourceManager &SM,
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        SourceLocation Loc = SourceLocation(),
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        bool *Invalid = 0) const;
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// getSize - Returns the size of the content encapsulated by this
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ///  ContentCache. This can be the size of the source file or the size of an
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ///  arbitrary scratch buffer.  If the ContentCache encapsulates a source
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ///  file this size is retrieved from the file's FileEntry.
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned getSize() const;
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// getSizeBytesMapped - Returns the number of bytes actually mapped for
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ///  this ContentCache.  This can be 0 if the MemBuffer was not actually
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ///  instantiated.
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned getSizeBytesMapped() const;
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// Returns the kind of memory used to back the memory buffer for
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// this content cache.  This is used for performance analysis.
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    llvm::MemoryBuffer::BufferKind getMemoryBufferKind() const;
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    void setBuffer(const llvm::MemoryBuffer *B) {
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      assert(!Buffer.getPointer() && "MemoryBuffer already set.");
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      Buffer.setPointer(B);
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      Buffer.setInt(false);
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// \brief Get the underlying buffer, returning NULL if the buffer is not
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// yet available.
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const llvm::MemoryBuffer *getRawBuffer() const {
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return Buffer.getPointer();
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// \brief Replace the existing buffer (which will be deleted)
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// with the given buffer.
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    void replaceBuffer(const llvm::MemoryBuffer *B, bool DoNotFree = false);
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// \brief Determine whether the buffer itself is invalid.
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool isBufferInvalid() const {
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return Buffer.getInt() & InvalidFlag;
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// \brief Determine whether the buffer should be freed.
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool shouldFreeBuffer() const {
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return (Buffer.getInt() & DoNotFreeFlag) == 0;
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ContentCache(const FileEntry *Ent = 0)
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      : Buffer(0, false), OrigEntry(Ent), ContentsEntry(Ent),
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        SourceLineCache(0), NumLines(0) {}
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ContentCache(const FileEntry *Ent, const FileEntry *contentEnt)
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      : Buffer(0, false), OrigEntry(Ent), ContentsEntry(contentEnt),
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        SourceLineCache(0), NumLines(0) {}
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ~ContentCache();
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// The copy ctor does not allow copies where source object has either
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ///  a non-NULL Buffer or SourceLineCache.  Ownership of allocated memory
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ///  is not transferred, so this is a logical error.
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ContentCache(const ContentCache &RHS)
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      : Buffer(0, false), SourceLineCache(0)
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    {
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      OrigEntry = RHS.OrigEntry;
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ContentsEntry = RHS.ContentsEntry;
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      assert (RHS.Buffer.getPointer() == 0 && RHS.SourceLineCache == 0
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              && "Passed ContentCache object cannot own a buffer.");
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NumLines = RHS.NumLines;
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  private:
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Disable assignments.
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ContentCache &operator=(const ContentCache& RHS);
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// FileInfo - Information about a FileID, basically just the logical file
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// that it represents and include stack information.
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// Each FileInfo has include stack information, indicating where it came
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// from.  This information encodes the #include chain that a token was
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// instantiated from.  The main include file has an invalid IncludeLoc.
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// FileInfos contain a "ContentCache *", with the contents of the file.
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class FileInfo {
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// IncludeLoc - The location of the #include that brought in this file.
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// This is an invalid SLOC for the main file (top of the #include chain).
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned IncludeLoc;  // Really a SourceLocation
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// Data - This contains the ContentCache* and the bits indicating the
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// characteristic of the file and whether it has #line info, all bitmangled
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// together.
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    uintptr_t Data;
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  public:
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// get - Return a FileInfo object.
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    static FileInfo get(SourceLocation IL, const ContentCache *Con,
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        CharacteristicKind FileCharacter) {
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      FileInfo X;
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      X.IncludeLoc = IL.getRawEncoding();
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      X.Data = (uintptr_t)Con;
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned");
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      assert((unsigned)FileCharacter < 4 && "invalid file character");
2032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      X.Data |= (unsigned)FileCharacter;
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return X;
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SourceLocation getIncludeLoc() const {
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return SourceLocation::getFromRawEncoding(IncludeLoc);
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const ContentCache* getContentCache() const {
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return reinterpret_cast<const ContentCache*>(Data & ~7UL);
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// getCharacteristic - Return whether this is a system header or not.
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CharacteristicKind getFileCharacteristic() const {
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return (CharacteristicKind)(Data & 3);
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// hasLineDirectives - Return true if this FileID has #line directives in
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// it.
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool hasLineDirectives() const { return (Data & 4) != 0; }
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// setHasLineDirectives - Set the flag that indicates that this FileID has
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// line table entries associated with it.
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    void setHasLineDirectives() {
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      Data |= 4;
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// InstantiationInfo - Each InstantiationInfo encodes the Instantiation
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// location - where the token was ultimately instantiated, and the
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// SpellingLoc - where the actual character data for the token came from.
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class InstantiationInfo {
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     // Really these are all SourceLocations.
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// SpellingLoc - Where the spelling for the token can be found.
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned SpellingLoc;
238f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
239f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    /// InstantiationLocStart/InstantiationLocEnd - In a macro expansion, these
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// indicate the start and end of the instantiation.  In object-like macros,
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// these will be the same.  In a function-like macro instantiation, the
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// start will be the identifier and the end will be the ')'.  Finally, in
243f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    /// macro-argument instantitions, the end will be 'SourceLocation()', an
244f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    /// invalid location.
245f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    unsigned InstantiationLocStart, InstantiationLocEnd;
246f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  public:
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SourceLocation getSpellingLoc() const {
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return SourceLocation::getFromRawEncoding(SpellingLoc);
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SourceLocation getInstantiationLocStart() const {
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return SourceLocation::getFromRawEncoding(InstantiationLocStart);
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SourceLocation getInstantiationLocEnd() const {
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      SourceLocation EndLoc =
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        SourceLocation::getFromRawEncoding(InstantiationLocEnd);
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return EndLoc.isInvalid() ? getInstantiationLocStart() : EndLoc;
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::pair<SourceLocation,SourceLocation> getInstantiationLocRange() const {
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return std::make_pair(getInstantiationLocStart(),
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            getInstantiationLocEnd());
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool isMacroArgInstantiation() const {
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Note that this needs to return false for default constructed objects.
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return getInstantiationLocStart().isValid() &&
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        SourceLocation::getFromRawEncoding(InstantiationLocEnd).isInvalid();
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// create - Return a InstantiationInfo for an expansion. ILStart and
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// ILEnd specify the instantiation range (where the macro is expanded),
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// and SL specifies the spelling location (where the characters from the
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// token come from). All three can refer to normal File SLocs or
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// instantiation locations.
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    static InstantiationInfo create(SourceLocation SL,
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                    SourceLocation ILStart,
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                    SourceLocation ILEnd) {
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      InstantiationInfo X;
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      X.SpellingLoc = SL.getRawEncoding();
281f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      X.InstantiationLocStart = ILStart.getRawEncoding();
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      X.InstantiationLocEnd = ILEnd.getRawEncoding();
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return X;
284c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// createForMacroArg - Return a special InstantiationInfo for the
287f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    /// expansion of a macro argument into a function-like macro's body. IL
288f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    /// specifies the instantiation location (where the macro is expanded).
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// This doesn't need to be a range because a macro is always instantiated
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// at a macro parameter reference, and macro parameters are always exactly
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// one token. SL specifies the spelling location (where the characters
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// from the token come from). IL and SL can both refer to normal File
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// SLocs or instantiation locations.
294f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    ///
295f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    /// Given the code:
296f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    /// \code
2972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ///   #define F(x) f(x)
298f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    ///   F(42);
2992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /// \endcode
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ///
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// When expanding '\c F(42)', the '\c x' would call this with an SL
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// pointing at '\c 42' anad an IL pointing at its location in the
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// definition of '\c F'.
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    static InstantiationInfo createForMacroArg(SourceLocation SL,
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                               SourceLocation IL) {
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // We store an intentionally invalid source location for the end of the
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // instantiation range to mark that this is a macro argument instantation
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // rather than a normal one.
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return create(SL, IL, SourceLocation());
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// SLocEntry - This is a discriminated union of FileInfo and
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// InstantiationInfo.  SourceManager keeps an array of these objects, and
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// they are uniquely identified by the FileID datatype.
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class SLocEntry {
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned Offset;   // low bit is set for instantiation info.
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    union {
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      FileInfo File;
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      InstantiationInfo Instantiation;
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    };
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  public:
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned getOffset() const { return Offset >> 1; }
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool isInstantiation() const { return Offset & 1; }
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool isFile() const { return !isInstantiation(); }
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const FileInfo &getFile() const {
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      assert(isFile() && "Not a file SLocEntry!");
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return File;
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const InstantiationInfo &getInstantiation() const {
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      assert(isInstantiation() && "Not an instantiation SLocEntry!");
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return Instantiation;
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    static SLocEntry get(unsigned Offset, const FileInfo &FI) {
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      SLocEntry E;
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      E.Offset = Offset << 1;
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      E.File = FI;
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return E;
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    static SLocEntry get(unsigned Offset, const InstantiationInfo &II) {
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      SLocEntry E;
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      E.Offset = (Offset << 1) | 1;
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      E.Instantiation = II;
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return E;
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // end SrcMgr namespace.
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// \brief External source of source location entries.
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ExternalSLocEntrySource {
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)public:
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~ExternalSLocEntrySource();
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Read the source location entry with index ID, which will always be
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// less than -1.
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \returns true if an error occurred that prevented the source-location
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// entry from being loaded.
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool ReadSLocEntry(int ID) = 0;
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// IsBeforeInTranslationUnitCache - This class holds the cache used by
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// isBeforeInTranslationUnit.  The cache structure is complex enough to be
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// worth breaking out of SourceManager.
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class IsBeforeInTranslationUnitCache {
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// L/R QueryFID - These are the FID's of the cached query.  If these match up
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// with a subsequent query, the result can be reused.
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FileID LQueryFID, RQueryFID;
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// CommonFID - This is the file found in common between the two #include
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// traces.  It is the nearest common ancestor of the #include tree.
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FileID CommonFID;
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// L/R CommonOffset - This is the offset of the previous query in CommonFID.
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// Usually, this represents the location of the #include for QueryFID, but if
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// LQueryFID is a parent of RQueryFID (or vise versa) then these can be a
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// random token in the parent.
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  unsigned LCommonOffset, RCommonOffset;
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)public:
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// isCacheValid - Return true if the currently cached values match up with
3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// the specified LHS/RHS query.  If not, we can't use the cache.
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool isCacheValid(FileID LHS, FileID RHS) const {
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return LQueryFID == LHS && RQueryFID == RHS;
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// getCachedResult - If the cache is valid, compute the result given the
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// specified offsets in the LHS/RHS FID's.
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool getCachedResult(unsigned LOffset, unsigned ROffset) const {
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // If one of the query files is the common file, use the offset.  Otherwise,
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // use the #include loc in the common file.
3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (LQueryFID != CommonFID) LOffset = LCommonOffset;
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (RQueryFID != CommonFID) ROffset = RCommonOffset;
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return LOffset < ROffset;
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set up a new query.
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void setQueryFIDs(FileID LHS, FileID RHS) {
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LQueryFID = LHS;
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    RQueryFID = RHS;
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void setCommonLoc(FileID commonFID, unsigned lCommonOffset,
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    unsigned rCommonOffset) {
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CommonFID = commonFID;
4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LCommonOffset = lCommonOffset;
4135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    RCommonOffset = rCommonOffset;
4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// \brief This class handles loading and caching of source files into memory.
4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///
4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// This object owns the MemoryBuffer objects for all of the loaded
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// files and assigns unique FileID's for each unique #include chain.
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// The SourceManager can be queried for information about SourceLocation
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// objects, turning them into either spelling or instantiation locations.
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// Spelling locations represent where the bytes corresponding to a token came
4265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// from and instantiation locations represent where the location is in the
4275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// user's view.  In the case of a macro expansion, for example, the spelling
4285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// location indicates  where the expanded token came from and the instantiation
4295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// location specifies where it was expanded.
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class SourceManager : public llvm::RefCountedBase<SourceManager> {
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Diagnostic object.
4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Diagnostic &Diag;
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FileManager &FileMgr;
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mutable llvm::BumpPtrAllocator ContentCacheAlloc;
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// FileInfos - Memoized information about all of the files tracked by this
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// SourceManager.  This set allows us to merge ContentCache entries based
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// on their FileEntry*.  All ContentCache objects will thus have unique,
4415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// non-null, FileEntry pointers.
4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos;
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief True if the ContentCache for files that are overriden by other
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// files, should report the original file name. Defaults to true.
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool OverridenFilesKeepOriginalName;
4475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Files that have been overriden with the contents from another file.
4495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  llvm::DenseMap<const FileEntry *, const FileEntry *> OverriddenFiles;
4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// MemBufferInfos - Information about various memory buffers that we have
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// read in.  All FileEntry* within the stored ContentCache objects are NULL,
4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// as they do not refer to a file.
4545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<SrcMgr::ContentCache*> MemBufferInfos;
4555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief The table of SLocEntries that are local to this module.
4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
4585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// Positive FileIDs are indexes into this table. Entry 0 indicates an invalid
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// instantiation.
4605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<SrcMgr::SLocEntry> LocalSLocEntryTable;
4615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief The table of SLocEntries that are loaded from other modules.
4635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
4645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// Negative FileIDs are indexes into this table. To get from ID to an index,
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// use (-ID - 2).
4665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<SrcMgr::SLocEntry> LoadedSLocEntryTable;
4675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief The starting offset of the next local SLocEntry.
4695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
4705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// This is LocalSLocEntryTable.back().Offset + the size of that entry.
4715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  unsigned NextLocalOffset;
4725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief The starting offset of the latest batch of loaded SLocEntries.
4745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
4755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// This is LoadedSLocEntryTable.back().Offset, except that that entry might
4765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// not have been loaded, so that value would be unknown.
4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  unsigned CurrentLoadedOffset;
4785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief A bitmap that indicates whether the entries of LoadedSLocEntryTable
4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// have already been loaded from the external source.
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
4825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// Same indexing as LoadedSLocEntryTable.
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<bool> SLocEntryLoaded;
4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief An external source for source location entries.
4865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ExternalSLocEntrySource *ExternalSLocEntries;
4875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// LastFileIDLookup - This is a one-entry cache to speed up getFileID.
4895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// LastFileIDLookup records the last FileID looked up or created, because it
4905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// is very common to look up many tokens from the same file.
4915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mutable FileID LastFileIDLookup;
4925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// LineTable - This holds information for #line directives.  It is referenced
4945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// by indices from SLocEntryTable.
4955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LineTableInfo *LineTable;
4965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// LastLineNo - These ivars serve as a cache used in the getLineNumber
4985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// method which is used to speedup getLineNumber calls to nearby locations.
4995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mutable FileID LastLineNoFileIDQuery;
5005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mutable SrcMgr::ContentCache *LastLineNoContentCache;
5015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mutable unsigned LastLineNoFilePos;
5025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mutable unsigned LastLineNoResult;
5032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
504c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// MainFileID - The file ID for the main source file of the translation unit.
5052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FileID MainFileID;
5062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
507c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Statistics for -print-stats.
5082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  mutable unsigned NumLinearScans, NumBinaryProbes;
5092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Cache results for the isBeforeInTranslationUnit method.
5112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  mutable IsBeforeInTranslationUnitCache IsBeforeInTUCache;
5122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Cache for the "fake" buffer used for error-recovery purposes.
5142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  mutable llvm::MemoryBuffer *FakeBufferForRecovery;
5152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // SourceManager doesn't support copy construction.
5172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  explicit SourceManager(const SourceManager&);
5182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void operator=(const SourceManager&);
5192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)public:
5202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  SourceManager(Diagnostic &Diag, FileManager &FileMgr);
521c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ~SourceManager();
5222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void clearIDTables();
524c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Diagnostic &getDiagnostics() const { return Diag; }
5262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FileManager &getFileManager() const { return FileMgr; }
528c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
52990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  /// \brief Set true if the SourceManager should report the original file name
5302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// for contents of files that were overriden by other files.Defaults to true.
5312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void setOverridenFilesKeepOriginalName(bool value) {
5322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    OverridenFilesKeepOriginalName = value;
53390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
53490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
5352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// createMainFileIDForMembuffer - Create the FileID for a memory buffer
5362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ///  that will represent the FileID for the main source.  One example
5372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ///  of when this would be used is when the main source is read from STDIN.
5382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) {
539c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    assert(MainFileID.isInvalid() && "MainFileID already set!");
5402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    MainFileID = createFileIDForMemBuffer(Buffer);
5412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return MainFileID;
5422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
543c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //===--------------------------------------------------------------------===//
5452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // MainFileID creation and querying methods.
5462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //===--------------------------------------------------------------------===//
54790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
5482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// getMainFileID - Returns the FileID of the main source file.
5492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FileID getMainFileID() const { return MainFileID; }
5502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// createMainFileID - Create the FileID for the main source file.
5522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FileID createMainFileID(const FileEntry *SourceFile) {
5532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    assert(MainFileID.isInvalid() && "MainFileID already set!");
5542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    MainFileID = createFileID(SourceFile, SourceLocation(), SrcMgr::C_User);
5552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return MainFileID;
5562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
5572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// \brief Set the file ID for the precompiled preamble, which is also the
5592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// main file.
5605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetPreambleFileID(FileID Preamble) {
5615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    assert(MainFileID.isInvalid() && "MainFileID already set!");
5625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MainFileID = Preamble;
563  }
564
565  //===--------------------------------------------------------------------===//
566  // Methods to create new FileID's and instantiations.
567  //===--------------------------------------------------------------------===//
568
569  /// createFileID - Create a new FileID that represents the specified file
570  /// being #included from the specified IncludePosition.  This translates NULL
571  /// into standard input.
572  FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
573                      SrcMgr::CharacteristicKind FileCharacter,
574                      int LoadedID = 0, unsigned LoadedOffset = 0) {
575    const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile);
576    assert(IR && "getOrCreateContentCache() cannot return NULL");
577    return createFileID(IR, IncludePos, FileCharacter, LoadedID, LoadedOffset);
578  }
579
580  /// createFileIDForMemBuffer - Create a new FileID that represents the
581  /// specified memory buffer.  This does no caching of the buffer and takes
582  /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once.
583  FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer,
584                                  int LoadedID = 0, unsigned LoadedOffset = 0) {
585    return createFileID(createMemBufferContentCache(Buffer), SourceLocation(),
586                        SrcMgr::C_User, LoadedID, LoadedOffset);
587  }
588
589  /// createMacroArgInstantiationLoc - Return a new SourceLocation that encodes
590  /// the fact that a token from SpellingLoc should actually be referenced from
591  /// InstantiationLoc, and that it represents the instantiation of a macro
592  /// argument into the function-like macro body.
593  SourceLocation createMacroArgInstantiationLoc(SourceLocation Loc,
594                                                SourceLocation InstantiationLoc,
595                                                unsigned TokLength);
596
597  /// createInstantiationLoc - Return a new SourceLocation that encodes the fact
598  /// that a token from SpellingLoc should actually be referenced from
599  /// InstantiationLoc.
600  SourceLocation createInstantiationLoc(SourceLocation Loc,
601                                        SourceLocation InstantiationLocStart,
602                                        SourceLocation InstantiationLocEnd,
603                                        unsigned TokLength,
604                                        int LoadedID = 0,
605                                        unsigned LoadedOffset = 0);
606
607  /// \brief Retrieve the memory buffer associated with the given file.
608  ///
609  /// \param Invalid If non-NULL, will be set \c true if an error
610  /// occurs while retrieving the memory buffer.
611  const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File,
612                                                   bool *Invalid = 0);
613
614  /// \brief Override the contents of the given source file by providing an
615  /// already-allocated buffer.
616  ///
617  /// \param SourceFile the source file whose contents will be overriden.
618  ///
619  /// \param Buffer the memory buffer whose contents will be used as the
620  /// data in the given source file.
621  ///
622  /// \param DoNotFree If true, then the buffer will not be freed when the
623  /// source manager is destroyed.
624  void overrideFileContents(const FileEntry *SourceFile,
625                            const llvm::MemoryBuffer *Buffer,
626                            bool DoNotFree = false);
627
628  /// \brief Override the the given source file with another one.
629  ///
630  /// \param SourceFile the source file which will be overriden.
631  ///
632  /// \param NewFile the file whose contents will be used as the
633  /// data instead of the contents of the given source file.
634  void overrideFileContents(const FileEntry *SourceFile,
635                            const FileEntry *NewFile);
636
637  //===--------------------------------------------------------------------===//
638  // FileID manipulation methods.
639  //===--------------------------------------------------------------------===//
640
641  /// getBuffer - Return the buffer for the specified FileID. If there is an
642  /// error opening this buffer the first time, this manufactures a temporary
643  /// buffer and returns a non-empty error string.
644  const llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc,
645                                      bool *Invalid = 0) const {
646    bool MyInvalid = false;
647    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
648    if (MyInvalid || !Entry.isFile()) {
649      if (Invalid)
650        *Invalid = true;
651
652      return getFakeBufferForRecovery();
653    }
654
655    return Entry.getFile().getContentCache()->getBuffer(Diag, *this, Loc,
656                                                        Invalid);
657  }
658
659  const llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = 0) const {
660    bool MyInvalid = false;
661    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
662    if (MyInvalid || !Entry.isFile()) {
663      if (Invalid)
664        *Invalid = true;
665
666      return getFakeBufferForRecovery();
667    }
668
669    return Entry.getFile().getContentCache()->getBuffer(Diag, *this,
670                                                        SourceLocation(),
671                                                        Invalid);
672  }
673
674  /// getFileEntryForID - Returns the FileEntry record for the provided FileID.
675  const FileEntry *getFileEntryForID(FileID FID) const {
676    bool MyInvalid = false;
677    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
678    if (MyInvalid || !Entry.isFile())
679      return 0;
680
681    return Entry.getFile().getContentCache()->OrigEntry;
682  }
683
684  /// Returns the FileEntry record for the provided SLocEntry.
685  const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const
686  {
687    return sloc.getFile().getContentCache()->OrigEntry;
688  }
689
690  /// getBufferData - Return a StringRef to the source buffer data for the
691  /// specified FileID.
692  ///
693  /// \param FID The file ID whose contents will be returned.
694  /// \param Invalid If non-NULL, will be set true if an error occurred.
695  StringRef getBufferData(FileID FID, bool *Invalid = 0) const;
696
697
698  //===--------------------------------------------------------------------===//
699  // SourceLocation manipulation methods.
700  //===--------------------------------------------------------------------===//
701
702  /// getFileID - Return the FileID for a SourceLocation.  This is a very
703  /// hot method that is used for all SourceManager queries that start with a
704  /// SourceLocation object.  It is responsible for finding the entry in
705  /// SLocEntryTable which contains the specified location.
706  ///
707  FileID getFileID(SourceLocation SpellingLoc) const {
708    unsigned SLocOffset = SpellingLoc.getOffset();
709
710    // If our one-entry cache covers this offset, just return it.
711    if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
712      return LastFileIDLookup;
713
714    return getFileIDSlow(SLocOffset);
715  }
716
717  /// getLocForStartOfFile - Return the source location corresponding to the
718  /// first byte of the specified file.
719  SourceLocation getLocForStartOfFile(FileID FID) const {
720    bool Invalid = false;
721    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
722    if (Invalid || !Entry.isFile())
723      return SourceLocation();
724
725    unsigned FileOffset = Entry.getOffset();
726    return SourceLocation::getFileLoc(FileOffset);
727  }
728
729  /// getInstantiationLoc - Given a SourceLocation object, return the
730  /// instantiation location referenced by the ID.
731  SourceLocation getInstantiationLoc(SourceLocation Loc) const {
732    // Handle the non-mapped case inline, defer to out of line code to handle
733    // instantiations.
734    if (Loc.isFileID()) return Loc;
735    return getInstantiationLocSlowCase(Loc);
736  }
737
738  /// getImmediateInstantiationRange - Loc is required to be an instantiation
739  /// location.  Return the start/end of the instantiation information.
740  std::pair<SourceLocation,SourceLocation>
741  getImmediateInstantiationRange(SourceLocation Loc) const;
742
743  /// getInstantiationRange - Given a SourceLocation object, return the
744  /// range of tokens covered by the instantiation in the ultimate file.
745  std::pair<SourceLocation,SourceLocation>
746  getInstantiationRange(SourceLocation Loc) const;
747
748
749  /// getSpellingLoc - Given a SourceLocation object, return the spelling
750  /// location referenced by the ID.  This is the place where the characters
751  /// that make up the lexed token can be found.
752  SourceLocation getSpellingLoc(SourceLocation Loc) const {
753    // Handle the non-mapped case inline, defer to out of line code to handle
754    // instantiations.
755    if (Loc.isFileID()) return Loc;
756    return getSpellingLocSlowCase(Loc);
757  }
758
759  /// getImmediateSpellingLoc - Given a SourceLocation object, return the
760  /// spelling location referenced by the ID.  This is the first level down
761  /// towards the place where the characters that make up the lexed token can be
762  /// found.  This should not generally be used by clients.
763  SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const;
764
765  /// getDecomposedLoc - Decompose the specified location into a raw FileID +
766  /// Offset pair.  The first element is the FileID, the second is the
767  /// offset from the start of the buffer of the location.
768  std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const {
769    FileID FID = getFileID(Loc);
770    return std::make_pair(FID, Loc.getOffset()-getSLocEntry(FID).getOffset());
771  }
772
773  /// getDecomposedInstantiationLoc - Decompose the specified location into a
774  /// raw FileID + Offset pair.  If the location is an instantiation record,
775  /// walk through it until we find the final location instantiated.
776  std::pair<FileID, unsigned>
777  getDecomposedInstantiationLoc(SourceLocation Loc) const {
778    FileID FID = getFileID(Loc);
779    const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
780
781    unsigned Offset = Loc.getOffset()-E->getOffset();
782    if (Loc.isFileID())
783      return std::make_pair(FID, Offset);
784
785    return getDecomposedInstantiationLocSlowCase(E);
786  }
787
788  /// getDecomposedSpellingLoc - Decompose the specified location into a raw
789  /// FileID + Offset pair.  If the location is an instantiation record, walk
790  /// through it until we find its spelling record.
791  std::pair<FileID, unsigned>
792  getDecomposedSpellingLoc(SourceLocation Loc) const {
793    FileID FID = getFileID(Loc);
794    const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
795
796    unsigned Offset = Loc.getOffset()-E->getOffset();
797    if (Loc.isFileID())
798      return std::make_pair(FID, Offset);
799    return getDecomposedSpellingLocSlowCase(E, Offset);
800  }
801
802  /// getFileOffset - This method returns the offset from the start
803  /// of the file that the specified SourceLocation represents. This is not very
804  /// meaningful for a macro ID.
805  unsigned getFileOffset(SourceLocation SpellingLoc) const {
806    return getDecomposedLoc(SpellingLoc).second;
807  }
808
809  /// isMacroArgInstantiation - This method tests whether the given source
810  /// location represents a macro argument's instantiation into the
811  /// function-like macro definition. Such source locations only appear inside
812  /// of the instantiation locations representing where a particular
813  /// function-like macro was expanded.
814  bool isMacroArgInstantiation(SourceLocation Loc) const;
815
816  //===--------------------------------------------------------------------===//
817  // Queries about the code at a SourceLocation.
818  //===--------------------------------------------------------------------===//
819
820  /// getCharacterData - Return a pointer to the start of the specified location
821  /// in the appropriate spelling MemoryBuffer.
822  ///
823  /// \param Invalid If non-NULL, will be set \c true if an error occurs.
824  const char *getCharacterData(SourceLocation SL, bool *Invalid = 0) const;
825
826  /// getColumnNumber - Return the column # for the specified file position.
827  /// This is significantly cheaper to compute than the line number.  This
828  /// returns zero if the column number isn't known.  This may only be called on
829  /// a file sloc, so you must choose a spelling or instantiation location
830  /// before calling this method.
831  unsigned getColumnNumber(FileID FID, unsigned FilePos,
832                           bool *Invalid = 0) const;
833  unsigned getSpellingColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
834  unsigned getInstantiationColumnNumber(SourceLocation Loc,
835                                        bool *Invalid = 0) const;
836  unsigned getPresumedColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
837
838
839  /// getLineNumber - Given a SourceLocation, return the spelling line number
840  /// for the position indicated.  This requires building and caching a table of
841  /// line offsets for the MemoryBuffer, so this is not cheap: use only when
842  /// about to emit a diagnostic.
843  unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = 0) const;
844  unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
845  unsigned getInstantiationLineNumber(SourceLocation Loc,
846                                      bool *Invalid = 0) const;
847  unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
848
849  /// Return the filename or buffer identifier of the buffer the location is in.
850  /// Note that this name does not respect #line directives.  Use getPresumedLoc
851  /// for normal clients.
852  const char *getBufferName(SourceLocation Loc, bool *Invalid = 0) const;
853
854  /// getFileCharacteristic - return the file characteristic of the specified
855  /// source location, indicating whether this is a normal file, a system
856  /// header, or an "implicit extern C" system header.
857  ///
858  /// This state can be modified with flags on GNU linemarker directives like:
859  ///   # 4 "foo.h" 3
860  /// which changes all source locations in the current file after that to be
861  /// considered to be from a system header.
862  SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const;
863
864  /// getPresumedLoc - This method returns the "presumed" location of a
865  /// SourceLocation specifies.  A "presumed location" can be modified by #line
866  /// or GNU line marker directives.  This provides a view on the data that a
867  /// user should see in diagnostics, for example.
868  ///
869  /// Note that a presumed location is always given as the instantiation point
870  /// of an instantiation location, not at the spelling location.
871  ///
872  /// \returns The presumed location of the specified SourceLocation. If the
873  /// presumed location cannot be calculate (e.g., because \p Loc is invalid
874  /// or the file containing \p Loc has changed on disk), returns an invalid
875  /// presumed location.
876  PresumedLoc getPresumedLoc(SourceLocation Loc) const;
877
878  /// isFromSameFile - Returns true if both SourceLocations correspond to
879  ///  the same file.
880  bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
881    return getFileID(Loc1) == getFileID(Loc2);
882  }
883
884  /// isFromMainFile - Returns true if the file of provided SourceLocation is
885  ///   the main file.
886  bool isFromMainFile(SourceLocation Loc) const {
887    return getFileID(Loc) == getMainFileID();
888  }
889
890  /// isInSystemHeader - Returns if a SourceLocation is in a system header.
891  bool isInSystemHeader(SourceLocation Loc) const {
892    return getFileCharacteristic(Loc) != SrcMgr::C_User;
893  }
894
895  /// isInExternCSystemHeader - Returns if a SourceLocation is in an "extern C"
896  /// system header.
897  bool isInExternCSystemHeader(SourceLocation Loc) const {
898    return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem;
899  }
900
901  /// \brief Given a specific chunk of a FileID (FileID with offset+length),
902  /// returns true if \arg Loc is inside that chunk and sets relative offset
903  /// (offset of \arg Loc from beginning of chunk) to \arg relativeOffset.
904  bool isInFileID(SourceLocation Loc,
905                  FileID FID, unsigned offset, unsigned length,
906                  unsigned *relativeOffset = 0) const {
907    assert(!FID.isInvalid());
908    if (Loc.isInvalid())
909      return false;
910
911    unsigned start = getSLocEntry(FID).getOffset() + offset;
912    unsigned end = start + length;
913
914#ifndef NDEBUG
915    // Make sure offset/length describe a chunk inside the given FileID.
916    unsigned NextOffset;
917    if (FID.ID == -2)
918      NextOffset = 1U << 31U;
919    else if (FID.ID+1 == (int)LocalSLocEntryTable.size())
920      NextOffset = getNextLocalOffset();
921    else
922      NextOffset = getSLocEntryByID(FID.ID+1).getOffset();
923    assert(start < NextOffset);
924    assert(end   < NextOffset);
925#endif
926
927    if (Loc.getOffset() >= start && Loc.getOffset() < end) {
928      if (relativeOffset)
929        *relativeOffset = Loc.getOffset() - start;
930      return true;
931    }
932
933    return false;
934  }
935
936  //===--------------------------------------------------------------------===//
937  // Line Table Manipulation Routines
938  //===--------------------------------------------------------------------===//
939
940  /// getLineTableFilenameID - Return the uniqued ID for the specified filename.
941  ///
942  unsigned getLineTableFilenameID(StringRef Str);
943
944  /// AddLineNote - Add a line note to the line table for the FileID and offset
945  /// specified by Loc.  If FilenameID is -1, it is considered to be
946  /// unspecified.
947  void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID);
948  void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID,
949                   bool IsFileEntry, bool IsFileExit,
950                   bool IsSystemHeader, bool IsExternCHeader);
951
952  /// \brief Determine if the source manager has a line table.
953  bool hasLineTable() const { return LineTable != 0; }
954
955  /// \brief Retrieve the stored line table.
956  LineTableInfo &getLineTable();
957
958  //===--------------------------------------------------------------------===//
959  // Queries for performance analysis.
960  //===--------------------------------------------------------------------===//
961
962  /// Return the total amount of physical memory allocated by the
963  /// ContentCache allocator.
964  size_t getContentCacheSize() const {
965    return ContentCacheAlloc.getTotalMemory();
966  }
967
968  struct MemoryBufferSizes {
969    const size_t malloc_bytes;
970    const size_t mmap_bytes;
971
972    MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)
973      : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {}
974  };
975
976  /// Return the amount of memory used by memory buffers, breaking down
977  /// by heap-backed versus mmap'ed memory.
978  MemoryBufferSizes getMemoryBufferSizes() const;
979
980  //===--------------------------------------------------------------------===//
981  // Other miscellaneous methods.
982  //===--------------------------------------------------------------------===//
983
984  /// \brief Get the source location for the given file:line:col triplet.
985  ///
986  /// If the source file is included multiple times, the source location will
987  /// be based upon the first inclusion.
988  SourceLocation getLocation(const FileEntry *SourceFile,
989                             unsigned Line, unsigned Col);
990
991  /// \brief Determines the order of 2 source locations in the translation unit.
992  ///
993  /// \returns true if LHS source location comes before RHS, false otherwise.
994  bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const;
995
996  /// \brief Determines the order of 2 source locations in the "source location
997  /// address space".
998  bool isBeforeInSourceLocationOffset(SourceLocation LHS,
999                                      SourceLocation RHS) const {
1000    return isBeforeInSourceLocationOffset(LHS, RHS.getOffset());
1001  }
1002
1003  /// \brief Determines the order of a source location and a source location
1004  /// offset in the "source location address space".
1005  ///
1006  /// Note that we always consider source locations loaded from
1007  bool isBeforeInSourceLocationOffset(SourceLocation LHS, unsigned RHS) const {
1008    unsigned LHSOffset = LHS.getOffset();
1009    bool LHSLoaded = LHSOffset >= CurrentLoadedOffset;
1010    bool RHSLoaded = RHS >= CurrentLoadedOffset;
1011    if (LHSLoaded == RHSLoaded)
1012      return LHS.getOffset() < RHS;
1013
1014    return LHSLoaded;
1015  }
1016
1017  // Iterators over FileInfos.
1018  typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>
1019      ::const_iterator fileinfo_iterator;
1020  fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); }
1021  fileinfo_iterator fileinfo_end() const { return FileInfos.end(); }
1022  bool hasFileInfo(const FileEntry *File) const {
1023    return FileInfos.find(File) != FileInfos.end();
1024  }
1025
1026  /// PrintStats - Print statistics to stderr.
1027  ///
1028  void PrintStats() const;
1029
1030  /// \brief Get the number of local SLocEntries we have.
1031  unsigned local_sloc_entry_size() const { return LocalSLocEntryTable.size(); }
1032
1033  /// \brief Get a local SLocEntry. This is exposed for indexing.
1034  const SrcMgr::SLocEntry &getLocalSLocEntry(unsigned Index,
1035                                             bool *Invalid = 0) const {
1036    assert(Index < LocalSLocEntryTable.size() && "Invalid index");
1037    return LocalSLocEntryTable[Index];
1038  }
1039
1040  /// \brief Get the number of loaded SLocEntries we have.
1041  unsigned loaded_sloc_entry_size() const { return LoadedSLocEntryTable.size();}
1042
1043  /// \brief Get a loaded SLocEntry. This is exposed for indexing.
1044  const SrcMgr::SLocEntry &getLoadedSLocEntry(unsigned Index, bool *Invalid=0) const {
1045    assert(Index < LoadedSLocEntryTable.size() && "Invalid index");
1046    if (!SLocEntryLoaded[Index])
1047      ExternalSLocEntries->ReadSLocEntry(-(static_cast<int>(Index) + 2));
1048    return LoadedSLocEntryTable[Index];
1049  }
1050
1051  const SrcMgr::SLocEntry &getSLocEntry(FileID FID, bool *Invalid = 0) const {
1052    return getSLocEntryByID(FID.ID);
1053  }
1054
1055  unsigned getNextLocalOffset() const { return NextLocalOffset; }
1056
1057  void setExternalSLocEntrySource(ExternalSLocEntrySource *Source) {
1058    assert(LoadedSLocEntryTable.empty() &&
1059           "Invalidating existing loaded entries");
1060    ExternalSLocEntries = Source;
1061  }
1062
1063  /// \brief Allocate a number of loaded SLocEntries, which will be actually
1064  /// loaded on demand from the external source.
1065  ///
1066  /// NumSLocEntries will be allocated, which occupy a total of TotalSize space
1067  /// in the global source view. The lowest ID and the base offset of the
1068  /// entries will be returned.
1069  std::pair<int, unsigned>
1070  AllocateLoadedSLocEntries(unsigned NumSLocEntries, unsigned TotalSize);
1071
1072private:
1073  const llvm::MemoryBuffer *getFakeBufferForRecovery() const;
1074
1075  /// \brief Get the entry with the given unwrapped FileID.
1076  const SrcMgr::SLocEntry &getSLocEntryByID(int ID) const {
1077    assert(ID != -1 && "Using FileID sentinel value");
1078    if (ID < 0)
1079      return getLoadedSLocEntryByID(ID);
1080    return getLocalSLocEntry(static_cast<unsigned>(ID));
1081  }
1082
1083  const SrcMgr::SLocEntry &getLoadedSLocEntryByID(int ID) const {
1084    return getLoadedSLocEntry(static_cast<unsigned>(-ID - 2));
1085  }
1086
1087  /// createInstantiationLoc - Implements the common elements of storing an
1088  /// instantiation info struct into the SLocEntry table and producing a source
1089  /// location that refers to it.
1090  SourceLocation createInstantiationLocImpl(const SrcMgr::InstantiationInfo &II,
1091                                            unsigned TokLength,
1092                                            int LoadedID = 0,
1093                                            unsigned LoadedOffset = 0);
1094
1095  /// isOffsetInFileID - Return true if the specified FileID contains the
1096  /// specified SourceLocation offset.  This is a very hot method.
1097  inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const {
1098    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID);
1099    // If the entry is after the offset, it can't contain it.
1100    if (SLocOffset < Entry.getOffset()) return false;
1101
1102    // If this is the very last entry then it does.
1103    if (FID.ID == -2)
1104      return true;
1105
1106    // If it is the last local entry, then it does if the location is local.
1107    if (static_cast<unsigned>(FID.ID+1) == LocalSLocEntryTable.size()) {
1108      return SLocOffset < NextLocalOffset;
1109    }
1110
1111    // Otherwise, the entry after it has to not include it. This works for both
1112    // local and loaded entries.
1113    return SLocOffset < getSLocEntry(FileID::get(FID.ID+1)).getOffset();
1114  }
1115
1116  /// createFileID - Create a new fileID for the specified ContentCache and
1117  ///  include position.  This works regardless of whether the ContentCache
1118  ///  corresponds to a file or some other input source.
1119  FileID createFileID(const SrcMgr::ContentCache* File,
1120                      SourceLocation IncludePos,
1121                      SrcMgr::CharacteristicKind DirCharacter,
1122                      int LoadedID, unsigned LoadedOffset);
1123
1124  const SrcMgr::ContentCache *
1125    getOrCreateContentCache(const FileEntry *SourceFile);
1126
1127  /// createMemBufferContentCache - Create a new ContentCache for the specified
1128  ///  memory buffer.
1129  const SrcMgr::ContentCache*
1130  createMemBufferContentCache(const llvm::MemoryBuffer *Buf);
1131
1132  FileID getFileIDSlow(unsigned SLocOffset) const;
1133  FileID getFileIDLocal(unsigned SLocOffset) const;
1134  FileID getFileIDLoaded(unsigned SLocOffset) const;
1135
1136  SourceLocation getInstantiationLocSlowCase(SourceLocation Loc) const;
1137  SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;
1138
1139  std::pair<FileID, unsigned>
1140  getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E) const;
1141  std::pair<FileID, unsigned>
1142  getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
1143                                   unsigned Offset) const;
1144};
1145
1146
1147}  // end namespace clang
1148
1149#endif
1150