SourceLocation.h revision 1eb4433ac451dc16f4133a88af2d002ac26c58ef
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;
232b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  template <typename T> struct DenseMapInfo;
249c728dc4d8da89c73fcae74c9e72d7a83ffd7b6dTed Kremenek}
259c728dc4d8da89c73fcae74c9e72d7a83ffd7b6dTed Kremenek
265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace clang {
271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28a9793ed6a77946c988ee38035baf4cde6ff2e864Ted Kremenekclass SourceManager;
299c728dc4d8da89c73fcae74c9e72d7a83ffd7b6dTed Kremenekclass FileEntry;
301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
312b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner/// FileID - This is an opaque identifier used by SourceManager which refers to
322b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner/// a source file (MemoryBuffer) along with its #include path and #line data.
332b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner///
342b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattnerclass FileID {
352b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  /// ID - Opaque identifier, 0 is "invalid".
362b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  unsigned ID;
372b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattnerpublic:
382b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID() : ID(0) {}
391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
402b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  bool isInvalid() const { return ID == 0; }
411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
422b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  bool operator==(const FileID &RHS) const { return ID == RHS.ID; }
432b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  bool operator<(const FileID &RHS) const { return ID < RHS.ID; }
442b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  bool operator<=(const FileID &RHS) const { return ID <= RHS.ID; }
452b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  bool operator!=(const FileID &RHS) const { return !(*this == RHS); }
462b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  bool operator>(const FileID &RHS) const { return RHS < *this; }
472b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  bool operator>=(const FileID &RHS) const { return RHS <= *this; }
481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
49de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  static FileID getSentinel() { return get(~0U); }
502b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  unsigned getHashValue() const { return ID; }
511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
522b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattnerprivate:
532b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  friend class SourceManager;
54de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  static FileID get(unsigned V) {
552b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner    FileID F;
562b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner    F.ID = V;
572b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner    return F;
582b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  }
592b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  unsigned getOpaqueValue() const { return ID; }
602b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner};
611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SourceLocation - This is a carefully crafted 32-bit identifier that encodes
645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// a full include stack, line and column number information for a position in
655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// an input translation unit.
665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass SourceLocation {
675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned ID;
68bcc2a67e5180612417727cbdd8afd0f79fdf726dChris Lattner  friend class SourceManager;
695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  enum {
70de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    MacroIDBit = 1U << 31
715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  };
729ebac5e0dab6f99717e3ff169c45048966146b2eChris Lattnerpublic:
735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation() : ID(0) {}  // 0 is an invalid FileID.
751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
76de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  bool isFileID() const  { return (ID & MacroIDBit) == 0; }
77de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  bool isMacroID() const { return (ID & MacroIDBit) != 0; }
781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
79b7489d8129136437953d412e2a6cf0ef87f4a461Chris Lattner  /// isValid - Return true if this is a valid SourceLocation object.  Invalid
80b7489d8129136437953d412e2a6cf0ef87f4a461Chris Lattner  /// SourceLocations are often used when events have no corresponding location
81b7489d8129136437953d412e2a6cf0ef87f4a461Chris Lattner  /// in the source (e.g. a diagnostic is required for a command line option).
82b7489d8129136437953d412e2a6cf0ef87f4a461Chris Lattner  ///
83b7489d8129136437953d412e2a6cf0ef87f4a461Chris Lattner  bool isValid() const { return ID != 0; }
84b7489d8129136437953d412e2a6cf0ef87f4a461Chris Lattner  bool isInvalid() const { return ID == 0; }
851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
866fda54c19321673965536b0a8f7236f635cf9730Chris Lattnerprivate:
87de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// getOffset - Return the index for SourceManager's SLocEntryTable table,
88de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  /// note that this is not an index *into* it though.
89de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  unsigned getOffset() const {
90de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return ID & ~MacroIDBit;
914d10ef18c32eae35be07e0d8d18b5ff485b4c5f9Chris Lattner  }
926fda54c19321673965536b0a8f7236f635cf9730Chris Lattner
93de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  static SourceLocation getFileLoc(unsigned ID) {
94de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
959dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner    SourceLocation L;
96de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    L.ID = ID;
979dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner    return L;
985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
100de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  static SourceLocation getMacroLoc(unsigned ID) {
101de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
102d1623a81992a24abbfcd5520b32a0dd90857b8a8Chris Lattner    SourceLocation L;
103de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    L.ID = MacroIDBit | ID;
1049dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner    return L;
1059dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  }
1064d10ef18c32eae35be07e0d8d18b5ff485b4c5f9Chris Lattnerpublic:
1071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1089dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  /// getFileLocWithOffset - Return a source location with the specified offset
1099dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  /// from this file SourceLocation.
110d1623a81992a24abbfcd5520b32a0dd90857b8a8Chris Lattner  SourceLocation getFileLocWithOffset(int Offset) const {
111de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    assert(((getOffset()+Offset) & MacroIDBit) == 0 && "invalid location");
112de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    SourceLocation L;
113de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    L.ID = ID+Offset;
114de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    return L;
1159dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  }
1161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getRawEncoding - When a SourceLocation itself cannot be used, this returns
1185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// an (opaque) 32-bit integer encoding for it.  This should only be passed
1195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// to SourceLocation::getFromRawEncoding, it should not be inspected
1205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// directly.
1215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned getRawEncoding() const { return ID; }
1221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getFromRawEncoding - Turn a raw encoding of a SourceLocation object into
1255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// a real SourceLocation.
1265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  static SourceLocation getFromRawEncoding(unsigned Encoding) {
1275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    SourceLocation X;
1285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    X.ID = Encoding;
1295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return X;
1305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
132ae50fa0a9e7217b043ed4ffe175af4b26dc90f34Chris Lattner  void print(llvm::raw_ostream &OS, const SourceManager &SM) const;
133b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  void dump(const SourceManager &SM) const;
1345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
1355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerinline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) {
1375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return LHS.getRawEncoding() == RHS.getRawEncoding();
1385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerinline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) {
1415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return !(LHS == RHS);
1425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1446fda54c19321673965536b0a8f7236f635cf9730Chris Lattnerinline bool operator<(const SourceLocation &LHS, const SourceLocation &RHS) {
1456fda54c19321673965536b0a8f7236f635cf9730Chris Lattner  return LHS.getRawEncoding() < RHS.getRawEncoding();
1466fda54c19321673965536b0a8f7236f635cf9730Chris Lattner}
1475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// SourceRange - a trival tuple used to represent a source range.
1495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass SourceRange {
1505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation B;
1515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation E;
1525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
1535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceRange(): B(SourceLocation()), E(SourceLocation()) {}
1545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceRange(SourceLocation loc) : B(loc), E(loc) {}
1555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {}
1561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
157311ff02fae0392bee6abe7723cdf5a69b2899a47Chris Lattner  SourceLocation getBegin() const { return B; }
158311ff02fae0392bee6abe7723cdf5a69b2899a47Chris Lattner  SourceLocation getEnd() const { return E; }
1591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
160e80a59cc41d42a970466cb020b6f44c5b8831d70Chris Lattner  void setBegin(SourceLocation b) { B = b; }
161e80a59cc41d42a970466cb020b6f44c5b8831d70Chris Lattner  void setEnd(SourceLocation e) { E = e; }
1621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool isValid() const { return B.isValid() && E.isValid(); }
1641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
165a898283deb689b2454f3a966ef1cbf81bcb3e3e4Ted Kremenek  bool operator==(const SourceRange &X) const {
166a898283deb689b2454f3a966ef1cbf81bcb3e3e4Ted Kremenek    return B == X.B && E == X.E;
167a898283deb689b2454f3a966ef1cbf81bcb3e3e4Ted Kremenek  }
1681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
169a898283deb689b2454f3a966ef1cbf81bcb3e3e4Ted Kremenek  bool operator!=(const SourceRange &X) const {
170a898283deb689b2454f3a966ef1cbf81bcb3e3e4Ted Kremenek    return B != X.B || E != X.E;
171a898283deb689b2454f3a966ef1cbf81bcb3e3e4Ted Kremenek  }
17219a95bcf3561ed977c48d5f2a2793b60a8c8e573Ted Kremenek};
1731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
174a50bd54164393ca3cd08016e7099bdeb531b5014Chris Lattner/// FullSourceLoc - A SourceLocation and its associated SourceManager.  Useful
175a50bd54164393ca3cd08016e7099bdeb531b5014Chris Lattner/// for argument passing to functions that expect both objects.
176a50bd54164393ca3cd08016e7099bdeb531b5014Chris Lattnerclass FullSourceLoc : public SourceLocation {
1779c728dc4d8da89c73fcae74c9e72d7a83ffd7b6dTed Kremenek  SourceManager* SrcMgr;
178a9793ed6a77946c988ee38035baf4cde6ff2e864Ted Kremenekpublic:
1793632a35e811096da86d957c3e6ba0e73d75782f5Ted Kremenek  /// Creates a FullSourceLoc where isValid() returns false.
180a50bd54164393ca3cd08016e7099bdeb531b5014Chris Lattner  explicit FullSourceLoc() : SrcMgr((SourceManager*) 0) {}
181a9793ed6a77946c988ee38035baf4cde6ff2e864Ted Kremenek
1821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  explicit FullSourceLoc(SourceLocation Loc, SourceManager &SM)
183a50bd54164393ca3cd08016e7099bdeb531b5014Chris Lattner    : SourceLocation(Loc), SrcMgr(&SM) {}
1841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
185b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  SourceManager &getManager() {
186b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner    assert(SrcMgr && "SourceManager is NULL.");
1879c728dc4d8da89c73fcae74c9e72d7a83ffd7b6dTed Kremenek    return *SrcMgr;
1889c728dc4d8da89c73fcae74c9e72d7a83ffd7b6dTed Kremenek  }
1891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
190b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  const SourceManager &getManager() const {
191b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner    assert(SrcMgr && "SourceManager is NULL.");
192a9793ed6a77946c988ee38035baf4cde6ff2e864Ted Kremenek    return *SrcMgr;
193a9793ed6a77946c988ee38035baf4cde6ff2e864Ted Kremenek  }
1941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1953b4d5e955e819dd3a4bed37ea2e47d6e4cb05274Chris Lattner  FileID getFileID() const;
1961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
197f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner  FullSourceLoc getInstantiationLoc() const;
198df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner  FullSourceLoc getSpellingLoc() const;
1999c728dc4d8da89c73fcae74c9e72d7a83ffd7b6dTed Kremenek
200f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner  unsigned getInstantiationLineNumber() const;
201f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner  unsigned getInstantiationColumnNumber() const;
2029c728dc4d8da89c73fcae74c9e72d7a83ffd7b6dTed Kremenek
203df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner  unsigned getSpellingLineNumber() const;
204df7c17a8d02fe09a3466786bae3e40fc3252687aChris Lattner  unsigned getSpellingColumnNumber() const;
2055c38b6388dc44dcb8467a9e0f22d93db7221717eChris Lattner
2069c728dc4d8da89c73fcae74c9e72d7a83ffd7b6dTed Kremenek  const char *getCharacterData() const;
2071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2089c728dc4d8da89c73fcae74c9e72d7a83ffd7b6dTed Kremenek  const llvm::MemoryBuffer* getBuffer() const;
2091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2103632a35e811096da86d957c3e6ba0e73d75782f5Ted Kremenek  /// getBufferData - Return a pointer to the start and end of the source buffer
2113632a35e811096da86d957c3e6ba0e73d75782f5Ted Kremenek  /// data for the specified FileID.
2123632a35e811096da86d957c3e6ba0e73d75782f5Ted Kremenek  std::pair<const char*, const char*> getBufferData() const;
2131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
214321abd4583dc02a254489132c5ccbe125d18ce4fTed Kremenek  /// getDecomposedLoc - Decompose the specified location into a raw FileID +
215321abd4583dc02a254489132c5ccbe125d18ce4fTed Kremenek  /// Offset pair.  The first element is the FileID, the second is the
216321abd4583dc02a254489132c5ccbe125d18ce4fTed Kremenek  /// offset from the start of the buffer of the location.
217321abd4583dc02a254489132c5ccbe125d18ce4fTed Kremenek  std::pair<FileID, unsigned> getDecomposedLoc() const;
218321abd4583dc02a254489132c5ccbe125d18ce4fTed Kremenek
2197bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber  bool isInSystemHeader() const;
2201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2215c38b6388dc44dcb8467a9e0f22d93db7221717eChris Lattner  /// Prints information about this FullSourceLoc to stderr. Useful for
2225c38b6388dc44dcb8467a9e0f22d93db7221717eChris Lattner  ///  debugging.
223b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  void dump() const { SourceLocation::dump(*SrcMgr); }
224a9793ed6a77946c988ee38035baf4cde6ff2e864Ted Kremenek
2251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  friend inline bool
2260b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor  operator==(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
2270b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor    return LHS.getRawEncoding() == RHS.getRawEncoding() &&
2280b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor          LHS.SrcMgr == RHS.SrcMgr;
2290b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor  }
230a50bd54164393ca3cd08016e7099bdeb531b5014Chris Lattner
2311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  friend inline bool
2320b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor  operator!=(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
2330b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor    return !(LHS == RHS);
2340b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor  }
2350b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
2360b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor};
2371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
238b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner/// PresumedLoc - This class represents an unpacked "presumed" location which
239b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner/// can be presented to the user.  A 'presumed' location can be modified by
240b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner/// #line and GNU line marker directives and is always the instantiation point
241b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner/// of a normal location.
242b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner///
243b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner/// You can get a PresumedLoc from a SourceLocation with SourceManager.
244b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattnerclass PresumedLoc {
245b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  const char *Filename;
246b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  unsigned Line, Col;
247b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  SourceLocation IncludeLoc;
248b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattnerpublic:
249b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  PresumedLoc() : Filename(0) {}
250b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  PresumedLoc(const char *FN, unsigned Ln, unsigned Co, SourceLocation IL)
251b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner    : Filename(FN), Line(Ln), Col(Co), IncludeLoc(IL) {
252b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  }
2531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
254b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// isInvalid - Return true if this object is invalid or uninitialized. This
255b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// occurs when created with invalid source locations or when walking off
256b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// the top of a #include stack.
257b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  bool isInvalid() const { return Filename == 0; }
258b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  bool isValid() const { return Filename != 0; }
2591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
260b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// getFilename - Return the presumed filename of this location.  This can be
261b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// affected by #line etc.
262b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  const char *getFilename() const { return Filename; }
263b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner
264b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// getLine - Return the presumed line number of this location.  This can be
265b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// affected by #line etc.
266b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  unsigned getLine() const { return Line; }
2671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
268b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// getColumn - Return the presumed column number of this location.  This can
269b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// not be affected by #line, but is packaged here for convenience.
270b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  unsigned getColumn() const { return Col; }
271b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner
272b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// getIncludeLoc - Return the presumed include location of this location.
273b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  /// This can be affected by GNU linemarker directives.
274b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner  SourceLocation getIncludeLoc() const { return IncludeLoc; }
275b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner};
276b9c3f966b103f7cfe8e5e60007c4c8b38f7298ebChris Lattner
2771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
278beb7713c6102687f7e49e27b8228e84a69d8f6c6Ted Kremenek}  // end namespace clang
27919a95bcf3561ed977c48d5f2a2793b60a8c8e573Ted Kremenek
2802b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattnernamespace llvm {
2812b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  /// Define DenseMapInfo so that FileID's can be used as keys in DenseMap and
2822b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  /// DenseSets.
2832b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  template <>
2842b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  struct DenseMapInfo<clang::FileID> {
2852b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner    static inline clang::FileID getEmptyKey() {
2862b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner      return clang::FileID();
2872b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner    }
2882b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner    static inline clang::FileID getTombstoneKey() {
2891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return clang::FileID::getSentinel();
2902b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner    }
2911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2922b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner    static unsigned getHashValue(clang::FileID S) {
2932b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner      return S.getHashValue();
2942b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner    }
2951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2962b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner    static bool isEqual(clang::FileID LHS, clang::FileID RHS) {
2972b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner      return LHS == RHS;
2982b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner    }
2991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3002b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner    static bool isPod() { return true; }
3012b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  };
3021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3032b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner}  // end namespace llvm
3042b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner
3055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif
306