Coverage Report

Created: 2018-07-19 20:53

/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
//                     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
/// Defines the clang::SourceLocation class and associated facilities.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#ifndef LLVM_CLANG_BASIC_SOURCELOCATION_H
16
#define LLVM_CLANG_BASIC_SOURCELOCATION_H
17
18
#include "clang/Basic/LLVM.h"
19
#include "llvm/ADT/StringRef.h"
20
#include "llvm/Support/PointerLikeTypeTraits.h"
21
#include <cassert>
22
#include <cstdint>
23
#include <string>
24
#include <utility>
25
26
namespace llvm {
27
28
template <typename T> struct DenseMapInfo;
29
template <typename T> struct isPodLike;
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
0
class FileID {
Unexecuted instantiation: clang::FileID::operator=(clang::FileID const&)
Unexecuted instantiation: clang::FileID::operator=(clang::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
182k
  bool isValid() const { return ID != 0; }
47
46.5M
  bool isInvalid() const { return ID == 0; }
48
49
107M
  bool operator==(const FileID &RHS) const { return ID == RHS.ID; }
50
116M
  bool operator<(const FileID &RHS) const { return ID < RHS.ID; }
51
  bool operator<=(const FileID &RHS) const { return ID <= RHS.ID; }
52
6.78M
  bool operator!=(const FileID &RHS) const { return !(*this == RHS); }
53
  bool operator>(const FileID &RHS) const { return RHS < *this; }
54
  bool operator>=(const FileID &RHS) const { return RHS <= *this; }
55
56
5.15M
  static FileID getSentinel() { return get(-1); }
57
5.73M
  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
258M
  static FileID get(int V) {
65
258M
    FileID F;
66
258M
    F.ID = V;
67
258M
    return F;
68
258M
  }
69
70
11.5k
  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
0
class SourceLocation {
Unexecuted instantiation: clang::SourceLocation::operator=(clang::SourceLocation const&)
Unexecuted instantiation: clang::SourceLocation::operator=(clang::SourceLocation&&)
89
  friend class ASTReader;
90
  friend class ASTWriter;
91
  friend class SourceManager;
92
93
  unsigned ID = 0;
94
95
  enum : unsigned {
96
    MacroIDBit = 1U << 31
97
  };
98
99
public:
100
1.35G
  bool isFileID() const  { return (ID & MacroIDBit) == 0; }
101
37.0M
  bool isMacroID() const { return (ID & MacroIDBit) != 0; }
102
103
  /// Return true if this is a valid SourceLocation object.
104
  ///
105
  /// Invalid SourceLocations are often used when events have no corresponding
106
  /// location in the source (e.g. a diagnostic is required for a command line
107
  /// option).
108
960M
  bool isValid() const { return ID != 0; }
109
271M
  bool isInvalid() const { return ID == 0; }
110
111
private:
112
  /// Return the offset into the manager's global input view.
113
612M
  unsigned getOffset() const {
114
612M
    return ID & ~MacroIDBit;
115
612M
  }
116
117
2.39M
  static SourceLocation getFileLoc(unsigned ID) {
118
2.39M
    assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
119
2.39M
    SourceLocation L;
120
2.39M
    L.ID = ID;
121
2.39M
    return L;
122
2.39M
  }
123
124
22.5M
  static SourceLocation getMacroLoc(unsigned ID) {
125
22.5M
    assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
126
22.5M
    SourceLocation L;
127
22.5M
    L.ID = MacroIDBit | ID;
128
22.5M
    return L;
129
22.5M
  }
130
131
public:
132
  /// Return a source location with the specified offset from this
133
  /// SourceLocation.
134
1.27G
  SourceLocation getLocWithOffset(int Offset) const {
135
1.27G
    assert(((getOffset()+Offset) & MacroIDBit) == 0 && "offset overflow");
136
1.27G
    SourceLocation L;
137
1.27G
    L.ID = ID+Offset;
138
1.27G
    return L;
139
1.27G
  }
140
141
  /// When a SourceLocation itself cannot be used, this returns
142
  /// an (opaque) 32-bit integer encoding for it.
143
  ///
144
  /// This should only be passed to SourceLocation::getFromRawEncoding, it
145
  /// should not be inspected directly.
146
2.65G
  unsigned getRawEncoding() const { return ID; }
147
148
  /// Turn a raw encoding of a SourceLocation object into
149
  /// a real SourceLocation.
150
  ///
151
  /// \see getRawEncoding.
152
1.24G
  static SourceLocation getFromRawEncoding(unsigned Encoding) {
153
1.24G
    SourceLocation X;
154
1.24G
    X.ID = Encoding;
155
1.24G
    return X;
156
1.24G
  }
157
158
  /// When a SourceLocation itself cannot be used, this returns
159
  /// an (opaque) pointer encoding for it.
160
  ///
161
  /// This should only be passed to SourceLocation::getFromPtrEncoding, it
162
  /// should not be inspected directly.
163
10.5k
  void* getPtrEncoding() const {
164
10.5k
    // Double cast to avoid a warning "cast to pointer from integer of different
165
10.5k
    // size".
166
10.5k
    return (void*)(uintptr_t)getRawEncoding();
167
10.5k
  }
168
169
  /// Turn a pointer encoding of a SourceLocation object back
170
  /// into a real SourceLocation.
171
12.3k
  static SourceLocation getFromPtrEncoding(const void *Encoding) {
172
12.3k
    return getFromRawEncoding((unsigned)(uintptr_t)Encoding);
173
12.3k
  }
174
175
  static bool isPairOfFileLocations(SourceLocation Start, SourceLocation End) {
176
    return Start.isValid() && Start.isFileID() && End.isValid() &&
177
           End.isFileID();
178
  }
179
180
  void print(raw_ostream &OS, const SourceManager &SM) const;
181
  std::string printToString(const SourceManager &SM) const;
182
  void dump(const SourceManager &SM) const;
183
};
184
185
22.6M
inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) {
186
22.6M
  return LHS.getRawEncoding() == RHS.getRawEncoding();
187
22.6M
}
188
189
368k
inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) {
190
368k
  return !(LHS == RHS);
191
368k
}
192
193
1.52M
inline bool operator<(const SourceLocation &LHS, const SourceLocation &RHS) {
194
1.52M
  return LHS.getRawEncoding() < RHS.getRawEncoding();
195
1.52M
}
196
197
/// A trivial tuple used to represent a source range.
198
0
class SourceRange {
Unexecuted instantiation: clang::SourceRange::operator=(clang::SourceRange const&)
Unexecuted instantiation: clang::SourceRange::operator=(clang::SourceRange&&)
199
  SourceLocation B;
200
  SourceLocation E;
201
202
public:
203
469M
  SourceRange() = default;
204
21.4M
  SourceRange(SourceLocation loc) : B(loc), E(loc) {}
205
290M
  SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {}
206
207
273M
  SourceLocation getBegin() const { return B; }
208
101M
  SourceLocation getEnd() const { return E; }
209
210
42.2M
  void setBegin(SourceLocation b) { B = b; }
211
112M
  void setEnd(SourceLocation e) { E = e; }
212
213
271M
  bool isValid() const { return B.isValid() && 
E.isValid()33.4M
; }
214
61.0M
  bool isInvalid() const { return !isValid(); }
215
216
11.0k
  bool operator==(const SourceRange &X) const {
217
11.0k
    return B == X.B && 
E == X.E9.90k
;
218
11.0k
  }
219
220
4
  bool operator!=(const SourceRange &X) const {
221
4
    return B != X.B || E != X.E;
222
4
  }
223
};
224
  
225
/// Represents a character-granular source range.
226
///
227
/// The underlying SourceRange can either specify the starting/ending character
228
/// of the range, or it can specify the start of the range and the start of the
229
/// last token of the range (a "token range").  In the token range case, the
230
/// size of the last token must be measured to determine the actual end of the
231
/// range.
232
0
class CharSourceRange { 
Unexecuted instantiation: clang::CharSourceRange::operator=(clang::CharSourceRange const&)
Unexecuted instantiation: clang::CharSourceRange::operator=(clang::CharSourceRange&&)
233
  SourceRange Range;
234
  bool IsTokenRange = false;
235
236
public:
237
11.4M
  CharSourceRange() = default;
238
5.99M
  CharSourceRange(SourceRange R, bool ITR) : Range(R), IsTokenRange(ITR) {}
239
240
1.62M
  static CharSourceRange getTokenRange(SourceRange R) {
241
1.62M
    return CharSourceRange(R, true);
242
1.62M
  }
243
244
2.51M
  static CharSourceRange getCharRange(SourceRange R) {
245
2.51M
    return CharSourceRange(R, false);
246
2.51M
  }
247
    
248
1.34k
  static CharSourceRange getTokenRange(SourceLocation B, SourceLocation E) {
249
1.34k
    return getTokenRange(SourceRange(B, E));
250
1.34k
  }
251
252
2.50M
  static CharSourceRange getCharRange(SourceLocation B, SourceLocation E) {
253
2.50M
    return getCharRange(SourceRange(B, E));
254
2.50M
  }
255
  
256
  /// Return true if the end of this range specifies the start of
257
  /// the last token.  Return false if the end of this range specifies the last
258
  /// character in the range.
259
505k
  bool isTokenRange() const { return IsTokenRange; }
260
147
  bool isCharRange() const { return !IsTokenRange; }
261
  
262
4.41M
  SourceLocation getBegin() const { return Range.getBegin(); }
263
4.87M
  SourceLocation getEnd() const { return Range.getEnd(); }
264
3.01k
  SourceRange getAsRange() const { return Range; }
265
 
266
127k
  void setBegin(SourceLocation b) { Range.setBegin(b); }
267
127k
  void setEnd(SourceLocation e) { Range.setEnd(e); }
268
127k
  void setTokenRange(bool TR) { IsTokenRange = TR; }
269
  
270
200k
  bool isValid() const { return Range.isValid(); }
271
25.0k
  bool isInvalid() const { return !isValid(); }
272
};
273
274
/// Represents an unpacked "presumed" location which can be presented
275
/// to the user.
276
///
277
/// A 'presumed' location can be modified by \#line and GNU line marker
278
/// directives and is always the expansion point of a normal location.
279
///
280
/// You can get a PresumedLoc from a SourceLocation with SourceManager.
281
class PresumedLoc {
282
  const char *Filename = nullptr;
283
  unsigned Line, Col;
284
  SourceLocation IncludeLoc;
285
286
public:
287
1.48k
  PresumedLoc() = default;
288
  PresumedLoc(const char *FN, unsigned Ln, unsigned Co, SourceLocation IL)
289
20.8M
      : Filename(FN), Line(Ln), Col(Co), IncludeLoc(IL) {}
290
291
  /// Return true if this object is invalid or uninitialized.
292
  ///
293
  /// This occurs when created with invalid source locations or when walking
294
  /// off the top of a \#include stack.
295
7.77M
  bool isInvalid() const { return Filename == nullptr; }
296
13.0M
  bool isValid() const { return Filename != nullptr; }
297
298
  /// Return the presumed filename of this location.
299
  ///
300
  /// This can be affected by \#line etc.
301
6.70M
  const char *getFilename() const {
302
6.70M
    assert(isValid());
303
6.70M
    return Filename;
304
6.70M
  }
305
306
  /// Return the presumed line number of this location.
307
  ///
308
  /// This can be affected by \#line etc.
309
8.03M
  unsigned getLine() const {
310
8.03M
    assert(isValid());
311
8.03M
    return Line;
312
8.03M
  }
313
314
  /// Return the presumed column number of this location.
315
  ///
316
  /// This cannot be affected by \#line, but is packaged here for convenience.
317
6.31M
  unsigned getColumn() const {
318
6.31M
    assert(isValid());
319
6.31M
    return Col;
320
6.31M
  }
321
322
  /// Return the presumed include location of this location.
323
  ///
324
  /// This can be affected by GNU linemarker directives.
325
63.9k
  SourceLocation getIncludeLoc() const {
326
63.9k
    assert(isValid());
327
63.9k
    return IncludeLoc;
328
63.9k
  }
329
};
330
331
class FileEntry;
332
333
/// A SourceLocation and its associated SourceManager.
334
///
335
/// This is useful for argument passing to functions that expect both objects.
336
0
class FullSourceLoc : public SourceLocation {
Unexecuted instantiation: clang::FullSourceLoc::operator=(clang::FullSourceLoc const&)
Unexecuted instantiation: clang::FullSourceLoc::operator=(clang::FullSourceLoc&&)
337
  const SourceManager *SrcMgr = nullptr;
338
339
public:
340
  /// Creates a FullSourceLoc where isValid() returns \c false.
341
59.5k
  FullSourceLoc() = default;
342
343
  explicit FullSourceLoc(SourceLocation Loc, const SourceManager &SM)
344
321k
      : SourceLocation(Loc), SrcMgr(&SM) {}
345
346
173
  bool hasManager() const {
347
173
      bool hasSrcMgr =  SrcMgr != nullptr;
348
173
      assert(hasSrcMgr == isValid() && "FullSourceLoc has location but no manager");
349
173
      return hasSrcMgr;
350
173
  }
351
352
  /// \pre This FullSourceLoc has an associated SourceManager.
353
152k
  const SourceManager &getManager() const {
354
152k
    assert(SrcMgr && "SourceManager is NULL.");
355
152k
    return *SrcMgr;
356
152k
  }
357
358
  FileID getFileID() const;
359
360
  FullSourceLoc getExpansionLoc() const;
361
  FullSourceLoc getSpellingLoc() const;
362
  FullSourceLoc getFileLoc() const;
363
  PresumedLoc getPresumedLoc(bool UseLineDirectives = true) const;
364
  bool isMacroArgExpansion(FullSourceLoc *StartLoc = nullptr) const;
365
  FullSourceLoc getImmediateMacroCallerLoc() const;
366
  std::pair<FullSourceLoc, StringRef> getModuleImportLoc() const;
367
  unsigned getFileOffset() const;
368
369
  unsigned getExpansionLineNumber(bool *Invalid = nullptr) const;
370
  unsigned getExpansionColumnNumber(bool *Invalid = nullptr) const;
371
372
  unsigned getSpellingLineNumber(bool *Invalid = nullptr) const;
373
  unsigned getSpellingColumnNumber(bool *Invalid = nullptr) const;
374
375
  const char *getCharacterData(bool *Invalid = nullptr) const;
376
377
  unsigned getLineNumber(bool *Invalid = nullptr) const;
378
  unsigned getColumnNumber(bool *Invalid = nullptr) const;
379
380
  const FileEntry *getFileEntry() const;
381
382
  /// Return a StringRef to the source buffer data for the
383
  /// specified FileID.
384
  StringRef getBufferData(bool *Invalid = nullptr) const;
385
386
  /// Decompose the specified location into a raw FileID + Offset pair.
387
  ///
388
  /// The first element is the FileID, the second is the offset from the
389
  /// start of the buffer of the location.
390
  std::pair<FileID, unsigned> getDecomposedLoc() const;
391
392
  bool isInSystemHeader() const;
393
394
  /// Determines the order of 2 source locations in the translation unit.
395
  ///
396
  /// \returns true if this source location comes before 'Loc', false otherwise.
397
  bool isBeforeInTranslationUnitThan(SourceLocation Loc) const;
398
399
  /// Determines the order of 2 source locations in the translation unit.
400
  ///
401
  /// \returns true if this source location comes before 'Loc', false otherwise.
402
62.5k
  bool isBeforeInTranslationUnitThan(FullSourceLoc Loc) const {
403
62.5k
    assert(Loc.isValid());
404
62.5k
    assert(SrcMgr == Loc.SrcMgr && "Loc comes from another SourceManager!");
405
62.5k
    return isBeforeInTranslationUnitThan((SourceLocation)Loc);
406
62.5k
  }
407
408
  /// Comparison function class, useful for sorting FullSourceLocs.
409
  struct BeforeThanCompare {
410
7.60k
    bool operator()(const FullSourceLoc& lhs, const FullSourceLoc& rhs) const {
411
7.60k
      return lhs.isBeforeInTranslationUnitThan(rhs);
412
7.60k
    }
413
  };
414
415
  /// Prints information about this FullSourceLoc to stderr.
416
  ///
417
  /// This is useful for debugging.
418
  void dump() const;
419
420
  friend bool
421
54.1k
  operator==(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
422
54.1k
    return LHS.getRawEncoding() == RHS.getRawEncoding() &&
423
54.1k
          
LHS.SrcMgr == RHS.SrcMgr1.57k
;
424
54.1k
  }
425
426
  friend bool
427
50.8k
  operator!=(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
428
50.8k
    return !(LHS == RHS);
429
50.8k
  }
430
};
431
432
} // namespace clang
433
434
namespace llvm {
435
436
  /// Define DenseMapInfo so that FileID's can be used as keys in DenseMap and
437
  /// DenseSets.
438
  template <>
439
  struct DenseMapInfo<clang::FileID> {
440
6.03M
    static clang::FileID getEmptyKey() {
441
6.03M
      return {};
442
6.03M
    }
443
444
5.15M
    static clang::FileID getTombstoneKey() {
445
5.15M
      return clang::FileID::getSentinel();
446
5.15M
    }
447
448
4.95M
    static unsigned getHashValue(clang::FileID S) {
449
4.95M
      return S.getHashValue();
450
4.95M
    }
451
452
15.6M
    static bool isEqual(clang::FileID LHS, clang::FileID RHS) {
453
15.6M
      return LHS == RHS;
454
15.6M
    }
455
  };
456
  
457
  template <>
458
  struct isPodLike<clang::SourceLocation> { static const bool value = true; };
459
  template <>
460
  struct isPodLike<clang::FileID> { static const bool value = true; };
461
462
  // Teach SmallPtrSet how to handle SourceLocation.
463
  template<>
464
  struct PointerLikeTypeTraits<clang::SourceLocation> {
465
    enum { NumLowBitsAvailable = 0 };
466
467
24
    static void *getAsVoidPointer(clang::SourceLocation L) {
468
24
      return L.getPtrEncoding();
469
24
    }
470
471
8
    static clang::SourceLocation getFromVoidPointer(void *P) {
472
8
      return clang::SourceLocation::getFromRawEncoding((unsigned)(uintptr_t)P);
473
8
    }
474
  };
475
476
} // namespace llvm
477
478
#endif // LLVM_CLANG_BASIC_SOURCELOCATION_H