Coverage Report

Created: 2020-02-25 14:32

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