Coverage Report

Created: 2021-09-21 08:58

/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 <cassert>
20
#include <cstdint>
21
#include <string>
22
#include <utility>
23
24
namespace llvm {
25
26
template <typename T> struct DenseMapInfo;
27
28
class FoldingSetNodeID;
29
template <typename T> struct FoldingSetTrait;
30
31
} // namespace llvm
32
33
namespace clang {
34
35
class SourceManager;
36
37
/// An opaque identifier used by SourceManager which refers to a
38
/// source file (MemoryBuffer) along with its \#include path and \#line data.
39
///
40
class FileID {
41
  /// A mostly-opaque identifier, where 0 is "invalid", >0 is
42
  /// this module, and <-1 is something loaded from another module.
43
  int ID = 0;
44
45
public:
46
6.52M
  bool isValid() const { return ID != 0; }
47
242M
  bool isInvalid() const { return ID == 0; }
48
49
898M
  bool operator==(const FileID &RHS) const { return ID == RHS.ID; }
50
2.11G
  bool operator<(const FileID &RHS) const { return ID < RHS.ID; }
51
0
  bool operator<=(const FileID &RHS) const { return ID <= RHS.ID; }
52
55.5M
  bool operator!=(const FileID &RHS) const { return !(*this == RHS); }
53
0
  bool operator>(const FileID &RHS) const { return RHS < *this; }
54
0
  bool operator>=(const FileID &RHS) const { return RHS <= *this; }
55
56
28.9M
  static FileID getSentinel() { return get(-1); }
57
28.6M
  unsigned getHashValue() const { return static_cast<unsigned>(ID); }
58
59
private:
60
  friend class ASTWriter;
61
  friend class ASTReader;
62
  friend class SourceManager;
63
64
576M
  static FileID get(int V) {
65
576M
    FileID F;
66
576M
    F.ID = V;
67
576M
    return F;
68
576M
  }
69
70
26.7k
  int getOpaqueValue() const { return ID; }
71
};
72
73
/// Encodes a location in the source. The SourceManager can decode this
74
/// to get at the full include stack, line and column information.
75
///
76
/// Technically, a source location is simply an offset into the manager's view
77
/// of the input source, which is all input buffers (including macro
78
/// expansions) concatenated in an effectively arbitrary order. The manager
79
/// actually maintains two blocks of input buffers. One, starting at offset
80
/// 0 and growing upwards, contains all buffers from this module. The other,
81
/// starting at the highest possible offset and growing downwards, contains
82
/// buffers of loaded modules.
83
///
84
/// In addition, one bit of SourceLocation is used for quick access to the
85
/// information whether the location is in a file or a macro expansion.
86
///
87
/// It is important that this type remains small. It is currently 32 bits wide.
88
class SourceLocation {
89
  friend class ASTReader;
90
  friend class ASTWriter;
91
  friend class SourceManager;
92
  friend struct llvm::FoldingSetTrait<SourceLocation>;
93
94
public:
95
  using UIntTy = uint32_t;
96
  using IntTy = int32_t;
97
98
private:
99
  UIntTy ID = 0;
100
101
  enum : UIntTy { MacroIDBit = 1ULL << (8 * sizeof(UIntTy) - 1) };
102
103
public:
104
4.25G
  bool isFileID() const  { return (ID & MacroIDBit) == 0; }
105
421M
  bool isMacroID() const { return (ID & MacroIDBit) != 0; }
106
107
  /// Return true if this is a valid SourceLocation object.
108
  ///
109
  /// Invalid SourceLocations are often used when events have no corresponding
110
  /// location in the source (e.g. a diagnostic is required for a command line
111
  /// option).
112
5.71G
  bool isValid() const { return ID != 0; }
113
1.11G
  bool isInvalid() const { return ID == 0; }
114
115
private:
116
  /// Return the offset into the manager's global input view.
117
11.0G
  UIntTy getOffset() const { return ID & ~MacroIDBit; }
118
119
70.3M
  static SourceLocation getFileLoc(UIntTy ID) {
120
70.3M
    assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
121
0
    SourceLocation L;
122
70.3M
    L.ID = ID;
123
70.3M
    return L;
124
70.3M
  }
125
126
118M
  static SourceLocation getMacroLoc(UIntTy ID) {
127
118M
    assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
128
0
    SourceLocation L;
129
118M
    L.ID = MacroIDBit | ID;
130
118M
    return L;
131
118M
  }
132
133
public:
134
  /// Return a source location with the specified offset from this
135
  /// SourceLocation.
136
3.52G
  SourceLocation getLocWithOffset(IntTy Offset) const {
137
3.52G
    assert(((getOffset()+Offset) & MacroIDBit) == 0 && "offset overflow");
138
0
    SourceLocation L;
139
3.52G
    L.ID = ID+Offset;
140
3.52G
    return L;
141
3.52G
  }
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
6.31G
  UIntTy getRawEncoding() const { return ID; }
149
150
  /// Turn a raw encoding of a SourceLocation object into
151
  /// a real SourceLocation.
152
  ///
153
  /// \see getRawEncoding.
154
5.31G
  static SourceLocation getFromRawEncoding(UIntTy Encoding) {
155
5.31G
    SourceLocation X;
156
5.31G
    X.ID = Encoding;
157
5.31G
    return X;
158
5.31G
  }
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.4k
  void* getPtrEncoding() const {
166
    // Double cast to avoid a warning "cast to pointer from integer of different
167
    // size".
168
16.4k
    return (void*)(uintptr_t)getRawEncoding();
169
16.4k
  }
170
171
  /// Turn a pointer encoding of a SourceLocation object back
172
  /// into a real SourceLocation.
173
13.5k
  static SourceLocation getFromPtrEncoding(const void *Encoding) {
174
13.5k
    return getFromRawEncoding((SourceLocation::UIntTy)(uintptr_t)Encoding);
175
13.5k
  }
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
222M
inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) {
189
222M
  return LHS.getRawEncoding() == RHS.getRawEncoding();
190
222M
}
191
192
1.92M
inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) {
193
1.92M
  return !(LHS == RHS);
194
1.92M
}
195
196
// Ordering is meaningful only if LHS and RHS have the same FileID!
197
// Otherwise use SourceManager::isBeforeInTranslationUnit().
198
2.14M
inline bool operator<(const SourceLocation &LHS, const SourceLocation &RHS) {
199
2.14M
  return LHS.getRawEncoding() < RHS.getRawEncoding();
200
2.14M
}
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
1.80G
  SourceRange() = default;
