clang API Documentation
00001 //===--- SourceLocation.h - Compact identifier for Source Files -*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the SourceLocation class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_CLANG_SOURCELOCATION_H 00015 #define LLVM_CLANG_SOURCELOCATION_H 00016 00017 #include <utility> 00018 #include <cassert> 00019 00020 namespace llvm { 00021 class MemoryBuffer; 00022 class raw_ostream; 00023 class StringRef; 00024 template <typename T> struct DenseMapInfo; 00025 template <typename T> struct isPodLike; 00026 } 00027 00028 namespace clang { 00029 00030 class SourceManager; 00031 00032 /// FileID - This is an opaque identifier used by SourceManager which refers to 00033 /// a source file (MemoryBuffer) along with its #include path and #line data. 00034 /// 00035 class FileID { 00036 /// ID - Opaque identifier, 0 is "invalid". 00037 unsigned ID; 00038 public: 00039 FileID() : ID(0) {} 00040 00041 bool isInvalid() const { return ID == 0; } 00042 00043 bool operator==(const FileID &RHS) const { return ID == RHS.ID; } 00044 bool operator<(const FileID &RHS) const { return ID < RHS.ID; } 00045 bool operator<=(const FileID &RHS) const { return ID <= RHS.ID; } 00046 bool operator!=(const FileID &RHS) const { return !(*this == RHS); } 00047 bool operator>(const FileID &RHS) const { return RHS < *this; } 00048 bool operator>=(const FileID &RHS) const { return RHS <= *this; } 00049 00050 static FileID getSentinel() { return get(~0U); } 00051 unsigned getHashValue() const { return ID; } 00052 00053 private: 00054 friend class SourceManager; 00055 static FileID get(unsigned V) { 00056 FileID F; 00057 F.ID = V; 00058 return F; 00059 } 00060 unsigned getOpaqueValue() const { return ID; } 00061 }; 00062 00063 00064 /// SourceLocation - This is a carefully crafted 32-bit identifier that encodes 00065 /// a full include stack, line and column number information for a position in 00066 /// an input translation unit. 00067 class SourceLocation { 00068 unsigned ID; 00069 friend class SourceManager; 00070 enum { 00071 MacroIDBit = 1U << 31 00072 }; 00073 public: 00074 00075 SourceLocation() : ID(0) {} // 0 is an invalid FileID. 00076 00077 bool isFileID() const { return (ID & MacroIDBit) == 0; } 00078 bool isMacroID() const { return (ID & MacroIDBit) != 0; } 00079 00080 /// isValid - Return true if this is a valid SourceLocation object. Invalid 00081 /// SourceLocations are often used when events have no corresponding location 00082 /// in the source (e.g. a diagnostic is required for a command line option). 00083 /// 00084 bool isValid() const { return ID != 0; } 00085 bool isInvalid() const { return ID == 0; } 00086 00087 private: 00088 /// getOffset - Return the index for SourceManager's SLocEntryTable table, 00089 /// note that this is not an index *into* it though. 00090 unsigned getOffset() const { 00091 return ID & ~MacroIDBit; 00092 } 00093 00094 static SourceLocation getFileLoc(unsigned ID) { 00095 assert((ID & MacroIDBit) == 0 && "Ran out of source locations!"); 00096 SourceLocation L; 00097 L.ID = ID; 00098 return L; 00099 } 00100 00101 static SourceLocation getMacroLoc(unsigned ID) { 00102 assert((ID & MacroIDBit) == 0 && "Ran out of source locations!"); 00103 SourceLocation L; 00104 L.ID = MacroIDBit | ID; 00105 return L; 00106 } 00107 public: 00108 00109 /// getFileLocWithOffset - Return a source location with the specified offset 00110 /// from this file SourceLocation. 00111 SourceLocation getFileLocWithOffset(int Offset) const { 00112 assert(((getOffset()+Offset) & MacroIDBit) == 0 && "invalid location"); 00113 SourceLocation L; 00114 L.ID = ID+Offset; 00115 return L; 00116 } 00117 00118 /// getRawEncoding - When a SourceLocation itself cannot be used, this returns 00119 /// an (opaque) 32-bit integer encoding for it. This should only be passed 00120 /// to SourceLocation::getFromRawEncoding, it should not be inspected 00121 /// directly. 00122 unsigned getRawEncoding() const { return ID; } 00123 00124 00125 /// getFromRawEncoding - Turn a raw encoding of a SourceLocation object into 00126 /// a real SourceLocation. 00127 static SourceLocation getFromRawEncoding(unsigned Encoding) { 00128 SourceLocation X; 00129 X.ID = Encoding; 00130 return X; 00131 } 00132 00133 void print(llvm::raw_ostream &OS, const SourceManager &SM) const; 00134 void dump(const SourceManager &SM) const; 00135 }; 00136 00137 inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) { 00138 return LHS.getRawEncoding() == RHS.getRawEncoding(); 00139 } 00140 00141 inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) { 00142 return !(LHS == RHS); 00143 } 00144 00145 inline bool operator<(const SourceLocation &LHS, const SourceLocation &RHS) { 00146 return LHS.getRawEncoding() < RHS.getRawEncoding(); 00147 } 00148 00149 /// SourceRange - a trival tuple used to represent a source range. 00150 class SourceRange { 00151 SourceLocation B; 00152 SourceLocation E; 00153 public: 00154 SourceRange(): B(SourceLocation()), E(SourceLocation()) {} 00155 SourceRange(SourceLocation loc) : B(loc), E(loc) {} 00156 SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {} 00157 00158 SourceLocation getBegin() const { return B; } 00159 SourceLocation getEnd() const { return E; } 00160 00161 void setBegin(SourceLocation b) { B = b; } 00162 void setEnd(SourceLocation e) { E = e; } 00163 00164 bool isValid() const { return B.isValid() && E.isValid(); } 00165 bool isInvalid() const { return !isValid(); } 00166 00167 bool operator==(const SourceRange &X) const { 00168 return B == X.B && E == X.E; 00169 } 00170 00171 bool operator!=(const SourceRange &X) const { 00172 return B != X.B || E != X.E; 00173 } 00174 }; 00175 00176 /// FullSourceLoc - A SourceLocation and its associated SourceManager. Useful 00177 /// for argument passing to functions that expect both objects. 00178 class FullSourceLoc : public SourceLocation { 00179 const SourceManager *SrcMgr; 00180 public: 00181 /// Creates a FullSourceLoc where isValid() returns false. 00182 explicit FullSourceLoc() : SrcMgr(0) {} 00183 00184 explicit FullSourceLoc(SourceLocation Loc, const SourceManager &SM) 00185 : SourceLocation(Loc), SrcMgr(&SM) {} 00186 00187 const SourceManager &getManager() const { 00188 assert(SrcMgr && "SourceManager is NULL."); 00189 return *SrcMgr; 00190 } 00191 00192 FileID getFileID() const; 00193 00194 FullSourceLoc getInstantiationLoc() const; 00195 FullSourceLoc getSpellingLoc() const; 00196 00197 unsigned getInstantiationLineNumber(bool *Invalid = 0) const; 00198 unsigned getInstantiationColumnNumber(bool *Invalid = 0) const; 00199 00200 unsigned getSpellingLineNumber(bool *Invalid = 0) const; 00201 unsigned getSpellingColumnNumber(bool *Invalid = 0) const; 00202 00203 const char *getCharacterData(bool *Invalid = 0) const; 00204 00205 const llvm::MemoryBuffer* getBuffer(bool *Invalid = 0) const; 00206 00207 /// getBufferData - Return a StringRef to the source buffer data for the 00208 /// specified FileID. 00209 llvm::StringRef getBufferData(bool *Invalid = 0) const; 00210 00211 /// getDecomposedLoc - Decompose the specified location into a raw FileID + 00212 /// Offset pair. The first element is the FileID, the second is the 00213 /// offset from the start of the buffer of the location. 00214 std::pair<FileID, unsigned> getDecomposedLoc() const; 00215 00216 bool isInSystemHeader() const; 00217 00218 /// Prints information about this FullSourceLoc to stderr. Useful for 00219 /// debugging. 00220 void dump() const { SourceLocation::dump(*SrcMgr); } 00221 00222 friend inline bool 00223 operator==(const FullSourceLoc &LHS, const FullSourceLoc &RHS) { 00224 return LHS.getRawEncoding() == RHS.getRawEncoding() && 00225 LHS.SrcMgr == RHS.SrcMgr; 00226 } 00227 00228 friend inline bool 00229 operator!=(const FullSourceLoc &LHS, const FullSourceLoc &RHS) { 00230 return !(LHS == RHS); 00231 } 00232 00233 }; 00234 00235 /// PresumedLoc - This class represents an unpacked "presumed" location which 00236 /// can be presented to the user. A 'presumed' location can be modified by 00237 /// #line and GNU line marker directives and is always the instantiation point 00238 /// of a normal location. 00239 /// 00240 /// You can get a PresumedLoc from a SourceLocation with SourceManager. 00241 class PresumedLoc { 00242 const char *Filename; 00243 unsigned Line, Col; 00244 SourceLocation IncludeLoc; 00245 public: 00246 PresumedLoc() : Filename(0) {} 00247 PresumedLoc(const char *FN, unsigned Ln, unsigned Co, SourceLocation IL) 00248 : Filename(FN), Line(Ln), Col(Co), IncludeLoc(IL) { 00249 } 00250 00251 /// isInvalid - Return true if this object is invalid or uninitialized. This 00252 /// occurs when created with invalid source locations or when walking off 00253 /// the top of a #include stack. 00254 bool isInvalid() const { return Filename == 0; } 00255 bool isValid() const { return Filename != 0; } 00256 00257 /// getFilename - Return the presumed filename of this location. This can be 00258 /// affected by #line etc. 00259 const char *getFilename() const { return Filename; } 00260 00261 /// getLine - Return the presumed line number of this location. This can be 00262 /// affected by #line etc. 00263 unsigned getLine() const { return Line; } 00264 00265 /// getColumn - Return the presumed column number of this location. This can 00266 /// not be affected by #line, but is packaged here for convenience. 00267 unsigned getColumn() const { return Col; } 00268 00269 /// getIncludeLoc - Return the presumed include location of this location. 00270 /// This can be affected by GNU linemarker directives. 00271 SourceLocation getIncludeLoc() const { return IncludeLoc; } 00272 }; 00273 00274 00275 } // end namespace clang 00276 00277 namespace llvm { 00278 /// Define DenseMapInfo so that FileID's can be used as keys in DenseMap and 00279 /// DenseSets. 00280 template <> 00281 struct DenseMapInfo<clang::FileID> { 00282 static inline clang::FileID getEmptyKey() { 00283 return clang::FileID(); 00284 } 00285 static inline clang::FileID getTombstoneKey() { 00286 return clang::FileID::getSentinel(); 00287 } 00288 00289 static unsigned getHashValue(clang::FileID S) { 00290 return S.getHashValue(); 00291 } 00292 00293 static bool isEqual(clang::FileID LHS, clang::FileID RHS) { 00294 return LHS == RHS; 00295 } 00296 }; 00297 00298 template <> 00299 struct isPodLike<clang::SourceLocation> { static const bool value = true; }; 00300 template <> 00301 struct isPodLike<clang::FileID> { static const bool value = true; }; 00302 00303 } // end namespace llvm 00304 00305 #endif