SourceLocation.h revision 2f7f5b1f5ff023cb8c4008ae53a12b09e3ea2622
1//===--- SourceLocation.h - Compact identifier for Source Files -*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9/// 10/// \file 11/// \brief Defines the clang::SourceLocation class and associated facilities. 12/// 13//===----------------------------------------------------------------------===// 14 15#ifndef LLVM_CLANG_SOURCELOCATION_H 16#define LLVM_CLANG_SOURCELOCATION_H 17 18#include "clang/Basic/LLVM.h" 19#include "llvm/Support/PointerLikeTypeTraits.h" 20#include "llvm/Support/Compiler.h" 21#include <utility> 22#include <functional> 23#include <cassert> 24 25namespace llvm { 26 class MemoryBuffer; 27 template <typename T> struct DenseMapInfo; 28 template <typename T> struct isPodLike; 29} 30 31namespace clang { 32 33class SourceManager; 34 35/// \brief An opaque identifier used by SourceManager which refers to a 36/// source file (MemoryBuffer) along with its \#include path and \#line data. 37/// 38class FileID { 39 /// ID - Opaque identifier, 0 is "invalid". >0 is this module, <-1 is 40 /// something loaded from another module. 41 int ID; 42public: 43 FileID() : ID(0) {} 44 45 bool isInvalid() const { return ID == 0; } 46 47 bool operator==(const FileID &RHS) const { return ID == RHS.ID; } 48 bool operator<(const FileID &RHS) const { return ID < RHS.ID; } 49 bool operator<=(const FileID &RHS) const { return ID <= RHS.ID; } 50 bool operator!=(const FileID &RHS) const { return !(*this == RHS); } 51 bool operator>(const FileID &RHS) const { return RHS < *this; } 52 bool operator>=(const FileID &RHS) const { return RHS <= *this; } 53 54 static FileID getSentinel() { return get(-1); } 55 unsigned getHashValue() const { return static_cast<unsigned>(ID); } 56 57private: 58 friend class SourceManager; 59 friend class ASTWriter; 60 friend class ASTReader; 61 62 static FileID get(int V) { 63 FileID F; 64 F.ID = V; 65 return F; 66 } 67 int getOpaqueValue() const { return ID; } 68}; 69 70 71/// \brief Encodes a location in the source. The SourceManager can decode this 72/// to get at the full include stack, line and column information. 73/// 74/// Technically, a source location is simply an offset into the manager's view 75/// of the input source, which is all input buffers (including macro 76/// expansions) concatenated in an effectively arbitrary order. The manager 77/// actually maintains two blocks of input buffers. One, starting at offset 78/// 0 and growing upwards, contains all buffers from this module. The other, 79/// starting at the highest possible offset and growing downwards, contains 80/// buffers of loaded modules. 81/// 82/// In addition, one bit of SourceLocation is used for quick access to the 83/// information whether the location is in a file or a macro expansion. 84/// 85/// It is important that this type remains small. It is currently 32 bits wide. 86class SourceLocation { 87 unsigned ID; 88 friend class SourceManager; 89 friend class ASTReader; 90 friend class ASTWriter; 91 enum { 92 MacroIDBit = 1U << 31 93 }; 94public: 95 96 SourceLocation() : ID(0) {} 97 98 bool isFileID() const { return (ID & MacroIDBit) == 0; } 99 bool isMacroID() const { return (ID & MacroIDBit) != 0; } 100 101 /// \brief Return true if this is a valid SourceLocation object. 102 /// 103 /// Invalid SourceLocations are often used when events have no corresponding 104 /// location in the source (e.g. a diagnostic is required for a command line 105 /// option). 106 bool isValid() const { return ID != 0; } 107 bool isInvalid() const { return ID == 0; } 108 109private: 110 /// \brief Return the offset into the manager's global input view. 111 unsigned getOffset() const { 112 return ID & ~MacroIDBit; 113 } 114 115 static SourceLocation getFileLoc(unsigned ID) { 116 assert((ID & MacroIDBit) == 0 && "Ran out of source locations!"); 117 SourceLocation L; 118 L.ID = ID; 119 return L; 120 } 121 122 static SourceLocation getMacroLoc(unsigned ID) { 123 assert((ID & MacroIDBit) == 0 && "Ran out of source locations!"); 124 SourceLocation L; 125 L.ID = MacroIDBit | ID; 126 return L; 127 } 128public: 129 130 /// \brief Return a source location with the specified offset from this 131 /// SourceLocation. 132 SourceLocation getLocWithOffset(int Offset) const { 133 assert(((getOffset()+Offset) & MacroIDBit) == 0 && "offset overflow"); 134 SourceLocation L; 135 L.ID = ID+Offset; 136 return L; 137 } 138 139 /// \brief When a SourceLocation itself cannot be used, this returns 140 /// an (opaque) 32-bit integer encoding for it. 141 /// 142 /// This should only be passed to SourceLocation::getFromRawEncoding, it 143 /// should not be inspected directly. 144 unsigned getRawEncoding() const { return ID; } 145 146 /// \brief Turn a raw encoding of a SourceLocation object into 147 /// a real SourceLocation. 148 /// 149 /// \see getRawEncoding. 150 static SourceLocation getFromRawEncoding(unsigned Encoding) { 151 SourceLocation X; 152 X.ID = Encoding; 153 return X; 154 } 155 156 /// \brief When a SourceLocation itself cannot be used, this returns 157 /// an (opaque) pointer encoding for it. 158 /// 159 /// This should only be passed to SourceLocation::getFromPtrEncoding, it 160 /// should not be inspected directly. 161 void* getPtrEncoding() const { 162 // Double cast to avoid a warning "cast to pointer from integer of different 163 // size". 164 return (void*)(uintptr_t)getRawEncoding(); 165 } 166 167 /// getFromPtrEncoding - Turn a pointer encoding of a SourceLocation object 168 /// into a real SourceLocation. 169 static SourceLocation getFromPtrEncoding(void *Encoding) { 170 return getFromRawEncoding((unsigned)(uintptr_t)Encoding); 171 } 172 173 void print(raw_ostream &OS, const SourceManager &SM) const; 174 void dump(const SourceManager &SM) const; 175}; 176 177inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) { 178 return LHS.getRawEncoding() == RHS.getRawEncoding(); 179} 180 181inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) { 182 return !(LHS == RHS); 183} 184 185inline bool operator<(const SourceLocation &LHS, const SourceLocation &RHS) { 186 return LHS.getRawEncoding() < RHS.getRawEncoding(); 187} 188 189/// SourceRange - a trival tuple used to represent a source range. 190class SourceRange { 191 SourceLocation B; 192 SourceLocation E; 193public: 194 SourceRange(): B(SourceLocation()), E(SourceLocation()) {} 195 SourceRange(SourceLocation loc) : B(loc), E(loc) {} 196 SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {} 197 198 SourceLocation getBegin() const { return B; } 199 SourceLocation getEnd() const { return E; } 200 201 void setBegin(SourceLocation b) { B = b; } 202 void setEnd(SourceLocation e) { E = e; } 203 204 bool isValid() const { return B.isValid() && E.isValid(); } 205 bool isInvalid() const { return !isValid(); } 206 207 bool operator==(const SourceRange &X) const { 208 return B == X.B && E == X.E; 209 } 210 211 bool operator!=(const SourceRange &X) const { 212 return B != X.B || E != X.E; 213 } 214}; 215 216/// CharSourceRange - This class represents a character granular source range. 217/// The underlying SourceRange can either specify the starting/ending character 218/// of the range, or it can specify the start or the range and the start of the 219/// last token of the range (a "token range"). In the token range case, the 220/// size of the last token must be measured to determine the actual end of the 221/// range. 222class CharSourceRange { 223 SourceRange Range; 224 bool IsTokenRange; 225public: 226 CharSourceRange() : IsTokenRange(false) {} 227 CharSourceRange(SourceRange R, bool ITR) : Range(R),IsTokenRange(ITR){} 228 229 static CharSourceRange getTokenRange(SourceRange R) { 230 CharSourceRange Result; 231 Result.Range = R; 232 Result.IsTokenRange = true; 233 return Result; 234 } 235 236 static CharSourceRange getCharRange(SourceRange R) { 237 CharSourceRange Result; 238 Result.Range = R; 239 Result.IsTokenRange = false; 240 return Result; 241 } 242 243 static CharSourceRange getTokenRange(SourceLocation B, SourceLocation E) { 244 return getTokenRange(SourceRange(B, E)); 245 } 246 static CharSourceRange getCharRange(SourceLocation B, SourceLocation E) { 247 return getCharRange(SourceRange(B, E)); 248 } 249 250 /// isTokenRange - Return true if the end of this range specifies the start of 251 /// the last token. Return false if the end of this range specifies the last 252 /// character in the range. 253 bool isTokenRange() const { return IsTokenRange; } 254 bool isCharRange() const { return !IsTokenRange; } 255 256 SourceLocation getBegin() const { return Range.getBegin(); } 257 SourceLocation getEnd() const { return Range.getEnd(); } 258 const SourceRange &getAsRange() const { return Range; } 259 260 void setBegin(SourceLocation b) { Range.setBegin(b); } 261 void setEnd(SourceLocation e) { Range.setEnd(e); } 262 263 bool isValid() const { return Range.isValid(); } 264 bool isInvalid() const { return !isValid(); } 265}; 266 267/// FullSourceLoc - A SourceLocation and its associated SourceManager. Useful 268/// for argument passing to functions that expect both objects. 269class FullSourceLoc : public SourceLocation { 270 const SourceManager *SrcMgr; 271public: 272 /// Creates a FullSourceLoc where isValid() returns false. 273 explicit FullSourceLoc() : SrcMgr(0) {} 274 275 explicit FullSourceLoc(SourceLocation Loc, const SourceManager &SM) 276 : SourceLocation(Loc), SrcMgr(&SM) {} 277 278 const SourceManager &getManager() const { 279 assert(SrcMgr && "SourceManager is NULL."); 280 return *SrcMgr; 281 } 282 283 FileID getFileID() const; 284 285 FullSourceLoc getExpansionLoc() const; 286 FullSourceLoc getSpellingLoc() const; 287 288 unsigned getExpansionLineNumber(bool *Invalid = 0) const; 289 unsigned getExpansionColumnNumber(bool *Invalid = 0) const; 290 291 unsigned getSpellingLineNumber(bool *Invalid = 0) const; 292 unsigned getSpellingColumnNumber(bool *Invalid = 0) const; 293 294 const char *getCharacterData(bool *Invalid = 0) const; 295 296 const llvm::MemoryBuffer* getBuffer(bool *Invalid = 0) const; 297 298 /// getBufferData - Return a StringRef to the source buffer data for the 299 /// specified FileID. 300 StringRef getBufferData(bool *Invalid = 0) const; 301 302 /// getDecomposedLoc - Decompose the specified location into a raw FileID + 303 /// Offset pair. The first element is the FileID, the second is the 304 /// offset from the start of the buffer of the location. 305 std::pair<FileID, unsigned> getDecomposedLoc() const; 306 307 bool isInSystemHeader() const; 308 309 /// \brief Determines the order of 2 source locations in the translation unit. 310 /// 311 /// \returns true if this source location comes before 'Loc', false otherwise. 312 bool isBeforeInTranslationUnitThan(SourceLocation Loc) const; 313 314 /// \brief Determines the order of 2 source locations in the translation unit. 315 /// 316 /// \returns true if this source location comes before 'Loc', false otherwise. 317 bool isBeforeInTranslationUnitThan(FullSourceLoc Loc) const { 318 assert(Loc.isValid()); 319 assert(SrcMgr == Loc.SrcMgr && "Loc comes from another SourceManager!"); 320 return isBeforeInTranslationUnitThan((SourceLocation)Loc); 321 } 322 323 /// \brief Comparison function class, useful for sorting FullSourceLocs. 324 struct BeforeThanCompare : public std::binary_function<FullSourceLoc, 325 FullSourceLoc, bool> { 326 bool operator()(const FullSourceLoc& lhs, const FullSourceLoc& rhs) const { 327 return lhs.isBeforeInTranslationUnitThan(rhs); 328 } 329 }; 330 331 /// Prints information about this FullSourceLoc to stderr. Useful for 332 /// debugging. 333 LLVM_ATTRIBUTE_USED void dump() const; 334 335 friend inline bool 336 operator==(const FullSourceLoc &LHS, const FullSourceLoc &RHS) { 337 return LHS.getRawEncoding() == RHS.getRawEncoding() && 338 LHS.SrcMgr == RHS.SrcMgr; 339 } 340 341 friend inline bool 342 operator!=(const FullSourceLoc &LHS, const FullSourceLoc &RHS) { 343 return !(LHS == RHS); 344 } 345 346}; 347 348/// PresumedLoc - This class represents an unpacked "presumed" location which 349/// can be presented to the user. A 'presumed' location can be modified by 350/// \#line and GNU line marker directives and is always the expansion point of 351/// a normal location. 352/// 353/// You can get a PresumedLoc from a SourceLocation with SourceManager. 354class PresumedLoc { 355 const char *Filename; 356 unsigned Line, Col; 357 SourceLocation IncludeLoc; 358public: 359 PresumedLoc() : Filename(0) {} 360 PresumedLoc(const char *FN, unsigned Ln, unsigned Co, SourceLocation IL) 361 : Filename(FN), Line(Ln), Col(Co), IncludeLoc(IL) { 362 } 363 364 /// isInvalid - Return true if this object is invalid or uninitialized. This 365 /// occurs when created with invalid source locations or when walking off 366 /// the top of a \#include stack. 367 bool isInvalid() const { return Filename == 0; } 368 bool isValid() const { return Filename != 0; } 369 370 /// \brief Return the presumed filename of this location. 371 /// 372 /// This can be affected by \#line etc. 373 const char *getFilename() const { return Filename; } 374 375 /// \brief Return the presumed line number of this location. 376 /// 377 /// This can be affected by \#line etc. 378 unsigned getLine() const { return Line; } 379 380 /// \brief Return the presumed column number of this location. 381 /// 382 /// This cannot be affected by \#line, but is packaged here for convenience. 383 unsigned getColumn() const { return Col; } 384 385 /// \brief Return the presumed include location of this location. 386 /// 387 /// This can be affected by GNU linemarker directives. 388 SourceLocation getIncludeLoc() const { return IncludeLoc; } 389}; 390 391 392} // end namespace clang 393 394namespace llvm { 395 /// Define DenseMapInfo so that FileID's can be used as keys in DenseMap and 396 /// DenseSets. 397 template <> 398 struct DenseMapInfo<clang::FileID> { 399 static inline clang::FileID getEmptyKey() { 400 return clang::FileID(); 401 } 402 static inline clang::FileID getTombstoneKey() { 403 return clang::FileID::getSentinel(); 404 } 405 406 static unsigned getHashValue(clang::FileID S) { 407 return S.getHashValue(); 408 } 409 410 static bool isEqual(clang::FileID LHS, clang::FileID RHS) { 411 return LHS == RHS; 412 } 413 }; 414 415 template <> 416 struct isPodLike<clang::SourceLocation> { static const bool value = true; }; 417 template <> 418 struct isPodLike<clang::FileID> { static const bool value = true; }; 419 420 // Teach SmallPtrSet how to handle SourceLocation. 421 template<> 422 class PointerLikeTypeTraits<clang::SourceLocation> { 423 public: 424 static inline void *getAsVoidPointer(clang::SourceLocation L) { 425 return L.getPtrEncoding(); 426 } 427 static inline clang::SourceLocation getFromVoidPointer(void *P) { 428 return clang::SourceLocation::getFromRawEncoding((unsigned)(uintptr_t)P); 429 } 430 enum { NumLowBitsAvailable = 0 }; 431 }; 432 433} // end namespace llvm 434 435#endif 436