Coverage Report

Created: 2021-09-21 08:58

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Basic/SourceLocation.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- SourceLocation.cpp - Compact identifier for Source Files -----------===//
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
//  This file defines accessor methods for the FullSourceLoc class.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/Basic/SourceLocation.h"
14
#include "clang/Basic/LLVM.h"
15
#include "clang/Basic/PrettyStackTrace.h"
16
#include "clang/Basic/SourceManager.h"
17
#include "llvm/ADT/DenseMapInfo.h"
18
#include "llvm/ADT/FoldingSet.h"
19
#include "llvm/ADT/StringRef.h"
20
#include "llvm/Support/Compiler.h"
21
#include "llvm/Support/MemoryBuffer.h"
22
#include "llvm/Support/raw_ostream.h"
23
#include <cassert>
24
#include <string>
25
#include <utility>
26
27
using namespace clang;
28
29
//===----------------------------------------------------------------------===//
30
// PrettyStackTraceLoc
31
//===----------------------------------------------------------------------===//
32
33
0
void PrettyStackTraceLoc::print(raw_ostream &OS) const {
34
0
  if (Loc.isValid()) {
35
0
    Loc.print(OS, SM);
36
0
    OS << ": ";
37
0
  }
38
0
  OS << Message << '\n';
39
0
}
40
41
//===----------------------------------------------------------------------===//
42
// SourceLocation
43
//===----------------------------------------------------------------------===//
44
45
static_assert(std::is_trivially_destructible<SourceLocation>::value,
46
              "SourceLocation must be trivially destructible because it is "
47
              "used in unions");
48
49
static_assert(std::is_trivially_destructible<SourceRange>::value,
50
              "SourceRange must be trivially destructible because it is "
51
              "used in unions");
