blob: aa844f2cd26ca6146b040c69ddd6d06e297eef7b [file] [log] [blame]
Eugene Zelenko25cae5a22018-02-16 23:40:07 +00001//===- SourceLocation.cpp - Compact identifier for Source Files -----------===//
Ted Kremenek4edcb4d2007-10-25 16:02:43 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremenek4edcb4d2007-10-25 16:02:43 +00007//
8//===----------------------------------------------------------------------===//
9//
Ted Kremenek1daa3cf2007-12-12 22:39:36 +000010// This file defines accessor methods for the FullSourceLoc class.
Ted Kremenek4edcb4d2007-10-25 16:02:43 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Basic/SourceLocation.h"
Eugene Zelenko25cae5a22018-02-16 23:40:07 +000015#include "clang/Basic/LLVM.h"
Chris Lattnerbd61a952009-03-05 00:00:31 +000016#include "clang/Basic/PrettyStackTrace.h"
Ted Kremenek1daa3cf2007-12-12 22:39:36 +000017#include "clang/Basic/SourceManager.h"
Eugene Zelenko25cae5a22018-02-16 23:40:07 +000018#include "llvm/ADT/StringRef.h"
19#include "llvm/Support/Compiler.h"
20#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerbd61a952009-03-05 00:00:31 +000021#include "llvm/Support/raw_ostream.h"
Eugene Zelenko25cae5a22018-02-16 23:40:07 +000022#include <cassert>
23#include <string>
24#include <utility>
25
Ted Kremenek4edcb4d2007-10-25 16:02:43 +000026using namespace clang;
27
Chris Lattnerbd61a952009-03-05 00:00:31 +000028//===----------------------------------------------------------------------===//
29// PrettyStackTraceLoc
30//===----------------------------------------------------------------------===//
31
Chris Lattner0e62c1c2011-07-23 10:55:15 +000032void PrettyStackTraceLoc::print(raw_ostream &OS) const {
Chris Lattnerbd61a952009-03-05 00:00:31 +000033 if (Loc.isValid()) {
34 Loc.print(OS, SM);
35 OS << ": ";
36 }
37 OS << Message << '\n';
38}
39
40//===----------------------------------------------------------------------===//
41// SourceLocation
42//===----------------------------------------------------------------------===//
43
Chris Lattner0e62c1c2011-07-23 10:55:15 +000044void SourceLocation::print(raw_ostream &OS, const SourceManager &SM)const{
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000045 if (!isValid()) {
Chris Lattnerbd61a952009-03-05 00:00:31 +000046 OS << "<invalid loc>";
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000047 return;
48 }
Mike Stump11289f42009-09-09 15:08:12 +000049
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000050 if (isFileID()) {
51 PresumedLoc PLoc = SM.getPresumedLoc(*this);
Fangrui Song6907ce22018-07-30 19:24:48 +000052
Douglas Gregor453b0122010-11-12 07:15:47 +000053 if (PLoc.isInvalid()) {
54 OS << "<invalid>";
55 return;
56 }
Chandler Carruth64ee7822011-07-26 05:17:23 +000057 // The macro expansion and spelling pos is identical for file locs.
Chris Lattnerbd61a952009-03-05 00:00:31 +000058 OS << PLoc.getFilename() << ':' << PLoc.getLine()
59 << ':' << PLoc.getColumn();
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000060 return;
61 }
Mike Stump11289f42009-09-09 15:08:12 +000062
Chandler Carruth35f53202011-07-25 16:49:02 +000063 SM.getExpansionLoc(*this).print(OS, SM);
Chris Lattnerbd61a952009-03-05 00:00:31 +000064
65 OS << " <Spelling=";
66 SM.getSpellingLoc(*this).print(OS, SM);
67 OS << '>';
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000068}
69
Alp Tokeref6b0072014-01-04 13:47:14 +000070LLVM_DUMP_METHOD std::string
71SourceLocation::printToString(const SourceManager &SM) const {
Argyrios Kyrtzidisdc6bb492012-11-09 19:40:48 +000072 std::string S;
73 llvm::raw_string_ostream OS(S);
74 print(OS, SM);
Benjamin Krameradaf7952012-12-12 14:17:17 +000075 return OS.str();
Argyrios Kyrtzidisdc6bb492012-11-09 19:40:48 +000076}
77
Alp Tokeref6b0072014-01-04 13:47:14 +000078LLVM_DUMP_METHOD void SourceLocation::dump(const SourceManager &SM) const {
Chris Lattnerbd61a952009-03-05 00:00:31 +000079 print(llvm::errs(), SM);
Stephen Kelly3124ce72018-08-15 20:32:06 +000080 llvm::errs() << '\n';
Chris Lattnerbd61a952009-03-05 00:00:31 +000081}
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000082
Stephen Kellyb8e088602018-08-30 23:10:52 +000083LLVM_DUMP_METHOD void SourceRange::dump(const SourceManager &SM) const {
84 print(llvm::errs(), SM);
85 llvm::errs() << '\n';
86}
87
88static PresumedLoc PrintDifference(raw_ostream &OS, const SourceManager &SM,
89 SourceLocation Loc, PresumedLoc Previous) {
90 if (Loc.isFileID()) {
91
92 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
93
94 if (PLoc.isInvalid()) {
95 OS << "<invalid sloc>";
96 return Previous;
97 }
98
99 if (Previous.isInvalid() ||
100 strcmp(PLoc.getFilename(), Previous.getFilename()) != 0) {
101 OS << PLoc.getFilename() << ':' << PLoc.getLine() << ':'
102 << PLoc.getColumn();
103 } else if (Previous.isInvalid() || PLoc.getLine() != Previous.getLine()) {
104 OS << "line" << ':' << PLoc.getLine() << ':' << PLoc.getColumn();
105 } else {
106 OS << "col" << ':' << PLoc.getColumn();
107 }
108 return PLoc;
109 }
110 auto PrintedLoc = PrintDifference(OS, SM, SM.getExpansionLoc(Loc), Previous);
111
112 OS << " <Spelling=";
113 PrintedLoc = PrintDifference(OS, SM, SM.getSpellingLoc(Loc), PrintedLoc);
114 OS << '>';
115 return PrintedLoc;
116}
117
118void SourceRange::print(raw_ostream &OS, const SourceManager &SM) const {
119
120 OS << '<';
121 auto PrintedLoc = PrintDifference(OS, SM, B, {});
122 if (B != E) {
123 OS << ", ";
124 PrintDifference(OS, SM, E, PrintedLoc);
125 }
126 OS << '>';
127}
128
129LLVM_DUMP_METHOD std::string
130SourceRange::printToString(const SourceManager &SM) const {
131 std::string S;
132 llvm::raw_string_ostream OS(S);
133 print(OS, SM);
134 return OS.str();
135}
136
Chris Lattnerbd61a952009-03-05 00:00:31 +0000137//===----------------------------------------------------------------------===//
138// FullSourceLoc
139//===----------------------------------------------------------------------===//
140
Chris Lattner71dc14b2009-01-17 08:45:21 +0000141FileID FullSourceLoc::getFileID() const {
142 assert(isValid());
Chris Lattnercbc35ecb2009-01-19 07:46:45 +0000143 return SrcMgr->getFileID(*this);
Chris Lattner71dc14b2009-01-17 08:45:21 +0000144}
145
Chandler Carruth35f53202011-07-25 16:49:02 +0000146FullSourceLoc FullSourceLoc::getExpansionLoc() const {
Chris Lattnerfb1fd912009-01-16 23:03:56 +0000147 assert(isValid());
Chandler Carruth35f53202011-07-25 16:49:02 +0000148 return FullSourceLoc(SrcMgr->getExpansionLoc(*this), *SrcMgr);
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000149}
150
Chris Lattner53e384f2009-01-16 07:00:02 +0000151FullSourceLoc FullSourceLoc::getSpellingLoc() const {
152 assert(isValid());
Chris Lattnerfb1fd912009-01-16 23:03:56 +0000153 return FullSourceLoc(SrcMgr->getSpellingLoc(*this), *SrcMgr);
Chris Lattnerd9a663a2008-09-29 21:46:13 +0000154}
155
Christof Doumafb4a0452017-06-27 09:50:38 +0000156FullSourceLoc FullSourceLoc::getFileLoc() const {
157 assert(isValid());
158 return FullSourceLoc(SrcMgr->getFileLoc(*this), *SrcMgr);
159}
160
Christof Doumafb4a0452017-06-27 09:50:38 +0000161PresumedLoc FullSourceLoc::getPresumedLoc(bool UseLineDirectives) const {
162 if (!isValid())
163 return PresumedLoc();
164
165 return SrcMgr->getPresumedLoc(*this, UseLineDirectives);
166}
167
168bool FullSourceLoc::isMacroArgExpansion(FullSourceLoc *StartLoc) const {
169 assert(isValid());
170 return SrcMgr->isMacroArgExpansion(*this, StartLoc);
171}
172
173FullSourceLoc FullSourceLoc::getImmediateMacroCallerLoc() const {
174 assert(isValid());
175 return FullSourceLoc(SrcMgr->getImmediateMacroCallerLoc(*this), *SrcMgr);
176}
177
178std::pair<FullSourceLoc, StringRef> FullSourceLoc::getModuleImportLoc() const {
179 if (!isValid())
180 return std::make_pair(FullSourceLoc(), StringRef());
181
182 std::pair<SourceLocation, StringRef> ImportLoc =
183 SrcMgr->getModuleImportLoc(*this);
184 return std::make_pair(FullSourceLoc(ImportLoc.first, *SrcMgr),
185 ImportLoc.second);
186}
187
188unsigned FullSourceLoc::getFileOffset() const {
189 assert(isValid());
190 return SrcMgr->getFileOffset(*this);
191}
192
193unsigned FullSourceLoc::getLineNumber(bool *Invalid) const {
194 assert(isValid());
195 return SrcMgr->getLineNumber(getFileID(), getFileOffset(), Invalid);
196}
197
198unsigned FullSourceLoc::getColumnNumber(bool *Invalid) const {
199 assert(isValid());
200 return SrcMgr->getColumnNumber(getFileID(), getFileOffset(), Invalid);
201}
202
Christof Doumafb4a0452017-06-27 09:50:38 +0000203const FileEntry *FullSourceLoc::getFileEntry() const {
204 assert(isValid());
205 return SrcMgr->getFileEntryForID(getFileID());
206}
207
Chandler Carruthd48db212011-07-25 21:09:52 +0000208unsigned FullSourceLoc::getExpansionLineNumber(bool *Invalid) const {
Chris Lattnerfb1fd912009-01-16 23:03:56 +0000209 assert(isValid());
Chandler Carruthd48db212011-07-25 21:09:52 +0000210 return SrcMgr->getExpansionLineNumber(*this, Invalid);
Ted Kremenek406192d2008-04-03 17:55:15 +0000211}
212
Chandler Carruth42f35f92011-07-25 20:57:57 +0000213unsigned FullSourceLoc::getExpansionColumnNumber(bool *Invalid) const {
Chris Lattnerfb1fd912009-01-16 23:03:56 +0000214 assert(isValid());
Chandler Carruth42f35f92011-07-25 20:57:57 +0000215 return SrcMgr->getExpansionColumnNumber(*this, Invalid);
Ted Kremenek406192d2008-04-03 17:55:15 +0000216}
217
Douglas Gregora71b9d02010-03-16 20:53:17 +0000218unsigned FullSourceLoc::getSpellingLineNumber(bool *Invalid) const {
Chris Lattnerfb1fd912009-01-16 23:03:56 +0000219 assert(isValid());
Douglas Gregora71b9d02010-03-16 20:53:17 +0000220 return SrcMgr->getSpellingLineNumber(*this, Invalid);
Chris Lattnerd9a663a2008-09-29 21:46:13 +0000221}
222
Douglas Gregora71b9d02010-03-16 20:53:17 +0000223unsigned FullSourceLoc::getSpellingColumnNumber(bool *Invalid) const {
Chris Lattnerfb1fd912009-01-16 23:03:56 +0000224 assert(isValid());
Douglas Gregora71b9d02010-03-16 20:53:17 +0000225 return SrcMgr->getSpellingColumnNumber(*this, Invalid);
Chris Lattnerd9a663a2008-09-29 21:46:13 +0000226}
227
Nico Weber4c311642008-08-10 19:59:06 +0000228bool FullSourceLoc::isInSystemHeader() const {
Chris Lattnerfb1fd912009-01-16 23:03:56 +0000229 assert(isValid());
230 return SrcMgr->isInSystemHeader(*this);
Nico Weber4c311642008-08-10 19:59:06 +0000231}
232
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000233bool FullSourceLoc::isBeforeInTranslationUnitThan(SourceLocation Loc) const {
234 assert(isValid());
235 return SrcMgr->isBeforeInTranslationUnit(*this, Loc);
236}
237
Alp Tokeref6b0072014-01-04 13:47:14 +0000238LLVM_DUMP_METHOD void FullSourceLoc::dump() const {
Benjamin Kramere60db7b2012-02-26 16:55:50 +0000239 SourceLocation::dump(*SrcMgr);
240}
241
Douglas Gregor42fe8582010-03-16 20:46:42 +0000242const char *FullSourceLoc::getCharacterData(bool *Invalid) const {
Chris Lattnerfcc0a5a2009-01-16 22:59:51 +0000243 assert(isValid());
Douglas Gregor42fe8582010-03-16 20:46:42 +0000244 return SrcMgr->getCharacterData(*this, Invalid);
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000245}
246
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000247StringRef FullSourceLoc::getBufferData(bool *Invalid) const {
Rafael Espindolab694a0d2014-08-18 18:17:32 +0000248 assert(isValid());
Aaron Ballman825fb3c2015-06-09 12:04:17 +0000249 return SrcMgr->getBuffer(SrcMgr->getFileID(*this), Invalid)->getBuffer();
Ted Kremenekbf165542009-01-28 20:46:26 +0000250}
Ted Kremenek3ceb7ca2009-03-10 05:13:43 +0000251
252std::pair<FileID, unsigned> FullSourceLoc::getDecomposedLoc() const {
253 return SrcMgr->getDecomposedLoc(*this);
254}