Eugene Zelenko | 25cae5a2 | 2018-02-16 23:40:07 +0000 | [diff] [blame] | 1 | //===- SourceLocation.cpp - Compact identifier for Source Files -----------===// |
Ted Kremenek | 4edcb4d | 2007-10-25 16:02:43 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Ted Kremenek | 4edcb4d | 2007-10-25 16:02:43 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Ted Kremenek | 1daa3cf | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 9 | // This file defines accessor methods for the FullSourceLoc class. |
Ted Kremenek | 4edcb4d | 2007-10-25 16:02:43 +0000 | [diff] [blame] | 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "clang/Basic/SourceLocation.h" |
Eugene Zelenko | 25cae5a2 | 2018-02-16 23:40:07 +0000 | [diff] [blame] | 14 | #include "clang/Basic/LLVM.h" |
Chris Lattner | bd61a95 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 15 | #include "clang/Basic/PrettyStackTrace.h" |
Ted Kremenek | 1daa3cf | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 16 | #include "clang/Basic/SourceManager.h" |
Eugene Zelenko | 25cae5a2 | 2018-02-16 23:40:07 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringRef.h" |
| 18 | #include "llvm/Support/Compiler.h" |
| 19 | #include "llvm/Support/MemoryBuffer.h" |
Chris Lattner | bd61a95 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 25cae5a2 | 2018-02-16 23:40:07 +0000 | [diff] [blame] | 21 | #include <cassert> |
| 22 | #include <string> |
| 23 | #include <utility> |
| 24 | |
Ted Kremenek | 4edcb4d | 2007-10-25 16:02:43 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | |
Chris Lattner | bd61a95 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 27 | //===----------------------------------------------------------------------===// |
| 28 | // PrettyStackTraceLoc |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 31 | void PrettyStackTraceLoc::print(raw_ostream &OS) const { |
Chris Lattner | bd61a95 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 32 | if (Loc.isValid()) { |
| 33 | Loc.print(OS, SM); |
| 34 | OS << ": "; |
| 35 | } |
| 36 | OS << Message << '\n'; |
| 37 | } |
| 38 | |
| 39 | //===----------------------------------------------------------------------===// |
| 40 | // SourceLocation |
| 41 | //===----------------------------------------------------------------------===// |
| 42 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 43 | void SourceLocation::print(raw_ostream &OS, const SourceManager &SM)const{ |
Chris Lattner | f1ca7d3 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 44 | if (!isValid()) { |
Chris Lattner | bd61a95 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 45 | OS << "<invalid loc>"; |
Chris Lattner | f1ca7d3 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 46 | return; |
| 47 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 48 | |
Chris Lattner | f1ca7d3 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 49 | if (isFileID()) { |
| 50 | PresumedLoc PLoc = SM.getPresumedLoc(*this); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 51 | |
Douglas Gregor | 453b012 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 52 | if (PLoc.isInvalid()) { |
| 53 | OS << "<invalid>"; |
| 54 | return; |
| 55 | } |
Chandler Carruth | 64ee782 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 56 | // The macro expansion and spelling pos is identical for file locs. |
Chris Lattner | bd61a95 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 57 | OS << PLoc.getFilename() << ':' << PLoc.getLine() |
| 58 | << ':' << PLoc.getColumn(); |
Chris Lattner | f1ca7d3 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 59 | return; |
| 60 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 61 | |
Chandler Carruth | 35f5320 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 62 | SM.getExpansionLoc(*this).print(OS, SM); |
Chris Lattner | bd61a95 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 63 | |
| 64 | OS << " <Spelling="; |
| 65 | SM.getSpellingLoc(*this).print(OS, SM); |
| 66 | OS << '>'; |
Chris Lattner | f1ca7d3 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Alp Toker | ef6b007 | 2014-01-04 13:47:14 +0000 | [diff] [blame] | 69 | LLVM_DUMP_METHOD std::string |
| 70 | SourceLocation::printToString(const SourceManager &SM) const { |
Argyrios Kyrtzidis | dc6bb49 | 2012-11-09 19:40:48 +0000 | [diff] [blame] | 71 | std::string S; |
| 72 | llvm::raw_string_ostream OS(S); |
| 73 | print(OS, SM); |
Benjamin Kramer | adaf795 | 2012-12-12 14:17:17 +0000 | [diff] [blame] | 74 | return OS.str(); |
Argyrios Kyrtzidis | dc6bb49 | 2012-11-09 19:40:48 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Alp Toker | ef6b007 | 2014-01-04 13:47:14 +0000 | [diff] [blame] | 77 | LLVM_DUMP_METHOD void SourceLocation::dump(const SourceManager &SM) const { |
Chris Lattner | bd61a95 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 78 | print(llvm::errs(), SM); |
Stephen Kelly | 3124ce7 | 2018-08-15 20:32:06 +0000 | [diff] [blame] | 79 | llvm::errs() << '\n'; |
Chris Lattner | bd61a95 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 80 | } |
Chris Lattner | f1ca7d3 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 81 | |
Stephen Kelly | b8e08860 | 2018-08-30 23:10:52 +0000 | [diff] [blame] | 82 | LLVM_DUMP_METHOD void SourceRange::dump(const SourceManager &SM) const { |
| 83 | print(llvm::errs(), SM); |
| 84 | llvm::errs() << '\n'; |
| 85 | } |
| 86 | |
| 87 | static PresumedLoc PrintDifference(raw_ostream &OS, const SourceManager &SM, |
| 88 | SourceLocation Loc, PresumedLoc Previous) { |
| 89 | if (Loc.isFileID()) { |
| 90 | |
| 91 | PresumedLoc PLoc = SM.getPresumedLoc(Loc); |
| 92 | |
| 93 | if (PLoc.isInvalid()) { |
| 94 | OS << "<invalid sloc>"; |
| 95 | return Previous; |
| 96 | } |
| 97 | |
| 98 | if (Previous.isInvalid() || |
| 99 | strcmp(PLoc.getFilename(), Previous.getFilename()) != 0) { |
| 100 | OS << PLoc.getFilename() << ':' << PLoc.getLine() << ':' |
| 101 | << PLoc.getColumn(); |
| 102 | } else if (Previous.isInvalid() || PLoc.getLine() != Previous.getLine()) { |
| 103 | OS << "line" << ':' << PLoc.getLine() << ':' << PLoc.getColumn(); |
| 104 | } else { |
| 105 | OS << "col" << ':' << PLoc.getColumn(); |
| 106 | } |
| 107 | return PLoc; |
| 108 | } |
| 109 | auto PrintedLoc = PrintDifference(OS, SM, SM.getExpansionLoc(Loc), Previous); |
| 110 | |
| 111 | OS << " <Spelling="; |
| 112 | PrintedLoc = PrintDifference(OS, SM, SM.getSpellingLoc(Loc), PrintedLoc); |
| 113 | OS << '>'; |
| 114 | return PrintedLoc; |
| 115 | } |
| 116 | |
| 117 | void SourceRange::print(raw_ostream &OS, const SourceManager &SM) const { |
| 118 | |
| 119 | OS << '<'; |
| 120 | auto PrintedLoc = PrintDifference(OS, SM, B, {}); |
| 121 | if (B != E) { |
| 122 | OS << ", "; |
| 123 | PrintDifference(OS, SM, E, PrintedLoc); |
| 124 | } |
| 125 | OS << '>'; |
| 126 | } |
| 127 | |
| 128 | LLVM_DUMP_METHOD std::string |
| 129 | SourceRange::printToString(const SourceManager &SM) const { |
| 130 | std::string S; |
| 131 | llvm::raw_string_ostream OS(S); |
| 132 | print(OS, SM); |
| 133 | return OS.str(); |
| 134 | } |
| 135 | |
Chris Lattner | bd61a95 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 136 | //===----------------------------------------------------------------------===// |
| 137 | // FullSourceLoc |
| 138 | //===----------------------------------------------------------------------===// |
| 139 | |
Chris Lattner | 71dc14b | 2009-01-17 08:45:21 +0000 | [diff] [blame] | 140 | FileID FullSourceLoc::getFileID() const { |
| 141 | assert(isValid()); |
Chris Lattner | cbc35ecb | 2009-01-19 07:46:45 +0000 | [diff] [blame] | 142 | return SrcMgr->getFileID(*this); |
Chris Lattner | 71dc14b | 2009-01-17 08:45:21 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Chandler Carruth | 35f5320 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 145 | FullSourceLoc FullSourceLoc::getExpansionLoc() const { |
Chris Lattner | fb1fd91 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 146 | assert(isValid()); |
Chandler Carruth | 35f5320 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 147 | return FullSourceLoc(SrcMgr->getExpansionLoc(*this), *SrcMgr); |
Ted Kremenek | 1daa3cf | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Chris Lattner | 53e384f | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 150 | FullSourceLoc FullSourceLoc::getSpellingLoc() const { |
| 151 | assert(isValid()); |
Chris Lattner | fb1fd91 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 152 | return FullSourceLoc(SrcMgr->getSpellingLoc(*this), *SrcMgr); |
Chris Lattner | d9a663a | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Christof Douma | fb4a045 | 2017-06-27 09:50:38 +0000 | [diff] [blame] | 155 | FullSourceLoc FullSourceLoc::getFileLoc() const { |
| 156 | assert(isValid()); |
| 157 | return FullSourceLoc(SrcMgr->getFileLoc(*this), *SrcMgr); |
| 158 | } |
| 159 | |
Christof Douma | fb4a045 | 2017-06-27 09:50:38 +0000 | [diff] [blame] | 160 | PresumedLoc FullSourceLoc::getPresumedLoc(bool UseLineDirectives) const { |
| 161 | if (!isValid()) |
| 162 | return PresumedLoc(); |
| 163 | |
| 164 | return SrcMgr->getPresumedLoc(*this, UseLineDirectives); |
| 165 | } |
| 166 | |
| 167 | bool FullSourceLoc::isMacroArgExpansion(FullSourceLoc *StartLoc) const { |
| 168 | assert(isValid()); |
| 169 | return SrcMgr->isMacroArgExpansion(*this, StartLoc); |
| 170 | } |
| 171 | |
| 172 | FullSourceLoc FullSourceLoc::getImmediateMacroCallerLoc() const { |
| 173 | assert(isValid()); |
| 174 | return FullSourceLoc(SrcMgr->getImmediateMacroCallerLoc(*this), *SrcMgr); |
| 175 | } |
| 176 | |
| 177 | std::pair<FullSourceLoc, StringRef> FullSourceLoc::getModuleImportLoc() const { |
| 178 | if (!isValid()) |
| 179 | return std::make_pair(FullSourceLoc(), StringRef()); |
| 180 | |
| 181 | std::pair<SourceLocation, StringRef> ImportLoc = |
| 182 | SrcMgr->getModuleImportLoc(*this); |
| 183 | return std::make_pair(FullSourceLoc(ImportLoc.first, *SrcMgr), |
| 184 | ImportLoc.second); |
| 185 | } |
| 186 | |
| 187 | unsigned FullSourceLoc::getFileOffset() const { |
| 188 | assert(isValid()); |
| 189 | return SrcMgr->getFileOffset(*this); |
| 190 | } |
| 191 | |
| 192 | unsigned FullSourceLoc::getLineNumber(bool *Invalid) const { |
| 193 | assert(isValid()); |
| 194 | return SrcMgr->getLineNumber(getFileID(), getFileOffset(), Invalid); |
| 195 | } |
| 196 | |
| 197 | unsigned FullSourceLoc::getColumnNumber(bool *Invalid) const { |
| 198 | assert(isValid()); |
| 199 | return SrcMgr->getColumnNumber(getFileID(), getFileOffset(), Invalid); |
| 200 | } |
| 201 | |
Christof Douma | fb4a045 | 2017-06-27 09:50:38 +0000 | [diff] [blame] | 202 | const FileEntry *FullSourceLoc::getFileEntry() const { |
| 203 | assert(isValid()); |
| 204 | return SrcMgr->getFileEntryForID(getFileID()); |
| 205 | } |
| 206 | |
Chandler Carruth | d48db21 | 2011-07-25 21:09:52 +0000 | [diff] [blame] | 207 | unsigned FullSourceLoc::getExpansionLineNumber(bool *Invalid) const { |
Chris Lattner | fb1fd91 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 208 | assert(isValid()); |
Chandler Carruth | d48db21 | 2011-07-25 21:09:52 +0000 | [diff] [blame] | 209 | return SrcMgr->getExpansionLineNumber(*this, Invalid); |
Ted Kremenek | 406192d | 2008-04-03 17:55:15 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Chandler Carruth | 42f35f9 | 2011-07-25 20:57:57 +0000 | [diff] [blame] | 212 | unsigned FullSourceLoc::getExpansionColumnNumber(bool *Invalid) const { |
Chris Lattner | fb1fd91 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 213 | assert(isValid()); |
Chandler Carruth | 42f35f9 | 2011-07-25 20:57:57 +0000 | [diff] [blame] | 214 | return SrcMgr->getExpansionColumnNumber(*this, Invalid); |
Ted Kremenek | 406192d | 2008-04-03 17:55:15 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Douglas Gregor | a71b9d0 | 2010-03-16 20:53:17 +0000 | [diff] [blame] | 217 | unsigned FullSourceLoc::getSpellingLineNumber(bool *Invalid) const { |
Chris Lattner | fb1fd91 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 218 | assert(isValid()); |
Douglas Gregor | a71b9d0 | 2010-03-16 20:53:17 +0000 | [diff] [blame] | 219 | return SrcMgr->getSpellingLineNumber(*this, Invalid); |
Chris Lattner | d9a663a | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Douglas Gregor | a71b9d0 | 2010-03-16 20:53:17 +0000 | [diff] [blame] | 222 | unsigned FullSourceLoc::getSpellingColumnNumber(bool *Invalid) const { |
Chris Lattner | fb1fd91 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 223 | assert(isValid()); |
Douglas Gregor | a71b9d0 | 2010-03-16 20:53:17 +0000 | [diff] [blame] | 224 | return SrcMgr->getSpellingColumnNumber(*this, Invalid); |
Chris Lattner | d9a663a | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Nico Weber | 4c31164 | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 227 | bool FullSourceLoc::isInSystemHeader() const { |
Chris Lattner | fb1fd91 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 228 | assert(isValid()); |
| 229 | return SrcMgr->isInSystemHeader(*this); |
Nico Weber | 4c31164 | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 232 | bool FullSourceLoc::isBeforeInTranslationUnitThan(SourceLocation Loc) const { |
| 233 | assert(isValid()); |
| 234 | return SrcMgr->isBeforeInTranslationUnit(*this, Loc); |
| 235 | } |
| 236 | |
Alp Toker | ef6b007 | 2014-01-04 13:47:14 +0000 | [diff] [blame] | 237 | LLVM_DUMP_METHOD void FullSourceLoc::dump() const { |
Benjamin Kramer | e60db7b | 2012-02-26 16:55:50 +0000 | [diff] [blame] | 238 | SourceLocation::dump(*SrcMgr); |
| 239 | } |
| 240 | |
Douglas Gregor | 42fe858 | 2010-03-16 20:46:42 +0000 | [diff] [blame] | 241 | const char *FullSourceLoc::getCharacterData(bool *Invalid) const { |
Chris Lattner | fcc0a5a | 2009-01-16 22:59:51 +0000 | [diff] [blame] | 242 | assert(isValid()); |
Douglas Gregor | 42fe858 | 2010-03-16 20:46:42 +0000 | [diff] [blame] | 243 | return SrcMgr->getCharacterData(*this, Invalid); |
Ted Kremenek | 1daa3cf | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 246 | StringRef FullSourceLoc::getBufferData(bool *Invalid) const { |
Rafael Espindola | b694a0d | 2014-08-18 18:17:32 +0000 | [diff] [blame] | 247 | assert(isValid()); |
Aaron Ballman | 825fb3c | 2015-06-09 12:04:17 +0000 | [diff] [blame] | 248 | return SrcMgr->getBuffer(SrcMgr->getFileID(*this), Invalid)->getBuffer(); |
Ted Kremenek | bf16554 | 2009-01-28 20:46:26 +0000 | [diff] [blame] | 249 | } |
Ted Kremenek | 3ceb7ca | 2009-03-10 05:13:43 +0000 | [diff] [blame] | 250 | |
| 251 | std::pair<FileID, unsigned> FullSourceLoc::getDecomposedLoc() const { |
| 252 | return SrcMgr->getDecomposedLoc(*this); |
| 253 | } |