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"
240d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner#include "llvm/ADT/DenseMap.h"
25f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek#include "llvm/Support/MemoryBuffer.h"
26d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis#include <map>
275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include <vector>
289dc62f044a6ba21f503bd56607d94b32704e7945Chris Lattner#include <cassert>
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace clang {
311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikieclass DiagnosticsEngine;
335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass SourceManager;
34099b4747042352f69184481a48508b599a8d3f73Ted Kremenekclass FileManager;
355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass FileEntry;
365b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattnerclass LineTableInfo;
37b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidisclass LangOptions;
38d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidisclass ASTWriter;
39d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidisclass ASTReader;
405330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
4129f39425fc7fcaede88a7e573f392975f8dc5e3eEric Christopher/// There are three different types of locations in a file: a spelling
4229f39425fc7fcaede88a7e573f392975f8dc5e3eEric Christopher/// location, an expansion location, and a presumed location.
4329f39425fc7fcaede88a7e573f392975f8dc5e3eEric Christopher///
4429f39425fc7fcaede88a7e573f392975f8dc5e3eEric Christopher/// Given an example of:
4529f39425fc7fcaede88a7e573f392975f8dc5e3eEric Christopher/// #define min(x, y) x < y ? x : y
4629f39425fc7fcaede88a7e573f392975f8dc5e3eEric Christopher///
4729f39425fc7fcaede88a7e573f392975f8dc5e3eEric Christopher/// and then later on a use of min:
4829f39425fc7fcaede88a7e573f392975f8dc5e3eEric Christopher/// #line 17
495330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher/// return min(a, b);
5029f39425fc7fcaede88a7e573f392975f8dc5e3eEric Christopher///
5129f39425fc7fcaede88a7e573f392975f8dc5e3eEric Christopher/// The expansion location is the line in the source code where the macro
5229f39425fc7fcaede88a7e573f392975f8dc5e3eEric Christopher/// was expanded (the return statement), the spelling location is the
5329f39425fc7fcaede88a7e573f392975f8dc5e3eEric Christopher/// location in the source where the macro was originally defined,
5429f39425fc7fcaede88a7e573f392975f8dc5e3eEric Christopher/// and the presumed location is where the line directive states that
5529f39425fc7fcaede88a7e573f392975f8dc5e3eEric Christopher/// the line is 17, or any other line.
5629f39425fc7fcaede88a7e573f392975f8dc5e3eEric Christopher
570b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner/// SrcMgr - Public enums and private classes that are part of the
580b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner/// SourceManager implementation.
595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace SrcMgr {
619d72851fec9e9c62570a027d42701562bbf29751Chris Lattner  /// CharacteristicKind - This is used to represent whether a file or directory
620b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner  /// holds normal user code, system code, or system code which is implicitly
630b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner  /// 'extern "C"' in C++ mode.  Entire directories can be tagged with this
640b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner  /// (this is maintained by DirectoryLookup and friends) as can specific
65f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// FileInfos when a #pragma system_header is seen or various other cases.
660b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner  ///
679d72851fec9e9c62570a027d42701562bbf29751Chris Lattner  enum CharacteristicKind {
680b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner    C_User, C_System, C_ExternCSystem
690b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner  };
701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
714710a8ea766b45079bf1c1dd36e29b59bb90829dDan Gohman  /// ContentCache - One instance of this struct is kept for every file
7206a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  /// loaded or used.  This object owns the MemoryBuffer object.
73c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek  class ContentCache {
74f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    enum CCFlags {
75f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor      /// \brief Whether the buffer is invalid.
76f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor      InvalidFlag = 0x01,
77f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor      /// \brief Whether the buffer should not be freed on destruction.
78f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor      DoNotFreeFlag = 0x02
79f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    };
805330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
81c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    /// Buffer - The actual buffer containing the characters from the input
82c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    /// file.  This is owned by the ContentCache object.
83f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    /// The bits indicate indicates whether the buffer is invalid.
84f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    mutable llvm::PointerIntPair<const llvm::MemoryBuffer *, 2> Buffer;
85c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek
86c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek  public:
87b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    /// Reference to the file entry representing this ContentCache.
88b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    /// This reference does not own the FileEntry object.
89b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    /// It is possible for this to be NULL if
9078d85f53b093867bbb0123f016956178eea7343eTed Kremenek    /// the ContentCache encapsulates an imaginary text buffer.
91b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    const FileEntry *OrigEntry;
92b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis
93b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    /// \brief References the file which the contents were actually loaded from.
94b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    /// Can be different from 'Entry' if we overridden the contents of one file
95b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    /// with the contents of another file.
96b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    const FileEntry *ContentsEntry;
971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
980d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner    /// SourceLineCache - A bump pointer allocated array of offsets for each
990d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner    /// source line.  This is lazily computed.  This is owned by the
1000d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner    /// SourceManager BumpPointerAllocator object.
10105816591ec488a933dfecc9ff9f3cbf3c32767c2Chris Lattner    unsigned *SourceLineCache;
1021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
103b6427f821de8cce1566fb6e755143ea0918d5543Ted Kremenek    /// NumLines - The number of lines in this ContentCache.  This is only valid
104b6427f821de8cce1566fb6e755143ea0918d5543Ted Kremenek    /// if SourceLineCache is non-null.
105a081da5e44600d02983d6562bed1b4fd61e410fdDouglas Gregor    unsigned NumLines : 31;
10610b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis
107a081da5e44600d02983d6562bed1b4fd61e410fdDouglas Gregor    /// \brief Indicates whether the buffer itself was provided to override
108a081da5e44600d02983d6562bed1b4fd61e410fdDouglas Gregor    /// the actual file contents.
109a081da5e44600d02983d6562bed1b4fd61e410fdDouglas Gregor    ///
110a081da5e44600d02983d6562bed1b4fd61e410fdDouglas Gregor    /// When true, the original entry may be a virtual file that does not
111a081da5e44600d02983d6562bed1b4fd61e410fdDouglas Gregor    /// exist.
112a081da5e44600d02983d6562bed1b4fd61e410fdDouglas Gregor    unsigned BufferOverridden : 1;
113a081da5e44600d02983d6562bed1b4fd61e410fdDouglas Gregor
1147955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor    ContentCache(const FileEntry *Ent = 0)
1157955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor      : Buffer(0, false), OrigEntry(Ent), ContentsEntry(Ent),
1167955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor        SourceLineCache(0), NumLines(0), BufferOverridden(false) {}
1177955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor
1187955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor    ContentCache(const FileEntry *Ent, const FileEntry *contentEnt)
1197955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor      : Buffer(0, false), OrigEntry(Ent), ContentsEntry(contentEnt),
1207955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor        SourceLineCache(0), NumLines(0), BufferOverridden(false) {}
1217955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor
1227955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor    ~ContentCache();
1237955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor
1247955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor    /// The copy ctor does not allow copies where source object has either
1257955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor    ///  a non-NULL Buffer or SourceLineCache.  Ownership of allocated memory
1267955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor    ///  is not transferred, so this is a logical error.
1277955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor    ContentCache(const ContentCache &RHS)
1287955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor      : Buffer(0, false), SourceLineCache(0), BufferOverridden(false)
1297955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor    {
1307955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor      OrigEntry = RHS.OrigEntry;
1317955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor      ContentsEntry = RHS.ContentsEntry;
1327955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor
1337955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor      assert (RHS.Buffer.getPointer() == 0 && RHS.SourceLineCache == 0 &&
1347955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor              "Passed ContentCache object cannot own a buffer.");
1357955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor
1367955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor      NumLines = RHS.NumLines;
1377955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor    }
1387955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor
13936c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor    /// getBuffer - Returns the memory buffer for the associated content.
14036c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor    ///
141a92d7e7a55a35b28437103130904a6401bf35408Jonathan D. Turner    /// \param Diag Object through which diagnostics will be emitted if the
14236c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor    /// buffer cannot be retrieved.
1435330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher    ///
144e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner    /// \param Loc If specified, is the location that invalid file diagnostics
145e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner    ///     will be emitted at.
146e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner    ///
14736c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor    /// \param Invalid If non-NULL, will be set \c true if an error occurred.
148d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie    const llvm::MemoryBuffer *getBuffer(DiagnosticsEngine &Diag,
149e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner                                        const SourceManager &SM,
150e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner                                        SourceLocation Loc = SourceLocation(),
15136c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor                                        bool *Invalid = 0) const;
1521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
153c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    /// getSize - Returns the size of the content encapsulated by this
154c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    ///  ContentCache. This can be the size of the source file or the size of an
155c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    ///  arbitrary scratch buffer.  If the ContentCache encapsulates a source
156c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    ///  file this size is retrieved from the file's FileEntry.
157c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    unsigned getSize() const;
1581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
159c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    /// getSizeBytesMapped - Returns the number of bytes actually mapped for
1603201f382956ed9beee9fb31229c2835c1208889cChandler Carruth    /// this ContentCache. This can be 0 if the MemBuffer was not actually
1613201f382956ed9beee9fb31229c2835c1208889cChandler Carruth    /// expanded.
162c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    unsigned getSizeBytesMapped() const;
1635330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
164f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek    /// Returns the kind of memory used to back the memory buffer for
165f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek    /// this content cache.  This is used for performance analysis.
166f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek    llvm::MemoryBuffer::BufferKind getMemoryBufferKind() const;
1671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16805816591ec488a933dfecc9ff9f3cbf3c32767c2Chris Lattner    void setBuffer(const llvm::MemoryBuffer *B) {
169c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor      assert(!Buffer.getPointer() && "MemoryBuffer already set.");
170c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor      Buffer.setPointer(B);
171c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor      Buffer.setInt(false);
172c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    }
1735330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
174cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor    /// \brief Get the underlying buffer, returning NULL if the buffer is not
175cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor    /// yet available.
176cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor    const llvm::MemoryBuffer *getRawBuffer() const {
177cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor      return Buffer.getPointer();
178cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor    }
1791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1802968442603b029949246467253eeac8139a5b6d8Douglas Gregor    /// \brief Replace the existing buffer (which will be deleted)
1812968442603b029949246467253eeac8139a5b6d8Douglas Gregor    /// with the given buffer.
182f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    void replaceBuffer(const llvm::MemoryBuffer *B, bool DoNotFree = false);
1832968442603b029949246467253eeac8139a5b6d8Douglas Gregor
184f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    /// \brief Determine whether the buffer itself is invalid.
185f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    bool isBufferInvalid() const {
186f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor      return Buffer.getInt() & InvalidFlag;
187f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    }
1885330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
189f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    /// \brief Determine whether the buffer should be freed.
190f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    bool shouldFreeBuffer() const {
191f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor      return (Buffer.getInt() & DoNotFreeFlag) == 0;
192f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    }
1935330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1940d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek  private:
1950d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek    // Disable assignments.
1961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    ContentCache &operator=(const ContentCache& RHS);
1971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  };
1985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
199de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// FileInfo - Information about a FileID, basically just the logical file
200de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// that it represents and include stack information.
2015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
202de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// Each FileInfo has include stack information, indicating where it came
2033201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// from. This information encodes the #include chain that a token was
2043201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// expanded from. The main include file has an invalid IncludeLoc.
2055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
206de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// FileInfos contain a "ContentCache *", with the contents of the file.
2075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
208de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  class FileInfo {
2095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    /// IncludeLoc - The location of the #include that brought in this file.
210de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    /// This is an invalid SLOC for the main file (top of the #include chain).
211de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned IncludeLoc;  // Really a SourceLocation
2121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
213d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    /// \brief Number of FileIDs (files and macros) that were created during
214d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    /// preprocessing of this #include, including this SLocEntry.
215d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    /// Zero means the preprocessor didn't provide such info for this SLocEntry.
216d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    unsigned NumCreatedFIDs;
217d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis
2186e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    /// Data - This contains the ContentCache* and the bits indicating the
2196e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    /// characteristic of the file and whether it has #line info, all bitmangled
2206e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    /// together.
2216e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    uintptr_t Data;
222d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis
22321032df7a0dfc129a8f0c5e004811b455baafb7aArgyrios Kyrtzidis    friend class clang::SourceManager;
22421032df7a0dfc129a8f0c5e004811b455baafb7aArgyrios Kyrtzidis    friend class clang::ASTWriter;
22521032df7a0dfc129a8f0c5e004811b455baafb7aArgyrios Kyrtzidis    friend class clang::ASTReader;
22678d85f53b093867bbb0123f016956178eea7343eTed Kremenek  public:
227de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    /// get - Return a FileInfo object.
228de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    static FileInfo get(SourceLocation IL, const ContentCache *Con,
229de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner                        CharacteristicKind FileCharacter) {
230de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      FileInfo X;
231de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      X.IncludeLoc = IL.getRawEncoding();
232d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis      X.NumCreatedFIDs = 0;
2336e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner      X.Data = (uintptr_t)Con;
23400282d6e1194655a2e89f940bd6fa8484b52e666Chris Lattner      assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned");
2356e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner      assert((unsigned)FileCharacter < 4 && "invalid file character");
2366e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner      X.Data |= (unsigned)FileCharacter;
2375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return X;
2385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
2391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
240de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    SourceLocation getIncludeLoc() const {
241de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return SourceLocation::getFromRawEncoding(IncludeLoc);
242de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
2436e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    const ContentCache* getContentCache() const {
24400282d6e1194655a2e89f940bd6fa8484b52e666Chris Lattner      return reinterpret_cast<const ContentCache*>(Data & ~7UL);
2456e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    }
2461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2470b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner    /// getCharacteristic - Return whether this is a system header or not.
2481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    CharacteristicKind getFileCharacteristic() const {
2496e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner      return (CharacteristicKind)(Data & 3);
2500b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner    }
251ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner
252ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    /// hasLineDirectives - Return true if this FileID has #line directives in
253ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    /// it.
254ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    bool hasLineDirectives() const { return (Data & 4) != 0; }
2551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
256ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    /// setHasLineDirectives - Set the flag that indicates that this FileID has
257ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    /// line table entries associated with it.
258ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    void setHasLineDirectives() {
259ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner      Data |= 4;
260ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    }
2619dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  };
2621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26378df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth  /// ExpansionInfo - Each ExpansionInfo encodes the expansion location - where
26478df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth  /// the token was ultimately expanded, and the SpellingLoc - where the actual
26578df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth  /// character data for the token came from.
26678df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth  class ExpansionInfo {
26778df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    // Really these are all SourceLocations.
2681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
269e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    /// SpellingLoc - Where the spelling for the token can be found.
270e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    unsigned SpellingLoc;
2711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27278df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// ExpansionLocStart/ExpansionLocEnd - In a macro expansion, these
27378df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// indicate the start and end of the expansion. In object-like macros,
2743201f382956ed9beee9fb31229c2835c1208889cChandler Carruth    /// these will be the same. In a function-like macro expansion, the start
2753201f382956ed9beee9fb31229c2835c1208889cChandler Carruth    /// will be the identifier and the end will be the ')'. Finally, in
276c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    /// macro-argument instantitions, the end will be 'SourceLocation()', an
277c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    /// invalid location.
27878df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    unsigned ExpansionLocStart, ExpansionLocEnd;
279c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth
2809dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  public:
281de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    SourceLocation getSpellingLoc() const {
282de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return SourceLocation::getFromRawEncoding(SpellingLoc);
283de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
28478df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    SourceLocation getExpansionLocStart() const {
28578df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth      return SourceLocation::getFromRawEncoding(ExpansionLocStart);
286e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    }
28778df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    SourceLocation getExpansionLocEnd() const {
288c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth      SourceLocation EndLoc =
28978df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth        SourceLocation::getFromRawEncoding(ExpansionLocEnd);
29078df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth      return EndLoc.isInvalid() ? getExpansionLocStart() : EndLoc;
291e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    }
2921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29378df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    std::pair<SourceLocation,SourceLocation> getExpansionLocRange() const {
29478df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth      return std::make_pair(getExpansionLocStart(), getExpansionLocEnd());
295e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    }
2961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29796d3589e523a04f4ff2058a7919226ce60696ae8Chandler Carruth    bool isMacroArgExpansion() const {
298c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth      // Note that this needs to return false for default constructed objects.
29978df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth      return getExpansionLocStart().isValid() &&
30078df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth        SourceLocation::getFromRawEncoding(ExpansionLocEnd).isInvalid();
301c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    }
302c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth
303cee5ec9df479994e4ba27fb65b7ded5bb5a980ebArgyrios Kyrtzidis    bool isFunctionMacroExpansion() const {
304cee5ec9df479994e4ba27fb65b7ded5bb5a980ebArgyrios Kyrtzidis      return getExpansionLocStart().isValid() &&
305cee5ec9df479994e4ba27fb65b7ded5bb5a980ebArgyrios Kyrtzidis          getExpansionLocStart() != getExpansionLocEnd();
306cee5ec9df479994e4ba27fb65b7ded5bb5a980ebArgyrios Kyrtzidis    }
307cee5ec9df479994e4ba27fb65b7ded5bb5a980ebArgyrios Kyrtzidis
30878df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// create - Return a ExpansionInfo for an expansion. Start and End specify
30978df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// the expansion range (where the macro is expanded), and SpellingLoc
31078df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// specifies the spelling location (where the characters from the token
31178df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// come from). All three can refer to normal File SLocs or expansion
31278df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// locations.
31378df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    static ExpansionInfo create(SourceLocation SpellingLoc,
31478df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth                                SourceLocation Start, SourceLocation End) {
31578df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth      ExpansionInfo X;
31678df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth      X.SpellingLoc = SpellingLoc.getRawEncoding();
31778df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth      X.ExpansionLocStart = Start.getRawEncoding();
31878df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth      X.ExpansionLocEnd = End.getRawEncoding();
3199dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner      return X;
3205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
321c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth
32278df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// createForMacroArg - Return a special ExpansionInfo for the expansion of
32378df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// a macro argument into a function-like macro's body. ExpansionLoc
32478df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// specifies the expansion location (where the macro is expanded). This
32578df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// doesn't need to be a range because a macro is always expanded at
32678df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// a macro parameter reference, and macro parameters are always exactly
32778df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// one token. SpellingLoc specifies the spelling location (where the
32878df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// characters from the token come from). ExpansionLoc and SpellingLoc can
32978df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// both refer to normal File SLocs or expansion locations.
330c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    ///
331c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    /// Given the code:
332c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    /// \code
333c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    ///   #define F(x) f(x)
334c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    ///   F(42);
335c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    /// \endcode
336c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    ///
33778df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// When expanding '\c F(42)', the '\c x' would call this with an
33878df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// SpellingLoc pointing at '\c 42' anad an ExpansionLoc pointing at its
33978df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// location in the definition of '\c F'.
34078df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    static ExpansionInfo createForMacroArg(SourceLocation SpellingLoc,
34178df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth                                           SourceLocation ExpansionLoc) {
342c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth      // We store an intentionally invalid source location for the end of the
34378df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth      // expansion range to mark that this is a macro argument ion rather than
34478df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth      // a normal one.
34578df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth      return create(SpellingLoc, ExpansionLoc, SourceLocation());
346c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    }
347de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  };
3481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
349de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// SLocEntry - This is a discriminated union of FileInfo and
35078df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth  /// ExpansionInfo.  SourceManager keeps an array of these objects, and
351de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// they are uniquely identified by the FileID datatype.
352de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  class SLocEntry {
3533201f382956ed9beee9fb31229c2835c1208889cChandler Carruth    unsigned Offset;   // low bit is set for expansion info.
354de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    union {
355de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      FileInfo File;
3561728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth      ExpansionInfo Expansion;
357de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    };
358de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  public:
359de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned getOffset() const { return Offset >> 1; }
3601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3611728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth    bool isExpansion() const { return Offset & 1; }
3621728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth    bool isFile() const { return !isExpansion(); }
3631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
364de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    const FileInfo &getFile() const {
365de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      assert(isFile() && "Not a file SLocEntry!");
366de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return File;
367de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
368de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
3691728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth    const ExpansionInfo &getExpansion() const {
3701728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth      assert(isExpansion() && "Not a macro expansion SLocEntry!");
3711728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth      return Expansion;
372de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
3731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
374de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    static SLocEntry get(unsigned Offset, const FileInfo &FI) {
375de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      SLocEntry E;
376de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      E.Offset = Offset << 1;
377de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      E.File = FI;
378de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return E;
379de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
380de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
38178df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    static SLocEntry get(unsigned Offset, const ExpansionInfo &Expansion) {
382de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      SLocEntry E;
383de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      E.Offset = (Offset << 1) | 1;
3841728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth      E.Expansion = Expansion;
385de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return E;
386de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
3875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  };
3885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}  // end SrcMgr namespace.
3897f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
3907f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor/// \brief External source of source location entries.
3917f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregorclass ExternalSLocEntrySource {
3927f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregorpublic:
3937f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  virtual ~ExternalSLocEntrySource();
3947f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
395f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief Read the source location entry with index ID, which will always be
396f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// less than -1.
397e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor  ///
398e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor  /// \returns true if an error occurred that prevented the source-location
399e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor  /// entry from being loaded.
400f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  virtual bool ReadSLocEntry(int ID) = 0;
4017f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor};
4025330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
403dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner
404dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner/// IsBeforeInTranslationUnitCache - This class holds the cache used by
405dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner/// isBeforeInTranslationUnit.  The cache structure is complex enough to be
406dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner/// worth breaking out of SourceManager.
407dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattnerclass IsBeforeInTranslationUnitCache {
408dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// L/R QueryFID - These are the FID's of the cached query.  If these match up
409dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// with a subsequent query, the result can be reused.
410dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  FileID LQueryFID, RQueryFID;
41137e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis
41237e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis  /// \brief True if LQueryFID was created before RQueryFID. This is used
41337e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis  /// to compare macro expansion locations.
41437e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis  bool IsLQFIDBeforeRQFID;
41537e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis
416dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// CommonFID - This is the file found in common between the two #include
417dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// traces.  It is the nearest common ancestor of the #include tree.
418dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  FileID CommonFID;
4195330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
420dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// L/R CommonOffset - This is the offset of the previous query in CommonFID.
421dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// Usually, this represents the location of the #include for QueryFID, but if
422dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// LQueryFID is a parent of RQueryFID (or vise versa) then these can be a
423dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// random token in the parent.
424dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  unsigned LCommonOffset, RCommonOffset;
425dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattnerpublic:
4265330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
427dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// isCacheValid - Return true if the currently cached values match up with
428dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// the specified LHS/RHS query.  If not, we can't use the cache.
429dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  bool isCacheValid(FileID LHS, FileID RHS) const {
430dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    return LQueryFID == LHS && RQueryFID == RHS;
431dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  }
4325330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
433dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// getCachedResult - If the cache is valid, compute the result given the
434dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// specified offsets in the LHS/RHS FID's.
435dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  bool getCachedResult(unsigned LOffset, unsigned ROffset) const {
436dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    // If one of the query files is the common file, use the offset.  Otherwise,
437dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    // use the #include loc in the common file.
438dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    if (LQueryFID != CommonFID) LOffset = LCommonOffset;
439dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    if (RQueryFID != CommonFID) ROffset = RCommonOffset;
44037e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis
44137e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis    // It is common for multiple macro expansions to be "included" from the same
44237e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis    // location (expansion location), in which case use the order of the FileIDs
4434d1cbcf6b9aaae7f82c6e332f46b2ad2d8971bd0Argyrios Kyrtzidis    // to determine which came first. This will also take care the case where
4444d1cbcf6b9aaae7f82c6e332f46b2ad2d8971bd0Argyrios Kyrtzidis    // one of the locations points at the inclusion/expansion point of the other
4454d1cbcf6b9aaae7f82c6e332f46b2ad2d8971bd0Argyrios Kyrtzidis    // in which case its FileID will come before the other.
446d7711ec430fde5706f85ba6c4b85283a8e743ff7Argyrios Kyrtzidis    if (LOffset == ROffset)
44737e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis      return IsLQFIDBeforeRQFID;
44837e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis
449dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    return LOffset < ROffset;
450dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  }
4515330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
452dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  // Set up a new query.
45337e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis  void setQueryFIDs(FileID LHS, FileID RHS, bool isLFIDBeforeRFID) {
45437e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis    assert(LHS != RHS);
455dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    LQueryFID = LHS;
456dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    RQueryFID = RHS;
45737e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis    IsLQFIDBeforeRQFID = isLFIDBeforeRFID;
45837e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis  }
45937e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis
46037e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis  void clear() {
46137e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis    LQueryFID = RQueryFID = FileID();
46237e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis    IsLQFIDBeforeRQFID = false;
463dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  }
4645330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
465dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  void setCommonLoc(FileID commonFID, unsigned lCommonOffset,
466dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner                    unsigned rCommonOffset) {
467dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    CommonFID = commonFID;
468dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    LCommonOffset = lCommonOffset;
469dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    RCommonOffset = rCommonOffset;
470dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  }
4715330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
472dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner};
4737f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
474f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor/// \brief This class handles loading and caching of source files into memory.
475f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor///
476f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor/// This object owns the MemoryBuffer objects for all of the loaded
4775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// files and assigns unique FileID's for each unique #include chain.
4785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
4795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// The SourceManager can be queried for information about SourceLocation
4803201f382956ed9beee9fb31229c2835c1208889cChandler Carruth/// objects, turning them into either spelling or expansion locations. Spelling
4813201f382956ed9beee9fb31229c2835c1208889cChandler Carruth/// locations represent where the bytes corresponding to a token came from and
4823201f382956ed9beee9fb31229c2835c1208889cChandler Carruth/// expansion locations represent where the location is in the user's view. In
4833201f382956ed9beee9fb31229c2835c1208889cChandler Carruth/// the case of a macro expansion, for example, the spelling location indicates
4843201f382956ed9beee9fb31229c2835c1208889cChandler Carruth/// where the expanded token came from and the expansion location specifies
4853201f382956ed9beee9fb31229c2835c1208889cChandler Carruth/// where it was expanded.
486c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmithclass SourceManager : public RefCountedBase<SourceManager> {
487d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  /// \brief DiagnosticsEngine object.
488d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  DiagnosticsEngine &Diag;
489389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
490389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis  FileManager &FileMgr;
491389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
4920d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  mutable llvm::BumpPtrAllocator ContentCacheAlloc;
4931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// FileInfos - Memoized information about all of the files tracked by this
4950d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek  /// SourceManager.  This set allows us to merge ContentCache entries based
4960d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek  /// on their FileEntry*.  All ContentCache objects will thus have unique,
4971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// non-null, FileEntry pointers.
4980d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos;
4991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
500299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  /// \brief True if the ContentCache for files that are overriden by other
501299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  /// files, should report the original file name. Defaults to true.
502299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  bool OverridenFilesKeepOriginalName;
503299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis
504b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  /// \brief Files that have been overriden with the contents from another file.
505b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  llvm::DenseMap<const FileEntry *, const FileEntry *> OverriddenFiles;
506b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis
5075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// MemBufferInfos - Information about various memory buffers that we have
5080d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  /// read in.  All FileEntry* within the stored ContentCache objects are NULL,
5090d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  /// as they do not refer to a file.
5100d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  std::vector<SrcMgr::ContentCache*> MemBufferInfos;
5111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
512f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief The table of SLocEntries that are local to this module.
513f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///
514f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// Positive FileIDs are indexes into this table. Entry 0 indicates an invalid
5153201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// expansion.
516f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  std::vector<SrcMgr::SLocEntry> LocalSLocEntryTable;
517f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
518f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief The table of SLocEntries that are loaded from other modules.
519f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///
520f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// Negative FileIDs are indexes into this table. To get from ID to an index,
521f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// use (-ID - 2).
522a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis  mutable std::vector<SrcMgr::SLocEntry> LoadedSLocEntryTable;
5237f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
524f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief The starting offset of the next local SLocEntry.
525f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///
526f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// This is LocalSLocEntryTable.back().Offset + the size of that entry.
527f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  unsigned NextLocalOffset;
528f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
529f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief The starting offset of the latest batch of loaded SLocEntries.
530f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///
531f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// This is LoadedSLocEntryTable.back().Offset, except that that entry might
532f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// not have been loaded, so that value would be unknown.
533f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  unsigned CurrentLoadedOffset;
534f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
535ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  /// \brief The highest possible offset is 2^31-1, so CurrentLoadedOffset
536ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  /// starts at 2^31.
537ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  static const unsigned MaxLoadedOffset = 1U << 31U;
538ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis
539f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief A bitmap that indicates whether the entries of LoadedSLocEntryTable
540f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// have already been loaded from the external source.
541f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///
542f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// Same indexing as LoadedSLocEntryTable.
5437f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  std::vector<bool> SLocEntryLoaded;
5447f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
5457f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  /// \brief An external source for source location entries.
5467f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  ExternalSLocEntrySource *ExternalSLocEntries;
5477f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
548de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// LastFileIDLookup - This is a one-entry cache to speed up getFileID.
549de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// LastFileIDLookup records the last FileID looked up or created, because it
550de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// is very common to look up many tokens from the same file.
551de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  mutable FileID LastFileIDLookup;
5521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5535b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  /// LineTable - This holds information for #line directives.  It is referenced
5545b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  /// by indices from SLocEntryTable.
5555b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  LineTableInfo *LineTable;
5561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5575e36a7a89da5d69ece23fc8228624a053f75c645Chris Lattner  /// LastLineNo - These ivars serve as a cache used in the getLineNumber
5585e36a7a89da5d69ece23fc8228624a053f75c645Chris Lattner  /// method which is used to speedup getLineNumber calls to nearby locations.
5592b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  mutable FileID LastLineNoFileIDQuery;
560f812a45dd93634c9300ed5533bd26b56374714a1Chris Lattner  mutable SrcMgr::ContentCache *LastLineNoContentCache;
561f812a45dd93634c9300ed5533bd26b56374714a1Chris Lattner  mutable unsigned LastLineNoFilePos;
562f812a45dd93634c9300ed5533bd26b56374714a1Chris Lattner  mutable unsigned LastLineNoResult;
5631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
56476edd0e4ae0592a7225d50d0bad6732ac64dca2aTed Kremenek  /// MainFileID - The file ID for the main source file of the translation unit.
5652b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID MainFileID;
56649c1f4aa2a6c360d25d605004ec3c4affd62db77Steve Naroff
567507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  /// \brief The file ID for the precompiled preamble there is one.
568507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  FileID PreambleFileID;
569507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
570de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  // Statistics for -print-stats.
571de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  mutable unsigned NumLinearScans, NumBinaryProbes;
5721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5732aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis  // Cache results for the isBeforeInTranslationUnit method.
574dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  mutable IsBeforeInTranslationUnitCache IsBeforeInTUCache;
5751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
576e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor  // Cache for the "fake" buffer used for error-recovery purposes.
577e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor  mutable llvm::MemoryBuffer *FakeBufferForRecovery;
5785330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
579a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis  mutable SrcMgr::ContentCache *FakeContentCacheForRecovery;
580a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis
581fb3612ef197cb8532c05f33889ec1aed7c26e5cbArgyrios Kyrtzidis  /// \brief Lazily computed map of macro argument chunks to their expanded
582fb3612ef197cb8532c05f33889ec1aed7c26e5cbArgyrios Kyrtzidis  /// source location.
583fb3612ef197cb8532c05f33889ec1aed7c26e5cbArgyrios Kyrtzidis  typedef std::map<unsigned, SourceLocation> MacroArgsMap;
584fb3612ef197cb8532c05f33889ec1aed7c26e5cbArgyrios Kyrtzidis
58570042f5d1ca378138f90b7b9384023701f5d03d8David Blaikie  mutable llvm::DenseMap<FileID, MacroArgsMap *> MacroArgsCacheMap;
586fb3612ef197cb8532c05f33889ec1aed7c26e5cbArgyrios Kyrtzidis
58749c1f4aa2a6c360d25d605004ec3c4affd62db77Steve Naroff  // SourceManager doesn't support copy construction.
58849c1f4aa2a6c360d25d605004ec3c4affd62db77Steve Naroff  explicit SourceManager(const SourceManager&);
5891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  void operator=(const SourceManager&);
5905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
591d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  SourceManager(DiagnosticsEngine &Diag, FileManager &FileMgr);
5925b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  ~SourceManager();
5931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5945b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  void clearIDTables();
5951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
596d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  DiagnosticsEngine &getDiagnostics() const { return Diag; }
59778a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios Kyrtzidis
598389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis  FileManager &getFileManager() const { return FileMgr; }
599389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
600299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  /// \brief Set true if the SourceManager should report the original file name
601299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  /// for contents of files that were overriden by other files.Defaults to true.
602299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  void setOverridenFilesKeepOriginalName(bool value) {
603299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis    OverridenFilesKeepOriginalName = value;
604299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  }
605299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis
606f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// createMainFileIDForMembuffer - Create the FileID for a memory buffer
607f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///  that will represent the FileID for the main source.  One example
608f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///  of when this would be used is when the main source is read from STDIN.
609f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) {
610f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    assert(MainFileID.isInvalid() && "MainFileID already set!");
611f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    MainFileID = createFileIDForMemBuffer(Buffer);
612f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    return MainFileID;
613f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  }
614f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
61506a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
61606a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  // MainFileID creation and querying methods.
61706a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
61806a062dc784c609b75dca15fd97f468d0d846596Chris Lattner
61976edd0e4ae0592a7225d50d0bad6732ac64dca2aTed Kremenek  /// getMainFileID - Returns the FileID of the main source file.
6202b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID getMainFileID() const { return MainFileID; }
6211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
62206a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  /// createMainFileID - Create the FileID for the main source file.
623a1f1fad8b60e1cb9d21a40a37f2e03150bcbeb6fDouglas Gregor  FileID createMainFileID(const FileEntry *SourceFile,
624a1f1fad8b60e1cb9d21a40a37f2e03150bcbeb6fDouglas Gregor                          SrcMgr::CharacteristicKind Kind = SrcMgr::C_User) {
62506a062dc784c609b75dca15fd97f468d0d846596Chris Lattner    assert(MainFileID.isInvalid() && "MainFileID already set!");
626a1f1fad8b60e1cb9d21a40a37f2e03150bcbeb6fDouglas Gregor    MainFileID = createFileID(SourceFile, SourceLocation(), Kind);
62706a062dc784c609b75dca15fd97f468d0d846596Chris Lattner    return MainFileID;
62806a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  }
6291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
630b8c879a5363f36bdae8831112b563333e3c05acbArgyrios Kyrtzidis  /// \brief Set the file ID for the main source file.
631b8c879a5363f36bdae8831112b563333e3c05acbArgyrios Kyrtzidis  void setMainFileID(FileID FID) {
632b8c879a5363f36bdae8831112b563333e3c05acbArgyrios Kyrtzidis    assert(MainFileID.isInvalid() && "MainFileID already set!");
633b8c879a5363f36bdae8831112b563333e3c05acbArgyrios Kyrtzidis    MainFileID = FID;
634b8c879a5363f36bdae8831112b563333e3c05acbArgyrios Kyrtzidis  }
635b8c879a5363f36bdae8831112b563333e3c05acbArgyrios Kyrtzidis
636507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  /// \brief Set the file ID for the precompiled preamble.
637507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  void setPreambleFileID(FileID Preamble) {
638507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    assert(PreambleFileID.isInvalid() && "PreambleFileID already set!");
639507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    PreambleFileID = Preamble;
640414cb64f09ce48a36377458ce5e5a90c3ad41d00Douglas Gregor  }
6415330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
642507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  /// \brief Get the file ID for the precompiled preamble if there is one.
643507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  FileID getPreambleFileID() const { return PreambleFileID; }
644507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
64506a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
6463201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  // Methods to create new FileID's and macro expansions.
64706a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
6481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// createFileID - Create a new FileID that represents the specified file
650d57b7ff9bebc4c45f325fc1be6f238cfcd4c3732Peter Collingbourne  /// being #included from the specified IncludePosition.  This translates NULL
651d57b7ff9bebc4c45f325fc1be6f238cfcd4c3732Peter Collingbourne  /// into standard input.
6522b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
6537f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                      SrcMgr::CharacteristicKind FileCharacter,
654f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor                      int LoadedID = 0, unsigned LoadedOffset = 0) {
655de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile);
6560d06e998910934e5ef070f53f4c272e7c6b846c6Dan Gohman    assert(IR && "getOrCreateContentCache() cannot return NULL");
657f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    return createFileID(IR, IncludePos, FileCharacter, LoadedID, LoadedOffset);
6585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
6591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// createFileIDForMemBuffer - Create a new FileID that represents the
6615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// specified memory buffer.  This does no caching of the buffer and takes
6625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once.
6637f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer,
664f453cb9f677e16c00b358ec91eccf5f003765dc6Axel Naumann                                  int LoadedID = 0, unsigned LoadedOffset = 0,
665f453cb9f677e16c00b358ec91eccf5f003765dc6Axel Naumann                                 SourceLocation IncludeLoc = SourceLocation()) {
666f453cb9f677e16c00b358ec91eccf5f003765dc6Axel Naumann    return createFileID(createMemBufferContentCache(Buffer), IncludeLoc,
667f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor                        SrcMgr::C_User, LoadedID, LoadedOffset);
6681036b68525f39cb69ac22c679ed440acd8392a16Ted Kremenek  }
66906a062dc784c609b75dca15fd97f468d0d846596Chris Lattner
670bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth  /// createMacroArgExpansionLoc - Return a new SourceLocation that encodes the
671bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth  /// fact that a token from SpellingLoc should actually be referenced from
6723201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// ExpansionLoc, and that it represents the expansion of a macro argument
6733201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// into the function-like macro body.
674bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth  SourceLocation createMacroArgExpansionLoc(SourceLocation Loc,
675bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                            SourceLocation ExpansionLoc,
676bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                            unsigned TokLength);
677c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth
678bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth  /// createExpansionLoc - Return a new SourceLocation that encodes the fact
679c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth  /// that a token from SpellingLoc should actually be referenced from
680bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth  /// ExpansionLoc.
681bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth  SourceLocation createExpansionLoc(SourceLocation Loc,
682bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                    SourceLocation ExpansionLocStart,
683bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                    SourceLocation ExpansionLocEnd,
684bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                    unsigned TokLength,
685bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                    int LoadedID = 0,
686bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                    unsigned LoadedOffset = 0);
6871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6882968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \brief Retrieve the memory buffer associated with the given file.
68950f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  ///
69050f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  /// \param Invalid If non-NULL, will be set \c true if an error
69150f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  /// occurs while retrieving the memory buffer.
69250f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File,
69350f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor                                                   bool *Invalid = 0);
6942968442603b029949246467253eeac8139a5b6d8Douglas Gregor
6952968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \brief Override the contents of the given source file by providing an
6962968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// already-allocated buffer.
6972968442603b029949246467253eeac8139a5b6d8Douglas Gregor  ///
698afbf5f810652f04c0bba235c0fa079885fb3caa8Dan Gohman  /// \param SourceFile the source file whose contents will be overriden.
6992968442603b029949246467253eeac8139a5b6d8Douglas Gregor  ///
7002968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \param Buffer the memory buffer whose contents will be used as the
7012968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// data in the given source file.
7022968442603b029949246467253eeac8139a5b6d8Douglas Gregor  ///
703f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// \param DoNotFree If true, then the buffer will not be freed when the
704f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// source manager is destroyed.
7050d06e998910934e5ef070f53f4c272e7c6b846c6Dan Gohman  void overrideFileContents(const FileEntry *SourceFile,
706f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor                            const llvm::MemoryBuffer *Buffer,
707f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor                            bool DoNotFree = false);
7082968442603b029949246467253eeac8139a5b6d8Douglas Gregor
709b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  /// \brief Override the the given source file with another one.
710b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  ///
711b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  /// \param SourceFile the source file which will be overriden.
712b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  ///
713b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  /// \param NewFile the file whose contents will be used as the
714b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  /// data instead of the contents of the given source file.
715b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  void overrideFileContents(const FileEntry *SourceFile,
716b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis                            const FileEntry *NewFile);
717b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis
71806a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
71906a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  // FileID manipulation methods.
72006a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
7211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7222ffb14f004affc363d86d6c7c63c356190db3679Daniel Dunbar  /// getBuffer - Return the buffer for the specified FileID. If there is an
7232ffb14f004affc363d86d6c7c63c356190db3679Daniel Dunbar  /// error opening this buffer the first time, this manufactures a temporary
7242ffb14f004affc363d86d6c7c63c356190db3679Daniel Dunbar  /// buffer and returns a non-empty error string.
725e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner  const llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc,
726e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner                                      bool *Invalid = 0) const {
727e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    bool MyInvalid = false;
728e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
729e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    if (MyInvalid || !Entry.isFile()) {
730e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor      if (Invalid)
731e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor        *Invalid = true;
7325330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
733e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor      return getFakeBufferForRecovery();
734e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    }
7355330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
7365330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher    return Entry.getFile().getContentCache()->getBuffer(Diag, *this, Loc,
737e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor                                                        Invalid);
73806a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  }
7391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
740e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner  const llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = 0) const {
741e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    bool MyInvalid = false;
742e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
743e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    if (MyInvalid || !Entry.isFile()) {
744e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor      if (Invalid)
745e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor        *Invalid = true;
7465330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
747e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor      return getFakeBufferForRecovery();
748e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    }
749e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor
7505330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher    return Entry.getFile().getContentCache()->getBuffer(Diag, *this,
7515330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher                                                        SourceLocation(),
752e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor                                                        Invalid);
753e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner  }
7545330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
75506a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  /// getFileEntryForID - Returns the FileEntry record for the provided FileID.
75606a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  const FileEntry *getFileEntryForID(FileID FID) const {
757e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    bool MyInvalid = false;
758e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
759e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    if (MyInvalid || !Entry.isFile())
760e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor      return 0;
7615330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
76239afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis    const SrcMgr::ContentCache *Content = Entry.getFile().getContentCache();
76339afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis    if (!Content)
76439afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis      return 0;
76539afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis    return Content->OrigEntry;
76606a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  }
7671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7689d5a165d301cc9df68631e624322dd2a962f65b3Ted Kremenek  /// Returns the FileEntry record for the provided SLocEntry.
7699d5a165d301cc9df68631e624322dd2a962f65b3Ted Kremenek  const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const
7709d5a165d301cc9df68631e624322dd2a962f65b3Ted Kremenek  {
77139afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis    const SrcMgr::ContentCache *Content = sloc.getFile().getContentCache();
77239afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis    if (!Content)
77339afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis      return 0;
77439afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis    return Content->OrigEntry;
7759d5a165d301cc9df68631e624322dd2a962f65b3Ted Kremenek  }
7769d5a165d301cc9df68631e624322dd2a962f65b3Ted Kremenek
777ceafc4b63599d14f0b5b10ff92e22bf242682dceBenjamin Kramer  /// getBufferData - Return a StringRef to the source buffer data for the
778ceafc4b63599d14f0b5b10ff92e22bf242682dceBenjamin Kramer  /// specified FileID.
779ceafc4b63599d14f0b5b10ff92e22bf242682dceBenjamin Kramer  ///
780f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor  /// \param FID The file ID whose contents will be returned.
781f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor  /// \param Invalid If non-NULL, will be set true if an error occurred.
782686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  StringRef getBufferData(FileID FID, bool *Invalid = 0) const;
783f6ac97b101c8840efa92bf29166077ce4049e293Benjamin Kramer
784d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  /// \brief Get the number of FileIDs (files and macros) that were created
7852c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// during preprocessing of \p FID, including it.
786d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  unsigned getNumCreatedFIDsForFileID(FileID FID) const {
787d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    bool Invalid = false;
788d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
789d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    if (Invalid || !Entry.isFile())
790d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis      return 0;
791d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis
792d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    return Entry.getFile().NumCreatedFIDs;
793d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  }
794d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis
795d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  /// \brief Set the number of FileIDs (files and macros) that were created
7962c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// during preprocessing of \p FID, including it.
797d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  void setNumCreatedFIDsForFileID(FileID FID, unsigned NumFIDs) const {
798d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    bool Invalid = false;
799d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
800d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    if (Invalid || !Entry.isFile())
801d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis      return;
802d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis
803d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    assert(Entry.getFile().NumCreatedFIDs == 0 && "Already set!");
804d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    const_cast<SrcMgr::FileInfo &>(Entry.getFile()).NumCreatedFIDs = NumFIDs;
805d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  }
8061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
80706a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
80806a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  // SourceLocation manipulation methods.
80906a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
8101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
811668ab1a36d6a5731c71a75a5f388ecafd538a896Chris Lattner  /// getFileID - Return the FileID for a SourceLocation.  This is a very
812de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// hot method that is used for all SourceManager queries that start with a
813de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// SourceLocation object.  It is responsible for finding the entry in
814de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// SLocEntryTable which contains the specified location.
815de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  ///
816de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  FileID getFileID(SourceLocation SpellingLoc) const {
817de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned SLocOffset = SpellingLoc.getOffset();
8181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
819de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    // If our one-entry cache covers this offset, just return it.
820de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
821de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return LastFileIDLookup;
822de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
823de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return getFileIDSlow(SLocOffset);
824de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
8251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8262b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  /// getLocForStartOfFile - Return the source location corresponding to the
8272b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  /// first byte of the specified file.
8282b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  SourceLocation getLocForStartOfFile(FileID FID) const {
829e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    bool Invalid = false;
830e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
831e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    if (Invalid || !Entry.isFile())
832e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor      return SourceLocation();
8335330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
834e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    unsigned FileOffset = Entry.getOffset();
835de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return SourceLocation::getFileLoc(FileOffset);
8362b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  }
837f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis
838f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis  /// \brief Return the source location corresponding to the last byte of the
839f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis  /// specified file.
840f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis  SourceLocation getLocForEndOfFile(FileID FID) const {
841f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis    bool Invalid = false;
842f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
843f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis    if (Invalid || !Entry.isFile())
844f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis      return SourceLocation();
845f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis
846f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis    unsigned FileOffset = Entry.getOffset();
847f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis    return SourceLocation::getFileLoc(FileOffset + getFileIDSize(FID) - 1);
848f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis  }
8491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8502c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Returns the include location if \p FID is a #include'd file
851d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  /// otherwise it returns an invalid location.
852d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  SourceLocation getIncludeLoc(FileID FID) const {
853d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    bool Invalid = false;
854d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
855d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    if (Invalid || !Entry.isFile())
856d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis      return SourceLocation();
8575330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
858d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    return Entry.getFile().getIncludeLoc();
859d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  }
860d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis
861402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth  /// getExpansionLoc - Given a SourceLocation object, return the expansion
862402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth  /// location referenced by the ID.
863402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth  SourceLocation getExpansionLoc(SourceLocation Loc) const {
864addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner    // Handle the non-mapped case inline, defer to out of line code to handle
865402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth    // expansions.
866de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (Loc.isFileID()) return Loc;
867f84ef95ecec34f27fd05eb4e0392ca6bd3bd0be0Chandler Carruth    return getExpansionLocSlowCase(Loc);
868de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
8691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8702c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Given \p Loc, if it is a macro location return the expansion
871796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis  /// location or the spelling location, depending on if it comes from a
872796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis  /// macro argument or not.
873796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis  SourceLocation getFileLoc(SourceLocation Loc) const {
874796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis    if (Loc.isFileID()) return Loc;
875796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis    return getFileLocSlowCase(Loc);
876796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis  }
877796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis
878999f739404edf2078cf9f9c28b4dc45c19765842Chandler Carruth  /// getImmediateExpansionRange - Loc is required to be an expansion location.
879999f739404edf2078cf9f9c28b4dc45c19765842Chandler Carruth  /// Return the start/end of the expansion information.
880e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  std::pair<SourceLocation,SourceLocation>
881999f739404edf2078cf9f9c28b4dc45c19765842Chandler Carruth  getImmediateExpansionRange(SourceLocation Loc) const;
8821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
883edc3dccece244a584f8ebdb81da6c962c08e79beChandler Carruth  /// getExpansionRange - Given a SourceLocation object, return the range of
884edc3dccece244a584f8ebdb81da6c962c08e79beChandler Carruth  /// tokens covered by the expansion the ultimate file.
8856678133b8ce642f93e5141f056fa643112041ad0Chris Lattner  std::pair<SourceLocation,SourceLocation>
886edc3dccece244a584f8ebdb81da6c962c08e79beChandler Carruth  getExpansionRange(SourceLocation Loc) const;
8871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
889de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// getSpellingLoc - Given a SourceLocation object, return the spelling
890de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// location referenced by the ID.  This is the place where the characters
891de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// that make up the lexed token can be found.
892de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  SourceLocation getSpellingLoc(SourceLocation Loc) const {
893addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner    // Handle the non-mapped case inline, defer to out of line code to handle
8943201f382956ed9beee9fb31229c2835c1208889cChandler Carruth    // expansions.
895de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (Loc.isFileID()) return Loc;
896addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner    return getSpellingLocSlowCase(Loc);
897de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
8981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
899387616edf98739f4a0dd234c907e2b913e6a535dChris Lattner  /// getImmediateSpellingLoc - Given a SourceLocation object, return the
900387616edf98739f4a0dd234c907e2b913e6a535dChris Lattner  /// spelling location referenced by the ID.  This is the first level down
901387616edf98739f4a0dd234c907e2b913e6a535dChris Lattner  /// towards the place where the characters that make up the lexed token can be
902387616edf98739f4a0dd234c907e2b913e6a535dChris Lattner  /// found.  This should not generally be used by clients.
9031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const;
904de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
905de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// getDecomposedLoc - Decompose the specified location into a raw FileID +
906de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// Offset pair.  The first element is the FileID, the second is the
907de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// offset from the start of the buffer of the location.
908de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const {
909de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    FileID FID = getFileID(Loc);
910a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    bool Invalid = false;
911a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    const SrcMgr::SLocEntry &E = getSLocEntry(FID, &Invalid);
912a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    if (Invalid)
913a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis      return std::make_pair(FileID(), 0);
914a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    return std::make_pair(FID, Loc.getOffset()-E.getOffset());
915de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
9161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9173201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// getDecomposedExpansionLoc - Decompose the specified location into a raw
9183201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// FileID + Offset pair. If the location is an expansion record, walk
9193201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// through it until we find the final location expanded.
920de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned>
921e7b2b6e87dbe5b1207f77b6ff9c210a02f95bb39Chandler Carruth  getDecomposedExpansionLoc(SourceLocation Loc) const {
922de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    FileID FID = getFileID(Loc);
923a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    bool Invalid = false;
924a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    const SrcMgr::SLocEntry *E = &getSLocEntry(FID, &Invalid);
925a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    if (Invalid)
926a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis      return std::make_pair(FileID(), 0);
9271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
928de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned Offset = Loc.getOffset()-E->getOffset();
929de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (Loc.isFileID())
930de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return std::make_pair(FID, Offset);
9311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
932e7b2b6e87dbe5b1207f77b6ff9c210a02f95bb39Chandler Carruth    return getDecomposedExpansionLocSlowCase(E);
933de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
934de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
935de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// getDecomposedSpellingLoc - Decompose the specified location into a raw
9363201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// FileID + Offset pair.  If the location is an expansion record, walk
937de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// through it until we find its spelling record.
938de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned>
939de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  getDecomposedSpellingLoc(SourceLocation Loc) const {
940de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    FileID FID = getFileID(Loc);
941a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    bool Invalid = false;
942a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    const SrcMgr::SLocEntry *E = &getSLocEntry(FID, &Invalid);
943a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    if (Invalid)
944a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis      return std::make_pair(FileID(), 0);
9451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
946de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned Offset = Loc.getOffset()-E->getOffset();
947de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (Loc.isFileID())
948de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return std::make_pair(FID, Offset);
949de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return getDecomposedSpellingLocSlowCase(E, Offset);
9501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
9511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
95252c29081281955d3db9e11d10573b2d38f709099Chris Lattner  /// getFileOffset - This method returns the offset from the start
95352c29081281955d3db9e11d10573b2d38f709099Chris Lattner  /// of the file that the specified SourceLocation represents. This is not very
95452c29081281955d3db9e11d10573b2d38f709099Chris Lattner  /// meaningful for a macro ID.
95552c29081281955d3db9e11d10573b2d38f709099Chris Lattner  unsigned getFileOffset(SourceLocation SpellingLoc) const {
956de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return getDecomposedLoc(SpellingLoc).second;
9575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
9581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
95996d3589e523a04f4ff2058a7919226ce60696ae8Chandler Carruth  /// isMacroArgExpansion - This method tests whether the given source location
96096d3589e523a04f4ff2058a7919226ce60696ae8Chandler Carruth  /// represents a macro argument's expansion into the function-like macro
96196d3589e523a04f4ff2058a7919226ce60696ae8Chandler Carruth  /// definition. Such source locations only appear inside of the expansion
96296d3589e523a04f4ff2058a7919226ce60696ae8Chandler Carruth  /// locations representing where a particular function-like macro was
96396d3589e523a04f4ff2058a7919226ce60696ae8Chandler Carruth  /// expanded.
96496d3589e523a04f4ff2058a7919226ce60696ae8Chandler Carruth  bool isMacroArgExpansion(SourceLocation Loc) const;
9651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9662c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Returns true if \p Loc is inside the [\p Start, +\p Length)
967499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis  /// chunk of the source location address space.
9682c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// If it's true and \p RelativeOffset is non-null, it will be set to the
9692c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// relative offset of \p Loc inside the chunk.
970499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis  bool isInSLocAddrSpace(SourceLocation Loc,
971499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis                         SourceLocation Start, unsigned Length,
972499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis                         unsigned *RelativeOffset = 0) const {
973499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis    assert(((Start.getOffset() < NextLocalOffset &&
974499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis               Start.getOffset()+Length <= NextLocalOffset) ||
975499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis            (Start.getOffset() >= CurrentLoadedOffset &&
976499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis                Start.getOffset()+Length < MaxLoadedOffset)) &&
977499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis           "Chunk is not valid SLoc address space");
978499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis    unsigned LocOffs = Loc.getOffset();
979499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis    unsigned BeginOffs = Start.getOffset();
980499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis    unsigned EndOffs = BeginOffs + Length;
981499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis    if (LocOffs >= BeginOffs && LocOffs < EndOffs) {
982499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis      if (RelativeOffset)
983499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis        *RelativeOffset = LocOffs - BeginOffs;
984499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis      return true;
985499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis    }
986499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis
987499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis    return false;
988499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis  }
989499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis
9902c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Return true if both \p LHS and \p RHS are in the local source
9912c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// location address space or the loaded one. If it's true and \p
9922c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// RelativeOffset is non-null, it will be set to the offset of \p RHS
9932c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// relative to \p LHS.
994b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis  bool isInSameSLocAddrSpace(SourceLocation LHS, SourceLocation RHS,
995b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis                             int *RelativeOffset) const {
996b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis    unsigned LHSOffs = LHS.getOffset(), RHSOffs = RHS.getOffset();
997b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis    bool LHSLoaded = LHSOffs >= CurrentLoadedOffset;
998b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis    bool RHSLoaded = RHSOffs >= CurrentLoadedOffset;
999b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis
1000b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis    if (LHSLoaded == RHSLoaded) {
1001b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis      if (RelativeOffset)
1002b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis        *RelativeOffset = RHSOffs - LHSOffs;
1003b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis      return true;
1004b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis    }
1005b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis
1006b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis    return false;
1007b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis  }
1008b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis
1009de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  //===--------------------------------------------------------------------===//
1010de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  // Queries about the code at a SourceLocation.
1011de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  //===--------------------------------------------------------------------===//
10121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getCharacterData - Return a pointer to the start of the specified location
1014de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// in the appropriate spelling MemoryBuffer.
101550f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  ///
101650f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  /// \param Invalid If non-NULL, will be set \c true if an error occurs.
101750f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  const char *getCharacterData(SourceLocation SL, bool *Invalid = 0) const;
10181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10199dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  /// getColumnNumber - Return the column # for the specified file position.
10209dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  /// This is significantly cheaper to compute than the line number.  This
10213201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// returns zero if the column number isn't known.  This may only be called
10223201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// on a file sloc, so you must choose a spelling or expansion location
1023f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner  /// before calling this method.
10245330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher  unsigned getColumnNumber(FileID FID, unsigned FilePos,
102550f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor                           bool *Invalid = 0) const;
10265ef04ee40c3332d31b6d1439f50d0ddb45812929Chandler Carruth  unsigned getSpellingColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
1027a77c031cb66f75d22672070052cc6e0205289ff8Chandler Carruth  unsigned getExpansionColumnNumber(SourceLocation Loc,
1028b49dcd249c7f4f034493741f95394987a4ccf244Chandler Carruth                                    bool *Invalid = 0) const;
10295ef04ee40c3332d31b6d1439f50d0ddb45812929Chandler Carruth  unsigned getPresumedColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
10301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1032df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner  /// getLineNumber - Given a SourceLocation, return the spelling line number
10335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// for the position indicated.  This requires building and caching a table of
10345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// line offsets for the MemoryBuffer, so this is not cheap: use only when
10355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// about to emit a diagnostic.
103650f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = 0) const;
10375ef04ee40c3332d31b6d1439f50d0ddb45812929Chandler Carruth  unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
1038642116259e8df6286063a17361c20e95b5017a0aChandler Carruth  unsigned getExpansionLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
10395ef04ee40c3332d31b6d1439f50d0ddb45812929Chandler Carruth  unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
10401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1041bff5c512af8ca7ac92e974e04c06ff4f820e4ee1Chris Lattner  /// Return the filename or buffer identifier of the buffer the location is in.
1042bff5c512af8ca7ac92e974e04c06ff4f820e4ee1Chris Lattner  /// Note that this name does not respect #line directives.  Use getPresumedLoc
1043bff5c512af8ca7ac92e974e04c06ff4f820e4ee1Chris Lattner  /// for normal clients.
104450f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  const char *getBufferName(SourceLocation Loc, bool *Invalid = 0) const;
10451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10466b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// getFileCharacteristic - return the file characteristic of the specified
10471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// source location, indicating whether this is a normal file, a system
10486b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// header, or an "implicit extern C" system header.
10496b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  ///
10506b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// This state can be modified with flags on GNU linemarker directives like:
10516b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  ///   # 4 "foo.h" 3
10526b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// which changes all source locations in the current file after that to be
10536b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// considered to be from a system header.
10546b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const;
10551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1056b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// getPresumedLoc - This method returns the "presumed" location of a
1057b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// SourceLocation specifies.  A "presumed location" can be modified by #line
1058b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// or GNU line marker directives.  This provides a view on the data that a
1059b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// user should see in diagnostics, for example.
1060b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  ///
10613201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// Note that a presumed location is always given as the expansion point of
10623201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// an expansion location, not at the spelling location.
1063cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  ///
1064cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  /// \returns The presumed location of the specified SourceLocation. If the
1065cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  /// presumed location cannot be calculate (e.g., because \p Loc is invalid
1066cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  /// or the file containing \p Loc has changed on disk), returns an invalid
1067cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  /// presumed location.
1068b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  PresumedLoc getPresumedLoc(SourceLocation Loc) const;
10691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10709fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  /// isFromSameFile - Returns true if both SourceLocations correspond to
10719fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  ///  the same file.
10729fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
1073a11d61793341fea195c29a0dab3fbd74f2b39a8cChris Lattner    return getFileID(Loc1) == getFileID(Loc2);
10749fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  }
10751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10769fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  /// isFromMainFile - Returns true if the file of provided SourceLocation is
10779fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  ///   the main file.
10789fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  bool isFromMainFile(SourceLocation Loc) const {
1079a11d61793341fea195c29a0dab3fbd74f2b39a8cChris Lattner    return getFileID(Loc) == getMainFileID();
10801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
10811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10827bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber  /// isInSystemHeader - Returns if a SourceLocation is in a system header.
10837bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber  bool isInSystemHeader(SourceLocation Loc) const {
10840b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner    return getFileCharacteristic(Loc) != SrcMgr::C_User;
1085721818304ac462d8c6ce05eecd02884033db78f1Chris Lattner  }
10861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10870d456588acac0713a7c33063922d35a8cc8c658eChris Lattner  /// isInExternCSystemHeader - Returns if a SourceLocation is in an "extern C"
10880d456588acac0713a7c33063922d35a8cc8c658eChris Lattner  /// system header.
10890d456588acac0713a7c33063922d35a8cc8c658eChris Lattner  bool isInExternCSystemHeader(SourceLocation Loc) const {
10900d456588acac0713a7c33063922d35a8cc8c658eChris Lattner    return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem;
10910d456588acac0713a7c33063922d35a8cc8c658eChris Lattner  }
10921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1093d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay  /// \brief Returns whether \p Loc is expanded from a macro in a system header.
1094d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay  bool isInSystemMacro(SourceLocation loc) {
1095d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay    return loc.isMacroID() && isInSystemHeader(getSpellingLoc(loc));
1096d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay  }
1097d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay
10982c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief The size of the SLocEnty that \p FID represents.
1099984e42ca1ff7775ce39372c314f1cb7d6862c4c7Argyrios Kyrtzidis  unsigned getFileIDSize(FileID FID) const;
110054232ade44d31e98ea83f43ca066128e315dcbdaArgyrios Kyrtzidis
11012c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Given a specific FileID, returns true if \p Loc is inside that
11022c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// FileID chunk and sets relative offset (offset of \p Loc from beginning
11032c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// of FileID) to \p relativeOffset.
1104d60a34a4e514ec0dfddd05ef2744be104e111f45Argyrios Kyrtzidis  bool isInFileID(SourceLocation Loc, FileID FID,
1105d60a34a4e514ec0dfddd05ef2744be104e111f45Argyrios Kyrtzidis                  unsigned *RelativeOffset = 0) const {
1106d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis    unsigned Offs = Loc.getOffset();
1107d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis    if (isOffsetInFileID(FID, Offs)) {
1108d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis      if (RelativeOffset)
1109d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis        *RelativeOffset = Offs - getSLocEntry(FID).getOffset();
1110d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis      return true;
1111d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis    }
1112d60a34a4e514ec0dfddd05ef2744be104e111f45Argyrios Kyrtzidis
1113d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis    return false;
1114d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis  }
1115469244a322dd5d35cee1d02d70a2edbc12ac5ce7Argyrios Kyrtzidis
111606a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
11175b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  // Line Table Manipulation Routines
11185b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  //===--------------------------------------------------------------------===//
11191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11205b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  /// getLineTableFilenameID - Return the uniqued ID for the specified filename.
11211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ///
1122686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  unsigned getLineTableFilenameID(StringRef Str);
11231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11244c4ea17d7f991516c37a871dfa4bbe5723fa85f0Chris Lattner  /// AddLineNote - Add a line note to the line table for the FileID and offset
11254c4ea17d7f991516c37a871dfa4bbe5723fa85f0Chris Lattner  /// specified by Loc.  If FilenameID is -1, it is considered to be
11264c4ea17d7f991516c37a871dfa4bbe5723fa85f0Chris Lattner  /// unspecified.
11274c4ea17d7f991516c37a871dfa4bbe5723fa85f0Chris Lattner  void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID);
11289d79ebac47ffde6a1cb312f4c09b66b1b9a397fbChris Lattner  void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID,
11291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                   bool IsFileEntry, bool IsFileExit,
11309d79ebac47ffde6a1cb312f4c09b66b1b9a397fbChris Lattner                   bool IsSystemHeader, bool IsExternCHeader);
1131bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
1132bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  /// \brief Determine if the source manager has a line table.
1133bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  bool hasLineTable() const { return LineTable != 0; }
1134bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
1135bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  /// \brief Retrieve the stored line table.
1136bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  LineTableInfo &getLineTable();
1137bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
11385b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  //===--------------------------------------------------------------------===//
1139457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek  // Queries for performance analysis.
1140457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek  //===--------------------------------------------------------------------===//
1141457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek
1142457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek  /// Return the total amount of physical memory allocated by the
1143457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek  /// ContentCache allocator.
1144457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek  size_t getContentCacheSize() const {
1145457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek    return ContentCacheAlloc.getTotalMemory();
1146457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek  }
11475330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1148f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek  struct MemoryBufferSizes {
1149f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek    const size_t malloc_bytes;
1150f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek    const size_t mmap_bytes;
11515330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1152f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek    MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)
1153f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek      : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {}
1154f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek  };
1155f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek
1156f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek  /// Return the amount of memory used by memory buffers, breaking down
1157f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek  /// by heap-backed versus mmap'ed memory.
1158f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek  MemoryBufferSizes getMemoryBufferSizes() const;
11595330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1160ca7dc2b755eb81ac95121ce1a1f1aa44a4a0fe12Ted Kremenek  // Return the amount of memory used for various side tables and
1161ca7dc2b755eb81ac95121ce1a1f1aa44a4a0fe12Ted Kremenek  // data structures in the SourceManager.
1162ca7dc2b755eb81ac95121ce1a1f1aa44a4a0fe12Ted Kremenek  size_t getDataStructureSizes() const;
1163457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek
1164457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek  //===--------------------------------------------------------------------===//
116506a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  // Other miscellaneous methods.
116606a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
116710b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis
116810b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  /// \brief Get the source location for the given file:line:col triplet.
116910b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  ///
117010b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  /// If the source file is included multiple times, the source location will
117110b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  /// be based upon the first inclusion.
1172ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  SourceLocation translateFileLineCol(const FileEntry *SourceFile,
1173507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis                                      unsigned Line, unsigned Col) const;
1174ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis
1175b201e16e0c331b0bdeae7b30f9f79aae32beb1b2Argyrios Kyrtzidis  /// \brief Get the FileID for the given file.
1176b201e16e0c331b0bdeae7b30f9f79aae32beb1b2Argyrios Kyrtzidis  ///
1177b201e16e0c331b0bdeae7b30f9f79aae32beb1b2Argyrios Kyrtzidis  /// If the source file is included multiple times, the FileID will be the
1178b201e16e0c331b0bdeae7b30f9f79aae32beb1b2Argyrios Kyrtzidis  /// first inclusion.
1179b201e16e0c331b0bdeae7b30f9f79aae32beb1b2Argyrios Kyrtzidis  FileID translateFile(const FileEntry *SourceFile) const;
1180b201e16e0c331b0bdeae7b30f9f79aae32beb1b2Argyrios Kyrtzidis
11812c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Get the source location in \p FID for the given line:col.
11822c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// Returns null location if \p FID is not a file SLocEntry.
1183507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  SourceLocation translateLineCol(FileID FID,
1184507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis                                  unsigned Line, unsigned Col) const;
1185efa2ff8603dae51f5f5ed7509a503f477498ad22Argyrios Kyrtzidis
11862c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief If \p Loc points inside a function macro argument, the returned
1187ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  /// location will be the macro location in which the argument was expanded.
1188ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  /// If a macro argument is used multiple times, the expanded location will
1189ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  /// be at the first expansion of the argument.
1190ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  /// e.g.
1191ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  ///   MY_MACRO(foo);
1192ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  ///             ^
1193ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  /// Passing a file location pointing at 'foo', will yield a macro location
1194ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  /// where 'foo' was expanded into.
1195507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  SourceLocation getMacroArgExpandedLocation(SourceLocation Loc) const;
11961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11972aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis  /// \brief Determines the order of 2 source locations in the translation unit.
11982aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis  ///
11992aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis  /// \returns true if LHS source location comes before RHS, false otherwise.
12002aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis  bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const;
12012aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis
1202aec230d29835285777ecc467e268c83b33a2addeArgyrios Kyrtzidis  /// \brief Comparison function class.
1203aec230d29835285777ecc467e268c83b33a2addeArgyrios Kyrtzidis  class LocBeforeThanCompare : public std::binary_function<SourceLocation,
1204aec230d29835285777ecc467e268c83b33a2addeArgyrios Kyrtzidis                                                         SourceLocation, bool> {
1205aec230d29835285777ecc467e268c83b33a2addeArgyrios Kyrtzidis    SourceManager &SM;
1206aec230d29835285777ecc467e268c83b33a2addeArgyrios Kyrtzidis
1207aec230d29835285777ecc467e268c83b33a2addeArgyrios Kyrtzidis  public:
1208aec230d29835285777ecc467e268c83b33a2addeArgyrios Kyrtzidis    explicit LocBeforeThanCompare(SourceManager &SM) : SM(SM) { }
1209aec230d29835285777ecc467e268c83b33a2addeArgyrios Kyrtzidis
1210aec230d29835285777ecc467e268c83b33a2addeArgyrios Kyrtzidis    bool operator()(SourceLocation LHS, SourceLocation RHS) const {
1211aec230d29835285777ecc467e268c83b33a2addeArgyrios Kyrtzidis      return SM.isBeforeInTranslationUnit(LHS, RHS);
1212aec230d29835285777ecc467e268c83b33a2addeArgyrios Kyrtzidis    }
1213aec230d29835285777ecc467e268c83b33a2addeArgyrios Kyrtzidis  };
1214aec230d29835285777ecc467e268c83b33a2addeArgyrios Kyrtzidis
1215b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis  /// \brief Determines the order of 2 source locations in the "source location
1216b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis  /// address space".
12175d579e7b0463a2e65328df85e4fed8e07799fd9eArgyrios Kyrtzidis  bool isBeforeInSLocAddrSpace(SourceLocation LHS, SourceLocation RHS) const {
12185d579e7b0463a2e65328df85e4fed8e07799fd9eArgyrios Kyrtzidis    return isBeforeInSLocAddrSpace(LHS, RHS.getOffset());
1219b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis  }
1220b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis
1221b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis  /// \brief Determines the order of a source location and a source location
1222b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis  /// offset in the "source location address space".
1223f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///
12245330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher  /// Note that we always consider source locations loaded from
12255d579e7b0463a2e65328df85e4fed8e07799fd9eArgyrios Kyrtzidis  bool isBeforeInSLocAddrSpace(SourceLocation LHS, unsigned RHS) const {
1226f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    unsigned LHSOffset = LHS.getOffset();
1227f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    bool LHSLoaded = LHSOffset >= CurrentLoadedOffset;
1228f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    bool RHSLoaded = RHS >= CurrentLoadedOffset;
1229f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    if (LHSLoaded == RHSLoaded)
12305d579e7b0463a2e65328df85e4fed8e07799fd9eArgyrios Kyrtzidis      return LHSOffset < RHS;
12315330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1232f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    return LHSLoaded;
1233b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis  }
1234b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis
1235c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner  // Iterators over FileInfos.
12360d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>
12370d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner      ::const_iterator fileinfo_iterator;
1238c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner  fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); }
1239c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner  fileinfo_iterator fileinfo_end() const { return FileInfos.end(); }
1240d93256e55673a17d18543397ec462416acb13792Douglas Gregor  bool hasFileInfo(const FileEntry *File) const {
1241d93256e55673a17d18543397ec462416acb13792Douglas Gregor    return FileInfos.find(File) != FileInfos.end();
1242d93256e55673a17d18543397ec462416acb13792Douglas Gregor  }
1243c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner
12445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// PrintStats - Print statistics to stderr.
12455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
12465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void PrintStats() const;
124778d85f53b093867bbb0123f016956178eea7343eTed Kremenek
1248f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief Get the number of local SLocEntries we have.
1249f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  unsigned local_sloc_entry_size() const { return LocalSLocEntryTable.size(); }
12505330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1251f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief Get a local SLocEntry. This is exposed for indexing.
12525330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher  const SrcMgr::SLocEntry &getLocalSLocEntry(unsigned Index,
1253f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor                                             bool *Invalid = 0) const {
1254f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    assert(Index < LocalSLocEntryTable.size() && "Invalid index");
1255f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    return LocalSLocEntryTable[Index];
1256bdfe48ac80573e026595af91e541474dbf02565fDouglas Gregor  }
12575330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1258f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief Get the number of loaded SLocEntries we have.
1259f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  unsigned loaded_sloc_entry_size() const { return LoadedSLocEntryTable.size();}
12605330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1261f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief Get a loaded SLocEntry. This is exposed for indexing.
126270042f5d1ca378138f90b7b9384023701f5d03d8David Blaikie  const SrcMgr::SLocEntry &getLoadedSLocEntry(unsigned Index,
126370042f5d1ca378138f90b7b9384023701f5d03d8David Blaikie                                              bool *Invalid = 0) const {
1264f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    assert(Index < LoadedSLocEntryTable.size() && "Invalid index");
1265a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis    if (SLocEntryLoaded[Index])
1266a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis      return LoadedSLocEntryTable[Index];
1267a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis    return loadSLocEntry(Index, Invalid);
1268f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  }
12695330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1270e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor  const SrcMgr::SLocEntry &getSLocEntry(FileID FID, bool *Invalid = 0) const {
1271c705d2520a51de1dc38d36efada8e9bc2d8b0d1fArgyrios Kyrtzidis    if (FID.ID == 0 || FID.ID == -1) {
1272c705d2520a51de1dc38d36efada8e9bc2d8b0d1fArgyrios Kyrtzidis      if (Invalid) *Invalid = true;
1273c705d2520a51de1dc38d36efada8e9bc2d8b0d1fArgyrios Kyrtzidis      return LocalSLocEntryTable[0];
1274c705d2520a51de1dc38d36efada8e9bc2d8b0d1fArgyrios Kyrtzidis    }
1275f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    return getSLocEntryByID(FID.ID);
1276bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  }
1277bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
1278f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  unsigned getNextLocalOffset() const { return NextLocalOffset; }
12795330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1280f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  void setExternalSLocEntrySource(ExternalSLocEntrySource *Source) {
1281f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    assert(LoadedSLocEntryTable.empty() &&
1282f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor           "Invalidating existing loaded entries");
1283f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    ExternalSLocEntries = Source;
1284f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  }
12855330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1286f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief Allocate a number of loaded SLocEntries, which will be actually
1287f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// loaded on demand from the external source.
1288f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///
1289f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// NumSLocEntries will be allocated, which occupy a total of TotalSize space
1290f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// in the global source view. The lowest ID and the base offset of the
1291f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// entries will be returned.
1292f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  std::pair<int, unsigned>
1293f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  AllocateLoadedSLocEntries(unsigned NumSLocEntries, unsigned TotalSize);
12945330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
12952c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Returns true if \p Loc came from a PCH/Module.
1296aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis  bool isLoadedSourceLocation(SourceLocation Loc) const {
1297aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis    return Loc.getOffset() >= CurrentLoadedOffset;
1298aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis  }
1299aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis
13002c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Returns true if \p Loc did not come from a PCH/Module.
1301aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis  bool isLocalSourceLocation(SourceLocation Loc) const {
1302aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis    return Loc.getOffset() < NextLocalOffset;
1303aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis  }
1304aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis
13052c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Returns true if \p FID came from a PCH/Module.
1306718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis  bool isLoadedFileID(FileID FID) const {
1307718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis    assert(FID.ID != -1 && "Using FileID sentinel value");
1308718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis    return FID.ID < 0;
1309718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis  }
1310718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis
13112c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Returns true if \p FID did not come from a PCH/Module.
1312718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis  bool isLocalFileID(FileID FID) const {
1313718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis    return !isLoadedFileID(FID);
1314718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis  }
1315718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis
13165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerprivate:
1317e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor  const llvm::MemoryBuffer *getFakeBufferForRecovery() const;
1318a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis  const SrcMgr::ContentCache *getFakeContentCacheForRecovery() const;
1319a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis
1320a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis  const SrcMgr::SLocEntry &loadSLocEntry(unsigned Index, bool *Invalid) const;
1321e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor
1322f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief Get the entry with the given unwrapped FileID.
1323f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  const SrcMgr::SLocEntry &getSLocEntryByID(int ID) const {
1324f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    assert(ID != -1 && "Using FileID sentinel value");
1325f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    if (ID < 0)
1326f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor      return getLoadedSLocEntryByID(ID);
1327f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    return getLocalSLocEntry(static_cast<unsigned>(ID));
1328f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  }
13295330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1330a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis  const SrcMgr::SLocEntry &getLoadedSLocEntryByID(int ID,
1331a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis                                                  bool *Invalid = 0) const {
1332a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis    return getLoadedSLocEntry(static_cast<unsigned>(-ID - 2), Invalid);
1333f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  }
13345330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1335bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth  /// createExpansionLoc - Implements the common elements of storing an
13363201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// expansion info struct into the SLocEntry table and producing a source
1337c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth  /// location that refers to it.
133878df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth  SourceLocation createExpansionLocImpl(const SrcMgr::ExpansionInfo &Expansion,
1339bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                        unsigned TokLength,
1340bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                        int LoadedID = 0,
1341bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                        unsigned LoadedOffset = 0);
1342c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth
1343de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// isOffsetInFileID - Return true if the specified FileID contains the
1344de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// specified SourceLocation offset.  This is a very hot method.
1345de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const {
1346de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID);
1347de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    // If the entry is after the offset, it can't contain it.
1348de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (SLocOffset < Entry.getOffset()) return false;
13491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1350f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    // If this is the very last entry then it does.
1351f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    if (FID.ID == -2)
1352f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor      return true;
1353f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
1354f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    // If it is the last local entry, then it does if the location is local.
1355f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    if (static_cast<unsigned>(FID.ID+1) == LocalSLocEntryTable.size()) {
1356f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor      return SLocOffset < NextLocalOffset;
1357f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    }
13587f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
1359f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    // Otherwise, the entry after it has to not include it. This works for both
1360f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    // local and loaded entries.
13617f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    return SLocOffset < getSLocEntry(FileID::get(FID.ID+1)).getOffset();
1362de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
13631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
136478d85f53b093867bbb0123f016956178eea7343eTed Kremenek  /// createFileID - Create a new fileID for the specified ContentCache and
136578d85f53b093867bbb0123f016956178eea7343eTed Kremenek  ///  include position.  This works regardless of whether the ContentCache
136678d85f53b093867bbb0123f016956178eea7343eTed Kremenek  ///  corresponds to a file or some other input source.
13672b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID createFileID(const SrcMgr::ContentCache* File,
13682b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner                      SourceLocation IncludePos,
13697f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                      SrcMgr::CharacteristicKind DirCharacter,
1370f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor                      int LoadedID, unsigned LoadedOffset);
13711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1372de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  const SrcMgr::ContentCache *
1373de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    getOrCreateContentCache(const FileEntry *SourceFile);
1374c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek
137578d85f53b093867bbb0123f016956178eea7343eTed Kremenek  /// createMemBufferContentCache - Create a new ContentCache for the specified
137678d85f53b093867bbb0123f016956178eea7343eTed Kremenek  ///  memory buffer.
13771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const SrcMgr::ContentCache*
13782b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  createMemBufferContentCache(const llvm::MemoryBuffer *Buf);
13791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1380de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  FileID getFileIDSlow(unsigned SLocOffset) const;
1381f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  FileID getFileIDLocal(unsigned SLocOffset) const;
1382f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  FileID getFileIDLoaded(unsigned SLocOffset) const;
1383de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
1384f84ef95ecec34f27fd05eb4e0392ca6bd3bd0be0Chandler Carruth  SourceLocation getExpansionLocSlowCase(SourceLocation Loc) const;
1385addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner  SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;
1386796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis  SourceLocation getFileLocSlowCase(SourceLocation Loc) const;
1387addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner
1388de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned>
1389e7b2b6e87dbe5b1207f77b6ff9c210a02f95bb39Chandler Carruth  getDecomposedExpansionLocSlowCase(const SrcMgr::SLocEntry *E) const;
1390de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned>
1391de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
1392de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner                                   unsigned Offset) const;
1393fb3612ef197cb8532c05f33889ec1aed7c26e5cbArgyrios Kyrtzidis  void computeMacroArgsCache(MacroArgsMap *&MacroArgsCache, FileID FID) const;
1394ac1ffcc55b861737ba2466cd1ca1accd8eafceaaArgyrios Kyrtzidis
1395ac1ffcc55b861737ba2466cd1ca1accd8eafceaaArgyrios Kyrtzidis  friend class ASTReader;
1396ac1ffcc55b861737ba2466cd1ca1accd8eafceaaArgyrios Kyrtzidis  friend class ASTWriter;
13975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
13985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}  // end namespace clang
14015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif
1403