SourceManager.h revision d54dff026b02303a35147224de72bb44cbb53c79
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)//
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===//
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//  This file defines the SourceManager interface.
113551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)//
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===//
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef LLVM_CLANG_SOURCEMANAGER_H
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LLVM_CLANG_SOURCEMANAGER_H
163551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/Basic/LLVM.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/Basic/SourceLocation.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Support/Allocator.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Support/DataTypes.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/ADT/PointerIntPair.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/ADT/PointerUnion.h"
23ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "llvm/ADT/IntrusiveRefCntPtr.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/ADT/OwningPtr.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/ADT/DenseMap.h"
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/ADT/DenseSet.h"
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Support/MemoryBuffer.h"
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <map>
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <cassert>
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace clang {
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class DiagnosticsEngine;
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)class ASTWriter;
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ASTReader;
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// There are three different types of locations in a file: a spelling
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// location, an expansion location, and a presumed location.
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// Given an example of:
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// #define min(x, y) x < y ? x : y
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///
49ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch/// and then later on a use of min:
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// #line 17
51ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch/// return min(a, b);
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// The expansion location is the line in the source code where the macro
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// was expanded (the return statement), the spelling location is the
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// location in the source where the macro was originally defined,
56ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch/// and the presumed location is where the line directive states that
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// the line is 17, or any other line.
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// SrcMgr - Public enums and private classes that are part of the
60ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch/// SourceManager implementation.
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace SrcMgr {
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// CharacteristicKind - This is used to represent whether a file or directory
64ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  /// holds normal user code, system code, or system code which is implicitly
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// 'extern "C"' in C++ mode.  Entire directories can be tagged with this
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// (this is maintained by DirectoryLookup and friends) as can specific
67ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  /// FileInfos when a #pragma system_header is seen or various other cases.
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
69ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  enum CharacteristicKind {
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    C_User, C_System, C_ExternCSystem
71ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  };
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// ContentCache - One instance of this struct is kept for every file
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// loaded or used.  This object owns the MemoryBuffer object.
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class ContentCache {
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    enum CCFlags {
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      /// \brief Whether the buffer is invalid.
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      InvalidFlag = 0x01,
79ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      /// \brief Whether the buffer should not be freed on destruction.
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      DoNotFreeFlag = 0x02
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    };
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// Buffer - The actual buffer containing the characters from the input
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// file.  This is owned by the ContentCache object.
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// The bits indicate indicates whether the buffer is invalid.
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    mutable llvm::PointerIntPair<const llvm::MemoryBuffer *, 2> Buffer;
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  public:
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// Reference to the file entry representing this ContentCache.
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// This reference does not own the FileEntry object.
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// It is possible for this to be NULL if
92ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    /// the ContentCache encapsulates an imaginary text buffer.
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const FileEntry *OrigEntry;
94ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// \brief References the file which the contents were actually loaded from.
96ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    /// Can be different from 'Entry' if we overridden the contents of one file
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// with the contents of another file.
98ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    const FileEntry *ContentsEntry;
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// SourceLineCache - A bump pointer allocated array of offsets for each
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// source line.  This is lazily computed.  This is owned by the
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// SourceManager BumpPointerAllocator object.
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned *SourceLineCache;
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// NumLines - The number of lines in this ContentCache.  This is only valid
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// if SourceLineCache is non-null.
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned NumLines : 31;
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// \brief Indicates whether the buffer itself was provided to override
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// the actual file contents.
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ///
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// When true, the original entry may be a virtual file that does not
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// exist.
114ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    unsigned BufferOverridden : 1;
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ContentCache(const FileEntry *Ent = 0)
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      : Buffer(0, false), OrigEntry(Ent), ContentsEntry(Ent),
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        SourceLineCache(0), NumLines(0), BufferOverridden(false) {}
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ContentCache(const FileEntry *Ent, const FileEntry *contentEnt)
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      : Buffer(0, false), OrigEntry(Ent), ContentsEntry(contentEnt),
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        SourceLineCache(0), NumLines(0), BufferOverridden(false) {}
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ~ContentCache();
125ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
126ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    /// The copy ctor does not allow copies where source object has either
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ///  a non-NULL Buffer or SourceLineCache.  Ownership of allocated memory
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ///  is not transferred, so this is a logical error.
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ContentCache(const ContentCache &RHS)
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      : Buffer(0, false), SourceLineCache(0), BufferOverridden(false)
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    {
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      OrigEntry = RHS.OrigEntry;
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ContentsEntry = RHS.ContentsEntry;
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      assert (RHS.Buffer.getPointer() == 0 && RHS.SourceLineCache == 0 &&
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              "Passed ContentCache object cannot own a buffer.");
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
138ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      NumLines = RHS.NumLines;
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// getBuffer - Returns the memory buffer for the associated content.
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ///
143ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    /// \param Diag Object through which diagnostics will be emitted if the
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// buffer cannot be retrieved.
145ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    ///
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// \param Loc If specified, is the location that invalid file diagnostics
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ///     will be emitted at.
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ///
149ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    /// \param Invalid If non-NULL, will be set \c true if an error occurred.
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const llvm::MemoryBuffer *getBuffer(DiagnosticsEngine &Diag,
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        const SourceManager &SM,
152ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                        SourceLocation Loc = SourceLocation(),
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        bool *Invalid = 0) const;
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// getSize - Returns the size of the content encapsulated by this
156ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    ///  ContentCache. This can be the size of the source file or the size of an
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ///  arbitrary scratch buffer.  If the ContentCache encapsulates a source
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ///  file this size is retrieved from the file's FileEntry.
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned getSize() const;
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// getSizeBytesMapped - Returns the number of bytes actually mapped for
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// this ContentCache. This can be 0 if the MemBuffer was not actually
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// expanded.
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned getSizeBytesMapped() const;
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
166ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    /// Returns the kind of memory used to back the memory buffer for
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// this content cache.  This is used for performance analysis.
168ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    llvm::MemoryBuffer::BufferKind getMemoryBufferKind() const;
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    void setBuffer(const llvm::MemoryBuffer *B) {
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      assert(!Buffer.getPointer() && "MemoryBuffer already set.");
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      Buffer.setPointer(B);
173ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      Buffer.setInt(false);
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// \brief Get the underlying buffer, returning NULL if the buffer is not
177ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    /// yet available.
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const llvm::MemoryBuffer *getRawBuffer() const {
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return Buffer.getPointer();
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
181ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// \brief Replace the existing buffer (which will be deleted)
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// with the given buffer.
184ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    void replaceBuffer(const llvm::MemoryBuffer *B, bool DoNotFree = false);
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
186ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    /// \brief Determine whether the buffer itself is invalid.
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool isBufferInvalid() const {
188ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      return Buffer.getInt() & InvalidFlag;
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// \brief Determine whether the buffer should be freed.
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool shouldFreeBuffer() const {
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return (Buffer.getInt() & DoNotFreeFlag) == 0;
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
196ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  private:
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Disable assignments.
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ContentCache &operator=(const ContentCache& RHS);
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// FileInfo - Information about a FileID, basically just the logical file
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// that it represents and include stack information.
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// Each FileInfo has include stack information, indicating where it came
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// from. This information encodes the #include chain that a token was
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// expanded from. The main include file has an invalid IncludeLoc.
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// FileInfos contain a "ContentCache *", with the contents of the file.
209ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  ///
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class FileInfo {
211ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    /// IncludeLoc - The location of the #include that brought in this file.
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// This is an invalid SLOC for the main file (top of the #include chain).
213ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    unsigned IncludeLoc;  // Really a SourceLocation
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    /// \brief Number of FileIDs (files and macros) that were created during
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// preprocessing of this #include, including this SLocEntry.
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// Zero means the preprocessor didn't provide such info for this SLocEntry.
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned NumCreatedFIDs;
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// Data - This contains the ContentCache* and the bits indicating the
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// characteristic of the file and whether it has #line info, all bitmangled
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// together.
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    uintptr_t Data;
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    friend class clang::SourceManager;
226ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    friend class clang::ASTWriter;
227ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    friend class clang::ASTReader;
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  public:
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// get - Return a FileInfo object.
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    static FileInfo get(SourceLocation IL, const ContentCache *Con,
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        CharacteristicKind FileCharacter) {
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      FileInfo X;
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      X.IncludeLoc = IL.getRawEncoding();
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      X.NumCreatedFIDs = 0;
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      X.Data = (uintptr_t)Con;
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned");
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      assert((unsigned)FileCharacter < 4 && "invalid file character");
238ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      X.Data |= (unsigned)FileCharacter;
239ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      return X;
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SourceLocation getIncludeLoc() const {
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return SourceLocation::getFromRawEncoding(IncludeLoc);
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const ContentCache* getContentCache() const {
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return reinterpret_cast<const ContentCache*>(Data & ~7UL);
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// getCharacteristic - Return whether this is a system header or not.
250ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    CharacteristicKind getFileCharacteristic() const {
251ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      return (CharacteristicKind)(Data & 3);
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// hasLineDirectives - Return true if this FileID has #line directives in
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// it.
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool hasLineDirectives() const { return (Data & 4) != 0; }
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// setHasLineDirectives - Set the flag that indicates that this FileID has
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// line table entries associated with it.
260ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    void setHasLineDirectives() {
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      Data |= 4;
262ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    }
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// ExpansionInfo - Each ExpansionInfo encodes the expansion location - where
266ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  /// the token was ultimately expanded, and the SpellingLoc - where the actual
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// character data for the token came from.
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class ExpansionInfo {
269ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    // Really these are all SourceLocations.
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// SpellingLoc - Where the spelling for the token can be found.
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned SpellingLoc;
273ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// ExpansionLocStart/ExpansionLocEnd - In a macro expansion, these
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// indicate the start and end of the expansion. In object-like macros,
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// these will be the same. In a function-like macro expansion, the start
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// will be the identifier and the end will be the ')'. Finally, in
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// macro-argument instantitions, the end will be 'SourceLocation()', an
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// invalid location.
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned ExpansionLocStart, ExpansionLocEnd;
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  public:
283ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    SourceLocation getSpellingLoc() const {
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return SourceLocation::getFromRawEncoding(SpellingLoc);
285ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    }
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SourceLocation getExpansionLocStart() const {
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return SourceLocation::getFromRawEncoding(ExpansionLocStart);
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SourceLocation getExpansionLocEnd() const {
290ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      SourceLocation EndLoc =
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        SourceLocation::getFromRawEncoding(ExpansionLocEnd);
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return EndLoc.isInvalid() ? getExpansionLocStart() : EndLoc;
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
294ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::pair<SourceLocation,SourceLocation> getExpansionLocRange() const {
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return std::make_pair(getExpansionLocStart(), getExpansionLocEnd());
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
298ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool isMacroArgExpansion() const {
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Note that this needs to return false for default constructed objects.
301ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      return getExpansionLocStart().isValid() &&
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        SourceLocation::getFromRawEncoding(ExpansionLocEnd).isInvalid();
303ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    }
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
305ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    bool isFunctionMacroExpansion() const {
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return getExpansionLocStart().isValid() &&
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          getExpansionLocStart() != getExpansionLocEnd();
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// create - Return a ExpansionInfo for an expansion. Start and End specify
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// the expansion range (where the macro is expanded), and SpellingLoc
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// specifies the spelling location (where the characters from the token
313ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    /// come from). All three can refer to normal File SLocs or expansion
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// locations.
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    static ExpansionInfo create(SourceLocation SpellingLoc,
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                SourceLocation Start, SourceLocation End) {
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ExpansionInfo X;
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      X.SpellingLoc = SpellingLoc.getRawEncoding();
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      X.ExpansionLocStart = Start.getRawEncoding();
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      X.ExpansionLocEnd = End.getRawEncoding();
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return X;
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// createForMacroArg - Return a special ExpansionInfo for the expansion of
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// a macro argument into a function-like macro's body. ExpansionLoc
326ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    /// specifies the expansion location (where the macro is expanded). This
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// doesn't need to be a range because a macro is always expanded at
328ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    /// a macro parameter reference, and macro parameters are always exactly
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// one token. SpellingLoc specifies the spelling location (where the
330ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    /// characters from the token come from). ExpansionLoc and SpellingLoc can
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// both refer to normal File SLocs or expansion locations.
332ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    ///
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// Given the code:
334ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    /// \code
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ///   #define F(x) f(x)
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ///   F(42);
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// \endcode
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ///
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// When expanding '\c F(42)', the '\c x' would call this with an
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// SpellingLoc pointing at '\c 42' anad an ExpansionLoc pointing at its
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// location in the definition of '\c F'.
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    static ExpansionInfo createForMacroArg(SourceLocation SpellingLoc,
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                           SourceLocation ExpansionLoc) {
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // We store an intentionally invalid source location for the end of the
345ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      // expansion range to mark that this is a macro argument ion rather than
346ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      // a normal one.
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return create(SpellingLoc, ExpansionLoc, SourceLocation());
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// SLocEntry - This is a discriminated union of FileInfo and
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// ExpansionInfo.  SourceManager keeps an array of these objects, and
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// they are uniquely identified by the FileID datatype.
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class SLocEntry {
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned Offset;   // low bit is set for expansion info.
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    union {
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      FileInfo File;
358ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      ExpansionInfo Expansion;
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    };
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  public:
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned getOffset() const { return Offset >> 1; }
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
363ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    bool isExpansion() const { return Offset & 1; }
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool isFile() const { return !isExpansion(); }
365ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const FileInfo &getFile() const {
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      assert(isFile() && "Not a file SLocEntry!");
368ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      return File;
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
371ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    const ExpansionInfo &getExpansion() const {
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      assert(isExpansion() && "Not a macro expansion SLocEntry!");
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return Expansion;
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
375ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    static SLocEntry get(unsigned Offset, const FileInfo &FI) {
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      SLocEntry E;
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      E.Offset = Offset << 1;
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      E.File = FI;
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return E;
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    static SLocEntry get(unsigned Offset, const ExpansionInfo &Expansion) {
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      SLocEntry E;
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      E.Offset = (Offset << 1) | 1;
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      E.Expansion = Expansion;
3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return E;
3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // end SrcMgr namespace.
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// \brief External source of source location entries.
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ExternalSLocEntrySource {
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)public:
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~ExternalSLocEntrySource();
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Read the source location entry with index ID, which will always be
3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// less than -1.
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \returns true if an error occurred that prevented the source-location
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// entry from being loaded.
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool ReadSLocEntry(int ID) = 0;
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// IsBeforeInTranslationUnitCache - This class holds the cache used by
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// isBeforeInTranslationUnit.  The cache structure is complex enough to be
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// worth breaking out of SourceManager.
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class IsBeforeInTranslationUnitCache {
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// L/R QueryFID - These are the FID's of the cached query.  If these match up
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// with a subsequent query, the result can be reused.
4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FileID LQueryFID, RQueryFID;
4135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief True if LQueryFID was created before RQueryFID. This is used
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// to compare macro expansion locations.
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsLQFIDBeforeRQFID;
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// CommonFID - This is the file found in common between the two #include
4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// traces.  It is the nearest common ancestor of the #include tree.
4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FileID CommonFID;
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// L/R CommonOffset - This is the offset of the previous query in CommonFID.
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// Usually, this represents the location of the #include for QueryFID, but if
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// LQueryFID is a parent of RQueryFID (or vise versa) then these can be a
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// random token in the parent.
4265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  unsigned LCommonOffset, RCommonOffset;
4275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)public:
4285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// isCacheValid - Return true if the currently cached values match up with
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// the specified LHS/RHS query.  If not, we can't use the cache.
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool isCacheValid(FileID LHS, FileID RHS) const {
4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return LQueryFID == LHS && RQueryFID == RHS;
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// getCachedResult - If the cache is valid, compute the result given the
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// specified offsets in the LHS/RHS FID's.
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool getCachedResult(unsigned LOffset, unsigned ROffset) const {
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // If one of the query files is the common file, use the offset.  Otherwise,
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // use the #include loc in the common file.
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (LQueryFID != CommonFID) LOffset = LCommonOffset;
4415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (RQueryFID != CommonFID) ROffset = RCommonOffset;
4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // It is common for multiple macro expansions to be "included" from the same
4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // location (expansion location), in which case use the order of the FileIDs
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // to determine which came first. This will also take care the case where
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // one of the locations points at the inclusion/expansion point of the other
4475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // in which case its FileID will come before the other.
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (LOffset == ROffset)
4495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return IsLQFIDBeforeRQFID;
4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return LOffset < ROffset;
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set up a new query.
4555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void setQueryFIDs(FileID LHS, FileID RHS, bool isLFIDBeforeRFID) {
4565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    assert(LHS != RHS);
4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LQueryFID = LHS;
4585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    RQueryFID = RHS;
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    IsLQFIDBeforeRQFID = isLFIDBeforeRFID;
4605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void clear() {
4635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LQueryFID = RQueryFID = FileID();
4645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    IsLQFIDBeforeRQFID = false;
4655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
4665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void setCommonLoc(FileID commonFID, unsigned lCommonOffset,
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    unsigned rCommonOffset) {
4695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CommonFID = commonFID;
4705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LCommonOffset = lCommonOffset;
4715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    RCommonOffset = rCommonOffset;
4725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
4735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
4755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// \brief This class handles loading and caching of source files into memory.
4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///
4785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// This object owns the MemoryBuffer objects for all of the loaded
4795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// files and assigns unique FileID's for each unique #include chain.
4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// The SourceManager can be queried for information about SourceLocation
4825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// objects, turning them into either spelling or expansion locations. Spelling
4835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// locations represent where the bytes corresponding to a token came from and
4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// expansion locations represent where the location is in the user's view. In
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// the case of a macro expansion, for example, the spelling location indicates
4865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// where the expanded token came from and the expansion location specifies
487ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch/// where it was expanded.
4885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class SourceManager : public RefCountedBase<SourceManager> {
489ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  /// \brief DiagnosticsEngine object.
490ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  DiagnosticsEngine &Diag;
491ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
492ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  FileManager &FileMgr;
493ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
494ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  mutable llvm::BumpPtrAllocator ContentCacheAlloc;
495ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
4965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  /// FileInfos - Memoized information about all of the files tracked by this
497ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  /// SourceManager.  This set allows us to merge ContentCache entries based
498ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  /// on their FileEntry*.  All ContentCache objects will thus have unique,
4995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// non-null, FileEntry pointers.
5005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos;
5015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief True if the ContentCache for files that are overriden by other
5035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// files, should report the original file name. Defaults to true.
5045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool OverridenFilesKeepOriginalName;
5055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  struct OverriddenFilesInfoTy {
5075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// \brief Files that have been overriden with the contents from another
5085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// file.
5095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    llvm::DenseMap<const FileEntry *, const FileEntry *> OverriddenFiles;
5105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// \brief Files that were overridden with a memory buffer.
5115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    llvm::DenseSet<const FileEntry *> OverriddenFilesWithBuffer;
5125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
5135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Lazily create the object keeping overridden files info, since
5155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// it is uncommonly used.
5165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  OwningPtr<OverriddenFilesInfoTy> OverriddenFilesInfo;
5175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  OverriddenFilesInfoTy &getOverriddenFilesInfo() {
5195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!OverriddenFilesInfo)
5205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      OverriddenFilesInfo.reset(new OverriddenFilesInfoTy);
5215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return *OverriddenFilesInfo;
5225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// MemBufferInfos - Information about various memory buffers that we have
5255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// read in.  All FileEntry* within the stored ContentCache objects are NULL,
5265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// as they do not refer to a file.
5275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<SrcMgr::ContentCache*> MemBufferInfos;
5285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief The table of SLocEntries that are local to this module.
5305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
5315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// Positive FileIDs are indexes into this table. Entry 0 indicates an invalid
5325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// expansion.
5335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<SrcMgr::SLocEntry> LocalSLocEntryTable;
5345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief The table of SLocEntries that are loaded from other modules.
5365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
5375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// Negative FileIDs are indexes into this table. To get from ID to an index,
5385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// use (-ID - 2).
5395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mutable std::vector<SrcMgr::SLocEntry> LoadedSLocEntryTable;
5405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief The starting offset of the next local SLocEntry.
5425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
5435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// This is LocalSLocEntryTable.back().Offset + the size of that entry.
5445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  unsigned NextLocalOffset;
5455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief The starting offset of the latest batch of loaded SLocEntries.
5475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
5485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// This is LoadedSLocEntryTable.back().Offset, except that that entry might
5495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// not have been loaded, so that value would be unknown.
5505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  unsigned CurrentLoadedOffset;
5515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief The highest possible offset is 2^31-1, so CurrentLoadedOffset
5535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// starts at 2^31.
5545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static const unsigned MaxLoadedOffset = 1U << 31U;
5555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief A bitmap that indicates whether the entries of LoadedSLocEntryTable
5575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  /// have already been loaded from the external source.
5585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
5595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// Same indexing as LoadedSLocEntryTable.
5605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<bool> SLocEntryLoaded;
5615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief An external source for source location entries.
5635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ExternalSLocEntrySource *ExternalSLocEntries;
5645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// LastFileIDLookup - This is a one-entry cache to speed up getFileID.
5665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// LastFileIDLookup records the last FileID looked up or created, because it
5675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// is very common to look up many tokens from the same file.
5685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mutable FileID LastFileIDLookup;
5695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// LineTable - This holds information for #line directives.  It is referenced
5715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  /// by indices from SLocEntryTable.
5725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LineTableInfo *LineTable;
5735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// LastLineNo - These ivars serve as a cache used in the getLineNumber
5755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// method which is used to speedup getLineNumber calls to nearby locations.
5765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mutable FileID LastLineNoFileIDQuery;
5775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mutable SrcMgr::ContentCache *LastLineNoContentCache;
5785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  mutable unsigned LastLineNoFilePos;
5795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mutable unsigned LastLineNoResult;
5805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// MainFileID - The file ID for the main source file of the translation unit.
5825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FileID MainFileID;
5835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief The file ID for the precompiled preamble there is one.
5855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FileID PreambleFileID;
586ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
5875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Statistics for -print-stats.
588ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  mutable unsigned NumLinearScans, NumBinaryProbes;
589ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
590ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // Cache results for the isBeforeInTranslationUnit method.
591ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  mutable IsBeforeInTranslationUnitCache IsBeforeInTUCache;
592ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
593ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // Cache for the "fake" buffer used for error-recovery purposes.
594ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  mutable llvm::MemoryBuffer *FakeBufferForRecovery;
5955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
596ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  mutable SrcMgr::ContentCache *FakeContentCacheForRecovery;
597ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
5985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Lazily computed map of macro argument chunks to their expanded
5995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// source location.
6005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef std::map<unsigned, SourceLocation> MacroArgsMap;
6015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mutable llvm::DenseMap<FileID, MacroArgsMap *> MacroArgsCacheMap;
6035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // SourceManager doesn't support copy construction.
6055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit SourceManager(const SourceManager&);
6065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void operator=(const SourceManager&);
6075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)public:
6085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SourceManager(DiagnosticsEngine &Diag, FileManager &FileMgr);
6095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~SourceManager();
6105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void clearIDTables();
6125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DiagnosticsEngine &getDiagnostics() const { return Diag; }
6145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FileManager &getFileManager() const { return FileMgr; }
6165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Set true if the SourceManager should report the original file name
6185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// for contents of files that were overriden by other files.Defaults to true.
6195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void setOverridenFilesKeepOriginalName(bool value) {
6205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    OverridenFilesKeepOriginalName = value;
6215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// createMainFileIDForMembuffer - Create the FileID for a memory buffer
6245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///  that will represent the FileID for the main source.  One example
6255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///  of when this would be used is when the main source is read from STDIN.
6265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) {
6275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    assert(MainFileID.isInvalid() && "MainFileID already set!");
6285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MainFileID = createFileIDForMemBuffer(Buffer);
6295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return MainFileID;
6305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //===--------------------------------------------------------------------===//
6335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // MainFileID creation and querying methods.
6345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  //===--------------------------------------------------------------------===//
6355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// getMainFileID - Returns the FileID of the main source file.
6375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FileID getMainFileID() const { return MainFileID; }
6385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// createMainFileID - Create the FileID for the main source file.
6405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FileID createMainFileID(const FileEntry *SourceFile,
6415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                          SrcMgr::CharacteristicKind Kind = SrcMgr::C_User) {
6425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    assert(MainFileID.isInvalid() && "MainFileID already set!");
6435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MainFileID = createFileID(SourceFile, SourceLocation(), Kind);
6445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return MainFileID;
6455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Set the file ID for the main source file.
6485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void setMainFileID(FileID FID) {
6495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    assert(MainFileID.isInvalid() && "MainFileID already set!");
6505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MainFileID = FID;
6515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Set the file ID for the precompiled preamble.
6545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void setPreambleFileID(FileID Preamble) {
6555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    assert(PreambleFileID.isInvalid() && "PreambleFileID already set!");
656ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    PreambleFileID = Preamble;
6575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
658ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
659ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  /// \brief Get the file ID for the precompiled preamble if there is one.
660ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  FileID getPreambleFileID() const { return PreambleFileID; }
661ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
662ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  //===--------------------------------------------------------------------===//
663ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // Methods to create new FileID's and macro expansions.
664ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  //===--------------------------------------------------------------------===//
6655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
666ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  /// createFileID - Create a new FileID that represents the specified file
667ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  /// being #included from the specified IncludePosition.  This translates NULL
6685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// into standard input.
6695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
6705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      SrcMgr::CharacteristicKind FileCharacter,
6715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      int LoadedID = 0, unsigned LoadedOffset = 0) {
6725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile);
6735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    assert(IR && "getOrCreateContentCache() cannot return NULL");
6745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return createFileID(IR, IncludePos, FileCharacter, LoadedID, LoadedOffset);
6755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// createFileIDForMemBuffer - Create a new FileID that represents the
6785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// specified memory buffer.  This does no caching of the buffer and takes
6795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once.
6805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer,
6815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  int LoadedID = 0, unsigned LoadedOffset = 0,
6825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 SourceLocation IncludeLoc = SourceLocation()) {
6835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return createFileID(createMemBufferContentCache(Buffer), IncludeLoc,
6845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        SrcMgr::C_User, LoadedID, LoadedOffset);
6855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// createMacroArgExpansionLoc - Return a new SourceLocation that encodes the
6885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// fact that a token from SpellingLoc should actually be referenced from
6895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// ExpansionLoc, and that it represents the expansion of a macro argument
6905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  /// into the function-like macro body.
6915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SourceLocation createMacroArgExpansionLoc(SourceLocation Loc,
6925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                            SourceLocation ExpansionLoc,
6935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                            unsigned TokLength);
6945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// createExpansionLoc - Return a new SourceLocation that encodes the fact
6965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// that a token from SpellingLoc should actually be referenced from
6975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  /// ExpansionLoc.
6985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SourceLocation createExpansionLoc(SourceLocation Loc,
6995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                    SourceLocation ExpansionLocStart,
7005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                    SourceLocation ExpansionLocEnd,
7015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                    unsigned TokLength,
7025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                    int LoadedID = 0,
7035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                    unsigned LoadedOffset = 0);
7045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
7055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Retrieve the memory buffer associated with the given file.
7065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
7075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \param Invalid If non-NULL, will be set \c true if an error
7085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// occurs while retrieving the memory buffer.
7095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File,
7105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                   bool *Invalid = 0);
7115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
7125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Override the contents of the given source file by providing an
7135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// already-allocated buffer.
7145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
7155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \param SourceFile the source file whose contents will be overriden.
7165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
7175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \param Buffer the memory buffer whose contents will be used as the
7185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  /// data in the given source file.
7195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
7205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \param DoNotFree If true, then the buffer will not be freed when the
7215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// source manager is destroyed.
7225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void overrideFileContents(const FileEntry *SourceFile,
7235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            const llvm::MemoryBuffer *Buffer,
7245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            bool DoNotFree = false);
7255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
726ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  /// \brief Override the the given source file with another one.
7275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ///
728ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  /// \param SourceFile the source file which will be overriden.
729ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  ///
730ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  /// \param NewFile the file whose contents will be used as the
731ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  /// data instead of the contents of the given source file.
732ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  void overrideFileContents(const FileEntry *SourceFile,
733ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                            const FileEntry *NewFile);
734ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
7355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  /// \brief Returns true if the file contents have been overridden.
736ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  bool isFileOverridden(const FileEntry *File) {
737ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    if (OverriddenFilesInfo) {
7385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (OverriddenFilesInfo->OverriddenFilesWithBuffer.count(File))
7395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return true;
7405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (OverriddenFilesInfo->OverriddenFiles.find(File) !=
7415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          OverriddenFilesInfo->OverriddenFiles.end())
7425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return true;
7435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
7445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
7455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
7465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Disable overridding the contents of a file, previously enabled
7485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// with \see overrideFileContents.
7495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// This should be called before parsing has begun.
7505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void disableFileContentsOverride(const FileEntry *File);
7515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //===--------------------------------------------------------------------===//
7535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // FileID manipulation methods.
7545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  //===--------------------------------------------------------------------===//
7555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// getBuffer - Return the buffer for the specified FileID. If there is an
7575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// error opening this buffer the first time, this manufactures a temporary
7585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// buffer and returns a non-empty error string.
7595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc,
7605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                      bool *Invalid = 0) const {
7615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool MyInvalid = false;
7625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
7635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (MyInvalid || !Entry.isFile()) {
7645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      if (Invalid)
7655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        *Invalid = true;
7665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return getFakeBufferForRecovery();
7685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
7695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return Entry.getFile().getContentCache()->getBuffer(Diag, *this, Loc,
7715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                                        Invalid);
7725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
7735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = 0) const {
7755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool MyInvalid = false;
7765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
7775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (MyInvalid || !Entry.isFile()) {
7785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      if (Invalid)
7795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        *Invalid = true;
7805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return getFakeBufferForRecovery();
7825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
7835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return Entry.getFile().getContentCache()->getBuffer(Diag, *this,
7855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                                        SourceLocation(),
7865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                        Invalid);
7875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
7885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// getFileEntryForID - Returns the FileEntry record for the provided FileID.
7905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const FileEntry *getFileEntryForID(FileID FID) const {
7915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool MyInvalid = false;
7925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
7935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (MyInvalid || !Entry.isFile())
7945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return 0;
7955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const SrcMgr::ContentCache *Content = Entry.getFile().getContentCache();
7975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!Content)
7985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return 0;
7995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return Content->OrigEntry;
800ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  }
8015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
802ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  /// Returns the FileEntry record for the provided SLocEntry.
803ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const
804ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  {
805ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    const SrcMgr::ContentCache *Content = sloc.getFile().getContentCache();
806ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    if (!Content)
807ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      return 0;
808ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    return Content->OrigEntry;
8095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
810ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
811ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  /// getBufferData - Return a StringRef to the source buffer data for the
8125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// specified FileID.
8135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
8145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \param FID The file ID whose contents will be returned.
8155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \param Invalid If non-NULL, will be set true if an error occurred.
8165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  StringRef getBufferData(FileID FID, bool *Invalid = 0) const;
8175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Get the number of FileIDs (files and macros) that were created
8195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// during preprocessing of \p FID, including it.
8205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  unsigned getNumCreatedFIDsForFileID(FileID FID) const {
8215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool Invalid = false;
8225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
8235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (Invalid || !Entry.isFile())
8245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return 0;
8255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return Entry.getFile().NumCreatedFIDs;
8275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// \brief Set the number of FileIDs (files and macros) that were created
8305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// during preprocessing of \p FID, including it.
8315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void setNumCreatedFIDsForFileID(FileID FID, unsigned NumFIDs) const {
8325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool Invalid = false;
8335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
8345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (Invalid || !Entry.isFile())
8355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return;
8365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    assert(Entry.getFile().NumCreatedFIDs == 0 && "Already set!");
8385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const_cast<SrcMgr::FileInfo &>(Entry.getFile()).NumCreatedFIDs = NumFIDs;
8395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //===--------------------------------------------------------------------===//
8425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // SourceLocation manipulation methods.
8435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //===--------------------------------------------------------------------===//
8445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// getFileID - Return the FileID for a SourceLocation.  This is a very
8465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// hot method that is used for all SourceManager queries that start with a
8475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// SourceLocation object.  It is responsible for finding the entry in
8485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// SLocEntryTable which contains the specified location.
8495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
8505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FileID getFileID(SourceLocation SpellingLoc) const {
8515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned SLocOffset = SpellingLoc.getOffset();
8525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // If our one-entry cache covers this offset, just return it.
8545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
8555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return LastFileIDLookup;
8565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
857    return getFileIDSlow(SLocOffset);
858  }
859
860  /// getLocForStartOfFile - Return the source location corresponding to the
861  /// first byte of the specified file.
862  SourceLocation getLocForStartOfFile(FileID FID) const {
863    bool Invalid = false;
864    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
865    if (Invalid || !Entry.isFile())
866      return SourceLocation();
867
868    unsigned FileOffset = Entry.getOffset();
869    return SourceLocation::getFileLoc(FileOffset);
870  }
871
872  /// \brief Return the source location corresponding to the last byte of the
873  /// specified file.
874  SourceLocation getLocForEndOfFile(FileID FID) const {
875    bool Invalid = false;
876    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
877    if (Invalid || !Entry.isFile())
878      return SourceLocation();
879
880    unsigned FileOffset = Entry.getOffset();
881    return SourceLocation::getFileLoc(FileOffset + getFileIDSize(FID) - 1);
882  }
883
884  /// \brief Returns the include location if \p FID is a #include'd file
885  /// otherwise it returns an invalid location.
886  SourceLocation getIncludeLoc(FileID FID) const {
887    bool Invalid = false;
888    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
889    if (Invalid || !Entry.isFile())
890      return SourceLocation();
891
892    return Entry.getFile().getIncludeLoc();
893  }
894
895  /// getExpansionLoc - Given a SourceLocation object, return the expansion
896  /// location referenced by the ID.
897  SourceLocation getExpansionLoc(SourceLocation Loc) const {
898    // Handle the non-mapped case inline, defer to out of line code to handle
899    // expansions.
900    if (Loc.isFileID()) return Loc;
901    return getExpansionLocSlowCase(Loc);
902  }
903
904  /// \brief Given \p Loc, if it is a macro location return the expansion
905  /// location or the spelling location, depending on if it comes from a
906  /// macro argument or not.
907  SourceLocation getFileLoc(SourceLocation Loc) const {
908    if (Loc.isFileID()) return Loc;
909    return getFileLocSlowCase(Loc);
910  }
911
912  /// getImmediateExpansionRange - Loc is required to be an expansion location.
913  /// Return the start/end of the expansion information.
914  std::pair<SourceLocation,SourceLocation>
915  getImmediateExpansionRange(SourceLocation Loc) const;
916
917  /// getExpansionRange - Given a SourceLocation object, return the range of
918  /// tokens covered by the expansion the ultimate file.
919  std::pair<SourceLocation,SourceLocation>
920  getExpansionRange(SourceLocation Loc) const;
921
922
923  /// getSpellingLoc - Given a SourceLocation object, return the spelling
924  /// location referenced by the ID.  This is the place where the characters
925  /// that make up the lexed token can be found.
926  SourceLocation getSpellingLoc(SourceLocation Loc) const {
927    // Handle the non-mapped case inline, defer to out of line code to handle
928    // expansions.
929    if (Loc.isFileID()) return Loc;
930    return getSpellingLocSlowCase(Loc);
931  }
932
933  /// getImmediateSpellingLoc - Given a SourceLocation object, return the
934  /// spelling location referenced by the ID.  This is the first level down
935  /// towards the place where the characters that make up the lexed token can be
936  /// found.  This should not generally be used by clients.
937  SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const;
938
939  /// getDecomposedLoc - Decompose the specified location into a raw FileID +
940  /// Offset pair.  The first element is the FileID, the second is the
941  /// offset from the start of the buffer of the location.
942  std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const {
943    FileID FID = getFileID(Loc);
944    bool Invalid = false;
945    const SrcMgr::SLocEntry &E = getSLocEntry(FID, &Invalid);
946    if (Invalid)
947      return std::make_pair(FileID(), 0);
948    return std::make_pair(FID, Loc.getOffset()-E.getOffset());
949  }
950
951  /// getDecomposedExpansionLoc - Decompose the specified location into a raw
952  /// FileID + Offset pair. If the location is an expansion record, walk
953  /// through it until we find the final location expanded.
954  std::pair<FileID, unsigned>
955  getDecomposedExpansionLoc(SourceLocation Loc) const {
956    FileID FID = getFileID(Loc);
957    bool Invalid = false;
958    const SrcMgr::SLocEntry *E = &getSLocEntry(FID, &Invalid);
959    if (Invalid)
960      return std::make_pair(FileID(), 0);
961
962    unsigned Offset = Loc.getOffset()-E->getOffset();
963    if (Loc.isFileID())
964      return std::make_pair(FID, Offset);
965
966    return getDecomposedExpansionLocSlowCase(E);
967  }
968
969  /// getDecomposedSpellingLoc - Decompose the specified location into a raw
970  /// FileID + Offset pair.  If the location is an expansion record, walk
971  /// through it until we find its spelling record.
972  std::pair<FileID, unsigned>
973  getDecomposedSpellingLoc(SourceLocation Loc) const {
974    FileID FID = getFileID(Loc);
975    bool Invalid = false;
976    const SrcMgr::SLocEntry *E = &getSLocEntry(FID, &Invalid);
977    if (Invalid)
978      return std::make_pair(FileID(), 0);
979
980    unsigned Offset = Loc.getOffset()-E->getOffset();
981    if (Loc.isFileID())
982      return std::make_pair(FID, Offset);
983    return getDecomposedSpellingLocSlowCase(E, Offset);
984  }
985
986  /// getFileOffset - This method returns the offset from the start
987  /// of the file that the specified SourceLocation represents. This is not very
988  /// meaningful for a macro ID.
989  unsigned getFileOffset(SourceLocation SpellingLoc) const {
990    return getDecomposedLoc(SpellingLoc).second;
991  }
992
993  /// isMacroArgExpansion - This method tests whether the given source location
994  /// represents a macro argument's expansion into the function-like macro
995  /// definition. Such source locations only appear inside of the expansion
996  /// locations representing where a particular function-like macro was
997  /// expanded.
998  bool isMacroArgExpansion(SourceLocation Loc) const;
999
1000  /// \brief Returns true if \p Loc is inside the [\p Start, +\p Length)
1001  /// chunk of the source location address space.
1002  /// If it's true and \p RelativeOffset is non-null, it will be set to the
1003  /// relative offset of \p Loc inside the chunk.
1004  bool isInSLocAddrSpace(SourceLocation Loc,
1005                         SourceLocation Start, unsigned Length,
1006                         unsigned *RelativeOffset = 0) const {
1007    assert(((Start.getOffset() < NextLocalOffset &&
1008               Start.getOffset()+Length <= NextLocalOffset) ||
1009            (Start.getOffset() >= CurrentLoadedOffset &&
1010                Start.getOffset()+Length < MaxLoadedOffset)) &&
1011           "Chunk is not valid SLoc address space");
1012    unsigned LocOffs = Loc.getOffset();
1013    unsigned BeginOffs = Start.getOffset();
1014    unsigned EndOffs = BeginOffs + Length;
1015    if (LocOffs >= BeginOffs && LocOffs < EndOffs) {
1016      if (RelativeOffset)
1017        *RelativeOffset = LocOffs - BeginOffs;
1018      return true;
1019    }
1020
1021    return false;
1022  }
1023
1024  /// \brief Return true if both \p LHS and \p RHS are in the local source
1025  /// location address space or the loaded one. If it's true and \p
1026  /// RelativeOffset is non-null, it will be set to the offset of \p RHS
1027  /// relative to \p LHS.
1028  bool isInSameSLocAddrSpace(SourceLocation LHS, SourceLocation RHS,
1029                             int *RelativeOffset) const {
1030    unsigned LHSOffs = LHS.getOffset(), RHSOffs = RHS.getOffset();
1031    bool LHSLoaded = LHSOffs >= CurrentLoadedOffset;
1032    bool RHSLoaded = RHSOffs >= CurrentLoadedOffset;
1033
1034    if (LHSLoaded == RHSLoaded) {
1035      if (RelativeOffset)
1036        *RelativeOffset = RHSOffs - LHSOffs;
1037      return true;
1038    }
1039
1040    return false;
1041  }
1042
1043  //===--------------------------------------------------------------------===//
1044  // Queries about the code at a SourceLocation.
1045  //===--------------------------------------------------------------------===//
1046
1047  /// getCharacterData - Return a pointer to the start of the specified location
1048  /// in the appropriate spelling MemoryBuffer.
1049  ///
1050  /// \param Invalid If non-NULL, will be set \c true if an error occurs.
1051  const char *getCharacterData(SourceLocation SL, bool *Invalid = 0) const;
1052
1053  /// getColumnNumber - Return the column # for the specified file position.
1054  /// This is significantly cheaper to compute than the line number.  This
1055  /// returns zero if the column number isn't known.  This may only be called
1056  /// on a file sloc, so you must choose a spelling or expansion location
1057  /// before calling this method.
1058  unsigned getColumnNumber(FileID FID, unsigned FilePos,
1059                           bool *Invalid = 0) const;
1060  unsigned getSpellingColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
1061  unsigned getExpansionColumnNumber(SourceLocation Loc,
1062                                    bool *Invalid = 0) const;
1063  unsigned getPresumedColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
1064
1065
1066  /// getLineNumber - Given a SourceLocation, return the spelling line number
1067  /// for the position indicated.  This requires building and caching a table of
1068  /// line offsets for the MemoryBuffer, so this is not cheap: use only when
1069  /// about to emit a diagnostic.
1070  unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = 0) const;
1071  unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
1072  unsigned getExpansionLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
1073  unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
1074
1075  /// Return the filename or buffer identifier of the buffer the location is in.
1076  /// Note that this name does not respect #line directives.  Use getPresumedLoc
1077  /// for normal clients.
1078  const char *getBufferName(SourceLocation Loc, bool *Invalid = 0) const;
1079
1080  /// getFileCharacteristic - return the file characteristic of the specified
1081  /// source location, indicating whether this is a normal file, a system
1082  /// header, or an "implicit extern C" system header.
1083  ///
1084  /// This state can be modified with flags on GNU linemarker directives like:
1085  ///   # 4 "foo.h" 3
1086  /// which changes all source locations in the current file after that to be
1087  /// considered to be from a system header.
1088  SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const;
1089
1090  /// getPresumedLoc - This method returns the "presumed" location of a
1091  /// SourceLocation specifies.  A "presumed location" can be modified by #line
1092  /// or GNU line marker directives.  This provides a view on the data that a
1093  /// user should see in diagnostics, for example.
1094  ///
1095  /// Note that a presumed location is always given as the expansion point of
1096  /// an expansion location, not at the spelling location.
1097  ///
1098  /// \returns The presumed location of the specified SourceLocation. If the
1099  /// presumed location cannot be calculate (e.g., because \p Loc is invalid
1100  /// or the file containing \p Loc has changed on disk), returns an invalid
1101  /// presumed location.
1102  PresumedLoc getPresumedLoc(SourceLocation Loc) const;
1103
1104  /// isFromSameFile - Returns true if both SourceLocations correspond to
1105  ///  the same file.
1106  bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
1107    return getFileID(Loc1) == getFileID(Loc2);
1108  }
1109
1110  /// isFromMainFile - Returns true if the file of provided SourceLocation is
1111  ///   the main file.
1112  bool isFromMainFile(SourceLocation Loc) const {
1113    return getFileID(Loc) == getMainFileID();
1114  }
1115
1116  /// isInSystemHeader - Returns if a SourceLocation is in a system header.
1117  bool isInSystemHeader(SourceLocation Loc) const {
1118    return getFileCharacteristic(Loc) != SrcMgr::C_User;
1119  }
1120
1121  /// isInExternCSystemHeader - Returns if a SourceLocation is in an "extern C"
1122  /// system header.
1123  bool isInExternCSystemHeader(SourceLocation Loc) const {
1124    return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem;
1125  }
1126
1127  /// \brief Returns whether \p Loc is expanded from a macro in a system header.
1128  bool isInSystemMacro(SourceLocation loc) {
1129    return loc.isMacroID() && isInSystemHeader(getSpellingLoc(loc));
1130  }
1131
1132  /// \brief The size of the SLocEnty that \p FID represents.
1133  unsigned getFileIDSize(FileID FID) const;
1134
1135  /// \brief Given a specific FileID, returns true if \p Loc is inside that
1136  /// FileID chunk and sets relative offset (offset of \p Loc from beginning
1137  /// of FileID) to \p relativeOffset.
1138  bool isInFileID(SourceLocation Loc, FileID FID,
1139                  unsigned *RelativeOffset = 0) const {
1140    unsigned Offs = Loc.getOffset();
1141    if (isOffsetInFileID(FID, Offs)) {
1142      if (RelativeOffset)
1143        *RelativeOffset = Offs - getSLocEntry(FID).getOffset();
1144      return true;
1145    }
1146
1147    return false;
1148  }
1149
1150  //===--------------------------------------------------------------------===//
1151  // Line Table Manipulation Routines
1152  //===--------------------------------------------------------------------===//
1153
1154  /// getLineTableFilenameID - Return the uniqued ID for the specified filename.
1155  ///
1156  unsigned getLineTableFilenameID(StringRef Str);
1157
1158  /// AddLineNote - Add a line note to the line table for the FileID and offset
1159  /// specified by Loc.  If FilenameID is -1, it is considered to be
1160  /// unspecified.
1161  void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID);
1162  void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID,
1163                   bool IsFileEntry, bool IsFileExit,
1164                   bool IsSystemHeader, bool IsExternCHeader);
1165
1166  /// \brief Determine if the source manager has a line table.
1167  bool hasLineTable() const { return LineTable != 0; }
1168
1169  /// \brief Retrieve the stored line table.
1170  LineTableInfo &getLineTable();
1171
1172  //===--------------------------------------------------------------------===//
1173  // Queries for performance analysis.
1174  //===--------------------------------------------------------------------===//
1175
1176  /// Return the total amount of physical memory allocated by the
1177  /// ContentCache allocator.
1178  size_t getContentCacheSize() const {
1179    return ContentCacheAlloc.getTotalMemory();
1180  }
1181
1182  struct MemoryBufferSizes {
1183    const size_t malloc_bytes;
1184    const size_t mmap_bytes;
1185
1186    MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)
1187      : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {}
1188  };
1189
1190  /// Return the amount of memory used by memory buffers, breaking down
1191  /// by heap-backed versus mmap'ed memory.
1192  MemoryBufferSizes getMemoryBufferSizes() const;
1193
1194  // Return the amount of memory used for various side tables and
1195  // data structures in the SourceManager.
1196  size_t getDataStructureSizes() const;
1197
1198  //===--------------------------------------------------------------------===//
1199  // Other miscellaneous methods.
1200  //===--------------------------------------------------------------------===//
1201
1202  /// \brief Get the source location for the given file:line:col triplet.
1203  ///
1204  /// If the source file is included multiple times, the source location will
1205  /// be based upon the first inclusion.
1206  SourceLocation translateFileLineCol(const FileEntry *SourceFile,
1207                                      unsigned Line, unsigned Col) const;
1208
1209  /// \brief Get the FileID for the given file.
1210  ///
1211  /// If the source file is included multiple times, the FileID will be the
1212  /// first inclusion.
1213  FileID translateFile(const FileEntry *SourceFile) const;
1214
1215  /// \brief Get the source location in \p FID for the given line:col.
1216  /// Returns null location if \p FID is not a file SLocEntry.
1217  SourceLocation translateLineCol(FileID FID,
1218                                  unsigned Line, unsigned Col) const;
1219
1220  /// \brief If \p Loc points inside a function macro argument, the returned
1221  /// location will be the macro location in which the argument was expanded.
1222  /// If a macro argument is used multiple times, the expanded location will
1223  /// be at the first expansion of the argument.
1224  /// e.g.
1225  ///   MY_MACRO(foo);
1226  ///             ^
1227  /// Passing a file location pointing at 'foo', will yield a macro location
1228  /// where 'foo' was expanded into.
1229  SourceLocation getMacroArgExpandedLocation(SourceLocation Loc) const;
1230
1231  /// \brief Determines the order of 2 source locations in the translation unit.
1232  ///
1233  /// \returns true if LHS source location comes before RHS, false otherwise.
1234  bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const;
1235
1236  /// \brief Comparison function class.
1237  class LocBeforeThanCompare : public std::binary_function<SourceLocation,
1238                                                         SourceLocation, bool> {
1239    SourceManager &SM;
1240
1241  public:
1242    explicit LocBeforeThanCompare(SourceManager &SM) : SM(SM) { }
1243
1244    bool operator()(SourceLocation LHS, SourceLocation RHS) const {
1245      return SM.isBeforeInTranslationUnit(LHS, RHS);
1246    }
1247  };
1248
1249  /// \brief Determines the order of 2 source locations in the "source location
1250  /// address space".
1251  bool isBeforeInSLocAddrSpace(SourceLocation LHS, SourceLocation RHS) const {
1252    return isBeforeInSLocAddrSpace(LHS, RHS.getOffset());
1253  }
1254
1255  /// \brief Determines the order of a source location and a source location
1256  /// offset in the "source location address space".
1257  ///
1258  /// Note that we always consider source locations loaded from
1259  bool isBeforeInSLocAddrSpace(SourceLocation LHS, unsigned RHS) const {
1260    unsigned LHSOffset = LHS.getOffset();
1261    bool LHSLoaded = LHSOffset >= CurrentLoadedOffset;
1262    bool RHSLoaded = RHS >= CurrentLoadedOffset;
1263    if (LHSLoaded == RHSLoaded)
1264      return LHSOffset < RHS;
1265
1266    return LHSLoaded;
1267  }
1268
1269  // Iterators over FileInfos.
1270  typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>
1271      ::const_iterator fileinfo_iterator;
1272  fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); }
1273  fileinfo_iterator fileinfo_end() const { return FileInfos.end(); }
1274  bool hasFileInfo(const FileEntry *File) const {
1275    return FileInfos.find(File) != FileInfos.end();
1276  }
1277
1278  /// PrintStats - Print statistics to stderr.
1279  ///
1280  void PrintStats() const;
1281
1282  /// \brief Get the number of local SLocEntries we have.
1283  unsigned local_sloc_entry_size() const { return LocalSLocEntryTable.size(); }
1284
1285  /// \brief Get a local SLocEntry. This is exposed for indexing.
1286  const SrcMgr::SLocEntry &getLocalSLocEntry(unsigned Index,
1287                                             bool *Invalid = 0) const {
1288    assert(Index < LocalSLocEntryTable.size() && "Invalid index");
1289    return LocalSLocEntryTable[Index];
1290  }
1291
1292  /// \brief Get the number of loaded SLocEntries we have.
1293  unsigned loaded_sloc_entry_size() const { return LoadedSLocEntryTable.size();}
1294
1295  /// \brief Get a loaded SLocEntry. This is exposed for indexing.
1296  const SrcMgr::SLocEntry &getLoadedSLocEntry(unsigned Index,
1297                                              bool *Invalid = 0) const {
1298    assert(Index < LoadedSLocEntryTable.size() && "Invalid index");
1299    if (SLocEntryLoaded[Index])
1300      return LoadedSLocEntryTable[Index];
1301    return loadSLocEntry(Index, Invalid);
1302  }
1303
1304  const SrcMgr::SLocEntry &getSLocEntry(FileID FID, bool *Invalid = 0) const {
1305    if (FID.ID == 0 || FID.ID == -1) {
1306      if (Invalid) *Invalid = true;
1307      return LocalSLocEntryTable[0];
1308    }
1309    return getSLocEntryByID(FID.ID);
1310  }
1311
1312  unsigned getNextLocalOffset() const { return NextLocalOffset; }
1313
1314  void setExternalSLocEntrySource(ExternalSLocEntrySource *Source) {
1315    assert(LoadedSLocEntryTable.empty() &&
1316           "Invalidating existing loaded entries");
1317    ExternalSLocEntries = Source;
1318  }
1319
1320  /// \brief Allocate a number of loaded SLocEntries, which will be actually
1321  /// loaded on demand from the external source.
1322  ///
1323  /// NumSLocEntries will be allocated, which occupy a total of TotalSize space
1324  /// in the global source view. The lowest ID and the base offset of the
1325  /// entries will be returned.
1326  std::pair<int, unsigned>
1327  AllocateLoadedSLocEntries(unsigned NumSLocEntries, unsigned TotalSize);
1328
1329  /// \brief Returns true if \p Loc came from a PCH/Module.
1330  bool isLoadedSourceLocation(SourceLocation Loc) const {
1331    return Loc.getOffset() >= CurrentLoadedOffset;
1332  }
1333
1334  /// \brief Returns true if \p Loc did not come from a PCH/Module.
1335  bool isLocalSourceLocation(SourceLocation Loc) const {
1336    return Loc.getOffset() < NextLocalOffset;
1337  }
1338
1339  /// \brief Returns true if \p FID came from a PCH/Module.
1340  bool isLoadedFileID(FileID FID) const {
1341    assert(FID.ID != -1 && "Using FileID sentinel value");
1342    return FID.ID < 0;
1343  }
1344
1345  /// \brief Returns true if \p FID did not come from a PCH/Module.
1346  bool isLocalFileID(FileID FID) const {
1347    return !isLoadedFileID(FID);
1348  }
1349
1350private:
1351  const llvm::MemoryBuffer *getFakeBufferForRecovery() const;
1352  const SrcMgr::ContentCache *getFakeContentCacheForRecovery() const;
1353
1354  const SrcMgr::SLocEntry &loadSLocEntry(unsigned Index, bool *Invalid) const;
1355
1356  /// \brief Get the entry with the given unwrapped FileID.
1357  const SrcMgr::SLocEntry &getSLocEntryByID(int ID) const {
1358    assert(ID != -1 && "Using FileID sentinel value");
1359    if (ID < 0)
1360      return getLoadedSLocEntryByID(ID);
1361    return getLocalSLocEntry(static_cast<unsigned>(ID));
1362  }
1363
1364  const SrcMgr::SLocEntry &getLoadedSLocEntryByID(int ID,
1365                                                  bool *Invalid = 0) const {
1366    return getLoadedSLocEntry(static_cast<unsigned>(-ID - 2), Invalid);
1367  }
1368
1369  /// createExpansionLoc - Implements the common elements of storing an
1370  /// expansion info struct into the SLocEntry table and producing a source
1371  /// location that refers to it.
1372  SourceLocation createExpansionLocImpl(const SrcMgr::ExpansionInfo &Expansion,
1373                                        unsigned TokLength,
1374                                        int LoadedID = 0,
1375                                        unsigned LoadedOffset = 0);
1376
1377  /// isOffsetInFileID - Return true if the specified FileID contains the
1378  /// specified SourceLocation offset.  This is a very hot method.
1379  inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const {
1380    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID);
1381    // If the entry is after the offset, it can't contain it.
1382    if (SLocOffset < Entry.getOffset()) return false;
1383
1384    // If this is the very last entry then it does.
1385    if (FID.ID == -2)
1386      return true;
1387
1388    // If it is the last local entry, then it does if the location is local.
1389    if (static_cast<unsigned>(FID.ID+1) == LocalSLocEntryTable.size()) {
1390      return SLocOffset < NextLocalOffset;
1391    }
1392
1393    // Otherwise, the entry after it has to not include it. This works for both
1394    // local and loaded entries.
1395    return SLocOffset < getSLocEntry(FileID::get(FID.ID+1)).getOffset();
1396  }
1397
1398  /// createFileID - Create a new fileID for the specified ContentCache and
1399  ///  include position.  This works regardless of whether the ContentCache
1400  ///  corresponds to a file or some other input source.
1401  FileID createFileID(const SrcMgr::ContentCache* File,
1402                      SourceLocation IncludePos,
1403                      SrcMgr::CharacteristicKind DirCharacter,
1404                      int LoadedID, unsigned LoadedOffset);
1405
1406  const SrcMgr::ContentCache *
1407    getOrCreateContentCache(const FileEntry *SourceFile);
1408
1409  /// createMemBufferContentCache - Create a new ContentCache for the specified
1410  ///  memory buffer.
1411  const SrcMgr::ContentCache*
1412  createMemBufferContentCache(const llvm::MemoryBuffer *Buf);
1413
1414  FileID getFileIDSlow(unsigned SLocOffset) const;
1415  FileID getFileIDLocal(unsigned SLocOffset) const;
1416  FileID getFileIDLoaded(unsigned SLocOffset) const;
1417
1418  SourceLocation getExpansionLocSlowCase(SourceLocation Loc) const;
1419  SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;
1420  SourceLocation getFileLocSlowCase(SourceLocation Loc) const;
1421
1422  std::pair<FileID, unsigned>
1423  getDecomposedExpansionLocSlowCase(const SrcMgr::SLocEntry *E) const;
1424  std::pair<FileID, unsigned>
1425  getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
1426                                   unsigned Offset) const;
1427  void computeMacroArgsCache(MacroArgsMap *&MacroArgsCache, FileID FID) const;
1428
1429  friend class ASTReader;
1430  friend class ASTWriter;
1431};
1432
1433
1434}  // end namespace clang
1435
1436#endif
1437