SourceManager.h revision 29f39425fc7fcaede88a7e573f392975f8dc5e3e
13d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes//===--- SourceManager.h - Track and cache source files ---------*- C++ -*-===//
23d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes//
33d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes//                     The LLVM Compiler Infrastructure
43d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes//
53d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes// This file is distributed under the University of Illinois Open Source
63d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes// License. See LICENSE.TXT for details.
73d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes//
83d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes//===----------------------------------------------------------------------===//
93d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes//
103d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes//  This file defines the SourceManager interface.
113d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes//
123d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes//===----------------------------------------------------------------------===//
133d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
143d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes#ifndef LLVM_CLANG_SOURCEMANAGER_H
153d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes#define LLVM_CLANG_SOURCEMANAGER_H
163d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
173d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes#include "clang/Basic/LLVM.h"
183d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes#include "clang/Basic/SourceLocation.h"
193d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes#include "llvm/Support/Allocator.h"
203d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes#include "llvm/Support/DataTypes.h"
213d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes#include "llvm/ADT/PointerIntPair.h"
223d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes#include "llvm/ADT/PointerUnion.h"
233d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes#include "llvm/ADT/IntrusiveRefCntPtr.h"
243d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes#include "llvm/ADT/DenseMap.h"
253d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes#include "llvm/Support/MemoryBuffer.h"
263d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes#include <map>
273d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes#include <vector>
283d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes#include <cassert>
293d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
303d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentesnamespace clang {
313d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
323d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentesclass Diagnostic;
333d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentesclass SourceManager;
343d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentesclass FileManager;
353d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentesclass FileEntry;
363d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentesclass LineTableInfo;
373d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentesclass LangOptions;
383d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentesclass ASTWriter;
393d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentesclass ASTReader;
403d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
413d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes/// There are three different types of locations in a file: a spelling
423d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes/// location, an expansion location, and a presumed location.
433d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes///
443d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes/// Given an example of:
453d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes/// #define min(x, y) x < y ? x : y
463d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes///
473d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes/// and then later on a use of min:
483d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes/// return min(a, b);
493d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes/// #line 17
503d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes///
513d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes/// The expansion location is the line in the source code where the macro
523d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes/// was expanded (the return statement), the spelling location is the
533d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes/// location in the source where the macro was originally defined,
543d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes/// and the presumed location is where the line directive states that
553d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes/// the line is 17, or any other line.
563d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
573d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes/// SrcMgr - Public enums and private classes that are part of the
583d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes/// SourceManager implementation.
593d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes///
603d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentesnamespace SrcMgr {
613d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// CharacteristicKind - This is used to represent whether a file or directory
623d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// holds normal user code, system code, or system code which is implicitly
633d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// 'extern "C"' in C++ mode.  Entire directories can be tagged with this
64de98db33fb10a13ead2fa56d6d4c944cedb8fbadOscar Fuentes  /// (this is maintained by DirectoryLookup and friends) as can specific
653d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// FileInfos when a #pragma system_header is seen or various other cases.
663d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  ///
673d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  enum CharacteristicKind {
683d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    C_User, C_System, C_ExternCSystem
693d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  };
703d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
713d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// ContentCache - One instance of this struct is kept for every file
723d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// loaded or used.  This object owns the MemoryBuffer object.
733d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  class ContentCache {
743d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    enum CCFlags {
753d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      /// \brief Whether the buffer is invalid.
763d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      InvalidFlag = 0x01,
773d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      /// \brief Whether the buffer should not be freed on destruction.
783d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      DoNotFreeFlag = 0x02
793d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    };
803d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
813d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// Buffer - The actual buffer containing the characters from the input
823d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// file.  This is owned by the ContentCache object.
833d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// The bits indicate indicates whether the buffer is invalid.
843d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    mutable llvm::PointerIntPair<const llvm::MemoryBuffer *, 2> Buffer;
853d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
863d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  public:
873d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// Reference to the file entry representing this ContentCache.
883d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// This reference does not own the FileEntry object.
893d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// It is possible for this to be NULL if
903d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// the ContentCache encapsulates an imaginary text buffer.
913d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    const FileEntry *OrigEntry;
923d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
933d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// \brief References the file which the contents were actually loaded from.
943d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// Can be different from 'Entry' if we overridden the contents of one file
953d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// with the contents of another file.
963d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    const FileEntry *ContentsEntry;
973d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
983d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// SourceLineCache - A bump pointer allocated array of offsets for each
993d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// source line.  This is lazily computed.  This is owned by the
1003d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// SourceManager BumpPointerAllocator object.
1013d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    unsigned *SourceLineCache;
1023d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
1033d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// NumLines - The number of lines in this ContentCache.  This is only valid
1043d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// if SourceLineCache is non-null.
1053d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    unsigned NumLines;
1063d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
1073d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// \brief Lazily computed map of macro argument chunks to their expanded
1083d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// source location.
1093d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    typedef std::map<unsigned, SourceLocation> MacroArgsMap;
1103d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    MacroArgsMap *MacroArgsCache;
1113d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
1123d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// getBuffer - Returns the memory buffer for the associated content.
1133d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    ///
1143d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// \param Diag Object through which diagnostics will be emitted if the
1153d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// buffer cannot be retrieved.
1163d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    ///
1173d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// \param Loc If specified, is the location that invalid file diagnostics
1183d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    ///     will be emitted at.
119de98db33fb10a13ead2fa56d6d4c944cedb8fbadOscar Fuentes    ///
1203d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// \param Invalid If non-NULL, will be set \c true if an error occurred.
1213d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    const llvm::MemoryBuffer *getBuffer(Diagnostic &Diag,
1223d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes                                        const SourceManager &SM,
1233d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes                                        SourceLocation Loc = SourceLocation(),
1243d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes                                        bool *Invalid = 0) const;
1253d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
1263d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// getSize - Returns the size of the content encapsulated by this
1273d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    ///  ContentCache. This can be the size of the source file or the size of an
1283d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    ///  arbitrary scratch buffer.  If the ContentCache encapsulates a source
1293d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    ///  file this size is retrieved from the file's FileEntry.
1303d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    unsigned getSize() const;
1313d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
1323d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// getSizeBytesMapped - Returns the number of bytes actually mapped for
1333d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// this ContentCache. This can be 0 if the MemBuffer was not actually
1343d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// expanded.
1353d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    unsigned getSizeBytesMapped() const;
1363d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
1373d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// Returns the kind of memory used to back the memory buffer for
1383d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// this content cache.  This is used for performance analysis.
1393d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    llvm::MemoryBuffer::BufferKind getMemoryBufferKind() const;
1403d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
1413d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    void setBuffer(const llvm::MemoryBuffer *B) {
1423d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      assert(!Buffer.getPointer() && "MemoryBuffer already set.");
1433d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      Buffer.setPointer(B);
1443d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      Buffer.setInt(false);
1453d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    }
1463d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
1473d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// \brief Get the underlying buffer, returning NULL if the buffer is not
1483d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// yet available.
1493d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    const llvm::MemoryBuffer *getRawBuffer() const {
1503d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      return Buffer.getPointer();
1513d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    }
1523d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
1533d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// \brief Replace the existing buffer (which will be deleted)
1543d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// with the given buffer.
1553d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    void replaceBuffer(const llvm::MemoryBuffer *B, bool DoNotFree = false);
1563d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
1573d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// \brief Determine whether the buffer itself is invalid.
1583d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    bool isBufferInvalid() const {
1593d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      return Buffer.getInt() & InvalidFlag;
1603d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    }
1613d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
1623d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// \brief Determine whether the buffer should be freed.
1633d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    bool shouldFreeBuffer() const {
1643d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      return (Buffer.getInt() & DoNotFreeFlag) == 0;
1653d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    }
1663d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
16701746745f1287effa1772ef51b973988afcea699Douglas Gregor    ContentCache(const FileEntry *Ent = 0)
1683d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      : Buffer(0, false), OrigEntry(Ent), ContentsEntry(Ent),
1693d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes        SourceLineCache(0), NumLines(0), MacroArgsCache(0) {}
1703d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
1713d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    ContentCache(const FileEntry *Ent, const FileEntry *contentEnt)
1723d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      : Buffer(0, false), OrigEntry(Ent), ContentsEntry(contentEnt),
1733d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes        SourceLineCache(0), NumLines(0), MacroArgsCache(0) {}
1743d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
1753d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    ~ContentCache();
1763d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
1773d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// The copy ctor does not allow copies where source object has either
1783d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    ///  a non-NULL Buffer or SourceLineCache.  Ownership of allocated memory
1793d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    ///  is not transferred, so this is a logical error.
1803d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    ContentCache(const ContentCache &RHS)
1813d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      : Buffer(0, false), SourceLineCache(0), MacroArgsCache(0)
1823d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    {
1833d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      OrigEntry = RHS.OrigEntry;
1843d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      ContentsEntry = RHS.ContentsEntry;
1853d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
1863d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      assert (RHS.Buffer.getPointer() == 0 && RHS.SourceLineCache == 0 &&
1873d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes              RHS.MacroArgsCache == 0
1883d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes              && "Passed ContentCache object cannot own a buffer.");
1893d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
1903d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      NumLines = RHS.NumLines;
1913d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    }
1923d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
1933d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  private:
1943d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    // Disable assignments.
1953d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    ContentCache &operator=(const ContentCache& RHS);
1963d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  };
197d413c01088ff0bf9ab1d856f75ef2ba84427fd6dOscar Fuentes
1983d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// FileInfo - Information about a FileID, basically just the logical file
1993d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// that it represents and include stack information.
2003d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  ///
2013d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// Each FileInfo has include stack information, indicating where it came
2023d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// from. This information encodes the #include chain that a token was
2033d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// expanded from. The main include file has an invalid IncludeLoc.
2043d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  ///
2053d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// FileInfos contain a "ContentCache *", with the contents of the file.
2063d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  ///
2073d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  class FileInfo {
2083d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// IncludeLoc - The location of the #include that brought in this file.
2093d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// This is an invalid SLOC for the main file (top of the #include chain).
2103d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    unsigned IncludeLoc;  // Really a SourceLocation
2113d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
2123d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// \brief Number of FileIDs (files and macros) that were created during
2133d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// preprocessing of this #include, including this SLocEntry.
2143d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// Zero means the preprocessor didn't provide such info for this SLocEntry.
2153d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    unsigned NumCreatedFIDs;
2163d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
2173d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// Data - This contains the ContentCache* and the bits indicating the
2183d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// characteristic of the file and whether it has #line info, all bitmangled
2193d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// together.
2203d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    uintptr_t Data;
2213d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
2223d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    friend class clang::SourceManager;
2233d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    friend class clang::ASTWriter;
2243d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    friend class clang::ASTReader;
2253d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  public:
2263d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// get - Return a FileInfo object.
2273d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    static FileInfo get(SourceLocation IL, const ContentCache *Con,
2283d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes                        CharacteristicKind FileCharacter) {
2293d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      FileInfo X;
2303d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      X.IncludeLoc = IL.getRawEncoding();
231ef23d711bc4ff72799693f40c15b29cfdabd89a0Douglas Gregor      X.NumCreatedFIDs = 0;
2323d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      X.Data = (uintptr_t)Con;
2333d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned");
2343d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      assert((unsigned)FileCharacter < 4 && "invalid file character");
2353d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      X.Data |= (unsigned)FileCharacter;
2363d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      return X;
2373d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    }
2383d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
2393d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    SourceLocation getIncludeLoc() const {
2403d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      return SourceLocation::getFromRawEncoding(IncludeLoc);
2413d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    }
2423d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    const ContentCache* getContentCache() const {
2433d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      return reinterpret_cast<const ContentCache*>(Data & ~7UL);
2443d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    }
2453d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
2463d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// getCharacteristic - Return whether this is a system header or not.
2473d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    CharacteristicKind getFileCharacteristic() const {
2483d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      return (CharacteristicKind)(Data & 3);
2493d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    }
2503d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
2513d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// hasLineDirectives - Return true if this FileID has #line directives in
2523d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// it.
2533d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    bool hasLineDirectives() const { return (Data & 4) != 0; }
2543d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
2553d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// setHasLineDirectives - Set the flag that indicates that this FileID has
2563d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// line table entries associated with it.
2573d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    void setHasLineDirectives() {
2583d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      Data |= 4;
2593d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    }
2603d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  };
2613d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
2623d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// ExpansionInfo - Each ExpansionInfo encodes the expansion location - where
2633d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// the token was ultimately expanded, and the SpellingLoc - where the actual
2643d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// character data for the token came from.
2653d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  class ExpansionInfo {
2663d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    // Really these are all SourceLocations.
2673d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
2683d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// SpellingLoc - Where the spelling for the token can be found.
2693d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    unsigned SpellingLoc;
2703d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
2713d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// ExpansionLocStart/ExpansionLocEnd - In a macro expansion, these
2723d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// indicate the start and end of the expansion. In object-like macros,
2733d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// these will be the same. In a function-like macro expansion, the start
2743d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// will be the identifier and the end will be the ')'. Finally, in
2753d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// macro-argument instantitions, the end will be 'SourceLocation()', an
2763d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// invalid location.
2773d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    unsigned ExpansionLocStart, ExpansionLocEnd;
2783d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
279ec8b2a95303ef51acdf5b034ab8f4371665fc136Oscar Fuentes  public:
280ec8b2a95303ef51acdf5b034ab8f4371665fc136Oscar Fuentes    SourceLocation getSpellingLoc() const {
281ec8b2a95303ef51acdf5b034ab8f4371665fc136Oscar Fuentes      return SourceLocation::getFromRawEncoding(SpellingLoc);
2823d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    }
2833d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    SourceLocation getExpansionLocStart() const {
2843d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      return SourceLocation::getFromRawEncoding(ExpansionLocStart);
2853d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    }
2863d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    SourceLocation getExpansionLocEnd() const {
2873d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      SourceLocation EndLoc =
2883d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes        SourceLocation::getFromRawEncoding(ExpansionLocEnd);
2893d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      return EndLoc.isInvalid() ? getExpansionLocStart() : EndLoc;
2903d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    }
2913d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
2923d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    std::pair<SourceLocation,SourceLocation> getExpansionLocRange() const {
2933d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      return std::make_pair(getExpansionLocStart(), getExpansionLocEnd());
2943d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    }
2953d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
2963d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    bool isMacroArgExpansion() const {
2973d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      // Note that this needs to return false for default constructed objects.
2983d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      return getExpansionLocStart().isValid() &&
2993d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes        SourceLocation::getFromRawEncoding(ExpansionLocEnd).isInvalid();
3003d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    }
3013d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
3023d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// create - Return a ExpansionInfo for an expansion. Start and End specify
3033d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// the expansion range (where the macro is expanded), and SpellingLoc
3043d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// specifies the spelling location (where the characters from the token
3053d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// come from). All three can refer to normal File SLocs or expansion
3063d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// locations.
3073d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    static ExpansionInfo create(SourceLocation SpellingLoc,
3083d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes                                SourceLocation Start, SourceLocation End) {
3093d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      ExpansionInfo X;
3103d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      X.SpellingLoc = SpellingLoc.getRawEncoding();
3113d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      X.ExpansionLocStart = Start.getRawEncoding();
3123d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      X.ExpansionLocEnd = End.getRawEncoding();
3133d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      return X;
3143d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    }
3153d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
3163d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// createForMacroArg - Return a special ExpansionInfo for the expansion of
3173d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// a macro argument into a function-like macro's body. ExpansionLoc
3183d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// specifies the expansion location (where the macro is expanded). This
3193d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// doesn't need to be a range because a macro is always expanded at
3203d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// a macro parameter reference, and macro parameters are always exactly
3213d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// one token. SpellingLoc specifies the spelling location (where the
3223d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// characters from the token come from). ExpansionLoc and SpellingLoc can
3233d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// both refer to normal File SLocs or expansion locations.
3243d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    ///
3253d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// Given the code:
3263d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// \code
3273d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    ///   #define F(x) f(x)
3283d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    ///   F(42);
3293d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// \endcode
3303d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    ///
3313d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// When expanding '\c F(42)', the '\c x' would call this with an
3323d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// SpellingLoc pointing at '\c 42' anad an ExpansionLoc pointing at its
3333d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    /// location in the definition of '\c F'.
3343d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    static ExpansionInfo createForMacroArg(SourceLocation SpellingLoc,
3353d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes                                           SourceLocation ExpansionLoc) {
3363d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      // We store an intentionally invalid source location for the end of the
3373d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      // expansion range to mark that this is a macro argument ion rather than
3383d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      // a normal one.
3393d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      return create(SpellingLoc, ExpansionLoc, SourceLocation());
3403d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    }
3413d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  };
3423d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
3433d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// SLocEntry - This is a discriminated union of FileInfo and
3443d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// ExpansionInfo.  SourceManager keeps an array of these objects, and
3453d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// they are uniquely identified by the FileID datatype.
3463d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  class SLocEntry {
3473d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    unsigned Offset;   // low bit is set for expansion info.
3483d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    union {
3493d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      FileInfo File;
3503d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      ExpansionInfo Expansion;
3513d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    };
3523d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  public:
3533d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    unsigned getOffset() const { return Offset >> 1; }
3543d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
3553d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    bool isExpansion() const { return Offset & 1; }
3563d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    bool isFile() const { return !isExpansion(); }
3573d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
3583d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    const FileInfo &getFile() const {
3593d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      assert(isFile() && "Not a file SLocEntry!");
3603d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      return File;
3613d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    }
3623d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
3633d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    const ExpansionInfo &getExpansion() const {
3643d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      assert(isExpansion() && "Not a macro expansion SLocEntry!");
3653d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      return Expansion;
3663d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    }
3673d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
3683d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    static SLocEntry get(unsigned Offset, const FileInfo &FI) {
3693d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      SLocEntry E;
370de98db33fb10a13ead2fa56d6d4c944cedb8fbadOscar Fuentes      E.Offset = Offset << 1;
3713d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      E.File = FI;
3723d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      return E;
3733d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    }
3743d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
3753d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    static SLocEntry get(unsigned Offset, const ExpansionInfo &Expansion) {
376de98db33fb10a13ead2fa56d6d4c944cedb8fbadOscar Fuentes      SLocEntry E;
3773d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      E.Offset = (Offset << 1) | 1;
3783d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      E.Expansion = Expansion;
3793d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      return E;
3803d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    }
3813d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  };
3823d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes}  // end SrcMgr namespace.
3833d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
3843d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes/// \brief External source of source location entries.
3853d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentesclass ExternalSLocEntrySource {
3863d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentespublic:
3873d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  virtual ~ExternalSLocEntrySource();
3883d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
3893d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// \brief Read the source location entry with index ID, which will always be
3903d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// less than -1.
39101746745f1287effa1772ef51b973988afcea699Douglas Gregor  ///
39201746745f1287effa1772ef51b973988afcea699Douglas Gregor  /// \returns true if an error occurred that prevented the source-location
39301746745f1287effa1772ef51b973988afcea699Douglas Gregor  /// entry from being loaded.
3943d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  virtual bool ReadSLocEntry(int ID) = 0;
3953d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes};
3963d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
3973d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
3983d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes/// IsBeforeInTranslationUnitCache - This class holds the cache used by
3993d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes/// isBeforeInTranslationUnit.  The cache structure is complex enough to be
4003d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes/// worth breaking out of SourceManager.
4013d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentesclass IsBeforeInTranslationUnitCache {
4023d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// L/R QueryFID - These are the FID's of the cached query.  If these match up
4033d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// with a subsequent query, the result can be reused.
4043d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  FileID LQueryFID, RQueryFID;
4053d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
4063d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// \brief True if LQueryFID was created before RQueryFID. This is used
4073d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// to compare macro expansion locations.
4083d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  bool IsLQFIDBeforeRQFID;
4093d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
4103d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// CommonFID - This is the file found in common between the two #include
4113d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// traces.  It is the nearest common ancestor of the #include tree.
4123d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  FileID CommonFID;
4133d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
4143d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// L/R CommonOffset - This is the offset of the previous query in CommonFID.
4153d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// Usually, this represents the location of the #include for QueryFID, but if
4163d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// LQueryFID is a parent of RQueryFID (or vise versa) then these can be a
4173d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// random token in the parent.
4183d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  unsigned LCommonOffset, RCommonOffset;
4193d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentespublic:
4203d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
4213d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// isCacheValid - Return true if the currently cached values match up with
422071d73d67e7cea60e7334f6ae96c1e8f8050a662Douglas Gregor  /// the specified LHS/RHS query.  If not, we can't use the cache.
423071d73d67e7cea60e7334f6ae96c1e8f8050a662Douglas Gregor  bool isCacheValid(FileID LHS, FileID RHS) const {
424071d73d67e7cea60e7334f6ae96c1e8f8050a662Douglas Gregor    return LQueryFID == LHS && RQueryFID == RHS;
4253d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  }
4263d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
4273d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// getCachedResult - If the cache is valid, compute the result given the
4283d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// specified offsets in the LHS/RHS FID's.
4293d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  bool getCachedResult(unsigned LOffset, unsigned ROffset) const {
4303d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    // If one of the query files is the common file, use the offset.  Otherwise,
4313d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    // use the #include loc in the common file.
4323d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    if (LQueryFID != CommonFID) LOffset = LCommonOffset;
4333d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    if (RQueryFID != CommonFID) ROffset = RCommonOffset;
4343d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
435de98db33fb10a13ead2fa56d6d4c944cedb8fbadOscar Fuentes    // It is common for multiple macro expansions to be "included" from the same
4363d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    // location (expansion location), in which case use the order of the FileIDs
4373d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    // to determine which came first.
4383d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    if (LOffset == ROffset && LQueryFID != CommonFID && RQueryFID != CommonFID)
4393d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes      return IsLQFIDBeforeRQFID;
4403d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
4413d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    return LOffset < ROffset;
4423d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  }
4433d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
4443d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  // Set up a new query.
4453d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  void setQueryFIDs(FileID LHS, FileID RHS, bool isLFIDBeforeRFID) {
4463d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    assert(LHS != RHS);
4473d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    LQueryFID = LHS;
4483d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    RQueryFID = RHS;
449579e43a44ac118d432cb053470dbb977dfffe9dbOscar Fuentes    IsLQFIDBeforeRQFID = isLFIDBeforeRFID;
450579e43a44ac118d432cb053470dbb977dfffe9dbOscar Fuentes  }
451579e43a44ac118d432cb053470dbb977dfffe9dbOscar Fuentes
4523d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  void clear() {
4533d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    LQueryFID = RQueryFID = FileID();
4543d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    IsLQFIDBeforeRQFID = false;
4553d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  }
4563d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
4573d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  void setCommonLoc(FileID commonFID, unsigned lCommonOffset,
4583d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes                    unsigned rCommonOffset) {
4593d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    CommonFID = commonFID;
4603d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    LCommonOffset = lCommonOffset;
4613d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes    RCommonOffset = rCommonOffset;
4623d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  }
4633d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
4643d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes};
4653d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
4663d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes/// \brief This class handles loading and caching of source files into memory.
4673d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes///
4683d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes/// This object owns the MemoryBuffer objects for all of the loaded
4693d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes/// files and assigns unique FileID's for each unique #include chain.
4703d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes///
4713d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes/// The SourceManager can be queried for information about SourceLocation
4723d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes/// objects, turning them into either spelling or expansion locations. Spelling
4733d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes/// locations represent where the bytes corresponding to a token came from and
4743d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes/// expansion locations represent where the location is in the user's view. In
4753d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes/// the case of a macro expansion, for example, the spelling location indicates
4763d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes/// where the expanded token came from and the expansion location specifies
4773d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes/// where it was expanded.
4783d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentesclass SourceManager : public llvm::RefCountedBase<SourceManager> {
4793d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// \brief Diagnostic object.
4803d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  Diagnostic &Diag;
4813d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
4823d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  FileManager &FileMgr;
4833d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
4843d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  mutable llvm::BumpPtrAllocator ContentCacheAlloc;
4853d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
4863d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// FileInfos - Memoized information about all of the files tracked by this
4873d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// SourceManager.  This set allows us to merge ContentCache entries based
4883d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// on their FileEntry*.  All ContentCache objects will thus have unique,
489de98db33fb10a13ead2fa56d6d4c944cedb8fbadOscar Fuentes  /// non-null, FileEntry pointers.
4903d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos;
4913d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
4923d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// \brief True if the ContentCache for files that are overriden by other
4933d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// files, should report the original file name. Defaults to true.
4943d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  bool OverridenFilesKeepOriginalName;
4953d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
4963d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// \brief Files that have been overriden with the contents from another file.
497de98db33fb10a13ead2fa56d6d4c944cedb8fbadOscar Fuentes  llvm::DenseMap<const FileEntry *, const FileEntry *> OverriddenFiles;
4983d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
4993d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// MemBufferInfos - Information about various memory buffers that we have
5003d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// read in.  All FileEntry* within the stored ContentCache objects are NULL,
5013d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// as they do not refer to a file.
5023d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  std::vector<SrcMgr::ContentCache*> MemBufferInfos;
503de98db33fb10a13ead2fa56d6d4c944cedb8fbadOscar Fuentes
5043d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// \brief The table of SLocEntries that are local to this module.
5053d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  ///
5063d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// Positive FileIDs are indexes into this table. Entry 0 indicates an invalid
5073d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// expansion.
5083d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  std::vector<SrcMgr::SLocEntry> LocalSLocEntryTable;
5093d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
5103d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// \brief The table of SLocEntries that are loaded from other modules.
5113d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  ///
5123d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// Negative FileIDs are indexes into this table. To get from ID to an index,
5133d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// use (-ID - 2).
5143d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  std::vector<SrcMgr::SLocEntry> LoadedSLocEntryTable;
5153d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
5163d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// \brief The starting offset of the next local SLocEntry.
5173d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  ///
5183d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// This is LocalSLocEntryTable.back().Offset + the size of that entry.
5198c46e8573e2c8b3e1f048c68c61e2fc452703234Chris Lattner  unsigned NextLocalOffset;
5203d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
5213d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// \brief The starting offset of the latest batch of loaded SLocEntries.
5223d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  ///
5233d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// This is LoadedSLocEntryTable.back().Offset, except that that entry might
5243d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// not have been loaded, so that value would be unknown.
5253d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  unsigned CurrentLoadedOffset;
5263d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
5273d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// \brief The highest possible offset is 2^31-1, so CurrentLoadedOffset
5283d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// starts at 2^31.
5293d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  static const unsigned MaxLoadedOffset = 1U << 31U;
5303d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
5313d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// \brief A bitmap that indicates whether the entries of LoadedSLocEntryTable
5323d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// have already been loaded from the external source.
5333d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  ///
5343d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// Same indexing as LoadedSLocEntryTable.
5353d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  std::vector<bool> SLocEntryLoaded;
5363d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
5373d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// \brief An external source for source location entries.
5383d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  ExternalSLocEntrySource *ExternalSLocEntries;
5393d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
5403d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// LastFileIDLookup - This is a one-entry cache to speed up getFileID.
5413d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// LastFileIDLookup records the last FileID looked up or created, because it
5423d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// is very common to look up many tokens from the same file.
5433d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  mutable FileID LastFileIDLookup;
5443d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
5453d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// LineTable - This holds information for #line directives.  It is referenced
5463d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// by indices from SLocEntryTable.
5473d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  LineTableInfo *LineTable;
5483d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
5493d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// LastLineNo - These ivars serve as a cache used in the getLineNumber
5503d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// method which is used to speedup getLineNumber calls to nearby locations.
5513d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  mutable FileID LastLineNoFileIDQuery;
5523d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  mutable SrcMgr::ContentCache *LastLineNoContentCache;
5533d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  mutable unsigned LastLineNoFilePos;
5543d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  mutable unsigned LastLineNoResult;
5553d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
5563d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  /// MainFileID - The file ID for the main source file of the translation unit.
5573d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  FileID MainFileID;
5583d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
5593d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  // Statistics for -print-stats.
5603d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  mutable unsigned NumLinearScans, NumBinaryProbes;
561de98db33fb10a13ead2fa56d6d4c944cedb8fbadOscar Fuentes
5623d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  // Cache results for the isBeforeInTranslationUnit method.
56307b9d69ad1d491635dddab2ed59940c6efb72f22Cedric Venet  mutable IsBeforeInTranslationUnitCache IsBeforeInTUCache;
56407b9d69ad1d491635dddab2ed59940c6efb72f22Cedric Venet
56507b9d69ad1d491635dddab2ed59940c6efb72f22Cedric Venet  // Cache for the "fake" buffer used for error-recovery purposes.
5663d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  mutable llvm::MemoryBuffer *FakeBufferForRecovery;
5673d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes
5683d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  // SourceManager doesn't support copy construction.
5693d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  explicit SourceManager(const SourceManager&);
5703d01fc7de86c75926e4e5ac7cc49f0116018893dOscar Fuentes  void operator=(const SourceManager&);
571de98db33fb10a13ead2fa56d6d4c944cedb8fbadOscar Fuentespublic:
572de98db33fb10a13ead2fa56d6d4c944cedb8fbadOscar Fuentes  SourceManager(Diagnostic &Diag, FileManager &FileMgr);
573de98db33fb10a13ead2fa56d6d4c944cedb8fbadOscar Fuentes  ~SourceManager();
574de98db33fb10a13ead2fa56d6d4c944cedb8fbadOscar Fuentes
575de98db33fb10a13ead2fa56d6d4c944cedb8fbadOscar Fuentes  void clearIDTables();
576de98db33fb10a13ead2fa56d6d4c944cedb8fbadOscar Fuentes
577de98db33fb10a13ead2fa56d6d4c944cedb8fbadOscar Fuentes  Diagnostic &getDiagnostics() const { return Diag; }
578de98db33fb10a13ead2fa56d6d4c944cedb8fbadOscar Fuentes
579de98db33fb10a13ead2fa56d6d4c944cedb8fbadOscar Fuentes  FileManager &getFileManager() const { return FileMgr; }
580de98db33fb10a13ead2fa56d6d4c944cedb8fbadOscar Fuentes
581de98db33fb10a13ead2fa56d6d4c944cedb8fbadOscar Fuentes  /// \brief Set true if the SourceManager should report the original file name
582de98db33fb10a13ead2fa56d6d4c944cedb8fbadOscar Fuentes  /// for contents of files that were overriden by other files.Defaults to true.
583  void setOverridenFilesKeepOriginalName(bool value) {
584    OverridenFilesKeepOriginalName = value;
585  }
586
587  /// createMainFileIDForMembuffer - Create the FileID for a memory buffer
588  ///  that will represent the FileID for the main source.  One example
589  ///  of when this would be used is when the main source is read from STDIN.
590  FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) {
591    assert(MainFileID.isInvalid() && "MainFileID already set!");
592    MainFileID = createFileIDForMemBuffer(Buffer);
593    return MainFileID;
594  }
595
596  //===--------------------------------------------------------------------===//
597  // MainFileID creation and querying methods.
598  //===--------------------------------------------------------------------===//
599
600  /// getMainFileID - Returns the FileID of the main source file.
601  FileID getMainFileID() const { return MainFileID; }
602
603  /// createMainFileID - Create the FileID for the main source file.
604  FileID createMainFileID(const FileEntry *SourceFile) {
605    assert(MainFileID.isInvalid() && "MainFileID already set!");
606    MainFileID = createFileID(SourceFile, SourceLocation(), SrcMgr::C_User);
607    return MainFileID;
608  }
609
610  /// \brief Set the file ID for the precompiled preamble, which is also the
611  /// main file.
612  void SetPreambleFileID(FileID Preamble) {
613    assert(MainFileID.isInvalid() && "MainFileID already set!");
614    MainFileID = Preamble;
615  }
616
617  //===--------------------------------------------------------------------===//
618  // Methods to create new FileID's and macro expansions.
619  //===--------------------------------------------------------------------===//
620
621  /// createFileID - Create a new FileID that represents the specified file
622  /// being #included from the specified IncludePosition.  This translates NULL
623  /// into standard input.
624  FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
625                      SrcMgr::CharacteristicKind FileCharacter,
626                      int LoadedID = 0, unsigned LoadedOffset = 0) {
627    const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile);
628    assert(IR && "getOrCreateContentCache() cannot return NULL");
629    return createFileID(IR, IncludePos, FileCharacter, LoadedID, LoadedOffset);
630  }
631
632  /// createFileIDForMemBuffer - Create a new FileID that represents the
633  /// specified memory buffer.  This does no caching of the buffer and takes
634  /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once.
635  FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer,
636                                  int LoadedID = 0, unsigned LoadedOffset = 0) {
637    return createFileID(createMemBufferContentCache(Buffer), SourceLocation(),
638                        SrcMgr::C_User, LoadedID, LoadedOffset);
639  }
640
641  /// createMacroArgExpansionLoc - Return a new SourceLocation that encodes the
642  /// fact that a token from SpellingLoc should actually be referenced from
643  /// ExpansionLoc, and that it represents the expansion of a macro argument
644  /// into the function-like macro body.
645  SourceLocation createMacroArgExpansionLoc(SourceLocation Loc,
646                                            SourceLocation ExpansionLoc,
647                                            unsigned TokLength);
648
649  /// createExpansionLoc - Return a new SourceLocation that encodes the fact
650  /// that a token from SpellingLoc should actually be referenced from
651  /// ExpansionLoc.
652  SourceLocation createExpansionLoc(SourceLocation Loc,
653                                    SourceLocation ExpansionLocStart,
654                                    SourceLocation ExpansionLocEnd,
655                                    unsigned TokLength,
656                                    int LoadedID = 0,
657                                    unsigned LoadedOffset = 0);
658
659  /// \brief Retrieve the memory buffer associated with the given file.
660  ///
661  /// \param Invalid If non-NULL, will be set \c true if an error
662  /// occurs while retrieving the memory buffer.
663  const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File,
664                                                   bool *Invalid = 0);
665
666  /// \brief Override the contents of the given source file by providing an
667  /// already-allocated buffer.
668  ///
669  /// \param SourceFile the source file whose contents will be overriden.
670  ///
671  /// \param Buffer the memory buffer whose contents will be used as the
672  /// data in the given source file.
673  ///
674  /// \param DoNotFree If true, then the buffer will not be freed when the
675  /// source manager is destroyed.
676  void overrideFileContents(const FileEntry *SourceFile,
677                            const llvm::MemoryBuffer *Buffer,
678                            bool DoNotFree = false);
679
680  /// \brief Override the the given source file with another one.
681  ///
682  /// \param SourceFile the source file which will be overriden.
683  ///
684  /// \param NewFile the file whose contents will be used as the
685  /// data instead of the contents of the given source file.
686  void overrideFileContents(const FileEntry *SourceFile,
687                            const FileEntry *NewFile);
688
689  //===--------------------------------------------------------------------===//
690  // FileID manipulation methods.
691  //===--------------------------------------------------------------------===//
692
693  /// getBuffer - Return the buffer for the specified FileID. If there is an
694  /// error opening this buffer the first time, this manufactures a temporary
695  /// buffer and returns a non-empty error string.
696  const llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc,
697                                      bool *Invalid = 0) const {
698    bool MyInvalid = false;
699    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
700    if (MyInvalid || !Entry.isFile()) {
701      if (Invalid)
702        *Invalid = true;
703
704      return getFakeBufferForRecovery();
705    }
706
707    return Entry.getFile().getContentCache()->getBuffer(Diag, *this, Loc,
708                                                        Invalid);
709  }
710
711  const llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = 0) const {
712    bool MyInvalid = false;
713    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
714    if (MyInvalid || !Entry.isFile()) {
715      if (Invalid)
716        *Invalid = true;
717
718      return getFakeBufferForRecovery();
719    }
720
721    return Entry.getFile().getContentCache()->getBuffer(Diag, *this,
722                                                        SourceLocation(),
723                                                        Invalid);
724  }
725
726  /// getFileEntryForID - Returns the FileEntry record for the provided FileID.
727  const FileEntry *getFileEntryForID(FileID FID) const {
728    bool MyInvalid = false;
729    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
730    if (MyInvalid || !Entry.isFile())
731      return 0;
732
733    return Entry.getFile().getContentCache()->OrigEntry;
734  }
735
736  /// Returns the FileEntry record for the provided SLocEntry.
737  const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const
738  {
739    return sloc.getFile().getContentCache()->OrigEntry;
740  }
741
742  /// getBufferData - Return a StringRef to the source buffer data for the
743  /// specified FileID.
744  ///
745  /// \param FID The file ID whose contents will be returned.
746  /// \param Invalid If non-NULL, will be set true if an error occurred.
747  StringRef getBufferData(FileID FID, bool *Invalid = 0) const;
748
749  /// \brief Get the number of FileIDs (files and macros) that were created
750  /// during preprocessing of \arg FID, including it.
751  unsigned getNumCreatedFIDsForFileID(FileID FID) const {
752    bool Invalid = false;
753    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
754    if (Invalid || !Entry.isFile())
755      return 0;
756
757    return Entry.getFile().NumCreatedFIDs;
758  }
759
760  /// \brief Set the number of FileIDs (files and macros) that were created
761  /// during preprocessing of \arg FID, including it.
762  void setNumCreatedFIDsForFileID(FileID FID, unsigned NumFIDs) const {
763    bool Invalid = false;
764    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
765    if (Invalid || !Entry.isFile())
766      return;
767
768    assert(Entry.getFile().NumCreatedFIDs == 0 && "Already set!");
769    const_cast<SrcMgr::FileInfo &>(Entry.getFile()).NumCreatedFIDs = NumFIDs;
770  }
771
772  //===--------------------------------------------------------------------===//
773  // SourceLocation manipulation methods.
774  //===--------------------------------------------------------------------===//
775
776  /// getFileID - Return the FileID for a SourceLocation.  This is a very
777  /// hot method that is used for all SourceManager queries that start with a
778  /// SourceLocation object.  It is responsible for finding the entry in
779  /// SLocEntryTable which contains the specified location.
780  ///
781  FileID getFileID(SourceLocation SpellingLoc) const {
782    unsigned SLocOffset = SpellingLoc.getOffset();
783
784    // If our one-entry cache covers this offset, just return it.
785    if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
786      return LastFileIDLookup;
787
788    return getFileIDSlow(SLocOffset);
789  }
790
791  /// getLocForStartOfFile - Return the source location corresponding to the
792  /// first byte of the specified file.
793  SourceLocation getLocForStartOfFile(FileID FID) const {
794    bool Invalid = false;
795    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
796    if (Invalid || !Entry.isFile())
797      return SourceLocation();
798
799    unsigned FileOffset = Entry.getOffset();
800    return SourceLocation::getFileLoc(FileOffset);
801  }
802
803  /// \brief Returns the include location if \arg FID is a #include'd file
804  /// otherwise it returns an invalid location.
805  SourceLocation getIncludeLoc(FileID FID) const {
806    bool Invalid = false;
807    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
808    if (Invalid || !Entry.isFile())
809      return SourceLocation();
810
811    return Entry.getFile().getIncludeLoc();
812  }
813
814  /// getExpansionLoc - Given a SourceLocation object, return the expansion
815  /// location referenced by the ID.
816  SourceLocation getExpansionLoc(SourceLocation Loc) const {
817    // Handle the non-mapped case inline, defer to out of line code to handle
818    // expansions.
819    if (Loc.isFileID()) return Loc;
820    return getExpansionLocSlowCase(Loc);
821  }
822
823  /// getImmediateExpansionRange - Loc is required to be an expansion location.
824  /// Return the start/end of the expansion information.
825  std::pair<SourceLocation,SourceLocation>
826  getImmediateExpansionRange(SourceLocation Loc) const;
827
828  /// getExpansionRange - Given a SourceLocation object, return the range of
829  /// tokens covered by the expansion the ultimate file.
830  std::pair<SourceLocation,SourceLocation>
831  getExpansionRange(SourceLocation Loc) const;
832
833
834  /// getSpellingLoc - Given a SourceLocation object, return the spelling
835  /// location referenced by the ID.  This is the place where the characters
836  /// that make up the lexed token can be found.
837  SourceLocation getSpellingLoc(SourceLocation Loc) const {
838    // Handle the non-mapped case inline, defer to out of line code to handle
839    // expansions.
840    if (Loc.isFileID()) return Loc;
841    return getSpellingLocSlowCase(Loc);
842  }
843
844  /// getImmediateSpellingLoc - Given a SourceLocation object, return the
845  /// spelling location referenced by the ID.  This is the first level down
846  /// towards the place where the characters that make up the lexed token can be
847  /// found.  This should not generally be used by clients.
848  SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const;
849
850  /// getDecomposedLoc - Decompose the specified location into a raw FileID +
851  /// Offset pair.  The first element is the FileID, the second is the
852  /// offset from the start of the buffer of the location.
853  std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const {
854    FileID FID = getFileID(Loc);
855    return std::make_pair(FID, Loc.getOffset()-getSLocEntry(FID).getOffset());
856  }
857
858  /// getDecomposedExpansionLoc - Decompose the specified location into a raw
859  /// FileID + Offset pair. If the location is an expansion record, walk
860  /// through it until we find the final location expanded.
861  std::pair<FileID, unsigned>
862  getDecomposedExpansionLoc(SourceLocation Loc) const {
863    FileID FID = getFileID(Loc);
864    const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
865
866    unsigned Offset = Loc.getOffset()-E->getOffset();
867    if (Loc.isFileID())
868      return std::make_pair(FID, Offset);
869
870    return getDecomposedExpansionLocSlowCase(E);
871  }
872
873  /// getDecomposedSpellingLoc - Decompose the specified location into a raw
874  /// FileID + Offset pair.  If the location is an expansion record, walk
875  /// through it until we find its spelling record.
876  std::pair<FileID, unsigned>
877  getDecomposedSpellingLoc(SourceLocation Loc) const {
878    FileID FID = getFileID(Loc);
879    const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
880
881    unsigned Offset = Loc.getOffset()-E->getOffset();
882    if (Loc.isFileID())
883      return std::make_pair(FID, Offset);
884    return getDecomposedSpellingLocSlowCase(E, Offset);
885  }
886
887  /// getFileOffset - This method returns the offset from the start
888  /// of the file that the specified SourceLocation represents. This is not very
889  /// meaningful for a macro ID.
890  unsigned getFileOffset(SourceLocation SpellingLoc) const {
891    return getDecomposedLoc(SpellingLoc).second;
892  }
893
894  /// isMacroArgExpansion - This method tests whether the given source location
895  /// represents a macro argument's expansion into the function-like macro
896  /// definition. Such source locations only appear inside of the expansion
897  /// locations representing where a particular function-like macro was
898  /// expanded.
899  bool isMacroArgExpansion(SourceLocation Loc) const;
900
901  /// \brief Returns true if \arg Loc is inside the [\arg Start, +\arg Length)
902  /// chunk of the source location address space.
903  /// If it's true and \arg RelativeOffset is non-null, it will be set to the
904  /// relative offset of \arg Loc inside the chunk.
905  bool isInSLocAddrSpace(SourceLocation Loc,
906                         SourceLocation Start, unsigned Length,
907                         unsigned *RelativeOffset = 0) const {
908    assert(((Start.getOffset() < NextLocalOffset &&
909               Start.getOffset()+Length <= NextLocalOffset) ||
910            (Start.getOffset() >= CurrentLoadedOffset &&
911                Start.getOffset()+Length < MaxLoadedOffset)) &&
912           "Chunk is not valid SLoc address space");
913    unsigned LocOffs = Loc.getOffset();
914    unsigned BeginOffs = Start.getOffset();
915    unsigned EndOffs = BeginOffs + Length;
916    if (LocOffs >= BeginOffs && LocOffs < EndOffs) {
917      if (RelativeOffset)
918        *RelativeOffset = LocOffs - BeginOffs;
919      return true;
920    }
921
922    return false;
923  }
924
925  /// \brief Return true if both \arg LHS and \arg RHS are in the local source
926  /// location address space or the loaded one. If it's true and
927  /// \arg RelativeOffset is non-null, it will be set to the offset of \arg RHS
928  /// relative to \arg LHS.
929  bool isInSameSLocAddrSpace(SourceLocation LHS, SourceLocation RHS,
930                             int *RelativeOffset) const {
931    unsigned LHSOffs = LHS.getOffset(), RHSOffs = RHS.getOffset();
932    bool LHSLoaded = LHSOffs >= CurrentLoadedOffset;
933    bool RHSLoaded = RHSOffs >= CurrentLoadedOffset;
934
935    if (LHSLoaded == RHSLoaded) {
936      if (RelativeOffset)
937        *RelativeOffset = RHSOffs - LHSOffs;
938      return true;
939    }
940
941    return false;
942  }
943
944  //===--------------------------------------------------------------------===//
945  // Queries about the code at a SourceLocation.
946  //===--------------------------------------------------------------------===//
947
948  /// getCharacterData - Return a pointer to the start of the specified location
949  /// in the appropriate spelling MemoryBuffer.
950  ///
951  /// \param Invalid If non-NULL, will be set \c true if an error occurs.
952  const char *getCharacterData(SourceLocation SL, bool *Invalid = 0) const;
953
954  /// getColumnNumber - Return the column # for the specified file position.
955  /// This is significantly cheaper to compute than the line number.  This
956  /// returns zero if the column number isn't known.  This may only be called
957  /// on a file sloc, so you must choose a spelling or expansion location
958  /// before calling this method.
959  unsigned getColumnNumber(FileID FID, unsigned FilePos,
960                           bool *Invalid = 0) const;
961  unsigned getSpellingColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
962  unsigned getExpansionColumnNumber(SourceLocation Loc,
963                                    bool *Invalid = 0) const;
964  unsigned getPresumedColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
965
966
967  /// getLineNumber - Given a SourceLocation, return the spelling line number
968  /// for the position indicated.  This requires building and caching a table of
969  /// line offsets for the MemoryBuffer, so this is not cheap: use only when
970  /// about to emit a diagnostic.
971  unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = 0) const;
972  unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
973  unsigned getExpansionLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
974  unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
975
976  /// Return the filename or buffer identifier of the buffer the location is in.
977  /// Note that this name does not respect #line directives.  Use getPresumedLoc
978  /// for normal clients.
979  const char *getBufferName(SourceLocation Loc, bool *Invalid = 0) const;
980
981  /// getFileCharacteristic - return the file characteristic of the specified
982  /// source location, indicating whether this is a normal file, a system
983  /// header, or an "implicit extern C" system header.
984  ///
985  /// This state can be modified with flags on GNU linemarker directives like:
986  ///   # 4 "foo.h" 3
987  /// which changes all source locations in the current file after that to be
988  /// considered to be from a system header.
989  SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const;
990
991  /// getPresumedLoc - This method returns the "presumed" location of a
992  /// SourceLocation specifies.  A "presumed location" can be modified by #line
993  /// or GNU line marker directives.  This provides a view on the data that a
994  /// user should see in diagnostics, for example.
995  ///
996  /// Note that a presumed location is always given as the expansion point of
997  /// an expansion location, not at the spelling location.
998  ///
999  /// \returns The presumed location of the specified SourceLocation. If the
1000  /// presumed location cannot be calculate (e.g., because \p Loc is invalid
1001  /// or the file containing \p Loc has changed on disk), returns an invalid
1002  /// presumed location.
1003  PresumedLoc getPresumedLoc(SourceLocation Loc) const;
1004
1005  /// isFromSameFile - Returns true if both SourceLocations correspond to
1006  ///  the same file.
1007  bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
1008    return getFileID(Loc1) == getFileID(Loc2);
1009  }
1010
1011  /// isFromMainFile - Returns true if the file of provided SourceLocation is
1012  ///   the main file.
1013  bool isFromMainFile(SourceLocation Loc) const {
1014    return getFileID(Loc) == getMainFileID();
1015  }
1016
1017  /// isInSystemHeader - Returns if a SourceLocation is in a system header.
1018  bool isInSystemHeader(SourceLocation Loc) const {
1019    return getFileCharacteristic(Loc) != SrcMgr::C_User;
1020  }
1021
1022  /// isInExternCSystemHeader - Returns if a SourceLocation is in an "extern C"
1023  /// system header.
1024  bool isInExternCSystemHeader(SourceLocation Loc) const {
1025    return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem;
1026  }
1027
1028  /// \brief The size of the SLocEnty that \arg FID represents.
1029  unsigned getFileIDSize(FileID FID) const;
1030
1031  /// \brief Given a specific FileID, returns true if \arg Loc is inside that
1032  /// FileID chunk and sets relative offset (offset of \arg Loc from beginning
1033  /// of FileID) to \arg relativeOffset.
1034  bool isInFileID(SourceLocation Loc, FileID FID,
1035                  unsigned *RelativeOffset = 0) const {
1036    unsigned Offs = Loc.getOffset();
1037    if (isOffsetInFileID(FID, Offs)) {
1038      if (RelativeOffset)
1039        *RelativeOffset = Offs - getSLocEntry(FID).getOffset();
1040      return true;
1041    }
1042
1043    return false;
1044  }
1045
1046  //===--------------------------------------------------------------------===//
1047  // Line Table Manipulation Routines
1048  //===--------------------------------------------------------------------===//
1049
1050  /// getLineTableFilenameID - Return the uniqued ID for the specified filename.
1051  ///
1052  unsigned getLineTableFilenameID(StringRef Str);
1053
1054  /// AddLineNote - Add a line note to the line table for the FileID and offset
1055  /// specified by Loc.  If FilenameID is -1, it is considered to be
1056  /// unspecified.
1057  void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID);
1058  void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID,
1059                   bool IsFileEntry, bool IsFileExit,
1060                   bool IsSystemHeader, bool IsExternCHeader);
1061
1062  /// \brief Determine if the source manager has a line table.
1063  bool hasLineTable() const { return LineTable != 0; }
1064
1065  /// \brief Retrieve the stored line table.
1066  LineTableInfo &getLineTable();
1067
1068  //===--------------------------------------------------------------------===//
1069  // Queries for performance analysis.
1070  //===--------------------------------------------------------------------===//
1071
1072  /// Return the total amount of physical memory allocated by the
1073  /// ContentCache allocator.
1074  size_t getContentCacheSize() const {
1075    return ContentCacheAlloc.getTotalMemory();
1076  }
1077
1078  struct MemoryBufferSizes {
1079    const size_t malloc_bytes;
1080    const size_t mmap_bytes;
1081
1082    MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)
1083      : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {}
1084  };
1085
1086  /// Return the amount of memory used by memory buffers, breaking down
1087  /// by heap-backed versus mmap'ed memory.
1088  MemoryBufferSizes getMemoryBufferSizes() const;
1089
1090  // Return the amount of memory used for various side tables and
1091  // data structures in the SourceManager.
1092  size_t getDataStructureSizes() const;
1093
1094  //===--------------------------------------------------------------------===//
1095  // Other miscellaneous methods.
1096  //===--------------------------------------------------------------------===//
1097
1098  /// \brief Get the source location for the given file:line:col triplet.
1099  ///
1100  /// If the source file is included multiple times, the source location will
1101  /// be based upon the first inclusion.
1102  ///
1103  /// If the location points inside a function macro argument, the returned
1104  /// location will be the macro location in which the argument was expanded.
1105  /// \sa getMacroArgExpandedLocation
1106  SourceLocation getLocation(const FileEntry *SourceFile,
1107                             unsigned Line, unsigned Col) {
1108    SourceLocation Loc = translateFileLineCol(SourceFile, Line, Col);
1109    return getMacroArgExpandedLocation(Loc);
1110  }
1111
1112  /// \brief Get the source location for the given file:line:col triplet.
1113  ///
1114  /// If the source file is included multiple times, the source location will
1115  /// be based upon the first inclusion.
1116  SourceLocation translateFileLineCol(const FileEntry *SourceFile,
1117                                      unsigned Line, unsigned Col);
1118
1119  /// \brief If \arg Loc points inside a function macro argument, the returned
1120  /// location will be the macro location in which the argument was expanded.
1121  /// If a macro argument is used multiple times, the expanded location will
1122  /// be at the first expansion of the argument.
1123  /// e.g.
1124  ///   MY_MACRO(foo);
1125  ///             ^
1126  /// Passing a file location pointing at 'foo', will yield a macro location
1127  /// where 'foo' was expanded into.
1128  SourceLocation getMacroArgExpandedLocation(SourceLocation Loc);
1129
1130  /// \brief Determines the order of 2 source locations in the translation unit.
1131  ///
1132  /// \returns true if LHS source location comes before RHS, false otherwise.
1133  bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const;
1134
1135  /// \brief Comparison function class.
1136  class LocBeforeThanCompare : public std::binary_function<SourceLocation,
1137                                                         SourceLocation, bool> {
1138    SourceManager &SM;
1139
1140  public:
1141    explicit LocBeforeThanCompare(SourceManager &SM) : SM(SM) { }
1142
1143    bool operator()(SourceLocation LHS, SourceLocation RHS) const {
1144      return SM.isBeforeInTranslationUnit(LHS, RHS);
1145    }
1146  };
1147
1148  /// \brief Determines the order of 2 source locations in the "source location
1149  /// address space".
1150  bool isBeforeInSLocAddrSpace(SourceLocation LHS, SourceLocation RHS) const {
1151    return isBeforeInSLocAddrSpace(LHS, RHS.getOffset());
1152  }
1153
1154  /// \brief Determines the order of a source location and a source location
1155  /// offset in the "source location address space".
1156  ///
1157  /// Note that we always consider source locations loaded from
1158  bool isBeforeInSLocAddrSpace(SourceLocation LHS, unsigned RHS) const {
1159    unsigned LHSOffset = LHS.getOffset();
1160    bool LHSLoaded = LHSOffset >= CurrentLoadedOffset;
1161    bool RHSLoaded = RHS >= CurrentLoadedOffset;
1162    if (LHSLoaded == RHSLoaded)
1163      return LHSOffset < RHS;
1164
1165    return LHSLoaded;
1166  }
1167
1168  // Iterators over FileInfos.
1169  typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>
1170      ::const_iterator fileinfo_iterator;
1171  fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); }
1172  fileinfo_iterator fileinfo_end() const { return FileInfos.end(); }
1173  bool hasFileInfo(const FileEntry *File) const {
1174    return FileInfos.find(File) != FileInfos.end();
1175  }
1176
1177  /// PrintStats - Print statistics to stderr.
1178  ///
1179  void PrintStats() const;
1180
1181  /// \brief Get the number of local SLocEntries we have.
1182  unsigned local_sloc_entry_size() const { return LocalSLocEntryTable.size(); }
1183
1184  /// \brief Get a local SLocEntry. This is exposed for indexing.
1185  const SrcMgr::SLocEntry &getLocalSLocEntry(unsigned Index,
1186                                             bool *Invalid = 0) const {
1187    assert(Index < LocalSLocEntryTable.size() && "Invalid index");
1188    return LocalSLocEntryTable[Index];
1189  }
1190
1191  /// \brief Get the number of loaded SLocEntries we have.
1192  unsigned loaded_sloc_entry_size() const { return LoadedSLocEntryTable.size();}
1193
1194  /// \brief Get a loaded SLocEntry. This is exposed for indexing.
1195  const SrcMgr::SLocEntry &getLoadedSLocEntry(unsigned Index, bool *Invalid=0) const {
1196    assert(Index < LoadedSLocEntryTable.size() && "Invalid index");
1197    if (!SLocEntryLoaded[Index])
1198      ExternalSLocEntries->ReadSLocEntry(-(static_cast<int>(Index) + 2));
1199    return LoadedSLocEntryTable[Index];
1200  }
1201
1202  const SrcMgr::SLocEntry &getSLocEntry(FileID FID, bool *Invalid = 0) const {
1203    return getSLocEntryByID(FID.ID);
1204  }
1205
1206  unsigned getNextLocalOffset() const { return NextLocalOffset; }
1207
1208  void setExternalSLocEntrySource(ExternalSLocEntrySource *Source) {
1209    assert(LoadedSLocEntryTable.empty() &&
1210           "Invalidating existing loaded entries");
1211    ExternalSLocEntries = Source;
1212  }
1213
1214  /// \brief Allocate a number of loaded SLocEntries, which will be actually
1215  /// loaded on demand from the external source.
1216  ///
1217  /// NumSLocEntries will be allocated, which occupy a total of TotalSize space
1218  /// in the global source view. The lowest ID and the base offset of the
1219  /// entries will be returned.
1220  std::pair<int, unsigned>
1221  AllocateLoadedSLocEntries(unsigned NumSLocEntries, unsigned TotalSize);
1222
1223private:
1224  const llvm::MemoryBuffer *getFakeBufferForRecovery() const;
1225
1226  /// \brief Get the entry with the given unwrapped FileID.
1227  const SrcMgr::SLocEntry &getSLocEntryByID(int ID) const {
1228    assert(ID != -1 && "Using FileID sentinel value");
1229    if (ID < 0)
1230      return getLoadedSLocEntryByID(ID);
1231    return getLocalSLocEntry(static_cast<unsigned>(ID));
1232  }
1233
1234  const SrcMgr::SLocEntry &getLoadedSLocEntryByID(int ID) const {
1235    return getLoadedSLocEntry(static_cast<unsigned>(-ID - 2));
1236  }
1237
1238  /// createExpansionLoc - Implements the common elements of storing an
1239  /// expansion info struct into the SLocEntry table and producing a source
1240  /// location that refers to it.
1241  SourceLocation createExpansionLocImpl(const SrcMgr::ExpansionInfo &Expansion,
1242                                        unsigned TokLength,
1243                                        int LoadedID = 0,
1244                                        unsigned LoadedOffset = 0);
1245
1246  /// isOffsetInFileID - Return true if the specified FileID contains the
1247  /// specified SourceLocation offset.  This is a very hot method.
1248  inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const {
1249    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID);
1250    // If the entry is after the offset, it can't contain it.
1251    if (SLocOffset < Entry.getOffset()) return false;
1252
1253    // If this is the very last entry then it does.
1254    if (FID.ID == -2)
1255      return true;
1256
1257    // If it is the last local entry, then it does if the location is local.
1258    if (static_cast<unsigned>(FID.ID+1) == LocalSLocEntryTable.size()) {
1259      return SLocOffset < NextLocalOffset;
1260    }
1261
1262    // Otherwise, the entry after it has to not include it. This works for both
1263    // local and loaded entries.
1264    return SLocOffset < getSLocEntry(FileID::get(FID.ID+1)).getOffset();
1265  }
1266
1267  /// createFileID - Create a new fileID for the specified ContentCache and
1268  ///  include position.  This works regardless of whether the ContentCache
1269  ///  corresponds to a file or some other input source.
1270  FileID createFileID(const SrcMgr::ContentCache* File,
1271                      SourceLocation IncludePos,
1272                      SrcMgr::CharacteristicKind DirCharacter,
1273                      int LoadedID, unsigned LoadedOffset);
1274
1275  const SrcMgr::ContentCache *
1276    getOrCreateContentCache(const FileEntry *SourceFile);
1277
1278  /// createMemBufferContentCache - Create a new ContentCache for the specified
1279  ///  memory buffer.
1280  const SrcMgr::ContentCache*
1281  createMemBufferContentCache(const llvm::MemoryBuffer *Buf);
1282
1283  FileID getFileIDSlow(unsigned SLocOffset) const;
1284  FileID getFileIDLocal(unsigned SLocOffset) const;
1285  FileID getFileIDLoaded(unsigned SLocOffset) const;
1286
1287  SourceLocation getExpansionLocSlowCase(SourceLocation Loc) const;
1288  SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;
1289
1290  std::pair<FileID, unsigned>
1291  getDecomposedExpansionLocSlowCase(const SrcMgr::SLocEntry *E) const;
1292  std::pair<FileID, unsigned>
1293  getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
1294                                   unsigned Offset) const;
1295  void computeMacroArgsCache(SrcMgr::ContentCache *Content, FileID FID);
1296};
1297
1298
1299}  // end namespace clang
1300
1301#endif
1302