52
53
111k
unsigned SourceLocation::getHashValue() const {
54
111k
  return llvm::DenseMapInfo<UIntTy>::getHashValue(ID);
55
111k
}
56
57
void llvm::FoldingSetTrait<SourceLocation>::Profile(
58
379k
    const SourceLocation &X, llvm::FoldingSetNodeID &ID) {
59
379k
  ID.AddInteger(X.ID);
60
379k
}
61
62
451
void SourceLocation::print(raw_ostream &OS, const SourceManager &SM)const{
63
451
  if (!isValid()) {
64
229
    OS << "<invalid loc>";
65
229
    return;
66
229
  }
67
68
222
  if (isFileID()) {
69
209
    PresumedLoc PLoc = SM.getPresumedLoc(*this);
70
71
209
    if (PLoc.isInvalid()) {
72
0
      OS << "<invalid>";
73
0
      return;
74
0
    }
75
    // The macro expansion and spelling pos is identical for file locs.
76
209
    OS << PLoc.getFilename() << ':' << PLoc.getLine()
77
209
       << ':' << PLoc.getColumn();
78
209
    return;
79
209
  }
80
81
13
  SM.getExpansionLoc(*this).print(OS, SM);
82
83
13
  OS << " <Spelling=";
84
13
  SM.getSpellingLoc(*this).print(OS, SM);
85
13
  OS << '>';
86
13
}
87
88
LLVM_DUMP_METHOD std::string
89
18
SourceLocation::printToString(const SourceManager &SM) const {
90
18
  std::string S;
91
18
  llvm::raw_string_ostream OS(S);
92
18
  print(OS, SM);
93
18
  return OS.str();
94
18
}
95
96
0
LLVM_DUMP_METHOD void SourceLocation::dump(const SourceManager &SM) const {
97
0
  print(llvm::errs(), SM);
98
0
  llvm::errs() << '\n';
99
0
}
100
101
0
LLVM_DUMP_METHOD void SourceRange::dump(const SourceManager &SM) const {
102
0
  print(llvm::errs(), SM);
103
0
  llvm::errs() << '\n';
104
0
}
105
106
static PresumedLoc PrintDifference(raw_ostream &OS, const SourceManager &SM,
107
7
                                   SourceLocation Loc, PresumedLoc Previous) {
108
7
  if (Loc.isFileID()) {
109
110
7
    PresumedLoc PLoc = SM.getPresumedLoc(Loc);
111
112
7
    if (PLoc.isInvalid()) {
113
0
      OS << "<invalid sloc>";
114
0
      return Previous;
115
0
    }
116
117
7
    if (Previous.isInvalid() ||
118
7
        
strcmp(PLoc.getFilename(), Previous.getFilename()) != 03
) {
119
5
      OS << PLoc.getFilename() << ':' << PLoc.getLine() << ':'
120
5
         << PLoc.getColumn();
121
5
    } else 
if (2
Previous.isInvalid()2
||
PLoc.getLine() != Previous.getLine()2
) {
122
1
      OS << "line" << ':' << PLoc.getLine() << ':' << PLoc.getColumn();
123
1
    } else {
124
1
      OS << "col" << ':' << PLoc.getColumn();
125
1
    }
126
7
    return PLoc;
127
7
  }
128
0
  auto PrintedLoc = PrintDifference(OS, SM, SM.getExpansionLoc(Loc), Previous);
129
130
0
  OS << " <Spelling=";
131
0
  PrintedLoc = PrintDifference(OS, SM, SM.getSpellingLoc(Loc), PrintedLoc);
132
0
  OS << '>';
133
0
  return PrintedLoc;
134
7
}
135
136
4
void SourceRange::print(raw_ostream &OS, const SourceManager &SM) const {
137
138
4
  OS << '<';
139
4
  auto PrintedLoc = PrintDifference(OS, SM, B, {});
140
4
  if (B != E) {
141
3
    OS << ", ";
142
3
    PrintDifference(OS, SM, E, PrintedLoc);
143
3
  }
144
4
  OS << '>';
145
4
}
146
147
LLVM_DUMP_METHOD std::string
148
4
SourceRange::printToString(const SourceManager &SM) const {
149
4
  std::string S;
150
4
  llvm::raw_string_ostream OS(S);
151
4
  print(OS, SM);
152
4
  return OS.str();
153
4
}
154
155
//===----------------------------------------------------------------------===//
156
// FullSourceLoc
157
//===----------------------------------------------------------------------===//
158
159
149k
FileID FullSourceLoc::getFileID() const {
160
149k
  assert(isValid());
161
0
  return SrcMgr->getFileID(*this);
162
149k
}
163
164
28.4k
FullSourceLoc FullSourceLoc::getExpansionLoc() const {
165
28.4k
  assert(isValid());
166
0
  return FullSourceLoc(SrcMgr->getExpansionLoc(*this), *SrcMgr);
167
28.4k
}
168
169
2.74k
FullSourceLoc FullSourceLoc::getSpellingLoc() const {
170
2.74k
  assert(isValid());
171
0
  return FullSourceLoc(SrcMgr->getSpellingLoc(*this), *SrcMgr);
172
2.74k
}
173
174
34.1k
FullSourceLoc FullSourceLoc::getFileLoc() const {
175
34.1k
  assert(isValid());
176
0
  return FullSourceLoc(SrcMgr->getFileLoc(*this), *SrcMgr);
177
34.1k
}
178
179
35.2k
PresumedLoc FullSourceLoc::getPresumedLoc(bool UseLineDirectives) const {
180
35.2k
  if (!isValid())
181
18
    return PresumedLoc();
182
183
35.2k
  return SrcMgr->getPresumedLoc(*this, UseLineDirectives);
184
35.2k
}
185
186
507
bool FullSourceLoc::isMacroArgExpansion(FullSourceLoc *StartLoc) const {
187
507
  assert(isValid());
188
0
  return SrcMgr->isMacroArgExpansion(*this, StartLoc);
189
507
}
190
191
0
FullSourceLoc FullSourceLoc::getImmediateMacroCallerLoc() const {
192
0
  assert(isValid());
193
0
  return FullSourceLoc(SrcMgr->getImmediateMacroCallerLoc(*this), *SrcMgr);
194
0
}
195
196
1.21k
std::pair<FullSourceLoc, StringRef> FullSourceLoc::getModuleImportLoc() const {
197
1.21k
  if (!isValid())
198
6
    return std::make_pair(FullSourceLoc(), StringRef());
199
200
1.20k
  std::pair<SourceLocation, StringRef> ImportLoc =
201
1.20k
      SrcMgr->getModuleImportLoc(*this);
202
1.20k
  return std::make_pair(FullSourceLoc(ImportLoc.first, *SrcMgr),
203
1.20k
                        ImportLoc.second);
204
1.21k
}
205
206
88.2k
unsigned FullSourceLoc::getFileOffset() const {
207
88.2k
  assert(isValid());
208
0
  return SrcMgr->getFileOffset(*this);
209
88.2k
}
210
211
56.0k
unsigned FullSourceLoc::getLineNumber(bool *Invalid) const {
212
56.0k
  assert(isValid());
213
0
  return SrcMgr->getLineNumber(getFileID(), getFileOffset(), Invalid);
214
56.0k
}
215
216
31.9k
unsigned FullSourceLoc::getColumnNumber(bool *Invalid) const {
217
31.9k
  assert(isValid());
218
0
  return SrcMgr->getColumnNumber(getFileID(), getFileOffset(), Invalid);
219
31.9k
}
220
221
24
const FileEntry *FullSourceLoc::getFileEntry() const {
222
24
  assert(isValid());
223
0
  return SrcMgr->getFileEntryForID(getFileID());
224
24
}
225
226
18.8k
unsigned FullSourceLoc::getExpansionLineNumber(bool *Invalid) const {
227
18.8k
  assert(isValid());
228
0
  return SrcMgr->getExpansionLineNumber(*this, Invalid);
229
18.8k
}
230
231
16.4k
unsigned FullSourceLoc::getExpansionColumnNumber(bool *Invalid) const {
232
16.4k
  assert(isValid());
233
0
  return SrcMgr->getExpansionColumnNumber(*this, Invalid);
234
16.4k
}
235
236
965
unsigned FullSourceLoc::getSpellingLineNumber(bool *Invalid) const {
237
965
  assert(isValid());
238
0
  return SrcMgr->getSpellingLineNumber(*this, Invalid);
239
965
}
240
241
325
unsigned FullSourceLoc::getSpellingColumnNumber(bool *Invalid) const {
242
325
  assert(isValid());
243
0
  return SrcMgr->getSpellingColumnNumber(*this, Invalid);
244
325
}
245
246
0
bool FullSourceLoc::isInSystemHeader() const {
247
0
  assert(isValid());
248
0
  return SrcMgr->isInSystemHeader(*this);
249
0
}
250
251
161k
bool FullSourceLoc::isBeforeInTranslationUnitThan(SourceLocation Loc) const {
252
161k
  assert(isValid());
253
0
  return SrcMgr->isBeforeInTranslationUnit(*this, Loc);
254
161k
}
255
256
0
LLVM_DUMP_METHOD void FullSourceLoc::dump() const {
257
0
  SourceLocation::dump(*SrcMgr);
258
0
}
259
260
270
const char *FullSourceLoc::getCharacterData(bool *Invalid) const {
261
270
  assert(isValid());
262
0
  return SrcMgr->getCharacterData(*this, Invalid);
263
270
}
264
265
31.9k
StringRef FullSourceLoc::getBufferData(bool *Invalid) const {
266
31.9k
  assert(isValid());
267
0
  return SrcMgr->getBufferData(SrcMgr->getFileID(*this), Invalid);
268
31.9k
}
269
270
312k
std::pair<FileID, unsigned> FullSourceLoc::getDecomposedLoc() const {
271
312k
  return SrcMgr->getDecomposedLoc(*this);
272
312k
}