SourceLocation.h revision ceafc4b63599d14f0b5b10ff92e22bf242682dce
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- SourceLocation.h - Compact identifier for 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 SourceLocation class.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifndef LLVM_CLANG_SOURCELOCATION_H
155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#define LLVM_CLANG_SOURCELOCATION_H
165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
173632a35e811096da86d957c3e6ba0e73d75782f5Ted Kremenek#include <utility>
18ae50fa0a9e7217b043ed4ffe175af4b26dc90f34Chris Lattner#include <cassert>
199dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner
209c728dc4d8da89c73fcae74c9e72d7a83ffd7b6dTed Kremeneknamespace llvm {
212b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  class MemoryBuffer;
22ae50fa0a9e7217b043ed4ffe175af4b26dc90f34Chris Lattner  class raw_ostream;
23ceafc4b63599d14f0b5b10ff92e22bf242682dceBenjamin Kramer  class StringRef;
242b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  template <typename T> struct DenseMapInfo;
2506159e878569e5f39bf0e8f11b84ac3ad0970597Chris Lattner  template <typename T> struct isPodLike;
269c728dc4d8da89c73fcae74c9e72d7a83ffd7b6dTed Kremenek}
279c728dc4d8da89c73fcae74c9e72d7a83ffd7b6dTed Kremenek
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace clang {
291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30a9793ed6a77946c988ee38035baf4cde6ff2e864Ted Kremenekclass SourceManager;
319c728dc4d8da89c73fcae74c9e72d7a83ffd7b6dTed Kremenekclass FileEntry;
321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
332b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner/// FileID - This is an opaque identifier used by SourceManager which refers to
342b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner/// a source file (MemoryBuffer) along with its #include path and #line data.
352b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner///
362b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattnerclass FileID {
372b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  /// ID - Opaque identifier, 0 is "invalid".
382b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  unsigned ID;
392b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattnerpublic:
402b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID() : ID(0) {}
411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
422b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  bool isInvalid() const { return ID == 0; }
431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
442b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  bool operator==(const FileID &RHS) const { return ID == RHS.ID; }
452b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  bool operator<(const FileID &RHS) const { return ID < RHS.ID; }
462b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  bool operator<=(const FileID &RHS) const { return ID <= RHS.ID; }
472b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  bool operator!=(const FileID &RHS) const { return !(*this == RHS); }
482b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  bool operator>(const FileID &RHS) const { return RHS < *this; }
492b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  bool operator>=(const FileID &RHS) const { return RHS <= *this; }
501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
51de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  static FileID getSentinel() { return get(~0U); }
522b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  unsigned getHashValue() const { return ID; }
531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
542b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattnerprivate:
552b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  friend class SourceManager;
56de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  static FileID get(unsigned V) {
572b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner    FileID F;
582b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner    F.ID = V;
592b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner    return F;
602b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  }
612b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  unsigned getOpaqueValue() const { return ID; }
622b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner};
631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SourceLocation - This is a carefully crafted 32-bit identifier that encodes
665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// a full include stack, line and column number information for a position in
675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// an input translation unit.
685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass SourceLocation {
695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned ID;
70bcc2a67e5180612417727cbdd8afd0f79fdf726dChris Lattner  friend class SourceManager;
715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  enum {
72de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    MacroIDBit = 1U << 31
735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  };
749ebac5e0dab6f99717e3ff169c45048966146b2eChris Lattnerpublic:
755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation() : ID(0) {}  // 0 is an invalid FileID.
771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
78de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  bool isFileID() const  { return (ID & MacroIDBit) == 0; }
79de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  bool isMacroID() const { return (ID & MacroIDBit) != 0; }
801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
81b7489d8129136437953d412e2a6cf0ef87f4a461Chris Lattner  /// isValid - Return true if this is a valid SourceLocation object.  Invalid
82b7489d8129136437953d412e2a6cf0ef87f4a461Chris Lattner  /// SourceLocations are often used when events have no corresponding location
83b7489d8129136437953d412e2a6cf0ef87f4a461Chris Lattner  /// in the source (e.g. a diagnostic is required for a command line option).
84b7489d8129136437953d412e2a6cf0ef87f4a461Chris Lattner  ///
85b7489d8129136437953d412e2a6cf0ef87f4a461Chris Lattner  bool isValid() const { return ID != 0; }
86b7489d8129136437953d412e2a6cf0ef87f4a461Chris Lattner  bool isInvalid() const { return ID == 0; }
871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
886fda54c19321673965536b0a8f7236f635cf9730Chris Lattnerprivate:
89de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// getOffset - Return the index for SourceManager's SLocEntryTable table,
90de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// note that this is not an index *into* it though.
91de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  unsigned getOffset() const {
92de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return ID & ~MacroIDBit;
934d10ef18c32eae35be07e0d8d18b5ff485b4c5f9Chris Lattner  }
946fda54c19321673965536b0a8f7236f635cf9730Chris Lattner
95de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  static SourceLocation getFileLoc(unsigned ID) {
96de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
979dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner    SourceLocation L;
98de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    L.ID = ID;
999dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner    return L;
1005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
102de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  static SourceLocation getMacroLoc(unsigned ID) {
103de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
104d1623a81992a24abbfcd5520b32a0dd90857b8a8Chris Lattner    SourceLocation L;
105de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    L.ID = MacroIDBit | ID;
1069dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner    return L;
1079dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  }
1084d10ef18c32eae35be07e0d8d18b5ff485b4c5f9Chris Lattnerpublic:
1091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1109dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  /// getFileLocWithOffset - Return a source location with the specified offset
1119dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  /// from this file SourceLocation.
112d1623a81992a24abbfcd5520b32a0dd90857b8a8Chris Lattner  SourceLocation getFileLocWithOffset(int Offset) const {
113de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    assert(((getOffset()+Offset) & MacroIDBit) == 0 && "invalid location");
114de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    SourceLocation L;
115de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    L.ID = ID+Offset;
116de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return L;
1179dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  }
1181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getRawEncoding - When a SourceLocation itself cannot be used, this returns
1205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// an (opaque) 32-bit integer encoding for it.  This should only be passed
1215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// to SourceLocation::getFromRawEncoding, it should not be inspected
1225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// directly.
1235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned getRawEncoding() const { return ID; }
1241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getFromRawEncoding - Turn a raw encoding of a SourceLocation object into
1275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// a real SourceLocation.
1285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  static SourceLocation getFromRawEncoding(unsigned Encoding) {
1295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    SourceLocation X;
1305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    X.ID = Encoding;
1315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return X;
1325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
134ae50fa0a9e7217b043ed4ffe175af4b26dc90f34Chris Lattner  void print(llvm::raw_ostream &OS, const SourceManager &SM) const;
135b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  void dump(const SourceManager &SM) const;
1365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
1375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerinline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) {
1395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return LHS.getRawEncoding() == RHS.getRawEncoding();
1405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerinline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) {
1435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return !(LHS == RHS);
1445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1466fda54c19321673965536b0a8f7236f635cf9730Chris Lattnerinline bool operator<(const SourceLocation &LHS, const SourceLocation &RHS) {
1476fda54c19321673965536b0a8f7236f635cf9730Chris Lattner  return LHS.getRawEncoding() < RHS.getRawEncoding();
1486fda54c19321673965536b0a8f7236f635cf9730Chris Lattner}
1495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SourceRange - a trival tuple used to represent a source range.
1515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass SourceRange {
1525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation B;
1535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation E;
1545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
1555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceRange(): B(SourceLocation()), E(SourceLocation()) {}
1565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceRange(SourceLocation loc) : B(loc), E(loc) {}
1575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {}
1581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
159311ff02fae0392bee6abe7723cdf5a69b2899a47Chris Lattner  SourceLocation getBegin() const { return B; }
160311ff02fae0392bee6abe7723cdf5a69b2899a47Chris Lattner  SourceLocation getEnd() const { return E; }
1611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
162e80a59cc41d42a970466cb020b6f44c5b8831d70Chris Lattner  void setBegin(SourceLocation b) { B = b; }
163e80a59cc41d42a970466cb020b6f44c5b8831d70Chris Lattner  void setEnd(SourceLocation e) { E = e; }
1641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool isValid() const { return B.isValid() && E.isValid(); }
166782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek  bool isInvalid() const { return !isValid(); }
1671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
168a898283deb689b2454f3a966ef1cbf81bcb3e3e4Ted Kremenek  bool operator==(const SourceRange &X) const {
169a898283deb689b2454f3a966ef1cbf81bcb3e3e4Ted Kremenek    return B == X.B && E == X.E;
170a898283deb689b2454f3a966ef1cbf81bcb3e3e4Ted Kremenek  }
1711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
172a898283deb689b2454f3a966ef1cbf81bcb3e3e4Ted Kremenek  bool operator!=(const SourceRange &X) const {
173a898283deb689b2454f3a966ef1cbf81bcb3e3e4Ted Kremenek    return B != X.B || E != X.E;
174a898283deb689b2454f3a966ef1cbf81bcb3e3e4Ted Kremenek  }
17519a95bcf3561ed977c48d5f2a2793b60a8c8e573Ted Kremenek};
1761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
177a50bd54164393ca3cd08016e7099bdeb531b5014Chris Lattner/// FullSourceLoc - A SourceLocation and its associated SourceManager.  Useful
178a50bd54164393ca3cd08016e7099bdeb531b5014Chris Lattner/// for argument passing to functions that expect both objects.
179a50bd54164393ca3cd08016e7099bdeb531b5014Chris Lattnerclass FullSourceLoc : public SourceLocation {
1809c728dc4d8da89c73fcae74c9e72d7a83ffd7b6dTed Kremenek  SourceManager* SrcMgr;
181a9793ed6a77946c988ee38035baf4cde6ff2e864Ted Kremenekpublic:
1823632a35e811096da86d957c3e6ba0e73d75782f5Ted Kremenek  /// Creates a FullSourceLoc where isValid() returns false.
183a50bd54164393ca3cd08016e7099bdeb531b5014Chris Lattner  explicit FullSourceLoc() : SrcMgr((SourceManager*) 0) {}
184a9793ed6a77946c988ee38035baf4cde6ff2e864Ted Kremenek
1851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  explicit FullSourceLoc(SourceLocation Loc, SourceManager &SM)
186a50bd54164393ca3cd08016e7099bdeb531b5014Chris Lattner    : SourceLocation(Loc), SrcMgr(&SM) {}
1871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
188b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  SourceManager &getManager() {
189b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner    assert(SrcMgr && "SourceManager is NULL.");
1909c728dc4d8da89c73fcae74c9e72d7a83ffd7b6dTed Kremenek    return *SrcMgr;
1919c728dc4d8da89c73fcae74c9e72d7a83ffd7b6dTed Kremenek  }
1921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
193b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  const SourceManager &getManager() const {
194b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner    assert(SrcMgr && "SourceManager is NULL.");
195a9793ed6a77946c988ee38035baf4cde6ff2e864Ted Kremenek    return *SrcMgr;
196a9793ed6a77946c988ee38035baf4cde6ff2e864Ted Kremenek  }
1971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1983b4d5e955e819dd3a4bed37ea2e47d6e4cb05274Chris Lattner  FileID getFileID() const;
1991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
200f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner  FullSourceLoc getInstantiationLoc() const;
201df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner  FullSourceLoc getSpellingLoc() const;
2029c728dc4d8da89c73fcae74c9e72d7a83ffd7b6dTed Kremenek
203f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner  unsigned getInstantiationLineNumber() const;
204f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner  unsigned getInstantiationColumnNumber() const;
2059c728dc4d8da89c73fcae74c9e72d7a83ffd7b6dTed Kremenek
206df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner  unsigned getSpellingLineNumber() const;
207df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner  unsigned getSpellingColumnNumber() const;
2085c38b6388dc44dcb8467a9e0f22d93db7221717eChris Lattner
2099c728dc4d8da89c73fcae74c9e72d7a83ffd7b6dTed Kremenek  const char *getCharacterData() const;
2101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2119c728dc4d8da89c73fcae74c9e72d7a83ffd7b6dTed Kremenek  const llvm::MemoryBuffer* getBuffer() const;
2121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
213ceafc4b63599d14f0b5b10ff92e22bf242682dceBenjamin Kramer  /// getBufferData - Return a StringRef to the source buffer data for the
214ceafc4b63599d14f0b5b10ff92e22bf242682dceBenjamin Kramer  /// specified FileID.
215ceafc4b63599d14f0b5b10ff92e22bf242682dceBenjamin Kramer  llvm::StringRef getBufferData() const;
2161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
217321abd4583dc02a254489132c5ccbe125d18ce4fTed Kremenek  /// getDecomposedLoc - Decompose the specified location into a raw FileID +
218321abd4583dc02a254489132c5ccbe125d18ce4fTed Kremenek  /// Offset pair.  The first element is the FileID, the second is the
219321abd4583dc02a254489132c5ccbe125d18ce4fTed Kremenek  /// offset from the start of the buffer of the location.
220321abd4583dc02a254489132c5ccbe125d18ce4fTed Kremenek  std::pair<FileID, unsigned> getDecomposedLoc() const;
221321abd4583dc02a254489132c5ccbe125d18ce4fTed Kremenek
2227bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber  bool isInSystemHeader() const;
2231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2245c38b6388dc44dcb8467a9e0f22d93db7221717eChris Lattner  /// Prints information about this FullSourceLoc to stderr. Useful for
2255c38b6388dc44dcb8467a9e0f22d93db7221717eChris Lattner  ///  debugging.
226b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  void dump() const { SourceLocation::dump(*SrcMgr); }
227a9793ed6a77946c988ee38035baf4cde6ff2e864Ted Kremenek
2281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  friend inline bool
2290b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor  operator==(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
2300b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor    return LHS.getRawEncoding() == RHS.getRawEncoding() &&
2310b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor          LHS.SrcMgr == RHS.SrcMgr;
2320b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor  }
233a50bd54164393ca3cd08016e7099bdeb531b5014Chris Lattner
2341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  friend inline bool
2350b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor  operator!=(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
2360b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor    return !(LHS == RHS);
2370b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor  }
2380b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
2390b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor};
2401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
241b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner/// PresumedLoc - This class represents an unpacked "presumed" location which
242b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner/// can be presented to the user.  A 'presumed' location can be modified by
243b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner/// #line and GNU line marker directives and is always the instantiation point
244b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner/// of a normal location.
245b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner///
246b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner/// You can get a PresumedLoc from a SourceLocation with SourceManager.
247b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattnerclass PresumedLoc {
248b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  const char *Filename;
249b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  unsigned Line, Col;
250b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  SourceLocation IncludeLoc;
251b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattnerpublic:
252b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  PresumedLoc() : Filename(0) {}
253b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  PresumedLoc(const char *FN, unsigned Ln, unsigned Co, SourceLocation IL)
254b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner    : Filename(FN), Line(Ln), Col(Co), IncludeLoc(IL) {
255b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  }
2561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
257b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// isInvalid - Return true if this object is invalid or uninitialized. This
258b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// occurs when created with invalid source locations or when walking off
259b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// the top of a #include stack.
260b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  bool isInvalid() const { return Filename == 0; }
261b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  bool isValid() const { return Filename != 0; }
2621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
263b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// getFilename - Return the presumed filename of this location.  This can be
264b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// affected by #line etc.
265b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  const char *getFilename() const { return Filename; }
266b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner
267b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// getLine - Return the presumed line number of this location.  This can be
268b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// affected by #line etc.
269b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  unsigned getLine() const { return Line; }
2701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
271b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// getColumn - Return the presumed column number of this location.  This can
272b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// not be affected by #line, but is packaged here for convenience.
273b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  unsigned getColumn() const { return Col; }
274b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner
275b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// getIncludeLoc - Return the presumed include location of this location.
276b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// This can be affected by GNU linemarker directives.
277b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  SourceLocation getIncludeLoc() const { return IncludeLoc; }
278b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner};
279b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner
2801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
281beb7713c6102687f7e49e27b8228e84a69d8f6c6Ted Kremenek}  // end namespace clang
28219a95bcf3561ed977c48d5f2a2793b60a8c8e573Ted Kremenek
2832b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattnernamespace llvm {
2842b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  /// Define DenseMapInfo so that FileID's can be used as keys in DenseMap and
2852b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  /// DenseSets.
2862b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  template <>
2872b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  struct DenseMapInfo<clang::FileID> {
2882b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner    static inline clang::FileID getEmptyKey() {
2892b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner      return clang::FileID();
2902b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner    }
2912b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner    static inline clang::FileID getTombstoneKey() {
2921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return clang::FileID::getSentinel();
2932b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner    }
2941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2952b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner    static unsigned getHashValue(clang::FileID S) {
2962b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner      return S.getHashValue();
2972b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner    }
2981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2992b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner    static bool isEqual(clang::FileID LHS, clang::FileID RHS) {
3002b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner      return LHS == RHS;
3012b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner    }
3022b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  };
30306159e878569e5f39bf0e8f11b84ac3ad0970597Chris Lattner
30406159e878569e5f39bf0e8f11b84ac3ad0970597Chris Lattner  template <>
30506159e878569e5f39bf0e8f11b84ac3ad0970597Chris Lattner  struct isPodLike<clang::SourceLocation> { static const bool value = true; };
30606159e878569e5f39bf0e8f11b84ac3ad0970597Chris Lattner  template <>
30706159e878569e5f39bf0e8f11b84ac3ad0970597Chris Lattner  struct isPodLike<clang::FileID> { static const bool value = true; };
3081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3092b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner}  // end namespace llvm
3102b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner
3115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif
312