218
120M
  SourceRange(SourceLocation loc) : B(loc), E(loc) {}
219
690M
  SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {}
220
221
873M
  SourceLocation getBegin() const { return B; }
222
401M
  SourceLocation getEnd() const { return E; }
223
224
166M
  void setBegin(SourceLocation b) { B = b; }
225
493M
  void setEnd(SourceLocation e) { E = e; }
226
227
776M
  bool isValid() const { return B.isValid() && 
E.isValid()60.8M
; }
228
590M
  bool isInvalid() const { return !isValid(); }
229
230
13.1M
  bool operator==(const SourceRange &X) const {
231
13.1M
    return B == X.B && 
E == X.E13.1M
;
232
13.1M
  }
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
1.15M
  CharSourceRange() = default;
261
28.2M
  CharSourceRange(SourceRange R, bool ITR) : Range(R), IsTokenRange(ITR) {}
262
263
1.91M
  static CharSourceRange getTokenRange(SourceRange R) {
264
1.91M
    return CharSourceRange(R, true);
265
1.91M
  }
266
267
6.90M
  static CharSourceRange getCharRange(SourceRange R) {
268
6.90M
    return CharSourceRange(R, false);
269
6.90M
  }
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.88M
  static CharSourceRange getCharRange(SourceLocation B, SourceLocation E) {
276
6.88M
    return getCharRange(SourceRange(B, E));
277
6.88M
  }
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
7.36M
  bool isTokenRange() const { return IsTokenRange; }
283
6.53k
  bool isCharRange() const { return !IsTokenRange; }
284
285
39.5M
  SourceLocation getBegin() const { return Range.getBegin(); }
286
34.8M
  SourceLocation getEnd() const { return Range.getEnd(); }
287
4.18k
  SourceRange getAsRange() const { return Range; }
