Coverage Report

Created: 2019-07-24 05:18

/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
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
190k
  bool isValid() const { return ID != 0; }
45
115M
  bool isInvalid() const { return ID == 0; }
46
47
183M
  bool operator==(const FileID &RHS) const { return ID == RHS.ID; }
48
161M
  bool operator<(const FileID &RHS) const { return ID < RHS.ID; }
49
0
  bool operator<=(const FileID &RHS) const { return ID <= RHS.ID; }
50
12.5M
  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
5.42M
  static FileID getSentinel() { return get(-1); }
55
5.21M
  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
387M
  static FileID get(int V) {
63
387M
    FileID F;
64
387M
    F.ID = V;
65
387M
    return F;
66
387M
  }
67
68
12.0k
  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
1.74G
  bool isFileID() const  { return (ID & MacroIDBit) == 0; }
99
53.4M
  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.24G
  bool isValid() const { return ID != 0; }
107
392M
  bool isInvalid() const { return ID == 0; }
108
109
private:
110
  /// Return the offset into the manager's global input view.
111
946M
  unsigned getOffset() const {
112
946M
    return ID & ~MacroIDBit;
113
946M
  }
114
115
14.0M
  static SourceLocation getFileLoc(unsigned ID) {
116
14.0M
    assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
117
14.0M
    SourceLocation L;
118
14.0M
    L.ID = ID;
119
14.0M
    return L;
120
14.0M
  }
121
122
28.7M
  static SourceLocation getMacroLoc(unsigned ID) {
123
28.7M
    assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
124
28.7M
    SourceLocation L;
125
28.7M
    L.ID = MacroIDBit | ID;
126
28.7M
    return L;
127
28.7M
  }
128
129
public:
130
  /// Return a source location with the specified offset from this
131
  /// SourceLocation.
132
1.64G
  SourceLocation getLocWithOffset(int Offset) const {
133
1.64G
    assert(((getOffset()+Offset) & MacroIDBit) == 0 && "offset overflow");
134
1.64G
    SourceLocation L;
135
1.64G
    L.ID = ID+Offset;
136
1.64G
    return L;
137
1.64G
  }
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.44G
  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.64G
  static SourceLocation getFromRawEncoding(unsigned Encoding) {
151
1.64G
    SourceLocation X;
152
1.64G
    X.ID = Encoding;
153
1.64G
    return X;
154
1.64G
  }
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.5k
  void* getPtrEncoding() const {
162
13.5k
    // Double cast to avoid a warning "cast to pointer from integer of different
163
13.5k
    // size".
164
13.5k
    return (void*)(uintptr_t)getRawEncoding();
165
13.5k
  }
166
167
  /// Turn a pointer encoding of a SourceLocation object back
168
  /// into a real SourceLocation.
