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//===----------------------------------------------------------------------===//
912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett///
102f7f5b1f5ff023cb8c4008ae53a12b09e3ea2622James Dennett/// \file
112f7f5b1f5ff023cb8c4008ae53a12b09e3ea2622James Dennett/// \brief Defines the SourceManager interface.
1212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett///
1349d7111bd42e38fb5f1d7ec5474b9f119e0d7b56James Dennett/// There are three different types of locations in a %file: a spelling
1412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// location, an expansion location, and a presumed location.
1512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett///
1612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// Given an example of:
1712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// \code
1812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// #define min(x, y) x < y ? x : y
1912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// \endcode
2012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett///
2112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// and then later on a use of min:
2212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// \code
232f7f5b1f5ff023cb8c4008ae53a12b09e3ea2622James Dennett/// #line 17
2412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// return min(a, b);
2512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// \endcode
2612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett///
2712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// The expansion location is the line in the source code where the macro
2812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// was expanded (the return statement), the spelling location is the
2912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// location in the source where the macro was originally defined,
3012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// and the presumed location is where the line directive states that
3112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// the line is 17, or any other line.
3212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett///
335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifndef LLVM_CLANG_SOURCEMANAGER_H
365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#define LLVM_CLANG_SOURCEMANAGER_H
375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
38aa48fe80a1b2000809900a437f0819d929793002Jordan Rose#include "clang/Basic/FileManager.h"
3930a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "clang/Basic/LLVM.h"
405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/SourceLocation.h"
41830ea5b7c75413526c19531f0180fa6e45b98919Douglas Gregor#include "llvm/ADT/ArrayRef.h"
420d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner#include "llvm/ADT/DenseMap.h"
43d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis#include "llvm/ADT/DenseSet.h"
4430a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "llvm/ADT/IntrusiveRefCntPtr.h"
4530a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "llvm/ADT/OwningPtr.h"
4630a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "llvm/ADT/PointerIntPair.h"
4730a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "llvm/ADT/PointerUnion.h"
4830a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "llvm/Support/Allocator.h"
4930a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "llvm/Support/DataTypes.h"
50f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek#include "llvm/Support/MemoryBuffer.h"
5130a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include <cassert>
52d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis#include <map>
535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include <vector>
545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace clang {
561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
57d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikieclass DiagnosticsEngine;
585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass SourceManager;
59099b4747042352f69184481a48508b599a8d3f73Ted Kremenekclass FileManager;
605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass FileEntry;
615b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattnerclass LineTableInfo;
62b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidisclass LangOptions;
63d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidisclass ASTWriter;
64d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidisclass ASTReader;
655330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
66c351d9837ea1d0b04842497e76c76125426a982cJames Dennett/// \brief Public enums and private classes that are part of the
670b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner/// SourceManager implementation.
685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace SrcMgr {
70af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Indicates whether a file or directory holds normal user code,
71af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// system code, or system code which is implicitly 'extern "C"' in C++ mode.
72af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
73af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// Entire directories can be tagged with this (this is maintained by
74af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// DirectoryLookup and friends) as can specific FileInfos when a \#pragma
75af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// system_header is seen or in various other cases.
760b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner  ///
779d72851fec9e9c62570a027d42701562bbf29751Chris Lattner  enum CharacteristicKind {
780b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner    C_User, C_System, C_ExternCSystem
790b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner  };
801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
81af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief One instance of this struct is kept for every file loaded or used.
8249d7111bd42e38fb5f1d7ec5474b9f119e0d7b56James Dennett  ///
83af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// This object owns the MemoryBuffer object.
84c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek  class ContentCache {
85f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    enum CCFlags {
86f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor      /// \brief Whether the buffer is invalid.
87f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor      InvalidFlag = 0x01,
88f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor      /// \brief Whether the buffer should not be freed on destruction.
89f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor      DoNotFreeFlag = 0x02
90f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    };
915330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
9212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// \brief The actual buffer containing the characters from the input
9312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// file.
9412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    ///
9512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// This is owned by the ContentCache object.  The bits indicate
9612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// whether the buffer is invalid.
97f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    mutable llvm::PointerIntPair<const llvm::MemoryBuffer *, 2> Buffer;
98c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek
99c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek  public:
10012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// \brief Reference to the file entry representing this ContentCache.
10112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    ///
102b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    /// This reference does not own the FileEntry object.
103af50aab0c317462129d73ae8000c6394c718598dJames Dennett    ///
104af50aab0c317462129d73ae8000c6394c718598dJames Dennett    /// It is possible for this to be NULL if the ContentCache encapsulates
105af50aab0c317462129d73ae8000c6394c718598dJames Dennett    /// an imaginary text buffer.
106b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    const FileEntry *OrigEntry;
107b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis
108b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    /// \brief References the file which the contents were actually loaded from.
10912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    ///
110b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    /// Can be different from 'Entry' if we overridden the contents of one file
111b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    /// with the contents of another file.
112b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    const FileEntry *ContentsEntry;
1131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// \brief A bump pointer allocated array of offsets for each source line.
11512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    ///
11612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// This is lazily computed.  This is owned by the SourceManager
11712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// BumpPointerAllocator object.
11805816591ec488a933dfecc9ff9f3cbf3c32767c2Chris Lattner    unsigned *SourceLineCache;
1191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// \brief The number of lines in this ContentCache.
12112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    ///
12212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// This is only valid if SourceLineCache is non-null.
123a081da5e44600d02983d6562bed1b4fd61e410fdDouglas Gregor    unsigned NumLines : 31;
12410b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis
125a081da5e44600d02983d6562bed1b4fd61e410fdDouglas Gregor    /// \brief Indicates whether the buffer itself was provided to override
126a081da5e44600d02983d6562bed1b4fd61e410fdDouglas Gregor    /// the actual file contents.
127a081da5e44600d02983d6562bed1b4fd61e410fdDouglas Gregor    ///
128a081da5e44600d02983d6562bed1b4fd61e410fdDouglas Gregor    /// When true, the original entry may be a virtual file that does not
129a081da5e44600d02983d6562bed1b4fd61e410fdDouglas Gregor    /// exist.
130a081da5e44600d02983d6562bed1b4fd61e410fdDouglas Gregor    unsigned BufferOverridden : 1;
131ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis
132ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis    /// \brief True if this content cache was initially created for a source
133ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis    /// file considered as a system one.
134ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis    unsigned IsSystemFile : 1;
135a081da5e44600d02983d6562bed1b4fd61e410fdDouglas Gregor
1367955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor    ContentCache(const FileEntry *Ent = 0)
1377955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor      : Buffer(0, false), OrigEntry(Ent), ContentsEntry(Ent),
138ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis        SourceLineCache(0), NumLines(0), BufferOverridden(false),
139ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis        IsSystemFile(false) {}
1407955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor
1417955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor    ContentCache(const FileEntry *Ent, const FileEntry *contentEnt)
1427955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor      : Buffer(0, false), OrigEntry(Ent), ContentsEntry(contentEnt),
143ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis        SourceLineCache(0), NumLines(0), BufferOverridden(false),
144ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis        IsSystemFile(false) {}
1457955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor
1467955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor    ~ContentCache();
1477955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor
1487955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor    /// The copy ctor does not allow copies where source object has either
14912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// a non-NULL Buffer or SourceLineCache.  Ownership of allocated memory
15012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// is not transferred, so this is a logical error.
1517955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor    ContentCache(const ContentCache &RHS)
152ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis      : Buffer(0, false), SourceLineCache(0), BufferOverridden(false),
153ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis        IsSystemFile(false)
1547955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor    {
1557955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor      OrigEntry = RHS.OrigEntry;
1567955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor      ContentsEntry = RHS.ContentsEntry;
1577955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor
1587955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor      assert (RHS.Buffer.getPointer() == 0 && RHS.SourceLineCache == 0 &&
1597955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor              "Passed ContentCache object cannot own a buffer.");
1607955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor
1617955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor      NumLines = RHS.NumLines;
1627955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor    }
1637955a25c65b3c3213a5e9375f51a02a765a3c880Douglas Gregor
16412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// \brief Returns the memory buffer for the associated content.
16536c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor    ///
166a92d7e7a55a35b28437103130904a6401bf35408Jonathan D. Turner    /// \param Diag Object through which diagnostics will be emitted if the
16712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    ///   buffer cannot be retrieved.
1685330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher    ///
169e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner    /// \param Loc If specified, is the location that invalid file diagnostics
17012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    ///   will be emitted at.
171e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner    ///
17236c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor    /// \param Invalid If non-NULL, will be set \c true if an error occurred.
173d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie    const llvm::MemoryBuffer *getBuffer(DiagnosticsEngine &Diag,
174e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner                                        const SourceManager &SM,
175e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner                                        SourceLocation Loc = SourceLocation(),
17636c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor                                        bool *Invalid = 0) const;
1771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// \brief Returns the size of the content encapsulated by this
17912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// ContentCache.
18012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    ///
18112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// This can be the size of the source file or the size of an
18212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// arbitrary scratch buffer.  If the ContentCache encapsulates a source
18312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// file this size is retrieved from the file's FileEntry.
184c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    unsigned getSize() const;
1851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// \brief Returns the number of bytes actually mapped for this
18712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// ContentCache.
18812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    ///
18912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// This can be 0 if the MemBuffer was not actually expanded.
190c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    unsigned getSizeBytesMapped() const;
1915330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
192f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek    /// Returns the kind of memory used to back the memory buffer for
193f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek    /// this content cache.  This is used for performance analysis.
194f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek    llvm::MemoryBuffer::BufferKind getMemoryBufferKind() const;
1951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19605816591ec488a933dfecc9ff9f3cbf3c32767c2Chris Lattner    void setBuffer(const llvm::MemoryBuffer *B) {
197c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor      assert(!Buffer.getPointer() && "MemoryBuffer already set.");
198c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor      Buffer.setPointer(B);
199c815108d08b0417c6f1104e7df70dc5278839406Douglas Gregor      Buffer.setInt(false);
200c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    }
2015330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
202cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor    /// \brief Get the underlying buffer, returning NULL if the buffer is not
203cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor    /// yet available.
204cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor    const llvm::MemoryBuffer *getRawBuffer() const {
205cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor      return Buffer.getPointer();
206cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor    }
2071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2082968442603b029949246467253eeac8139a5b6d8Douglas Gregor    /// \brief Replace the existing buffer (which will be deleted)
2092968442603b029949246467253eeac8139a5b6d8Douglas Gregor    /// with the given buffer.
210f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    void replaceBuffer(const llvm::MemoryBuffer *B, bool DoNotFree = false);
2112968442603b029949246467253eeac8139a5b6d8Douglas Gregor
212f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    /// \brief Determine whether the buffer itself is invalid.
213f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    bool isBufferInvalid() const {
214f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor      return Buffer.getInt() & InvalidFlag;
215f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    }
2165330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
217f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    /// \brief Determine whether the buffer should be freed.
218f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    bool shouldFreeBuffer() const {
219f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor      return (Buffer.getInt() & DoNotFreeFlag) == 0;
220f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    }
2215330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
2220d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek  private:
2230d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek    // Disable assignments.
224be2fa7ebf01259b63dc52fe46c8d101c18e72269Craig Topper    ContentCache &operator=(const ContentCache& RHS) LLVM_DELETED_FUNCTION;
2251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  };
2265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
22712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Information about a FileID, basically just the logical file
228de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// that it represents and include stack information.
2295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
230de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// Each FileInfo has include stack information, indicating where it came
231c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  /// from. This information encodes the \#include chain that a token was
2323201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// expanded from. The main include file has an invalid IncludeLoc.
2335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
234de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// FileInfos contain a "ContentCache *", with the contents of the file.
2355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
236de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  class FileInfo {
23712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// \brief The location of the \#include that brought in this file.
23812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    ///
239c351d9837ea1d0b04842497e76c76125426a982cJames Dennett    /// This is an invalid SLOC for the main file (top of the \#include chain).
240de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned IncludeLoc;  // Really a SourceLocation
2411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
242d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    /// \brief Number of FileIDs (files and macros) that were created during
243c351d9837ea1d0b04842497e76c76125426a982cJames Dennett    /// preprocessing of this \#include, including this SLocEntry.
24412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    ///
245d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    /// Zero means the preprocessor didn't provide such info for this SLocEntry.
246d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    unsigned NumCreatedFIDs;
247d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis
248c351d9837ea1d0b04842497e76c76125426a982cJames Dennett    /// \brief Contains the ContentCache* and the bits indicating the
249c351d9837ea1d0b04842497e76c76125426a982cJames Dennett    /// characteristic of the file and whether it has \#line info, all
250c351d9837ea1d0b04842497e76c76125426a982cJames Dennett    /// bitmangled together.
2516e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    uintptr_t Data;
252d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis
25321032df7a0dfc129a8f0c5e004811b455baafb7aArgyrios Kyrtzidis    friend class clang::SourceManager;
25421032df7a0dfc129a8f0c5e004811b455baafb7aArgyrios Kyrtzidis    friend class clang::ASTWriter;
25521032df7a0dfc129a8f0c5e004811b455baafb7aArgyrios Kyrtzidis    friend class clang::ASTReader;
25678d85f53b093867bbb0123f016956178eea7343eTed Kremenek  public:
25712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// \brief Return a FileInfo object.
258de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    static FileInfo get(SourceLocation IL, const ContentCache *Con,
259de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner                        CharacteristicKind FileCharacter) {
260de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      FileInfo X;
261de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      X.IncludeLoc = IL.getRawEncoding();
262d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis      X.NumCreatedFIDs = 0;
2636e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner      X.Data = (uintptr_t)Con;
26400282d6e1194655a2e89f940bd6fa8484b52e666Chris Lattner      assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned");
2656e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner      assert((unsigned)FileCharacter < 4 && "invalid file character");
2666e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner      X.Data |= (unsigned)FileCharacter;
2675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return X;
2685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
2691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
270de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    SourceLocation getIncludeLoc() const {
271de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return SourceLocation::getFromRawEncoding(IncludeLoc);
272de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
2736e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    const ContentCache* getContentCache() const {
274a5bec298d96f7eb5309df5aab9d19eb7f8b75a81Michael J. Spencer      return reinterpret_cast<const ContentCache*>(Data & ~uintptr_t(7));
2756e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    }
2761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
277c351d9837ea1d0b04842497e76c76125426a982cJames Dennett    /// \brief Return whether this is a system header or not.
2781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    CharacteristicKind getFileCharacteristic() const {
2796e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner      return (CharacteristicKind)(Data & 3);
2800b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner    }
281ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner
282c351d9837ea1d0b04842497e76c76125426a982cJames Dennett    /// \brief Return true if this FileID has \#line directives in it.
283ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    bool hasLineDirectives() const { return (Data & 4) != 0; }
2841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
285c351d9837ea1d0b04842497e76c76125426a982cJames Dennett    /// \brief Set the flag that indicates that this FileID has
286ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    /// line table entries associated with it.
287ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    void setHasLineDirectives() {
288ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner      Data |= 4;
289ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    }
2909dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  };
2911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Each ExpansionInfo encodes the expansion location - where
29378df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth  /// the token was ultimately expanded, and the SpellingLoc - where the actual
29478df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth  /// character data for the token came from.
29578df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth  class ExpansionInfo {
29678df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    // Really these are all SourceLocations.
2971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
298af50aab0c317462129d73ae8000c6394c718598dJames Dennett    /// \brief Where the spelling for the token can be found.
299e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    unsigned SpellingLoc;
3001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
301af50aab0c317462129d73ae8000c6394c718598dJames Dennett    /// In a macro expansion, ExpansionLocStart and ExpansionLocEnd
30278df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// indicate the start and end of the expansion. In object-like macros,
303af50aab0c317462129d73ae8000c6394c718598dJames Dennett    /// they will be the same. In a function-like macro expansion, the start
3043201f382956ed9beee9fb31229c2835c1208889cChandler Carruth    /// will be the identifier and the end will be the ')'. Finally, in
30512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// macro-argument instantiations, the end will be 'SourceLocation()', an
306c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    /// invalid location.
30778df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    unsigned ExpansionLocStart, ExpansionLocEnd;
308c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth
3099dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  public:
310de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    SourceLocation getSpellingLoc() const {
311de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return SourceLocation::getFromRawEncoding(SpellingLoc);
312de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
31378df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    SourceLocation getExpansionLocStart() const {
31478df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth      return SourceLocation::getFromRawEncoding(ExpansionLocStart);
315e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    }
31678df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    SourceLocation getExpansionLocEnd() const {
317c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth      SourceLocation EndLoc =
31878df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth        SourceLocation::getFromRawEncoding(ExpansionLocEnd);
31978df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth      return EndLoc.isInvalid() ? getExpansionLocStart() : EndLoc;
320e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    }
3211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32278df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    std::pair<SourceLocation,SourceLocation> getExpansionLocRange() const {
32378df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth      return std::make_pair(getExpansionLocStart(), getExpansionLocEnd());
324e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    }
3251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32696d3589e523a04f4ff2058a7919226ce60696ae8Chandler Carruth    bool isMacroArgExpansion() const {
327c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth      // Note that this needs to return false for default constructed objects.
32878df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth      return getExpansionLocStart().isValid() &&
32978df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth        SourceLocation::getFromRawEncoding(ExpansionLocEnd).isInvalid();
330c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    }
331c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth
332c3cd6f7a5d33ad44f6c9cf4faa7046c77baa128eMatt Beaumont-Gay    bool isMacroBodyExpansion() const {
333c3cd6f7a5d33ad44f6c9cf4faa7046c77baa128eMatt Beaumont-Gay      return getExpansionLocStart().isValid() &&
334c3cd6f7a5d33ad44f6c9cf4faa7046c77baa128eMatt Beaumont-Gay        SourceLocation::getFromRawEncoding(ExpansionLocEnd).isValid();
335c3cd6f7a5d33ad44f6c9cf4faa7046c77baa128eMatt Beaumont-Gay    }
336c3cd6f7a5d33ad44f6c9cf4faa7046c77baa128eMatt Beaumont-Gay
337cee5ec9df479994e4ba27fb65b7ded5bb5a980ebArgyrios Kyrtzidis    bool isFunctionMacroExpansion() const {
338cee5ec9df479994e4ba27fb65b7ded5bb5a980ebArgyrios Kyrtzidis      return getExpansionLocStart().isValid() &&
339cee5ec9df479994e4ba27fb65b7ded5bb5a980ebArgyrios Kyrtzidis          getExpansionLocStart() != getExpansionLocEnd();
340cee5ec9df479994e4ba27fb65b7ded5bb5a980ebArgyrios Kyrtzidis    }
341cee5ec9df479994e4ba27fb65b7ded5bb5a980ebArgyrios Kyrtzidis
34212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// \brief Return a ExpansionInfo for an expansion.
34312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    ///
34412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// Start and End specify the expansion range (where the macro is
34512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// expanded), and SpellingLoc specifies the spelling location (where
34612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// the characters from the token come from). All three can refer to
34712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// normal File SLocs or expansion locations.
34878df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    static ExpansionInfo create(SourceLocation SpellingLoc,
34978df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth                                SourceLocation Start, SourceLocation End) {
35078df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth      ExpansionInfo X;
35178df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth      X.SpellingLoc = SpellingLoc.getRawEncoding();
35278df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth      X.ExpansionLocStart = Start.getRawEncoding();
35378df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth      X.ExpansionLocEnd = End.getRawEncoding();
3549dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner      return X;
3555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
356c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth
35712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// \brief Return a special ExpansionInfo for the expansion of
35812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// a macro argument into a function-like macro's body.
35912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    ///
36012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// ExpansionLoc specifies the expansion location (where the macro is
36112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// expanded). This doesn't need to be a range because a macro is always
36212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// expanded at a macro parameter reference, and macro parameters are
36312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// always exactly one token. SpellingLoc specifies the spelling location
36412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// (where the characters from the token come from). ExpansionLoc and
36512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett    /// SpellingLoc can both refer to normal File SLocs or expansion locations.
366c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    ///
367c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    /// Given the code:
368c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    /// \code
369af50aab0c317462129d73ae8000c6394c718598dJames Dennett    ///   #define F(x) f(x)
370c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    ///   F(42);
371c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    /// \endcode
372c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    ///
37378df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// When expanding '\c F(42)', the '\c x' would call this with an
374af50aab0c317462129d73ae8000c6394c718598dJames Dennett    /// SpellingLoc pointing at '\c 42' and an ExpansionLoc pointing at its
37578df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    /// location in the definition of '\c F'.
37678df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    static ExpansionInfo createForMacroArg(SourceLocation SpellingLoc,
37778df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth                                           SourceLocation ExpansionLoc) {
378c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth      // We store an intentionally invalid source location for the end of the
37978df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth      // expansion range to mark that this is a macro argument ion rather than
38078df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth      // a normal one.
38178df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth      return create(SpellingLoc, ExpansionLoc, SourceLocation());
382c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth    }
383de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  };
3841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
38512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief This is a discriminated union of FileInfo and ExpansionInfo.
38612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
38712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// SourceManager keeps an array of these objects, and they are uniquely
38812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// identified by the FileID datatype.
389de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  class SLocEntry {
3903201f382956ed9beee9fb31229c2835c1208889cChandler Carruth    unsigned Offset;   // low bit is set for expansion info.
391de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    union {
392de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      FileInfo File;
3931728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth      ExpansionInfo Expansion;
394de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    };
395de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  public:
396de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned getOffset() const { return Offset >> 1; }
3971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3981728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth    bool isExpansion() const { return Offset & 1; }
3991728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth    bool isFile() const { return !isExpansion(); }
4001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
401de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    const FileInfo &getFile() const {
402de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      assert(isFile() && "Not a file SLocEntry!");
403de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return File;
404de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
405de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
4061728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth    const ExpansionInfo &getExpansion() const {
4071728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth      assert(isExpansion() && "Not a macro expansion SLocEntry!");
4081728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth      return Expansion;
409de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
4101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
411de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    static SLocEntry get(unsigned Offset, const FileInfo &FI) {
412de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      SLocEntry E;
413de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      E.Offset = Offset << 1;
414de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      E.File = FI;
415de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return E;
416de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
417de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
41878df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth    static SLocEntry get(unsigned Offset, const ExpansionInfo &Expansion) {
419de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      SLocEntry E;
420de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      E.Offset = (Offset << 1) | 1;
4211728762d5a8cfaf8d64385f47b311e84de1ae7a2Chandler Carruth      E.Expansion = Expansion;
422de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return E;
423de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
4245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  };
4255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}  // end SrcMgr namespace.
4267f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
4277f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor/// \brief External source of source location entries.
4287f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregorclass ExternalSLocEntrySource {
4297f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregorpublic:
4307f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  virtual ~ExternalSLocEntrySource();
4317f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
432f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief Read the source location entry with index ID, which will always be
433f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// less than -1.
434e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor  ///
435e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor  /// \returns true if an error occurred that prevented the source-location
436e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor  /// entry from being loaded.
437f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  virtual bool ReadSLocEntry(int ID) = 0;
4386c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor
4396c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor  /// \brief Retrieve the module import location and name for the given ID, if
4406c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor  /// in fact it was loaded from a module (rather than, say, a precompiled
4416c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor  /// header).
4426c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor  virtual std::pair<SourceLocation, StringRef> getModuleImportLoc(int ID) = 0;
4437f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor};
4445330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
445dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner
44612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// \brief Holds the cache used by isBeforeInTranslationUnit.
44712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett///
44812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// The cache structure is complex enough to be worth breaking out of
44912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett/// SourceManager.
4502564f811ba107cb314a594d730aa3357b6181b62Ted Kremenekclass InBeforeInTUCacheEntry {
451af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief The FileID's of the cached query.
452af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
453af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// If these match up with a subsequent query, the result can be reused.
454dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  FileID LQueryFID, RQueryFID;
45537e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis
456af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief True if LQueryFID was created before RQueryFID.
457af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
458af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// This is used to compare macro expansion locations.
45937e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis  bool IsLQFIDBeforeRQFID;
46037e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis
46112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief The file found in common between the two \#include traces, i.e.,
46212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// the nearest common ancestor of the \#include tree.
463dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  FileID CommonFID;
4645330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
465af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief The offset of the previous query in CommonFID.
466af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
467c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  /// Usually, this represents the location of the \#include for QueryFID, but
468c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  /// if LQueryFID is a parent of RQueryFID (or vice versa) then these can be a
469dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  /// random token in the parent.
470dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  unsigned LCommonOffset, RCommonOffset;
471dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattnerpublic:
47212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Return true if the currently cached values match up with
473af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// the specified LHS/RHS query.
474af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
475af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// If not, we can't use the cache.
476dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  bool isCacheValid(FileID LHS, FileID RHS) const {
477dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    return LQueryFID == LHS && RQueryFID == RHS;
478dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  }
4795330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
48012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief If the cache is valid, compute the result given the
481af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// specified offsets in the LHS/RHS FileID's.
482dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  bool getCachedResult(unsigned LOffset, unsigned ROffset) const {
483dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    // If one of the query files is the common file, use the offset.  Otherwise,
484dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    // use the #include loc in the common file.
485dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    if (LQueryFID != CommonFID) LOffset = LCommonOffset;
486dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    if (RQueryFID != CommonFID) ROffset = RCommonOffset;
48737e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis
48837e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis    // It is common for multiple macro expansions to be "included" from the same
48937e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis    // location (expansion location), in which case use the order of the FileIDs
4904d1cbcf6b9aaae7f82c6e332f46b2ad2d8971bd0Argyrios Kyrtzidis    // to determine which came first. This will also take care the case where
4914d1cbcf6b9aaae7f82c6e332f46b2ad2d8971bd0Argyrios Kyrtzidis    // one of the locations points at the inclusion/expansion point of the other
4924d1cbcf6b9aaae7f82c6e332f46b2ad2d8971bd0Argyrios Kyrtzidis    // in which case its FileID will come before the other.
493d7711ec430fde5706f85ba6c4b85283a8e743ff7Argyrios Kyrtzidis    if (LOffset == ROffset)
49437e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis      return IsLQFIDBeforeRQFID;
49537e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis
496dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    return LOffset < ROffset;
497dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  }
4985330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
499af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Set up a new query.
50037e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis  void setQueryFIDs(FileID LHS, FileID RHS, bool isLFIDBeforeRFID) {
50137e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis    assert(LHS != RHS);
502dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    LQueryFID = LHS;
503dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    RQueryFID = RHS;
50437e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis    IsLQFIDBeforeRQFID = isLFIDBeforeRFID;
50537e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis  }
50637e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis
50737e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis  void clear() {
50837e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis    LQueryFID = RQueryFID = FileID();
50937e59a10a7a537428e5997fd5896f5b89fd34e6bArgyrios Kyrtzidis    IsLQFIDBeforeRQFID = false;
510dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  }
5115330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
512dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  void setCommonLoc(FileID commonFID, unsigned lCommonOffset,
513dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner                    unsigned rCommonOffset) {
514dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    CommonFID = commonFID;
515dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    LCommonOffset = lCommonOffset;
516dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner    RCommonOffset = rCommonOffset;
517dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner  }
5185330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
519dcb1d68f6ffa183f3919aee6b554aec3793bf13eChris Lattner};
5207f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
5214565e487531c7bf6d348dbe9f5529784966fc7aeDouglas Gregor/// \brief The stack used when building modules on demand, which is used
522830ea5b7c75413526c19531f0180fa6e45b98919Douglas Gregor/// to provide a link between the source managers of the different compiler
523830ea5b7c75413526c19531f0180fa6e45b98919Douglas Gregor/// instances.
524cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenkotypedef ArrayRef<std::pair<std::string, FullSourceLoc> > ModuleBuildStack;
525830ea5b7c75413526c19531f0180fa6e45b98919Douglas Gregor
526f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor/// \brief This class handles loading and caching of source files into memory.
527f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor///
528f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor/// This object owns the MemoryBuffer objects for all of the loaded
529c351d9837ea1d0b04842497e76c76125426a982cJames Dennett/// files and assigns unique FileID's for each unique \#include chain.
5305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
5315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// The SourceManager can be queried for information about SourceLocation
5323201f382956ed9beee9fb31229c2835c1208889cChandler Carruth/// objects, turning them into either spelling or expansion locations. Spelling
5333201f382956ed9beee9fb31229c2835c1208889cChandler Carruth/// locations represent where the bytes corresponding to a token came from and
5343201f382956ed9beee9fb31229c2835c1208889cChandler Carruth/// expansion locations represent where the location is in the user's view. In
5353201f382956ed9beee9fb31229c2835c1208889cChandler Carruth/// the case of a macro expansion, for example, the spelling location indicates
5363201f382956ed9beee9fb31229c2835c1208889cChandler Carruth/// where the expanded token came from and the expansion location specifies
5373201f382956ed9beee9fb31229c2835c1208889cChandler Carruth/// where it was expanded.
538c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmithclass SourceManager : public RefCountedBase<SourceManager> {
539d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  /// \brief DiagnosticsEngine object.
540d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  DiagnosticsEngine &Diag;
541389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
542389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis  FileManager &FileMgr;
543389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
5440d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  mutable llvm::BumpPtrAllocator ContentCacheAlloc;
5451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
54612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Memoized information about all of the files tracked by this
54712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// SourceManager.
54812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
54912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// This map allows us to merge ContentCache entries based
5500d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek  /// on their FileEntry*.  All ContentCache objects will thus have unique,
5511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// non-null, FileEntry pointers.
5520d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos;
5531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
554299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  /// \brief True if the ContentCache for files that are overriden by other
555299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  /// files, should report the original file name. Defaults to true.
556299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  bool OverridenFilesKeepOriginalName;
557299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis
558ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis  /// \brief True if non-system source files should be treated as volatile
559ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis  /// (likely to change while trying to use them). Defaults to false.
560ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis  bool UserFilesAreVolatile;
561ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis
562d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis  struct OverriddenFilesInfoTy {
563d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis    /// \brief Files that have been overriden with the contents from another
564d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis    /// file.
565d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis    llvm::DenseMap<const FileEntry *, const FileEntry *> OverriddenFiles;
566d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis    /// \brief Files that were overridden with a memory buffer.
567d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis    llvm::DenseSet<const FileEntry *> OverriddenFilesWithBuffer;
568d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis  };
569d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis
570d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis  /// \brief Lazily create the object keeping overridden files info, since
571d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis  /// it is uncommonly used.
572d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis  OwningPtr<OverriddenFilesInfoTy> OverriddenFilesInfo;
573b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis
574d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis  OverriddenFilesInfoTy &getOverriddenFilesInfo() {
575d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis    if (!OverriddenFilesInfo)
576d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis      OverriddenFilesInfo.reset(new OverriddenFilesInfoTy);
577d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis    return *OverriddenFilesInfo;
578d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis  }
57912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett
58012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Information about various memory buffers that we have read in.
58112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
58212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// All FileEntry* within the stored ContentCache objects are NULL,
5830d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  /// as they do not refer to a file.
5840d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  std::vector<SrcMgr::ContentCache*> MemBufferInfos;
5851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
586f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief The table of SLocEntries that are local to this module.
587f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///
588f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// Positive FileIDs are indexes into this table. Entry 0 indicates an invalid
5893201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// expansion.
590f512acee01617c9da8079ed88ded3bb9f2418349Benjamin Kramer  SmallVector<SrcMgr::SLocEntry, 0> LocalSLocEntryTable;
591f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
592f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief The table of SLocEntries that are loaded from other modules.
593f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///
594f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// Negative FileIDs are indexes into this table. To get from ID to an index,
595f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// use (-ID - 2).
596f512acee01617c9da8079ed88ded3bb9f2418349Benjamin Kramer  mutable SmallVector<SrcMgr::SLocEntry, 0> LoadedSLocEntryTable;
5977f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
598f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief The starting offset of the next local SLocEntry.
599f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///
600f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// This is LocalSLocEntryTable.back().Offset + the size of that entry.
601f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  unsigned NextLocalOffset;
602f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
603f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief The starting offset of the latest batch of loaded SLocEntries.
604f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///
605f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// This is LoadedSLocEntryTable.back().Offset, except that that entry might
606f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// not have been loaded, so that value would be unknown.
607f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  unsigned CurrentLoadedOffset;
608f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
609ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  /// \brief The highest possible offset is 2^31-1, so CurrentLoadedOffset
610ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  /// starts at 2^31.
611ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  static const unsigned MaxLoadedOffset = 1U << 31U;
612ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis
613f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief A bitmap that indicates whether the entries of LoadedSLocEntryTable
614f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// have already been loaded from the external source.
615f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///
616f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// Same indexing as LoadedSLocEntryTable.
6177f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  std::vector<bool> SLocEntryLoaded;
6187f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
6197f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  /// \brief An external source for source location entries.
6207f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  ExternalSLocEntrySource *ExternalSLocEntries;
6217f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
62212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief A one-entry cache to speed up getFileID.
623c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  ///
624de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// LastFileIDLookup records the last FileID looked up or created, because it
625de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// is very common to look up many tokens from the same file.
626de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  mutable FileID LastFileIDLookup;
6271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
628c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  /// \brief Holds information for \#line directives.
629c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  ///
630af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// This is referenced by indices from SLocEntryTable.
6315b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  LineTableInfo *LineTable;
6321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
63312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief These ivars serve as a cache used in the getLineNumber
6345e36a7a89da5d69ece23fc8228624a053f75c645Chris Lattner  /// method which is used to speedup getLineNumber calls to nearby locations.
6352b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  mutable FileID LastLineNoFileIDQuery;
636f812a45dd93634c9300ed5533bd26b56374714a1Chris Lattner  mutable SrcMgr::ContentCache *LastLineNoContentCache;
637f812a45dd93634c9300ed5533bd26b56374714a1Chris Lattner  mutable unsigned LastLineNoFilePos;
638f812a45dd93634c9300ed5533bd26b56374714a1Chris Lattner  mutable unsigned LastLineNoResult;
6391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
640c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  /// \brief The file ID for the main source file of the translation unit.
6412b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID MainFileID;
64249c1f4aa2a6c360d25d605004ec3c4affd62db77Steve Naroff
643507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  /// \brief The file ID for the precompiled preamble there is one.
644507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  FileID PreambleFileID;
645507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
646de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  // Statistics for -print-stats.
647de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  mutable unsigned NumLinearScans, NumBinaryProbes;
6481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
649ecc65238c98ba21d08763da7b7972d617677e908Argyrios Kyrtzidis  /// \brief Associates a FileID with its "included/expanded in" decomposed
650ecc65238c98ba21d08763da7b7972d617677e908Argyrios Kyrtzidis  /// location.
651ecc65238c98ba21d08763da7b7972d617677e908Argyrios Kyrtzidis  ///
652ecc65238c98ba21d08763da7b7972d617677e908Argyrios Kyrtzidis  /// Used to cache results from and speed-up \c getDecomposedIncludedLoc
653ecc65238c98ba21d08763da7b7972d617677e908Argyrios Kyrtzidis  /// function.
654ecc65238c98ba21d08763da7b7972d617677e908Argyrios Kyrtzidis  mutable llvm::DenseMap<FileID, std::pair<FileID, unsigned> > IncludedLocMap;
655ecc65238c98ba21d08763da7b7972d617677e908Argyrios Kyrtzidis
6562564f811ba107cb314a594d730aa3357b6181b62Ted Kremenek  /// The key value into the IsBeforeInTUCache table.
6572564f811ba107cb314a594d730aa3357b6181b62Ted Kremenek  typedef std::pair<FileID, FileID> IsBeforeInTUCacheKey;
6582564f811ba107cb314a594d730aa3357b6181b62Ted Kremenek
6592564f811ba107cb314a594d730aa3357b6181b62Ted Kremenek  /// The IsBeforeInTranslationUnitCache is a mapping from FileID pairs
6602564f811ba107cb314a594d730aa3357b6181b62Ted Kremenek  /// to cache results.
6612564f811ba107cb314a594d730aa3357b6181b62Ted Kremenek  typedef llvm::DenseMap<IsBeforeInTUCacheKey, InBeforeInTUCacheEntry>
6622564f811ba107cb314a594d730aa3357b6181b62Ted Kremenek          InBeforeInTUCache;
6632564f811ba107cb314a594d730aa3357b6181b62Ted Kremenek
6642564f811ba107cb314a594d730aa3357b6181b62Ted Kremenek  /// Cache results for the isBeforeInTranslationUnit method.
6652564f811ba107cb314a594d730aa3357b6181b62Ted Kremenek  mutable InBeforeInTUCache IBTUCache;
6662564f811ba107cb314a594d730aa3357b6181b62Ted Kremenek  mutable InBeforeInTUCacheEntry IBTUCacheOverflow;
6672564f811ba107cb314a594d730aa3357b6181b62Ted Kremenek
6682564f811ba107cb314a594d730aa3357b6181b62Ted Kremenek  /// Return the cache entry for comparing the given file IDs
6692564f811ba107cb314a594d730aa3357b6181b62Ted Kremenek  /// for isBeforeInTranslationUnit.
6702564f811ba107cb314a594d730aa3357b6181b62Ted Kremenek  InBeforeInTUCacheEntry &getInBeforeInTUCache(FileID LFID, FileID RFID) const;
6711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
672e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor  // Cache for the "fake" buffer used for error-recovery purposes.
673e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor  mutable llvm::MemoryBuffer *FakeBufferForRecovery;
6745330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
675a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis  mutable SrcMgr::ContentCache *FakeContentCacheForRecovery;
676a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis
677fb3612ef197cb8532c05f33889ec1aed7c26e5cbArgyrios Kyrtzidis  /// \brief Lazily computed map of macro argument chunks to their expanded
678fb3612ef197cb8532c05f33889ec1aed7c26e5cbArgyrios Kyrtzidis  /// source location.
679fb3612ef197cb8532c05f33889ec1aed7c26e5cbArgyrios Kyrtzidis  typedef std::map<unsigned, SourceLocation> MacroArgsMap;
680fb3612ef197cb8532c05f33889ec1aed7c26e5cbArgyrios Kyrtzidis
68170042f5d1ca378138f90b7b9384023701f5d03d8David Blaikie  mutable llvm::DenseMap<FileID, MacroArgsMap *> MacroArgsCacheMap;
682fb3612ef197cb8532c05f33889ec1aed7c26e5cbArgyrios Kyrtzidis
6834565e487531c7bf6d348dbe9f5529784966fc7aeDouglas Gregor  /// \brief The stack of modules being built, which is used to detect
684830ea5b7c75413526c19531f0180fa6e45b98919Douglas Gregor  /// cycles in the module dependency graph as modules are being built, as
6854565e487531c7bf6d348dbe9f5529784966fc7aeDouglas Gregor  /// well as to describe why we're rebuilding a particular module.
686830ea5b7c75413526c19531f0180fa6e45b98919Douglas Gregor  ///
687830ea5b7c75413526c19531f0180fa6e45b98919Douglas Gregor  /// There is no way to set this value from the command line. If we ever need
688830ea5b7c75413526c19531f0180fa6e45b98919Douglas Gregor  /// to do so (e.g., if on-demand module construction moves out-of-process),
689830ea5b7c75413526c19531f0180fa6e45b98919Douglas Gregor  /// we can add a cc1-level option to do so.
6904565e487531c7bf6d348dbe9f5529784966fc7aeDouglas Gregor  SmallVector<std::pair<std::string, FullSourceLoc>, 2> StoredModuleBuildStack;
691830ea5b7c75413526c19531f0180fa6e45b98919Douglas Gregor
69249c1f4aa2a6c360d25d605004ec3c4affd62db77Steve Naroff  // SourceManager doesn't support copy construction.
693be2fa7ebf01259b63dc52fe46c8d101c18e72269Craig Topper  explicit SourceManager(const SourceManager&) LLVM_DELETED_FUNCTION;
694be2fa7ebf01259b63dc52fe46c8d101c18e72269Craig Topper  void operator=(const SourceManager&) LLVM_DELETED_FUNCTION;
6955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
696ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis  SourceManager(DiagnosticsEngine &Diag, FileManager &FileMgr,
697ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis                bool UserFilesAreVolatile = false);
6985b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  ~SourceManager();
6991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7005b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  void clearIDTables();
7011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
702d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  DiagnosticsEngine &getDiagnostics() const { return Diag; }
70378a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios Kyrtzidis
704389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis  FileManager &getFileManager() const { return FileMgr; }
705389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
706299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  /// \brief Set true if the SourceManager should report the original file name
707299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  /// for contents of files that were overriden by other files.Defaults to true.
708299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  void setOverridenFilesKeepOriginalName(bool value) {
709299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis    OverridenFilesKeepOriginalName = value;
710299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  }
711299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis
712ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis  /// \brief True if non-system source files should be treated as volatile
713ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis  /// (likely to change while trying to use them).
714ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis  bool userFilesAreVolatile() const { return UserFilesAreVolatile; }
715ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis
7164565e487531c7bf6d348dbe9f5529784966fc7aeDouglas Gregor  /// \brief Retrieve the module build stack.
7174565e487531c7bf6d348dbe9f5529784966fc7aeDouglas Gregor  ModuleBuildStack getModuleBuildStack() const {
7184565e487531c7bf6d348dbe9f5529784966fc7aeDouglas Gregor    return StoredModuleBuildStack;
719830ea5b7c75413526c19531f0180fa6e45b98919Douglas Gregor  }
720830ea5b7c75413526c19531f0180fa6e45b98919Douglas Gregor
7214565e487531c7bf6d348dbe9f5529784966fc7aeDouglas Gregor  /// \brief Set the module build stack.
7224565e487531c7bf6d348dbe9f5529784966fc7aeDouglas Gregor  void setModuleBuildStack(ModuleBuildStack stack) {
7234565e487531c7bf6d348dbe9f5529784966fc7aeDouglas Gregor    StoredModuleBuildStack.clear();
7244565e487531c7bf6d348dbe9f5529784966fc7aeDouglas Gregor    StoredModuleBuildStack.append(stack.begin(), stack.end());
725830ea5b7c75413526c19531f0180fa6e45b98919Douglas Gregor  }
726830ea5b7c75413526c19531f0180fa6e45b98919Douglas Gregor
7274565e487531c7bf6d348dbe9f5529784966fc7aeDouglas Gregor  /// \brief Push an entry to the module build stack.
7284565e487531c7bf6d348dbe9f5529784966fc7aeDouglas Gregor  void pushModuleBuildStack(StringRef moduleName, FullSourceLoc importLoc) {
7294565e487531c7bf6d348dbe9f5529784966fc7aeDouglas Gregor    StoredModuleBuildStack.push_back(std::make_pair(moduleName.str(),importLoc));
730830ea5b7c75413526c19531f0180fa6e45b98919Douglas Gregor  }
731830ea5b7c75413526c19531f0180fa6e45b98919Douglas Gregor
73212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Create the FileID for a memory buffer that will represent the
73312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// FileID for the main source.
73412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
73512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// One example of when this would be used is when the main source is read
73612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// from STDIN.
737ecd27bf256c92f56c7c7ede6f40ec5d31a40b35eArgyrios Kyrtzidis  FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer,
738ecd27bf256c92f56c7c7ede6f40ec5d31a40b35eArgyrios Kyrtzidis                             SrcMgr::CharacteristicKind Kind = SrcMgr::C_User) {
739f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    assert(MainFileID.isInvalid() && "MainFileID already set!");
740ecd27bf256c92f56c7c7ede6f40ec5d31a40b35eArgyrios Kyrtzidis    MainFileID = createFileIDForMemBuffer(Buffer, Kind);
741f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    return MainFileID;
742f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  }
743f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
74406a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
74506a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  // MainFileID creation and querying methods.
74606a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
74706a062dc784c609b75dca15fd97f468d0d846596Chris Lattner
74812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Returns the FileID of the main source file.
7492b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID getMainFileID() const { return MainFileID; }
7501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
75112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Create the FileID for the main source file.
752a1f1fad8b60e1cb9d21a40a37f2e03150bcbeb6fDouglas Gregor  FileID createMainFileID(const FileEntry *SourceFile,
753a1f1fad8b60e1cb9d21a40a37f2e03150bcbeb6fDouglas Gregor                          SrcMgr::CharacteristicKind Kind = SrcMgr::C_User) {
75406a062dc784c609b75dca15fd97f468d0d846596Chris Lattner    assert(MainFileID.isInvalid() && "MainFileID already set!");
755a1f1fad8b60e1cb9d21a40a37f2e03150bcbeb6fDouglas Gregor    MainFileID = createFileID(SourceFile, SourceLocation(), Kind);
75606a062dc784c609b75dca15fd97f468d0d846596Chris Lattner    return MainFileID;
75706a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  }
7581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
759b8c879a5363f36bdae8831112b563333e3c05acbArgyrios Kyrtzidis  /// \brief Set the file ID for the main source file.
760b8c879a5363f36bdae8831112b563333e3c05acbArgyrios Kyrtzidis  void setMainFileID(FileID FID) {
761b8c879a5363f36bdae8831112b563333e3c05acbArgyrios Kyrtzidis    assert(MainFileID.isInvalid() && "MainFileID already set!");
762b8c879a5363f36bdae8831112b563333e3c05acbArgyrios Kyrtzidis    MainFileID = FID;
763b8c879a5363f36bdae8831112b563333e3c05acbArgyrios Kyrtzidis  }
764b8c879a5363f36bdae8831112b563333e3c05acbArgyrios Kyrtzidis
765507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  /// \brief Set the file ID for the precompiled preamble.
766507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  void setPreambleFileID(FileID Preamble) {
767507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    assert(PreambleFileID.isInvalid() && "PreambleFileID already set!");
768507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    PreambleFileID = Preamble;
769414cb64f09ce48a36377458ce5e5a90c3ad41d00Douglas Gregor  }
7705330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
771507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  /// \brief Get the file ID for the precompiled preamble if there is one.
772507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  FileID getPreambleFileID() const { return PreambleFileID; }
773507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
77406a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
7753201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  // Methods to create new FileID's and macro expansions.
77606a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
7771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
77812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Create a new FileID that represents the specified file
77912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// being \#included from the specified IncludePosition.
78012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
78112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// This translates NULL into standard input.
7822b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
7837f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                      SrcMgr::CharacteristicKind FileCharacter,
784f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor                      int LoadedID = 0, unsigned LoadedOffset = 0) {
785ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis    const SrcMgr::ContentCache *
786ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis      IR = getOrCreateContentCache(SourceFile,
787ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis                              /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
7880d06e998910934e5ef070f53f4c272e7c6b846c6Dan Gohman    assert(IR && "getOrCreateContentCache() cannot return NULL");
789f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    return createFileID(IR, IncludePos, FileCharacter, LoadedID, LoadedOffset);
7905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
7911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
79212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Create a new FileID that represents the specified memory buffer.
79312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
79412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// This does no caching of the buffer and takes ownership of the
79512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// MemoryBuffer, so only pass a MemoryBuffer to this once.
7967f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer,
797ecd27bf256c92f56c7c7ede6f40ec5d31a40b35eArgyrios Kyrtzidis                      SrcMgr::CharacteristicKind FileCharacter = SrcMgr::C_User,
798f453cb9f677e16c00b358ec91eccf5f003765dc6Axel Naumann                                  int LoadedID = 0, unsigned LoadedOffset = 0,
799f453cb9f677e16c00b358ec91eccf5f003765dc6Axel Naumann                                 SourceLocation IncludeLoc = SourceLocation()) {
800f453cb9f677e16c00b358ec91eccf5f003765dc6Axel Naumann    return createFileID(createMemBufferContentCache(Buffer), IncludeLoc,
801ecd27bf256c92f56c7c7ede6f40ec5d31a40b35eArgyrios Kyrtzidis                        FileCharacter, LoadedID, LoadedOffset);
8021036b68525f39cb69ac22c679ed440acd8392a16Ted Kremenek  }
80306a062dc784c609b75dca15fd97f468d0d846596Chris Lattner
80412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Return a new SourceLocation that encodes the
805bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth  /// fact that a token from SpellingLoc should actually be referenced from
8063201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// ExpansionLoc, and that it represents the expansion of a macro argument
8073201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// into the function-like macro body.
808bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth  SourceLocation createMacroArgExpansionLoc(SourceLocation Loc,
809bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                            SourceLocation ExpansionLoc,
810bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                            unsigned TokLength);
811c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth
81212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Return a new SourceLocation that encodes the fact
813c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth  /// that a token from SpellingLoc should actually be referenced from
814bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth  /// ExpansionLoc.
815bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth  SourceLocation createExpansionLoc(SourceLocation Loc,
816bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                    SourceLocation ExpansionLocStart,
817bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                    SourceLocation ExpansionLocEnd,
818bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                    unsigned TokLength,
819bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                    int LoadedID = 0,
820bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                    unsigned LoadedOffset = 0);
8211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8222968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \brief Retrieve the memory buffer associated with the given file.
82350f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  ///
82450f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  /// \param Invalid If non-NULL, will be set \c true if an error
82550f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  /// occurs while retrieving the memory buffer.
82650f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File,
82750f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor                                                   bool *Invalid = 0);
8282968442603b029949246467253eeac8139a5b6d8Douglas Gregor
8292968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \brief Override the contents of the given source file by providing an
8302968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// already-allocated buffer.
8312968442603b029949246467253eeac8139a5b6d8Douglas Gregor  ///
832afbf5f810652f04c0bba235c0fa079885fb3caa8Dan Gohman  /// \param SourceFile the source file whose contents will be overriden.
8332968442603b029949246467253eeac8139a5b6d8Douglas Gregor  ///
8342968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \param Buffer the memory buffer whose contents will be used as the
8352968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// data in the given source file.
8362968442603b029949246467253eeac8139a5b6d8Douglas Gregor  ///
837f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// \param DoNotFree If true, then the buffer will not be freed when the
838f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// source manager is destroyed.
8390d06e998910934e5ef070f53f4c272e7c6b846c6Dan Gohman  void overrideFileContents(const FileEntry *SourceFile,
840f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor                            const llvm::MemoryBuffer *Buffer,
841f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor                            bool DoNotFree = false);
8422968442603b029949246467253eeac8139a5b6d8Douglas Gregor
843bed28ac1d1463adca3ecf24fca5c30646fa9dbb2Sylvestre Ledru  /// \brief Override the given source file with another one.
844b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  ///
845b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  /// \param SourceFile the source file which will be overriden.
846b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  ///
847b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  /// \param NewFile the file whose contents will be used as the
848b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  /// data instead of the contents of the given source file.
849b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  void overrideFileContents(const FileEntry *SourceFile,
850b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis                            const FileEntry *NewFile);
851b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis
852d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis  /// \brief Returns true if the file contents have been overridden.
853d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis  bool isFileOverridden(const FileEntry *File) {
854d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis    if (OverriddenFilesInfo) {
855d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis      if (OverriddenFilesInfo->OverriddenFilesWithBuffer.count(File))
856d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis        return true;
857d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis      if (OverriddenFilesInfo->OverriddenFiles.find(File) !=
858d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis          OverriddenFilesInfo->OverriddenFiles.end())
859d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis        return true;
860d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis    }
861d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis    return false;
862d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis  }
863d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis
864d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis  /// \brief Disable overridding the contents of a file, previously enabled
865af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// with #overrideFileContents.
86612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
867d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis  /// This should be called before parsing has begun.
868d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis  void disableFileContentsOverride(const FileEntry *File);
869d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis
87006a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
87106a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  // FileID manipulation methods.
87206a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
8731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
87412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Return the buffer for the specified FileID.
87512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
87612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// If there is an error opening this buffer the first time, this
87712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// manufactures a temporary buffer and returns a non-empty error string.
878e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner  const llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc,
879e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner                                      bool *Invalid = 0) const {
880e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    bool MyInvalid = false;
881e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
882e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    if (MyInvalid || !Entry.isFile()) {
883e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor      if (Invalid)
884e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor        *Invalid = true;
8855330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
886e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor      return getFakeBufferForRecovery();
887e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    }
8885330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
8895330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher    return Entry.getFile().getContentCache()->getBuffer(Diag, *this, Loc,
890e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor                                                        Invalid);
89106a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  }
8921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
893e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner  const llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = 0) const {
894e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    bool MyInvalid = false;
895e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
896e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    if (MyInvalid || !Entry.isFile()) {
897e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor      if (Invalid)
898e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor        *Invalid = true;
8995330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
900e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor      return getFakeBufferForRecovery();
901e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    }
902e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor
9035330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher    return Entry.getFile().getContentCache()->getBuffer(Diag, *this,
9045330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher                                                        SourceLocation(),
905e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor                                                        Invalid);
906e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner  }
9075330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
90812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Returns the FileEntry record for the provided FileID.
90906a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  const FileEntry *getFileEntryForID(FileID FID) const {
910e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    bool MyInvalid = false;
911e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
912e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    if (MyInvalid || !Entry.isFile())
913e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor      return 0;
9145330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
91539afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis    const SrcMgr::ContentCache *Content = Entry.getFile().getContentCache();
91639afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis    if (!Content)
91739afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis      return 0;
91839afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis    return Content->OrigEntry;
91906a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  }
9201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
92112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Returns the FileEntry record for the provided SLocEntry.
9229d5a165d301cc9df68631e624322dd2a962f65b3Ted Kremenek  const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const
9239d5a165d301cc9df68631e624322dd2a962f65b3Ted Kremenek  {
92439afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis    const SrcMgr::ContentCache *Content = sloc.getFile().getContentCache();
92539afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis    if (!Content)
92639afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis      return 0;
92739afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis    return Content->OrigEntry;
9289d5a165d301cc9df68631e624322dd2a962f65b3Ted Kremenek  }
9299d5a165d301cc9df68631e624322dd2a962f65b3Ted Kremenek
93012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Return a StringRef to the source buffer data for the
931ceafc4b63599d14f0b5b10ff92e22bf242682dceBenjamin Kramer  /// specified FileID.
932ceafc4b63599d14f0b5b10ff92e22bf242682dceBenjamin Kramer  ///
933f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor  /// \param FID The file ID whose contents will be returned.
934f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor  /// \param Invalid If non-NULL, will be set true if an error occurred.
935686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  StringRef getBufferData(FileID FID, bool *Invalid = 0) const;
936f6ac97b101c8840efa92bf29166077ce4049e293Benjamin Kramer
937d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  /// \brief Get the number of FileIDs (files and macros) that were created
9382c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// during preprocessing of \p FID, including it.
939d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  unsigned getNumCreatedFIDsForFileID(FileID FID) const {
940d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    bool Invalid = false;
941d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
942d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    if (Invalid || !Entry.isFile())
943d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis      return 0;
944d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis
945d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    return Entry.getFile().NumCreatedFIDs;
946d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  }
947d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis
948d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  /// \brief Set the number of FileIDs (files and macros) that were created
9492c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// during preprocessing of \p FID, including it.
950d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  void setNumCreatedFIDsForFileID(FileID FID, unsigned NumFIDs) const {
951d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    bool Invalid = false;
952d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
953d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    if (Invalid || !Entry.isFile())
954d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis      return;
955d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis
956d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    assert(Entry.getFile().NumCreatedFIDs == 0 && "Already set!");
957d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    const_cast<SrcMgr::FileInfo &>(Entry.getFile()).NumCreatedFIDs = NumFIDs;
958d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  }
9591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
96006a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
96106a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  // SourceLocation manipulation methods.
96206a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
9631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
96412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Return the FileID for a SourceLocation.
96512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
96612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// This is a very hot method that is used for all SourceManager queries
96712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// that start with a SourceLocation object.  It is responsible for finding
96812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// the entry in SLocEntryTable which contains the specified location.
969de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  ///
970de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  FileID getFileID(SourceLocation SpellingLoc) const {
971de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned SLocOffset = SpellingLoc.getOffset();
9721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
973de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    // If our one-entry cache covers this offset, just return it.
974de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
975de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return LastFileIDLookup;
976de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
977de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return getFileIDSlow(SLocOffset);
978de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
9791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
980aa48fe80a1b2000809900a437f0819d929793002Jordan Rose  /// \brief Return the filename of the file containing a SourceLocation.
981aa48fe80a1b2000809900a437f0819d929793002Jordan Rose  StringRef getFilename(SourceLocation SpellingLoc) const {
982aa48fe80a1b2000809900a437f0819d929793002Jordan Rose    if (const FileEntry *F = getFileEntryForID(getFileID(SpellingLoc)))
983aa48fe80a1b2000809900a437f0819d929793002Jordan Rose      return F->getName();
984aa48fe80a1b2000809900a437f0819d929793002Jordan Rose    return StringRef();
985aa48fe80a1b2000809900a437f0819d929793002Jordan Rose  }
986aa48fe80a1b2000809900a437f0819d929793002Jordan Rose
98712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Return the source location corresponding to the first byte of
98812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// the specified file.
9892b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  SourceLocation getLocForStartOfFile(FileID FID) const {
990e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    bool Invalid = false;
991e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
992e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    if (Invalid || !Entry.isFile())
993e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor      return SourceLocation();
9945330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
995e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    unsigned FileOffset = Entry.getOffset();
996de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return SourceLocation::getFileLoc(FileOffset);
9972b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  }
998f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis
999f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis  /// \brief Return the source location corresponding to the last byte of the
1000f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis  /// specified file.
1001f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis  SourceLocation getLocForEndOfFile(FileID FID) const {
1002f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis    bool Invalid = false;
1003f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
1004f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis    if (Invalid || !Entry.isFile())
1005f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis      return SourceLocation();
1006f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis
1007f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis    unsigned FileOffset = Entry.getOffset();
1008b18840ddd6aa472bbf78ab9de59159eb128fb6a9Argyrios Kyrtzidis    return SourceLocation::getFileLoc(FileOffset + getFileIDSize(FID));
1009f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis  }
10101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1011809d1be9820039b4cf6efa48246a0d70ffa13394James Dennett  /// \brief Returns the include location if \p FID is a \#include'd file
1012d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  /// otherwise it returns an invalid location.
1013d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  SourceLocation getIncludeLoc(FileID FID) const {
1014d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    bool Invalid = false;
1015d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
1016d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    if (Invalid || !Entry.isFile())
1017d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis      return SourceLocation();
10185330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1019d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    return Entry.getFile().getIncludeLoc();
1020d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  }
1021d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis
10226c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor  // \brief Returns the import location if the given source location is
10236c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor  // located within a module, or an invalid location if the source location
10246c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor  // is within the current translation unit.
10256c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor  std::pair<SourceLocation, StringRef>
10266c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor  getModuleImportLoc(SourceLocation Loc) const {
10276c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor    FileID FID = getFileID(Loc);
10286c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor
10296c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor    // Positive file IDs are in the current translation unit, and -1 is a
10306c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor    // placeholder.
10316c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor    if (FID.ID >= -1)
10326c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor      return std::make_pair(SourceLocation(), "");
10336c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor
10346c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor    return ExternalSLocEntries->getModuleImportLoc(FID.ID);
10356c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor  }
10366c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor
103712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Given a SourceLocation object \p Loc, return the expansion
1038402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth  /// location referenced by the ID.
1039402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth  SourceLocation getExpansionLoc(SourceLocation Loc) const {
1040addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner    // Handle the non-mapped case inline, defer to out of line code to handle
1041402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth    // expansions.
1042de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (Loc.isFileID()) return Loc;
1043f84ef95ecec34f27fd05eb4e0392ca6bd3bd0be0Chandler Carruth    return getExpansionLocSlowCase(Loc);
1044de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
10451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10462c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Given \p Loc, if it is a macro location return the expansion
1047796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis  /// location or the spelling location, depending on if it comes from a
1048796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis  /// macro argument or not.
1049796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis  SourceLocation getFileLoc(SourceLocation Loc) const {
1050796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis    if (Loc.isFileID()) return Loc;
1051796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis    return getFileLocSlowCase(Loc);
1052796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis  }
1053796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis
105412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Return the start/end of the expansion information for an
105512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// expansion location.
105612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
105712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \pre \p Loc is required to be an expansion location.
1058e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  std::pair<SourceLocation,SourceLocation>
1059999f739404edf2078cf9f9c28b4dc45c19765842Chandler Carruth  getImmediateExpansionRange(SourceLocation Loc) const;
10601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
106112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Given a SourceLocation object, return the range of
1062edc3dccece244a584f8ebdb81da6c962c08e79beChandler Carruth  /// tokens covered by the expansion the ultimate file.
10636678133b8ce642f93e5141f056fa643112041ad0Chris Lattner  std::pair<SourceLocation,SourceLocation>
1064edc3dccece244a584f8ebdb81da6c962c08e79beChandler Carruth  getExpansionRange(SourceLocation Loc) const;
10651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
106712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Given a SourceLocation object, return the spelling
106812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// location referenced by the ID.
106912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
107012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// This is the place where the characters that make up the lexed token
107112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// can be found.
1072de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  SourceLocation getSpellingLoc(SourceLocation Loc) const {
1073addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner    // Handle the non-mapped case inline, defer to out of line code to handle
10743201f382956ed9beee9fb31229c2835c1208889cChandler Carruth    // expansions.
1075de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (Loc.isFileID()) return Loc;
1076addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner    return getSpellingLocSlowCase(Loc);
1077de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
10781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
107912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Given a SourceLocation object, return the spelling location
108012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// referenced by the ID.
108112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
108212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// This is the first level down towards the place where the characters
108312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// that make up the lexed token can be found.  This should not generally
108412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// be used by clients.
10851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const;
1086de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
108712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Decompose the specified location into a raw FileID + Offset pair.
108812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
108912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// The first element is the FileID, the second is the offset from the
109012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// start of the buffer of the location.
1091de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const {
1092de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    FileID FID = getFileID(Loc);
1093a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    bool Invalid = false;
1094a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    const SrcMgr::SLocEntry &E = getSLocEntry(FID, &Invalid);
1095a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    if (Invalid)
1096a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis      return std::make_pair(FileID(), 0);
1097a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    return std::make_pair(FID, Loc.getOffset()-E.getOffset());
1098de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
10991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
110012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Decompose the specified location into a raw FileID + Offset pair.
110112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
110212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// If the location is an expansion record, walk through it until we find
110312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// the final location expanded.
1104de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned>
1105e7b2b6e87dbe5b1207f77b6ff9c210a02f95bb39Chandler Carruth  getDecomposedExpansionLoc(SourceLocation Loc) const {
1106de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    FileID FID = getFileID(Loc);
1107a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    bool Invalid = false;
1108a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    const SrcMgr::SLocEntry *E = &getSLocEntry(FID, &Invalid);
1109a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    if (Invalid)
1110a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis      return std::make_pair(FileID(), 0);
11111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1112de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned Offset = Loc.getOffset()-E->getOffset();
1113de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (Loc.isFileID())
1114de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return std::make_pair(FID, Offset);
11151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1116e7b2b6e87dbe5b1207f77b6ff9c210a02f95bb39Chandler Carruth    return getDecomposedExpansionLocSlowCase(E);
1117de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
1118de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
111912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Decompose the specified location into a raw FileID + Offset pair.
112012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
112112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// If the location is an expansion record, walk through it until we find
112212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// its spelling record.
1123de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned>
1124de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  getDecomposedSpellingLoc(SourceLocation Loc) const {
1125de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    FileID FID = getFileID(Loc);
1126a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    bool Invalid = false;
1127a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    const SrcMgr::SLocEntry *E = &getSLocEntry(FID, &Invalid);
1128a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    if (Invalid)
1129a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis      return std::make_pair(FileID(), 0);
11301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1131de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned Offset = Loc.getOffset()-E->getOffset();
1132de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (Loc.isFileID())
1133de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return std::make_pair(FID, Offset);
1134de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return getDecomposedSpellingLocSlowCase(E, Offset);
11351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
11361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1137ecc65238c98ba21d08763da7b7972d617677e908Argyrios Kyrtzidis  /// \brief Returns the "included/expanded in" decomposed location of the given
1138ecc65238c98ba21d08763da7b7972d617677e908Argyrios Kyrtzidis  /// FileID.
1139ecc65238c98ba21d08763da7b7972d617677e908Argyrios Kyrtzidis  std::pair<FileID, unsigned> getDecomposedIncludedLoc(FileID FID) const;
1140ecc65238c98ba21d08763da7b7972d617677e908Argyrios Kyrtzidis
114112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Returns the offset from the start of the file that the
114212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// specified SourceLocation represents.
114312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
114412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// This is not very meaningful for a macro ID.
114552c29081281955d3db9e11d10573b2d38f709099Chris Lattner  unsigned getFileOffset(SourceLocation SpellingLoc) const {
1146de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return getDecomposedLoc(SpellingLoc).second;
11475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
11481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
114912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Tests whether the given source location represents a macro
115012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// argument's expansion into the function-like macro definition.
115112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
115212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// Such source locations only appear inside of the expansion
115396d3589e523a04f4ff2058a7919226ce60696ae8Chandler Carruth  /// locations representing where a particular function-like macro was
115496d3589e523a04f4ff2058a7919226ce60696ae8Chandler Carruth  /// expanded.
115596d3589e523a04f4ff2058a7919226ce60696ae8Chandler Carruth  bool isMacroArgExpansion(SourceLocation Loc) const;
11561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1157c3cd6f7a5d33ad44f6c9cf4faa7046c77baa128eMatt Beaumont-Gay  /// \brief Tests whether the given source location represents the expansion of
1158c3cd6f7a5d33ad44f6c9cf4faa7046c77baa128eMatt Beaumont-Gay  /// a macro body.
1159c3cd6f7a5d33ad44f6c9cf4faa7046c77baa128eMatt Beaumont-Gay  ///
1160c3cd6f7a5d33ad44f6c9cf4faa7046c77baa128eMatt Beaumont-Gay  /// This is equivalent to testing whether the location is part of a macro
1161c3cd6f7a5d33ad44f6c9cf4faa7046c77baa128eMatt Beaumont-Gay  /// expansion but not the expansion of an argument to a function-like macro.
1162c3cd6f7a5d33ad44f6c9cf4faa7046c77baa128eMatt Beaumont-Gay  bool isMacroBodyExpansion(SourceLocation Loc) const;
1163c3cd6f7a5d33ad44f6c9cf4faa7046c77baa128eMatt Beaumont-Gay
1164c50c6ff49aa3648ae031349de6f09439f52425f0Argyrios Kyrtzidis  /// \brief Returns true if the given MacroID location points at the beginning
1165c50c6ff49aa3648ae031349de6f09439f52425f0Argyrios Kyrtzidis  /// of the immediate macro expansion.
1166c50c6ff49aa3648ae031349de6f09439f52425f0Argyrios Kyrtzidis  ///
1167c50c6ff49aa3648ae031349de6f09439f52425f0Argyrios Kyrtzidis  /// \param MacroBegin If non-null and function returns true, it is set to the
1168c50c6ff49aa3648ae031349de6f09439f52425f0Argyrios Kyrtzidis  /// begin location of the immediate macro expansion.
1169c50c6ff49aa3648ae031349de6f09439f52425f0Argyrios Kyrtzidis  bool isAtStartOfImmediateMacroExpansion(SourceLocation Loc,
1170c50c6ff49aa3648ae031349de6f09439f52425f0Argyrios Kyrtzidis                                          SourceLocation *MacroBegin = 0) const;
1171c50c6ff49aa3648ae031349de6f09439f52425f0Argyrios Kyrtzidis
1172c50c6ff49aa3648ae031349de6f09439f52425f0Argyrios Kyrtzidis  /// \brief Returns true if the given MacroID location points at the character
1173c50c6ff49aa3648ae031349de6f09439f52425f0Argyrios Kyrtzidis  /// end of the immediate macro expansion.
1174c50c6ff49aa3648ae031349de6f09439f52425f0Argyrios Kyrtzidis  ///
1175c50c6ff49aa3648ae031349de6f09439f52425f0Argyrios Kyrtzidis  /// \param MacroEnd If non-null and function returns true, it is set to the
1176c50c6ff49aa3648ae031349de6f09439f52425f0Argyrios Kyrtzidis  /// character end location of the immediate macro expansion.
1177c50c6ff49aa3648ae031349de6f09439f52425f0Argyrios Kyrtzidis  bool isAtEndOfImmediateMacroExpansion(SourceLocation Loc,
1178c50c6ff49aa3648ae031349de6f09439f52425f0Argyrios Kyrtzidis                                        SourceLocation *MacroEnd = 0) const;
1179c50c6ff49aa3648ae031349de6f09439f52425f0Argyrios Kyrtzidis
11802c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Returns true if \p Loc is inside the [\p Start, +\p Length)
1181499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis  /// chunk of the source location address space.
118212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
11832c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// If it's true and \p RelativeOffset is non-null, it will be set to the
11842c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// relative offset of \p Loc inside the chunk.
1185499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis  bool isInSLocAddrSpace(SourceLocation Loc,
1186499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis                         SourceLocation Start, unsigned Length,
1187499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis                         unsigned *RelativeOffset = 0) const {
1188499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis    assert(((Start.getOffset() < NextLocalOffset &&
1189499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis               Start.getOffset()+Length <= NextLocalOffset) ||
1190499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis            (Start.getOffset() >= CurrentLoadedOffset &&
1191499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis                Start.getOffset()+Length < MaxLoadedOffset)) &&
1192499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis           "Chunk is not valid SLoc address space");
1193499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis    unsigned LocOffs = Loc.getOffset();
1194499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis    unsigned BeginOffs = Start.getOffset();
1195499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis    unsigned EndOffs = BeginOffs + Length;
1196499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis    if (LocOffs >= BeginOffs && LocOffs < EndOffs) {
1197499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis      if (RelativeOffset)
1198499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis        *RelativeOffset = LocOffs - BeginOffs;
1199499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis      return true;
1200499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis    }
1201499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis
1202499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis    return false;
1203499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis  }
1204499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis
12052c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Return true if both \p LHS and \p RHS are in the local source
120612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// location address space or the loaded one.
120712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
120812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// If it's true and \p RelativeOffset is non-null, it will be set to the
120912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// offset of \p RHS relative to \p LHS.
1210b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis  bool isInSameSLocAddrSpace(SourceLocation LHS, SourceLocation RHS,
1211b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis                             int *RelativeOffset) const {
1212b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis    unsigned LHSOffs = LHS.getOffset(), RHSOffs = RHS.getOffset();
1213b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis    bool LHSLoaded = LHSOffs >= CurrentLoadedOffset;
1214b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis    bool RHSLoaded = RHSOffs >= CurrentLoadedOffset;
1215b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis
1216b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis    if (LHSLoaded == RHSLoaded) {
1217b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis      if (RelativeOffset)
1218b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis        *RelativeOffset = RHSOffs - LHSOffs;
1219b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis      return true;
1220b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis    }
1221b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis
1222b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis    return false;
1223b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis  }
1224b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis
1225de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  //===--------------------------------------------------------------------===//
1226de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  // Queries about the code at a SourceLocation.
1227de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  //===--------------------------------------------------------------------===//
12281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
122912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Return a pointer to the start of the specified location
1230de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// in the appropriate spelling MemoryBuffer.
123150f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  ///
123250f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  /// \param Invalid If non-NULL, will be set \c true if an error occurs.
123350f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  const char *getCharacterData(SourceLocation SL, bool *Invalid = 0) const;
12341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
123512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Return the column # for the specified file position.
123612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
12379dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  /// This is significantly cheaper to compute than the line number.  This
12383201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// returns zero if the column number isn't known.  This may only be called
12393201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// on a file sloc, so you must choose a spelling or expansion location
1240f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner  /// before calling this method.
12415330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher  unsigned getColumnNumber(FileID FID, unsigned FilePos,
124250f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor                           bool *Invalid = 0) const;
12435ef04ee40c3332d31b6d1439f50d0ddb45812929Chandler Carruth  unsigned getSpellingColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
1244a77c031cb66f75d22672070052cc6e0205289ff8Chandler Carruth  unsigned getExpansionColumnNumber(SourceLocation Loc,
1245b49dcd249c7f4f034493741f95394987a4ccf244Chandler Carruth                                    bool *Invalid = 0) const;
12465ef04ee40c3332d31b6d1439f50d0ddb45812929Chandler Carruth  unsigned getPresumedColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
12471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
124912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Given a SourceLocation, return the spelling line number
125012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// for the position indicated.
125112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
125212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// This requires building and caching a table of line offsets for the
125312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// MemoryBuffer, so this is not cheap: use only when about to emit a
125412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// diagnostic.
125550f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = 0) const;
12565ef04ee40c3332d31b6d1439f50d0ddb45812929Chandler Carruth  unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
1257642116259e8df6286063a17361c20e95b5017a0aChandler Carruth  unsigned getExpansionLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
12585ef04ee40c3332d31b6d1439f50d0ddb45812929Chandler Carruth  unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
12591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1260c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  /// \brief Return the filename or buffer identifier of the buffer the
1261c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  /// location is in.
1262c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  ///
1263c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  /// Note that this name does not respect \#line directives.  Use
1264c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  /// getPresumedLoc for normal clients.
126550f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  const char *getBufferName(SourceLocation Loc, bool *Invalid = 0) const;
12661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
126712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Return the file characteristic of the specified source
126812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// location, indicating whether this is a normal file, a system
12696b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// header, or an "implicit extern C" system header.
12706b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  ///
12716b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// This state can be modified with flags on GNU linemarker directives like:
1272c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  /// \code
12736b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  ///   # 4 "foo.h" 3
1274c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  /// \endcode
12756b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// which changes all source locations in the current file after that to be
12766b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// considered to be from a system header.
12776b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const;
12781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
127912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Returns the "presumed" location of a SourceLocation specifies.
128012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
128112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// A "presumed location" can be modified by \#line or GNU line marker
128212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// directives.  This provides a view on the data that a user should see
128312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// in diagnostics, for example.
1284b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  ///
12853201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// Note that a presumed location is always given as the expansion point of
12863201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// an expansion location, not at the spelling location.
1287cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  ///
1288cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  /// \returns The presumed location of the specified SourceLocation. If the
1289cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  /// presumed location cannot be calculate (e.g., because \p Loc is invalid
1290cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  /// or the file containing \p Loc has changed on disk), returns an invalid
1291cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  /// presumed location.
129262221b17c90457df9ca0ff20bb54d634e8951defRichard Smith  PresumedLoc getPresumedLoc(SourceLocation Loc,
129362221b17c90457df9ca0ff20bb54d634e8951defRichard Smith                             bool UseLineDirectives = true) const;
12941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
129512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Returns true if both SourceLocations correspond to the same file.
12969fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
1297a11d61793341fea195c29a0dab3fbd74f2b39a8cChris Lattner    return getFileID(Loc1) == getFileID(Loc2);
12989fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  }
12991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
130012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Returns true if the file of provided SourceLocation is the main
130112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// file.
13029fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  bool isFromMainFile(SourceLocation Loc) const {
1303a11d61793341fea195c29a0dab3fbd74f2b39a8cChris Lattner    return getFileID(Loc) == getMainFileID();
13041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
13051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
130612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Returns if a SourceLocation is in a system header.
13077bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber  bool isInSystemHeader(SourceLocation Loc) const {
13080b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner    return getFileCharacteristic(Loc) != SrcMgr::C_User;
1309721818304ac462d8c6ce05eecd02884033db78f1Chris Lattner  }
13101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
131112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Returns if a SourceLocation is in an "extern C" system header.
13120d456588acac0713a7c33063922d35a8cc8c658eChris Lattner  bool isInExternCSystemHeader(SourceLocation Loc) const {
13130d456588acac0713a7c33063922d35a8cc8c658eChris Lattner    return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem;
13140d456588acac0713a7c33063922d35a8cc8c658eChris Lattner  }
13151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1316d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay  /// \brief Returns whether \p Loc is expanded from a macro in a system header.
1317d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay  bool isInSystemMacro(SourceLocation loc) {
1318d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay    return loc.isMacroID() && isInSystemHeader(getSpellingLoc(loc));
1319d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay  }
1320d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay
13212c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief The size of the SLocEnty that \p FID represents.
1322984e42ca1ff7775ce39372c314f1cb7d6862c4c7Argyrios Kyrtzidis  unsigned getFileIDSize(FileID FID) const;
132354232ade44d31e98ea83f43ca066128e315dcbdaArgyrios Kyrtzidis
13242c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Given a specific FileID, returns true if \p Loc is inside that
13252c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// FileID chunk and sets relative offset (offset of \p Loc from beginning
13262c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// of FileID) to \p relativeOffset.
1327d60a34a4e514ec0dfddd05ef2744be104e111f45Argyrios Kyrtzidis  bool isInFileID(SourceLocation Loc, FileID FID,
1328d60a34a4e514ec0dfddd05ef2744be104e111f45Argyrios Kyrtzidis                  unsigned *RelativeOffset = 0) const {
1329d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis    unsigned Offs = Loc.getOffset();
1330d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis    if (isOffsetInFileID(FID, Offs)) {
1331d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis      if (RelativeOffset)
1332d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis        *RelativeOffset = Offs - getSLocEntry(FID).getOffset();
1333d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis      return true;
1334d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis    }
1335d60a34a4e514ec0dfddd05ef2744be104e111f45Argyrios Kyrtzidis
1336d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis    return false;
1337d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis  }
1338469244a322dd5d35cee1d02d70a2edbc12ac5ce7Argyrios Kyrtzidis
133906a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
13405b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  // Line Table Manipulation Routines
13415b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  //===--------------------------------------------------------------------===//
13421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
134312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Return the uniqued ID for the specified filename.
13441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ///
1345686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  unsigned getLineTableFilenameID(StringRef Str);
13461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
134712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Add a line note to the line table for the FileID and offset
134812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// specified by Loc.
134912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
135012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// If FilenameID is -1, it is considered to be unspecified.
13514c4ea17d7f991516c37a871dfa4bbe5723fa85f0Chris Lattner  void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID);
13529d79ebac47ffde6a1cb312f4c09b66b1b9a397fbChris Lattner  void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID,
13531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                   bool IsFileEntry, bool IsFileExit,
13549d79ebac47ffde6a1cb312f4c09b66b1b9a397fbChris Lattner                   bool IsSystemHeader, bool IsExternCHeader);
1355bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
1356bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  /// \brief Determine if the source manager has a line table.
1357bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  bool hasLineTable() const { return LineTable != 0; }
1358bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
1359bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  /// \brief Retrieve the stored line table.
1360bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  LineTableInfo &getLineTable();
1361bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
13625b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  //===--------------------------------------------------------------------===//
1363457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek  // Queries for performance analysis.
1364457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek  //===--------------------------------------------------------------------===//
1365457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek
136612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Return the total amount of physical memory allocated by the
1367457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek  /// ContentCache allocator.
1368457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek  size_t getContentCacheSize() const {
1369457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek    return ContentCacheAlloc.getTotalMemory();
1370457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek  }
13715330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1372f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek  struct MemoryBufferSizes {
1373f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek    const size_t malloc_bytes;
1374f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek    const size_t mmap_bytes;
13755330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1376f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek    MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)
1377f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek      : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {}
1378f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek  };
1379f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek
138012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Return the amount of memory used by memory buffers, breaking down
1381f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek  /// by heap-backed versus mmap'ed memory.
1382f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek  MemoryBufferSizes getMemoryBufferSizes() const;
13835330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
138412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Return the amount of memory used for various side tables and
138512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// data structures in the SourceManager.
1386ca7dc2b755eb81ac95121ce1a1f1aa44a4a0fe12Ted Kremenek  size_t getDataStructureSizes() const;
1387457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek
1388457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek  //===--------------------------------------------------------------------===//
138906a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  // Other miscellaneous methods.
139006a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
139110b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis
139210b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  /// \brief Get the source location for the given file:line:col triplet.
139310b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  ///
139410b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  /// If the source file is included multiple times, the source location will
139510b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  /// be based upon the first inclusion.
1396ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  SourceLocation translateFileLineCol(const FileEntry *SourceFile,
1397507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis                                      unsigned Line, unsigned Col) const;
1398ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis
1399b201e16e0c331b0bdeae7b30f9f79aae32beb1b2Argyrios Kyrtzidis  /// \brief Get the FileID for the given file.
1400b201e16e0c331b0bdeae7b30f9f79aae32beb1b2Argyrios Kyrtzidis  ///
1401b201e16e0c331b0bdeae7b30f9f79aae32beb1b2Argyrios Kyrtzidis  /// If the source file is included multiple times, the FileID will be the
1402b201e16e0c331b0bdeae7b30f9f79aae32beb1b2Argyrios Kyrtzidis  /// first inclusion.
1403b201e16e0c331b0bdeae7b30f9f79aae32beb1b2Argyrios Kyrtzidis  FileID translateFile(const FileEntry *SourceFile) const;
1404b201e16e0c331b0bdeae7b30f9f79aae32beb1b2Argyrios Kyrtzidis
14052c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Get the source location in \p FID for the given line:col.
14062c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// Returns null location if \p FID is not a file SLocEntry.
1407507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  SourceLocation translateLineCol(FileID FID,
1408507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis                                  unsigned Line, unsigned Col) const;
1409efa2ff8603dae51f5f5ed7509a503f477498ad22Argyrios Kyrtzidis
14102c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief If \p Loc points inside a function macro argument, the returned
1411ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  /// location will be the macro location in which the argument was expanded.
1412ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  /// If a macro argument is used multiple times, the expanded location will
1413ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  /// be at the first expansion of the argument.
1414ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  /// e.g.
1415ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  ///   MY_MACRO(foo);
1416ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  ///             ^
1417ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  /// Passing a file location pointing at 'foo', will yield a macro location
1418ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  /// where 'foo' was expanded into.
1419507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  SourceLocation getMacroArgExpandedLocation(SourceLocation Loc) const;
14201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14212aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis  /// \brief Determines the order of 2 source locations in the translation unit.
14222aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis  ///
14232aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis  /// \returns true if LHS source location comes before RHS, false otherwise.
14242aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis  bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const;
14252aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis
1426b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis  /// \brief Determines the order of 2 source locations in the "source location
1427b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis  /// address space".
14285d579e7b0463a2e65328df85e4fed8e07799fd9eArgyrios Kyrtzidis  bool isBeforeInSLocAddrSpace(SourceLocation LHS, SourceLocation RHS) const {
14295d579e7b0463a2e65328df85e4fed8e07799fd9eArgyrios Kyrtzidis    return isBeforeInSLocAddrSpace(LHS, RHS.getOffset());
1430b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis  }
1431b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis
1432b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis  /// \brief Determines the order of a source location and a source location
1433b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis  /// offset in the "source location address space".
1434f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///
14355330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher  /// Note that we always consider source locations loaded from
14365d579e7b0463a2e65328df85e4fed8e07799fd9eArgyrios Kyrtzidis  bool isBeforeInSLocAddrSpace(SourceLocation LHS, unsigned RHS) const {
1437f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    unsigned LHSOffset = LHS.getOffset();
1438f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    bool LHSLoaded = LHSOffset >= CurrentLoadedOffset;
1439f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    bool RHSLoaded = RHS >= CurrentLoadedOffset;
1440f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    if (LHSLoaded == RHSLoaded)
14415d579e7b0463a2e65328df85e4fed8e07799fd9eArgyrios Kyrtzidis      return LHSOffset < RHS;
14425330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1443f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    return LHSLoaded;
1444b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis  }
1445b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis
1446c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner  // Iterators over FileInfos.
14470d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>
14480d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner      ::const_iterator fileinfo_iterator;
1449c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner  fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); }
1450c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner  fileinfo_iterator fileinfo_end() const { return FileInfos.end(); }
1451d93256e55673a17d18543397ec462416acb13792Douglas Gregor  bool hasFileInfo(const FileEntry *File) const {
1452d93256e55673a17d18543397ec462416acb13792Douglas Gregor    return FileInfos.find(File) != FileInfos.end();
1453d93256e55673a17d18543397ec462416acb13792Douglas Gregor  }
1454c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner
145512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Print statistics to stderr.
14565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
14575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void PrintStats() const;
145878d85f53b093867bbb0123f016956178eea7343eTed Kremenek
1459f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief Get the number of local SLocEntries we have.
1460f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  unsigned local_sloc_entry_size() const { return LocalSLocEntryTable.size(); }
14615330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1462f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief Get a local SLocEntry. This is exposed for indexing.
14635330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher  const SrcMgr::SLocEntry &getLocalSLocEntry(unsigned Index,
1464f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor                                             bool *Invalid = 0) const {
1465f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    assert(Index < LocalSLocEntryTable.size() && "Invalid index");
1466f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    return LocalSLocEntryTable[Index];
1467bdfe48ac80573e026595af91e541474dbf02565fDouglas Gregor  }
14685330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1469f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief Get the number of loaded SLocEntries we have.
1470f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  unsigned loaded_sloc_entry_size() const { return LoadedSLocEntryTable.size();}
14715330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1472f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief Get a loaded SLocEntry. This is exposed for indexing.
147370042f5d1ca378138f90b7b9384023701f5d03d8David Blaikie  const SrcMgr::SLocEntry &getLoadedSLocEntry(unsigned Index,
147470042f5d1ca378138f90b7b9384023701f5d03d8David Blaikie                                              bool *Invalid = 0) const {
1475f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    assert(Index < LoadedSLocEntryTable.size() && "Invalid index");
1476a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis    if (SLocEntryLoaded[Index])
1477a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis      return LoadedSLocEntryTable[Index];
1478a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis    return loadSLocEntry(Index, Invalid);
1479f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  }
14805330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1481e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor  const SrcMgr::SLocEntry &getSLocEntry(FileID FID, bool *Invalid = 0) const {
1482c705d2520a51de1dc38d36efada8e9bc2d8b0d1fArgyrios Kyrtzidis    if (FID.ID == 0 || FID.ID == -1) {
1483c705d2520a51de1dc38d36efada8e9bc2d8b0d1fArgyrios Kyrtzidis      if (Invalid) *Invalid = true;
1484c705d2520a51de1dc38d36efada8e9bc2d8b0d1fArgyrios Kyrtzidis      return LocalSLocEntryTable[0];
1485c705d2520a51de1dc38d36efada8e9bc2d8b0d1fArgyrios Kyrtzidis    }
14864ff3225c1f343c3e0d461a0e387c7c6012cff5baArgyrios Kyrtzidis    return getSLocEntryByID(FID.ID, Invalid);
1487bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  }
1488bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
1489f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  unsigned getNextLocalOffset() const { return NextLocalOffset; }
14905330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1491f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  void setExternalSLocEntrySource(ExternalSLocEntrySource *Source) {
1492f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    assert(LoadedSLocEntryTable.empty() &&
1493f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor           "Invalidating existing loaded entries");
1494f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    ExternalSLocEntries = Source;
1495f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  }
14965330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1497f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief Allocate a number of loaded SLocEntries, which will be actually
1498f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// loaded on demand from the external source.
1499f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///
1500f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// NumSLocEntries will be allocated, which occupy a total of TotalSize space
1501f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// in the global source view. The lowest ID and the base offset of the
1502f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// entries will be returned.
1503f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  std::pair<int, unsigned>
1504f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  AllocateLoadedSLocEntries(unsigned NumSLocEntries, unsigned TotalSize);
15055330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
15062c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Returns true if \p Loc came from a PCH/Module.
1507aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis  bool isLoadedSourceLocation(SourceLocation Loc) const {
1508aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis    return Loc.getOffset() >= CurrentLoadedOffset;
1509aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis  }
1510aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis
15112c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Returns true if \p Loc did not come from a PCH/Module.
1512aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis  bool isLocalSourceLocation(SourceLocation Loc) const {
1513aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis    return Loc.getOffset() < NextLocalOffset;
1514aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis  }
1515aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis
15162c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Returns true if \p FID came from a PCH/Module.
1517718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis  bool isLoadedFileID(FileID FID) const {
1518718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis    assert(FID.ID != -1 && "Using FileID sentinel value");
1519718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis    return FID.ID < 0;
1520718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis  }
1521718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis
15222c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Returns true if \p FID did not come from a PCH/Module.
1523718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis  bool isLocalFileID(FileID FID) const {
1524718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis    return !isLoadedFileID(FID);
1525718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis  }
1526718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis
152729271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay  /// Gets the location of the immediate macro caller, one level up the stack
152829271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay  /// toward the initial macro typed into the source.
152929271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay  SourceLocation getImmediateMacroCallerLoc(SourceLocation Loc) const {
153029271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay    if (!Loc.isMacroID()) return Loc;
153129271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay
153229271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay    // When we have the location of (part of) an expanded parameter, its
1533678839297204002df215e0be12bcd10b20a9a4a4Richard Smith    // spelling location points to the argument as expanded in the macro call,
153429271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay    // and therefore is used to locate the macro caller.
153529271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay    if (isMacroArgExpansion(Loc))
153629271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay      return getImmediateSpellingLoc(Loc);
153729271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay
153829271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay    // Otherwise, the caller of the macro is located where this macro is
153929271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay    // expanded (while the spelling is part of the macro definition).
154029271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay    return getImmediateExpansionRange(Loc).first;
154129271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay  }
154229271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay
15435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerprivate:
1544e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor  const llvm::MemoryBuffer *getFakeBufferForRecovery() const;
1545a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis  const SrcMgr::ContentCache *getFakeContentCacheForRecovery() const;
1546a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis
1547a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis  const SrcMgr::SLocEntry &loadSLocEntry(unsigned Index, bool *Invalid) const;
1548e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor
1549f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief Get the entry with the given unwrapped FileID.
15504ff3225c1f343c3e0d461a0e387c7c6012cff5baArgyrios Kyrtzidis  const SrcMgr::SLocEntry &getSLocEntryByID(int ID, bool *Invalid = 0) const {
1551f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    assert(ID != -1 && "Using FileID sentinel value");
1552f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    if (ID < 0)
15534ff3225c1f343c3e0d461a0e387c7c6012cff5baArgyrios Kyrtzidis      return getLoadedSLocEntryByID(ID, Invalid);
15544ff3225c1f343c3e0d461a0e387c7c6012cff5baArgyrios Kyrtzidis    return getLocalSLocEntry(static_cast<unsigned>(ID), Invalid);
1555f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  }
15565330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1557a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis  const SrcMgr::SLocEntry &getLoadedSLocEntryByID(int ID,
1558a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis                                                  bool *Invalid = 0) const {
1559a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis    return getLoadedSLocEntry(static_cast<unsigned>(-ID - 2), Invalid);
1560f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  }
15615330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1562af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// Implements the common elements of storing an expansion info struct into
1563af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// the SLocEntry table and producing a source location that refers to it.
156478df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth  SourceLocation createExpansionLocImpl(const SrcMgr::ExpansionInfo &Expansion,
1565bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                        unsigned TokLength,
1566bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                        int LoadedID = 0,
1567bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                        unsigned LoadedOffset = 0);
1568c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth
1569af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Return true if the specified FileID contains the
1570de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// specified SourceLocation offset.  This is a very hot method.
1571de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const {
1572de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID);
1573de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    // If the entry is after the offset, it can't contain it.
1574de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (SLocOffset < Entry.getOffset()) return false;
15751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1576f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    // If this is the very last entry then it does.
1577f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    if (FID.ID == -2)
1578f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor      return true;
1579f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
1580f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    // If it is the last local entry, then it does if the location is local.
1581b2efdf3e92f986956f0a755ba815f3870838583bBenjamin Kramer    if (FID.ID+1 == static_cast<int>(LocalSLocEntryTable.size()))
1582f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor      return SLocOffset < NextLocalOffset;
15837f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
1584f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    // Otherwise, the entry after it has to not include it. This works for both
1585f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    // local and loaded entries.
1586b2efdf3e92f986956f0a755ba815f3870838583bBenjamin Kramer    return SLocOffset < getSLocEntryByID(FID.ID+1).getOffset();
1587de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
15881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1589c50c6ff49aa3648ae031349de6f09439f52425f0Argyrios Kyrtzidis  /// \brief Returns the previous in-order FileID or an invalid FileID if there
1590c50c6ff49aa3648ae031349de6f09439f52425f0Argyrios Kyrtzidis  /// is no previous one.
1591c50c6ff49aa3648ae031349de6f09439f52425f0Argyrios Kyrtzidis  FileID getPreviousFileID(FileID FID) const;
1592c50c6ff49aa3648ae031349de6f09439f52425f0Argyrios Kyrtzidis
1593c50c6ff49aa3648ae031349de6f09439f52425f0Argyrios Kyrtzidis  /// \brief Returns the next in-order FileID or an invalid FileID if there is
1594c50c6ff49aa3648ae031349de6f09439f52425f0Argyrios Kyrtzidis  /// no next one.
1595c50c6ff49aa3648ae031349de6f09439f52425f0Argyrios Kyrtzidis  FileID getNextFileID(FileID FID) const;
1596c50c6ff49aa3648ae031349de6f09439f52425f0Argyrios Kyrtzidis
1597af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Create a new fileID for the specified ContentCache and
1598af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// include position.
1599af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
1600af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// This works regardless of whether the ContentCache corresponds to a
1601af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// file or some other input source.
16022b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID createFileID(const SrcMgr::ContentCache* File,
16032b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner                      SourceLocation IncludePos,
16047f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                      SrcMgr::CharacteristicKind DirCharacter,
1605f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor                      int LoadedID, unsigned LoadedOffset);
16061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1607de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  const SrcMgr::ContentCache *
1608ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis    getOrCreateContentCache(const FileEntry *SourceFile,
1609ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis                            bool isSystemFile = false);
1610c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek
1611af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Create a new ContentCache for the specified  memory buffer.
16121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const SrcMgr::ContentCache*
16132b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  createMemBufferContentCache(const llvm::MemoryBuffer *Buf);
16141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1615de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  FileID getFileIDSlow(unsigned SLocOffset) const;
1616f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  FileID getFileIDLocal(unsigned SLocOffset) const;
1617f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  FileID getFileIDLoaded(unsigned SLocOffset) const;
1618de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
1619f84ef95ecec34f27fd05eb4e0392ca6bd3bd0be0Chandler Carruth  SourceLocation getExpansionLocSlowCase(SourceLocation Loc) const;
1620addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner  SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;
1621796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis  SourceLocation getFileLocSlowCase(SourceLocation Loc) const;
1622addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner
1623de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned>
1624e7b2b6e87dbe5b1207f77b6ff9c210a02f95bb39Chandler Carruth  getDecomposedExpansionLocSlowCase(const SrcMgr::SLocEntry *E) const;
1625de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned>
1626de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
1627de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner                                   unsigned Offset) const;
1628fb3612ef197cb8532c05f33889ec1aed7c26e5cbArgyrios Kyrtzidis  void computeMacroArgsCache(MacroArgsMap *&MacroArgsCache, FileID FID) const;
16290872a06d1ee1a3b62ef833f955051418d18006a1Argyrios Kyrtzidis  void associateFileChunkWithMacroArgExp(MacroArgsMap &MacroArgsCache,
16300872a06d1ee1a3b62ef833f955051418d18006a1Argyrios Kyrtzidis                                         FileID FID,
16310872a06d1ee1a3b62ef833f955051418d18006a1Argyrios Kyrtzidis                                         SourceLocation SpellLoc,
16320872a06d1ee1a3b62ef833f955051418d18006a1Argyrios Kyrtzidis                                         SourceLocation ExpansionLoc,
16330872a06d1ee1a3b62ef833f955051418d18006a1Argyrios Kyrtzidis                                         unsigned ExpansionLength) const;
1634ac1ffcc55b861737ba2466cd1ca1accd8eafceaaArgyrios Kyrtzidis  friend class ASTReader;
1635ac1ffcc55b861737ba2466cd1ca1accd8eafceaaArgyrios Kyrtzidis  friend class ASTWriter;
16365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
16375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1638aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko/// \brief Comparison function object.
1639aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenkotemplate<typename T>
1640aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenkoclass BeforeThanCompare;
1641aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
1642aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko/// \brief Compare two source locations.
1643aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenkotemplate<>
1644aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenkoclass BeforeThanCompare<SourceLocation> {
1645aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  SourceManager &SM;
1646aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
1647aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenkopublic:
1648aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  explicit BeforeThanCompare(SourceManager &SM) : SM(SM) { }
1649aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
1650aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  bool operator()(SourceLocation LHS, SourceLocation RHS) const {
1651aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    return SM.isBeforeInTranslationUnit(LHS, RHS);
1652aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  }
1653aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko};
1654aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
1655aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko/// \brief Compare two non-overlapping source ranges.
1656aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenkotemplate<>
1657aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenkoclass BeforeThanCompare<SourceRange> {
1658aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  SourceManager &SM;
1659aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
1660aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenkopublic:
1661aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  explicit BeforeThanCompare(SourceManager &SM) : SM(SM) { }
1662aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
1663aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  bool operator()(SourceRange LHS, SourceRange RHS) {
1664aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    return SM.isBeforeInTranslationUnit(LHS.getBegin(), RHS.getBegin());
1665aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  }
1666aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko};
16675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}  // end namespace clang
16695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1670b7551f71a4c5a901bbae72b0a80af3745b7a0c0dDouglas Gregor
16715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif
1672