SourceLocation.h revision 9410928fb8434e8d26364cee45ebc1b798aafe41
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 /// \brief A mostly-opaque identifier, where 0 is "invalid", >0 is 40 /// this module, and <-1 is 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(const void *Encoding) { 170 return getFromRawEncoding((unsigned)(uintptr_t)Encoding); 171 } 172 173 void print(raw_ostream &OS, const SourceManager &SM) const; 174 LLVM_ATTRIBUTE_USED std::string printToString(const SourceManager &SM) const; 175 void dump(const SourceManager &SM) const; 176}; 177 178inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) { 179 return LHS.getRawEncoding() == RHS.getRawEncoding(); 180} 181 182inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) { 183 return !(LHS == RHS); 184} 185 186inline bool operator<(const SourceLocation &LHS, const SourceLocation &RHS) { 187 return LHS.getRawEncoding() < RHS.getRawEncoding(); 188} 189 190/// \brief A trival tuple used to represent a source range. 191class SourceRange { 192 SourceLocation B; 193 SourceLocation E; 194public: 195 SourceRange(): B(SourceLocation()), E(SourceLocation()) {} 196 SourceRange(SourceLocation loc) : B(loc), E(loc) {} 197 SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {} 198 199 SourceLocation getBegin() const { return B; } 200 SourceLocation getEnd() const { return E; } 201 202 void setBegin(SourceLocation b) { B = b; } 203 void setEnd(SourceLocation e) { E = e; } 204 205 bool isValid() const { return B.isValid() && E.isValid(); } 206 bool isInvalid() const { return !isValid(); } 207 208 bool operator==(const SourceRange &X) const { 209 return B == X.B && E == X.E; 210 } 211 212 bool operator!=(const SourceRange &X) const { 213 return B != X.B || E != X.E; 214 } 215}; 216 217/// \brief Represents a character-granular source range. 218/// 219/// The underlying SourceRange can either specify the starting/ending character 220/// of the range, or it can specify the start or the range and the start of the 221/// last token of the range (a "token range"). In the token range case, the 222/// size of the last token must be measured to determine the actual end of the 223/// range. 224class CharSourceRange { 225 SourceRange Range; 226 bool IsTokenRange; 227public: 228 CharSourceRange() : IsTokenRange(false) {} 229 CharSourceRange(SourceRange R, bool ITR) : Range(R),IsTokenRange(ITR){} 230 231 static CharSourceRange getTokenRange(SourceRange R) { 232 CharSourceRange Result; 233 Result.Range = R; 234 Result.IsTokenRange = true; 235 return Result; 236 } 237 238 static CharSourceRange getCharRange(SourceRange R) { 239 CharSourceRange Result; 240 Result.Range = R; 241 Result.IsTokenRange = false; 242 return Result; 243 } 244 245 static CharSourceRange getTokenRange(SourceLocation B, SourceLocation E) { 246 return getTokenRange(SourceRange(B, E)); 247 } 248 static CharSourceRange getCharRange(SourceLocation B, SourceLocation E) { 249 return getCharRange(SourceRange(B, E)); 250 } 251 252 /// \brief Return true if the end of this range specifies the start of 253 /// the last token. Return false if the end of this range specifies the last 254 /// character in the range. 255 bool isTokenRange() const { return IsTokenRange; } 256 bool isCharRange() const { return !IsTokenRange; } 257 258 SourceLocation getBegin() const { return Range.getBegin(); } 259 SourceLocation getEnd() const { return Range.getEnd(); } 260 const SourceRange &getAsRange() const { return Range; } 261 262 void setBegin(SourceLocation b) { Range.setBegin(b); } 263 void setEnd(SourceLocation e) { Range.setEnd(e); } 264 265 bool isValid() const { return Range.isValid(); } 266 bool isInvalid() const { return !isValid(); } 267}; 268 269/// \brief A SourceLocation and its associated SourceManager. 270/// 271/// This is useful for argument passing to functions that expect both objects. 272class FullSourceLoc : public SourceLocation { 273 const SourceManager *SrcMgr; 274public: 275 /// \brief Creates a FullSourceLoc where isValid() returns \c false. 276 explicit FullSourceLoc() : SrcMgr(0) {} 277 278 explicit FullSourceLoc(SourceLocation Loc, const SourceManager &SM) 279 : SourceLocation(Loc), SrcMgr(&SM) {} 280 281 /// \pre This FullSourceLoc has an associated SourceManager. 282 const SourceManager &getManager() const { 283 assert(SrcMgr && "SourceManager is NULL."); 284 return *SrcMgr; 285 } 286 287 FileID getFileID() const; 288 289 FullSourceLoc getExpansionLoc() const; 290 FullSourceLoc getSpellingLoc() const; 291 292 unsigned getExpansionLineNumber(bool *Invalid = 0) const; 293 unsigned getExpansionColumnNumber(bool *Invalid = 0) const; 294 295 unsigned getSpellingLineNumber(bool *Invalid = 0) const; 296 unsigned getSpellingColumnNumber(bool *Invalid = 0) const; 297 298 const char *getCharacterData(bool *Invalid = 0) const; 299 300 const llvm::MemoryBuffer* getBuffer(bool *Invalid = 0) const; 301 302 /// \brief Return a StringRef to the source buffer data for the 303 /// specified FileID. 304 StringRef getBufferData(bool *Invalid = 0) const; 305 306 /// \brief Decompose the specified location into a raw FileID + Offset pair. 307 /// 308 /// The first element is the FileID, the second is the offset from the 309 /// start of the buffer of the location. 310 std::pair<FileID, unsigned> getDecomposedLoc() const; 311 312 bool isInSystemHeader() 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(SourceLocation Loc) const; 318 319 /// \brief Determines the order of 2 source locations in the translation unit. 320 /// 321 /// \returns true if this source location comes before 'Loc', false otherwise. 322 bool isBeforeInTranslationUnitThan(FullSourceLoc Loc) const { 323 assert(Loc.isValid()); 324 assert(SrcMgr == Loc.SrcMgr && "Loc comes from another SourceManager!"); 325 return isBeforeInTranslationUnitThan((SourceLocation)Loc); 326 } 327 328 /// \brief Comparison function class, useful for sorting FullSourceLocs. 329 struct BeforeThanCompare : public std::binary_function<FullSourceLoc, 330 FullSourceLoc, bool> { 331 bool operator()(const FullSourceLoc& lhs, const FullSourceLoc& rhs) const { 332 return lhs.isBeforeInTranslationUnitThan(rhs); 333 } 334 }; 335 336 /// \brief Prints information about this FullSourceLoc to stderr. 337 /// 338 /// This is useful for debugging. 339 LLVM_ATTRIBUTE_USED void dump() const; 340 341 friend inline bool 342 operator==(const FullSourceLoc &LHS, const FullSourceLoc &RHS) { 343 return LHS.getRawEncoding() == RHS.getRawEncoding() && 344 LHS.SrcMgr == RHS.SrcMgr; 345 } 346 347 friend inline bool 348 operator!=(const FullSourceLoc &LHS, const FullSourceLoc &RHS) { 349 return !(LHS == RHS); 350 } 351 352}; 353 354/// \brief Represents an unpacked "presumed" location which can be presented 355/// to the user. 356/// 357/// A 'presumed' location can be modified by \#line and GNU line marker 358/// directives and is always the expansion point of a normal location. 359/// 360/// You can get a PresumedLoc from a SourceLocation with SourceManager. 361class PresumedLoc { 362 const char *Filename; 363 unsigned Line, Col; 364 SourceLocation IncludeLoc; 365public: 366 PresumedLoc() : Filename(0) {} 367 PresumedLoc(const char *FN, unsigned Ln, unsigned Co, SourceLocation IL) 368 : Filename(FN), Line(Ln), Col(Co), IncludeLoc(IL) { 369 } 370 371 /// \brief Return true if this object is invalid or uninitialized. 372 /// 373 /// This occurs when created with invalid source locations or when walking 374 /// off the top of a \#include stack. 375 bool isInvalid() const { return Filename == 0; } 376 bool isValid() const { return Filename != 0; } 377 378 /// \brief Return the presumed filename of this location. 379 /// 380 /// This can be affected by \#line etc. 381 const char *getFilename() const { return Filename; } 382 383 /// \brief Return the presumed line number of this location. 384 /// 385 /// This can be affected by \#line etc. 386 unsigned getLine() const { return Line; } 387 388 /// \brief Return the presumed column number of this location. 389 /// 390 /// This cannot be affected by \#line, but is packaged here for convenience. 391 unsigned getColumn() const { return Col; } 392 393 /// \brief Return the presumed include location of this location. 394 /// 395 /// This can be affected by GNU linemarker directives. 396 SourceLocation getIncludeLoc() const { return IncludeLoc; } 397}; 398 399 400} // end namespace clang 401 402namespace llvm { 403 /// Define DenseMapInfo so that FileID's can be used as keys in DenseMap and 404 /// DenseSets. 405 template <> 406 struct DenseMapInfo<clang::FileID> { 407 static inline clang::FileID getEmptyKey() { 408 return clang::FileID(); 409 } 410 static inline clang::FileID getTombstoneKey() { 411 return clang::FileID::getSentinel(); 412 } 413 414 static unsigned getHashValue(clang::FileID S) { 415 return S.getHashValue(); 416 } 417 418 static bool isEqual(clang::FileID LHS, clang::FileID RHS) { 419 return LHS == RHS; 420 } 421 }; 422 423 template <> 424 struct isPodLike<clang::SourceLocation> { static const bool value = true; }; 425 template <> 426 struct isPodLike<clang::FileID> { static const bool value = true; }; 427 428 // Teach SmallPtrSet how to handle SourceLocation. 429 template<> 430 class PointerLikeTypeTraits<clang::SourceLocation> { 431 public: 432 static inline void *getAsVoidPointer(clang::SourceLocation L) { 433 return L.getPtrEncoding(); 434 } 435 static inline clang::SourceLocation getFromVoidPointer(void *P) { 436 return clang::SourceLocation::getFromRawEncoding((unsigned)(uintptr_t)P); 437 } 438 enum { NumLowBitsAvailable = 0 }; 439 }; 440 441} // end namespace llvm 442 443#endif 444