SourceManager.h revision 2564f811ba107cb314a594d730aa3357b6181b62
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
6492564f811ba107cb314a594d730aa3357b6181b62Ted Kremenek  /// The key value into the IsBeforeInTUCache table.
6502564f811ba107cb314a594d730aa3357b6181b62Ted Kremenek  typedef std::pair<FileID, FileID> IsBeforeInTUCacheKey;
6512564f811ba107cb314a594d730aa3357b6181b62Ted Kremenek
6522564f811ba107cb314a594d730aa3357b6181b62Ted Kremenek  /// The IsBeforeInTranslationUnitCache is a mapping from FileID pairs
6532564f811ba107cb314a594d730aa3357b6181b62Ted Kremenek  /// to cache results.
6542564f811ba107cb314a594d730aa3357b6181b62Ted Kremenek  typedef llvm::DenseMap<IsBeforeInTUCacheKey, InBeforeInTUCacheEntry>
6552564f811ba107cb314a594d730aa3357b6181b62Ted Kremenek          InBeforeInTUCache;
6562564f811ba107cb314a594d730aa3357b6181b62Ted Kremenek
6572564f811ba107cb314a594d730aa3357b6181b62Ted Kremenek  /// Cache results for the isBeforeInTranslationUnit method.
6582564f811ba107cb314a594d730aa3357b6181b62Ted Kremenek  mutable InBeforeInTUCache IBTUCache;
6592564f811ba107cb314a594d730aa3357b6181b62Ted Kremenek  mutable InBeforeInTUCacheEntry IBTUCacheOverflow;
6602564f811ba107cb314a594d730aa3357b6181b62Ted Kremenek
6612564f811ba107cb314a594d730aa3357b6181b62Ted Kremenek  /// Return the cache entry for comparing the given file IDs
6622564f811ba107cb314a594d730aa3357b6181b62Ted Kremenek  /// for isBeforeInTranslationUnit.
6632564f811ba107cb314a594d730aa3357b6181b62Ted Kremenek  InBeforeInTUCacheEntry &getInBeforeInTUCache(FileID LFID, FileID RFID) const;
6641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
665e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor  // Cache for the "fake" buffer used for error-recovery purposes.
666e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor  mutable llvm::MemoryBuffer *FakeBufferForRecovery;
6675330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
668a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis  mutable SrcMgr::ContentCache *FakeContentCacheForRecovery;
669a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis
670fb3612ef197cb8532c05f33889ec1aed7c26e5cbArgyrios Kyrtzidis  /// \brief Lazily computed map of macro argument chunks to their expanded
671fb3612ef197cb8532c05f33889ec1aed7c26e5cbArgyrios Kyrtzidis  /// source location.
672fb3612ef197cb8532c05f33889ec1aed7c26e5cbArgyrios Kyrtzidis  typedef std::map<unsigned, SourceLocation> MacroArgsMap;
673fb3612ef197cb8532c05f33889ec1aed7c26e5cbArgyrios Kyrtzidis
67470042f5d1ca378138f90b7b9384023701f5d03d8David Blaikie  mutable llvm::DenseMap<FileID, MacroArgsMap *> MacroArgsCacheMap;
675fb3612ef197cb8532c05f33889ec1aed7c26e5cbArgyrios Kyrtzidis
6764565e487531c7bf6d348dbe9f5529784966fc7aeDouglas Gregor  /// \brief The stack of modules being built, which is used to detect
677830ea5b7c75413526c19531f0180fa6e45b98919Douglas Gregor  /// cycles in the module dependency graph as modules are being built, as
6784565e487531c7bf6d348dbe9f5529784966fc7aeDouglas Gregor  /// well as to describe why we're rebuilding a particular module.
679830ea5b7c75413526c19531f0180fa6e45b98919Douglas Gregor  ///
680830ea5b7c75413526c19531f0180fa6e45b98919Douglas Gregor  /// There is no way to set this value from the command line. If we ever need
681830ea5b7c75413526c19531f0180fa6e45b98919Douglas Gregor  /// to do so (e.g., if on-demand module construction moves out-of-process),
682830ea5b7c75413526c19531f0180fa6e45b98919Douglas Gregor  /// we can add a cc1-level option to do so.
6834565e487531c7bf6d348dbe9f5529784966fc7aeDouglas Gregor  SmallVector<std::pair<std::string, FullSourceLoc>, 2> StoredModuleBuildStack;
684830ea5b7c75413526c19531f0180fa6e45b98919Douglas Gregor
68549c1f4aa2a6c360d25d605004ec3c4affd62db77Steve Naroff  // SourceManager doesn't support copy construction.
686be2fa7ebf01259b63dc52fe46c8d101c18e72269Craig Topper  explicit SourceManager(const SourceManager&) LLVM_DELETED_FUNCTION;
687be2fa7ebf01259b63dc52fe46c8d101c18e72269Craig Topper  void operator=(const SourceManager&) LLVM_DELETED_FUNCTION;
6885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
689ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis  SourceManager(DiagnosticsEngine &Diag, FileManager &FileMgr,
690ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis                bool UserFilesAreVolatile = false);
6915b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  ~SourceManager();
6921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6935b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  void clearIDTables();
6941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
695d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  DiagnosticsEngine &getDiagnostics() const { return Diag; }
69678a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios Kyrtzidis
697389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis  FileManager &getFileManager() const { return FileMgr; }
698389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis
699299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  /// \brief Set true if the SourceManager should report the original file name
700299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  /// for contents of files that were overriden by other files.Defaults to true.
701299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  void setOverridenFilesKeepOriginalName(bool value) {
702299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis    OverridenFilesKeepOriginalName = value;
703299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis  }
704299a4a967b02c9f0d0d94ad8560e3ced893f9116Argyrios Kyrtzidis
705ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis  /// \brief True if non-system source files should be treated as volatile
706ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis  /// (likely to change while trying to use them).
707ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis  bool userFilesAreVolatile() const { return UserFilesAreVolatile; }
708ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis
7094565e487531c7bf6d348dbe9f5529784966fc7aeDouglas Gregor  /// \brief Retrieve the module build stack.
7104565e487531c7bf6d348dbe9f5529784966fc7aeDouglas Gregor  ModuleBuildStack getModuleBuildStack() const {
7114565e487531c7bf6d348dbe9f5529784966fc7aeDouglas Gregor    return StoredModuleBuildStack;
712830ea5b7c75413526c19531f0180fa6e45b98919Douglas Gregor  }
713830ea5b7c75413526c19531f0180fa6e45b98919Douglas Gregor
7144565e487531c7bf6d348dbe9f5529784966fc7aeDouglas Gregor  /// \brief Set the module build stack.
7154565e487531c7bf6d348dbe9f5529784966fc7aeDouglas Gregor  void setModuleBuildStack(ModuleBuildStack stack) {
7164565e487531c7bf6d348dbe9f5529784966fc7aeDouglas Gregor    StoredModuleBuildStack.clear();
7174565e487531c7bf6d348dbe9f5529784966fc7aeDouglas Gregor    StoredModuleBuildStack.append(stack.begin(), stack.end());
718830ea5b7c75413526c19531f0180fa6e45b98919Douglas Gregor  }
719830ea5b7c75413526c19531f0180fa6e45b98919Douglas Gregor
7204565e487531c7bf6d348dbe9f5529784966fc7aeDouglas Gregor  /// \brief Push an entry to the module build stack.
7214565e487531c7bf6d348dbe9f5529784966fc7aeDouglas Gregor  void pushModuleBuildStack(StringRef moduleName, FullSourceLoc importLoc) {
7224565e487531c7bf6d348dbe9f5529784966fc7aeDouglas Gregor    StoredModuleBuildStack.push_back(std::make_pair(moduleName.str(),importLoc));
723830ea5b7c75413526c19531f0180fa6e45b98919Douglas Gregor  }
724830ea5b7c75413526c19531f0180fa6e45b98919Douglas Gregor
72512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Create the FileID for a memory buffer that will represent the
72612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// FileID for the main source.
72712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
72812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// One example of when this would be used is when the main source is read
72912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// from STDIN.
730ecd27bf256c92f56c7c7ede6f40ec5d31a40b35eArgyrios Kyrtzidis  FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer,
731ecd27bf256c92f56c7c7ede6f40ec5d31a40b35eArgyrios Kyrtzidis                             SrcMgr::CharacteristicKind Kind = SrcMgr::C_User) {
732f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    assert(MainFileID.isInvalid() && "MainFileID already set!");
733ecd27bf256c92f56c7c7ede6f40ec5d31a40b35eArgyrios Kyrtzidis    MainFileID = createFileIDForMemBuffer(Buffer, Kind);
734f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    return MainFileID;
735f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  }
736f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
73706a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
73806a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  // MainFileID creation and querying methods.
73906a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
74006a062dc784c609b75dca15fd97f468d0d846596Chris Lattner
74112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Returns the FileID of the main source file.
7422b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID getMainFileID() const { return MainFileID; }
7431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
74412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Create the FileID for the main source file.
745a1f1fad8b60e1cb9d21a40a37f2e03150bcbeb6fDouglas Gregor  FileID createMainFileID(const FileEntry *SourceFile,
746a1f1fad8b60e1cb9d21a40a37f2e03150bcbeb6fDouglas Gregor                          SrcMgr::CharacteristicKind Kind = SrcMgr::C_User) {
74706a062dc784c609b75dca15fd97f468d0d846596Chris Lattner    assert(MainFileID.isInvalid() && "MainFileID already set!");
748a1f1fad8b60e1cb9d21a40a37f2e03150bcbeb6fDouglas Gregor    MainFileID = createFileID(SourceFile, SourceLocation(), Kind);
74906a062dc784c609b75dca15fd97f468d0d846596Chris Lattner    return MainFileID;
75006a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  }
7511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
752b8c879a5363f36bdae8831112b563333e3c05acbArgyrios Kyrtzidis  /// \brief Set the file ID for the main source file.
753b8c879a5363f36bdae8831112b563333e3c05acbArgyrios Kyrtzidis  void setMainFileID(FileID FID) {
754b8c879a5363f36bdae8831112b563333e3c05acbArgyrios Kyrtzidis    assert(MainFileID.isInvalid() && "MainFileID already set!");
755b8c879a5363f36bdae8831112b563333e3c05acbArgyrios Kyrtzidis    MainFileID = FID;
756b8c879a5363f36bdae8831112b563333e3c05acbArgyrios Kyrtzidis  }
757b8c879a5363f36bdae8831112b563333e3c05acbArgyrios Kyrtzidis
758507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  /// \brief Set the file ID for the precompiled preamble.
759507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  void setPreambleFileID(FileID Preamble) {
760507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    assert(PreambleFileID.isInvalid() && "PreambleFileID already set!");
761507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis    PreambleFileID = Preamble;
762414cb64f09ce48a36377458ce5e5a90c3ad41d00Douglas Gregor  }
7635330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
764507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  /// \brief Get the file ID for the precompiled preamble if there is one.
765507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  FileID getPreambleFileID() const { return PreambleFileID; }
766507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis
76706a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
7683201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  // Methods to create new FileID's and macro expansions.
76906a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
7701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
77112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Create a new FileID that represents the specified file
77212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// being \#included from the specified IncludePosition.
77312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
77412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// This translates NULL into standard input.
7752b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
7767f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                      SrcMgr::CharacteristicKind FileCharacter,
777f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor                      int LoadedID = 0, unsigned LoadedOffset = 0) {
778ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis    const SrcMgr::ContentCache *
779ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis      IR = getOrCreateContentCache(SourceFile,
780ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis                              /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
7810d06e998910934e5ef070f53f4c272e7c6b846c6Dan Gohman    assert(IR && "getOrCreateContentCache() cannot return NULL");
782f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    return createFileID(IR, IncludePos, FileCharacter, LoadedID, LoadedOffset);
7835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
7841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
78512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Create a new FileID that represents the specified memory buffer.
78612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
78712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// This does no caching of the buffer and takes ownership of the
78812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// MemoryBuffer, so only pass a MemoryBuffer to this once.
7897f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer,
790ecd27bf256c92f56c7c7ede6f40ec5d31a40b35eArgyrios Kyrtzidis                      SrcMgr::CharacteristicKind FileCharacter = SrcMgr::C_User,
791f453cb9f677e16c00b358ec91eccf5f003765dc6Axel Naumann                                  int LoadedID = 0, unsigned LoadedOffset = 0,
792f453cb9f677e16c00b358ec91eccf5f003765dc6Axel Naumann                                 SourceLocation IncludeLoc = SourceLocation()) {
793f453cb9f677e16c00b358ec91eccf5f003765dc6Axel Naumann    return createFileID(createMemBufferContentCache(Buffer), IncludeLoc,
794ecd27bf256c92f56c7c7ede6f40ec5d31a40b35eArgyrios Kyrtzidis                        FileCharacter, LoadedID, LoadedOffset);
7951036b68525f39cb69ac22c679ed440acd8392a16Ted Kremenek  }
79606a062dc784c609b75dca15fd97f468d0d846596Chris Lattner
79712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Return a new SourceLocation that encodes the
798bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth  /// fact that a token from SpellingLoc should actually be referenced from
7993201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// ExpansionLoc, and that it represents the expansion of a macro argument
8003201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// into the function-like macro body.
801bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth  SourceLocation createMacroArgExpansionLoc(SourceLocation Loc,
802bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                            SourceLocation ExpansionLoc,
803bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                            unsigned TokLength);
804c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth
80512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Return a new SourceLocation that encodes the fact
806c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth  /// that a token from SpellingLoc should actually be referenced from
807bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth  /// ExpansionLoc.
808bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth  SourceLocation createExpansionLoc(SourceLocation Loc,
809bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                    SourceLocation ExpansionLocStart,
810bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                    SourceLocation ExpansionLocEnd,
811bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                    unsigned TokLength,
812bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                    int LoadedID = 0,
813bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                    unsigned LoadedOffset = 0);
8141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8152968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \brief Retrieve the memory buffer associated with the given file.
81650f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  ///
81750f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  /// \param Invalid If non-NULL, will be set \c true if an error
81850f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  /// occurs while retrieving the memory buffer.
81950f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File,
82050f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor                                                   bool *Invalid = 0);
8212968442603b029949246467253eeac8139a5b6d8Douglas Gregor
8222968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \brief Override the contents of the given source file by providing an
8232968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// already-allocated buffer.
8242968442603b029949246467253eeac8139a5b6d8Douglas Gregor  ///
825afbf5f810652f04c0bba235c0fa079885fb3caa8Dan Gohman  /// \param SourceFile the source file whose contents will be overriden.
8262968442603b029949246467253eeac8139a5b6d8Douglas Gregor  ///
8272968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \param Buffer the memory buffer whose contents will be used as the
8282968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// data in the given source file.
8292968442603b029949246467253eeac8139a5b6d8Douglas Gregor  ///
830f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// \param DoNotFree If true, then the buffer will not be freed when the
831f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// source manager is destroyed.
8320d06e998910934e5ef070f53f4c272e7c6b846c6Dan Gohman  void overrideFileContents(const FileEntry *SourceFile,
833f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor                            const llvm::MemoryBuffer *Buffer,
834f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor                            bool DoNotFree = false);
8352968442603b029949246467253eeac8139a5b6d8Douglas Gregor
836bed28ac1d1463adca3ecf24fca5c30646fa9dbb2Sylvestre Ledru  /// \brief Override the given source file with another one.
837b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  ///
838b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  /// \param SourceFile the source file which will be overriden.
839b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  ///
840b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  /// \param NewFile the file whose contents will be used as the
841b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  /// data instead of the contents of the given source file.
842b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  void overrideFileContents(const FileEntry *SourceFile,
843b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis                            const FileEntry *NewFile);
844b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis
845d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis  /// \brief Returns true if the file contents have been overridden.
846d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis  bool isFileOverridden(const FileEntry *File) {
847d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis    if (OverriddenFilesInfo) {
848d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis      if (OverriddenFilesInfo->OverriddenFilesWithBuffer.count(File))
849d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis        return true;
850d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis      if (OverriddenFilesInfo->OverriddenFiles.find(File) !=
851d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis          OverriddenFilesInfo->OverriddenFiles.end())
852d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis        return true;
853d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis    }
854d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis    return false;
855d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis  }
856d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis
857d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis  /// \brief Disable overridding the contents of a file, previously enabled
858af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// with #overrideFileContents.
85912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
860d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis  /// This should be called before parsing has begun.
861d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis  void disableFileContentsOverride(const FileEntry *File);
862d54dff026b02303a35147224de72bb44cbb53c79Argyrios Kyrtzidis
86306a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
86406a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  // FileID manipulation methods.
86506a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
8661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
86712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Return the buffer for the specified FileID.
86812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
86912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// If there is an error opening this buffer the first time, this
87012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// manufactures a temporary buffer and returns a non-empty error string.
871e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner  const llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc,
872e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner                                      bool *Invalid = 0) const {
873e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    bool MyInvalid = false;
874e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
875e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    if (MyInvalid || !Entry.isFile()) {
876e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor      if (Invalid)
877e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor        *Invalid = true;
8785330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
879e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor      return getFakeBufferForRecovery();
880e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    }
8815330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
8825330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher    return Entry.getFile().getContentCache()->getBuffer(Diag, *this, Loc,
883e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor                                                        Invalid);
88406a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  }
8851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
886e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner  const llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = 0) const {
887e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    bool MyInvalid = false;
888e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
889e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    if (MyInvalid || !Entry.isFile()) {
890e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor      if (Invalid)
891e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor        *Invalid = true;
8925330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
893e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor      return getFakeBufferForRecovery();
894e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    }
895e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor
8965330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher    return Entry.getFile().getContentCache()->getBuffer(Diag, *this,
8975330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher                                                        SourceLocation(),
898e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor                                                        Invalid);
899e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner  }
9005330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
90112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Returns the FileEntry record for the provided FileID.
90206a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  const FileEntry *getFileEntryForID(FileID FID) const {
903e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    bool MyInvalid = false;
904e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
905e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    if (MyInvalid || !Entry.isFile())
906e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor      return 0;
9075330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
90839afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis    const SrcMgr::ContentCache *Content = Entry.getFile().getContentCache();
90939afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis    if (!Content)
91039afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis      return 0;
91139afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis    return Content->OrigEntry;
91206a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  }
9131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
91412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Returns the FileEntry record for the provided SLocEntry.
9159d5a165d301cc9df68631e624322dd2a962f65b3Ted Kremenek  const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const
9169d5a165d301cc9df68631e624322dd2a962f65b3Ted Kremenek  {
91739afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis    const SrcMgr::ContentCache *Content = sloc.getFile().getContentCache();
91839afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis    if (!Content)
91939afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis      return 0;
92039afcafe1c0f66db67648b5230b700659448432cArgyrios Kyrtzidis    return Content->OrigEntry;
9219d5a165d301cc9df68631e624322dd2a962f65b3Ted Kremenek  }
9229d5a165d301cc9df68631e624322dd2a962f65b3Ted Kremenek
92312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Return a StringRef to the source buffer data for the
924ceafc4b63599d14f0b5b10ff92e22bf242682dceBenjamin Kramer  /// specified FileID.
925ceafc4b63599d14f0b5b10ff92e22bf242682dceBenjamin Kramer  ///
926f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor  /// \param FID The file ID whose contents will be returned.
927f715ca12bfc9fddfde75f98a197424434428b821Douglas Gregor  /// \param Invalid If non-NULL, will be set true if an error occurred.
928686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  StringRef getBufferData(FileID FID, bool *Invalid = 0) const;
929f6ac97b101c8840efa92bf29166077ce4049e293Benjamin Kramer
930d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  /// \brief Get the number of FileIDs (files and macros) that were created
9312c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// during preprocessing of \p FID, including it.
932d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  unsigned getNumCreatedFIDsForFileID(FileID FID) const {
933d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    bool Invalid = false;
934d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
935d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    if (Invalid || !Entry.isFile())
936d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis      return 0;
937d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis
938d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    return Entry.getFile().NumCreatedFIDs;
939d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  }
940d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis
941d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  /// \brief Set the number of FileIDs (files and macros) that were created
9422c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// during preprocessing of \p FID, including it.
943d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  void setNumCreatedFIDsForFileID(FileID FID, unsigned NumFIDs) const {
944d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    bool Invalid = false;
945d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
946d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    if (Invalid || !Entry.isFile())
947d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis      return;
948d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis
949d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    assert(Entry.getFile().NumCreatedFIDs == 0 && "Already set!");
950d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    const_cast<SrcMgr::FileInfo &>(Entry.getFile()).NumCreatedFIDs = NumFIDs;
951d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  }
9521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
95306a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
95406a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  // SourceLocation manipulation methods.
95506a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
9561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
95712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Return the FileID for a SourceLocation.
95812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
95912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// This is a very hot method that is used for all SourceManager queries
96012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// that start with a SourceLocation object.  It is responsible for finding
96112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// the entry in SLocEntryTable which contains the specified location.
962de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  ///
963de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  FileID getFileID(SourceLocation SpellingLoc) const {
964de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned SLocOffset = SpellingLoc.getOffset();
9651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
966de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    // If our one-entry cache covers this offset, just return it.
967de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
968de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return LastFileIDLookup;
969de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
970de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return getFileIDSlow(SLocOffset);
971de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
9721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
973aa48fe80a1b2000809900a437f0819d929793002Jordan Rose  /// \brief Return the filename of the file containing a SourceLocation.
974aa48fe80a1b2000809900a437f0819d929793002Jordan Rose  StringRef getFilename(SourceLocation SpellingLoc) const {
975aa48fe80a1b2000809900a437f0819d929793002Jordan Rose    if (const FileEntry *F = getFileEntryForID(getFileID(SpellingLoc)))
976aa48fe80a1b2000809900a437f0819d929793002Jordan Rose      return F->getName();
977aa48fe80a1b2000809900a437f0819d929793002Jordan Rose    return StringRef();
978aa48fe80a1b2000809900a437f0819d929793002Jordan Rose  }
979aa48fe80a1b2000809900a437f0819d929793002Jordan Rose
98012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Return the source location corresponding to the first byte of
98112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// the specified file.
9822b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  SourceLocation getLocForStartOfFile(FileID FID) const {
983e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    bool Invalid = false;
984e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
985e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    if (Invalid || !Entry.isFile())
986e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor      return SourceLocation();
9875330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
988e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor    unsigned FileOffset = Entry.getOffset();
989de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return SourceLocation::getFileLoc(FileOffset);
9902b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  }
991f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis
992f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis  /// \brief Return the source location corresponding to the last byte of the
993f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis  /// specified file.
994f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis  SourceLocation getLocForEndOfFile(FileID FID) const {
995f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis    bool Invalid = false;
996f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
997f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis    if (Invalid || !Entry.isFile())
998f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis      return SourceLocation();
999f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis
1000f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis    unsigned FileOffset = Entry.getOffset();
1001f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis    return SourceLocation::getFileLoc(FileOffset + getFileIDSize(FID) - 1);
1002f226ff9fe8c8db6c5b74a61ce649eda1491c3502Argyrios Kyrtzidis  }
10031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1004809d1be9820039b4cf6efa48246a0d70ffa13394James Dennett  /// \brief Returns the include location if \p FID is a \#include'd file
1005d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  /// otherwise it returns an invalid location.
1006d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  SourceLocation getIncludeLoc(FileID FID) const {
1007d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    bool Invalid = false;
1008d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
1009d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    if (Invalid || !Entry.isFile())
1010d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis      return SourceLocation();
10115330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1012d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis    return Entry.getFile().getIncludeLoc();
1013d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis  }
1014d9d2b679d0728ea7f539f38aaea38e26b8b08043Argyrios Kyrtzidis
10156c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor  // \brief Returns the import location if the given source location is
10166c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor  // located within a module, or an invalid location if the source location
10176c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor  // is within the current translation unit.
10186c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor  std::pair<SourceLocation, StringRef>
10196c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor  getModuleImportLoc(SourceLocation Loc) const {
10206c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor    FileID FID = getFileID(Loc);
10216c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor
10226c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor    // Positive file IDs are in the current translation unit, and -1 is a
10236c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor    // placeholder.
10246c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor    if (FID.ID >= -1)
10256c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor      return std::make_pair(SourceLocation(), "");
10266c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor
10276c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor    return ExternalSLocEntries->getModuleImportLoc(FID.ID);
10286c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor  }
10296c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor
103012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Given a SourceLocation object \p Loc, return the expansion
1031402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth  /// location referenced by the ID.
1032402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth  SourceLocation getExpansionLoc(SourceLocation Loc) const {
1033addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner    // Handle the non-mapped case inline, defer to out of line code to handle
1034402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth    // expansions.
1035de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (Loc.isFileID()) return Loc;
1036f84ef95ecec34f27fd05eb4e0392ca6bd3bd0be0Chandler Carruth    return getExpansionLocSlowCase(Loc);
1037de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
10381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10392c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Given \p Loc, if it is a macro location return the expansion
1040796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis  /// location or the spelling location, depending on if it comes from a
1041796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis  /// macro argument or not.
1042796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis  SourceLocation getFileLoc(SourceLocation Loc) const {
1043796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis    if (Loc.isFileID()) return Loc;
1044796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis    return getFileLocSlowCase(Loc);
1045796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis  }
1046796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis
104712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Return the start/end of the expansion information for an
104812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// expansion location.
104912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
105012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \pre \p Loc is required to be an expansion location.
1051e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  std::pair<SourceLocation,SourceLocation>
1052999f739404edf2078cf9f9c28b4dc45c19765842Chandler Carruth  getImmediateExpansionRange(SourceLocation Loc) const;
10531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
105412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Given a SourceLocation object, return the range of
1055edc3dccece244a584f8ebdb81da6c962c08e79beChandler Carruth  /// tokens covered by the expansion the ultimate file.
10566678133b8ce642f93e5141f056fa643112041ad0Chris Lattner  std::pair<SourceLocation,SourceLocation>
1057edc3dccece244a584f8ebdb81da6c962c08e79beChandler Carruth  getExpansionRange(SourceLocation Loc) const;
10581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
106012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Given a SourceLocation object, return the spelling
106112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// location referenced by the ID.
106212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
106312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// This is the place where the characters that make up the lexed token
106412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// can be found.
1065de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  SourceLocation getSpellingLoc(SourceLocation Loc) const {
1066addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner    // Handle the non-mapped case inline, defer to out of line code to handle
10673201f382956ed9beee9fb31229c2835c1208889cChandler Carruth    // expansions.
1068de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (Loc.isFileID()) return Loc;
1069addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner    return getSpellingLocSlowCase(Loc);
1070de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
10711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
107212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Given a SourceLocation object, return the spelling location
107312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// referenced by the ID.
107412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
107512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// This is the first level down towards the place where the characters
107612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// that make up the lexed token can be found.  This should not generally
107712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// be used by clients.
10781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const;
1079de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
108012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Decompose the specified location into a raw FileID + Offset pair.
108112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
108212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// The first element is the FileID, the second is the offset from the
108312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// start of the buffer of the location.
1084de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const {
1085de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    FileID FID = getFileID(Loc);
1086a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    bool Invalid = false;
1087a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    const SrcMgr::SLocEntry &E = getSLocEntry(FID, &Invalid);
1088a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    if (Invalid)
1089a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis      return std::make_pair(FileID(), 0);
1090a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    return std::make_pair(FID, Loc.getOffset()-E.getOffset());
1091de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
10921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
109312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Decompose the specified location into a raw FileID + Offset pair.
109412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
109512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// If the location is an expansion record, walk through it until we find
109612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// the final location expanded.
1097de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned>
1098e7b2b6e87dbe5b1207f77b6ff9c210a02f95bb39Chandler Carruth  getDecomposedExpansionLoc(SourceLocation Loc) const {
1099de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    FileID FID = getFileID(Loc);
1100a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    bool Invalid = false;
1101a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    const SrcMgr::SLocEntry *E = &getSLocEntry(FID, &Invalid);
1102a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    if (Invalid)
1103a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis      return std::make_pair(FileID(), 0);
11041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1105de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned Offset = Loc.getOffset()-E->getOffset();
1106de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (Loc.isFileID())
1107de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return std::make_pair(FID, Offset);
11081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1109e7b2b6e87dbe5b1207f77b6ff9c210a02f95bb39Chandler Carruth    return getDecomposedExpansionLocSlowCase(E);
1110de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
1111de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
111212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Decompose the specified location into a raw FileID + Offset pair.
111312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
111412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// If the location is an expansion record, walk through it until we find
111512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// its spelling record.
1116de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned>
1117de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  getDecomposedSpellingLoc(SourceLocation Loc) const {
1118de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    FileID FID = getFileID(Loc);
1119a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    bool Invalid = false;
1120a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    const SrcMgr::SLocEntry *E = &getSLocEntry(FID, &Invalid);
1121a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis    if (Invalid)
1122a246d270156a55999ecfa4099a0967ec4c9a254eArgyrios Kyrtzidis      return std::make_pair(FileID(), 0);
11231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1124de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned Offset = Loc.getOffset()-E->getOffset();
1125de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (Loc.isFileID())
1126de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return std::make_pair(FID, Offset);
1127de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return getDecomposedSpellingLocSlowCase(E, Offset);
11281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
11291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
113012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Returns the offset from the start of the file that the
113112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// specified SourceLocation represents.
113212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
113312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// This is not very meaningful for a macro ID.
113452c29081281955d3db9e11d10573b2d38f709099Chris Lattner  unsigned getFileOffset(SourceLocation SpellingLoc) const {
1135de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return getDecomposedLoc(SpellingLoc).second;
11365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
11371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
113812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Tests whether the given source location represents a macro
113912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// argument's expansion into the function-like macro definition.
114012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
114112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// Such source locations only appear inside of the expansion
114296d3589e523a04f4ff2058a7919226ce60696ae8Chandler Carruth  /// locations representing where a particular function-like macro was
114396d3589e523a04f4ff2058a7919226ce60696ae8Chandler Carruth  /// expanded.
114496d3589e523a04f4ff2058a7919226ce60696ae8Chandler Carruth  bool isMacroArgExpansion(SourceLocation Loc) const;
11451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1146c3cd6f7a5d33ad44f6c9cf4faa7046c77baa128eMatt Beaumont-Gay  /// \brief Tests whether the given source location represents the expansion of
1147c3cd6f7a5d33ad44f6c9cf4faa7046c77baa128eMatt Beaumont-Gay  /// a macro body.
1148c3cd6f7a5d33ad44f6c9cf4faa7046c77baa128eMatt Beaumont-Gay  ///
1149c3cd6f7a5d33ad44f6c9cf4faa7046c77baa128eMatt Beaumont-Gay  /// This is equivalent to testing whether the location is part of a macro
1150c3cd6f7a5d33ad44f6c9cf4faa7046c77baa128eMatt Beaumont-Gay  /// expansion but not the expansion of an argument to a function-like macro.
1151c3cd6f7a5d33ad44f6c9cf4faa7046c77baa128eMatt Beaumont-Gay  bool isMacroBodyExpansion(SourceLocation Loc) const;
1152c3cd6f7a5d33ad44f6c9cf4faa7046c77baa128eMatt Beaumont-Gay
11532c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Returns true if \p Loc is inside the [\p Start, +\p Length)
1154499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis  /// chunk of the source location address space.
115512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
11562c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// If it's true and \p RelativeOffset is non-null, it will be set to the
11572c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// relative offset of \p Loc inside the chunk.
1158499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis  bool isInSLocAddrSpace(SourceLocation Loc,
1159499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis                         SourceLocation Start, unsigned Length,
1160499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis                         unsigned *RelativeOffset = 0) const {
1161499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis    assert(((Start.getOffset() < NextLocalOffset &&
1162499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis               Start.getOffset()+Length <= NextLocalOffset) ||
1163499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis            (Start.getOffset() >= CurrentLoadedOffset &&
1164499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis                Start.getOffset()+Length < MaxLoadedOffset)) &&
1165499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis           "Chunk is not valid SLoc address space");
1166499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis    unsigned LocOffs = Loc.getOffset();
1167499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis    unsigned BeginOffs = Start.getOffset();
1168499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis    unsigned EndOffs = BeginOffs + Length;
1169499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis    if (LocOffs >= BeginOffs && LocOffs < EndOffs) {
1170499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis      if (RelativeOffset)
1171499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis        *RelativeOffset = LocOffs - BeginOffs;
1172499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis      return true;
1173499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis    }
1174499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis
1175499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis    return false;
1176499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis  }
1177499ea5550d6e2fc5cfbd33b47f06d92ce25d7a13Argyrios Kyrtzidis
11782c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Return true if both \p LHS and \p RHS are in the local source
117912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// location address space or the loaded one.
118012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
118112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// If it's true and \p RelativeOffset is non-null, it will be set to the
118212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// offset of \p RHS relative to \p LHS.
1183b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis  bool isInSameSLocAddrSpace(SourceLocation LHS, SourceLocation RHS,
1184b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis                             int *RelativeOffset) const {
1185b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis    unsigned LHSOffs = LHS.getOffset(), RHSOffs = RHS.getOffset();
1186b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis    bool LHSLoaded = LHSOffs >= CurrentLoadedOffset;
1187b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis    bool RHSLoaded = RHSOffs >= CurrentLoadedOffset;
1188b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis
1189b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis    if (LHSLoaded == RHSLoaded) {
1190b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis      if (RelativeOffset)
1191b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis        *RelativeOffset = RHSOffs - LHSOffs;
1192b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis      return true;
1193b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis    }
1194b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis
1195b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis    return false;
1196b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis  }
1197b6c465e17ec37390667223a18a340e8652c212ffArgyrios Kyrtzidis
1198de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  //===--------------------------------------------------------------------===//
1199de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  // Queries about the code at a SourceLocation.
1200de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  //===--------------------------------------------------------------------===//
12011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
120212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Return a pointer to the start of the specified location
1203de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// in the appropriate spelling MemoryBuffer.
120450f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  ///
120550f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  /// \param Invalid If non-NULL, will be set \c true if an error occurs.
120650f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  const char *getCharacterData(SourceLocation SL, bool *Invalid = 0) const;
12071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
120812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Return the column # for the specified file position.
120912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
12109dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  /// This is significantly cheaper to compute than the line number.  This
12113201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// returns zero if the column number isn't known.  This may only be called
12123201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// on a file sloc, so you must choose a spelling or expansion location
1213f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner  /// before calling this method.
12145330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher  unsigned getColumnNumber(FileID FID, unsigned FilePos,
121550f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor                           bool *Invalid = 0) const;
12165ef04ee40c3332d31b6d1439f50d0ddb45812929Chandler Carruth  unsigned getSpellingColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
1217a77c031cb66f75d22672070052cc6e0205289ff8Chandler Carruth  unsigned getExpansionColumnNumber(SourceLocation Loc,
1218b49dcd249c7f4f034493741f95394987a4ccf244Chandler Carruth                                    bool *Invalid = 0) const;
12195ef04ee40c3332d31b6d1439f50d0ddb45812929Chandler Carruth  unsigned getPresumedColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
12201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
122212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Given a SourceLocation, return the spelling line number
122312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// for the position indicated.
122412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
122512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// This requires building and caching a table of line offsets for the
122612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// MemoryBuffer, so this is not cheap: use only when about to emit a
122712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// diagnostic.
122850f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = 0) const;
12295ef04ee40c3332d31b6d1439f50d0ddb45812929Chandler Carruth  unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
1230642116259e8df6286063a17361c20e95b5017a0aChandler Carruth  unsigned getExpansionLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
12315ef04ee40c3332d31b6d1439f50d0ddb45812929Chandler Carruth  unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
12321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1233c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  /// \brief Return the filename or buffer identifier of the buffer the
1234c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  /// location is in.
1235c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  ///
1236c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  /// Note that this name does not respect \#line directives.  Use
1237c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  /// getPresumedLoc for normal clients.
123850f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  const char *getBufferName(SourceLocation Loc, bool *Invalid = 0) const;
12391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
124012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Return the file characteristic of the specified source
124112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// location, indicating whether this is a normal file, a system
12426b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// header, or an "implicit extern C" system header.
12436b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  ///
12446b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// This state can be modified with flags on GNU linemarker directives like:
1245c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  /// \code
12466b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  ///   # 4 "foo.h" 3
1247c351d9837ea1d0b04842497e76c76125426a982cJames Dennett  /// \endcode
12486b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// which changes all source locations in the current file after that to be
12496b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// considered to be from a system header.
12506b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const;
12511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
125212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Returns the "presumed" location of a SourceLocation specifies.
125312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
125412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// A "presumed location" can be modified by \#line or GNU line marker
125512a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// directives.  This provides a view on the data that a user should see
125612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// in diagnostics, for example.
1257b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  ///
12583201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// Note that a presumed location is always given as the expansion point of
12593201f382956ed9beee9fb31229c2835c1208889cChandler Carruth  /// an expansion location, not at the spelling location.
1260cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  ///
1261cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  /// \returns The presumed location of the specified SourceLocation. If the
1262cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  /// presumed location cannot be calculate (e.g., because \p Loc is invalid
1263cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  /// or the file containing \p Loc has changed on disk), returns an invalid
1264cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  /// presumed location.
126562221b17c90457df9ca0ff20bb54d634e8951defRichard Smith  PresumedLoc getPresumedLoc(SourceLocation Loc,
126662221b17c90457df9ca0ff20bb54d634e8951defRichard Smith                             bool UseLineDirectives = true) const;
12671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
126812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Returns true if both SourceLocations correspond to the same file.
12699fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
1270a11d61793341fea195c29a0dab3fbd74f2b39a8cChris Lattner    return getFileID(Loc1) == getFileID(Loc2);
12719fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  }
12721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
127312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Returns true if the file of provided SourceLocation is the main
127412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// file.
12759fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  bool isFromMainFile(SourceLocation Loc) const {
1276a11d61793341fea195c29a0dab3fbd74f2b39a8cChris Lattner    return getFileID(Loc) == getMainFileID();
12771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
12781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
127912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Returns if a SourceLocation is in a system header.
12807bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber  bool isInSystemHeader(SourceLocation Loc) const {
12810b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner    return getFileCharacteristic(Loc) != SrcMgr::C_User;
1282721818304ac462d8c6ce05eecd02884033db78f1Chris Lattner  }
12831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
128412a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Returns if a SourceLocation is in an "extern C" system header.
12850d456588acac0713a7c33063922d35a8cc8c658eChris Lattner  bool isInExternCSystemHeader(SourceLocation Loc) const {
12860d456588acac0713a7c33063922d35a8cc8c658eChris Lattner    return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem;
12870d456588acac0713a7c33063922d35a8cc8c658eChris Lattner  }
12881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1289d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay  /// \brief Returns whether \p Loc is expanded from a macro in a system header.
1290d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay  bool isInSystemMacro(SourceLocation loc) {
1291d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay    return loc.isMacroID() && isInSystemHeader(getSpellingLoc(loc));
1292d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay  }
1293d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay
12942c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief The size of the SLocEnty that \p FID represents.
1295984e42ca1ff7775ce39372c314f1cb7d6862c4c7Argyrios Kyrtzidis  unsigned getFileIDSize(FileID FID) const;
129654232ade44d31e98ea83f43ca066128e315dcbdaArgyrios Kyrtzidis
12972c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Given a specific FileID, returns true if \p Loc is inside that
12982c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// FileID chunk and sets relative offset (offset of \p Loc from beginning
12992c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// of FileID) to \p relativeOffset.
1300d60a34a4e514ec0dfddd05ef2744be104e111f45Argyrios Kyrtzidis  bool isInFileID(SourceLocation Loc, FileID FID,
1301d60a34a4e514ec0dfddd05ef2744be104e111f45Argyrios Kyrtzidis                  unsigned *RelativeOffset = 0) const {
1302d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis    unsigned Offs = Loc.getOffset();
1303d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis    if (isOffsetInFileID(FID, Offs)) {
1304d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis      if (RelativeOffset)
1305d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis        *RelativeOffset = Offs - getSLocEntry(FID).getOffset();
1306d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis      return true;
1307d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis    }
1308d60a34a4e514ec0dfddd05ef2744be104e111f45Argyrios Kyrtzidis
1309d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis    return false;
1310d7cb46c316808169679a8d72c69f02a1e55d78a8Argyrios Kyrtzidis  }
1311469244a322dd5d35cee1d02d70a2edbc12ac5ce7Argyrios Kyrtzidis
131206a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
13135b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  // Line Table Manipulation Routines
13145b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  //===--------------------------------------------------------------------===//
13151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
131612a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Return the uniqued ID for the specified filename.
13171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ///
1318686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  unsigned getLineTableFilenameID(StringRef Str);
13191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
132012a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Add a line note to the line table for the FileID and offset
132112a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// specified by Loc.
132212a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  ///
132312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// If FilenameID is -1, it is considered to be unspecified.
13244c4ea17d7f991516c37a871dfa4bbe5723fa85f0Chris Lattner  void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID);
13259d79ebac47ffde6a1cb312f4c09b66b1b9a397fbChris Lattner  void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID,
13261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                   bool IsFileEntry, bool IsFileExit,
13279d79ebac47ffde6a1cb312f4c09b66b1b9a397fbChris Lattner                   bool IsSystemHeader, bool IsExternCHeader);
1328bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
1329bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  /// \brief Determine if the source manager has a line table.
1330bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  bool hasLineTable() const { return LineTable != 0; }
1331bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
1332bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  /// \brief Retrieve the stored line table.
1333bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  LineTableInfo &getLineTable();
1334bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
13355b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  //===--------------------------------------------------------------------===//
1336457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek  // Queries for performance analysis.
1337457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek  //===--------------------------------------------------------------------===//
1338457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek
133912a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Return the total amount of physical memory allocated by the
1340457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek  /// ContentCache allocator.
1341457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek  size_t getContentCacheSize() const {
1342457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek    return ContentCacheAlloc.getTotalMemory();
1343457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek  }
13445330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1345f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek  struct MemoryBufferSizes {
1346f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek    const size_t malloc_bytes;
1347f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek    const size_t mmap_bytes;
13485330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1349f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek    MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)
1350f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek      : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {}
1351f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek  };
1352f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek
135312a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Return the amount of memory used by memory buffers, breaking down
1354f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek  /// by heap-backed versus mmap'ed memory.
1355f61b831d7f6a15676b07647f507de80324cb7056Ted Kremenek  MemoryBufferSizes getMemoryBufferSizes() const;
13565330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
135712a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Return the amount of memory used for various side tables and
135812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// data structures in the SourceManager.
1359ca7dc2b755eb81ac95121ce1a1f1aa44a4a0fe12Ted Kremenek  size_t getDataStructureSizes() const;
1360457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek
1361457aaf0692dfb2d9638f383334b81027f637f20cTed Kremenek  //===--------------------------------------------------------------------===//
136206a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  // Other miscellaneous methods.
136306a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
136410b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis
136510b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  /// \brief Get the source location for the given file:line:col triplet.
136610b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  ///
136710b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  /// If the source file is included multiple times, the source location will
136810b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  /// be based upon the first inclusion.
1369ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  SourceLocation translateFileLineCol(const FileEntry *SourceFile,
1370507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis                                      unsigned Line, unsigned Col) const;
1371ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis
1372b201e16e0c331b0bdeae7b30f9f79aae32beb1b2Argyrios Kyrtzidis  /// \brief Get the FileID for the given file.
1373b201e16e0c331b0bdeae7b30f9f79aae32beb1b2Argyrios Kyrtzidis  ///
1374b201e16e0c331b0bdeae7b30f9f79aae32beb1b2Argyrios Kyrtzidis  /// If the source file is included multiple times, the FileID will be the
1375b201e16e0c331b0bdeae7b30f9f79aae32beb1b2Argyrios Kyrtzidis  /// first inclusion.
1376b201e16e0c331b0bdeae7b30f9f79aae32beb1b2Argyrios Kyrtzidis  FileID translateFile(const FileEntry *SourceFile) const;
1377b201e16e0c331b0bdeae7b30f9f79aae32beb1b2Argyrios Kyrtzidis
13782c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Get the source location in \p FID for the given line:col.
13792c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// Returns null location if \p FID is not a file SLocEntry.
1380507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  SourceLocation translateLineCol(FileID FID,
1381507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis                                  unsigned Line, unsigned Col) const;
1382efa2ff8603dae51f5f5ed7509a503f477498ad22Argyrios Kyrtzidis
13832c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief If \p Loc points inside a function macro argument, the returned
1384ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  /// location will be the macro location in which the argument was expanded.
1385ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  /// If a macro argument is used multiple times, the expanded location will
1386ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  /// be at the first expansion of the argument.
1387ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  /// e.g.
1388ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  ///   MY_MACRO(foo);
1389ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  ///             ^
1390ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  /// Passing a file location pointing at 'foo', will yield a macro location
1391ac836e442cbd17f33533bd0b4879258945bc1723Argyrios Kyrtzidis  /// where 'foo' was expanded into.
1392507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  SourceLocation getMacroArgExpandedLocation(SourceLocation Loc) const;
13931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13942aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis  /// \brief Determines the order of 2 source locations in the translation unit.
13952aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis  ///
13962aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis  /// \returns true if LHS source location comes before RHS, false otherwise.
13972aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis  bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const;
13982aa03d588bd2d3c73deb662880c2244bf2e384b9Argyrios Kyrtzidis
1399b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis  /// \brief Determines the order of 2 source locations in the "source location
1400b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis  /// address space".
14015d579e7b0463a2e65328df85e4fed8e07799fd9eArgyrios Kyrtzidis  bool isBeforeInSLocAddrSpace(SourceLocation LHS, SourceLocation RHS) const {
14025d579e7b0463a2e65328df85e4fed8e07799fd9eArgyrios Kyrtzidis    return isBeforeInSLocAddrSpace(LHS, RHS.getOffset());
1403b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis  }
1404b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis
1405b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis  /// \brief Determines the order of a source location and a source location
1406b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis  /// offset in the "source location address space".
1407f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///
14085330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher  /// Note that we always consider source locations loaded from
14095d579e7b0463a2e65328df85e4fed8e07799fd9eArgyrios Kyrtzidis  bool isBeforeInSLocAddrSpace(SourceLocation LHS, unsigned RHS) const {
1410f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    unsigned LHSOffset = LHS.getOffset();
1411f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    bool LHSLoaded = LHSOffset >= CurrentLoadedOffset;
1412f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    bool RHSLoaded = RHS >= CurrentLoadedOffset;
1413f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    if (LHSLoaded == RHSLoaded)
14145d579e7b0463a2e65328df85e4fed8e07799fd9eArgyrios Kyrtzidis      return LHSOffset < RHS;
14155330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1416f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    return LHSLoaded;
1417b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis  }
1418b73377eeb3eff76be134203aebb6068244b177f3Argyrios Kyrtzidis
1419c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner  // Iterators over FileInfos.
14200d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>
14210d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner      ::const_iterator fileinfo_iterator;
1422c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner  fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); }
1423c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner  fileinfo_iterator fileinfo_end() const { return FileInfos.end(); }
1424d93256e55673a17d18543397ec462416acb13792Douglas Gregor  bool hasFileInfo(const FileEntry *File) const {
1425d93256e55673a17d18543397ec462416acb13792Douglas Gregor    return FileInfos.find(File) != FileInfos.end();
1426d93256e55673a17d18543397ec462416acb13792Douglas Gregor  }
1427c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner
142812a4ced3dc30adcf2e1532c6323d517e9949a61eJames Dennett  /// \brief Print statistics to stderr.
14295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
14305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void PrintStats() const;
143178d85f53b093867bbb0123f016956178eea7343eTed Kremenek
1432f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief Get the number of local SLocEntries we have.
1433f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  unsigned local_sloc_entry_size() const { return LocalSLocEntryTable.size(); }
14345330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1435f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief Get a local SLocEntry. This is exposed for indexing.
14365330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher  const SrcMgr::SLocEntry &getLocalSLocEntry(unsigned Index,
1437f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor                                             bool *Invalid = 0) const {
1438f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    assert(Index < LocalSLocEntryTable.size() && "Invalid index");
1439f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    return LocalSLocEntryTable[Index];
1440bdfe48ac80573e026595af91e541474dbf02565fDouglas Gregor  }
14415330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1442f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief Get the number of loaded SLocEntries we have.
1443f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  unsigned loaded_sloc_entry_size() const { return LoadedSLocEntryTable.size();}
14445330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1445f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief Get a loaded SLocEntry. This is exposed for indexing.
144670042f5d1ca378138f90b7b9384023701f5d03d8David Blaikie  const SrcMgr::SLocEntry &getLoadedSLocEntry(unsigned Index,
144770042f5d1ca378138f90b7b9384023701f5d03d8David Blaikie                                              bool *Invalid = 0) const {
1448f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    assert(Index < LoadedSLocEntryTable.size() && "Invalid index");
1449a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis    if (SLocEntryLoaded[Index])
1450a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis      return LoadedSLocEntryTable[Index];
1451a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis    return loadSLocEntry(Index, Invalid);
1452f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  }
14535330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1454e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor  const SrcMgr::SLocEntry &getSLocEntry(FileID FID, bool *Invalid = 0) const {
1455c705d2520a51de1dc38d36efada8e9bc2d8b0d1fArgyrios Kyrtzidis    if (FID.ID == 0 || FID.ID == -1) {
1456c705d2520a51de1dc38d36efada8e9bc2d8b0d1fArgyrios Kyrtzidis      if (Invalid) *Invalid = true;
1457c705d2520a51de1dc38d36efada8e9bc2d8b0d1fArgyrios Kyrtzidis      return LocalSLocEntryTable[0];
1458c705d2520a51de1dc38d36efada8e9bc2d8b0d1fArgyrios Kyrtzidis    }
1459f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    return getSLocEntryByID(FID.ID);
1460bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  }
1461bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
1462f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  unsigned getNextLocalOffset() const { return NextLocalOffset; }
14635330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1464f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  void setExternalSLocEntrySource(ExternalSLocEntrySource *Source) {
1465f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    assert(LoadedSLocEntryTable.empty() &&
1466f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor           "Invalidating existing loaded entries");
1467f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    ExternalSLocEntries = Source;
1468f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  }
14695330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1470f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief Allocate a number of loaded SLocEntries, which will be actually
1471f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// loaded on demand from the external source.
1472f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ///
1473f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// NumSLocEntries will be allocated, which occupy a total of TotalSize space
1474f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// in the global source view. The lowest ID and the base offset of the
1475f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// entries will be returned.
1476f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  std::pair<int, unsigned>
1477f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  AllocateLoadedSLocEntries(unsigned NumSLocEntries, unsigned TotalSize);
14785330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
14792c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Returns true if \p Loc came from a PCH/Module.
1480aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis  bool isLoadedSourceLocation(SourceLocation Loc) const {
1481aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis    return Loc.getOffset() >= CurrentLoadedOffset;
1482aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis  }
1483aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis
14842c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Returns true if \p Loc did not come from a PCH/Module.
1485aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis  bool isLocalSourceLocation(SourceLocation Loc) const {
1486aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis    return Loc.getOffset() < NextLocalOffset;
1487aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis  }
1488aa6edaeb35e02a07bd4840c0159900754f083ce5Argyrios Kyrtzidis
14892c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Returns true if \p FID came from a PCH/Module.
1490718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis  bool isLoadedFileID(FileID FID) const {
1491718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis    assert(FID.ID != -1 && "Using FileID sentinel value");
1492718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis    return FID.ID < 0;
1493718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis  }
1494718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis
14952c3c7675b4c01b9d555c47232597a000fbb93c26Matt Beaumont-Gay  /// \brief Returns true if \p FID did not come from a PCH/Module.
1496718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis  bool isLocalFileID(FileID FID) const {
1497718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis    return !isLoadedFileID(FID);
1498718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis  }
1499718699169a7ccaf623e375031789efeed82f869bArgyrios Kyrtzidis
150029271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay  /// Gets the location of the immediate macro caller, one level up the stack
150129271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay  /// toward the initial macro typed into the source.
150229271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay  SourceLocation getImmediateMacroCallerLoc(SourceLocation Loc) const {
150329271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay    if (!Loc.isMacroID()) return Loc;
150429271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay
150529271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay    // When we have the location of (part of) an expanded parameter, its
1506678839297204002df215e0be12bcd10b20a9a4a4Richard Smith    // spelling location points to the argument as expanded in the macro call,
150729271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay    // and therefore is used to locate the macro caller.
150829271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay    if (isMacroArgExpansion(Loc))
150929271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay      return getImmediateSpellingLoc(Loc);
151029271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay
151129271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay    // Otherwise, the caller of the macro is located where this macro is
151229271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay    // expanded (while the spelling is part of the macro definition).
151329271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay    return getImmediateExpansionRange(Loc).first;
151429271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay  }
151529271fbcef645117df05d5b60e593acb6562422cMatt Beaumont-Gay
15165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerprivate:
1517e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor  const llvm::MemoryBuffer *getFakeBufferForRecovery() const;
1518a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis  const SrcMgr::ContentCache *getFakeContentCacheForRecovery() const;
1519a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis
1520a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis  const SrcMgr::SLocEntry &loadSLocEntry(unsigned Index, bool *Invalid) const;
1521e23ac65af568ffe611b0990818ac3a57c856a4d8Douglas Gregor
1522f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  /// \brief Get the entry with the given unwrapped FileID.
1523f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  const SrcMgr::SLocEntry &getSLocEntryByID(int ID) const {
1524f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    assert(ID != -1 && "Using FileID sentinel value");
1525f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    if (ID < 0)
1526f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor      return getLoadedSLocEntryByID(ID);
1527f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    return getLocalSLocEntry(static_cast<unsigned>(ID));
1528f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  }
15295330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1530a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis  const SrcMgr::SLocEntry &getLoadedSLocEntryByID(int ID,
1531a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis                                                  bool *Invalid = 0) const {
1532a4c29b6e55c9d4ef44a51c45c6785e8b4fe9deedArgyrios Kyrtzidis    return getLoadedSLocEntry(static_cast<unsigned>(-ID - 2), Invalid);
1533f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  }
15345330ee071743b8a896aa46979b020e6c3ca9b1ccEric Christopher
1535af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// Implements the common elements of storing an expansion info struct into
1536af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// the SLocEntry table and producing a source location that refers to it.
153778df836808aee22c3157e1bc23bc4ec569b80568Chandler Carruth  SourceLocation createExpansionLocImpl(const SrcMgr::ExpansionInfo &Expansion,
1538bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                        unsigned TokLength,
1539bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                        int LoadedID = 0,
1540bf340e452339e374ea6eef78c1f0a2abdd16c5a3Chandler Carruth                                        unsigned LoadedOffset = 0);
1541c8d1ecca1cd3fadbd331d15c420755aa6184554bChandler Carruth
1542af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Return true if the specified FileID contains the
1543de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// specified SourceLocation offset.  This is a very hot method.
1544de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const {
1545de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID);
1546de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    // If the entry is after the offset, it can't contain it.
1547de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (SLocOffset < Entry.getOffset()) return false;
15481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1549f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    // If this is the very last entry then it does.
1550f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    if (FID.ID == -2)
1551f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor      return true;
1552f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor
1553f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    // If it is the last local entry, then it does if the location is local.
1554b2efdf3e92f986956f0a755ba815f3870838583bBenjamin Kramer    if (FID.ID+1 == static_cast<int>(LocalSLocEntryTable.size()))
1555f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor      return SLocOffset < NextLocalOffset;
15567f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
1557f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    // Otherwise, the entry after it has to not include it. This works for both
1558f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor    // local and loaded entries.
1559b2efdf3e92f986956f0a755ba815f3870838583bBenjamin Kramer    return SLocOffset < getSLocEntryByID(FID.ID+1).getOffset();
1560de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
15611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1562af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Create a new fileID for the specified ContentCache and
1563af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// include position.
1564af50aab0c317462129d73ae8000c6394c718598dJames Dennett  ///
1565af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// This works regardless of whether the ContentCache corresponds to a
1566af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// file or some other input source.
15672b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID createFileID(const SrcMgr::ContentCache* File,
15682b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner                      SourceLocation IncludePos,
15697f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                      SrcMgr::CharacteristicKind DirCharacter,
1570f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor                      int LoadedID, unsigned LoadedOffset);
15711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1572de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  const SrcMgr::ContentCache *
1573ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis    getOrCreateContentCache(const FileEntry *SourceFile,
1574ff398965a5abfaf5bc47bc022876f56a28e5b9a7Argyrios Kyrtzidis                            bool isSystemFile = false);
1575c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek
1576af50aab0c317462129d73ae8000c6394c718598dJames Dennett  /// \brief Create a new ContentCache for the specified  memory buffer.
15771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const SrcMgr::ContentCache*
15782b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  createMemBufferContentCache(const llvm::MemoryBuffer *Buf);
15791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1580de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  FileID getFileIDSlow(unsigned SLocOffset) const;
1581f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  FileID getFileIDLocal(unsigned SLocOffset) const;
1582f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  FileID getFileIDLoaded(unsigned SLocOffset) const;
1583de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
1584f84ef95ecec34f27fd05eb4e0392ca6bd3bd0be0Chandler Carruth  SourceLocation getExpansionLocSlowCase(SourceLocation Loc) const;
1585addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner  SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;
1586796dbfb6c43336f58c026137c438e53eadc381f7Argyrios Kyrtzidis  SourceLocation getFileLocSlowCase(SourceLocation Loc) const;
1587addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner
1588de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned>
1589e7b2b6e87dbe5b1207f77b6ff9c210a02f95bb39Chandler Carruth  getDecomposedExpansionLocSlowCase(const SrcMgr::SLocEntry *E) const;
1590de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned>
1591de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
1592de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner                                   unsigned Offset) const;
1593fb3612ef197cb8532c05f33889ec1aed7c26e5cbArgyrios Kyrtzidis  void computeMacroArgsCache(MacroArgsMap *&MacroArgsCache, FileID FID) const;
15940872a06d1ee1a3b62ef833f955051418d18006a1Argyrios Kyrtzidis  void associateFileChunkWithMacroArgExp(MacroArgsMap &MacroArgsCache,
15950872a06d1ee1a3b62ef833f955051418d18006a1Argyrios Kyrtzidis                                         FileID FID,
15960872a06d1ee1a3b62ef833f955051418d18006a1Argyrios Kyrtzidis                                         SourceLocation SpellLoc,
15970872a06d1ee1a3b62ef833f955051418d18006a1Argyrios Kyrtzidis                                         SourceLocation ExpansionLoc,
15980872a06d1ee1a3b62ef833f955051418d18006a1Argyrios Kyrtzidis                                         unsigned ExpansionLength) const;
1599ac1ffcc55b861737ba2466cd1ca1accd8eafceaaArgyrios Kyrtzidis  friend class ASTReader;
1600ac1ffcc55b861737ba2466cd1ca1accd8eafceaaArgyrios Kyrtzidis  friend class ASTWriter;
16015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
16025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1603aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko/// \brief Comparison function object.
1604aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenkotemplate<typename T>
1605aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenkoclass BeforeThanCompare;
1606aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
1607aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko/// \brief Compare two source locations.
1608aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenkotemplate<>
1609aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenkoclass BeforeThanCompare<SourceLocation> {
1610aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  SourceManager &SM;
1611aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
1612aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenkopublic:
1613aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  explicit BeforeThanCompare(SourceManager &SM) : SM(SM) { }
1614aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
1615aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  bool operator()(SourceLocation LHS, SourceLocation RHS) const {
1616aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    return SM.isBeforeInTranslationUnit(LHS, RHS);
1617aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  }
1618aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko};
1619aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
1620aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko/// \brief Compare two non-overlapping source ranges.
1621aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenkotemplate<>
1622aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenkoclass BeforeThanCompare<SourceRange> {
1623aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  SourceManager &SM;
1624aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
1625aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenkopublic:
1626aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  explicit BeforeThanCompare(SourceManager &SM) : SM(SM) { }
1627aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
1628aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  bool operator()(SourceRange LHS, SourceRange RHS) {
1629aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    return SM.isBeforeInTranslationUnit(LHS.getBegin(), RHS.getBegin());
1630aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  }
1631aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko};
16325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}  // end namespace clang
16345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1635b7551f71a4c5a901bbae72b0a80af3745b7a0c0dDouglas Gregor
16365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif
1637