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
18 #include "llvm/Bitcode/SerializationFwd.h"
29 /// SourceLocation - This is a carefully crafted 32-bit identifier that encodes
30 /// a full include stack, line and column number information for a position in
31 /// an input translation unit.
32 class SourceLocation {
37 // bit 31: 0 -> FileID, 1 -> MacroID (invalid for FileID)
38 // 30...17 -> FileID of source location, index into SourceManager table.
40 // 0...16 -> Index into the chunk of the specified FileID.
41 FilePosBits = 32-1-FileIDBits,
44 // bit 31: 1 -> MacroID, 0 -> FileID (invalid for MacroID)
48 // bits 28...9 -> MacroID number.
50 // bits 8...0 -> Macro Physical offset
51 MacroPhysOffsBits = 9,
55 ChunkSize = (1 << FilePosBits)
58 SourceLocation() : ID(0) {} // 0 is an invalid FileID.
60 bool isFileID() const { return (ID >> 31) == 0; }
61 bool isMacroID() const { return (ID >> 31) != 0; }
63 /// isValid - Return true if this is a valid SourceLocation object. Invalid
64 /// SourceLocations are often used when events have no corresponding location
65 /// in the source (e.g. a diagnostic is required for a command line option).
67 bool isValid() const { return ID != 0; }
68 bool isInvalid() const { return ID == 0; }
70 static SourceLocation getFileLoc(unsigned FileID, unsigned FilePos) {
72 // If a FilePos is larger than (1<<FilePosBits), the SourceManager makes
73 // enough consequtive FileIDs that we have one for each chunk.
74 if (FilePos >= ChunkSize) {
75 FileID += FilePos >> FilePosBits;
76 FilePos &= ChunkSize-1;
79 // FIXME: Find a way to handle out of FileID bits! Maybe MaxFileID is an
80 // escape of some sort?
81 assert(FileID < (1 << FileIDBits) && "Out of fileid's");
83 L.ID = (FileID << FilePosBits) | FilePos;
87 static bool isValidMacroPhysOffs(int Val) {
89 return Val < (1 << (MacroPhysOffsBits-1));
90 return -Val <= (1 << (MacroPhysOffsBits-1));
93 static SourceLocation getMacroLoc(unsigned MacroID, int PhysOffs){
94 assert(MacroID < (1 << MacroIDBits) && "Too many macros!");
95 assert(isValidMacroPhysOffs(PhysOffs) && "Physoffs too large!");
97 // Mask off sign bits.
98 PhysOffs &= (1 << MacroPhysOffsBits)-1;
102 (MacroID << MacroPhysOffsBits) |
108 /// getFileID - Return the file identifier for this SourceLocation. This
109 /// FileID can be used with the SourceManager object to obtain an entire
110 /// include stack for a file position reference.
111 unsigned getFileID() const {
112 assert(isFileID() && "can't get the file id of a non-file sloc!");
113 return ID >> FilePosBits;
116 /// getRawFilePos - Return the byte offset from the start of the file-chunk
117 /// referred to by FileID. This method should not be used to get the offset
118 /// from the start of the file, instead you should use
119 /// SourceManager::getDecomposedFileLoc. This method will be
120 // incorrect for large files.
121 unsigned getRawFilePos() const {
122 assert(isFileID() && "can't get the file id of a non-file sloc!");
123 return ID & (ChunkSize-1);
126 unsigned getMacroID() const {
127 assert(isMacroID() && "Is not a macro id!");
128 return (ID >> MacroPhysOffsBits) & ((1 << MacroIDBits)-1);
131 int getMacroPhysOffs() const {
132 assert(isMacroID() && "Is not a macro id!");
133 int Val = ID & ((1 << MacroPhysOffsBits)-1);
134 // Sign extend it properly.
135 unsigned ShAmt = sizeof(int)*8 - MacroPhysOffsBits;
136 return (Val << ShAmt) >> ShAmt;
139 /// getFileLocWithOffset - Return a source location with the specified offset
140 /// from this file SourceLocation.
141 SourceLocation getFileLocWithOffset(int Offset) const {
142 unsigned FileID = getFileID();
143 Offset += getRawFilePos();
144 // Handle negative offsets correctly.
149 return getFileLoc(FileID, Offset);
152 /// getRawEncoding - When a SourceLocation itself cannot be used, this returns
153 /// an (opaque) 32-bit integer encoding for it. This should only be passed
154 /// to SourceLocation::getFromRawEncoding, it should not be inspected
156 unsigned getRawEncoding() const { return ID; }
159 bool operator<(const SourceLocation &RHS) const {
163 /// getFromRawEncoding - Turn a raw encoding of a SourceLocation object into
164 /// a real SourceLocation.
165 static SourceLocation getFromRawEncoding(unsigned Encoding) {
171 /// Emit - Emit this SourceLocation object to Bitcode.
172 void Emit(llvm::Serializer& S) const;
174 /// ReadVal - Read a SourceLocation object from Bitcode.
175 static SourceLocation ReadVal(llvm::Deserializer& D);
178 inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) {
179 return LHS.getRawEncoding() == RHS.getRawEncoding();
182 inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) {
183 return !(LHS == RHS);
186 /// SourceRange - a trival tuple used to represent a source range.
191 SourceRange(): B(SourceLocation()), E(SourceLocation()) {}
192 SourceRange(SourceLocation loc) : B(loc), E(loc) {}
193 SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {}
195 SourceLocation getBegin() const { return B; }
196 SourceLocation getEnd() const { return E; }
198 void setBegin(SourceLocation b) { B = b; }
199 void setEnd(SourceLocation e) { E = e; }
201 bool isValid() const { return B.isValid() && E.isValid(); }
203 /// Emit - Emit this SourceRange object to Bitcode.
204 void Emit(llvm::Serializer& S) const;
206 /// ReadVal - Read a SourceRange object from Bitcode.
207 static SourceRange ReadVal(llvm::Deserializer& D);
210 /// FullSourceLoc - A tuple containing both a SourceLocation
211 /// and its associated SourceManager. Useful for argument passing to functions
212 /// that expect both objects.
213 class FullSourceLoc {
215 SourceManager* SrcMgr;
217 // Creates a FullSourceLoc where isValid() returns false.
218 explicit FullSourceLoc()
219 : Loc(SourceLocation()), SrcMgr((SourceManager*) 0) {}
221 explicit FullSourceLoc(SourceLocation loc, SourceManager& smgr)
222 : Loc(loc), SrcMgr(&smgr) {}
224 bool isValid() const { return Loc.isValid(); }
225 bool isInvalid() const { return Loc.isInvalid(); }
227 SourceLocation getLocation() const { return Loc; }
229 SourceManager& getManager() {
230 assert (SrcMgr && "SourceManager is NULL.");
234 const SourceManager& getManager() const {
235 assert (SrcMgr && "SourceManager is NULL.");
239 FullSourceLoc getLogicalLoc() const;
240 FullSourceLoc getPhysicalLoc() const;
241 FullSourceLoc getIncludeLoc() const;
243 unsigned getLineNumber() const;
244 unsigned getColumnNumber() const;
246 unsigned getLogicalLineNumber() const;
247 unsigned getLogicalColumnNumber() const;
249 unsigned getPhysicalLineNumber() const;
250 unsigned getPhysicalColumnNumber() const;
252 const char *getCharacterData() const;
254 const llvm::MemoryBuffer* getBuffer() const;
256 const char* getSourceName() const;
257 const FileEntry* getFileEntryForLoc() const;
259 bool isInSystemHeader() const;
261 bool isFileID() const { return Loc.isFileID(); }
263 unsigned getCanonicalFileID() const;
265 bool operator==(const FullSourceLoc& RHS) const {
266 return SrcMgr == RHS.SrcMgr && Loc == RHS.Loc;
269 bool operator!=(const FullSourceLoc& RHS) const {
270 return SrcMgr != RHS.SrcMgr || Loc != RHS.Loc;
273 /// Prints information about this FullSourceLoc to stderr. Useful for
278 } // end namespace clang