SourceManager.h revision c351d9837ea1d0b04842497e76c76125426a982c
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- SourceManager.h - Track and cache source files ---------*- C++ -*-===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file defines the SourceManager interface.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifndef LLVM_CLANG_SOURCEMANAGER_H
155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#define LLVM_CLANG_SOURCEMANAGER_H
165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
17d47d3b0cfeb7e8564ff77f48130fe63282b6d127Chris Lattner#include "clang/Basic/LLVM.h"
185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/SourceLocation.h"
190d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner#include "llvm/Support/Allocator.h"
2003013fa9a0bf1ef4b907f5fec006c8f4000fdd21Michael J. Spencer#include "llvm/Support/DataTypes.h"
21c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor#include "llvm/ADT/PointerIntPair.h"
22aea67dbd653a2dd6dd5cc2159279e81e855b2482Douglas Gregor#include "llvm/ADT/PointerUnion.h"
234f32786ac45210143654390177105eb749b614e9Ted Kremenek#include "llvm/ADT/IntrusiveRefCntPtr.h"
24d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis#include "llvm/ADT/OwningPtr.h"
250d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner#include "llvm/ADT/DenseMap.h"
26d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis#include "llvm/ADT/DenseSet.h"
27f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek#include "llvm/Support/MemoryBuffer.h"
28d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis#include <map>
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include <vector>
309dc62f044a6ba21f503bd56607d94b32704e7945Chris Lattner#include <cassert>
315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace clang {
331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikieclass DiagnosticsEngine;
355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass SourceManager;
36099b4747042352f69184481a48508b599a8d3f73Ted Kremenekclass FileManager;
375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass FileEntry;
385b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattnerclass LineTableInfo;
39b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidisclass LangOptions;
40d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidisclass ASTWriter;
41d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidisclass ASTReader;
425330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
43c351d9837ea1d0b04842497e76c76125426a982cJames Dennett/// \file
4429f39425fc7fcaede88a7e573f392975f8dc5e3eEric Christopher/// There are three different types of locations in a file: a spelling
4529f39425fc7fcaede88a7e573f392975f8dc5e3eEric Christopher/// location, an expansion location, and a presumed location.
4629f39425fc7fcaede88a7e573f392975f8dc5e3eEric Christopher///
4729f39425fc7fcaede88a7e573f392975f8dc5e3eEric Christopher/// Given an example of:
48c351d9837ea1d0b04842497e76c76125426a982cJames Dennett/// \code
4929f39425fc7fcaede88a7e573f392975f8dc5e3eEric Christopher/// #define min(x, y) x < y ? x : y
50c351d9837ea1d0b04842497e76c76125426a982cJames Dennett/// \endcode
5129f39425fc7fcaede88a7e573f392975f8dc5e3eEric Christopher///
5229f39425fc7fcaede88a7e573f392975f8dc5e3eEric Christopher/// and then later on a use of min:
53c351d9837ea1d0b04842497e76c76125426a982cJames Dennett/// \code
5429f39425fc7fcaede88a7e573f392975f8dc5e3eEric Christopher/// #line 17
555330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher/// return min(a, b);
56c351d9837ea1d0b04842497e76c76125426a982cJames Dennett/// \endcode
5729f39425fc7fcaede88a7e573f392975f8dc5e3eEric Christopher///
5829f39425fc7fcaede88a7e573f392975f8dc5e3eEric Christopher/// The expansion location is the line in the source code where the macro
5929f39425fc7fcaede88a7e573f392975f8dc5e3eEric Christopher/// was expanded (the return statement), the spelling location is the
6029f39425fc7fcaede88a7e573f392975f8dc5e3eEric Christopher/// location in the source where the macro was originally defined,
6129f39425fc7fcaede88a7e573f392975f8dc5e3eEric Christopher/// and the presumed location is where the line directive states that
6229f39425fc7fcaede88a7e573f392975f8dc5e3eEric Christopher/// the line is 17, or any other line.
6329f39425fc7fcaede88a7e573f392975f8dc5e3eEric Christopher
64c351d9837ea1d0b04842497e76c76125426a982cJames Dennett/// \namespace SrcMgr
65c351d9837ea1d0b04842497e76c76125426a982cJames Dennett/// \brief Public enums and private classes that are part of the
660b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner/// SourceManager implementation.
675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace SrcMgr {
699d72851fec9e9c62570a027d42701562bbf29751Chris Lattner  /// CharacteristicKind - This is used to represent whether a file or directory
700b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner  /// holds normal user code, system code, or system code which is implicitly
710b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner  /// 'extern "C"' in C++ mode.  Entire directories can be tagged with this
720b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner  /// (this is maintained by DirectoryLookup and friends) as can specific
73c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  /// FileInfos when a \#pragma system_header is seen or various other cases.
740b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner  ///
759d72851fec9e9c62570a027d42701562bbf29751Chris Lattner  enum CharacteristicKind {
760b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner    C_User, C_System, C_ExternCSystem
770b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner  };
781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
794710a8ea766b45079bf1c1dd36e29b59bb90829dDan Gohman  /// ContentCache - One instance of this struct is kept for every file
8006a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  /// loaded or used.  This object owns the MemoryBuffer object.
81c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek  class ContentCache {
82f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    enum CCFlags {
83f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor      /// \brief Whether the buffer is invalid.
84f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor      InvalidFlag = 0x01,
85f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor      /// \brief Whether the buffer should not be freed on destruction.
86f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor      DoNotFreeFlag = 0x02
87f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    };
885330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
89c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    /// Buffer - The actual buffer containing the characters from the input
90c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    /// file.  This is owned by the ContentCache object.
91f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    /// The bits indicate indicates whether the buffer is invalid.
92f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    mutable llvm::PointerIntPair<const llvm::MemoryBuffer *, 2> Buffer;
93c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek
94c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek  public:
95b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    /// Reference to the file entry representing this ContentCache.
96b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    /// This reference does not own the FileEntry object.
97b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    /// It is possible for this to be NULL if
9878d85f53b093867bbb0123f016956178eea7343eTed Kremenek    /// the ContentCache encapsulates an imaginary text buffer.
99b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    const FileEntry *OrigEntry;
100b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis
101b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    /// \brief References the file which the contents were actually loaded from.
102b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    /// Can be different from 'Entry' if we overridden the contents of one file
103b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    /// with the contents of another file.
104b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    const FileEntry *ContentsEntry;
1051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1060d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner    /// SourceLineCache - A bump pointer allocated array of offsets for each
1070d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner    /// source line.  This is lazily computed.  This is owned by the
1080d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner    /// SourceManager BumpPointerAllocator object.
10905816591ec488a933dfecc9ff9f3cbf3c32767c2Chris Lattner    unsigned *SourceLineCache;
1101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
111b6427f821de8cce1566fb6e755143ea0918d5543Ted Kremenek    /// NumLines - The number of lines in this ContentCache.  This is only valid
112b6427f821de8cce1566fb6e755143ea0918d5543Ted Kremenek    /// if SourceLineCache is non-null.
113a081da5e44600d02983d6562bed1b4fd61e410fdDouglas Gregor    unsigned NumLines : 31;
11410b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis
115a081da5e44600d02983d6562bed1b4fd61e410fdDouglas Gregor    /// \brief Indicates whether the buffer itself was provided to override
116a081da5e44600d02983d6562bed1b4fd61e410fdDouglas Gregor    /// the actual file contents.
117a081da5e44600d02983d6562bed1b4fd61e410fdDouglas Gregor    ///
118a081da5e44600d02983d6562bed1b4fd61e410fdDouglas Gregor    /// When true, the original entry may be a virtual file that does not
119a081da5e44600d02983d6562bed1b4fd61e410fdDouglas Gregor    /// exist.
120a081da5e44600d02983d6562bed1b4fd61e410fdDouglas Gregor    unsigned BufferOverridden : 1;
121a081da5e44600d02983d6562bed1b4fd61e410fdDouglas Gregor
1227955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor    ContentCache(const FileEntry *Ent = 0)
1237955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor      : Buffer(0, false), OrigEntry(Ent), ContentsEntry(Ent),
1247955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor        SourceLineCache(0), NumLines(0), BufferOverridden(false) {}
1257955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor
1267955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor    ContentCache(const FileEntry *Ent, const FileEntry *contentEnt)
1277955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor      : Buffer(0, false), OrigEntry(Ent), ContentsEntry(contentEnt),
1287955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor        SourceLineCache(0), NumLines(0), BufferOverridden(false) {}
1297955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor
1307955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor    ~ContentCache();
1317955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor
1327955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor    /// The copy ctor does not allow copies where source object has either
1337955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor    ///  a non-NULL Buffer or SourceLineCache.  Ownership of allocated memory
1347955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor    ///  is not transferred, so this is a logical error.
1357955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor    ContentCache(const ContentCache &RHS)
1367955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor      : Buffer(0, false), SourceLineCache(0), BufferOverridden(false)
1377955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor    {
1387955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor      OrigEntry = RHS.OrigEntry;
1397955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor      ContentsEntry = RHS.ContentsEntry;
1407955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor
1417955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor      assert (RHS.Buffer.getPointer() == 0 && RHS.SourceLineCache == 0 &&
1427955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor              "Passed ContentCache object cannot own a buffer.");
1437955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor
1447955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor      NumLines = RHS.NumLines;
1457955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor    }
1467955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor
14736c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor    /// getBuffer - Returns the memory buffer for the associated content.
14836c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor    ///
149a92d7e7a55a35b28437103130904a6401bf35408Jonathan D. Turner    /// \param Diag Object through which diagnostics will be emitted if the
15036c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor    /// buffer cannot be retrieved.
1515330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher    ///
152e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner    /// \param Loc If specified, is the location that invalid file diagnostics
153e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner    ///     will be emitted at.
154e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner    ///
15536c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor    /// \param Invalid If non-NULL, will be set \c true if an error occurred.
156d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie    const llvm::MemoryBuffer *getBuffer(DiagnosticsEngine &Diag,
157e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner                                        const SourceManager &SM,
158e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner                                        SourceLocation Loc = SourceLocation(),
15936c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor                                        bool *Invalid = 0) const;
1601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
161c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    /// getSize - Returns the size of the content encapsulated by this
162c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    ///  ContentCache. This can be the size of the source file or the size of an
163c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    ///  arbitrary scratch buffer.  If the ContentCache encapsulates a source
164c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    ///  file this size is retrieved from the file's FileEntry.
165c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    unsigned getSize() const;
1661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
167c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    /// getSizeBytesMapped - Returns the number of bytes actually mapped for
1683201f382956ed9beee9fb31229c2835c1208889cChandler Carruth    /// this ContentCache. This can be 0 if the MemBuffer was not actually
1693201f382956ed9beee9fb31229c2835c1208889cChandler Carruth    /// expanded.
170c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    unsigned getSizeBytesMapped() const;
1715330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
172f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek    /// Returns the kind of memory used to back the memory buffer for
173f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek    /// this content cache.  This is used for performance analysis.
174f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek    llvm::MemoryBuffer::BufferKind getMemoryBufferKind() const;
1751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17605816591ec488a933dfecc9ff9f3cbf3c32767c2Chris Lattner    void setBuffer(const llvm::MemoryBuffer *B) {
177c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor      assert(!Buffer.getPointer() && "MemoryBuffer already set.");
178c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor      Buffer.setPointer(B);
179c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor      Buffer.setInt(false);
180c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    }
1815330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
182cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor    /// \brief Get the underlying buffer, returning NULL if the buffer is not
183cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor    /// yet available.
184cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor    const llvm::MemoryBuffer *getRawBuffer() const {
185cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor      return Buffer.getPointer();
186cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor    }
1871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1882968442603b029949246467253eeac8139a5b6d8Douglas Gregor    /// \brief Replace the existing buffer (which will be deleted)
1892968442603b029949246467253eeac8139a5b6d8Douglas Gregor    /// with the given buffer.
190f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    void replaceBuffer(const llvm::MemoryBuffer *B, bool DoNotFree = false);
1912968442603b029949246467253eeac8139a5b6d8Douglas Gregor
192f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    /// \brief Determine whether the buffer itself is invalid.
193f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    bool isBufferInvalid() const {
194f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor      return Buffer.getInt() & InvalidFlag;
195f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    }
1965330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
197f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    /// \brief Determine whether the buffer should be freed.
198f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    bool shouldFreeBuffer() const {
199f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor      return (Buffer.getInt() & DoNotFreeFlag) == 0;
200f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    }
2015330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
2020d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek  private:
2030d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek    // Disable assignments.
2041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    ContentCache &operator=(const ContentCache& RHS);
2051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  };
2065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
207de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// FileInfo - Information about a FileID, basically just the logical file
208de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// that it represents and include stack information.
2095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
210de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// Each FileInfo has include stack information, indicating where it came
211c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  /// from. This information encodes the \#include chain that a token was
2123201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// expanded from. The main include file has an invalid IncludeLoc.
2135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
214de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// FileInfos contain a "ContentCache *", with the contents of the file.
2155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
216de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  class FileInfo {
217c351d9837ea1d0b04842497e76c76125426a982cJames Dennett    /// IncludeLoc - The location of the \#include that brought in this file.
218c351d9837ea1d0b04842497e76c76125426a982cJames Dennett    /// This is an invalid SLOC for the main file (top of the \#include chain).
219de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned IncludeLoc;  // Really a SourceLocation
2201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
221d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    /// \brief Number of FileIDs (files and macros) that were created during
222c351d9837ea1d0b04842497e76c76125426a982cJames Dennett    /// preprocessing of this \#include, including this SLocEntry.
223d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    /// Zero means the preprocessor didn't provide such info for this SLocEntry.
224d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    unsigned NumCreatedFIDs;
225d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis
226c351d9837ea1d0b04842497e76c76125426a982cJames Dennett    /// \brief Contains the ContentCache* and the bits indicating the
227c351d9837ea1d0b04842497e76c76125426a982cJames Dennett    /// characteristic of the file and whether it has \#line info, all
228c351d9837ea1d0b04842497e76c76125426a982cJames Dennett    /// bitmangled together.
2296e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    uintptr_t Data;
230d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis
23121032df7a0dfc129a8f0c5e004811b455baafb7aArgyrios Kyrtzidis    friend class clang::SourceManager;
23221032df7a0dfc129a8f0c5e004811b455baafb7aArgyrios Kyrtzidis    friend class clang::ASTWriter;
23321032df7a0dfc129a8f0c5e004811b455baafb7aArgyrios Kyrtzidis    friend class clang::ASTReader;
23478d85f53b093867bbb0123f016956178eea7343eTed Kremenek  public:
235de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    /// get - Return a FileInfo object.
236de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    static FileInfo get(SourceLocation IL, const ContentCache *Con,
237de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner                        CharacteristicKind FileCharacter) {
238de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      FileInfo X;
239de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      X.IncludeLoc = IL.getRawEncoding();
240d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis      X.NumCreatedFIDs = 0;
2416e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner      X.Data = (uintptr_t)Con;
24200282d6e1194655a2e89f940bd6fa8484b52e666Chris Lattner      assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned");
2436e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner      assert((unsigned)FileCharacter < 4 && "invalid file character");
2446e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner      X.Data |= (unsigned)FileCharacter;
2455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return X;
2465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
2471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
248de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    SourceLocation getIncludeLoc() const {
249de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return SourceLocation::getFromRawEncoding(IncludeLoc);
250de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
2516e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    const ContentCache* getContentCache() const {
25200282d6e1194655a2e89f940bd6fa8484b52e666Chris Lattner      return reinterpret_cast<const ContentCache*>(Data & ~7UL);
2536e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    }
2541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
255c351d9837ea1d0b04842497e76c76125426a982cJames Dennett    /// \brief Return whether this is a system header or not.
2561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    CharacteristicKind getFileCharacteristic() const {
2576e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner      return (CharacteristicKind)(Data & 3);
2580b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner    }
259ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner
260c351d9837ea1d0b04842497e76c76125426a982cJames Dennett    /// \brief Return true if this FileID has \#line directives in it.
261ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    bool hasLineDirectives() const { return (Data & 4) != 0; }
2621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
263c351d9837ea1d0b04842497e76c76125426a982cJames Dennett    /// \brief Set the flag that indicates that this FileID has
264ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    /// line table entries associated with it.
265ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    void setHasLineDirectives() {
266ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner      Data |= 4;
267ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    }
2689dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  };
2691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27078df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth  /// ExpansionInfo - Each ExpansionInfo encodes the expansion location - where
27178df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth  /// the token was ultimately expanded, and the SpellingLoc - where the actual
27278df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth  /// character data for the token came from.
27378df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth  class ExpansionInfo {
27478df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    // Really these are all SourceLocations.
2751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
276e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    /// SpellingLoc - Where the spelling for the token can be found.
277e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    unsigned SpellingLoc;
2781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27978df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// ExpansionLocStart/ExpansionLocEnd - In a macro expansion, these
28078df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// indicate the start and end of the expansion. In object-like macros,
2813201f382956ed9beee9fb31229c2835c1208889cChandler Carruth    /// these will be the same. In a function-like macro expansion, the start
2823201f382956ed9beee9fb31229c2835c1208889cChandler Carruth    /// will be the identifier and the end will be the ')'. Finally, in
283c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    /// macro-argument instantitions, the end will be 'SourceLocation()', an
284c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    /// invalid location.
28578df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    unsigned ExpansionLocStart, ExpansionLocEnd;
286c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth
2879dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  public:
288de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    SourceLocation getSpellingLoc() const {
289de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return SourceLocation::getFromRawEncoding(SpellingLoc);
290de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
29178df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    SourceLocation getExpansionLocStart() const {
29278df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth      return SourceLocation::getFromRawEncoding(ExpansionLocStart);
293e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    }
29478df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    SourceLocation getExpansionLocEnd() const {
295c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth      SourceLocation EndLoc =
29678df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth        SourceLocation::getFromRawEncoding(ExpansionLocEnd);
29778df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth      return EndLoc.isInvalid() ? getExpansionLocStart() : EndLoc;
298e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    }
2991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30078df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    std::pair<SourceLocation,SourceLocation> getExpansionLocRange() const {
30178df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth      return std::make_pair(getExpansionLocStart(), getExpansionLocEnd());
302e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    }
3031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30496d3589e523a04f4ff2058a7919226ce60696ae8Chandler Carruth    bool isMacroArgExpansion() const {
305c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth      // Note that this needs to return false for default constructed objects.
30678df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth      return getExpansionLocStart().isValid() &&
30778df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth        SourceLocation::getFromRawEncoding(ExpansionLocEnd).isInvalid();
308c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    }
309c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth
310cee5ec9df479994e4ba27fb65b7ded5bb5a980ebArgyrios Kyrtzidis    bool isFunctionMacroExpansion() const {
311cee5ec9df479994e4ba27fb65b7ded5bb5a980ebArgyrios Kyrtzidis      return getExpansionLocStart().isValid() &&
312cee5ec9df479994e4ba27fb65b7ded5bb5a980ebArgyrios Kyrtzidis          getExpansionLocStart() != getExpansionLocEnd();
313cee5ec9df479994e4ba27fb65b7ded5bb5a980ebArgyrios Kyrtzidis    }
314cee5ec9df479994e4ba27fb65b7ded5bb5a980ebArgyrios Kyrtzidis
31578df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// create - Return a ExpansionInfo for an expansion. Start and End specify
31678df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// the expansion range (where the macro is expanded), and SpellingLoc
31778df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// specifies the spelling location (where the characters from the token
31878df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// come from). All three can refer to normal File SLocs or expansion
31978df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// locations.
32078df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    static ExpansionInfo create(SourceLocation SpellingLoc,
32178df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth                                SourceLocation Start, SourceLocation End) {
32278df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth      ExpansionInfo X;
32378df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth      X.SpellingLoc = SpellingLoc.getRawEncoding();
32478df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth      X.ExpansionLocStart = Start.getRawEncoding();
32578df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth      X.ExpansionLocEnd = End.getRawEncoding();
3269dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner      return X;
3275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
328c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth
32978df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// createForMacroArg - Return a special ExpansionInfo for the expansion of
33078df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// a macro argument into a function-like macro's body. ExpansionLoc
33178df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// specifies the expansion location (where the macro is expanded). This
33278df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// doesn't need to be a range because a macro is always expanded at
33378df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// a macro parameter reference, and macro parameters are always exactly
33478df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// one token. SpellingLoc specifies the spelling location (where the
33578df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// characters from the token come from). ExpansionLoc and SpellingLoc can
33678df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// both refer to normal File SLocs or expansion locations.
337c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    ///
338c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    /// Given the code:
339c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    /// \code
340c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    ///   #define F(x) f(x)
341c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    ///   F(42);
342c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    /// \endcode
343c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    ///
34478df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// When expanding '\c F(42)', the '\c x' would call this with an
34578df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// SpellingLoc pointing at '\c 42' anad an ExpansionLoc pointing at its
34678df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// location in the definition of '\c F'.
34778df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    static ExpansionInfo createForMacroArg(SourceLocation SpellingLoc,
34878df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth                                           SourceLocation ExpansionLoc) {
349c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth      // We store an intentionally invalid source location for the end of the
35078df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth      // expansion range to mark that this is a macro argument ion rather than
35178df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth      // a normal one.
35278df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth      return create(SpellingLoc, ExpansionLoc, SourceLocation());
353c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    }
354de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  };
3551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
356de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// SLocEntry - This is a discriminated union of FileInfo and
35778df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth  /// ExpansionInfo.  SourceManager keeps an array of these objects, and
358de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// they are uniquely identified by the FileID datatype.
359de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  class SLocEntry {
3603201f382956ed9beee9fb31229c2835c1208889cChandler Carruth    unsigned Offset;   // low bit is set for expansion info.
361de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    union {
362de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      FileInfo File;
3631728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth      ExpansionInfo Expansion;
364de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    };
365de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  public:
366de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned getOffset() const { return Offset >> 1; }
3671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3681728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth    bool isExpansion() const { return Offset & 1; }
3691728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth    bool isFile() const { return !isExpansion(); }
3701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
371de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    const FileInfo &getFile() const {
372de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      assert(isFile() && "Not a file SLocEntry!");
373de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return File;
374de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
375de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
3761728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth    const ExpansionInfo &getExpansion() const {
3771728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth      assert(isExpansion() && "Not a macro expansion SLocEntry!");
3781728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth      return Expansion;
379de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
3801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
381de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    static SLocEntry get(unsigned Offset, const FileInfo &FI) {
382de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      SLocEntry E;
383de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      E.Offset = Offset << 1;
384de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      E.File = FI;
385de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return E;
386de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
387de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
38878df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    static SLocEntry get(unsigned Offset, const ExpansionInfo &Expansion) {
389de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      SLocEntry E;
390de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      E.Offset = (Offset << 1) | 1;
3911728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth      E.Expansion = Expansion;
392de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return E;
393de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
3945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  };
3955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}  // end SrcMgr namespace.
3967f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
3977f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor/// \brief External source of source location entries.
3987f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregorclass ExternalSLocEntrySource {
3997f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregorpublic:
4007f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  virtual ~ExternalSLocEntrySource();
4017f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
402f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief Read the source location entry with index ID, which will always be
403f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// less than -1.
404e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor  ///
405e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor  /// \returns true if an error occurred that prevented the source-location
406e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor  /// entry from being loaded.
407f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  virtual bool ReadSLocEntry(int ID) = 0;
4087f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor};
4095330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
410dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner
411dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner/// IsBeforeInTranslationUnitCache - This class holds the cache used by
412dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner/// isBeforeInTranslationUnit.  The cache structure is complex enough to be
413dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner/// worth breaking out of SourceManager.
414dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattnerclass IsBeforeInTranslationUnitCache {
415dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// L/R QueryFID - These are the FID's of the cached query.  If these match up
416dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// with a subsequent query, the result can be reused.
417dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  FileID LQueryFID, RQueryFID;
41837e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis
41937e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis  /// \brief True if LQueryFID was created before RQueryFID. This is used
42037e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis  /// to compare macro expansion locations.
42137e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis  bool IsLQFIDBeforeRQFID;
42237e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis
423c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  /// CommonFID - This is the file found in common between the two \#include
424c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  /// traces.  It is the nearest common ancestor of the \#include tree.
425dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  FileID CommonFID;
4265330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
427dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// L/R CommonOffset - This is the offset of the previous query in CommonFID.
428c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  /// Usually, this represents the location of the \#include for QueryFID, but
429c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  /// if LQueryFID is a parent of RQueryFID (or vice versa) then these can be a
430dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// random token in the parent.
431dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  unsigned LCommonOffset, RCommonOffset;
432dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattnerpublic:
4335330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
434dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// isCacheValid - Return true if the currently cached values match up with
435dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// the specified LHS/RHS query.  If not, we can't use the cache.
436dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  bool isCacheValid(FileID LHS, FileID RHS) const {
437dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    return LQueryFID == LHS && RQueryFID == RHS;
438dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  }
4395330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
440dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// getCachedResult - If the cache is valid, compute the result given the
441dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// specified offsets in the LHS/RHS FID's.
442dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  bool getCachedResult(unsigned LOffset, unsigned ROffset) const {
443dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    // If one of the query files is the common file, use the offset.  Otherwise,
444dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    // use the #include loc in the common file.
445dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    if (LQueryFID != CommonFID) LOffset = LCommonOffset;
446dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    if (RQueryFID != CommonFID) ROffset = RCommonOffset;
44737e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis
44837e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis    // It is common for multiple macro expansions to be "included" from the same
44937e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis    // location (expansion location), in which case use the order of the FileIDs
4504d1cbcf6b9aaae7f82c6e332f46b2ad2d8971bd0Argyrios Kyrtzidis    // to determine which came first. This will also take care the case where
4514d1cbcf6b9aaae7f82c6e332f46b2ad2d8971bd0Argyrios Kyrtzidis    // one of the locations points at the inclusion/expansion point of the other
4524d1cbcf6b9aaae7f82c6e332f46b2ad2d8971bd0Argyrios Kyrtzidis    // in which case its FileID will come before the other.
453d7711ec430fde5706f85ba6c4b85283a8e743ff7Argyrios Kyrtzidis    if (LOffset == ROffset)
45437e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis      return IsLQFIDBeforeRQFID;
45537e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis
456dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    return LOffset < ROffset;
457dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  }
4585330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
459dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  // Set up a new query.
46037e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis  void setQueryFIDs(FileID LHS, FileID RHS, bool isLFIDBeforeRFID) {
46137e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis    assert(LHS != RHS);
462dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    LQueryFID = LHS;
463dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    RQueryFID = RHS;
46437e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis    IsLQFIDBeforeRQFID = isLFIDBeforeRFID;
46537e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis  }
46637e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis
46737e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis  void clear() {
46837e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis    LQueryFID = RQueryFID = FileID();
46937e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis    IsLQFIDBeforeRQFID = false;
470dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  }
4715330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
472dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  void setCommonLoc(FileID commonFID, unsigned lCommonOffset,
473dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner                    unsigned rCommonOffset) {
474dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    CommonFID = commonFID;
475dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    LCommonOffset = lCommonOffset;
476dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    RCommonOffset = rCommonOffset;
477dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  }
4785330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
479dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner};
4807f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
481f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor/// \brief This class handles loading and caching of source files into memory.
482f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor///
483f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor/// This object owns the MemoryBuffer objects for all of the loaded
484c351d9837ea1d0b04842497e76c76125426a982cJames Dennett/// files and assigns unique FileID's for each unique \#include chain.
4855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
4865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// The SourceManager can be queried for information about SourceLocation
4873201f382956ed9beee9fb31229c2835c1208889cChandler Carruth/// objects, turning them into either spelling or expansion locations. Spelling
4883201f382956ed9beee9fb31229c2835c1208889cChandler Carruth/// locations represent where the bytes corresponding to a token came from and
4893201f382956ed9beee9fb31229c2835c1208889cChandler Carruth/// expansion locations represent where the location is in the user's view. In
4903201f382956ed9beee9fb31229c2835c1208889cChandler Carruth/// the case of a macro expansion, for example, the spelling location indicates
4913201f382956ed9beee9fb31229c2835c1208889cChandler Carruth/// where the expanded token came from and the expansion location specifies
4923201f382956ed9beee9fb31229c2835c1208889cChandler Carruth/// where it was expanded.
493c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmithclass SourceManager : public RefCountedBase<SourceManager> {
494d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  /// \brief DiagnosticsEngine object.
495d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  DiagnosticsEngine &Diag;
496389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
497389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis  FileManager &FileMgr;
498389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
4990d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  mutable llvm::BumpPtrAllocator ContentCacheAlloc;
5001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// FileInfos - Memoized information about all of the files tracked by this
5020d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek  /// SourceManager.  This set allows us to merge ContentCache entries based
5030d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek  /// on their FileEntry*.  All ContentCache objects will thus have unique,
5041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// non-null, FileEntry pointers.
5050d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos;
5061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
507299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  /// \brief True if the ContentCache for files that are overriden by other
508299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  /// files, should report the original file name. Defaults to true.
509299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  bool OverridenFilesKeepOriginalName;
510299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis
511d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis  struct OverriddenFilesInfoTy {
512d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis    /// \brief Files that have been overriden with the contents from another
513d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis    /// file.
514d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis    llvm::DenseMap<const FileEntry *, const FileEntry *> OverriddenFiles;
515d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis    /// \brief Files that were overridden with a memory buffer.
516d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis    llvm::DenseSet<const FileEntry *> OverriddenFilesWithBuffer;
517d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis  };
518d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis
519d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis  /// \brief Lazily create the object keeping overridden files info, since
520d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis  /// it is uncommonly used.
521d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis  OwningPtr<OverriddenFilesInfoTy> OverriddenFilesInfo;
522b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis
523d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis  OverriddenFilesInfoTy &getOverriddenFilesInfo() {
524d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis    if (!OverriddenFilesInfo)
525d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis      OverriddenFilesInfo.reset(new OverriddenFilesInfoTy);
526d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis    return *OverriddenFilesInfo;
527d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis  }
528d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis
5295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// MemBufferInfos - Information about various memory buffers that we have
5300d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  /// read in.  All FileEntry* within the stored ContentCache objects are NULL,
5310d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  /// as they do not refer to a file.
5320d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  std::vector<SrcMgr::ContentCache*> MemBufferInfos;
5331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
534f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief The table of SLocEntries that are local to this module.
535f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///
536f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// Positive FileIDs are indexes into this table. Entry 0 indicates an invalid
5373201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// expansion.
538f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  std::vector<SrcMgr::SLocEntry> LocalSLocEntryTable;
539f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
540f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief The table of SLocEntries that are loaded from other modules.
541f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///
542f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// Negative FileIDs are indexes into this table. To get from ID to an index,
543f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// use (-ID - 2).
544a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis  mutable std::vector<SrcMgr::SLocEntry> LoadedSLocEntryTable;
5457f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
546f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief The starting offset of the next local SLocEntry.
547f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///
548f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// This is LocalSLocEntryTable.back().Offset + the size of that entry.
549f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  unsigned NextLocalOffset;
550f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
551f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief The starting offset of the latest batch of loaded SLocEntries.
552f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///
553f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// This is LoadedSLocEntryTable.back().Offset, except that that entry might
554f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// not have been loaded, so that value would be unknown.
555f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  unsigned CurrentLoadedOffset;
556f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
557ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  /// \brief The highest possible offset is 2^31-1, so CurrentLoadedOffset
558ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  /// starts at 2^31.
559ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  static const unsigned MaxLoadedOffset = 1U << 31U;
560ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis
561f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief A bitmap that indicates whether the entries of LoadedSLocEntryTable
562f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// have already been loaded from the external source.
563f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///
564f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// Same indexing as LoadedSLocEntryTable.
5657f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  std::vector<bool> SLocEntryLoaded;
5667f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
5677f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  /// \brief An external source for source location entries.
5687f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  ExternalSLocEntrySource *ExternalSLocEntries;
5697f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
570c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  /// \brief This is a one-entry cache to speed up getFileID.
571c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  ///
572de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// LastFileIDLookup records the last FileID looked up or created, because it
573de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// is very common to look up many tokens from the same file.
574de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  mutable FileID LastFileIDLookup;
5751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
576c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  /// \brief Holds information for \#line directives.
577c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  ///
578c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  /// It is referenced by indices from SLocEntryTable.
5795b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  LineTableInfo *LineTable;
5801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5815e36a7a89da5d69ece23fc8228624a053f75c645Chris Lattner  /// LastLineNo - These ivars serve as a cache used in the getLineNumber
5825e36a7a89da5d69ece23fc8228624a053f75c645Chris Lattner  /// method which is used to speedup getLineNumber calls to nearby locations.
5832b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  mutable FileID LastLineNoFileIDQuery;
584f812a45dd93634c9300ed5533bd26b56374714a1Chris Lattner  mutable SrcMgr::ContentCache *LastLineNoContentCache;
585f812a45dd93634c9300ed5533bd26b56374714a1Chris Lattner  mutable unsigned LastLineNoFilePos;
586f812a45dd93634c9300ed5533bd26b56374714a1Chris Lattner  mutable unsigned LastLineNoResult;
5871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
588c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  /// \brief The file ID for the main source file of the translation unit.
5892b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID MainFileID;
59049c1f4aa2a6c360d25d605004ec3c4affd62db77Steve Naroff
591507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  /// \brief The file ID for the precompiled preamble there is one.
592507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  FileID PreambleFileID;
593507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
594d73ef135ba029db59c0b5649e6117845d9e39600Jordan Rose  /// \brief The file ID for the preprocessor's predefines.
595d73ef135ba029db59c0b5649e6117845d9e39600Jordan Rose  FileID PredefinesFileID;
596d73ef135ba029db59c0b5649e6117845d9e39600Jordan Rose
597de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  // Statistics for -print-stats.
598de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  mutable unsigned NumLinearScans, NumBinaryProbes;
5991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6002aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis  // Cache results for the isBeforeInTranslationUnit method.
601dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  mutable IsBeforeInTranslationUnitCache IsBeforeInTUCache;
6021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
603e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor  // Cache for the "fake" buffer used for error-recovery purposes.
604e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor  mutable llvm::MemoryBuffer *FakeBufferForRecovery;
6055330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
606a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis  mutable SrcMgr::ContentCache *FakeContentCacheForRecovery;
607a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis
608fb3612ef197cb8532c05f33889ec1aed7c26e5cbArgyrios Kyrtzidis  /// \brief Lazily computed map of macro argument chunks to their expanded
609fb3612ef197cb8532c05f33889ec1aed7c26e5cbArgyrios Kyrtzidis  /// source location.
610fb3612ef197cb8532c05f33889ec1aed7c26e5cbArgyrios Kyrtzidis  typedef std::map<unsigned, SourceLocation> MacroArgsMap;
611fb3612ef197cb8532c05f33889ec1aed7c26e5cbArgyrios Kyrtzidis
61270042f5d1ca378138f90b7b9384023701f5d03d8David Blaikie  mutable llvm::DenseMap<FileID, MacroArgsMap *> MacroArgsCacheMap;
613fb3612ef197cb8532c05f33889ec1aed7c26e5cbArgyrios Kyrtzidis
61449c1f4aa2a6c360d25d605004ec3c4affd62db77Steve Naroff  // SourceManager doesn't support copy construction.
61549c1f4aa2a6c360d25d605004ec3c4affd62db77Steve Naroff  explicit SourceManager(const SourceManager&);
6161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  void operator=(const SourceManager&);
6175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
618d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  SourceManager(DiagnosticsEngine &Diag, FileManager &FileMgr);
6195b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  ~SourceManager();
6201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6215b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  void clearIDTables();
6221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
623d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  DiagnosticsEngine &getDiagnostics() const { return Diag; }
62478a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios Kyrtzidis
625389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis  FileManager &getFileManager() const { return FileMgr; }
626389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
627299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  /// \brief Set true if the SourceManager should report the original file name
628299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  /// for contents of files that were overriden by other files.Defaults to true.
629299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  void setOverridenFilesKeepOriginalName(bool value) {
630299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis    OverridenFilesKeepOriginalName = value;
631299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  }
632299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis
633f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// createMainFileIDForMembuffer - Create the FileID for a memory buffer
634f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///  that will represent the FileID for the main source.  One example
635f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///  of when this would be used is when the main source is read from STDIN.
636f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) {
637f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    assert(MainFileID.isInvalid() && "MainFileID already set!");
638f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    MainFileID = createFileIDForMemBuffer(Buffer);
639f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    return MainFileID;
640f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  }
641d73ef135ba029db59c0b5649e6117845d9e39600Jordan Rose
642d73ef135ba029db59c0b5649e6117845d9e39600Jordan Rose  /// \brief Create the FileID for a memory buffer that contains the
643d73ef135ba029db59c0b5649e6117845d9e39600Jordan Rose  /// preprocessor's predefines.
644d73ef135ba029db59c0b5649e6117845d9e39600Jordan Rose  FileID createPredefinesFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) {
645d73ef135ba029db59c0b5649e6117845d9e39600Jordan Rose    assert(PredefinesFileID.isInvalid() && "PredefinesFileID already set!");
646d73ef135ba029db59c0b5649e6117845d9e39600Jordan Rose    PredefinesFileID = createFileIDForMemBuffer(Buffer);
647d73ef135ba029db59c0b5649e6117845d9e39600Jordan Rose    return PredefinesFileID;
648d73ef135ba029db59c0b5649e6117845d9e39600Jordan Rose  }
649f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
65006a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
65106a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  // MainFileID creation and querying methods.
65206a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
65306a062dc784c609b75dca15fd97f468d0d846596Chris Lattner
65476edd0e4ae0592a7225d50d0bad6732ac64dca2aTed Kremenek  /// getMainFileID - Returns the FileID of the main source file.
6552b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID getMainFileID() const { return MainFileID; }
6561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
657d73ef135ba029db59c0b5649e6117845d9e39600Jordan Rose  /// \brief Returns the FileID of the preprocessor predefines buffer.
658d73ef135ba029db59c0b5649e6117845d9e39600Jordan Rose  FileID getPredefinesFileID() const { return PredefinesFileID; }
659d73ef135ba029db59c0b5649e6117845d9e39600Jordan Rose
66006a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  /// createMainFileID - Create the FileID for the main source file.
661a1f1fad8b60e1cb9d21a40a37f2e03150bcbeb6fDouglas Gregor  FileID createMainFileID(const FileEntry *SourceFile,
662a1f1fad8b60e1cb9d21a40a37f2e03150bcbeb6fDouglas Gregor                          SrcMgr::CharacteristicKind Kind = SrcMgr::C_User) {
66306a062dc784c609b75dca15fd97f468d0d846596Chris Lattner    assert(MainFileID.isInvalid() && "MainFileID already set!");
664a1f1fad8b60e1cb9d21a40a37f2e03150bcbeb6fDouglas Gregor    MainFileID = createFileID(SourceFile, SourceLocation(), Kind);
66506a062dc784c609b75dca15fd97f468d0d846596Chris Lattner    return MainFileID;
66606a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  }
6671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
668b8c879a5363f36bdae8831112b563333e3c05acbArgyrios Kyrtzidis  /// \brief Set the file ID for the main source file.
669b8c879a5363f36bdae8831112b563333e3c05acbArgyrios Kyrtzidis  void setMainFileID(FileID FID) {
670b8c879a5363f36bdae8831112b563333e3c05acbArgyrios Kyrtzidis    assert(MainFileID.isInvalid() && "MainFileID already set!");
671b8c879a5363f36bdae8831112b563333e3c05acbArgyrios Kyrtzidis    MainFileID = FID;
672b8c879a5363f36bdae8831112b563333e3c05acbArgyrios Kyrtzidis  }
673b8c879a5363f36bdae8831112b563333e3c05acbArgyrios Kyrtzidis
674507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  /// \brief Set the file ID for the precompiled preamble.
675507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  void setPreambleFileID(FileID Preamble) {
676507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    assert(PreambleFileID.isInvalid() && "PreambleFileID already set!");
677507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    PreambleFileID = Preamble;
678414cb64f09ce48a36377458ce5e5a90c3ad41d00Douglas Gregor  }
6795330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
680507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  /// \brief Get the file ID for the precompiled preamble if there is one.
681507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  FileID getPreambleFileID() const { return PreambleFileID; }
682507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
68306a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
6843201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  // Methods to create new FileID's and macro expansions.
68506a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
6861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// createFileID - Create a new FileID that represents the specified file
688c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  /// being \#included from the specified IncludePosition.  This translates NULL
689d57b7ff9bebc4c45f325fc1be6f238cfcd4c3732Peter Collingbourne  /// into standard input.
6902b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
6917f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                      SrcMgr::CharacteristicKind FileCharacter,
692f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor                      int LoadedID = 0, unsigned LoadedOffset = 0) {
693de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile);
6940d06e998910934e5ef070f53f4c272e7c6b846c6Dan Gohman    assert(IR && "getOrCreateContentCache() cannot return NULL");
695f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    return createFileID(IR, IncludePos, FileCharacter, LoadedID, LoadedOffset);
6965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
6971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// createFileIDForMemBuffer - Create a new FileID that represents the
6995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// specified memory buffer.  This does no caching of the buffer and takes
7005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once.
7017f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer,
702f453cb9f677e16c00b358ec91eccf5f003765dc6Axel Naumann                                  int LoadedID = 0, unsigned LoadedOffset = 0,
703f453cb9f677e16c00b358ec91eccf5f003765dc6Axel Naumann                                 SourceLocation IncludeLoc = SourceLocation()) {
704f453cb9f677e16c00b358ec91eccf5f003765dc6Axel Naumann    return createFileID(createMemBufferContentCache(Buffer), IncludeLoc,
705f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor                        SrcMgr::C_User, LoadedID, LoadedOffset);
7061036b68525f39cb69ac22c679ed440acd8392a16Ted Kremenek  }
70706a062dc784c609b75dca15fd97f468d0d846596Chris Lattner
708bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth  /// createMacroArgExpansionLoc - Return a new SourceLocation that encodes the
709bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth  /// fact that a token from SpellingLoc should actually be referenced from
7103201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// ExpansionLoc, and that it represents the expansion of a macro argument
7113201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// into the function-like macro body.
712bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth  SourceLocation createMacroArgExpansionLoc(SourceLocation Loc,
713bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                            SourceLocation ExpansionLoc,
714bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                            unsigned TokLength);
715c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth
716bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth  /// createExpansionLoc - Return a new SourceLocation that encodes the fact
717c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth  /// that a token from SpellingLoc should actually be referenced from
718bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth  /// ExpansionLoc.
719bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth  SourceLocation createExpansionLoc(SourceLocation Loc,
720bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                    SourceLocation ExpansionLocStart,
721bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                    SourceLocation ExpansionLocEnd,
722bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                    unsigned TokLength,
723bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                    int LoadedID = 0,
724bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                    unsigned LoadedOffset = 0);
7251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7262968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \brief Retrieve the memory buffer associated with the given file.
72750f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  ///
72850f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  /// \param Invalid If non-NULL, will be set \c true if an error
72950f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  /// occurs while retrieving the memory buffer.
73050f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File,
73150f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor                                                   bool *Invalid = 0);
7322968442603b029949246467253eeac8139a5b6d8Douglas Gregor
7332968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \brief Override the contents of the given source file by providing an
7342968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// already-allocated buffer.
7352968442603b029949246467253eeac8139a5b6d8Douglas Gregor  ///
736afbf5f810652f04c0bba235c0fa079885fb3caa8Dan Gohman  /// \param SourceFile the source file whose contents will be overriden.
7372968442603b029949246467253eeac8139a5b6d8Douglas Gregor  ///
7382968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \param Buffer the memory buffer whose contents will be used as the
7392968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// data in the given source file.
7402968442603b029949246467253eeac8139a5b6d8Douglas Gregor  ///
741f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// \param DoNotFree If true, then the buffer will not be freed when the
742f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// source manager is destroyed.
7430d06e998910934e5ef070f53f4c272e7c6b846c6Dan Gohman  void overrideFileContents(const FileEntry *SourceFile,
744f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor                            const llvm::MemoryBuffer *Buffer,
745f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor                            bool DoNotFree = false);
7462968442603b029949246467253eeac8139a5b6d8Douglas Gregor
747b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  /// \brief Override the the given source file with another one.
748b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  ///
749b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  /// \param SourceFile the source file which will be overriden.
750b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  ///
751b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  /// \param NewFile the file whose contents will be used as the
752b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  /// data instead of the contents of the given source file.
753b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  void overrideFileContents(const FileEntry *SourceFile,
754b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis                            const FileEntry *NewFile);
755b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis
756d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis  /// \brief Returns true if the file contents have been overridden.
757d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis  bool isFileOverridden(const FileEntry *File) {
758d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis    if (OverriddenFilesInfo) {
759d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis      if (OverriddenFilesInfo->OverriddenFilesWithBuffer.count(File))
760d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis        return true;
761d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis      if (OverriddenFilesInfo->OverriddenFiles.find(File) !=
762d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis          OverriddenFilesInfo->OverriddenFiles.end())
763d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis        return true;
764d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis    }
765d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis    return false;
766d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis  }
767d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis
768d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis  /// \brief Disable overridding the contents of a file, previously enabled
769d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis  /// with \see overrideFileContents.
770d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis  /// This should be called before parsing has begun.
771d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis  void disableFileContentsOverride(const FileEntry *File);
772d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis
77306a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
77406a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  // FileID manipulation methods.
77506a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
7761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7772ffb14f004affc363d86d6c7c63c356190db3679Daniel Dunbar  /// getBuffer - Return the buffer for the specified FileID. If there is an
7782ffb14f004affc363d86d6c7c63c356190db3679Daniel Dunbar  /// error opening this buffer the first time, this manufactures a temporary
7792ffb14f004affc363d86d6c7c63c356190db3679Daniel Dunbar  /// buffer and returns a non-empty error string.
780e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner  const llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc,
781e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner                                      bool *Invalid = 0) const {
782e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    bool MyInvalid = false;
783e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
784e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    if (MyInvalid || !Entry.isFile()) {
785e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor      if (Invalid)
786e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor        *Invalid = true;
7875330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
788e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor      return getFakeBufferForRecovery();
789e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    }
7905330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
7915330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher    return Entry.getFile().getContentCache()->getBuffer(Diag, *this, Loc,
792e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor                                                        Invalid);
79306a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  }
7941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
795e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner  const llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = 0) const {
796e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    bool MyInvalid = false;
797e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
798e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    if (MyInvalid || !Entry.isFile()) {
799e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor      if (Invalid)
800e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor        *Invalid = true;
8015330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
802e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor      return getFakeBufferForRecovery();
803e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    }
804e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor
8055330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher    return Entry.getFile().getContentCache()->getBuffer(Diag, *this,
8065330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher                                                        SourceLocation(),
807e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor                                                        Invalid);
808e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner  }
8095330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
81006a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  /// getFileEntryForID - Returns the FileEntry record for the provided FileID.
81106a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  const FileEntry *getFileEntryForID(FileID FID) const {
812e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    bool MyInvalid = false;
813e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
814e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    if (MyInvalid || !Entry.isFile())
815e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor      return 0;
8165330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
81739afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis    const SrcMgr::ContentCache *Content = Entry.getFile().getContentCache();
81839afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis    if (!Content)
81939afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis      return 0;
82039afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis    return Content->OrigEntry;
82106a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  }
8221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8239d5a165d301cc9df68631e624322dd2a962f65b3Ted Kremenek  /// Returns the FileEntry record for the provided SLocEntry.
8249d5a165d301cc9df68631e624322dd2a962f65b3Ted Kremenek  const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const
8259d5a165d301cc9df68631e624322dd2a962f65b3Ted Kremenek  {
82639afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis    const SrcMgr::ContentCache *Content = sloc.getFile().getContentCache();
82739afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis    if (!Content)
82839afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis      return 0;
82939afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis    return Content->OrigEntry;
8309d5a165d301cc9df68631e624322dd2a962f65b3Ted Kremenek  }
8319d5a165d301cc9df68631e624322dd2a962f65b3Ted Kremenek
832ceafc4b63599d14f0b5b10ff92e22bf242682dceBenjamin Kramer  /// getBufferData - Return a StringRef to the source buffer data for the
833ceafc4b63599d14f0b5b10ff92e22bf242682dceBenjamin Kramer  /// specified FileID.
834ceafc4b63599d14f0b5b10ff92e22bf242682dceBenjamin Kramer  ///
835f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor  /// \param FID The file ID whose contents will be returned.
836f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor  /// \param Invalid If non-NULL, will be set true if an error occurred.
837686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  StringRef getBufferData(FileID FID, bool *Invalid = 0) const;
838f6ac97b101c8840efa92bf29166077ce4049e293Benjamin Kramer
839d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  /// \brief Get the number of FileIDs (files and macros) that were created
8402c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// during preprocessing of \p FID, including it.
841d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  unsigned getNumCreatedFIDsForFileID(FileID FID) const {
842d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    bool Invalid = false;
843d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
844d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    if (Invalid || !Entry.isFile())
845d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis      return 0;
846d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis
847d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    return Entry.getFile().NumCreatedFIDs;
848d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  }
849d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis
850d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  /// \brief Set the number of FileIDs (files and macros) that were created
8512c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// during preprocessing of \p FID, including it.
852d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  void setNumCreatedFIDsForFileID(FileID FID, unsigned NumFIDs) const {
853d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    bool Invalid = false;
854d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
855d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    if (Invalid || !Entry.isFile())
856d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis      return;
857d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis
858d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    assert(Entry.getFile().NumCreatedFIDs == 0 && "Already set!");
859d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    const_cast<SrcMgr::FileInfo &>(Entry.getFile()).NumCreatedFIDs = NumFIDs;
860d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  }
8611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
86206a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
86306a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  // SourceLocation manipulation methods.
86406a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
8651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
866668ab1a36d6a5731c71a75a5f388ecafd538a896Chris Lattner  /// getFileID - Return the FileID for a SourceLocation.  This is a very
867de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// hot method that is used for all SourceManager queries that start with a
868de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// SourceLocation object.  It is responsible for finding the entry in
869de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// SLocEntryTable which contains the specified location.
870de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  ///
871de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  FileID getFileID(SourceLocation SpellingLoc) const {
872de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned SLocOffset = SpellingLoc.getOffset();
8731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
874de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    // If our one-entry cache covers this offset, just return it.
875de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
876de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return LastFileIDLookup;
877de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
878de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return getFileIDSlow(SLocOffset);
879de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
8801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8812b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  /// getLocForStartOfFile - Return the source location corresponding to the
8822b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  /// first byte of the specified file.
8832b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  SourceLocation getLocForStartOfFile(FileID FID) const {
884e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    bool Invalid = false;
885e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
886e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    if (Invalid || !Entry.isFile())
887e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor      return SourceLocation();
8885330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
889e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    unsigned FileOffset = Entry.getOffset();
890de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return SourceLocation::getFileLoc(FileOffset);
8912b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  }
892f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis
893f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis  /// \brief Return the source location corresponding to the last byte of the
894f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis  /// specified file.
895f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis  SourceLocation getLocForEndOfFile(FileID FID) const {
896f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis    bool Invalid = false;
897f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
898f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis    if (Invalid || !Entry.isFile())
899f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis      return SourceLocation();
900f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis
901f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis    unsigned FileOffset = Entry.getOffset();
902f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis    return SourceLocation::getFileLoc(FileOffset + getFileIDSize(FID) - 1);
903f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis  }
9041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
905809d1be9820039b4cf6efa48246a0d70ffa13394James Dennett  /// \brief Returns the include location if \p FID is a \#include'd file
906d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  /// otherwise it returns an invalid location.
907d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  SourceLocation getIncludeLoc(FileID FID) const {
908d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    bool Invalid = false;
909d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
910d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    if (Invalid || !Entry.isFile())
911d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis      return SourceLocation();
9125330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
913d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    return Entry.getFile().getIncludeLoc();
914d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  }
915d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis
916402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth  /// getExpansionLoc - Given a SourceLocation object, return the expansion
917402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth  /// location referenced by the ID.
918402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth  SourceLocation getExpansionLoc(SourceLocation Loc) const {
919addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner    // Handle the non-mapped case inline, defer to out of line code to handle
920402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth    // expansions.
921de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (Loc.isFileID()) return Loc;
922f84ef95ecec34f27fd05eb4e0392ca6bd3bd0be0Chandler Carruth    return getExpansionLocSlowCase(Loc);
923de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
9241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9252c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Given \p Loc, if it is a macro location return the expansion
926796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis  /// location or the spelling location, depending on if it comes from a
927796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis  /// macro argument or not.
928796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis  SourceLocation getFileLoc(SourceLocation Loc) const {
929796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis    if (Loc.isFileID()) return Loc;
930796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis    return getFileLocSlowCase(Loc);
931796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis  }
932796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis
933999f739404edf2078cf9f9c28b4dc45c19765842Chandler Carruth  /// getImmediateExpansionRange - Loc is required to be an expansion location.
934999f739404edf2078cf9f9c28b4dc45c19765842Chandler Carruth  /// Return the start/end of the expansion information.
935e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  std::pair<SourceLocation,SourceLocation>
936999f739404edf2078cf9f9c28b4dc45c19765842Chandler Carruth  getImmediateExpansionRange(SourceLocation Loc) const;
9371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
938edc3dccece244a584f8ebdb81da6c962c08e79beChandler Carruth  /// getExpansionRange - Given a SourceLocation object, return the range of
939edc3dccece244a584f8ebdb81da6c962c08e79beChandler Carruth  /// tokens covered by the expansion the ultimate file.
9406678133b8ce642f93e5141f056fa643112041ad0Chris Lattner  std::pair<SourceLocation,SourceLocation>
941edc3dccece244a584f8ebdb81da6c962c08e79beChandler Carruth  getExpansionRange(SourceLocation Loc) const;
9421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
944de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// getSpellingLoc - Given a SourceLocation object, return the spelling
945de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// location referenced by the ID.  This is the place where the characters
946de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// that make up the lexed token can be found.
947de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  SourceLocation getSpellingLoc(SourceLocation Loc) const {
948addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner    // Handle the non-mapped case inline, defer to out of line code to handle
9493201f382956ed9beee9fb31229c2835c1208889cChandler Carruth    // expansions.
950de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (Loc.isFileID()) return Loc;
951addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner    return getSpellingLocSlowCase(Loc);
952de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
9531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
954387616edf98739f4a0dd234c907e2b913e6a535dChris Lattner  /// getImmediateSpellingLoc - Given a SourceLocation object, return the
955387616edf98739f4a0dd234c907e2b913e6a535dChris Lattner  /// spelling location referenced by the ID.  This is the first level down
956387616edf98739f4a0dd234c907e2b913e6a535dChris Lattner  /// towards the place where the characters that make up the lexed token can be
957387616edf98739f4a0dd234c907e2b913e6a535dChris Lattner  /// found.  This should not generally be used by clients.
9581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const;
959de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
960de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// getDecomposedLoc - Decompose the specified location into a raw FileID +
961de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// Offset pair.  The first element is the FileID, the second is the
962de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// offset from the start of the buffer of the location.
963de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const {
964de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    FileID FID = getFileID(Loc);
965a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    bool Invalid = false;
966a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    const SrcMgr::SLocEntry &E = getSLocEntry(FID, &Invalid);
967a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    if (Invalid)
968a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis      return std::make_pair(FileID(), 0);
969a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    return std::make_pair(FID, Loc.getOffset()-E.getOffset());
970de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
9711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9723201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// getDecomposedExpansionLoc - Decompose the specified location into a raw
9733201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// FileID + Offset pair. If the location is an expansion record, walk
9743201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// through it until we find the final location expanded.
975de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned>
976e7b2b6e87dbe5b1207f77b6ff9c210a02f95bb39Chandler Carruth  getDecomposedExpansionLoc(SourceLocation Loc) const {
977de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    FileID FID = getFileID(Loc);
978a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    bool Invalid = false;
979a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    const SrcMgr::SLocEntry *E = &getSLocEntry(FID, &Invalid);
980a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    if (Invalid)
981a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis      return std::make_pair(FileID(), 0);
9821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
983de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned Offset = Loc.getOffset()-E->getOffset();
984de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (Loc.isFileID())
985de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return std::make_pair(FID, Offset);
9861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
987e7b2b6e87dbe5b1207f77b6ff9c210a02f95bb39Chandler Carruth    return getDecomposedExpansionLocSlowCase(E);
988de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
989de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
990de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// getDecomposedSpellingLoc - Decompose the specified location into a raw
9913201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// FileID + Offset pair.  If the location is an expansion record, walk
992de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// through it until we find its spelling record.
993de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned>
994de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  getDecomposedSpellingLoc(SourceLocation Loc) const {
995de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    FileID FID = getFileID(Loc);
996a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    bool Invalid = false;
997a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    const SrcMgr::SLocEntry *E = &getSLocEntry(FID, &Invalid);
998a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    if (Invalid)
999a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis      return std::make_pair(FileID(), 0);
10001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1001de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned Offset = Loc.getOffset()-E->getOffset();
1002de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (Loc.isFileID())
1003de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return std::make_pair(FID, Offset);
1004de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return getDecomposedSpellingLocSlowCase(E, Offset);
10051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
10061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
100752c29081281955d3db9e11d10573b2d38f709099Chris Lattner  /// getFileOffset - This method returns the offset from the start
100852c29081281955d3db9e11d10573b2d38f709099Chris Lattner  /// of the file that the specified SourceLocation represents. This is not very
100952c29081281955d3db9e11d10573b2d38f709099Chris Lattner  /// meaningful for a macro ID.
101052c29081281955d3db9e11d10573b2d38f709099Chris Lattner  unsigned getFileOffset(SourceLocation SpellingLoc) const {
1011de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return getDecomposedLoc(SpellingLoc).second;
10125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
10131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
101496d3589e523a04f4ff2058a7919226ce60696ae8Chandler Carruth  /// isMacroArgExpansion - This method tests whether the given source location
101596d3589e523a04f4ff2058a7919226ce60696ae8Chandler Carruth  /// represents a macro argument's expansion into the function-like macro
101696d3589e523a04f4ff2058a7919226ce60696ae8Chandler Carruth  /// definition. Such source locations only appear inside of the expansion
101796d3589e523a04f4ff2058a7919226ce60696ae8Chandler Carruth  /// locations representing where a particular function-like macro was
101896d3589e523a04f4ff2058a7919226ce60696ae8Chandler Carruth  /// expanded.
101996d3589e523a04f4ff2058a7919226ce60696ae8Chandler Carruth  bool isMacroArgExpansion(SourceLocation Loc) const;
10201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10212c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Returns true if \p Loc is inside the [\p Start, +\p Length)
1022499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis  /// chunk of the source location address space.
10232c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// If it's true and \p RelativeOffset is non-null, it will be set to the
10242c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// relative offset of \p Loc inside the chunk.
1025499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis  bool isInSLocAddrSpace(SourceLocation Loc,
1026499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis                         SourceLocation Start, unsigned Length,
1027499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis                         unsigned *RelativeOffset = 0) const {
1028499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis    assert(((Start.getOffset() < NextLocalOffset &&
1029499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis               Start.getOffset()+Length <= NextLocalOffset) ||
1030499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis            (Start.getOffset() >= CurrentLoadedOffset &&
1031499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis                Start.getOffset()+Length < MaxLoadedOffset)) &&
1032499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis           "Chunk is not valid SLoc address space");
1033499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis    unsigned LocOffs = Loc.getOffset();
1034499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis    unsigned BeginOffs = Start.getOffset();
1035499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis    unsigned EndOffs = BeginOffs + Length;
1036499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis    if (LocOffs >= BeginOffs && LocOffs < EndOffs) {
1037499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis      if (RelativeOffset)
1038499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis        *RelativeOffset = LocOffs - BeginOffs;
1039499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis      return true;
1040499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis    }
1041499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis
1042499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis    return false;
1043499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis  }
1044499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis
10452c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Return true if both \p LHS and \p RHS are in the local source
10462c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// location address space or the loaded one. If it's true and \p
10472c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// RelativeOffset is non-null, it will be set to the offset of \p RHS
10482c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// relative to \p LHS.
1049b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis  bool isInSameSLocAddrSpace(SourceLocation LHS, SourceLocation RHS,
1050b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis                             int *RelativeOffset) const {
1051b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis    unsigned LHSOffs = LHS.getOffset(), RHSOffs = RHS.getOffset();
1052b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis    bool LHSLoaded = LHSOffs >= CurrentLoadedOffset;
1053b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis    bool RHSLoaded = RHSOffs >= CurrentLoadedOffset;
1054b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis
1055b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis    if (LHSLoaded == RHSLoaded) {
1056b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis      if (RelativeOffset)
1057b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis        *RelativeOffset = RHSOffs - LHSOffs;
1058b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis      return true;
1059b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis    }
1060b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis
1061b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis    return false;
1062b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis  }
1063b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis
1064de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  //===--------------------------------------------------------------------===//
1065de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  // Queries about the code at a SourceLocation.
1066de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  //===--------------------------------------------------------------------===//
10671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getCharacterData - Return a pointer to the start of the specified location
1069de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// in the appropriate spelling MemoryBuffer.
107050f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  ///
107150f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  /// \param Invalid If non-NULL, will be set \c true if an error occurs.
107250f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  const char *getCharacterData(SourceLocation SL, bool *Invalid = 0) const;
10731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10749dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  /// getColumnNumber - Return the column # for the specified file position.
10759dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  /// This is significantly cheaper to compute than the line number.  This
10763201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// returns zero if the column number isn't known.  This may only be called
10773201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// on a file sloc, so you must choose a spelling or expansion location
1078f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner  /// before calling this method.
10795330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher  unsigned getColumnNumber(FileID FID, unsigned FilePos,
108050f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor                           bool *Invalid = 0) const;
10815ef04ee40c3332d31b6d1439f50d0ddb45812929Chandler Carruth  unsigned getSpellingColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
1082a77c031cb66f75d22672070052cc6e0205289ff8Chandler Carruth  unsigned getExpansionColumnNumber(SourceLocation Loc,
1083b49dcd249c7f4f034493741f95394987a4ccf244Chandler Carruth                                    bool *Invalid = 0) const;
10845ef04ee40c3332d31b6d1439f50d0ddb45812929Chandler Carruth  unsigned getPresumedColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
10851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1087df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner  /// getLineNumber - Given a SourceLocation, return the spelling line number
10885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// for the position indicated.  This requires building and caching a table of
10895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// line offsets for the MemoryBuffer, so this is not cheap: use only when
10905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// about to emit a diagnostic.
109150f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = 0) const;
10925ef04ee40c3332d31b6d1439f50d0ddb45812929Chandler Carruth  unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
1093642116259e8df6286063a17361c20e95b5017a0aChandler Carruth  unsigned getExpansionLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
10945ef04ee40c3332d31b6d1439f50d0ddb45812929Chandler Carruth  unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
10951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1096c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  /// \brief Return the filename or buffer identifier of the buffer the
1097c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  /// location is in.
1098c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  ///
1099c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  /// Note that this name does not respect \#line directives.  Use
1100c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  /// getPresumedLoc for normal clients.
110150f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  const char *getBufferName(SourceLocation Loc, bool *Invalid = 0) const;
11021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11036b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// getFileCharacteristic - return the file characteristic of the specified
11041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// source location, indicating whether this is a normal file, a system
11056b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// header, or an "implicit extern C" system header.
11066b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  ///
11076b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// This state can be modified with flags on GNU linemarker directives like:
1108c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  /// \code
11096b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  ///   # 4 "foo.h" 3
1110c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  /// \endcode
11116b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// which changes all source locations in the current file after that to be
11126b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// considered to be from a system header.
11136b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const;
11141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1115b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// getPresumedLoc - This method returns the "presumed" location of a
1116c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  /// SourceLocation specifies.  A "presumed location" can be modified by \#line
1117b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// or GNU line marker directives.  This provides a view on the data that a
1118b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// user should see in diagnostics, for example.
1119b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  ///
11203201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// Note that a presumed location is always given as the expansion point of
11213201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// an expansion location, not at the spelling location.
1122cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  ///
1123cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  /// \returns The presumed location of the specified SourceLocation. If the
1124cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  /// presumed location cannot be calculate (e.g., because \p Loc is invalid
1125cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  /// or the file containing \p Loc has changed on disk), returns an invalid
1126cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  /// presumed location.
1127b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  PresumedLoc getPresumedLoc(SourceLocation Loc) const;
11281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11299fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  /// isFromSameFile - Returns true if both SourceLocations correspond to
11309fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  ///  the same file.
11319fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
1132a11d61793341fea195c29a0dab3fbd74f2b39a8cChris Lattner    return getFileID(Loc1) == getFileID(Loc2);
11339fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  }
11341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11359fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  /// isFromMainFile - Returns true if the file of provided SourceLocation is
11369fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  ///   the main file.
11379fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  bool isFromMainFile(SourceLocation Loc) const {
1138a11d61793341fea195c29a0dab3fbd74f2b39a8cChris Lattner    return getFileID(Loc) == getMainFileID();
11391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
11401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1141d73ef135ba029db59c0b5649e6117845d9e39600Jordan Rose  /// isFromPredefines - Returns true if the provided SourceLocation is
1142d73ef135ba029db59c0b5649e6117845d9e39600Jordan Rose  ///   within the processor's predefines buffer.
1143d73ef135ba029db59c0b5649e6117845d9e39600Jordan Rose  bool isFromPredefines(SourceLocation Loc) const {
1144d73ef135ba029db59c0b5649e6117845d9e39600Jordan Rose    return getFileID(Loc) == getPredefinesFileID();
1145d73ef135ba029db59c0b5649e6117845d9e39600Jordan Rose  }
1146d73ef135ba029db59c0b5649e6117845d9e39600Jordan Rose
11477bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber  /// isInSystemHeader - Returns if a SourceLocation is in a system header.
11487bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber  bool isInSystemHeader(SourceLocation Loc) const {
11490b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner    return getFileCharacteristic(Loc) != SrcMgr::C_User;
1150721818304ac462d8c6ce05eecd02884033db78f1Chris Lattner  }
11511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11520d456588acac0713a7c33063922d35a8cc8c658eChris Lattner  /// isInExternCSystemHeader - Returns if a SourceLocation is in an "extern C"
11530d456588acac0713a7c33063922d35a8cc8c658eChris Lattner  /// system header.
11540d456588acac0713a7c33063922d35a8cc8c658eChris Lattner  bool isInExternCSystemHeader(SourceLocation Loc) const {
11550d456588acac0713a7c33063922d35a8cc8c658eChris Lattner    return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem;
11560d456588acac0713a7c33063922d35a8cc8c658eChris Lattner  }
11571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1158d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay  /// \brief Returns whether \p Loc is expanded from a macro in a system header.
1159d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay  bool isInSystemMacro(SourceLocation loc) {
1160d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay    return loc.isMacroID() && isInSystemHeader(getSpellingLoc(loc));
1161d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay  }
1162d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay
11632c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief The size of the SLocEnty that \p FID represents.
1164984e42ca1ff7775ce39372c314f1cb7d6862c4c7Argyrios Kyrtzidis  unsigned getFileIDSize(FileID FID) const;
116554232ade44d31e98ea83f43ca066128e315dcbdaArgyrios Kyrtzidis
11662c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Given a specific FileID, returns true if \p Loc is inside that
11672c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// FileID chunk and sets relative offset (offset of \p Loc from beginning
11682c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// of FileID) to \p relativeOffset.
1169d60a34a4e514ec0dfddd05ef2744be104e111f45Argyrios Kyrtzidis  bool isInFileID(SourceLocation Loc, FileID FID,
1170d60a34a4e514ec0dfddd05ef2744be104e111f45Argyrios Kyrtzidis                  unsigned *RelativeOffset = 0) const {
1171d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis    unsigned Offs = Loc.getOffset();
1172d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis    if (isOffsetInFileID(FID, Offs)) {
1173d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis      if (RelativeOffset)
1174d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis        *RelativeOffset = Offs - getSLocEntry(FID).getOffset();
1175d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis      return true;
1176d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis    }
1177d60a34a4e514ec0dfddd05ef2744be104e111f45Argyrios Kyrtzidis
1178d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis    return false;
1179d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis  }
1180469244a322dd5d35cee1d02d70a2edbc12ac5ce7Argyrios Kyrtzidis
118106a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
11825b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  // Line Table Manipulation Routines
11835b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  //===--------------------------------------------------------------------===//
11841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11855b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  /// getLineTableFilenameID - Return the uniqued ID for the specified filename.
11861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ///
1187686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  unsigned getLineTableFilenameID(StringRef Str);
11881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11894c4ea17d7f991516c37a871dfa4bbe5723fa85f0Chris Lattner  /// AddLineNote - Add a line note to the line table for the FileID and offset
11904c4ea17d7f991516c37a871dfa4bbe5723fa85f0Chris Lattner  /// specified by Loc.  If FilenameID is -1, it is considered to be
11914c4ea17d7f991516c37a871dfa4bbe5723fa85f0Chris Lattner  /// unspecified.
11924c4ea17d7f991516c37a871dfa4bbe5723fa85f0Chris Lattner  void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID);
11939d79ebac47ffde6a1cb312f4c09b66b1b9a397fbChris Lattner  void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID,
11941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                   bool IsFileEntry, bool IsFileExit,
11959d79ebac47ffde6a1cb312f4c09b66b1b9a397fbChris Lattner                   bool IsSystemHeader, bool IsExternCHeader);
1196bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
1197bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  /// \brief Determine if the source manager has a line table.
1198bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  bool hasLineTable() const { return LineTable != 0; }
1199bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
1200bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  /// \brief Retrieve the stored line table.
1201bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  LineTableInfo &getLineTable();
1202bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
12035b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  //===--------------------------------------------------------------------===//
1204457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek  // Queries for performance analysis.
1205457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek  //===--------------------------------------------------------------------===//
1206457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek
1207457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek  /// Return the total amount of physical memory allocated by the
1208457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek  /// ContentCache allocator.
1209457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek  size_t getContentCacheSize() const {
1210457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek    return ContentCacheAlloc.getTotalMemory();
1211457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek  }
12125330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1213f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek  struct MemoryBufferSizes {
1214f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek    const size_t malloc_bytes;
1215f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek    const size_t mmap_bytes;
12165330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1217f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek    MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)
1218f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek      : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {}
1219f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek  };
1220f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek
1221f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek  /// Return the amount of memory used by memory buffers, breaking down
1222f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek  /// by heap-backed versus mmap'ed memory.
1223f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek  MemoryBufferSizes getMemoryBufferSizes() const;
12245330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1225ca7dc2b755eb81ac95121ce1a1f1aa44a4a0fe12Ted Kremenek  // Return the amount of memory used for various side tables and
1226ca7dc2b755eb81ac95121ce1a1f1aa44a4a0fe12Ted Kremenek  // data structures in the SourceManager.
1227ca7dc2b755eb81ac95121ce1a1f1aa44a4a0fe12Ted Kremenek  size_t getDataStructureSizes() const;
1228457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek
1229457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek  //===--------------------------------------------------------------------===//
123006a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  // Other miscellaneous methods.
123106a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
123210b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis
123310b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  /// \brief Get the source location for the given file:line:col triplet.
123410b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  ///
123510b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  /// If the source file is included multiple times, the source location will
123610b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  /// be based upon the first inclusion.
1237ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  SourceLocation translateFileLineCol(const FileEntry *SourceFile,
1238507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis                                      unsigned Line, unsigned Col) const;
1239ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis
1240b201e16e0c331b0bdeae7b30f9f79aae32beb1b2Argyrios Kyrtzidis  /// \brief Get the FileID for the given file.
1241b201e16e0c331b0bdeae7b30f9f79aae32beb1b2Argyrios Kyrtzidis  ///
1242b201e16e0c331b0bdeae7b30f9f79aae32beb1b2Argyrios Kyrtzidis  /// If the source file is included multiple times, the FileID will be the
1243b201e16e0c331b0bdeae7b30f9f79aae32beb1b2Argyrios Kyrtzidis  /// first inclusion.
1244b201e16e0c331b0bdeae7b30f9f79aae32beb1b2Argyrios Kyrtzidis  FileID translateFile(const FileEntry *SourceFile) const;
1245b201e16e0c331b0bdeae7b30f9f79aae32beb1b2Argyrios Kyrtzidis
12462c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Get the source location in \p FID for the given line:col.
12472c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// Returns null location if \p FID is not a file SLocEntry.
1248507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  SourceLocation translateLineCol(FileID FID,
1249507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis                                  unsigned Line, unsigned Col) const;
1250efa2ff8603dae51f5f5ed7509a503f477498ad22Argyrios Kyrtzidis
12512c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief If \p Loc points inside a function macro argument, the returned
1252ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  /// location will be the macro location in which the argument was expanded.
1253ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  /// If a macro argument is used multiple times, the expanded location will
1254ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  /// be at the first expansion of the argument.
1255ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  /// e.g.
1256ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  ///   MY_MACRO(foo);
1257ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  ///             ^
1258ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  /// Passing a file location pointing at 'foo', will yield a macro location
1259ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  /// where 'foo' was expanded into.
1260507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  SourceLocation getMacroArgExpandedLocation(SourceLocation Loc) const;
12611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12622aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis  /// \brief Determines the order of 2 source locations in the translation unit.
12632aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis  ///
12642aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis  /// \returns true if LHS source location comes before RHS, false otherwise.
12652aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis  bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const;
12662aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis
1267aec230d29835285777ecc467e268c83b33a2addeArgyrios Kyrtzidis  /// \brief Comparison function class.
1268aec230d29835285777ecc467e268c83b33a2addeArgyrios Kyrtzidis  class LocBeforeThanCompare : public std::binary_function<SourceLocation,
1269aec230d29835285777ecc467e268c83b33a2addeArgyrios Kyrtzidis                                                         SourceLocation, bool> {
1270aec230d29835285777ecc467e268c83b33a2addeArgyrios Kyrtzidis    SourceManager &SM;
1271aec230d29835285777ecc467e268c83b33a2addeArgyrios Kyrtzidis
1272aec230d29835285777ecc467e268c83b33a2addeArgyrios Kyrtzidis  public:
1273aec230d29835285777ecc467e268c83b33a2addeArgyrios Kyrtzidis    explicit LocBeforeThanCompare(SourceManager &SM) : SM(SM) { }
1274aec230d29835285777ecc467e268c83b33a2addeArgyrios Kyrtzidis
1275aec230d29835285777ecc467e268c83b33a2addeArgyrios Kyrtzidis    bool operator()(SourceLocation LHS, SourceLocation RHS) const {
1276aec230d29835285777ecc467e268c83b33a2addeArgyrios Kyrtzidis      return SM.isBeforeInTranslationUnit(LHS, RHS);
1277aec230d29835285777ecc467e268c83b33a2addeArgyrios Kyrtzidis    }
1278aec230d29835285777ecc467e268c83b33a2addeArgyrios Kyrtzidis  };
1279aec230d29835285777ecc467e268c83b33a2addeArgyrios Kyrtzidis
1280b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis  /// \brief Determines the order of 2 source locations in the "source location
1281b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis  /// address space".
12825d579e7b0463a2e65328df85e4fed8e07799fd9eArgyrios Kyrtzidis  bool isBeforeInSLocAddrSpace(SourceLocation LHS, SourceLocation RHS) const {
12835d579e7b0463a2e65328df85e4fed8e07799fd9eArgyrios Kyrtzidis    return isBeforeInSLocAddrSpace(LHS, RHS.getOffset());
1284b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis  }
1285b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis
1286b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis  /// \brief Determines the order of a source location and a source location
1287b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis  /// offset in the "source location address space".
1288f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///
12895330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher  /// Note that we always consider source locations loaded from
12905d579e7b0463a2e65328df85e4fed8e07799fd9eArgyrios Kyrtzidis  bool isBeforeInSLocAddrSpace(SourceLocation LHS, unsigned RHS) const {
1291f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    unsigned LHSOffset = LHS.getOffset();
1292f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    bool LHSLoaded = LHSOffset >= CurrentLoadedOffset;
1293f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    bool RHSLoaded = RHS >= CurrentLoadedOffset;
1294f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    if (LHSLoaded == RHSLoaded)
12955d579e7b0463a2e65328df85e4fed8e07799fd9eArgyrios Kyrtzidis      return LHSOffset < RHS;
12965330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1297f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    return LHSLoaded;
1298b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis  }
1299b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis
1300c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner  // Iterators over FileInfos.
13010d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>
13020d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner      ::const_iterator fileinfo_iterator;
1303c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner  fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); }
1304c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner  fileinfo_iterator fileinfo_end() const { return FileInfos.end(); }
1305d93256e55673a17d18543397ec462416acb13792Douglas Gregor  bool hasFileInfo(const FileEntry *File) const {
1306d93256e55673a17d18543397ec462416acb13792Douglas Gregor    return FileInfos.find(File) != FileInfos.end();
1307d93256e55673a17d18543397ec462416acb13792Douglas Gregor  }
1308c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner
13095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// PrintStats - Print statistics to stderr.
13105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
13115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void PrintStats() const;
131278d85f53b093867bbb0123f016956178eea7343eTed Kremenek
1313f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief Get the number of local SLocEntries we have.
1314f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  unsigned local_sloc_entry_size() const { return LocalSLocEntryTable.size(); }
13155330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1316f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief Get a local SLocEntry. This is exposed for indexing.
13175330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher  const SrcMgr::SLocEntry &getLocalSLocEntry(unsigned Index,
1318f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor                                             bool *Invalid = 0) const {
1319f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    assert(Index < LocalSLocEntryTable.size() && "Invalid index");
1320f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    return LocalSLocEntryTable[Index];
1321bdfe48ac80573e026595af91e541474dbf02565fDouglas Gregor  }
13225330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1323f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief Get the number of loaded SLocEntries we have.
1324f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  unsigned loaded_sloc_entry_size() const { return LoadedSLocEntryTable.size();}
13255330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1326f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief Get a loaded SLocEntry. This is exposed for indexing.
132770042f5d1ca378138f90b7b9384023701f5d03d8David Blaikie  const SrcMgr::SLocEntry &getLoadedSLocEntry(unsigned Index,
132870042f5d1ca378138f90b7b9384023701f5d03d8David Blaikie                                              bool *Invalid = 0) const {
1329f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    assert(Index < LoadedSLocEntryTable.size() && "Invalid index");
1330a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis    if (SLocEntryLoaded[Index])
1331a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis      return LoadedSLocEntryTable[Index];
1332a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis    return loadSLocEntry(Index, Invalid);
1333f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  }
13345330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1335e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor  const SrcMgr::SLocEntry &getSLocEntry(FileID FID, bool *Invalid = 0) const {
1336c705d2520a51de1dc38d36efada8e9bc2d8b0d1fArgyrios Kyrtzidis    if (FID.ID == 0 || FID.ID == -1) {
1337c705d2520a51de1dc38d36efada8e9bc2d8b0d1fArgyrios Kyrtzidis      if (Invalid) *Invalid = true;
1338c705d2520a51de1dc38d36efada8e9bc2d8b0d1fArgyrios Kyrtzidis      return LocalSLocEntryTable[0];
1339c705d2520a51de1dc38d36efada8e9bc2d8b0d1fArgyrios Kyrtzidis    }
1340f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    return getSLocEntryByID(FID.ID);
1341bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  }
1342bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
1343f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  unsigned getNextLocalOffset() const { return NextLocalOffset; }
13445330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1345f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  void setExternalSLocEntrySource(ExternalSLocEntrySource *Source) {
1346f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    assert(LoadedSLocEntryTable.empty() &&
1347f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor           "Invalidating existing loaded entries");
1348f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    ExternalSLocEntries = Source;
1349f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  }
13505330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1351f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief Allocate a number of loaded SLocEntries, which will be actually
1352f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// loaded on demand from the external source.
1353f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///
1354f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// NumSLocEntries will be allocated, which occupy a total of TotalSize space
1355f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// in the global source view. The lowest ID and the base offset of the
1356f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// entries will be returned.
1357f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  std::pair<int, unsigned>
1358f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  AllocateLoadedSLocEntries(unsigned NumSLocEntries, unsigned TotalSize);
13595330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
13602c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Returns true if \p Loc came from a PCH/Module.
1361aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis  bool isLoadedSourceLocation(SourceLocation Loc) const {
1362aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis    return Loc.getOffset() >= CurrentLoadedOffset;
1363aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis  }
1364aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis
13652c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Returns true if \p Loc did not come from a PCH/Module.
1366aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis  bool isLocalSourceLocation(SourceLocation Loc) const {
1367aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis    return Loc.getOffset() < NextLocalOffset;
1368aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis  }
1369aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis
13702c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Returns true if \p FID came from a PCH/Module.
1371718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis  bool isLoadedFileID(FileID FID) const {
1372718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis    assert(FID.ID != -1 && "Using FileID sentinel value");
1373718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis    return FID.ID < 0;
1374718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis  }
1375718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis
13762c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Returns true if \p FID did not come from a PCH/Module.
1377718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis  bool isLocalFileID(FileID FID) const {
1378718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis    return !isLoadedFileID(FID);
1379718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis  }
1380718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis
13815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerprivate:
1382e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor  const llvm::MemoryBuffer *getFakeBufferForRecovery() const;
1383a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis  const SrcMgr::ContentCache *getFakeContentCacheForRecovery() const;
1384a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis
1385a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis  const SrcMgr::SLocEntry &loadSLocEntry(unsigned Index, bool *Invalid) const;
1386e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor
1387f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief Get the entry with the given unwrapped FileID.
1388f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  const SrcMgr::SLocEntry &getSLocEntryByID(int ID) const {
1389f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    assert(ID != -1 && "Using FileID sentinel value");
1390f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    if (ID < 0)
1391f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor      return getLoadedSLocEntryByID(ID);
1392f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    return getLocalSLocEntry(static_cast<unsigned>(ID));
1393f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  }
13945330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1395a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis  const SrcMgr::SLocEntry &getLoadedSLocEntryByID(int ID,
1396a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis                                                  bool *Invalid = 0) const {
1397a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis    return getLoadedSLocEntry(static_cast<unsigned>(-ID - 2), Invalid);
1398f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  }
13995330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1400bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth  /// createExpansionLoc - Implements the common elements of storing an
14013201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// expansion info struct into the SLocEntry table and producing a source
1402c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth  /// location that refers to it.
140378df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth  SourceLocation createExpansionLocImpl(const SrcMgr::ExpansionInfo &Expansion,
1404bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                        unsigned TokLength,
1405bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                        int LoadedID = 0,
1406bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                        unsigned LoadedOffset = 0);
1407c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth
1408de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// isOffsetInFileID - Return true if the specified FileID contains the
1409de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// specified SourceLocation offset.  This is a very hot method.
1410de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const {
1411de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID);
1412de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    // If the entry is after the offset, it can't contain it.
1413de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (SLocOffset < Entry.getOffset()) return false;
14141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1415f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    // If this is the very last entry then it does.
1416f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    if (FID.ID == -2)
1417f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor      return true;
1418f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
1419f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    // If it is the last local entry, then it does if the location is local.
1420f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    if (static_cast<unsigned>(FID.ID+1) == LocalSLocEntryTable.size()) {
1421f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor      return SLocOffset < NextLocalOffset;
1422f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    }
14237f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
1424f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    // Otherwise, the entry after it has to not include it. This works for both
1425f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    // local and loaded entries.
14267f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    return SLocOffset < getSLocEntry(FileID::get(FID.ID+1)).getOffset();
1427de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
14281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
142978d85f53b093867bbb0123f016956178eea7343eTed Kremenek  /// createFileID - Create a new fileID for the specified ContentCache and
143078d85f53b093867bbb0123f016956178eea7343eTed Kremenek  ///  include position.  This works regardless of whether the ContentCache
143178d85f53b093867bbb0123f016956178eea7343eTed Kremenek  ///  corresponds to a file or some other input source.
14322b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID createFileID(const SrcMgr::ContentCache* File,
14332b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner                      SourceLocation IncludePos,
14347f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                      SrcMgr::CharacteristicKind DirCharacter,
1435f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor                      int LoadedID, unsigned LoadedOffset);
14361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1437de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  const SrcMgr::ContentCache *
1438de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    getOrCreateContentCache(const FileEntry *SourceFile);
1439c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek
144078d85f53b093867bbb0123f016956178eea7343eTed Kremenek  /// createMemBufferContentCache - Create a new ContentCache for the specified
144178d85f53b093867bbb0123f016956178eea7343eTed Kremenek  ///  memory buffer.
14421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const SrcMgr::ContentCache*
14432b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  createMemBufferContentCache(const llvm::MemoryBuffer *Buf);
14441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1445de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  FileID getFileIDSlow(unsigned SLocOffset) const;
1446f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  FileID getFileIDLocal(unsigned SLocOffset) const;
1447f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  FileID getFileIDLoaded(unsigned SLocOffset) const;
1448de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
1449f84ef95ecec34f27fd05eb4e0392ca6bd3bd0be0Chandler Carruth  SourceLocation getExpansionLocSlowCase(SourceLocation Loc) const;
1450addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner  SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;
1451796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis  SourceLocation getFileLocSlowCase(SourceLocation Loc) const;
1452addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner
1453de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned>
1454e7b2b6e87dbe5b1207f77b6ff9c210a02f95bb39Chandler Carruth  getDecomposedExpansionLocSlowCase(const SrcMgr::SLocEntry *E) const;
1455de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned>
1456de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
1457de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner                                   unsigned Offset) const;
1458fb3612ef197cb8532c05f33889ec1aed7c26e5cbArgyrios Kyrtzidis  void computeMacroArgsCache(MacroArgsMap *&MacroArgsCache, FileID FID) const;
1459ac1ffcc55b861737ba2466cd1ca1accd8eafceaaArgyrios Kyrtzidis
1460ac1ffcc55b861737ba2466cd1ca1accd8eafceaaArgyrios Kyrtzidis  friend class ASTReader;
1461ac1ffcc55b861737ba2466cd1ca1accd8eafceaaArgyrios Kyrtzidis  friend class ASTWriter;
14625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
14635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}  // end namespace clang
14665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif
1468