169
12.8k
  static SourceLocation getFromPtrEncoding(const void *Encoding) {
170
12.8k
    return getFromRawEncoding((unsigned)(uintptr_t)Encoding);
171
12.8k
  }
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
105M
inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) {
184
105M
  return LHS.getRawEncoding() == RHS.getRawEncoding();
185
105M
}
186
187
4.68M
inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) {
188
4.68M
  return !(LHS == RHS);
189
4.68M
}
190
191
1.86M
inline bool operator<(const SourceLocation &LHS, const SourceLocation &RHS) {
192
1.86M
  return LHS.getRawEncoding() < RHS.getRawEncoding();
193
1.86M
}
194
195
/// A trivial tuple used to represent a source range.
196
class SourceRange {
197
  SourceLocation B;
198
  SourceLocation E;
199
200
public:
201
554M
  SourceRange() = default;
202
27.0M
  SourceRange(SourceLocation loc) : B(loc), E(loc) {}
203
407M
  SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {}
204
205
433M
  SourceLocation getBegin() const { return B; }
206
179M
  SourceLocation getEnd() const { return E; }
207
208
58.2M
  void setBegin(SourceLocation b) { B = b; }
209
138M
  void setEnd(SourceLocation e) { E = e; }
210
211
315M
  bool isValid() const { return B.isValid() && 
E.isValid()43.6M
; }
212
69.8M
  bool isInvalid() const { return !isValid(); }
213
214
11.1k
  bool operator==(const SourceRange &X) const {
215
11.1k
    return B == X.B && 
E == X.E9.95k
;
216
11.1k
  }
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
class CharSourceRange {
235
  SourceRange Range;
236
  bool IsTokenRange = false;
237
238
public:
239
14.4M
  CharSourceRange() = default;
240
11.8M
  CharSourceRange(SourceRange R, bool ITR) : Range(R), IsTokenRange(ITR) {}
241
242
1.87M
  static CharSourceRange getTokenRange(SourceRange R) {
243
1.87M
    return CharSourceRange(R, true);
244
1.87M
  }
245
246
4.92M
  static CharSourceRange getCharRange(SourceRange R) {
247
4.92M
    return CharSourceRange(R, false);
248
4.92M
  }
249
250
1.37k
  static CharSourceRange getTokenRange(SourceLocation B, SourceLocation E) {
251
1.37k
    return getTokenRange(SourceRange(B, E));
252
1.37k
  }
253
254
4.90M
  static CharSourceRange getCharRange(SourceLocation B, SourceLocation E) {
255
4.90M
    return getCharRange(SourceRange(B, E));
256
4.90M
  }
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
3.06M
  bool isTokenRange() const { return IsTokenRange; }
262
157
  bool isCharRange() const { return !IsTokenRange; }
263
264
19.0M
  SourceLocation getBegin() const { return Range.getBegin(); }
265
14.9M
  SourceLocation getEnd() const { return Range.getEnd(); }
266
3.07k
  SourceRange getAsRange() const { return Range; }
267
268
518k
  void setBegin(SourceLocation b) { Range.setBegin(b); }
269
477k
  void setEnd(SourceLocation e) { Range.setEnd(e); }
270
477k
  void setTokenRange(bool TR) { IsTokenRange = TR; }
271
272
2.51M
  bool isValid() const { return Range.isValid(); }
273
2.21M
  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
  FileID ID;
286
  unsigned Line, Col;
287
  SourceLocation IncludeLoc;
288
289
public:
290
5.37k
  PresumedLoc() = default;
291
  PresumedLoc(const char *FN, FileID FID, unsigned Ln, unsigned Co,
292
              SourceLocation IL)
293
33.0M
      : Filename(FN), ID(FID), Line(Ln), Col(Co), IncludeLoc(IL) {}
294
295
  /// Return true if this object is invalid or uninitialized.
296
  ///
297
  /// This occurs when created with invalid source locations or when walking
298
  /// off the top of a \#include stack.
299
14.2M
  bool isInvalid() const { return Filename == nullptr; }
300
13.1M
  bool isValid() const { return Filename != nullptr; }
301
302
  /// Return the presumed filename of this location.
303
  ///
304
  /// This can be affected by \#line etc.
305
12.3M
  const char *getFilename() const {
306
12.3M
    assert(isValid());
307
12.3M
    return Filename;
308
12.3M
  }
309
310
10.2k
  FileID getFileID() const {
311
10.2k
    assert(isValid());
312
10.2k
    return ID;
313
10.2k
  }
314
315
  /// Return the presumed line number of this location.
316
  ///
317
  /// This can be affected by \#line etc.
318
8.25M
  unsigned getLine() const {
319
8.25M
    assert(isValid());
320
8.25M
    return Line;
321
8.25M
  }
322
323
  /// Return the presumed column number of this location.
324
  ///
325
  /// This cannot be affected by \#line, but is packaged here for convenience.
326
6.38M
  unsigned getColumn() const {
327
6.38M
    assert(isValid());
328
6.38M
    return Col;
329
6.38M
  }
330
331
  /// Return the presumed include location of this location.
332
  ///
333
  /// This can be affected by GNU linemarker directives.
334
67.8k
  SourceLocation getIncludeLoc() const {
335
67.8k
    assert(isValid());
336
67.8k
    return IncludeLoc;
337
67.8k
  }
338
};
339
340
class FileEntry;
341
342
/// A SourceLocation and its associated SourceManager.
343
///
344
/// This is useful for argument passing to functions that expect both objects.
345
class FullSourceLoc : public SourceLocation {
346
  const SourceManager *SrcMgr = nullptr;
347
348
public:
349
  /// Creates a FullSourceLoc where isValid() returns \c false.
350
66.2k
  FullSourceLoc() = default;
351
352
  explicit FullSourceLoc(SourceLocation Loc, const SourceManager &SM)
353
355k
      : SourceLocation(Loc), SrcMgr(&SM) {}
354
355
372
  bool hasManager() const {
356
372
      bool hasSrcMgr =  SrcMgr != nullptr;
357
372
      assert(hasSrcMgr == isValid() && "FullSourceLoc has location but no manager");
358
372
      return hasSrcMgr;
359
372
  }
360
361
  /// \pre This FullSourceLoc has an associated SourceManager.
362
151k
  const SourceManager &getManager() const {
363
151k
    assert(SrcMgr && "SourceManager is NULL.");
364
151k
    return *SrcMgr;
365
151k
  }
366
367
  FileID getFileID() const;
368
369
  FullSourceLoc getExpansionLoc() const;
370
  FullSourceLoc getSpellingLoc() const;
371
  FullSourceLoc getFileLoc() const;
372
  PresumedLoc getPresumedLoc(bool UseLineDirectives = true) const;
373
  bool isMacroArgExpansion(FullSourceLoc *StartLoc = nullptr) const;
374
  FullSourceLoc getImmediateMacroCallerLoc() const;
375
  std::pair<FullSourceLoc, StringRef> getModuleImportLoc() const;
376
  unsigned getFileOffset() const;
377
378
  unsigned getExpansionLineNumber(bool *Invalid = nullptr) const;
379
  unsigned getExpansionColumnNumber(bool *Invalid = nullptr) const;
380
381
  unsigned getSpellingLineNumber(bool *Invalid = nullptr) const;
382
  unsigned getSpellingColumnNumber(bool *Invalid = nullptr) const;
383
384
  const char *getCharacterData(bool *Invalid = nullptr) const;
385
386
  unsigned getLineNumber(bool *Invalid = nullptr) const;
387
  unsigned getColumnNumber(bool *Invalid = nullptr) const;
388
389
  const FileEntry *getFileEntry() const;
390
391
  /// Return a StringRef to the source buffer data for the
392
  /// specified FileID.
393
  StringRef getBufferData(bool *Invalid = nullptr) const;
394
395
  /// Decompose the specified location into a raw FileID + Offset pair.
396
  ///
397
  /// The first element is the FileID, the second is the offset from the
398
  /// start of the buffer of the location.
399
  std::pair<FileID, unsigned> getDecomposedLoc() const;
400
401
  bool isInSystemHeader() const;
402
403
  /// Determines the order of 2 source locations in the translation unit.
404
  ///
405
  /// \returns true if this source location comes before 'Loc', false otherwise.
406
  bool isBeforeInTranslationUnitThan(SourceLocation Loc) const;
407
408
  /// Determines the order of 2 source locations in the translation unit.
409
  ///
410
  /// \returns true if this source location comes before 'Loc', false otherwise.
411
69.6k
  bool isBeforeInTranslationUnitThan(FullSourceLoc Loc) const {
412
69.6k
    assert(Loc.isValid());
413
69.6k
    assert(SrcMgr == Loc.SrcMgr && "Loc comes from another SourceManager!");
414
69.6k
    return isBeforeInTranslationUnitThan((SourceLocation)Loc);
415
69.6k
  }
416
417
  /// Comparison function class, useful for sorting FullSourceLocs.
418
  struct BeforeThanCompare {
419
7.60k
    bool operator()(const FullSourceLoc& lhs, const FullSourceLoc& rhs) const {
420
7.60k
      return lhs.isBeforeInTranslationUnitThan(rhs);
421
7.60k
    }
422
  };
423
424
  /// Prints information about this FullSourceLoc to stderr.
425
  ///
426
  /// This is useful for debugging.
427
  void dump() const;
428
429
  friend bool
430
61.6k
  operator==(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
431
61.6k
    return LHS.getRawEncoding() == RHS.getRawEncoding() &&
432
61.6k
          
LHS.SrcMgr == RHS.SrcMgr1.65k
;
433
61.6k
  }
434
435
  friend bool
436
57.9k
  operator!=(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
437
57.9k
    return !(LHS == RHS);
438
57.9k
  }
439
};
440
441
} // namespace clang
442
443
namespace llvm {
444
445
  /// Define DenseMapInfo so that FileID's can be used as keys in DenseMap and
446
  /// DenseSets.
447
  template <>
448
  struct DenseMapInfo<clang::FileID> {
449
6.34M
    static clang::FileID getEmptyKey() {
450
6.34M
      return {};
451
6.34M
    }
452
453
5.42M
    static clang::FileID getTombstoneKey() {
454
5.42M
      return clang::FileID::getSentinel();
455
5.42M
    }
456
457
5.21M
    static unsigned getHashValue(clang::FileID S) {
458
5.21M
      return S.getHashValue();
459
5.21M
    }
460
461
17.1M
    static bool isEqual(clang::FileID LHS, clang::FileID RHS) {
462
17.1M
      return LHS == RHS;
463
17.1M
    }
464
  };
465
466
  // Teach SmallPtrSet how to handle SourceLocation.
467
  template<>
468
  struct PointerLikeTypeTraits<clang::SourceLocation> {
469
    enum { NumLowBitsAvailable = 0 };
470
471
26
    static void *getAsVoidPointer(clang::SourceLocation L) {
472
26
      return L.getPtrEncoding();
473
26
    }
474
475
8
    static clang::SourceLocation getFromVoidPointer(void *P) {
476
8
      return clang::SourceLocation::getFromRawEncoding((unsigned)(uintptr_t)P);
477
8
    }
478
  };
479
480
} // namespace llvm
481
482
#endif // LLVM_CLANG_BASIC_SOURCELOCATION_H