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