1 //===- SMLoc.h - Source location for use with diagnostics -------*- 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 // This file declares the SMLoc class. This class encapsulates a location in 11 // source code for use in diagnostics. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_SUPPORT_SMLOC_H 16 #define LLVM_SUPPORT_SMLOC_H 17 18 #include <cassert> 19 20 namespace llvm { 21 22 /// Represents a location in source code. 23 class SMLoc { 24 const char *Ptr; 25 public: 26 SMLoc() : Ptr(0) {} 27 28 bool isValid() const { return Ptr != 0; } 29 30 bool operator==(const SMLoc &RHS) const { return RHS.Ptr == Ptr; } 31 bool operator!=(const SMLoc &RHS) const { return RHS.Ptr != Ptr; } 32 33 const char *getPointer() const { return Ptr; } 34 35 static SMLoc getFromPointer(const char *Ptr) { 36 SMLoc L; 37 L.Ptr = Ptr; 38 return L; 39 } 40 }; 41 42 /// Represents a range in source code. 43 /// 44 /// SMRange is implemented using a half-open range, as is the convention in C++. 45 /// In the string "abc", the range (1,3] represents the substring "bc", and the 46 /// range (2,2] represents an empty range between the characters "b" and "c". 47 class SMRange { 48 public: 49 SMLoc Start, End; 50 51 SMRange() {} 52 SMRange(SMLoc St, SMLoc En) : Start(St), End(En) { 53 assert(Start.isValid() == End.isValid() && 54 "Start and end should either both be valid or both be invalid!"); 55 } 56 57 bool isValid() const { return Start.isValid(); } 58 }; 59 60 } // end namespace llvm 61 62 #endif 63 64