SourceLocation.h revision 4a0beb20655ae9f28d519e088c6ec8dceec59edd
1//===--- SourceLocation.h - Compact identifier for Source Files -*- 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/// \file
11/// \brief Defines the clang::SourceLocation class and associated facilities.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_SOURCELOCATION_H
16#define LLVM_CLANG_SOURCELOCATION_H
17
18#include "clang/Basic/LLVM.h"
19#include "llvm/Support/PointerLikeTypeTraits.h"
20#include "llvm/Support/Compiler.h"
21#include <utility>
22#include <functional>
23#include <cassert>
24
25namespace llvm {
26  class MemoryBuffer;
27  template <typename T> struct DenseMapInfo;
28  template <typename T> struct isPodLike;
29}
30
31namespace clang {
32
33class SourceManager;
34
35/// \brief An opaque identifier used by SourceManager which refers to a
36/// source file (MemoryBuffer) along with its \#include path and \#line data.
37///
38class FileID {
39  /// \brief A mostly-opaque identifier, where 0 is "invalid", >0 is
40  /// this module, and <-1 is something loaded from another module.
41  int ID;
42public:
43  FileID() : ID(0) {}
44
45  bool isInvalid() const { return ID == 0; }
46
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; }
53
54  static FileID getSentinel() { return get(-1); }
55  unsigned getHashValue() const { return static_cast<unsigned>(ID); }
56
57private:
58  friend class SourceManager;
59  friend class ASTWriter;
60  friend class ASTReader;
61
62  static FileID get(int V) {
63    FileID F;
64    F.ID = V;
65    return F;
66  }
67  int getOpaqueValue() const { return ID; }
68};
69
70
71/// \brief Encodes a location in the source. The SourceManager can decode this
72/// to get at the full include stack, line and column information.
73///
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.
81///
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.
84///
85/// It is important that this type remains small. It is currently 32 bits wide.
86class SourceLocation {
87  unsigned ID;
88  friend class SourceManager;
89  friend class ASTReader;
90  friend class ASTWriter;
91  enum {
92    MacroIDBit = 1U << 31
93  };
94public:
95
96  SourceLocation() : ID(0) {}
97
98  bool isFileID() const  { return (ID & MacroIDBit) == 0; }
99  bool isMacroID() const { return (ID & MacroIDBit) != 0; }
100
101  /// \brief Return true if this is a valid SourceLocation object.
102  ///
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
105  /// option).
106  bool isValid() const { return ID != 0; }
107  bool isInvalid() const { return ID == 0; }
108
109private:
110  /// \brief Return the offset into the manager's global input view.
111  unsigned getOffset() const {
112    return ID & ~MacroIDBit;
113  }
114
115  static SourceLocation getFileLoc(unsigned ID) {
116    assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
117    SourceLocation L;
118    L.ID = ID;
119    return L;
120  }
121
122  static SourceLocation getMacroLoc(unsigned ID) {
123    assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
124    SourceLocation L;
125    L.ID = MacroIDBit | ID;
126    return L;
127  }
128public:
129
130  /// \brief Return a source location with the specified offset from this
131  /// SourceLocation.
132  SourceLocation getLocWithOffset(int Offset) const {
133    assert(((getOffset()+Offset) & MacroIDBit) == 0 && "offset overflow");
134    SourceLocation L;
135    L.ID = ID+Offset;
136    return L;
137  }
138
139  /// \brief When a SourceLocation itself cannot be used, this returns
140  /// an (opaque) 32-bit integer encoding for it.
141  ///
142  /// This should only be passed to SourceLocation::getFromRawEncoding, it
143  /// should not be inspected directly.
144  unsigned getRawEncoding() const { return ID; }
145
146  /// \brief Turn a raw encoding of a SourceLocation object into
147  /// a real SourceLocation.
148  ///
149  /// \see getRawEncoding.
150  static SourceLocation getFromRawEncoding(unsigned Encoding) {
151    SourceLocation X;
152    X.ID = Encoding;
153    return X;
154  }
155
156  /// \brief When a SourceLocation itself cannot be used, this returns
157  /// an (opaque) pointer encoding for it.
158  ///
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
163    // size".
164    return (void*)(uintptr_t)getRawEncoding();
165  }
166
167  /// getFromPtrEncoding - Turn a pointer encoding of a SourceLocation object
168  /// into a real SourceLocation.
169  static SourceLocation getFromPtrEncoding(const void *Encoding) {
170    return getFromRawEncoding((unsigned)(uintptr_t)Encoding);
171  }
172
173  void print(raw_ostream &OS, const SourceManager &SM) const;
174  void dump(const SourceManager &SM) const;
175};
176
177inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) {
178  return LHS.getRawEncoding() == RHS.getRawEncoding();
179}
180
181inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) {
182  return !(LHS == RHS);
183}
184
185inline bool operator<(const SourceLocation &LHS, const SourceLocation &RHS) {
186  return LHS.getRawEncoding() < RHS.getRawEncoding();
187}
188
189/// \brief A trival tuple used to represent a source range.
190class SourceRange {
191  SourceLocation B;
192  SourceLocation E;
193public:
194  SourceRange(): B(SourceLocation()), E(SourceLocation()) {}
195  SourceRange(SourceLocation loc) : B(loc), E(loc) {}
196  SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {}
197
198  SourceLocation getBegin() const { return B; }
199  SourceLocation getEnd() const { return E; }
200
201  void setBegin(SourceLocation b) { B = b; }
202  void setEnd(SourceLocation e) { E = e; }
203
204  bool isValid() const { return B.isValid() && E.isValid(); }
205  bool isInvalid() const { return !isValid(); }
206
207  bool operator==(const SourceRange &X) const {
208    return B == X.B && E == X.E;
209  }
210
211  bool operator!=(const SourceRange &X) const {
212    return B != X.B || E != X.E;
213  }
214};
215
216/// \brief Represents a character-granular source range.
217///
218/// The underlying SourceRange can either specify the starting/ending character
219/// of the range, or it can specify the start or the range and the start of the
220/// last token of the range (a "token range").  In the token range case, the
221/// size of the last token must be measured to determine the actual end of the
222/// range.
223class CharSourceRange {
224  SourceRange Range;
225  bool IsTokenRange;
226public:
227  CharSourceRange() : IsTokenRange(false) {}
228  CharSourceRange(SourceRange R, bool ITR) : Range(R),IsTokenRange(ITR){}
229
230  static CharSourceRange getTokenRange(SourceRange R) {
231    CharSourceRange Result;
232    Result.Range = R;
233    Result.IsTokenRange = true;
234    return Result;
235  }
236
237  static CharSourceRange getCharRange(SourceRange R) {
238    CharSourceRange Result;
239    Result.Range = R;
240    Result.IsTokenRange = false;
241    return Result;
242  }
243
244  static CharSourceRange getTokenRange(SourceLocation B, SourceLocation E) {
245    return getTokenRange(SourceRange(B, E));
246  }
247  static CharSourceRange getCharRange(SourceLocation B, SourceLocation E) {
248    return getCharRange(SourceRange(B, E));
249  }
250
251  /// \brief Return true if the end of this range specifies the start of
252  /// the last token.  Return false if the end of this range specifies the last
253  /// character in the range.
254  bool isTokenRange() const { return IsTokenRange; }
255  bool isCharRange() const { return !IsTokenRange; }
256
257  SourceLocation getBegin() const { return Range.getBegin(); }
258  SourceLocation getEnd() const { return Range.getEnd(); }
259  const SourceRange &getAsRange() const { return Range; }
260
261  void setBegin(SourceLocation b) { Range.setBegin(b); }
262  void setEnd(SourceLocation e) { Range.setEnd(e); }
263
264  bool isValid() const { return Range.isValid(); }
265  bool isInvalid() const { return !isValid(); }
266};
267
268/// \brief A SourceLocation and its associated SourceManager.
269///
270/// This is useful for argument passing to functions that expect both objects.
271class FullSourceLoc : public SourceLocation {
272  const SourceManager *SrcMgr;
273public:
274  /// \brief Creates a FullSourceLoc where isValid() returns \c false.
275  explicit FullSourceLoc() : SrcMgr(0) {}
276
277  explicit FullSourceLoc(SourceLocation Loc, const SourceManager &SM)
278    : SourceLocation(Loc), SrcMgr(&SM) {}
279
280  /// \pre This FullSourceLoc has an associated SourceManager.
281  const SourceManager &getManager() const {
282    assert(SrcMgr && "SourceManager is NULL.");
283    return *SrcMgr;
284  }
285
286  FileID getFileID() const;
287
288  FullSourceLoc getExpansionLoc() const;
289  FullSourceLoc getSpellingLoc() const;
290
291  unsigned getExpansionLineNumber(bool *Invalid = 0) const;
292  unsigned getExpansionColumnNumber(bool *Invalid = 0) const;
293
294  unsigned getSpellingLineNumber(bool *Invalid = 0) const;
295  unsigned getSpellingColumnNumber(bool *Invalid = 0) const;
296
297  const char *getCharacterData(bool *Invalid = 0) const;
298
299  const llvm::MemoryBuffer* getBuffer(bool *Invalid = 0) const;
300
301  /// \brief Return a StringRef to the source buffer data for the
302  /// specified FileID.
303  StringRef getBufferData(bool *Invalid = 0) const;
304
305  /// \brief Decompose the specified location into a raw FileID + Offset pair.
306  ///
307  /// The first element is the FileID, the second is the offset from the
308  /// start of the buffer of the location.
309  std::pair<FileID, unsigned> getDecomposedLoc() const;
310
311  bool isInSystemHeader() const;
312
313  /// \brief Determines the order of 2 source locations in the translation unit.
314  ///
315  /// \returns true if this source location comes before 'Loc', false otherwise.
316  bool isBeforeInTranslationUnitThan(SourceLocation Loc) const;
317
318  /// \brief Determines the order of 2 source locations in the translation unit.
319  ///
320  /// \returns true if this source location comes before 'Loc', false otherwise.
321  bool isBeforeInTranslationUnitThan(FullSourceLoc Loc) const {
322    assert(Loc.isValid());
323    assert(SrcMgr == Loc.SrcMgr && "Loc comes from another SourceManager!");
324    return isBeforeInTranslationUnitThan((SourceLocation)Loc);
325  }
326
327  /// \brief Comparison function class, useful for sorting FullSourceLocs.
328  struct BeforeThanCompare : public std::binary_function<FullSourceLoc,
329                                                         FullSourceLoc, bool> {
330    bool operator()(const FullSourceLoc& lhs, const FullSourceLoc& rhs) const {
331      return lhs.isBeforeInTranslationUnitThan(rhs);
332    }
333  };
334
335  /// \brief Prints information about this FullSourceLoc to stderr.
336  ///
337  /// This is useful for debugging.
338  LLVM_ATTRIBUTE_USED void dump() const;
339
340  friend inline bool
341  operator==(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
342    return LHS.getRawEncoding() == RHS.getRawEncoding() &&
343          LHS.SrcMgr == RHS.SrcMgr;
344  }
345
346  friend inline bool
347  operator!=(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
348    return !(LHS == RHS);
349  }
350
351};
352
353/// \brief Represents an unpacked "presumed" location which can be presented
354/// to the user.
355///
356/// A 'presumed' location can be modified by \#line and GNU line marker
357/// directives and is always the expansion point of a normal location.
358///
359/// You can get a PresumedLoc from a SourceLocation with SourceManager.
360class PresumedLoc {
361  const char *Filename;
362  unsigned Line, Col;
363  SourceLocation IncludeLoc;
364public:
365  PresumedLoc() : Filename(0) {}
366  PresumedLoc(const char *FN, unsigned Ln, unsigned Co, SourceLocation IL)
367    : Filename(FN), Line(Ln), Col(Co), IncludeLoc(IL) {
368  }
369
370  /// \brief Return true if this object is invalid or uninitialized.
371  ///
372  /// This occurs when created with invalid source locations or when walking
373  /// off the top of a \#include stack.
374  bool isInvalid() const { return Filename == 0; }
375  bool isValid() const { return Filename != 0; }
376
377  /// \brief Return the presumed filename of this location.
378  ///
379  /// This can be affected by \#line etc.
380  const char *getFilename() const { return Filename; }
381
382  /// \brief Return the presumed line number of this location.
383  ///
384  /// This can be affected by \#line etc.
385  unsigned getLine() const { return Line; }
386
387  /// \brief Return the presumed column number of this location.
388  ///
389  /// This cannot be affected by \#line, but is packaged here for convenience.
390  unsigned getColumn() const { return Col; }
391
392  /// \brief Return the presumed include location of this location.
393  ///
394  /// This can be affected by GNU linemarker directives.
395  SourceLocation getIncludeLoc() const { return IncludeLoc; }
396};
397
398
399}  // end namespace clang
400
401namespace llvm {
402  /// Define DenseMapInfo so that FileID's can be used as keys in DenseMap and
403  /// DenseSets.
404  template <>
405  struct DenseMapInfo<clang::FileID> {
406    static inline clang::FileID getEmptyKey() {
407      return clang::FileID();
408    }
409    static inline clang::FileID getTombstoneKey() {
410      return clang::FileID::getSentinel();
411    }
412
413    static unsigned getHashValue(clang::FileID S) {
414      return S.getHashValue();
415    }
416
417    static bool isEqual(clang::FileID LHS, clang::FileID RHS) {
418      return LHS == RHS;
419    }
420  };
421
422  template <>
423  struct isPodLike<clang::SourceLocation> { static const bool value = true; };
424  template <>
425  struct isPodLike<clang::FileID> { static const bool value = true; };
426
427  // Teach SmallPtrSet how to handle SourceLocation.
428  template<>
429  class PointerLikeTypeTraits<clang::SourceLocation> {
430  public:
431    static inline void *getAsVoidPointer(clang::SourceLocation L) {
432      return L.getPtrEncoding();
433    }
434    static inline clang::SourceLocation getFromVoidPointer(void *P) {
435      return clang::SourceLocation::getFromRawEncoding((unsigned)(uintptr_t)P);
436    }
437    enum { NumLowBitsAvailable = 0 };
438  };
439
440}  // end namespace llvm
441
442#endif
443