SourceManager.h revision 10b46d2f0b976676d10681d73fe061b5ae409b36
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- SourceManager.h - Track and cache source files ---------*- C++ -*-===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file defines the SourceManager interface.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifndef LLVM_CLANG_SOURCEMANAGER_H
155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#define LLVM_CLANG_SOURCEMANAGER_H
165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/SourceLocation.h"
180d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner#include "llvm/Support/Allocator.h"
19464c4b3fdd473c9722cc00e7ef5f9929578f9c46Chris Lattner#include "llvm/Support/DataTypes.h"
200d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner#include "llvm/ADT/DenseMap.h"
215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include <vector>
229dc62f044a6ba21f503bd56607d94b32704e7945Chris Lattner#include <cassert>
235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace llvm {
255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass MemoryBuffer;
265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace clang {
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass SourceManager;
31099b4747042352f69184481a48508b599a8d3f73Ted Kremenekclass FileManager;
325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass FileEntry;
335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass IdentifierTokenInfo;
345b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattnerclass LineTableInfo;
355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
360b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner/// SrcMgr - Public enums and private classes that are part of the
370b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner/// SourceManager implementation.
385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace SrcMgr {
409d72851fec9e9c62570a027d42701562bbf29751Chris Lattner  /// CharacteristicKind - This is used to represent whether a file or directory
410b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner  /// holds normal user code, system code, or system code which is implicitly
420b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner  /// 'extern "C"' in C++ mode.  Entire directories can be tagged with this
430b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner  /// (this is maintained by DirectoryLookup and friends) as can specific
440b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner  /// FileIDInfos when a #pragma system_header is seen or various other cases.
450b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner  ///
469d72851fec9e9c62570a027d42701562bbf29751Chris Lattner  enum CharacteristicKind {
470b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner    C_User, C_System, C_ExternCSystem
480b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner  };
490b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner
5078d85f53b093867bbb0123f016956178eea7343eTed Kremenek  /// ContentCache - Once instance of this struct is kept for every file
5106a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  /// loaded or used.  This object owns the MemoryBuffer object.
52c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek  class ContentCache {
53c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    /// Buffer - The actual buffer containing the characters from the input
54c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    /// file.  This is owned by the ContentCache object.
5505816591ec488a933dfecc9ff9f3cbf3c32767c2Chris Lattner    mutable const llvm::MemoryBuffer *Buffer;
56c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek
57c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek  public:
5878d85f53b093867bbb0123f016956178eea7343eTed Kremenek    /// Reference to the file entry.  This reference does not own
5978d85f53b093867bbb0123f016956178eea7343eTed Kremenek    /// the FileEntry object.  It is possible for this to be NULL if
6078d85f53b093867bbb0123f016956178eea7343eTed Kremenek    /// the ContentCache encapsulates an imaginary text buffer.
6105816591ec488a933dfecc9ff9f3cbf3c32767c2Chris Lattner    const FileEntry *Entry;
6278d85f53b093867bbb0123f016956178eea7343eTed Kremenek
630d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner    /// SourceLineCache - A bump pointer allocated array of offsets for each
640d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner    /// source line.  This is lazily computed.  This is owned by the
650d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner    /// SourceManager BumpPointerAllocator object.
6605816591ec488a933dfecc9ff9f3cbf3c32767c2Chris Lattner    unsigned *SourceLineCache;
675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
68b6427f821de8cce1566fb6e755143ea0918d5543Ted Kremenek    /// NumLines - The number of lines in this ContentCache.  This is only valid
69b6427f821de8cce1566fb6e755143ea0918d5543Ted Kremenek    /// if SourceLineCache is non-null.
705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    unsigned NumLines;
7110b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis
7210b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis    /// FirstFID - First FileID that was created for this ContentCache.
7310b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis    /// Represents the first source inclusion of the file associated with this
7410b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis    /// ContentCache.
7510b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis    mutable FileID FirstFID;
7610b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis
77c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    /// getBuffer - Returns the memory buffer for the associated content.
7805816591ec488a933dfecc9ff9f3cbf3c32767c2Chris Lattner    const llvm::MemoryBuffer *getBuffer() const;
79c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek
80c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    /// getSize - Returns the size of the content encapsulated by this
81c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    ///  ContentCache. This can be the size of the source file or the size of an
82c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    ///  arbitrary scratch buffer.  If the ContentCache encapsulates a source
83c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    ///  file this size is retrieved from the file's FileEntry.
84c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    unsigned getSize() const;
85c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek
86c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    /// getSizeBytesMapped - Returns the number of bytes actually mapped for
87c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    ///  this ContentCache.  This can be 0 if the MemBuffer was not actually
88c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    ///  instantiated.
89c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    unsigned getSizeBytesMapped() const;
90c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek
9105816591ec488a933dfecc9ff9f3cbf3c32767c2Chris Lattner    void setBuffer(const llvm::MemoryBuffer *B) {
92c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek      assert(!Buffer && "MemoryBuffer already set.");
93c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek      Buffer = B;
94c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek    }
9578d85f53b093867bbb0123f016956178eea7343eTed Kremenek
960d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner    ContentCache(const FileEntry *Ent = 0)
970d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner      : Buffer(0), Entry(Ent), SourceLineCache(0), NumLines(0) {}
9878d85f53b093867bbb0123f016956178eea7343eTed Kremenek
9978d85f53b093867bbb0123f016956178eea7343eTed Kremenek    ~ContentCache();
1000d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek
1010d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek    /// The copy ctor does not allow copies where source object has either
1020d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek    ///  a non-NULL Buffer or SourceLineCache.  Ownership of allocated memory
1030d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek    ///  is not transfered, so this is a logical error.
1040d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner    ContentCache(const ContentCache &RHS) : Buffer(0), SourceLineCache(0) {
1050d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek      Entry = RHS.Entry;
1060d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek
1070d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner      assert (RHS.Buffer == 0 && RHS.SourceLineCache == 0
1080d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek              && "Passed ContentCache object cannot own a buffer.");
1090d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek
1100d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek      NumLines = RHS.NumLines;
1110d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek    }
1120d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek
1130d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek  private:
1140d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek    // Disable assignments.
11505816591ec488a933dfecc9ff9f3cbf3c32767c2Chris Lattner    ContentCache &operator=(const ContentCache& RHS);
11678d85f53b093867bbb0123f016956178eea7343eTed Kremenek  };
1175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
118de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// FileInfo - Information about a FileID, basically just the logical file
119de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// that it represents and include stack information.
1205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
121de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// Each FileInfo has include stack information, indicating where it came
122de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// from.  This information encodes the #include chain that a token was
123de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// instantiated from.  The main include file has an invalid IncludeLoc.
1245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
125de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// FileInfos contain a "ContentCache *", with the contents of the file.
1265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
127de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  class FileInfo {
1285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    /// IncludeLoc - The location of the #include that brought in this file.
129de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    /// This is an invalid SLOC for the main file (top of the #include chain).
130de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned IncludeLoc;  // Really a SourceLocation
1319dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner
1326e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    /// Data - This contains the ContentCache* and the bits indicating the
1336e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    /// characteristic of the file and whether it has #line info, all bitmangled
1346e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    /// together.
1356e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    uintptr_t Data;
13678d85f53b093867bbb0123f016956178eea7343eTed Kremenek  public:
137de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    /// get - Return a FileInfo object.
138de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    static FileInfo get(SourceLocation IL, const ContentCache *Con,
139de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner                        CharacteristicKind FileCharacter) {
140de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      FileInfo X;
141de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      X.IncludeLoc = IL.getRawEncoding();
1426e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner      X.Data = (uintptr_t)Con;
14300282d6e1194655a2e89f940bd6fa8484b52e666Chris Lattner      assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned");
1446e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner      assert((unsigned)FileCharacter < 4 && "invalid file character");
1456e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner      X.Data |= (unsigned)FileCharacter;
1465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return X;
1475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
149de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    SourceLocation getIncludeLoc() const {
150de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return SourceLocation::getFromRawEncoding(IncludeLoc);
151de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
1526e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    const ContentCache* getContentCache() const {
15300282d6e1194655a2e89f940bd6fa8484b52e666Chris Lattner      return reinterpret_cast<const ContentCache*>(Data & ~7UL);
1546e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner    }
155de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
1560b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner    /// getCharacteristic - Return whether this is a system header or not.
1579d72851fec9e9c62570a027d42701562bbf29751Chris Lattner    CharacteristicKind getFileCharacteristic() const {
1586e1aff2f586025f2d385ee49239f626b0fc63fd7Chris Lattner      return (CharacteristicKind)(Data & 3);
1590b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner    }
160ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner
161ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    /// hasLineDirectives - Return true if this FileID has #line directives in
162ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    /// it.
163ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    bool hasLineDirectives() const { return (Data & 4) != 0; }
164ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner
165ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    /// setHasLineDirectives - Set the flag that indicates that this FileID has
166ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    /// line table entries associated with it.
167ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    void setHasLineDirectives() {
168ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner      Data |= 4;
169ac50e3427cb9eb3dc9f13f29a78f00ef3122433dChris Lattner    }
1709dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  };
1719dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner
172de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// InstantiationInfo - Each InstantiationInfo encodes the Instantiation
173de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// location - where the token was ultimately instantiated, and the
174de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// SpellingLoc - where the actual character data for the token came from.
175de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  class InstantiationInfo {
176e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner     // Really these are all SourceLocations.
177e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner
178e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    /// SpellingLoc - Where the spelling for the token can be found.
179e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    unsigned SpellingLoc;
180e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner
181e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    /// InstantiationLocStart/InstantiationLocEnd - In a macro expansion, these
18214f79002e58556798e86168c63e48d533287eda5Douglas Gregor    /// indicate the start and end of the instantiation.  In object-like macros,
183e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    /// these will be the same.  In a function-like macro instantiation, the
184e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    /// start will be the identifier and the end will be the ')'.
185e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    unsigned InstantiationLocStart, InstantiationLocEnd;
1869dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  public:
187de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    SourceLocation getSpellingLoc() const {
188de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return SourceLocation::getFromRawEncoding(SpellingLoc);
189de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
190e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    SourceLocation getInstantiationLocStart() const {
191e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner      return SourceLocation::getFromRawEncoding(InstantiationLocStart);
192e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    }
193e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    SourceLocation getInstantiationLocEnd() const {
194e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner      return SourceLocation::getFromRawEncoding(InstantiationLocEnd);
195e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    }
196e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner
197e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    std::pair<SourceLocation,SourceLocation> getInstantiationLocRange() const {
198e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner      return std::make_pair(getInstantiationLocStart(),
199e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner                            getInstantiationLocEnd());
200e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    }
2015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
202e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    /// get - Return a InstantiationInfo for an expansion.  IL specifies
203df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner    /// the instantiation location (where the macro is expanded), and SL
204df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner    /// specifies the spelling location (where the characters from the token
205e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    /// come from).  IL and PL can both refer to normal File SLocs or
206e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    /// instantiation locations.
207e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner    static InstantiationInfo get(SourceLocation ILStart, SourceLocation ILEnd,
208e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner                                 SourceLocation SL) {
209de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      InstantiationInfo X;
210de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      X.SpellingLoc = SL.getRawEncoding();
211e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner      X.InstantiationLocStart = ILStart.getRawEncoding();
212e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner      X.InstantiationLocEnd = ILEnd.getRawEncoding();
2139dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner      return X;
2145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
215de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  };
216de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
217de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// SLocEntry - This is a discriminated union of FileInfo and
218de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// InstantiationInfo.  SourceManager keeps an array of these objects, and
219de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// they are uniquely identified by the FileID datatype.
220de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  class SLocEntry {
221de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned Offset;   // low bit is set for instantiation info.
222de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    union {
223de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      FileInfo File;
224de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      InstantiationInfo Instantiation;
225de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    };
226de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  public:
227de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned getOffset() const { return Offset >> 1; }
228099b4747042352f69184481a48508b599a8d3f73Ted Kremenek
229de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    bool isInstantiation() const { return Offset & 1; }
230de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    bool isFile() const { return !isInstantiation(); }
231de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
232de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    const FileInfo &getFile() const {
233de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      assert(isFile() && "Not a file SLocEntry!");
234de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return File;
235de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
236de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
237de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    const InstantiationInfo &getInstantiation() const {
238de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      assert(isInstantiation() && "Not an instantiation SLocEntry!");
239de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return Instantiation;
240de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
241099b4747042352f69184481a48508b599a8d3f73Ted Kremenek
242de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    static SLocEntry get(unsigned Offset, const FileInfo &FI) {
243de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      SLocEntry E;
244de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      E.Offset = Offset << 1;
245de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      E.File = FI;
246de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return E;
247de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
248de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
249de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    static SLocEntry get(unsigned Offset, const InstantiationInfo &II) {
250de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      SLocEntry E;
251de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      E.Offset = (Offset << 1) | 1;
252de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      E.Instantiation = II;
253de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return E;
254de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    }
2555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  };
2565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}  // end SrcMgr namespace.
2577f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
2587f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor/// \brief External source of source location entries.
2597f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregorclass ExternalSLocEntrySource {
2607f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregorpublic:
2617f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  virtual ~ExternalSLocEntrySource();
2627f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
2637f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  /// \brief Read the source location entry with index ID.
2647f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  virtual void ReadSLocEntry(unsigned ID) = 0;
2657f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor};
2667f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
2675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SourceManager - This file handles loading and caching of source files into
2685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// memory.  This object owns the MemoryBuffer objects for all of the loaded
2695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// files and assigns unique FileID's for each unique #include chain.
2705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
2715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// The SourceManager can be queried for information about SourceLocation
272f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner/// objects, turning them into either spelling or instantiation locations.
273f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner/// Spelling locations represent where the bytes corresponding to a token came
274f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner/// from and instantiation locations represent where the location is in the
275f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner/// user's view.  In the case of a macro expansion, for example, the spelling
276f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner/// location indicates where the expanded token came from and the instantiation
277f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner/// location specifies where it was expanded.
2785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass SourceManager {
2790d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  mutable llvm::BumpPtrAllocator ContentCacheAlloc;
2800d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner
2815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// FileInfos - Memoized information about all of the files tracked by this
2820d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek  /// SourceManager.  This set allows us to merge ContentCache entries based
2830d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek  /// on their FileEntry*.  All ContentCache objects will thus have unique,
2840d892d8bfddd4916cc4f3467e1184a623d0716daTed Kremenek  /// non-null, FileEntry pointers.
2850d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos;
2865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// MemBufferInfos - Information about various memory buffers that we have
2880d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  /// read in.  All FileEntry* within the stored ContentCache objects are NULL,
2890d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  /// as they do not refer to a file.
2900d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  std::vector<SrcMgr::ContentCache*> MemBufferInfos;
2915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
292de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// SLocEntryTable - This is an array of SLocEntry's that we have created.
293de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// FileID is an index into this vector.  This array is sorted by the offset.
294de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::vector<SrcMgr::SLocEntry> SLocEntryTable;
295de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// NextOffset - This is the next available offset that a new SLocEntry can
296de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// start at.  It is SLocEntryTable.back().getOffset()+size of back() entry.
297de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  unsigned NextOffset;
2987f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
2997f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  /// \brief If source location entries are being lazily loaded from
3007f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  /// an external source, this vector indicates whether the Ith source
3017f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  /// location entry has already been loaded from the external storage.
3027f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  std::vector<bool> SLocEntryLoaded;
3037f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
3047f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  /// \brief An external source for source location entries.
3057f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  ExternalSLocEntrySource *ExternalSLocEntries;
3067f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
307de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// LastFileIDLookup - This is a one-entry cache to speed up getFileID.
308de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// LastFileIDLookup records the last FileID looked up or created, because it
309de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// is very common to look up many tokens from the same file.
310de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  mutable FileID LastFileIDLookup;
3119dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner
3125b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  /// LineTable - This holds information for #line directives.  It is referenced
3135b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  /// by indices from SLocEntryTable.
3145b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  LineTableInfo *LineTable;
3155b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner
3165e36a7a89da5d69ece23fc8228624a053f75c645Chris Lattner  /// LastLineNo - These ivars serve as a cache used in the getLineNumber
3175e36a7a89da5d69ece23fc8228624a053f75c645Chris Lattner  /// method which is used to speedup getLineNumber calls to nearby locations.
3182b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  mutable FileID LastLineNoFileIDQuery;
319f812a45dd93634c9300ed5533bd26b56374714a1Chris Lattner  mutable SrcMgr::ContentCache *LastLineNoContentCache;
320f812a45dd93634c9300ed5533bd26b56374714a1Chris Lattner  mutable unsigned LastLineNoFilePos;
321f812a45dd93634c9300ed5533bd26b56374714a1Chris Lattner  mutable unsigned LastLineNoResult;
32278d85f53b093867bbb0123f016956178eea7343eTed Kremenek
32376edd0e4ae0592a7225d50d0bad6732ac64dca2aTed Kremenek  /// MainFileID - The file ID for the main source file of the translation unit.
3242b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID MainFileID;
32549c1f4aa2a6c360d25d605004ec3c4affd62db77Steve Naroff
326de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  // Statistics for -print-stats.
327de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  mutable unsigned NumLinearScans, NumBinaryProbes;
328de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
32949c1f4aa2a6c360d25d605004ec3c4affd62db77Steve Naroff  // SourceManager doesn't support copy construction.
33049c1f4aa2a6c360d25d605004ec3c4affd62db77Steve Naroff  explicit SourceManager(const SourceManager&);
33149c1f4aa2a6c360d25d605004ec3c4affd62db77Steve Naroff  void operator=(const SourceManager&);
3325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
3337f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  SourceManager()
3347f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    : ExternalSLocEntries(0), LineTable(0), NumLinearScans(0),
3357f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor      NumBinaryProbes(0) {
336de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    clearIDTables();
337de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
3385b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  ~SourceManager();
3395b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner
3405b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  void clearIDTables();
3415b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner
34206a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
34306a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  // MainFileID creation and querying methods.
34406a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
34506a062dc784c609b75dca15fd97f468d0d846596Chris Lattner
34676edd0e4ae0592a7225d50d0bad6732ac64dca2aTed Kremenek  /// getMainFileID - Returns the FileID of the main source file.
3472b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID getMainFileID() const { return MainFileID; }
34876edd0e4ae0592a7225d50d0bad6732ac64dca2aTed Kremenek
34906a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  /// createMainFileID - Create the FileID for the main source file.
35006a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  FileID createMainFileID(const FileEntry *SourceFile,
35106a062dc784c609b75dca15fd97f468d0d846596Chris Lattner                          SourceLocation IncludePos) {
35206a062dc784c609b75dca15fd97f468d0d846596Chris Lattner    assert(MainFileID.isInvalid() && "MainFileID already set!");
35306a062dc784c609b75dca15fd97f468d0d846596Chris Lattner    MainFileID = createFileID(SourceFile, IncludePos, SrcMgr::C_User);
35406a062dc784c609b75dca15fd97f468d0d846596Chris Lattner    return MainFileID;
35506a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  }
35606a062dc784c609b75dca15fd97f468d0d846596Chris Lattner
35706a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
358de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  // Methods to create new FileID's and instantiations.
35906a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
36006a062dc784c609b75dca15fd97f468d0d846596Chris Lattner
3615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// createFileID - Create a new FileID that represents the specified file
3625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// being #included from the specified IncludePosition.  This returns 0 on
3635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// error and translates NULL into standard input.
3647f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  /// PreallocateID should be non-zero to specify which a pre-allocated,
3657f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  /// lazily computed source location is being filled in by this operation.
3662b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
3677f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                      SrcMgr::CharacteristicKind FileCharacter,
3687f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                      unsigned PreallocatedID = 0,
3697f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                      unsigned Offset = 0) {
370de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile);
3712b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner    if (IR == 0) return FileID();    // Error opening file?
3727f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    return createFileID(IR, IncludePos, FileCharacter, PreallocatedID, Offset);
3735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// createFileIDForMemBuffer - Create a new FileID that represents the
3765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// specified memory buffer.  This does no caching of the buffer and takes
3775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once.
3787f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer,
3797f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                                  unsigned PreallocatedID = 0,
3807f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                                  unsigned Offset = 0) {
3817bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber    return createFileID(createMemBufferContentCache(Buffer), SourceLocation(),
3827f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                        SrcMgr::C_User, PreallocatedID, Offset);
3835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3851036b68525f39cb69ac22c679ed440acd8392a16Ted Kremenek  /// createMainFileIDForMembuffer - Create the FileID for a memory buffer
3861036b68525f39cb69ac22c679ed440acd8392a16Ted Kremenek  ///  that will represent the FileID for the main source.  One example
3871036b68525f39cb69ac22c679ed440acd8392a16Ted Kremenek  ///  of when this would be used is when the main source is read from STDIN.
3882b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) {
3892b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner    assert(MainFileID.isInvalid() && "MainFileID already set!");
3907697b5c5a918729718da9ecd9e374bbd973a69e9Chris Lattner    MainFileID = createFileIDForMemBuffer(Buffer);
3911036b68525f39cb69ac22c679ed440acd8392a16Ted Kremenek    return MainFileID;
3921036b68525f39cb69ac22c679ed440acd8392a16Ted Kremenek  }
39306a062dc784c609b75dca15fd97f468d0d846596Chris Lattner
394de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// createInstantiationLoc - Return a new SourceLocation that encodes the fact
395de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// that a token at Loc should actually be referenced from InstantiationLoc.
396de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// TokLength is the length of the token being instantiated.
397de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  SourceLocation createInstantiationLoc(SourceLocation Loc,
398e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner                                        SourceLocation InstantiationLocStart,
399e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner                                        SourceLocation InstantiationLocEnd,
4007f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                                        unsigned TokLength,
4017f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                                        unsigned PreallocatedID = 0,
4027f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                                        unsigned Offset = 0);
403de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
40406a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
40506a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  // FileID manipulation methods.
40606a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
40706a062dc784c609b75dca15fd97f468d0d846596Chris Lattner
40806a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  /// getBuffer - Return the buffer for the specified FileID.
40906a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  ///
41006a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  const llvm::MemoryBuffer *getBuffer(FileID FID) const {
411de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return getSLocEntry(FID).getFile().getContentCache()->getBuffer();
41206a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  }
41306a062dc784c609b75dca15fd97f468d0d846596Chris Lattner
41406a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  /// getFileEntryForID - Returns the FileEntry record for the provided FileID.
41506a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  const FileEntry *getFileEntryForID(FileID FID) const {
416de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return getSLocEntry(FID).getFile().getContentCache()->Entry;
41706a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  }
41806a062dc784c609b75dca15fd97f468d0d846596Chris Lattner
41906a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  /// getBufferData - Return a pointer to the start and end of the source buffer
42006a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  /// data for the specified FileID.
42106a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  std::pair<const char*, const char*> getBufferData(FileID FID) const;
42206a062dc784c609b75dca15fd97f468d0d846596Chris Lattner
42306a062dc784c609b75dca15fd97f468d0d846596Chris Lattner
42406a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
42506a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  // SourceLocation manipulation methods.
42606a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
4271036b68525f39cb69ac22c679ed440acd8392a16Ted Kremenek
428668ab1a36d6a5731c71a75a5f388ecafd538a896Chris Lattner  /// getFileID - Return the FileID for a SourceLocation.  This is a very
429de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// hot method that is used for all SourceManager queries that start with a
430de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// SourceLocation object.  It is responsible for finding the entry in
431de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// SLocEntryTable which contains the specified location.
432de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  ///
433de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  FileID getFileID(SourceLocation SpellingLoc) const {
434de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned SLocOffset = SpellingLoc.getOffset();
435de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
436de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    // If our one-entry cache covers this offset, just return it.
437de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
438de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return LastFileIDLookup;
439de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
440de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return getFileIDSlow(SLocOffset);
441de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
442de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
4432b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  /// getLocForStartOfFile - Return the source location corresponding to the
4442b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  /// first byte of the specified file.
4452b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  SourceLocation getLocForStartOfFile(FileID FID) const {
4467f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    assert(FID.ID < SLocEntryTable.size() && "FileID out of range");
4477f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    assert(getSLocEntry(FID).isFile() && "FileID is not a file");
4487f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    unsigned FileOffset = getSLocEntry(FID).getOffset();
449de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return SourceLocation::getFileLoc(FileOffset);
4502b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  }
4512b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner
4526678133b8ce642f93e5141f056fa643112041ad0Chris Lattner  /// getInstantiationLoc - Given a SourceLocation object, return the
4536678133b8ce642f93e5141f056fa643112041ad0Chris Lattner  /// instantiation location referenced by the ID.
454de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  SourceLocation getInstantiationLoc(SourceLocation Loc) const {
455addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner    // Handle the non-mapped case inline, defer to out of line code to handle
456addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner    // instantiations.
457de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (Loc.isFileID()) return Loc;
458addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner    return getInstantiationLocSlowCase(Loc);
459de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
460de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
461e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  /// getImmediateInstantiationRange - Loc is required to be an instantiation
462e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  /// location.  Return the start/end of the instantiation information.
463e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  std::pair<SourceLocation,SourceLocation>
464e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  getImmediateInstantiationRange(SourceLocation Loc) const;
465e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner
4666678133b8ce642f93e5141f056fa643112041ad0Chris Lattner  /// getInstantiationRange - Given a SourceLocation object, return the
4676678133b8ce642f93e5141f056fa643112041ad0Chris Lattner  /// range of tokens covered by the instantiation in the ultimate file.
4686678133b8ce642f93e5141f056fa643112041ad0Chris Lattner  std::pair<SourceLocation,SourceLocation>
4696678133b8ce642f93e5141f056fa643112041ad0Chris Lattner  getInstantiationRange(SourceLocation Loc) const;
4706678133b8ce642f93e5141f056fa643112041ad0Chris Lattner
4716678133b8ce642f93e5141f056fa643112041ad0Chris Lattner
472de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// getSpellingLoc - Given a SourceLocation object, return the spelling
473de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// location referenced by the ID.  This is the place where the characters
474de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// that make up the lexed token can be found.
475de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  SourceLocation getSpellingLoc(SourceLocation Loc) const {
476addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner    // Handle the non-mapped case inline, defer to out of line code to handle
477addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner    // instantiations.
478de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (Loc.isFileID()) return Loc;
479addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner    return getSpellingLocSlowCase(Loc);
480de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
481387616edf98739f4a0dd234c907e2b913e6a535dChris Lattner
482387616edf98739f4a0dd234c907e2b913e6a535dChris Lattner  /// getImmediateSpellingLoc - Given a SourceLocation object, return the
483387616edf98739f4a0dd234c907e2b913e6a535dChris Lattner  /// spelling location referenced by the ID.  This is the first level down
484387616edf98739f4a0dd234c907e2b913e6a535dChris Lattner  /// towards the place where the characters that make up the lexed token can be
485387616edf98739f4a0dd234c907e2b913e6a535dChris Lattner  /// found.  This should not generally be used by clients.
486387616edf98739f4a0dd234c907e2b913e6a535dChris Lattner  SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const;
487de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
488de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// getDecomposedLoc - Decompose the specified location into a raw FileID +
489de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// Offset pair.  The first element is the FileID, the second is the
490de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// offset from the start of the buffer of the location.
491de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const {
492de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    FileID FID = getFileID(Loc);
493de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return std::make_pair(FID, Loc.getOffset()-getSLocEntry(FID).getOffset());
494de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
495de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
496de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// getDecomposedInstantiationLoc - Decompose the specified location into a
497de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// raw FileID + Offset pair.  If the location is an instantiation record,
498de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// walk through it until we find the final location instantiated.
499de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned>
500de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  getDecomposedInstantiationLoc(SourceLocation Loc) const {
501de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    FileID FID = getFileID(Loc);
502de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
503de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
504de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned Offset = Loc.getOffset()-E->getOffset();
505de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (Loc.isFileID())
506de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return std::make_pair(FID, Offset);
507de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
508de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return getDecomposedInstantiationLocSlowCase(E, Offset);
509de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
510de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
511de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// getDecomposedSpellingLoc - Decompose the specified location into a raw
512de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// FileID + Offset pair.  If the location is an instantiation record, walk
513de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// through it until we find its spelling record.
514de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned>
515de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  getDecomposedSpellingLoc(SourceLocation Loc) const {
516de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    FileID FID = getFileID(Loc);
517de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
518de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
519de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned Offset = Loc.getOffset()-E->getOffset();
520de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (Loc.isFileID())
521de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner      return std::make_pair(FID, Offset);
522de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return getDecomposedSpellingLocSlowCase(E, Offset);
523de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
524de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
52552c29081281955d3db9e11d10573b2d38f709099Chris Lattner  /// getFileOffset - This method returns the offset from the start
52652c29081281955d3db9e11d10573b2d38f709099Chris Lattner  /// of the file that the specified SourceLocation represents. This is not very
52752c29081281955d3db9e11d10573b2d38f709099Chris Lattner  /// meaningful for a macro ID.
52852c29081281955d3db9e11d10573b2d38f709099Chris Lattner  unsigned getFileOffset(SourceLocation SpellingLoc) const {
529de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return getDecomposedLoc(SpellingLoc).second;
5305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
5315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
532de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
533de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  //===--------------------------------------------------------------------===//
534de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  // Queries about the code at a SourceLocation.
535de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  //===--------------------------------------------------------------------===//
536de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
5375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getCharacterData - Return a pointer to the start of the specified location
538de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// in the appropriate spelling MemoryBuffer.
5395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *getCharacterData(SourceLocation SL) const;
5405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5419dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  /// getColumnNumber - Return the column # for the specified file position.
5429dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  /// This is significantly cheaper to compute than the line number.  This
5439dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  /// returns zero if the column number isn't known.  This may only be called on
544f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner  /// a file sloc, so you must choose a spelling or instantiation location
545f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner  /// before calling this method.
5467da5aea7669e6db3e593162b8a123aef06a04d07Chris Lattner  unsigned getColumnNumber(FileID FID, unsigned FilePos) const;
5477da5aea7669e6db3e593162b8a123aef06a04d07Chris Lattner  unsigned getSpellingColumnNumber(SourceLocation Loc) const;
5487da5aea7669e6db3e593162b8a123aef06a04d07Chris Lattner  unsigned getInstantiationColumnNumber(SourceLocation Loc) const;
5499dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner
5509dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner
551df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner  /// getLineNumber - Given a SourceLocation, return the spelling line number
5525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// for the position indicated.  This requires building and caching a table of
5535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// line offsets for the MemoryBuffer, so this is not cheap: use only when
5545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// about to emit a diagnostic.
55530fc933e5fbbb5f0ea60c47976d435254e378536Chris Lattner  unsigned getLineNumber(FileID FID, unsigned FilePos) const;
556de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
55730fc933e5fbbb5f0ea60c47976d435254e378536Chris Lattner  unsigned getInstantiationLineNumber(SourceLocation Loc) const;
55830fc933e5fbbb5f0ea60c47976d435254e378536Chris Lattner  unsigned getSpellingLineNumber(SourceLocation Loc) const;
5595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
560bff5c512af8ca7ac92e974e04c06ff4f820e4ee1Chris Lattner  /// Return the filename or buffer identifier of the buffer the location is in.
561bff5c512af8ca7ac92e974e04c06ff4f820e4ee1Chris Lattner  /// Note that this name does not respect #line directives.  Use getPresumedLoc
562bff5c512af8ca7ac92e974e04c06ff4f820e4ee1Chris Lattner  /// for normal clients.
563bff5c512af8ca7ac92e974e04c06ff4f820e4ee1Chris Lattner  const char *getBufferName(SourceLocation Loc) const;
564bff5c512af8ca7ac92e974e04c06ff4f820e4ee1Chris Lattner
5656b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// getFileCharacteristic - return the file characteristic of the specified
5666b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// source location, indicating whether this is a normal file, a system
5676b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// header, or an "implicit extern C" system header.
5686b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  ///
5696b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// This state can be modified with flags on GNU linemarker directives like:
5706b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  ///   # 4 "foo.h" 3
5716b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// which changes all source locations in the current file after that to be
5726b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  /// considered to be from a system header.
5736b3066780bda02e3117d71a18ca2f430ed1454afChris Lattner  SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const;
574de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
575b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// getPresumedLoc - This method returns the "presumed" location of a
576b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// SourceLocation specifies.  A "presumed location" can be modified by #line
577b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// or GNU line marker directives.  This provides a view on the data that a
578b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// user should see in diagnostics, for example.
579b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  ///
580b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// Note that a presumed location is always given as the instantiation point
581b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// of an instantiation location, not at the spelling location.
582b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  PresumedLoc getPresumedLoc(SourceLocation Loc) const;
5835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5849fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  /// isFromSameFile - Returns true if both SourceLocations correspond to
5859fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  ///  the same file.
5869fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
587a11d61793341fea195c29a0dab3fbd74f2b39a8cChris Lattner    return getFileID(Loc1) == getFileID(Loc2);
5889fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  }
5899fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek
5909fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  /// isFromMainFile - Returns true if the file of provided SourceLocation is
5919fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  ///   the main file.
5929fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  bool isFromMainFile(SourceLocation Loc) const {
593a11d61793341fea195c29a0dab3fbd74f2b39a8cChris Lattner    return getFileID(Loc) == getMainFileID();
5949fd87b1db485e2f31d0e5687f9168b370d546847Ted Kremenek  }
595de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
5967bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber  /// isInSystemHeader - Returns if a SourceLocation is in a system header.
5977bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber  bool isInSystemHeader(SourceLocation Loc) const {
5980b9e736308af5397f558ffc8e780c438c2fdb563Chris Lattner    return getFileCharacteristic(Loc) != SrcMgr::C_User;
599721818304ac462d8c6ce05eecd02884033db78f1Chris Lattner  }
60006a062dc784c609b75dca15fd97f468d0d846596Chris Lattner
6010d456588acac0713a7c33063922d35a8cc8c658eChris Lattner  /// isInExternCSystemHeader - Returns if a SourceLocation is in an "extern C"
6020d456588acac0713a7c33063922d35a8cc8c658eChris Lattner  /// system header.
6030d456588acac0713a7c33063922d35a8cc8c658eChris Lattner  bool isInExternCSystemHeader(SourceLocation Loc) const {
6040d456588acac0713a7c33063922d35a8cc8c658eChris Lattner    return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem;
6050d456588acac0713a7c33063922d35a8cc8c658eChris Lattner  }
6060d456588acac0713a7c33063922d35a8cc8c658eChris Lattner
60706a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
6085b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  // Line Table Manipulation Routines
6095b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  //===--------------------------------------------------------------------===//
6105b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner
6115b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  /// getLineTableFilenameID - Return the uniqued ID for the specified filename.
6125b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  ///
6135b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  unsigned getLineTableFilenameID(const char *Ptr, unsigned Len);
6145b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner
6154c4ea17d7f991516c37a871dfa4bbe5723fa85f0Chris Lattner  /// AddLineNote - Add a line note to the line table for the FileID and offset
6164c4ea17d7f991516c37a871dfa4bbe5723fa85f0Chris Lattner  /// specified by Loc.  If FilenameID is -1, it is considered to be
6174c4ea17d7f991516c37a871dfa4bbe5723fa85f0Chris Lattner  /// unspecified.
6184c4ea17d7f991516c37a871dfa4bbe5723fa85f0Chris Lattner  void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID);
6199d79ebac47ffde6a1cb312f4c09b66b1b9a397fbChris Lattner  void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID,
6209d79ebac47ffde6a1cb312f4c09b66b1b9a397fbChris Lattner                   bool IsFileEntry, bool IsFileExit,
6219d79ebac47ffde6a1cb312f4c09b66b1b9a397fbChris Lattner                   bool IsSystemHeader, bool IsExternCHeader);
622bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
623bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  /// \brief Determine if the source manager has a line table.
624bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  bool hasLineTable() const { return LineTable != 0; }
625bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
626bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  /// \brief Retrieve the stored line table.
627bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  LineTableInfo &getLineTable();
628bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
6295b9a504720fb52594ca3686e10eb6c0cfa2e7d62Chris Lattner  //===--------------------------------------------------------------------===//
63006a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  // Other miscellaneous methods.
63106a062dc784c609b75dca15fd97f468d0d846596Chris Lattner  //===--------------------------------------------------------------------===//
63210b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis
63310b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  /// \brief Get the source location for the given file:line:col triplet.
63410b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  ///
63510b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  /// If the source file is included multiple times, the source location will
63610b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  /// be based upon the first inclusion.
63710b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis  SourceLocation getLocation(const FileEntry *SourceFile,
63810b46d2f0b976676d10681d73fe061b5ae409b36Argyrios Kyrtzidis                             unsigned Line, unsigned Col) const;
639e55f4ee4fd435e29015e2eea952744ee2a621030Ted Kremenek
640c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner  // Iterators over FileInfos.
6410d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner  typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>
6420d0bf8cf58b35302312cc155287fde3e81eb25a7Chris Lattner      ::const_iterator fileinfo_iterator;
643c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner  fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); }
644c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner  fileinfo_iterator fileinfo_end() const { return FileInfos.end(); }
645c6fe32a91c7372caf09152ee31a24c4b5d24deedChris Lattner
6465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// PrintStats - Print statistics to stderr.
6475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
6485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void PrintStats() const;
64978d85f53b093867bbb0123f016956178eea7343eTed Kremenek
65014f79002e58556798e86168c63e48d533287eda5Douglas Gregor  // Iteration over the source location entry table.
65114f79002e58556798e86168c63e48d533287eda5Douglas Gregor  typedef std::vector<SrcMgr::SLocEntry>::const_iterator sloc_entry_iterator;
65214f79002e58556798e86168c63e48d533287eda5Douglas Gregor
65314f79002e58556798e86168c63e48d533287eda5Douglas Gregor  sloc_entry_iterator sloc_entry_begin() const {
65414f79002e58556798e86168c63e48d533287eda5Douglas Gregor    return SLocEntryTable.begin();
65514f79002e58556798e86168c63e48d533287eda5Douglas Gregor  }
65614f79002e58556798e86168c63e48d533287eda5Douglas Gregor
65714f79002e58556798e86168c63e48d533287eda5Douglas Gregor  sloc_entry_iterator sloc_entry_end() const {
65814f79002e58556798e86168c63e48d533287eda5Douglas Gregor    return SLocEntryTable.end();
65914f79002e58556798e86168c63e48d533287eda5Douglas Gregor  }
66014f79002e58556798e86168c63e48d533287eda5Douglas Gregor
66114f79002e58556798e86168c63e48d533287eda5Douglas Gregor  unsigned sloc_entry_size() const { return SLocEntryTable.size(); }
66214f79002e58556798e86168c63e48d533287eda5Douglas Gregor
663bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  const SrcMgr::SLocEntry &getSLocEntry(FileID FID) const {
664bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor    assert(FID.ID < SLocEntryTable.size() && "Invalid id");
6657f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    if (ExternalSLocEntries &&
6667f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor        FID.ID < SLocEntryLoaded.size() &&
6677f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor        !SLocEntryLoaded[FID.ID])
6687f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor      ExternalSLocEntries->ReadSLocEntry(FID.ID);
669bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor    return SLocEntryTable[FID.ID];
670bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  }
671bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
672f60e9918690fcf02974bc1ebecd42c99d561855eDouglas Gregor  unsigned getNextOffset() const { return NextOffset; }
673f60e9918690fcf02974bc1ebecd42c99d561855eDouglas Gregor
6747f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  /// \brief Preallocate some number of source location entries, which
6757f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  /// will be loaded as needed from the given external source.
6767f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  void PreallocateSLocEntries(ExternalSLocEntrySource *Source,
6777f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                              unsigned NumSLocEntries,
6787f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                              unsigned NextOffset);
6797f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
6802bf1eb09f06a9792fa94dff0703f2aa2c4bace2aDouglas Gregor  /// \brief Clear out any preallocated source location entries that
6812bf1eb09f06a9792fa94dff0703f2aa2c4bace2aDouglas Gregor  /// haven't already been loaded.
6822bf1eb09f06a9792fa94dff0703f2aa2c4bace2aDouglas Gregor  void ClearPreallocatedSLocEntries();
6832bf1eb09f06a9792fa94dff0703f2aa2c4bace2aDouglas Gregor
6845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerprivate:
685de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// isOffsetInFileID - Return true if the specified FileID contains the
686de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// specified SourceLocation offset.  This is a very hot method.
687de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const {
688de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID);
689de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    // If the entry is after the offset, it can't contain it.
690de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (SLocOffset < Entry.getOffset()) return false;
691de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
692de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    // If this is the last entry than it does.  Otherwise, the entry after it
693de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    // has to not include it.
694de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    if (FID.ID+1 == SLocEntryTable.size()) return true;
6957f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
6967f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    return SLocOffset < getSLocEntry(FileID::get(FID.ID+1)).getOffset();
697de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  }
698de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
69978d85f53b093867bbb0123f016956178eea7343eTed Kremenek  /// createFileID - Create a new fileID for the specified ContentCache and
70078d85f53b093867bbb0123f016956178eea7343eTed Kremenek  ///  include position.  This works regardless of whether the ContentCache
70178d85f53b093867bbb0123f016956178eea7343eTed Kremenek  ///  corresponds to a file or some other input source.
7022b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID createFileID(const SrcMgr::ContentCache* File,
7032b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner                      SourceLocation IncludePos,
7047f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                      SrcMgr::CharacteristicKind DirCharacter,
7057f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                      unsigned PreallocatedID = 0,
7067f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                      unsigned Offset = 0);
7075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
708de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  const SrcMgr::ContentCache *
709de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    getOrCreateContentCache(const FileEntry *SourceFile);
710c16c208e8519476d838ad11fffc8e0ecea50550dTed Kremenek
71178d85f53b093867bbb0123f016956178eea7343eTed Kremenek  /// createMemBufferContentCache - Create a new ContentCache for the specified
71278d85f53b093867bbb0123f016956178eea7343eTed Kremenek  ///  memory buffer.
71378d85f53b093867bbb0123f016956178eea7343eTed Kremenek  const SrcMgr::ContentCache*
7142b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  createMemBufferContentCache(const llvm::MemoryBuffer *Buf);
7159dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner
716de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  FileID getFileIDSlow(unsigned SLocOffset) const;
717de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner
718addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner  SourceLocation getInstantiationLocSlowCase(SourceLocation Loc) const;
719addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner  SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;
720addb797ca2b5afc1a1e82fd8d5d6eb2a592e75a9Chris Lattner
721de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned>
722de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E,
723de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner                                        unsigned Offset) const;
724de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  std::pair<FileID, unsigned>
725de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
726de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner                                   unsigned Offset) const;
7275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
7285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}  // end namespace clang
7315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif
733