SourceLocation.h revision 686775deca8b8685eb90801495880e3abdd844c2
15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//===--- SourceLocation.h - Compact identifier for Source Files -*- C++ -*-===// 25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// 35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// The LLVM Compiler Infrastructure 45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// 55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// This file is distributed under the University of Illinois Open Source 65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// License. See LICENSE.TXT for details. 75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// 85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//===----------------------------------------------------------------------===// 95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// 105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// This file defines the SourceLocation class. 115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// 125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//===----------------------------------------------------------------------===// 135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#ifndef LLVM_CLANG_SOURCELOCATION_H 155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define LLVM_CLANG_SOURCELOCATION_H 165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "clang/Basic/LLVM.h" 185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "llvm/Support/PointerLikeTypeTraits.h" 195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <utility> 205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <functional> 215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <cassert> 225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace llvm { 24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) class MemoryBuffer; 255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) class raw_ostream; 265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) template <typename T> struct DenseMapInfo; 275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) template <typename T> struct isPodLike; 285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace clang { 315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class SourceManager; 335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// FileID - This is an opaque identifier used by SourceManager which refers to 355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// a source file (MemoryBuffer) along with its #include path and #line data. 365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// 375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class FileID { 38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) /// ID - Opaque identifier, 0 is "invalid". >0 is this module, <-1 is 395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// something loaded from another module. 405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) int ID; 415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)public: 425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) FileID() : ID(0) {} 435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) bool isInvalid() const { return ID == 0; } 455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) bool operator==(const FileID &RHS) const { return ID == RHS.ID; } 475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) bool operator<(const FileID &RHS) const { return ID < RHS.ID; } 485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) bool operator<=(const FileID &RHS) const { return ID <= RHS.ID; } 495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) bool operator!=(const FileID &RHS) const { return !(*this == RHS); } 505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) bool operator>(const FileID &RHS) const { return RHS < *this; } 515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) bool operator>=(const FileID &RHS) const { return RHS <= *this; } 525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) static FileID getSentinel() { return get(-1); } 545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) unsigned getHashValue() const { return static_cast<unsigned>(ID); } 555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)private: 575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) friend class SourceManager; 585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) friend class ASTWriter; 595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) friend class ASTReader; 605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) static FileID get(int V) { 625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) FileID F; 635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) F.ID = V; 645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return F; 655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) int getOpaqueValue() const { return ID; } 675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}; 68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// \brief Encodes a location in the source. The SourceManager can decode this 715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// to get at the full include stack, line and column information. 725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// 73a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)/// Technically, a source location is simply an offset into the manager's view 745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// of the input source, which is all input buffers (including macro 755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// instantiations) concatenated in an effectively arbitrary order. The manager 765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// actually maintains two blocks of input buffers. One, starting at offset 0 775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// and growing upwards, contains all buffers from this module. The other, 785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// starting at the highest possible offset and growing downwards, contains 795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// buffers of loaded modules. 805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// 815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// In addition, one bit of SourceLocation is used for quick access to the 825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// information whether the location is in a file or a macro instantiation. 835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// 845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// It is important that this type remains small. It is currently 32 bits wide. 855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class SourceLocation { 865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) unsigned ID; 875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) friend class SourceManager; 885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) enum { 8923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) MacroIDBit = 1U << 31 9023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) }; 9123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)public: 9223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) 935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) SourceLocation() : ID(0) {} 945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) bool isFileID() const { return (ID & MacroIDBit) == 0; } 965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) bool isMacroID() const { return (ID & MacroIDBit) != 0; } 975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// \brief Return true if this is a valid SourceLocation object. 995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// 1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// Invalid SourceLocations are often used when events have no corresponding 1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// location in the source (e.g. a diagnostic is required for a command line 1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// option). 1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) bool isValid() const { return ID != 0; } 1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) bool isInvalid() const { return ID == 0; } 1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)private: 1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// \brief Return the offset into the manager's global input view. 1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) unsigned getOffset() const { 1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return ID & ~MacroIDBit; 1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) static SourceLocation getFileLoc(unsigned ID) { 1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) assert((ID & MacroIDBit) == 0 && "Ran out of source locations!"); 1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) SourceLocation L; 1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) L.ID = ID; 1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return L; 1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) static SourceLocation getMacroLoc(unsigned ID) { 1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) assert((ID & MacroIDBit) == 0 && "Ran out of source locations!"); 1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) SourceLocation L; 1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) L.ID = MacroIDBit | ID; 1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return L; 1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)public: 1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// getFileLocWithOffset - Return a source location with the specified offset 1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// from this file SourceLocation. 1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) SourceLocation getFileLocWithOffset(int Offset) const { 1305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) assert(((getOffset()+Offset) & MacroIDBit) == 0 && 1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) "offset overflow or macro loc"); 1325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) SourceLocation L; 1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) L.ID = ID+Offset; 1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return L; 1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// getRawEncoding - When a SourceLocation itself cannot be used, this returns 1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// an (opaque) 32-bit integer encoding for it. This should only be passed 1395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// to SourceLocation::getFromRawEncoding, it should not be inspected 1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// directly. 1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) unsigned getRawEncoding() const { return ID; } 1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// getFromRawEncoding - Turn a raw encoding of a SourceLocation object into 1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// a real SourceLocation. 1455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) static SourceLocation getFromRawEncoding(unsigned Encoding) { 1465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) SourceLocation X; 1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) X.ID = Encoding; 148 return X; 149 } 150 151 /// getPtrEncoding - When a SourceLocation itself cannot be used, this returns 152 /// an (opaque) pointer encoding for it. This should only be passed 153 /// to SourceLocation::getFromPtrEncoding, it should not be inspected 154 /// directly. 155 void* getPtrEncoding() const { 156 // Double cast to avoid a warning "cast to pointer from integer of different 157 // size". 158 return (void*)(uintptr_t)getRawEncoding(); 159 } 160 161 /// getFromPtrEncoding - Turn a pointer encoding of a SourceLocation object 162 /// into a real SourceLocation. 163 static SourceLocation getFromPtrEncoding(void *Encoding) { 164 return getFromRawEncoding((unsigned)(uintptr_t)Encoding); 165 } 166 167 void print(llvm::raw_ostream &OS, const SourceManager &SM) const; 168 void dump(const SourceManager &SM) const; 169}; 170 171inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) { 172 return LHS.getRawEncoding() == RHS.getRawEncoding(); 173} 174 175inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) { 176 return !(LHS == RHS); 177} 178 179inline bool operator<(const SourceLocation &LHS, const SourceLocation &RHS) { 180 return LHS.getRawEncoding() < RHS.getRawEncoding(); 181} 182 183/// SourceRange - a trival tuple used to represent a source range. 184class SourceRange { 185 SourceLocation B; 186 SourceLocation E; 187public: 188 SourceRange(): B(SourceLocation()), E(SourceLocation()) {} 189 SourceRange(SourceLocation loc) : B(loc), E(loc) {} 190 SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {} 191 192 SourceLocation getBegin() const { return B; } 193 SourceLocation getEnd() const { return E; } 194 195 void setBegin(SourceLocation b) { B = b; } 196 void setEnd(SourceLocation e) { E = e; } 197 198 bool isValid() const { return B.isValid() && E.isValid(); } 199 bool isInvalid() const { return !isValid(); } 200 201 bool operator==(const SourceRange &X) const { 202 return B == X.B && E == X.E; 203 } 204 205 bool operator!=(const SourceRange &X) const { 206 return B != X.B || E != X.E; 207 } 208}; 209 210/// CharSourceRange - This class represents a character granular source range. 211/// The underlying SourceRange can either specify the starting/ending character 212/// of the range, or it can specify the start or the range and the start of the 213/// last token of the range (a "token range"). In the token range case, the 214/// size of the last token must be measured to determine the actual end of the 215/// range. 216class CharSourceRange { 217 SourceRange Range; 218 bool IsTokenRange; 219public: 220 CharSourceRange() : IsTokenRange(false) {} 221 CharSourceRange(SourceRange R, bool ITR) : Range(R),IsTokenRange(ITR){} 222 223 static CharSourceRange getTokenRange(SourceRange R) { 224 CharSourceRange Result; 225 Result.Range = R; 226 Result.IsTokenRange = true; 227 return Result; 228 } 229 230 static CharSourceRange getCharRange(SourceRange R) { 231 CharSourceRange Result; 232 Result.Range = R; 233 Result.IsTokenRange = false; 234 return Result; 235 } 236 237 static CharSourceRange getTokenRange(SourceLocation B, SourceLocation E) { 238 return getTokenRange(SourceRange(B, E)); 239 } 240 static CharSourceRange getCharRange(SourceLocation B, SourceLocation E) { 241 return getCharRange(SourceRange(B, E)); 242 } 243 244 /// isTokenRange - Return true if the end of this range specifies the start of 245 /// the last token. Return false if the end of this range specifies the last 246 /// character in the range. 247 bool isTokenRange() const { return IsTokenRange; } 248 249 SourceLocation getBegin() const { return Range.getBegin(); } 250 SourceLocation getEnd() const { return Range.getEnd(); } 251 const SourceRange &getAsRange() const { return Range; } 252 253 void setBegin(SourceLocation b) { Range.setBegin(b); } 254 void setEnd(SourceLocation e) { Range.setEnd(e); } 255 256 bool isValid() const { return Range.isValid(); } 257 bool isInvalid() const { return !isValid(); } 258}; 259 260/// FullSourceLoc - A SourceLocation and its associated SourceManager. Useful 261/// for argument passing to functions that expect both objects. 262class FullSourceLoc : public SourceLocation { 263 const SourceManager *SrcMgr; 264public: 265 /// Creates a FullSourceLoc where isValid() returns false. 266 explicit FullSourceLoc() : SrcMgr(0) {} 267 268 explicit FullSourceLoc(SourceLocation Loc, const SourceManager &SM) 269 : SourceLocation(Loc), SrcMgr(&SM) {} 270 271 const SourceManager &getManager() const { 272 assert(SrcMgr && "SourceManager is NULL."); 273 return *SrcMgr; 274 } 275 276 FileID getFileID() const; 277 278 FullSourceLoc getInstantiationLoc() const; 279 FullSourceLoc getSpellingLoc() const; 280 281 unsigned getInstantiationLineNumber(bool *Invalid = 0) const; 282 unsigned getInstantiationColumnNumber(bool *Invalid = 0) const; 283 284 unsigned getSpellingLineNumber(bool *Invalid = 0) const; 285 unsigned getSpellingColumnNumber(bool *Invalid = 0) const; 286 287 const char *getCharacterData(bool *Invalid = 0) const; 288 289 const llvm::MemoryBuffer* getBuffer(bool *Invalid = 0) const; 290 291 /// getBufferData - Return a StringRef to the source buffer data for the 292 /// specified FileID. 293 StringRef getBufferData(bool *Invalid = 0) const; 294 295 /// getDecomposedLoc - Decompose the specified location into a raw FileID + 296 /// Offset pair. The first element is the FileID, the second is the 297 /// offset from the start of the buffer of the location. 298 std::pair<FileID, unsigned> getDecomposedLoc() const; 299 300 bool isInSystemHeader() const; 301 302 /// \brief Determines the order of 2 source locations in the translation unit. 303 /// 304 /// \returns true if this source location comes before 'Loc', false otherwise. 305 bool isBeforeInTranslationUnitThan(SourceLocation Loc) const; 306 307 /// \brief Determines the order of 2 source locations in the translation unit. 308 /// 309 /// \returns true if this source location comes before 'Loc', false otherwise. 310 bool isBeforeInTranslationUnitThan(FullSourceLoc Loc) const { 311 assert(Loc.isValid()); 312 assert(SrcMgr == Loc.SrcMgr && "Loc comes from another SourceManager!"); 313 return isBeforeInTranslationUnitThan((SourceLocation)Loc); 314 } 315 316 /// \brief Comparison function class, useful for sorting FullSourceLocs. 317 struct BeforeThanCompare : public std::binary_function<FullSourceLoc, 318 FullSourceLoc, bool> { 319 bool operator()(const FullSourceLoc& lhs, const FullSourceLoc& rhs) const { 320 return lhs.isBeforeInTranslationUnitThan(rhs); 321 } 322 }; 323 324 /// Prints information about this FullSourceLoc to stderr. Useful for 325 /// debugging. 326 void dump() const { SourceLocation::dump(*SrcMgr); } 327 328 friend inline bool 329 operator==(const FullSourceLoc &LHS, const FullSourceLoc &RHS) { 330 return LHS.getRawEncoding() == RHS.getRawEncoding() && 331 LHS.SrcMgr == RHS.SrcMgr; 332 } 333 334 friend inline bool 335 operator!=(const FullSourceLoc &LHS, const FullSourceLoc &RHS) { 336 return !(LHS == RHS); 337 } 338 339}; 340 341/// PresumedLoc - This class represents an unpacked "presumed" location which 342/// can be presented to the user. A 'presumed' location can be modified by 343/// #line and GNU line marker directives and is always the instantiation point 344/// of a normal location. 345/// 346/// You can get a PresumedLoc from a SourceLocation with SourceManager. 347class PresumedLoc { 348 const char *Filename; 349 unsigned Line, Col; 350 SourceLocation IncludeLoc; 351public: 352 PresumedLoc() : Filename(0) {} 353 PresumedLoc(const char *FN, unsigned Ln, unsigned Co, SourceLocation IL) 354 : Filename(FN), Line(Ln), Col(Co), IncludeLoc(IL) { 355 } 356 357 /// isInvalid - Return true if this object is invalid or uninitialized. This 358 /// occurs when created with invalid source locations or when walking off 359 /// the top of a #include stack. 360 bool isInvalid() const { return Filename == 0; } 361 bool isValid() const { return Filename != 0; } 362 363 /// getFilename - Return the presumed filename of this location. This can be 364 /// affected by #line etc. 365 const char *getFilename() const { return Filename; } 366 367 /// getLine - Return the presumed line number of this location. This can be 368 /// affected by #line etc. 369 unsigned getLine() const { return Line; } 370 371 /// getColumn - Return the presumed column number of this location. This can 372 /// not be affected by #line, but is packaged here for convenience. 373 unsigned getColumn() const { return Col; } 374 375 /// getIncludeLoc - Return the presumed include location of this location. 376 /// This can be affected by GNU linemarker directives. 377 SourceLocation getIncludeLoc() const { return IncludeLoc; } 378}; 379 380 381} // end namespace clang 382 383namespace llvm { 384 /// Define DenseMapInfo so that FileID's can be used as keys in DenseMap and 385 /// DenseSets. 386 template <> 387 struct DenseMapInfo<clang::FileID> { 388 static inline clang::FileID getEmptyKey() { 389 return clang::FileID(); 390 } 391 static inline clang::FileID getTombstoneKey() { 392 return clang::FileID::getSentinel(); 393 } 394 395 static unsigned getHashValue(clang::FileID S) { 396 return S.getHashValue(); 397 } 398 399 static bool isEqual(clang::FileID LHS, clang::FileID RHS) { 400 return LHS == RHS; 401 } 402 }; 403 404 template <> 405 struct isPodLike<clang::SourceLocation> { static const bool value = true; }; 406 template <> 407 struct isPodLike<clang::FileID> { static const bool value = true; }; 408 409 // Teach SmallPtrSet how to handle SourceLocation. 410 template<> 411 class PointerLikeTypeTraits<clang::SourceLocation> { 412 public: 413 static inline void *getAsVoidPointer(clang::SourceLocation L) { 414 return L.getPtrEncoding(); 415 } 416 static inline clang::SourceLocation getFromVoidPointer(void *P) { 417 return clang::SourceLocation::getFromRawEncoding((unsigned)(uintptr_t)P); 418 } 419 enum { NumLowBitsAvailable = 0 }; 420 }; 421 422} // end namespace llvm 423 424#endif 425