/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 | 521k | const SourceLocation &X, llvm::FoldingSetNodeID &ID) { |
59 | 521k | ID.AddInteger(X.ID); |
60 | 521k | } |
61 | | |
62 | 481 | void SourceLocation::print(raw_ostream &OS, const SourceManager &SM)const{ |
63 | 481 | if (!isValid()) { |
64 | 229 | OS << "<invalid loc>"; |
65 | 229 | return; |
66 | 229 | } |
67 | | |
68 | 252 | if (isFileID()) { |
69 | 239 | PresumedLoc PLoc = SM.getPresumedLoc(*this); |
70 | | |
71 | 239 | 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 | 239 | OS << PLoc.getFilename() << ':' << PLoc.getLine() |
77 | 239 | << ':' << PLoc.getColumn(); |
78 | 239 | return; |
79 | 239 | } |
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 | 16 | SourceLocation::printToString(const SourceManager &SM) const { |
90 | 16 | std::string S; |
91 | 16 | llvm::raw_string_ostream OS(S); |
92 | 16 | print(OS, SM); |
93 | 16 | return S; |
94 | 16 | } |
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 S; |
153 | 4 | } |
154 | | |
155 | | //===----------------------------------------------------------------------===// |
156 | | // FullSourceLoc |
157 | | //===----------------------------------------------------------------------===// |
158 | | |
159 | 126k | FileID FullSourceLoc::getFileID() const { |
160 | 126k | assert(isValid()); |
161 | 0 | return SrcMgr->getFileID(*this); |
162 | 126k | } |
163 | | |
164 | 33.6k | FullSourceLoc FullSourceLoc::getExpansionLoc() const { |
165 | 33.6k | assert(isValid()); |
166 | 0 | return FullSourceLoc(SrcMgr->getExpansionLoc(*this), *SrcMgr); |
167 | 33.6k | } |
168 | | |
169 | 2.75k | FullSourceLoc FullSourceLoc::getSpellingLoc() const { |
170 | 2.75k | assert(isValid()); |
171 | 0 | return FullSourceLoc(SrcMgr->getSpellingLoc(*this), *SrcMgr); |
172 | 2.75k | } |
173 | | |
174 | 23.0k | FullSourceLoc FullSourceLoc::getFileLoc() const { |
175 | 23.0k | assert(isValid()); |
176 | 0 | return FullSourceLoc(SrcMgr->getFileLoc(*this), *SrcMgr); |
177 | 23.0k | } |
178 | | |
179 | 24.1k | PresumedLoc FullSourceLoc::getPresumedLoc(bool UseLineDirectives) const { |
180 | 24.1k | if (!isValid()) |
181 | 18 | return PresumedLoc(); |
182 | | |
183 | 24.1k | return SrcMgr->getPresumedLoc(*this, UseLineDirectives); |
184 | 24.1k | } |
185 | | |
186 | 503 | bool FullSourceLoc::isMacroArgExpansion(FullSourceLoc *StartLoc) const { |
187 | 503 | assert(isValid()); |
188 | 0 | return SrcMgr->isMacroArgExpansion(*this, StartLoc); |
189 | 503 | } |
190 | | |
191 | 0 | FullSourceLoc FullSourceLoc::getImmediateMacroCallerLoc() const { |
192 | 0 | assert(isValid()); |
193 | 0 | return FullSourceLoc(SrcMgr->getImmediateMacroCallerLoc(*this), *SrcMgr); |
194 | 0 | } |
195 | | |
196 | 1.18k | std::pair<FullSourceLoc, StringRef> FullSourceLoc::getModuleImportLoc() const { |
197 | 1.18k | if (!isValid()) |
198 | 6 | return std::make_pair(FullSourceLoc(), StringRef()); |
199 | | |
200 | 1.17k | std::pair<SourceLocation, StringRef> ImportLoc = |
201 | 1.17k | SrcMgr->getModuleImportLoc(*this); |
202 | 1.17k | return std::make_pair(FullSourceLoc(ImportLoc.first, *SrcMgr), |
203 | 1.17k | ImportLoc.second); |
204 | 1.18k | } |
205 | | |
206 | 71.3k | unsigned FullSourceLoc::getFileOffset() const { |
207 | 71.3k | assert(isValid()); |
208 | 0 | return SrcMgr->getFileOffset(*this); |
209 | 71.3k | } |
210 | | |
211 | 50.2k | unsigned FullSourceLoc::getLineNumber(bool *Invalid) const { |
212 | 50.2k | assert(isValid()); |
213 | 0 | return SrcMgr->getLineNumber(getFileID(), getFileOffset(), Invalid); |
214 | 50.2k | } |
215 | | |
216 | 20.9k | unsigned FullSourceLoc::getColumnNumber(bool *Invalid) const { |
217 | 20.9k | assert(isValid()); |
218 | 0 | return SrcMgr->getColumnNumber(getFileID(), getFileOffset(), Invalid); |
219 | 20.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.5k | unsigned FullSourceLoc::getExpansionColumnNumber(bool *Invalid) const { |
232 | 16.5k | assert(isValid()); |
233 | 0 | return SrcMgr->getExpansionColumnNumber(*this, Invalid); |
234 | 16.5k | } |
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 | 222k | bool FullSourceLoc::isBeforeInTranslationUnitThan(SourceLocation Loc) const { |
252 | 222k | assert(isValid()); |
253 | 0 | return SrcMgr->isBeforeInTranslationUnit(*this, Loc); |
254 | 222k | } |
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 | 20.9k | StringRef FullSourceLoc::getBufferData(bool *Invalid) const { |
266 | 20.9k | assert(isValid()); |
267 | 0 | return SrcMgr->getBufferData(SrcMgr->getFileID(*this), Invalid); |
268 | 20.9k | } |
269 | | |
270 | 423k | std::pair<FileID, unsigned> FullSourceLoc::getDecomposedLoc() const { |
271 | 423k | return SrcMgr->getDecomposedLoc(*this); |
272 | 423k | } |