Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/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
410
void SourceLocation::print(raw_ostream &OS, const SourceManager &SM)const{
44
410
  if (!isValid()) {
45
222
    OS << "<invalid loc>";
46
222
    return;
47
222
  }
48
188
49
188
  if (isFileID()) {
50
175
    PresumedLoc PLoc = SM.getPresumedLoc(*this);
51
175
52
175
    if (PLoc.isInvalid()) {
53
0
      OS << "<invalid>";
54
0
      return;
55
0
    }
56
175
    // The macro expansion and spelling pos is identical for file locs.
57
175
    OS << PLoc.getFilename() << ':' << PLoc.getLine()
58
175
       << ':' << PLoc.getColumn();
59
175
    return;
60
175
  }
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
7
        
strcmp(PLoc.getFilename(), Previous.getFilename()) != 03
) {
100
5
      OS << PLoc.getFilename() << ':' << PLoc.getLine() << ':'
101
5
         << PLoc.getColumn();
102
5
    } else 
if (2
Previous.isInvalid()2
||
PLoc.getLine() != Previous.getLine()2
) {
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
98.5k
FileID FullSourceLoc::getFileID() const {
141
98.5k
  assert(isValid());
142
98.5k
  return SrcMgr->getFileID(*this);
143
98.5k
}
144
145
21.2k
FullSourceLoc FullSourceLoc::getExpansionLoc() const {
146
21.2k
  assert(isValid());
147
21.2k
  return FullSourceLoc(SrcMgr->getExpansionLoc(*this), *SrcMgr);
148
21.2k
}
149
150
2.51k
FullSourceLoc FullSourceLoc::getSpellingLoc() const {
151
2.51k
  assert(isValid());
152
2.51k
  return FullSourceLoc(SrcMgr->getSpellingLoc(*this), *SrcMgr);
153
2.51k
}
154
155
21.7k
FullSourceLoc FullSourceLoc::getFileLoc() const {
156
21.7k
  assert(isValid());
157
21.7k
  return FullSourceLoc(SrcMgr->getFileLoc(*this), *SrcMgr);
158
21.7k
}
159
160
22.9k
PresumedLoc FullSourceLoc::getPresumedLoc(bool UseLineDirectives) const {
161
22.9k
  if (!isValid())
162
12
    return PresumedLoc();
163
22.9k
164
22.9k
  return SrcMgr->getPresumedLoc(*this, UseLineDirectives);
165
22.9k
}
166
167
481
bool FullSourceLoc::isMacroArgExpansion(FullSourceLoc *StartLoc) const {
168
481
  assert(isValid());
169
481
  return SrcMgr->isMacroArgExpansion(*this, StartLoc);
170
481
}
171
172
0
FullSourceLoc FullSourceLoc::getImmediateMacroCallerLoc() const {
173
0
  assert(isValid());
174
0
  return FullSourceLoc(SrcMgr->getImmediateMacroCallerLoc(*this), *SrcMgr);
175
0
}
176
177
1.53k
std::pair<FullSourceLoc, StringRef> FullSourceLoc::getModuleImportLoc() const {
178
1.53k
  if (!isValid())
179
4
    return std::make_pair(FullSourceLoc(), StringRef());
180
1.53k
181
1.53k
  std::pair<SourceLocation, StringRef> ImportLoc =
182
1.53k
      SrcMgr->getModuleImportLoc(*this);
183
1.53k
  return std::make_pair(FullSourceLoc(ImportLoc.first, *SrcMgr),
184
1.53k
                        ImportLoc.second);
185
1.53k
}
186
187
57.2k
unsigned FullSourceLoc::getFileOffset() const {
188
57.2k
  assert(isValid());
189
57.2k
  return SrcMgr->getFileOffset(*this);
190
57.2k
}
191
192
37.3k
unsigned FullSourceLoc::getLineNumber(bool *Invalid) const {
193
37.3k
  assert(isValid());
194
37.3k
  return SrcMgr->getLineNumber(getFileID(), getFileOffset(), Invalid);
195
37.3k
}
196
197
19.7k
unsigned FullSourceLoc::getColumnNumber(bool *Invalid) const {
198
19.7k
  assert(isValid());
199
19.7k
  return SrcMgr->getColumnNumber(getFileID(), getFileOffset(), Invalid);
200
19.7k
}
201
202
16
const FileEntry *FullSourceLoc::getFileEntry() const {
203
16
  assert(isValid());
204
16
  return SrcMgr->getFileEntryForID(getFileID());
205
16
}
206
207
17.5k
unsigned FullSourceLoc::getExpansionLineNumber(bool *Invalid) const {
208
17.5k
  assert(isValid());
209
17.5k
  return SrcMgr->getExpansionLineNumber(*this, Invalid);
210
17.5k
}
211
212
15.4k
unsigned FullSourceLoc::getExpansionColumnNumber(bool *Invalid) const {
213
15.4k
  assert(isValid());
214
15.4k
  return SrcMgr->getExpansionColumnNumber(*this, Invalid);
215
15.4k
}
216
217
932
unsigned FullSourceLoc::getSpellingLineNumber(bool *Invalid) const {
218
932
  assert(isValid());
219
932
  return SrcMgr->getSpellingLineNumber(*this, Invalid);
220
932
}
221
222
312
unsigned FullSourceLoc::getSpellingColumnNumber(bool *Invalid) const {
223
312
  assert(isValid());
224
312
  return SrcMgr->getSpellingColumnNumber(*this, Invalid);
225
312
}
226
227
0
bool FullSourceLoc::isInSystemHeader() const {
228
0
  assert(isValid());
229
0
  return SrcMgr->isInSystemHeader(*this);
230
0
}
231
232
79.9k
bool FullSourceLoc::isBeforeInTranslationUnitThan(SourceLocation Loc) const {
233
79.9k
  assert(isValid());
234
79.9k
  return SrcMgr->isBeforeInTranslationUnit(*this, Loc);
235
79.9k
}
236
237
0
LLVM_DUMP_METHOD void FullSourceLoc::dump() const {
238
0
  SourceLocation::dump(*SrcMgr);
239
0
}
240
241
200
const char *FullSourceLoc::getCharacterData(bool *Invalid) const {
242
200
  assert(isValid());
243
200
  return SrcMgr->getCharacterData(*this, Invalid);
244
200
}
245
246
19.6k
StringRef FullSourceLoc::getBufferData(bool *Invalid) const {
247
19.6k
  assert(isValid());
248
19.6k
  return SrcMgr->getBuffer(SrcMgr->getFileID(*this), Invalid)->getBuffer();
249
19.6k
}
250
251
137k
std::pair<FileID, unsigned> FullSourceLoc::getDecomposedLoc() const {
252
137k
  return SrcMgr->getDecomposedLoc(*this);
253
137k
}