1 //===--- SourceLocation.h - Compact identifier for Source Files -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the SourceLocation class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_SOURCELOCATION_H
15 #define LLVM_CLANG_SOURCELOCATION_H
24 template <typename T> struct DenseMapInfo;
25 template <typename T> struct isPodLike;
33 /// FileID - This is an opaque identifier used by SourceManager which refers to
34 /// a source file (MemoryBuffer) along with its #include path and #line data.
37 /// ID - Opaque identifier, 0 is "invalid".
42 bool isInvalid() const { return ID == 0; }
44 bool operator==(const FileID &RHS) const { return ID == RHS.ID; }
45 bool operator<(const FileID &RHS) const { return ID < RHS.ID; }
46 bool operator<=(const FileID &RHS) const { return ID <= RHS.ID; }
47 bool operator!=(const FileID &RHS) const { return !(*this == RHS); }
48 bool operator>(const FileID &RHS) const { return RHS < *this; }
49 bool operator>=(const FileID &RHS) const { return RHS <= *this; }
51 static FileID getSentinel() { return get(~0U); }
52 unsigned getHashValue() const { return ID; }
55 friend class SourceManager;
56 static FileID get(unsigned V) {
61 unsigned getOpaqueValue() const { return ID; }
65 /// SourceLocation - This is a carefully crafted 32-bit identifier that encodes
66 /// a full include stack, line and column number information for a position in
67 /// an input translation unit.
68 class SourceLocation {
70 friend class SourceManager;
76 SourceLocation() : ID(0) {} // 0 is an invalid FileID.
78 bool isFileID() const { return (ID & MacroIDBit) == 0; }
79 bool isMacroID() const { return (ID & MacroIDBit) != 0; }
81 /// isValid - Return true if this is a valid SourceLocation object. Invalid
82 /// SourceLocations are often used when events have no corresponding location
83 /// in the source (e.g. a diagnostic is required for a command line option).
85 bool isValid() const { return ID != 0; }
86 bool isInvalid() const { return ID == 0; }
89 /// getOffset - Return the index for SourceManager's SLocEntryTable table,
90 /// note that this is not an index *into* it though.
91 unsigned getOffset() const {
92 return ID & ~MacroIDBit;
95 static SourceLocation getFileLoc(unsigned ID) {
96 assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
102 static SourceLocation getMacroLoc(unsigned ID) {
103 assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
105 L.ID = MacroIDBit | ID;
110 /// getFileLocWithOffset - Return a source location with the specified offset
111 /// from this file SourceLocation.
112 SourceLocation getFileLocWithOffset(int Offset) const {
113 assert(((getOffset()+Offset) & MacroIDBit) == 0 && "invalid location");
119 /// getRawEncoding - When a SourceLocation itself cannot be used, this returns
120 /// an (opaque) 32-bit integer encoding for it. This should only be passed
121 /// to SourceLocation::getFromRawEncoding, it should not be inspected
123 unsigned getRawEncoding() const { return ID; }
126 /// getFromRawEncoding - Turn a raw encoding of a SourceLocation object into
127 /// a real SourceLocation.
128 static SourceLocation getFromRawEncoding(unsigned Encoding) {
134 void print(llvm::raw_ostream &OS, const SourceManager &SM) const;
135 void dump(const SourceManager &SM) const;
138 inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) {
139 return LHS.getRawEncoding() == RHS.getRawEncoding();
142 inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) {
143 return !(LHS == RHS);
146 inline bool operator<(const SourceLocation &LHS, const SourceLocation &RHS) {
147 return LHS.getRawEncoding() < RHS.getRawEncoding();
150 /// SourceRange - a trival tuple used to represent a source range.
155 SourceRange(): B(SourceLocation()), E(SourceLocation()) {}
156 SourceRange(SourceLocation loc) : B(loc), E(loc) {}
157 SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {}
159 SourceLocation getBegin() const { return B; }
160 SourceLocation getEnd() const { return E; }
162 void setBegin(SourceLocation b) { B = b; }
163 void setEnd(SourceLocation e) { E = e; }
165 bool isValid() const { return B.isValid() && E.isValid(); }
166 bool isInvalid() const { return !isValid(); }
168 bool operator==(const SourceRange &X) const {
169 return B == X.B && E == X.E;
172 bool operator!=(const SourceRange &X) const {
173 return B != X.B || E != X.E;
177 /// FullSourceLoc - A SourceLocation and its associated SourceManager. Useful
178 /// for argument passing to functions that expect both objects.
179 class FullSourceLoc : public SourceLocation {
180 SourceManager* SrcMgr;
182 /// Creates a FullSourceLoc where isValid() returns false.
183 explicit FullSourceLoc() : SrcMgr((SourceManager*) 0) {}
185 explicit FullSourceLoc(SourceLocation Loc, SourceManager &SM)
186 : SourceLocation(Loc), SrcMgr(&SM) {}
188 SourceManager &getManager() {
189 assert(SrcMgr && "SourceManager is NULL.");
193 const SourceManager &getManager() const {
194 assert(SrcMgr && "SourceManager is NULL.");
198 FileID getFileID() const;
200 FullSourceLoc getInstantiationLoc() const;
201 FullSourceLoc getSpellingLoc() const;
203 unsigned getInstantiationLineNumber() const;
204 unsigned getInstantiationColumnNumber() const;
206 unsigned getSpellingLineNumber() const;
207 unsigned getSpellingColumnNumber() const;
209 const char *getCharacterData(bool *Invalid = 0) const;
211 const llvm::MemoryBuffer* getBuffer(bool *Invalid = 0) const;
213 /// getBufferData - Return a StringRef to the source buffer data for the
214 /// specified FileID.
215 llvm::StringRef getBufferData(bool *Invalid = 0) const;
217 /// getDecomposedLoc - Decompose the specified location into a raw FileID +
218 /// Offset pair. The first element is the FileID, the second is the
219 /// offset from the start of the buffer of the location.
220 std::pair<FileID, unsigned> getDecomposedLoc() const;
222 bool isInSystemHeader() const;
224 /// Prints information about this FullSourceLoc to stderr. Useful for
226 void dump() const { SourceLocation::dump(*SrcMgr); }
229 operator==(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
230 return LHS.getRawEncoding() == RHS.getRawEncoding() &&
231 LHS.SrcMgr == RHS.SrcMgr;
235 operator!=(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
236 return !(LHS == RHS);
241 /// PresumedLoc - This class represents an unpacked "presumed" location which
242 /// can be presented to the user. A 'presumed' location can be modified by
243 /// #line and GNU line marker directives and is always the instantiation point
244 /// of a normal location.
246 /// You can get a PresumedLoc from a SourceLocation with SourceManager.
248 const char *Filename;
250 SourceLocation IncludeLoc;
252 PresumedLoc() : Filename(0) {}
253 PresumedLoc(const char *FN, unsigned Ln, unsigned Co, SourceLocation IL)
254 : Filename(FN), Line(Ln), Col(Co), IncludeLoc(IL) {
257 /// isInvalid - Return true if this object is invalid or uninitialized. This
258 /// occurs when created with invalid source locations or when walking off
259 /// the top of a #include stack.
260 bool isInvalid() const { return Filename == 0; }
261 bool isValid() const { return Filename != 0; }
263 /// getFilename - Return the presumed filename of this location. This can be
264 /// affected by #line etc.
265 const char *getFilename() const { return Filename; }
267 /// getLine - Return the presumed line number of this location. This can be
268 /// affected by #line etc.
269 unsigned getLine() const { return Line; }
271 /// getColumn - Return the presumed column number of this location. This can
272 /// not be affected by #line, but is packaged here for convenience.
273 unsigned getColumn() const { return Col; }
275 /// getIncludeLoc - Return the presumed include location of this location.
276 /// This can be affected by GNU linemarker directives.
277 SourceLocation getIncludeLoc() const { return IncludeLoc; }
281 } // end namespace clang
284 /// Define DenseMapInfo so that FileID's can be used as keys in DenseMap and
287 struct DenseMapInfo<clang::FileID> {
288 static inline clang::FileID getEmptyKey() {
289 return clang::FileID();
291 static inline clang::FileID getTombstoneKey() {
292 return clang::FileID::getSentinel();
295 static unsigned getHashValue(clang::FileID S) {
296 return S.getHashValue();
299 static bool isEqual(clang::FileID LHS, clang::FileID RHS) {
305 struct isPodLike<clang::SourceLocation> { static const bool value = true; };
307 struct isPodLike<clang::FileID> { static const bool value = true; };
309 } // end namespace llvm