288
289
3.81M
  void setBegin(SourceLocation b) { Range.setBegin(b); }
290
2.95M
  void setEnd(SourceLocation e) { Range.setEnd(e); }
291
2.95M
  void setTokenRange(bool TR) { IsTokenRange = TR; }
292
293
4.60M
  bool isValid() const { return Range.isValid(); }
294
3.99M
  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
29.2k
  PresumedLoc() = default;
312
  PresumedLoc(const char *FN, FileID FID, unsigned Ln, unsigned Co,
313
              SourceLocation IL)
314
102M
      : 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
8.06M
  bool isInvalid() const { return Filename == nullptr; }
321
114M
  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
92.7M
  const char *getFilename() const {
327
92.7M
    assert(isValid());
328
0
    return Filename;
329
92.7M
  }
330
331
7.38M
  FileID getFileID() const {
332
7.38M
    assert(isValid());
333
0
    return ID;
334
7.38M
  }
335
336
  /// Return the presumed line number of this location.
337
  ///
338
  /// This can be affected by \#line etc.
339
10.0M
  unsigned getLine() const {
340
10.0M
    assert(isValid());
341
0
    return Line;
342
10.0M
  }
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
2.07M
  unsigned getColumn() const {
348
2.07M
    assert(isValid());
349
0
    return Col;
350
2.07M
  }
351
352
  /// Return the presumed include location of this location.
353
  ///
354
  /// This can be affected by GNU linemarker directives.
