Coverage Report

Created: 2020-09-19 12:23

/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/StringRef.h"
18
#include "llvm/Support/Compiler.h"
19
#include "llvm/Support/MemoryBuffer.h"
20
#include "llvm/Support/raw_ostream.h"
21
#include <cassert>
22
#include <string>
23
#include <utility>
24
25
using namespace clang;
26
27
//===----------------------------------------------------------------------===//
28
// PrettyStackTraceLoc
29
//===----------------------------------------------------------------------===//
30
31
0
void PrettyStackTraceLoc::print(raw_ostream &OS) const {
32
0
  if (Loc.isValid()) {
33
0
    Loc.print(OS, SM);
34
0
    OS << ": ";
35
0
  }
36
0
  OS << Message << '\n';
37
0
}
38
39
//===----------------------------------------------------------------------===//
40
// SourceLocation
41
//===----------------------------------------------------------------------===//
42
43
432
void SourceLocation::print(raw_ostream &OS, const SourceManager &SM)const{
44
432
  if (!isValid()) {
45
223
    OS << "<invalid loc>";
46
223
    return;
47
223
  }
48
209
49
209
  if (isFileID()) {
50
196
    PresumedLoc PLoc = SM.getPresumedLoc(*this);
51
196
52
196
    if (PLoc.isInvalid()) {
53
0
      OS << "<invalid>";
54
0
      return;
55
0
    }
56
    // The macro expansion and spelling pos is identical for file locs.
57
196
    OS << PLoc.getFilename() << ':' << PLoc.getLine()
58
196
       << ':' << PLoc.getColumn();
59
196
    return;
60
196
  }
61
13
62
13
  SM.getExpansionLoc(*this).print(OS, SM);
63
13
64
13
  OS << " <Spelling=";
65
13
  SM.getSpellingLoc(*this).print(OS, SM);
66
13
  OS << '>';
67
13
}
68
69
LLVM_DUMP_METHOD std::string
70
18
SourceLocation::printToString(const SourceManager &SM) const {
71
18
  std::string S;
72
18
  llvm::raw_string_ostream OS(S);
73
18
  print(OS, SM);
74
18
  return OS.str();
75
18
}
76
77
0
LLVM_DUMP_METHOD void SourceLocation::dump(const SourceManager &SM) const {
78
0
  print(llvm::errs(), SM);
79
0
  llvm::errs() << '\n';
80
0
}
81
82
0
LLVM_DUMP_METHOD void SourceRange::dump(const SourceManager &SM) const {
83
0
  print(llvm::errs(), SM);
84
0
  llvm::errs() << '\n';
85
0
}
86
87
static PresumedLoc PrintDifference(raw_ostream &OS, const SourceManager &SM,
88
7
                                   SourceLocation Loc, PresumedLoc Previous) {
89
7
  if (Loc.isFileID()) {
90
7
91
7
    PresumedLoc PLoc = SM.getPresumedLoc(Loc);
92
7
93
7
    if (PLoc.isInvalid()) {
94
0
      OS << "<invalid sloc>";
95
0
      return Previous;
96
0
    }
97
7
98
7
    if (Previous.isInvalid() ||
99
5
        
strcmp(PLoc.getFilename(), Previous.getFilename()) != 03
) {
100
5
      OS << PLoc.getFilename() << ':' << PLoc.getLine() << ':'
101
5
         << PLoc.getColumn();
102
2
    } else if (Previous.isInvalid() || PLoc.getLine() != Previous.getLine()) {
103
1
      OS << "line" << ':' << PLoc.getLine() << ':' << PLoc.getColumn();
104
1
    } else {
105
1
      OS << "col" << ':' << PLoc.getColumn();
106
1
    }
107
7
    return PLoc;
108
7
  }
109
0
  auto PrintedLoc = PrintDifference(OS, SM, SM.getExpansionLoc(Loc), Previous);
110
0
111
0
  OS << " <Spelling=";
112
0
  PrintedLoc = PrintDifference(OS, SM, SM.getSpellingLoc(Loc), PrintedLoc);
113
0
  OS << '>';
114
0
  return PrintedLoc;
115
0
}
116
117
4
void SourceRange::print(raw_ostream &OS, const SourceManager &SM) const {
118
4
119
4
  OS << '<';
120
4
  auto PrintedLoc = PrintDifference(OS, SM, B, {});
121
4
  if (B != E) {
122
3
    OS << ", ";
123
3
    PrintDifference(OS, SM, E, PrintedLoc);
124
3
  }
125
4
  OS << '>';
126
4
}
127
128
LLVM_DUMP_METHOD std::string
129
4
SourceRange::printToString(const SourceManager &SM) const {
130
4
  std::string S;
131
4
  llvm::raw_string_ostream OS(S);
132
4
  print(OS, SM);
133
4
  return OS.str();
134
4
}
135
136
//===----------------------------------------------------------------------===//
137
// FullSourceLoc
138
//===----------------------------------------------------------------------===//
139
140
122k
FileID FullSourceLoc::getFileID() const {
141
122k
  assert(isValid());
142
122k
  return SrcMgr->getFileID(*this);
143
122k
}
144
145
26.6k
FullSourceLoc FullSourceLoc::getExpansionLoc() const {
146
26.6k
  assert(isValid());
147
26.6k
  return FullSourceLoc(SrcMgr->getExpansionLoc(*this), *SrcMgr);
148
26.6k
}
149
150
2.65k
FullSourceLoc FullSourceLoc::getSpellingLoc() const {
151
2.65k
  assert(isValid());
152
2.65k
  return FullSourceLoc(SrcMgr->getSpellingLoc(*this), *SrcMgr);
153
2.65k
}
154
155
26.3k
FullSourceLoc FullSourceLoc::getFileLoc() const {
156
26.3k
  assert(isValid());
157
26.3k
  return FullSourceLoc(SrcMgr->getFileLoc(*this), *SrcMgr);
158
26.3k
}
159
160
27.4k
PresumedLoc FullSourceLoc::getPresumedLoc(bool UseLineDirectives) const {
161
27.4k
  if (!isValid())
162
14
    return PresumedLoc();
163
27.3k
164
27.3k
  return SrcMgr->getPresumedLoc(*this, UseLineDirectives);
165
27.3k
}
166
167
505
bool FullSourceLoc::isMacroArgExpansion(FullSourceLoc *StartLoc) const {
168
505
  assert(isValid());
169
505
  return SrcMgr->isMacroArgExpansion(*this, StartLoc);
170
505
}
171
172
0
FullSourceLoc FullSourceLoc::getImmediateMacroCallerLoc() const {
173
0
  assert(isValid());
174
0
  return FullSourceLoc(SrcMgr->getImmediateMacroCallerLoc(*this), *SrcMgr);
175
0
}
176
177
1.28k
std::pair<FullSourceLoc, StringRef> FullSourceLoc::getModuleImportLoc() const {
178
1.28k
  if (!isValid())
179
4
    return std::make_pair(FullSourceLoc(), StringRef());
180
1.28k
181
1.28k
  std::pair<SourceLocation, StringRef> ImportLoc =
182
1.28k
      SrcMgr->getModuleImportLoc(*this);
183
1.28k
  return std::make_pair(FullSourceLoc(ImportLoc.first, *SrcMgr),
184
1.28k
                        ImportLoc.second);
185
1.28k
}
186
187
71.2k
unsigned FullSourceLoc::getFileOffset() const {
188
71.2k
  assert(isValid());
189
71.2k
  return SrcMgr->getFileOffset(*this);
190
71.2k
}
191
192
46.8k
unsigned FullSourceLoc::getLineNumber(bool *Invalid) const {
193
46.8k
  assert(isValid());
194
46.8k
  return SrcMgr->getLineNumber(getFileID(), getFileOffset(), Invalid);
195
46.8k
}
196
197
24.1k
unsigned FullSourceLoc::getColumnNumber(bool *Invalid) const {
198
24.1k
  assert(isValid());
199
24.1k
  return SrcMgr->getColumnNumber(getFileID(), getFileOffset(), Invalid);
200
24.1k
}
201
202
24
const FileEntry *FullSourceLoc::getFileEntry() const {
203
24
  assert(isValid());
204
24
  return SrcMgr->getFileEntryForID(getFileID());
205
24
}
206
207
18.2k
unsigned FullSourceLoc::getExpansionLineNumber(bool *Invalid) const {
208
18.2k
  assert(isValid());
209
18.2k
  return SrcMgr->getExpansionLineNumber(*this, Invalid);
210
18.2k
}
211
212
16.0k
unsigned FullSourceLoc::getExpansionColumnNumber(bool *Invalid) const {
213
16.0k
  assert(isValid());
214
16.0k
  return SrcMgr->getExpansionColumnNumber(*this, Invalid);
215
16.0k
}
216
217
962
unsigned FullSourceLoc::getSpellingLineNumber(bool *Invalid) const {
218
962
  assert(isValid());
219
962
  return SrcMgr->getSpellingLineNumber(*this, Invalid);
220
962
}
221
222
324
unsigned FullSourceLoc::getSpellingColumnNumber(bool *Invalid) const {
223
324
  assert(isValid());
224
324
  return SrcMgr->getSpellingColumnNumber(*this, Invalid);
225
324
}
226
227
0
bool FullSourceLoc::isInSystemHeader() const {
228
0
  assert(isValid());
229
0
  return SrcMgr->isInSystemHeader(*this);
230
0
}
231
232
156k
bool FullSourceLoc::isBeforeInTranslationUnitThan(SourceLocation Loc) const {
233
156k
  assert(isValid());
234
156k
  return SrcMgr->isBeforeInTranslationUnit(*this, Loc);
235
156k
}
236
237
0
LLVM_DUMP_METHOD void FullSourceLoc::dump() const {
238
0
  SourceLocation::dump(*SrcMgr);
239
0
}
240
241
246
const char *FullSourceLoc::getCharacterData(bool *Invalid) const {
242
246
  assert(isValid());
243
246
  return SrcMgr->getCharacterData(*this, Invalid);
244
246
}
245
246
24.1k
StringRef FullSourceLoc::getBufferData(bool *Invalid) const {
247
24.1k
  assert(isValid());
248
24.1k
  return SrcMgr->getBuffer(SrcMgr->getFileID(*this), Invalid)->getBuffer();
249
24.1k
}
250
251
295k
std::pair<FileID, unsigned> FullSourceLoc::getDecomposedLoc() const {
252
295k
  return SrcMgr->getDecomposedLoc(*this);
253
295k
}