Coverage Report

Created: 2021-01-23 06:44

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/Basic/SourceLocation.h
Line
Count
Source (jump to first uncovered line)
1
//===- SourceLocation.h - Compact identifier for Source Files ---*- C++ -*-===//
2
//
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
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
/// \file
10
/// Defines the clang::SourceLocation class and associated facilities.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#ifndef LLVM_CLANG_BASIC_SOURCELOCATION_H
15
#define LLVM_CLANG_BASIC_SOURCELOCATION_H
16
17
#include "clang/Basic/LLVM.h"
18
#include "llvm/ADT/StringRef.h"
19
#include "llvm/Support/PointerLikeTypeTraits.h"
20
#include <cassert>
21
#include <cstdint>
22
#include <string>
23
#include <utility>
24
25
namespace llvm {
26
27
template <typename T> struct DenseMapInfo;
28
29
class FoldingSetNodeID;
30
template <typename T> struct FoldingSetTrait;
31
32
} // namespace llvm
33
34
namespace clang {
35
36
class SourceManager;
37
38
/// An opaque identifier used by SourceManager which refers to a
39
/// source file (MemoryBuffer) along with its \#include path and \#line data.
40
///
41
class FileID {
42
  /// A mostly-opaque identifier, where 0 is "invalid", >0 is
43
  /// this module, and <-1 is something loaded from another module.
44
  int ID = 0;
45
46
public:
47
6.11M
  bool isValid() const { return ID != 0; }
48
196M
  bool isInvalid() const { return ID == 0; }
49
50
759M
  bool operator==(const FileID &RHS) const { return ID == RHS.ID; }
51
2.03G
  bool operator<(const FileID &RHS) const { return ID < RHS.ID; }
52
0
  bool operator<=(const FileID &RHS) const { return ID <= RHS.ID; }
53
44.3M
  bool operator!=(const FileID &RHS) const { return !(*this == RHS); }
54
0
  bool operator>(const FileID &RHS) const { return RHS < *this; }
55
0
  bool operator>=(const FileID &RHS) const { return RHS <= *this; }
56
57
28.1M
  static FileID getSentinel() { return get(-1); }
58
27.8M
  unsigned getHashValue() const { return static_cast<unsigned>(ID); }
59
60
private:
61
  friend class ASTWriter;
62
  friend class ASTReader;
63
  friend class SourceManager;
64
65
498M
  static FileID get(int V) {
66
498M
    FileID F;
67
498M
    F.ID = V;
68
498M
    return F;
69
498M
  }
70
71
26.7k
  int getOpaqueValue() const { return ID; }
72
};
73
74
/// Encodes a location in the source. The SourceManager can decode this
75
/// to get at the full include stack, line and column information.
76
///
77
/// Technically, a source location is simply an offset into the manager's view
78
/// of the input source, which is all input buffers (including macro
79
/// expansions) concatenated in an effectively arbitrary order. The manager
80
/// actually maintains two blocks of input buffers. One, starting at offset
81
/// 0 and growing upwards, contains all buffers from this module. The other,
82
/// starting at the highest possible offset and growing downwards, contains
83
/// buffers of loaded modules.
84
///
85
/// In addition, one bit of SourceLocation is used for quick access to the
86
/// information whether the location is in a file or a macro expansion.
87
///
88
/// It is important that this type remains small. It is currently 32 bits wide.
89
class SourceLocation {
90
  friend class ASTReader;
91
  friend class ASTWriter;
92
  friend class SourceManager;
93
  friend struct llvm::FoldingSetTrait<SourceLocation>;
94
95
  unsigned ID = 0;
96
97
  enum : unsigned {
98
    MacroIDBit = 1U << 31
99
  };
100
101
public:
102
2.82G
  bool isFileID() const  { return (ID & MacroIDBit) == 0; }
103
343M
  bool isMacroID() const { return (ID & MacroIDBit) != 0; }
104
105
  /// Return true if this is a valid SourceLocation object.
106
  ///
107
  /// Invalid SourceLocations are often used when events have no corresponding
108
  /// location in the source (e.g. a diagnostic is required for a command line
109
  /// option).
110
3.98G
  bool isValid() const { return ID != 0; }
111
758M
  bool isInvalid() const { return ID == 0; }
112
113
private:
114
  /// Return the offset into the manager's global input view.
115
8.10G
  unsigned getOffset() const {
116
8.10G
    return ID & ~MacroIDBit;
117
8.10G
  }
118
119
57.3M
  static SourceLocation getFileLoc(unsigned ID) {
120
57.3M
    assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
121
57.3M
    SourceLocation L;
122
57.3M
    L.ID = ID;
123
57.3M
    return L;
124
57.3M
  }
125
126
115M
  static SourceLocation getMacroLoc(unsigned ID) {
127
115M
    assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
128
115M
    SourceLocation L;
129
115M
    L.ID = MacroIDBit | ID;
130
115M
    return L;
131
115M
  }
132
133
public:
134
  /// Return a source location with the specified offset from this
135
  /// SourceLocation.
136
2.31G
  SourceLocation getLocWithOffset(int Offset) const {
137
2.31G
    assert(((getOffset()+Offset) & MacroIDBit) == 0 && "offset overflow");
138
2.31G
    SourceLocation L;
139
2.31G
    L.ID = ID+Offset;
140
2.31G
    return L;
141
2.31G
  }
142
143
  /// When a SourceLocation itself cannot be used, this returns
144
  /// an (opaque) 32-bit integer encoding for it.
145
  ///
146
  /// This should only be passed to SourceLocation::getFromRawEncoding, it
147
  /// should not be inspected directly.
148
4.15G
  unsigned getRawEncoding() const { return ID; }
149
150
  /// Turn a raw encoding of a SourceLocation object into
151
  /// a real SourceLocation.
152
  ///
153
  /// \see getRawEncoding.
154
3.66G
  static SourceLocation getFromRawEncoding(unsigned Encoding) {
155
3.66G
    SourceLocation X;
156
3.66G
    X.ID = Encoding;
157
3.66G
    return X;
158
3.66G
  }
159
160
  /// When a SourceLocation itself cannot be used, this returns
161
  /// an (opaque) pointer encoding for it.
162
  ///
163
  /// This should only be passed to SourceLocation::getFromPtrEncoding, it
164
  /// should not be inspected directly.
165
16.3k
  void* getPtrEncoding() const {
166
    // Double cast to avoid a warning "cast to pointer from integer of different
167
    // size".
168
16.3k
    return (void*)(uintptr_t)getRawEncoding();
169
16.3k
  }
170
171
  /// Turn a pointer encoding of a SourceLocation object back
172
  /// into a real SourceLocation.
173
13.4k
  static SourceLocation getFromPtrEncoding(const void *Encoding) {
174
13.4k
    return getFromRawEncoding((unsigned)(uintptr_t)Encoding);
175
13.4k
  }
176
177
3.29k
  static bool isPairOfFileLocations(SourceLocation Start, SourceLocation End) {
178
3.29k
    return Start.isValid() && Start.isFileID() && End.isValid() &&
179
3.29k
           End.isFileID();
180
3.29k
  }
181
182
  unsigned getHashValue() const;
183
  void print(raw_ostream &OS, const SourceManager &SM) const;
184
  std::string printToString(const SourceManager &SM) const;
185
  void dump(const SourceManager &SM) const;
186
};
187
188
186M
inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) {
189
186M
  return LHS.getRawEncoding() == RHS.getRawEncoding();
190
186M
}
191
192
5.83M
inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) {
193
5.83M
  return !(LHS == RHS);
194
5.83M
}
195
196
// Ordering is meaningful only if LHS and RHS have the same FileID!
197
// Otherwise use SourceManager::isBeforeInTranslationUnit().
198
1.75M
inline bool operator<(const SourceLocation &LHS, const SourceLocation &RHS) {
199
1.75M
  return LHS.getRawEncoding() < RHS.getRawEncoding();
200
1.75M
}
201
0
inline bool operator>(const SourceLocation &LHS, const SourceLocation &RHS) {
202
0
  return LHS.getRawEncoding() > RHS.getRawEncoding();
203
0
}
204
969
inline bool operator<=(const SourceLocation &LHS, const SourceLocation &RHS) {
205
969
  return LHS.getRawEncoding() <= RHS.getRawEncoding();
206
969
}
207
14
inline bool operator>=(const SourceLocation &LHS, const SourceLocation &RHS) {
208
14
  return LHS.getRawEncoding() >= RHS.getRawEncoding();
209
14
}
210
211
/// A trivial tuple used to represent a source range.
212
class SourceRange {
213
  SourceLocation B;
214
  SourceLocation E;
215
216
public:
217
995M
  SourceRange() = default;
218
56.8M
  SourceRange(SourceLocation loc) : B(loc), E(loc) {}
219
569M
  SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {}
220
221
618M
  SourceLocation getBegin() const { return B; }
222
265M
  SourceLocation getEnd() const { return E; }
223
224
107M
  void setBegin(SourceLocation b) { B = b; }
225
269M
  void setEnd(SourceLocation e) { E = e; }
226
227
434M
  bool isValid() const { return B.isValid() && 
E.isValid()61.9M
; }
228
300M
  bool isInvalid() const { return !isValid(); }
229
230
13.2M
  bool operator==(const SourceRange &X) const {
231
13.2M
    return B == X.B && 
E == X.E13.2M
;
232
13.2M
  }
233
234
7
  bool operator!=(const SourceRange &X) const {
235
7
    return B != X.B || E != X.E;
236
7
  }
237
238
  // Returns true iff other is wholly contained within this range.
239
0
  bool fullyContains(const SourceRange &other) const {
240
0
    return B <= other.B && E >= other.E;
241
0
  }
242
243
  void print(raw_ostream &OS, const SourceManager &SM) const;
244
  std::string printToString(const SourceManager &SM) const;
245
  void dump(const SourceManager &SM) const;
246
};
247
248
/// Represents a character-granular source range.
249
///
250
/// The underlying SourceRange can either specify the starting/ending character
251
/// of the range, or it can specify the start of the range and the start of the
252
/// last token of the range (a "token range").  In the token range case, the
253
/// size of the last token must be measured to determine the actual end of the
254
/// range.
255
class CharSourceRange {
256
  SourceRange Range;
257
  bool IsTokenRange = false;
258
259
public:
260
742k
  CharSourceRange() = default;
261
31.5M
  CharSourceRange(SourceRange R, bool ITR) : Range(R), IsTokenRange(ITR) {}
262
263
1.82M
  static CharSourceRange getTokenRange(SourceRange R) {
264
1.82M
    return CharSourceRange(R, true);
265
1.82M
  }
266
267
6.75M
  static CharSourceRange getCharRange(SourceRange R) {
268
6.75M
    return CharSourceRange(R, false);
269
6.75M
  }
270
271
1.36k
  static CharSourceRange getTokenRange(SourceLocation B, SourceLocation E) {
272
1.36k
    return getTokenRange(SourceRange(B, E));
273
1.36k
  }
274
275
6.72M
  static CharSourceRange getCharRange(SourceLocation B, SourceLocation E) {
276
6.72M
    return getCharRange(SourceRange(B, E));
277
6.72M
  }
278
279
  /// Return true if the end of this range specifies the start of
280
  /// the last token.  Return false if the end of this range specifies the last
281
  /// character in the range.
282
8.67M
  bool isTokenRange() const { return IsTokenRange; }
283
6.43k
  bool isCharRange() const { return !IsTokenRange; }
284
285
44.2M
  SourceLocation getBegin() const { return Range.getBegin(); }
286
40.2M
  SourceLocation getEnd() const { return Range.getEnd(); }
287
4.11k
  SourceRange getAsRange() const { return Range; }
288
289
4.97M
  void setBegin(SourceLocation b) { Range.setBegin(b); }
290
3.84M
  void setEnd(SourceLocation e) { Range.setEnd(e); }
291
3.84M
  void setTokenRange(bool TR) { IsTokenRange = TR; }
292
293
4.83M
  bool isValid() const { return Range.isValid(); }
294
4.42M
  bool isInvalid() const { return !isValid(); }
295
};
296
297
/// Represents an unpacked "presumed" location which can be presented
298
/// to the user.
299
///
300
/// A 'presumed' location can be modified by \#line and GNU line marker
301
/// directives and is always the expansion point of a normal location.
302
///
303
/// You can get a PresumedLoc from a SourceLocation with SourceManager.
304
class PresumedLoc {
305
  const char *Filename = nullptr;
306
  FileID ID;
307
  unsigned Line, Col;
308
  SourceLocation IncludeLoc;
309
310
public:
311
26.2k
  PresumedLoc() = default;
312
  PresumedLoc(const char *FN, FileID FID, unsigned Ln, unsigned Co,
313
              SourceLocation IL)
314
61.9M
      : Filename(FN), ID(FID), Line(Ln), Col(Co), IncludeLoc(IL) {}
315
316
  /// Return true if this object is invalid or uninitialized.
317
  ///
318
  /// This occurs when created with invalid source locations or when walking
319
  /// off the top of a \#include stack.
320
6.10M
  bool isInvalid() const { return Filename == nullptr; }
321
76.8M
  bool isValid() const { return Filename != nullptr; }
322
323
  /// Return the presumed filename of this location.
324
  ///
325
  /// This can be affected by \#line etc.
326
54.5M
  const char *getFilename() const {
327
54.5M
    assert(isValid());
328
54.5M
    return Filename;
329
54.5M
  }
330
331
5.41M
  FileID getFileID() const {
332
5.41M
    assert(isValid());
333
5.41M
    return ID;
334
5.41M
  }
335
336
  /// Return the presumed line number of this location.
337
  ///
338
  /// This can be affected by \#line etc.
339
7.89M
  unsigned getLine() const {
340
7.89M
    assert(isValid());
341
7.89M
    return Line;
342
7.89M
  }
343
344
  /// Return the presumed column number of this location.
345
  ///
346
  /// This cannot be affected by \#line, but is packaged here for convenience.
347
1.92M
  unsigned getColumn() const {
348
1.92M
    assert(isValid());
349
1.92M
    return Col;
350
1.92M
  }
351
352
  /// Return the presumed include location of this location.
353
  ///
354
  /// This can be affected by GNU linemarker directives.
355
115k
  SourceLocation getIncludeLoc() const {
356
115k
    assert(isValid());
357
115k
    return IncludeLoc;
358
115k
  }
359
};
360
361
class FileEntry;
362
363
/// A SourceLocation and its associated SourceManager.
364
///
365
/// This is useful for argument passing to functions that expect both objects.
366
class FullSourceLoc : public SourceLocation {
367
  const SourceManager *SrcMgr = nullptr;
368
369
public:
370
  /// Creates a FullSourceLoc where isValid() returns \c false.
371
77.4k
  FullSourceLoc() = default;
372
373
  explicit FullSourceLoc(SourceLocation Loc, const SourceManager &SM)
374
330k
      : SourceLocation(Loc), SrcMgr(&SM) {}
375
376
27.6k
  bool hasManager() const {
377
27.6k
      bool hasSrcMgr =  SrcMgr != nullptr;
378
27.6k
      assert(hasSrcMgr == isValid() && "FullSourceLoc has location but no manager");
379
27.6k
      return hasSrcMgr;
380
27.6k
  }
381
382
  /// \pre This FullSourceLoc has an associated SourceManager.
383
252k
  const SourceManager &getManager() const {
384
252k
    assert(SrcMgr && "SourceManager is NULL.");
385
252k
    return *SrcMgr;
386
252k
  }
387
388
  FileID getFileID() const;
389
390
  FullSourceLoc getExpansionLoc() const;
391
  FullSourceLoc getSpellingLoc() const;
392
  FullSourceLoc getFileLoc() const;
393
  PresumedLoc getPresumedLoc(bool UseLineDirectives = true) const;
394
  bool isMacroArgExpansion(FullSourceLoc *StartLoc = nullptr) const;
395
  FullSourceLoc getImmediateMacroCallerLoc() const;
396
  std::pair<FullSourceLoc, StringRef> getModuleImportLoc() const;
397
  unsigned getFileOffset() const;
398
399
  unsigned getExpansionLineNumber(bool *Invalid = nullptr) const;
400
  unsigned getExpansionColumnNumber(bool *Invalid = nullptr) const;
401
402
  unsigned getSpellingLineNumber(bool *Invalid = nullptr) const;
403
  unsigned getSpellingColumnNumber(bool *Invalid = nullptr) const;
404
405
  const char *getCharacterData(bool *Invalid = nullptr) const;
406
407
  unsigned getLineNumber(bool *Invalid = nullptr) const;
408
  unsigned getColumnNumber(bool *Invalid = nullptr) const;
409
410
  const FileEntry *getFileEntry() const;
411
412
  /// Return a StringRef to the source buffer data for the
413
  /// specified FileID.
414
  StringRef getBufferData(bool *Invalid = nullptr) const;
415
416
  /// Decompose the specified location into a raw FileID + Offset pair.
417
  ///
418
  /// The first element is the FileID, the second is the offset from the
419
  /// start of the buffer of the location.
420
  std::pair<FileID, unsigned> getDecomposedLoc() const;
421
422
  bool isInSystemHeader() const;
423
424
  /// Determines the order of 2 source locations in the translation unit.
425
  ///
426
  /// \returns true if this source location comes before 'Loc', false otherwise.
427
  bool isBeforeInTranslationUnitThan(SourceLocation Loc) const;
428
429
  /// Determines the order of 2 source locations in the translation unit.
430
  ///
431
  /// \returns true if this source location comes before 'Loc', false otherwise.
432
146k
  bool isBeforeInTranslationUnitThan(FullSourceLoc Loc) const {
433
146k
    assert(Loc.isValid());
434
146k
    assert(SrcMgr == Loc.SrcMgr && "Loc comes from another SourceManager!");
435
146k
    return isBeforeInTranslationUnitThan((SourceLocation)Loc);
436
146k
  }
437
438
  /// Comparison function class, useful for sorting FullSourceLocs.
439
  struct BeforeThanCompare {
440
7.60k
    bool operator()(const FullSourceLoc& lhs, const FullSourceLoc& rhs) const {
441
7.60k
      return lhs.isBeforeInTranslationUnitThan(rhs);
442
7.60k
    }
443
  };
444
445
  /// Prints information about this FullSourceLoc to stderr.
446
  ///
447
  /// This is useful for debugging.
448
  void dump() const;
449
450
  friend bool
451
140k
  operator==(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
452
140k
    return LHS.getRawEncoding() == RHS.getRawEncoding() &&
453
2.44k
          LHS.SrcMgr == RHS.SrcMgr;
454
140k
  }
455
456
  friend bool
457
136k
  operator!=(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
458
136k
    return !(LHS == RHS);
459
136k
  }
460
};
461
462
} // namespace clang
463
464
namespace llvm {
465
466
  /// Define DenseMapInfo so that FileID's can be used as keys in DenseMap and
467
  /// DenseSets.
468
  template <>
469
  struct DenseMapInfo<clang::FileID> {
470
32.7M
    static clang::FileID getEmptyKey() {
471
32.7M
      return {};
472
32.7M
    }
473
474
28.1M
    static clang::FileID getTombstoneKey() {
475
28.1M
      return clang::FileID::getSentinel();
476
28.1M
    }
477
478
27.8M
    static unsigned getHashValue(clang::FileID S) {
479
27.8M
      return S.getHashValue();
480
27.8M
    }
481
482
363M
    static bool isEqual(clang::FileID LHS, clang::FileID RHS) {
483
363M
      return LHS == RHS;
484
363M
    }
485
  };
486
487
  /// Define DenseMapInfo so that SourceLocation's can be used as keys in
488
  /// DenseMap and DenseSet. This trait class is eqivalent to
489
  /// DenseMapInfo<unsigned> which uses SourceLocation::ID is used as a key.
490
  template <> struct DenseMapInfo<clang::SourceLocation> {
491
153k
    static clang::SourceLocation getEmptyKey() {
492
153k
      return clang::SourceLocation::getFromRawEncoding(~0U);
493
153k
    }
494
495
111k
    static clang::SourceLocation getTombstoneKey() {
496
111k
      return clang::SourceLocation::getFromRawEncoding(~0U - 1);
497
111k
    }
498
499
108k
    static unsigned getHashValue(clang::SourceLocation Loc) {
500
108k
      return Loc.getHashValue();
501
108k
    }
502
503
655k
    static bool isEqual(clang::SourceLocation LHS, clang::SourceLocation RHS) {
504
655k
      return LHS == RHS;
505
655k
    }
506
  };
507
508
  // Allow calling FoldingSetNodeID::Add with SourceLocation object as parameter
509
  template <> struct FoldingSetTrait<clang::SourceLocation> {
510
    static void Profile(const clang::SourceLocation &X, FoldingSetNodeID &ID);
511
  };
512
513
  // Teach SmallPtrSet how to handle SourceLocation.
514
  template<>
515
  struct PointerLikeTypeTraits<clang::SourceLocation> {
516
    static constexpr int NumLowBitsAvailable = 0;
517
518
24
    static void *getAsVoidPointer(clang::SourceLocation L) {
519
24
      return L.getPtrEncoding();
520
24
    }
521
522
8
    static clang::SourceLocation getFromVoidPointer(void *P) {
523
8
      return clang::SourceLocation::getFromRawEncoding((unsigned)(uintptr_t)P);
524
8
    }
525
  };
526
527
} // namespace llvm
528
529
#endif // LLVM_CLANG_BASIC_SOURCELOCATION_H