355
141k
  SourceLocation getIncludeLoc() const {
356
141k
    assert(isValid());
357
0
    return IncludeLoc;
358
141k
  }
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
///
367
/// This class does not guarantee the presence of either the SourceManager or
368
/// a valid SourceLocation. Clients should use `isValid()` and `hasManager()`
369
/// before calling the member functions.
370
class FullSourceLoc : public SourceLocation {
371
  const SourceManager *SrcMgr = nullptr;
372
373
public:
374
  /// Creates a FullSourceLoc where isValid() returns \c false.
375
79.8k
  FullSourceLoc() = default;
376
377
  explicit FullSourceLoc(SourceLocation Loc, const SourceManager &SM)
378
395k
      : SourceLocation(Loc), SrcMgr(&SM) {}
379
380
  /// Checks whether the SourceManager is present.
381
34.5k
  bool hasManager() const { return SrcMgr != nullptr; }
382
383
  /// \pre hasManager()
384
284k
  const SourceManager &getManager() const {
385
284k
    assert(SrcMgr && "SourceManager is NULL.");
386
0
    return *SrcMgr;
387
284k
  }
388
389
  FileID getFileID() const;
390
391
  FullSourceLoc getExpansionLoc() const;
392
  FullSourceLoc getSpellingLoc() const;
393
  FullSourceLoc getFileLoc() const;
394
  PresumedLoc getPresumedLoc(bool UseLineDirectives = true) const;
395
  bool isMacroArgExpansion(FullSourceLoc *StartLoc = nullptr) const;
396
  FullSourceLoc getImmediateMacroCallerLoc() const;
397
  std::pair<FullSourceLoc, StringRef> getModuleImportLoc() const;
398
  unsigned getFileOffset() const;
399
400
  unsigned getExpansionLineNumber(bool *Invalid = nullptr) const;
401
  unsigned getExpansionColumnNumber(bool *Invalid = nullptr) const;
402
403
  unsigned getSpellingLineNumber(bool *Invalid = nullptr) const;
404
  unsigned getSpellingColumnNumber(bool *Invalid = nullptr) const;
405
406
  const char *getCharacterData(bool *Invalid = nullptr) const;
407
408
  unsigned getLineNumber(bool *Invalid = nullptr) const;
409
  unsigned getColumnNumber(bool *Invalid = nullptr) const;
410
411
  const FileEntry *getFileEntry() const;
412
413
  /// Return a StringRef to the source buffer data for the
414
  /// specified FileID.
415
  StringRef getBufferData(bool *Invalid = nullptr) const;
416
417
  /// Decompose the specified location into a raw FileID + Offset pair.
418
  ///
419
  /// The first element is the FileID, the second is the offset from the
420
  /// start of the buffer of the location.
421
  std::pair<FileID, unsigned> getDecomposedLoc() const;
422
423
  bool isInSystemHeader() const;
424
425
  /// Determines the order of 2 source locations in the translation unit.
426
  ///
427
  /// \returns true if this source location comes before 'Loc', false otherwise.
428
  bool isBeforeInTranslationUnitThan(SourceLocation Loc) const;
429
430
  /// Determines the order of 2 source locations in the translation unit.
431
  ///
432
  /// \returns true if this source location comes before 'Loc', false otherwise.
433
151k
  bool isBeforeInTranslationUnitThan(FullSourceLoc Loc) const {
434
151k
    assert(Loc.isValid());
435
0
    assert(SrcMgr == Loc.SrcMgr && "Loc comes from another SourceManager!");
436
0
    return isBeforeInTranslationUnitThan((SourceLocation)Loc);
437
151k
  }
438
439
  /// Comparison function class, useful for sorting FullSourceLocs.
440
  struct BeforeThanCompare {
441
7.60k
    bool operator()(const FullSourceLoc& lhs, const FullSourceLoc& rhs) const {
442
7.60k
      return lhs.isBeforeInTranslationUnitThan(rhs);
443
7.60k
    }
444
  };
445
446
  /// Prints information about this FullSourceLoc to stderr.
447
  ///
448
  /// This is useful for debugging.
449
  void dump() const;
450
451
  friend bool
452
144k
  operator==(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
453
144k
    return LHS.getRawEncoding() == RHS.getRawEncoding() &&
454
144k
          
LHS.SrcMgr == RHS.SrcMgr2.73k
;
455
144k
  }
456
457
  friend bool
458
140k
  operator!=(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
459
140k
    return !(LHS == RHS);
460
140k
  }
461
};
462
463
} // namespace clang
464
465
namespace llvm {
466
467
  /// Define DenseMapInfo so that FileID's can be used as keys in DenseMap and
468
  /// DenseSets.
469
  template <>
470
  struct DenseMapInfo<clang::FileID> {
471
33.7M
    static clang::FileID getEmptyKey() {
472
33.7M
      return {};
473
33.7M
    }
474
475
28.9M
    static clang::FileID getTombstoneKey() {
476
28.9M
      return clang::FileID::getSentinel();
477
28.9M
    }
478
479
28.6M
    static unsigned getHashValue(clang::FileID S) {
480
28.6M
      return S.getHashValue();
481
28.6M
    }
482
483
339M
    static bool isEqual(clang::FileID LHS, clang::FileID RHS) {
484
339M
      return LHS == RHS;
485
339M
    }
486
  };
487
488
  /// Define DenseMapInfo so that SourceLocation's can be used as keys in
489
  /// DenseMap and DenseSet. This trait class is eqivalent to
490
  /// DenseMapInfo<unsigned> which uses SourceLocation::ID is used as a key.
491
  template <> struct DenseMapInfo<clang::SourceLocation> {
492
348k
    static clang::SourceLocation getEmptyKey() {
493
348k
      constexpr clang::SourceLocation::UIntTy Zero = 0;
494
348k
      return clang::SourceLocation::getFromRawEncoding(~Zero);
495
348k
    }
496
497
206k
    static clang::SourceLocation getTombstoneKey() {
498
206k
      constexpr clang::SourceLocation::UIntTy Zero = 0;
499
206k
      return clang::SourceLocation::getFromRawEncoding(~Zero - 1);
500
206k
    }
501
502
111k
    static unsigned getHashValue(clang::SourceLocation Loc) {
503
111k
      return Loc.getHashValue();
504
111k
    }
505
506
3.63M
    static bool isEqual(clang::SourceLocation LHS, clang::SourceLocation RHS) {
507
3.63M
      return LHS == RHS;
508
3.63M
    }
509
  };
510
511
  // Allow calling FoldingSetNodeID::Add with SourceLocation object as parameter
512
  template <> struct FoldingSetTrait<clang::SourceLocation> {
513
    static void Profile(const clang::SourceLocation &X, FoldingSetNodeID &ID);
514
  };
515
516
} // namespace llvm
517
518
#endif // LLVM_CLANG_BASIC_SOURCELOCATION_H