1 //===- SourceLocation.h - Compact identifier for Source Files ---*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 /// Defines the clang::SourceLocation class and associated facilities.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_BASIC_SOURCELOCATION_H
15 #define LLVM_CLANG_BASIC_SOURCELOCATION_H
17 #include "clang/Basic/LLVM.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Support/PointerLikeTypeTraits.h"
27 template <typename T> struct DenseMapInfo;
35 /// An opaque identifier used by SourceManager which refers to a
36 /// source file (MemoryBuffer) along with its \#include path and \#line data.
39 /// A mostly-opaque identifier, where 0 is "invalid", >0 is
40 /// this module, and <-1 is something loaded from another module.
44 bool isValid() const { return ID != 0; }
45 bool isInvalid() const { return ID == 0; }
47 bool operator==(const FileID &RHS) const { return ID == RHS.ID; }
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 !(*this == RHS); }
51 bool operator>(const FileID &RHS) const { return RHS < *this; }
52 bool operator>=(const FileID &RHS) const { return RHS <= *this; }
54 static FileID getSentinel() { return get(-1); }
55 unsigned getHashValue() const { return static_cast<unsigned>(ID); }
58 friend class ASTWriter;
59 friend class ASTReader;
60 friend class SourceManager;
62 static FileID get(int V) {
68 int getOpaqueValue() const { return ID; }
71 /// Encodes a location in the source. The SourceManager can decode this
72 /// to get at the full include stack, line and column information.
74 /// Technically, a source location is simply an offset into the manager's view
75 /// of the input source, which is all input buffers (including macro
76 /// expansions) concatenated in an effectively arbitrary order. The manager
77 /// actually maintains two blocks of input buffers. One, starting at offset
78 /// 0 and growing upwards, contains all buffers from this module. The other,
79 /// starting at the highest possible offset and growing downwards, contains
80 /// buffers of loaded modules.
82 /// In addition, one bit of SourceLocation is used for quick access to the
83 /// information whether the location is in a file or a macro expansion.
85 /// It is important that this type remains small. It is currently 32 bits wide.
86 class SourceLocation {
87 friend class ASTReader;
88 friend class ASTWriter;
89 friend class SourceManager;
98 bool isFileID() const { return (ID & MacroIDBit) == 0; }
99 bool isMacroID() const { return (ID & MacroIDBit) != 0; }
101 /// Return true if this is a valid SourceLocation object.
103 /// Invalid SourceLocations are often used when events have no corresponding
104 /// location in the source (e.g. a diagnostic is required for a command line
106 bool isValid() const { return ID != 0; }
107 bool isInvalid() const { return ID == 0; }
110 /// Return the offset into the manager's global input view.
111 unsigned getOffset() const {
112 return ID & ~MacroIDBit;
115 static SourceLocation getFileLoc(unsigned ID) {
116 assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
122 static SourceLocation getMacroLoc(unsigned ID) {
123 assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
125 L.ID = MacroIDBit | ID;
130 /// Return a source location with the specified offset from this
132 SourceLocation getLocWithOffset(int Offset) const {
133 assert(((getOffset()+Offset) & MacroIDBit) == 0 && "offset overflow");
139 /// When a SourceLocation itself cannot be used, this returns
140 /// an (opaque) 32-bit integer encoding for it.
142 /// This should only be passed to SourceLocation::getFromRawEncoding, it
143 /// should not be inspected directly.
144 unsigned getRawEncoding() const { return ID; }
146 /// Turn a raw encoding of a SourceLocation object into
147 /// a real SourceLocation.
149 /// \see getRawEncoding.
150 static SourceLocation getFromRawEncoding(unsigned Encoding) {
156 /// When a SourceLocation itself cannot be used, this returns
157 /// an (opaque) pointer encoding for it.
159 /// This should only be passed to SourceLocation::getFromPtrEncoding, it
160 /// should not be inspected directly.
161 void* getPtrEncoding() const {
162 // Double cast to avoid a warning "cast to pointer from integer of different
164 return (void*)(uintptr_t)getRawEncoding();
167 /// Turn a pointer encoding of a SourceLocation object back
168 /// into a real SourceLocation.
169 static SourceLocation getFromPtrEncoding(const void *Encoding) {
170 return getFromRawEncoding((unsigned)(uintptr_t)Encoding);
173 static bool isPairOfFileLocations(SourceLocation Start, SourceLocation End) {
174 return Start.isValid() && Start.isFileID() && End.isValid() &&
178 void print(raw_ostream &OS, const SourceManager &SM) const;
179 std::string printToString(const SourceManager &SM) const;
180 void dump(const SourceManager &SM) const;
183 inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) {
184 return LHS.getRawEncoding() == RHS.getRawEncoding();
187 inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) {
188 return !(LHS == RHS);
191 // Ordering is meaningful only if LHS and RHS have the same FileID!
192 // Otherwise use SourceManager::isBeforeInTranslationUnit().
193 inline bool operator<(const SourceLocation &LHS, const SourceLocation &RHS) {
194 return LHS.getRawEncoding() < RHS.getRawEncoding();
196 inline bool operator>(const SourceLocation &LHS, const SourceLocation &RHS) {
197 return LHS.getRawEncoding() > RHS.getRawEncoding();
199 inline bool operator<=(const SourceLocation &LHS, const SourceLocation &RHS) {
200 return LHS.getRawEncoding() <= RHS.getRawEncoding();
202 inline bool operator>=(const SourceLocation &LHS, const SourceLocation &RHS) {
203 return LHS.getRawEncoding() >= RHS.getRawEncoding();
206 /// A trivial tuple used to represent a source range.
212 SourceRange() = default;
213 SourceRange(SourceLocation loc) : B(loc), E(loc) {}
214 SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {}
216 SourceLocation getBegin() const { return B; }
217 SourceLocation getEnd() const { return E; }
219 void setBegin(SourceLocation b) { B = b; }
220 void setEnd(SourceLocation e) { E = e; }
222 bool isValid() const { return B.isValid() && E.isValid(); }
223 bool isInvalid() const { return !isValid(); }
225 bool operator==(const SourceRange &X) const {
226 return B == X.B && E == X.E;
229 bool operator!=(const SourceRange &X) const {
230 return B != X.B || E != X.E;
233 // Returns true iff other is wholly contained within this range.
234 bool fullyContains(const SourceRange &other) const {
235 return B <= other.B && E >= other.E;
238 void print(raw_ostream &OS, const SourceManager &SM) const;
239 std::string printToString(const SourceManager &SM) const;
240 void dump(const SourceManager &SM) const;
243 /// Represents a character-granular source range.
245 /// The underlying SourceRange can either specify the starting/ending character
246 /// of the range, or it can specify the start of the range and the start of the
247 /// last token of the range (a "token range"). In the token range case, the
248 /// size of the last token must be measured to determine the actual end of the
250 class CharSourceRange {
252 bool IsTokenRange = false;
255 CharSourceRange() = default;
256 CharSourceRange(SourceRange R, bool ITR) : Range(R), IsTokenRange(ITR) {}
258 static CharSourceRange getTokenRange(SourceRange R) {
259 return CharSourceRange(R, true);
262 static CharSourceRange getCharRange(SourceRange R) {
263 return CharSourceRange(R, false);
266 static CharSourceRange getTokenRange(SourceLocation B, SourceLocation E) {
267 return getTokenRange(SourceRange(B, E));
270 static CharSourceRange getCharRange(SourceLocation B, SourceLocation E) {
271 return getCharRange(SourceRange(B, E));
274 /// Return true if the end of this range specifies the start of
275 /// the last token. Return false if the end of this range specifies the last
276 /// character in the range.
277 bool isTokenRange() const { return IsTokenRange; }
278 bool isCharRange() const { return !IsTokenRange; }
280 SourceLocation getBegin() const { return Range.getBegin(); }
281 SourceLocation getEnd() const { return Range.getEnd(); }
282 SourceRange getAsRange() const { return Range; }
284 void setBegin(SourceLocation b) { Range.setBegin(b); }
285 void setEnd(SourceLocation e) { Range.setEnd(e); }
286 void setTokenRange(bool TR) { IsTokenRange = TR; }
288 bool isValid() const { return Range.isValid(); }
289 bool isInvalid() const { return !isValid(); }
292 /// Represents an unpacked "presumed" location which can be presented
295 /// A 'presumed' location can be modified by \#line and GNU line marker
296 /// directives and is always the expansion point of a normal location.
298 /// You can get a PresumedLoc from a SourceLocation with SourceManager.
300 const char *Filename = nullptr;
303 SourceLocation IncludeLoc;
306 PresumedLoc() = default;
307 PresumedLoc(const char *FN, FileID FID, unsigned Ln, unsigned Co,
309 : Filename(FN), ID(FID), Line(Ln), Col(Co), IncludeLoc(IL) {}
311 /// Return true if this object is invalid or uninitialized.
313 /// This occurs when created with invalid source locations or when walking
314 /// off the top of a \#include stack.
315 bool isInvalid() const { return Filename == nullptr; }
316 bool isValid() const { return Filename != nullptr; }
318 /// Return the presumed filename of this location.
320 /// This can be affected by \#line etc.
321 const char *getFilename() const {
326 FileID getFileID() const {
331 /// Return the presumed line number of this location.
333 /// This can be affected by \#line etc.
334 unsigned getLine() const {
339 /// Return the presumed column number of this location.
341 /// This cannot be affected by \#line, but is packaged here for convenience.
342 unsigned getColumn() const {
347 /// Return the presumed include location of this location.
349 /// This can be affected by GNU linemarker directives.
350 SourceLocation getIncludeLoc() const {
358 /// A SourceLocation and its associated SourceManager.
360 /// This is useful for argument passing to functions that expect both objects.
361 class FullSourceLoc : public SourceLocation {
362 const SourceManager *SrcMgr = nullptr;
365 /// Creates a FullSourceLoc where isValid() returns \c false.
366 FullSourceLoc() = default;
368 explicit FullSourceLoc(SourceLocation Loc, const SourceManager &SM)
369 : SourceLocation(Loc), SrcMgr(&SM) {}
371 bool hasManager() const {
372 bool hasSrcMgr = SrcMgr != nullptr;
373 assert(hasSrcMgr == isValid() && "FullSourceLoc has location but no manager");
377 /// \pre This FullSourceLoc has an associated SourceManager.
378 const SourceManager &getManager() const {
379 assert(SrcMgr && "SourceManager is NULL.");
383 FileID getFileID() const;
385 FullSourceLoc getExpansionLoc() const;
386 FullSourceLoc getSpellingLoc() const;
387 FullSourceLoc getFileLoc() const;
388 PresumedLoc getPresumedLoc(bool UseLineDirectives = true) const;
389 bool isMacroArgExpansion(FullSourceLoc *StartLoc = nullptr) const;
390 FullSourceLoc getImmediateMacroCallerLoc() const;
391 std::pair<FullSourceLoc, StringRef> getModuleImportLoc() const;
392 unsigned getFileOffset() const;
394 unsigned getExpansionLineNumber(bool *Invalid = nullptr) const;
395 unsigned getExpansionColumnNumber(bool *Invalid = nullptr) const;
397 unsigned getSpellingLineNumber(bool *Invalid = nullptr) const;
398 unsigned getSpellingColumnNumber(bool *Invalid = nullptr) const;
400 const char *getCharacterData(bool *Invalid = nullptr) const;
402 unsigned getLineNumber(bool *Invalid = nullptr) const;
403 unsigned getColumnNumber(bool *Invalid = nullptr) const;
405 const FileEntry *getFileEntry() const;
407 /// Return a StringRef to the source buffer data for the
408 /// specified FileID.
409 StringRef getBufferData(bool *Invalid = nullptr) const;
411 /// Decompose the specified location into a raw FileID + Offset pair.
413 /// The first element is the FileID, the second is the offset from the
414 /// start of the buffer of the location.
415 std::pair<FileID, unsigned> getDecomposedLoc() const;
417 bool isInSystemHeader() const;
419 /// Determines the order of 2 source locations in the translation unit.
421 /// \returns true if this source location comes before 'Loc', false otherwise.
422 bool isBeforeInTranslationUnitThan(SourceLocation Loc) const;
424 /// Determines the order of 2 source locations in the translation unit.
426 /// \returns true if this source location comes before 'Loc', false otherwise.
427 bool isBeforeInTranslationUnitThan(FullSourceLoc Loc) const {
428 assert(Loc.isValid());
429 assert(SrcMgr == Loc.SrcMgr && "Loc comes from another SourceManager!");
430 return isBeforeInTranslationUnitThan((SourceLocation)Loc);
433 /// Comparison function class, useful for sorting FullSourceLocs.
434 struct BeforeThanCompare {
435 bool operator()(const FullSourceLoc& lhs, const FullSourceLoc& rhs) const {
436 return lhs.isBeforeInTranslationUnitThan(rhs);
440 /// Prints information about this FullSourceLoc to stderr.
442 /// This is useful for debugging.
446 operator==(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
447 return LHS.getRawEncoding() == RHS.getRawEncoding() &&
448 LHS.SrcMgr == RHS.SrcMgr;
452 operator!=(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
453 return !(LHS == RHS);
461 /// Define DenseMapInfo so that FileID's can be used as keys in DenseMap and
464 struct DenseMapInfo<clang::FileID> {
465 static clang::FileID getEmptyKey() {
469 static clang::FileID getTombstoneKey() {
470 return clang::FileID::getSentinel();
473 static unsigned getHashValue(clang::FileID S) {
474 return S.getHashValue();
477 static bool isEqual(clang::FileID LHS, clang::FileID RHS) {
482 // Teach SmallPtrSet how to handle SourceLocation.
484 struct PointerLikeTypeTraits<clang::SourceLocation> {
485 enum { NumLowBitsAvailable = 0 };
487 static void *getAsVoidPointer(clang::SourceLocation L) {
488 return L.getPtrEncoding();
491 static clang::SourceLocation getFromVoidPointer(void *P) {
492 return clang::SourceLocation::getFromRawEncoding((unsigned)(uintptr_t)P);
498 #endif // LLVM_CLANG_BASIC_SOURCELOCATION_H