Coverage Report

Created: 2020-09-22 08:39

/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
} // namespace llvm
30
31
namespace clang {
32
33
class SourceManager;
34
35
/// An opaque identifier used by SourceManager which refers to a
36
/// source file (MemoryBuffer) along with its \#include path and \#line data.
37
///
38
class FileID {
39
  /// A mostly-opaque identifier, where 0 is "invalid", >0 is
40
  /// this module, and <-1 is something loaded from another module.
41
  int ID = 0;
42
43
public:
44
6.12M
  bool isValid() const { return ID != 0; }
45
166M
  bool isInvalid() const { return ID == 0; }
46
47
513M
  bool operator==(const FileID &RHS) const { return ID == RHS.ID; }
48
1.80G
  bool operator<(const FileID &RHS) const { return ID < RHS.ID; }
49
0
  bool operator<=(const FileID &RHS) const { return ID <= RHS.ID; }
50
65.7M
  bool operator!=(const FileID &RHS) const { return !(*this == RHS); }
51
0
  bool operator>(const FileID &RHS) const { return RHS < *this; }
52
0
  bool operator>=(const FileID &RHS) const { return RHS <= *this; }
53
54
11.2M
  static FileID getSentinel() { return get(-1); }
55
11.0M
  unsigned getHashValue() const { return static_cast<unsigned>(ID); }
56
57
private:
58
  friend class ASTWriter;
59
  friend class ASTReader;
60
  friend class SourceManager;
61
62
423M
  static FileID get(int V) {
63
423M
    FileID F;
64
423M
    F.ID = V;
65
423M
    return F;
66
423M
  }
67
68
30.1k
  int getOpaqueValue() const { return ID; }
69
};
70
71
/// 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.
86
class SourceLocation {
87
  friend class ASTReader;
88
  friend class ASTWriter;
89
  friend class SourceManager;
90
91
  unsigned ID = 0;
92
93
  enum : unsigned {
94
    MacroIDBit = 1U << 31
95
  };
96
97
public:
98
3.86G
  bool isFileID() const  { return (ID & MacroIDBit) == 0; }
99
434M
  bool isMacroID() const { return (ID & MacroIDBit) != 0; }
100
101
  /// 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
5.25G
  bool isValid() const { return ID != 0; }
107
732M
  bool isInvalid() const { return ID == 0; }
108
109
private:
110
  /// Return the offset into the manager's global input view.
111
11.8G
  unsigned getOffset() const {
112
11.8G
    return ID & ~MacroIDBit;
113
11.8G
  }
114
115
46.3M
  static SourceLocation getFileLoc(unsigned ID) {
116
46.3M
    assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
117
46.3M
    SourceLocation L;
118
46.3M
    L.ID = ID;
119
46.3M
    return L;
120
46.3M
  }
121
122
194M
  static SourceLocation getMacroLoc(unsigned ID) {
123
194M
    assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
124
194M
    SourceLocation L;
125
194M
    L.ID = MacroIDBit | ID;
126
194M
    return L;
127
194M
  }
128
129
public:
130
  /// Return a source location with the specified offset from this
131
  /// SourceLocation.
132
3.18G
  SourceLocation getLocWithOffset(int Offset) const {
133
3.18G
    assert(((getOffset()+Offset) & MacroIDBit) == 0 && "offset overflow");
134
3.18G
    SourceLocation L;
135
3.18G
    L.ID = ID+Offset;
136
3.18G
    return L;
137
3.18G
  }
138
139
  /// 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
6.46G
  unsigned getRawEncoding() const { return ID; }
145
146
  /// Turn a raw encoding of a SourceLocation object into
147
  /// a real SourceLocation.
148
  ///
149
  /// \see getRawEncoding.
150
5.46G
  static SourceLocation getFromRawEncoding(unsigned Encoding) {
151
5.46G
    SourceLocation X;
152
5.46G
    X.ID = Encoding;
153
5.46G
    return X;
154
5.46G
  }
155
156
  /// 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
16.3k
  void* getPtrEncoding() const {
162
    // Double cast to avoid a warning "cast to pointer from integer of different
163
    // size".
164
16.3k
    return (void*)(uintptr_t)getRawEncoding();
165
16.3k
  }
166
167
  /// Turn a pointer encoding of a SourceLocation object back
168
  /// into a real SourceLocation.
169
13.3k
  static SourceLocation getFromPtrEncoding(const void *Encoding) {
170
13.3k
    return getFromRawEncoding((unsigned)(uintptr_t)Encoding);
171
13.3k
  }
172
173
3.29k
  static bool isPairOfFileLocations(SourceLocation Start, SourceLocation End) {
174
3.29k
    return Start.isValid() && Start.isFileID() && End.isValid() &&
175
3.29k
           End.isFileID();
176
3.29k
  }
177
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;
181
};
182
183
198M
inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) {
184
198M
  return LHS.getRawEncoding() == RHS.getRawEncoding();
185
198M
}
186
187
5.78M
inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) {
188
5.78M
  return !(LHS == RHS);
189
5.78M
}
190
191
// Ordering is meaningful only if LHS and RHS have the same FileID!
192
// Otherwise use SourceManager::isBeforeInTranslationUnit().
193
1.67M
inline bool operator<(const SourceLocation &LHS, const SourceLocation &RHS) {
194
1.67M
  return LHS.getRawEncoding() < RHS.getRawEncoding();
195
1.67M
}
196
0
inline bool operator>(const SourceLocation &LHS, const SourceLocation &RHS) {
197
0
  return LHS.getRawEncoding() > RHS.getRawEncoding();
198
0
}
199
493
inline bool operator<=(const SourceLocation &LHS, const SourceLocation &RHS) {
200
493
  return LHS.getRawEncoding() <= RHS.getRawEncoding();
201
493
}
202
14
inline bool operator>=(const SourceLocation &LHS, const SourceLocation &RHS) {
203
14
  return LHS.getRawEncoding() >= RHS.getRawEncoding();
204
14
}
205
206
/// A trivial tuple used to represent a source range.
207
class SourceRange {
208
  SourceLocation B;
209
  SourceLocation E;
210
211
public:
212
997M
  SourceRange() = default;
213
55.1M
  SourceRange(SourceLocation loc) : B(loc), E(loc) {}
214
614M
  SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {}
215
216
606M
  SourceLocation getBegin() const { return B; }
217
269M
  SourceLocation getEnd() const { return E; }
218
219
124M
  void setBegin(SourceLocation b) { B = b; }
220
285M
  void setEnd(SourceLocation e) { E = e; }
221
222
426M
  bool isValid() const { return B.isValid() && 
E.isValid()59.7M
; }
223
295M
  bool isInvalid() const { return !isValid(); }
224
225
12.6M
  bool operator==(const SourceRange &X) const {
226
12.6M
    return B == X.B && 
E == X.E12.6M
;
227
12.6M
  }
228
229
7
  bool operator!=(const SourceRange &X) const {
230
7
    return B != X.B || E != X.E;
231
7
  }
232
233
  // Returns true iff other is wholly contained within this range.
234
0
  bool fullyContains(const SourceRange &other) const {
235
0
    return B <= other.B && E >= other.E;
236
0
  }
237
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;
241
};
242
243
/// Represents a character-granular source range.
244
///
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
249
/// range.
250
class CharSourceRange {
251
  SourceRange Range;
252
  bool IsTokenRange = false;
253
254
public:
255
724k
  CharSourceRange() = default;
256
40.9M
  CharSourceRange(SourceRange R, bool ITR) : Range(R), IsTokenRange(ITR) {}
257
258
1.76M
  static CharSourceRange getTokenRange(SourceRange R) {
259
1.76M
    return CharSourceRange(R, true);
260
1.76M
  }
261
262
7.68M
  static CharSourceRange getCharRange(SourceRange R) {
263
7.68M
    return CharSourceRange(R, false);
264
7.68M
  }
265
266
1.26k
  static CharSourceRange getTokenRange(SourceLocation B, SourceLocation E) {
267
1.26k
    return getTokenRange(SourceRange(B, E));
268
1.26k
  }
269
270
7.66M
  static CharSourceRange getCharRange(SourceLocation B, SourceLocation E) {
271
7.66M
    return getCharRange(SourceRange(B, E));
272
7.66M
  }
273
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
8.56M
  bool isTokenRange() const { return IsTokenRange; }
278
6.43k
  bool isCharRange() const { return !IsTokenRange; }
279
280
45.1M
  SourceLocation getBegin() const { return Range.getBegin(); }
281
48.1M
  SourceLocation getEnd() const { return Range.getEnd(); }
282
3.23k
  SourceRange getAsRange() const { return Range; }
283
284
4.96M
  void setBegin(SourceLocation b) { Range.setBegin(b); }
285
3.82M
  void setEnd(SourceLocation e) { Range.setEnd(e); }
286
3.82M
  void setTokenRange(bool TR) { IsTokenRange = TR; }
287
288
4.74M
  bool isValid() const { return Range.isValid(); }
289
4.34M
  bool isInvalid() const { return !isValid(); }
290
};
291
292
/// Represents an unpacked "presumed" location which can be presented
293
/// to the user.
294
///
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.
297
///
298
/// You can get a PresumedLoc from a SourceLocation with SourceManager.
299
class PresumedLoc {
300
  const char *Filename = nullptr;
301
  FileID ID;
302
  unsigned Line, Col;
303
  SourceLocation IncludeLoc;
304
305
public:
306
25.1k
  PresumedLoc() = default;
307
  PresumedLoc(const char *FN, FileID FID, unsigned Ln, unsigned Co,
308
              SourceLocation IL)
309
46.7M
      : Filename(FN), ID(FID), Line(Ln), Col(Co), IncludeLoc(IL) {}
310
311
  /// Return true if this object is invalid or uninitialized.
312
  ///
313
  /// This occurs when created with invalid source locations or when walking
314
  /// off the top of a \#include stack.
315
11.4M
  bool isInvalid() const { return Filename == nullptr; }
316
50.8M
  bool isValid() const { return Filename != nullptr; }
317
318
  /// Return the presumed filename of this location.
319
  ///
320
  /// This can be affected by \#line etc.
321
34.0M
  const char *getFilename() const {
322
34.0M
    assert(isValid());
323
34.0M
    return Filename;
324
34.0M
  }
325
326
60.2k
  FileID getFileID() const {
327
60.2k
    assert(isValid());
328
60.2k
    return ID;
329
60.2k
  }
330
331
  /// Return the presumed line number of this location.
332
  ///
333
  /// This can be affected by \#line etc.
334
7.84M
  unsigned getLine() const {
335
7.84M
    assert(isValid());
336
7.84M
    return Line;
337
7.84M
  }
338
339
  /// Return the presumed column number of this location.
340
  ///
341
  /// This cannot be affected by \#line, but is packaged here for convenience.
342
1.90M
  unsigned getColumn() const {
343
1.90M
    assert(isValid());
344
1.90M
    return Col;
345
1.90M
  }
346
347
  /// Return the presumed include location of this location.
348
  ///
349
  /// This can be affected by GNU linemarker directives.
350
114k
  SourceLocation getIncludeLoc() const {
351
114k
    assert(isValid());
352
114k
    return IncludeLoc;
353
114k
  }
354
};
355
356
class FileEntry;
357
358
/// A SourceLocation and its associated SourceManager.
359
///
360
/// This is useful for argument passing to functions that expect both objects.
361
class FullSourceLoc : public SourceLocation {
362
  const SourceManager *SrcMgr = nullptr;
363
364
public:
365
  /// Creates a FullSourceLoc where isValid() returns \c false.
366
77.4k
  FullSourceLoc() = default;
367
368
  explicit FullSourceLoc(SourceLocation Loc, const SourceManager &SM)
369
325k
      : SourceLocation(Loc), SrcMgr(&SM) {}
370
371
26.7k
  bool hasManager() const {
372
26.7k
      bool hasSrcMgr =  SrcMgr != nullptr;
373
26.7k
      assert(hasSrcMgr == isValid() && "FullSourceLoc has location but no manager");
374
26.7k
      return hasSrcMgr;
375
26.7k
  }
376
377
  /// \pre This FullSourceLoc has an associated SourceManager.
378
247k
  const SourceManager &getManager() const {
379
247k
    assert(SrcMgr && "SourceManager is NULL.");
380
247k
    return *SrcMgr;
381
247k
  }
382
383
  FileID getFileID() const;
384
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;
393
394
  unsigned getExpansionLineNumber(bool *Invalid = nullptr) const;
395
  unsigned getExpansionColumnNumber(bool *Invalid = nullptr) const;
396
397
  unsigned getSpellingLineNumber(bool *Invalid = nullptr) const;
398
  unsigned getSpellingColumnNumber(bool *Invalid = nullptr) const;
399
400
  const char *getCharacterData(bool *Invalid = nullptr) const;
401
402
  unsigned getLineNumber(bool *Invalid = nullptr) const;
403
  unsigned getColumnNumber(bool *Invalid = nullptr) const;
404
405
  const FileEntry *getFileEntry() const;
406
407
  /// Return a StringRef to the source buffer data for the
408
  /// specified FileID.
409
  StringRef getBufferData(bool *Invalid = nullptr) const;
410
411
  /// Decompose the specified location into a raw FileID + Offset pair.
412
  ///
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;
416
417
  bool isInSystemHeader() const;
418
419
  /// Determines the order of 2 source locations in the translation unit.
420
  ///
421
  /// \returns true if this source location comes before 'Loc', false otherwise.
422
  bool isBeforeInTranslationUnitThan(SourceLocation Loc) 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
146k
  bool isBeforeInTranslationUnitThan(FullSourceLoc Loc) const {
428
146k
    assert(Loc.isValid());
429
146k
    assert(SrcMgr == Loc.SrcMgr && "Loc comes from another SourceManager!");
430
146k
    return isBeforeInTranslationUnitThan((SourceLocation)Loc);
431
146k
  }
432
433
  /// Comparison function class, useful for sorting FullSourceLocs.
434
  struct BeforeThanCompare {
435
7.60k
    bool operator()(const FullSourceLoc& lhs, const FullSourceLoc& rhs) const {
436
7.60k
      return lhs.isBeforeInTranslationUnitThan(rhs);
437
7.60k
    }
438
  };
439
440
  /// Prints information about this FullSourceLoc to stderr.
441
  ///
442
  /// This is useful for debugging.
443
  void dump() const;
444
445
  friend bool
446
139k
  operator==(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
447
139k
    return LHS.getRawEncoding() == RHS.getRawEncoding() &&
448
2.45k
          LHS.SrcMgr == RHS.SrcMgr;
449
139k
  }
450
451
  friend bool
452
135k
  operator!=(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
453
135k
    return !(LHS == RHS);
454
135k
  }
455
};
456
457
} // namespace clang
458
459
namespace llvm {
460
461
  /// Define DenseMapInfo so that FileID's can be used as keys in DenseMap and
462
  /// DenseSets.
463
  template <>
464
  struct DenseMapInfo<clang::FileID> {
465
15.0M
    static clang::FileID getEmptyKey() {
466
15.0M
      return {};
467
15.0M
    }
468
469
11.2M
    static clang::FileID getTombstoneKey() {
470
11.2M
      return clang::FileID::getSentinel();
471
11.2M
    }
472
473
11.0M
    static unsigned getHashValue(clang::FileID S) {
474
11.0M
      return S.getHashValue();
475
11.0M
    }
476
477
126M
    static bool isEqual(clang::FileID LHS, clang::FileID RHS) {
478
126M
      return LHS == RHS;
479
126M
    }
480
  };
481
482
  // Teach SmallPtrSet how to handle SourceLocation.
483
  template<>
484
  struct PointerLikeTypeTraits<clang::SourceLocation> {
485
    static constexpr int NumLowBitsAvailable = 0;
486
487
24
    static void *getAsVoidPointer(clang::SourceLocation L) {
488
24
      return L.getPtrEncoding();
489
24
    }
490
491
8
    static clang::SourceLocation getFromVoidPointer(void *P) {
492
8
      return clang::SourceLocation::getFromRawEncoding((unsigned)(uintptr_t)P);
493
8
    }
494
  };
495
496
} // namespace llvm
497
498
#endif // LLVM_CLANG_BASIC_SOURCELOCATION_H