Coverage Report

Created: 2019-02-23 12:57

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/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
0
class FileID {
Unexecuted instantiation: clang::FileID::operator=(clang::FileID const&)
Unexecuted instantiation: clang::FileID::operator=(clang::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
180k
  bool isValid() const { return ID != 0; }
45
101M
  bool isInvalid() const { return ID == 0; }
46
47
152M
  bool operator==(const FileID &RHS) const { return ID == RHS.ID; }
48
135M
  bool operator<(const FileID &RHS) const { return ID < RHS.ID; }
49
  bool operator<=(const FileID &RHS) const { return ID <= RHS.ID; }
50
7.06M
  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
5.10M
  static FileID getSentinel() { return get(-1); }
55
4.90M
  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
263M
  static FileID get(int V) {
63
263M
    FileID F;
64
263M
    F.ID = V;
65
263M
    return F;
66
263M
  }
67
68
11.7k
  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
0
class SourceLocation {
Unexecuted instantiation: clang::SourceLocation::operator=(clang::SourceLocation const&)
Unexecuted instantiation: clang::SourceLocation::operator=(clang::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
1.61G
  bool isFileID() const  { return (ID & MacroIDBit) == 0; }
99
39.1M
  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
1.10G
  bool isValid() const { return ID != 0; }
107
294M
  bool isInvalid() const { return ID == 0; }
108
109
private:
110
  /// Return the offset into the manager's global input view.
111
777M
  unsigned getOffset() const {
112
777M
    return ID & ~MacroIDBit;
113
777M
  }
114
115
2.50M
  static SourceLocation getFileLoc(unsigned ID) {
116
2.50M
    assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
117
2.50M
    SourceLocation L;
118
2.50M
    L.ID = ID;
119
2.50M
    return L;
120
2.50M
  }
121
122
27.0M
  static SourceLocation getMacroLoc(unsigned ID) {
123
27.0M
    assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
124
27.0M
    SourceLocation L;
125
27.0M
    L.ID = MacroIDBit | ID;
126
27.0M
    return L;
127
27.0M
  }
128
129
public:
130
  /// Return a source location with the specified offset from this
131
  /// SourceLocation.
132
1.54G
  SourceLocation getLocWithOffset(int Offset) const {
133
1.54G
    assert(((getOffset()+Offset) & MacroIDBit) == 0 && "offset overflow");
134
1.54G
    SourceLocation L;
135
1.54G
    L.ID = ID+Offset;
136
1.54G
    return L;
137
1.54G
  }
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
3.31G
  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
1.44G
  static SourceLocation getFromRawEncoding(unsigned Encoding) {
151
1.44G
    SourceLocation X;
152
1.44G
    X.ID = Encoding;
153
1.44G
    return X;
154
1.44G
  }
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
13.3k
  void* getPtrEncoding() const {
162
13.3k
    // Double cast to avoid a warning "cast to pointer from integer of different
163
13.3k
    // size".
164
13.3k
    return (void*)(uintptr_t)getRawEncoding();
165
13.3k
  }
166
167
  /// Turn a pointer encoding of a SourceLocation object back
168
  /// into a real SourceLocation.
169
12.7k
  static SourceLocation getFromPtrEncoding(const void *Encoding) {
170
12.7k
    return getFromRawEncoding((unsigned)(uintptr_t)Encoding);
171
12.7k
  }
172
173
  static bool isPairOfFileLocations(SourceLocation Start, SourceLocation End) {
174
    return Start.isValid() && Start.isFileID() && End.isValid() &&
175
           End.isFileID();
176
  }
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
103M
inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) {
184
103M
  return LHS.getRawEncoding() == RHS.getRawEncoding();
185
103M
}
186
187
332k
inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) {
188
332k
  return !(LHS == RHS);
189
332k
}
190
191
1.57M
inline bool operator<(const SourceLocation &LHS, const SourceLocation &RHS) {
192
1.57M
  return LHS.getRawEncoding() < RHS.getRawEncoding();
193
1.57M
}
194
195
/// A trivial tuple used to represent a source range.
196
0
class SourceRange {
Unexecuted instantiation: clang::SourceRange::operator=(clang::SourceRange const&)
Unexecuted instantiation: clang::SourceRange::operator=(clang::SourceRange&&)
197
  SourceLocation B;
198
  SourceLocation E;
199
200
public:
201
516M
  SourceRange() = default;
202
23.1M
  SourceRange(SourceLocation loc) : B(loc), E(loc) {}
203
363M
  SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {}
204
205
383M
  SourceLocation getBegin() const { return B; }
206
158M
  SourceLocation getEnd() const { return E; }
207
208
54.1M
  void setBegin(SourceLocation b) { B = b; }
209
128M
  void setEnd(SourceLocation e) { E = e; }
210
211
290M
  bool isValid() const { return B.isValid() && 
E.isValid()36.9M
; }
212
64.8M
  bool isInvalid() const { return !isValid(); }
213
214
11.6k
  bool operator==(const SourceRange &X) const {
215
11.6k
    return B == X.B && 
E == X.E10.2k
;
216
11.6k
  }
217
218
4
  bool operator!=(const SourceRange &X) const {
219
4
    return B != X.B || E != X.E;
220
4
  }
221
222
  void print(raw_ostream &OS, const SourceManager &SM) const;
223
  std::string printToString(const SourceManager &SM) const;
224
  void dump(const SourceManager &SM) const;
225
};
226
227
/// Represents a character-granular source range.
228
///
229
/// The underlying SourceRange can either specify the starting/ending character
230
/// of the range, or it can specify the start of the range and the start of the
231
/// last token of the range (a "token range").  In the token range case, the
232
/// size of the last token must be measured to determine the actual end of the
233
/// range.
234
0
class CharSourceRange {
Unexecuted instantiation: clang::CharSourceRange::operator=(clang::CharSourceRange const&)
Unexecuted instantiation: clang::CharSourceRange::operator=(clang::CharSourceRange&&)
235
  SourceRange Range;
236
  bool IsTokenRange = false;
237
238
public:
239
12.9M
  CharSourceRange() = default;
240
6.30M
  CharSourceRange(SourceRange R, bool ITR) : Range(R), IsTokenRange(ITR) {}
241
242
1.78M
  static CharSourceRange getTokenRange(SourceRange R) {
243
1.78M
    return CharSourceRange(R, true);
244
1.78M
  }
245
246
2.54M
  static CharSourceRange getCharRange(SourceRange R) {
247
2.54M
    return CharSourceRange(R, false);
248
2.54M
  }
249
250
1.34k
  static CharSourceRange getTokenRange(SourceLocation B, SourceLocation E) {
251
1.34k
    return getTokenRange(SourceRange(B, E));
252
1.34k
  }
253
254
2.53M
  static CharSourceRange getCharRange(SourceLocation B, SourceLocation E) {
255
2.53M
    return getCharRange(SourceRange(B, E));
256
2.53M
  }
257
258
  /// Return true if the end of this range specifies the start of
259
  /// the last token.  Return false if the end of this range specifies the last
260
  /// character in the range.
261
511k
  bool isTokenRange() const { return IsTokenRange; }
262
147
  bool isCharRange() const { return !IsTokenRange; }
263
264
4.46M
  SourceLocation getBegin() const { return Range.getBegin(); }
265
4.95M
  SourceLocation getEnd() const { return Range.getEnd(); }
266
3.01k
  SourceRange getAsRange() const { return Range; }
267
268
117k
  void setBegin(SourceLocation b) { Range.setBegin(b); }
269
117k
  void setEnd(SourceLocation e) { Range.setEnd(e); }
270
117k
  void setTokenRange(bool TR) { IsTokenRange = TR; }
271
272
236k
  bool isValid() const { return Range.isValid(); }
273
25.6k
  bool isInvalid() const { return !isValid(); }
274
};
275
276
/// Represents an unpacked "presumed" location which can be presented
277
/// to the user.
278
///
279
/// A 'presumed' location can be modified by \#line and GNU line marker
280
/// directives and is always the expansion point of a normal location.
281
///
282
/// You can get a PresumedLoc from a SourceLocation with SourceManager.
283
class PresumedLoc {
284
  const char *Filename = nullptr;
285
  unsigned Line, Col;
286
  SourceLocation IncludeLoc;
287
288
public:
289
2.16k
  PresumedLoc() = default;
290
  PresumedLoc(const char *FN, unsigned Ln, unsigned Co, SourceLocation IL)
291
26.9M
      : Filename(FN), Line(Ln), Col(Co), IncludeLoc(IL) {}
292
293
  /// Return true if this object is invalid or uninitialized.
294
  ///
295
  /// This occurs when created with invalid source locations or when walking
296
  /// off the top of a \#include stack.
297
13.8M
  bool isInvalid() const { return Filename == nullptr; }
298
12.8M
  bool isValid() const { return Filename != nullptr; }
299
300
  /// Return the presumed filename of this location.
301
  ///
302
  /// This can be affected by \#line etc.
303
6.82M
  const char *getFilename() const {
304
6.82M
    assert(isValid());
305
6.82M
    return Filename;
306
6.82M
  }
307
308
  /// Return the presumed line number of this location.
309
  ///
310
  /// This can be affected by \#line etc.
311
7.96M
  unsigned getLine() const {
312
7.96M
    assert(isValid());
313
7.96M
    return Line;
314
7.96M
  }
315
316
  /// Return the presumed column number of this location.
317
  ///
318
  /// This cannot be affected by \#line, but is packaged here for convenience.
319
6.20M
  unsigned getColumn() const {
320
6.20M
    assert(isValid());
321
6.20M
    return Col;
322
6.20M
  }
323
324
  /// Return the presumed include location of this location.
325
  ///
326
  /// This can be affected by GNU linemarker directives.
327
67.9k
  SourceLocation getIncludeLoc() const {
328
67.9k
    assert(isValid());
329
67.9k
    return IncludeLoc;
330
67.9k
  }
331
};
332
333
class FileEntry;
334
335
/// A SourceLocation and its associated SourceManager.
336
///
337
/// This is useful for argument passing to functions that expect both objects.
338
0
class FullSourceLoc : public SourceLocation {
Unexecuted instantiation: clang::FullSourceLoc::operator=(clang::FullSourceLoc const&)
Unexecuted instantiation: clang::FullSourceLoc::operator=(clang::FullSourceLoc&&)
339
  const SourceManager *SrcMgr = nullptr;
340
341
public:
342
  /// Creates a FullSourceLoc where isValid() returns \c false.
343
62.4k
  FullSourceLoc() = default;
344
345
  explicit FullSourceLoc(SourceLocation Loc, const SourceManager &SM)
346
348k
      : SourceLocation(Loc), SrcMgr(&SM) {}
347
348
372
  bool hasManager() const {
349
372
      bool hasSrcMgr =  SrcMgr != nullptr;
350
372
      assert(hasSrcMgr == isValid() && "FullSourceLoc has location but no manager");
351
372
      return hasSrcMgr;
352
372
  }
353
354
  /// \pre This FullSourceLoc has an associated SourceManager.
355
159k
  const SourceManager &getManager() const {
356
159k
    assert(SrcMgr && "SourceManager is NULL.");
357
159k
    return *SrcMgr;
358
159k
  }
359
360
  FileID getFileID() const;
361
362
  FullSourceLoc getExpansionLoc() const;
363
  FullSourceLoc getSpellingLoc() const;
364
  FullSourceLoc getFileLoc() const;
365
  PresumedLoc getPresumedLoc(bool UseLineDirectives = true) const;
366
  bool isMacroArgExpansion(FullSourceLoc *StartLoc = nullptr) const;
367
  FullSourceLoc getImmediateMacroCallerLoc() const;
368
  std::pair<FullSourceLoc, StringRef> getModuleImportLoc() const;
369
  unsigned getFileOffset() const;
370
371
  unsigned getExpansionLineNumber(bool *Invalid = nullptr) const;
372
  unsigned getExpansionColumnNumber(bool *Invalid = nullptr) const;
373
374
  unsigned getSpellingLineNumber(bool *Invalid = nullptr) const;
375
  unsigned getSpellingColumnNumber(bool *Invalid = nullptr) const;
376
377
  const char *getCharacterData(bool *Invalid = nullptr) const;
378
379
  unsigned getLineNumber(bool *Invalid = nullptr) const;
380
  unsigned getColumnNumber(bool *Invalid = nullptr) const;
381
382
  const FileEntry *getFileEntry() const;
383
384
  /// Return a StringRef to the source buffer data for the
385
  /// specified FileID.
386
  StringRef getBufferData(bool *Invalid = nullptr) const;
387
388
  /// Decompose the specified location into a raw FileID + Offset pair.
389
  ///
390
  /// The first element is the FileID, the second is the offset from the
391
  /// start of the buffer of the location.
392
  std::pair<FileID, unsigned> getDecomposedLoc() const;
393
394
  bool isInSystemHeader() const;
395
396
  /// Determines the order of 2 source locations in the translation unit.
397
  ///
398
  /// \returns true if this source location comes before 'Loc', false otherwise.
399
  bool isBeforeInTranslationUnitThan(SourceLocation Loc) const;
400
401
  /// Determines the order of 2 source locations in the translation unit.
402
  ///
403
  /// \returns true if this source location comes before 'Loc', false otherwise.
404
68.0k
  bool isBeforeInTranslationUnitThan(FullSourceLoc Loc) const {
405
68.0k
    assert(Loc.isValid());
406
68.0k
    assert(SrcMgr == Loc.SrcMgr && "Loc comes from another SourceManager!");
407
68.0k
    return isBeforeInTranslationUnitThan((SourceLocation)Loc);
408
68.0k
  }
409
410
  /// Comparison function class, useful for sorting FullSourceLocs.
411
  struct BeforeThanCompare {
412
7.60k
    bool operator()(const FullSourceLoc& lhs, const FullSourceLoc& rhs) const {
413
7.60k
      return lhs.isBeforeInTranslationUnitThan(rhs);
414
7.60k
    }
415
  };
416
417
  /// Prints information about this FullSourceLoc to stderr.
418
  ///
419
  /// This is useful for debugging.
420
  void dump() const;
421
422
  friend bool
423
59.9k
  operator==(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
424
59.9k
    return LHS.getRawEncoding() == RHS.getRawEncoding() &&
425
59.9k
          
LHS.SrcMgr == RHS.SrcMgr1.61k
;
426
59.9k
  }
427
428
  friend bool
429
56.3k
  operator!=(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
430
56.3k
    return !(LHS == RHS);
431
56.3k
  }
432
};
433
434
} // namespace clang
435
436
namespace llvm {
437
438
  /// Define DenseMapInfo so that FileID's can be used as keys in DenseMap and
439
  /// DenseSets.
440
  template <>
441
  struct DenseMapInfo<clang::FileID> {
442
5.98M
    static clang::FileID getEmptyKey() {
443
5.98M
      return {};
444
5.98M
    }
445
446
5.10M
    static clang::FileID getTombstoneKey() {
447
5.10M
      return clang::FileID::getSentinel();
448
5.10M
    }
449
450
4.90M
    static unsigned getHashValue(clang::FileID S) {
451
4.90M
      return S.getHashValue();
452
4.90M
    }
453
454
15.8M
    static bool isEqual(clang::FileID LHS, clang::FileID RHS) {
455
15.8M
      return LHS == RHS;
456
15.8M
    }
457
  };
458
459
  // Teach SmallPtrSet how to handle SourceLocation.
460
  template<>
461
  struct PointerLikeTypeTraits<clang::SourceLocation> {
462
    enum { NumLowBitsAvailable = 0 };
463
464
24
    static void *getAsVoidPointer(clang::SourceLocation L) {
465
24
      return L.getPtrEncoding();
466
24
    }
467
468
8
    static clang::SourceLocation getFromVoidPointer(void *P) {
469
8
      return clang::SourceLocation::getFromRawEncoding((unsigned)(uintptr_t)P);
470
8
    }
471
  };
472
473
} // namespace llvm
474
475
#endif // LLVM_CLANG_BASIC_SOURCELOCATION_H