1 //===- SourceLocation.cpp - Compact identifier for Source Files -----------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines accessor methods for the FullSourceLoc class.
11 //===----------------------------------------------------------------------===//
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"
25 using namespace clang;
27 //===----------------------------------------------------------------------===//
28 // PrettyStackTraceLoc
29 //===----------------------------------------------------------------------===//
31 void PrettyStackTraceLoc::print(raw_ostream &OS) const {
36 OS << Message << '\n';
39 //===----------------------------------------------------------------------===//
41 //===----------------------------------------------------------------------===//
43 void SourceLocation::print(raw_ostream &OS, const SourceManager &SM)const{
45 OS << "<invalid loc>";
50 PresumedLoc PLoc = SM.getPresumedLoc(*this);
52 if (PLoc.isInvalid()) {
56 // The macro expansion and spelling pos is identical for file locs.
57 OS << PLoc.getFilename() << ':' << PLoc.getLine()
58 << ':' << PLoc.getColumn();
62 SM.getExpansionLoc(*this).print(OS, SM);
65 SM.getSpellingLoc(*this).print(OS, SM);
69 LLVM_DUMP_METHOD std::string
70 SourceLocation::printToString(const SourceManager &SM) const {
72 llvm::raw_string_ostream OS(S);
77 LLVM_DUMP_METHOD void SourceLocation::dump(const SourceManager &SM) const {
78 print(llvm::errs(), SM);
82 LLVM_DUMP_METHOD void SourceRange::dump(const SourceManager &SM) const {
83 print(llvm::errs(), SM);
87 static PresumedLoc PrintDifference(raw_ostream &OS, const SourceManager &SM,
88 SourceLocation Loc, PresumedLoc Previous) {
91 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
93 if (PLoc.isInvalid()) {
94 OS << "<invalid sloc>";
98 if (Previous.isInvalid() ||
99 strcmp(PLoc.getFilename(), Previous.getFilename()) != 0) {
100 OS << PLoc.getFilename() << ':' << PLoc.getLine() << ':'
102 } else if (Previous.isInvalid() || PLoc.getLine() != Previous.getLine()) {
103 OS << "line" << ':' << PLoc.getLine() << ':' << PLoc.getColumn();
105 OS << "col" << ':' << PLoc.getColumn();
109 auto PrintedLoc = PrintDifference(OS, SM, SM.getExpansionLoc(Loc), Previous);
112 PrintedLoc = PrintDifference(OS, SM, SM.getSpellingLoc(Loc), PrintedLoc);
117 void SourceRange::print(raw_ostream &OS, const SourceManager &SM) const {
120 auto PrintedLoc = PrintDifference(OS, SM, B, {});
123 PrintDifference(OS, SM, E, PrintedLoc);
128 LLVM_DUMP_METHOD std::string
129 SourceRange::printToString(const SourceManager &SM) const {
131 llvm::raw_string_ostream OS(S);
136 //===----------------------------------------------------------------------===//
138 //===----------------------------------------------------------------------===//
140 FileID FullSourceLoc::getFileID() const {
142 return SrcMgr->getFileID(*this);
145 FullSourceLoc FullSourceLoc::getExpansionLoc() const {
147 return FullSourceLoc(SrcMgr->getExpansionLoc(*this), *SrcMgr);
150 FullSourceLoc FullSourceLoc::getSpellingLoc() const {
152 return FullSourceLoc(SrcMgr->getSpellingLoc(*this), *SrcMgr);
155 FullSourceLoc FullSourceLoc::getFileLoc() const {
157 return FullSourceLoc(SrcMgr->getFileLoc(*this), *SrcMgr);
160 PresumedLoc FullSourceLoc::getPresumedLoc(bool UseLineDirectives) const {
162 return PresumedLoc();
164 return SrcMgr->getPresumedLoc(*this, UseLineDirectives);
167 bool FullSourceLoc::isMacroArgExpansion(FullSourceLoc *StartLoc) const {
169 return SrcMgr->isMacroArgExpansion(*this, StartLoc);
172 FullSourceLoc FullSourceLoc::getImmediateMacroCallerLoc() const {
174 return FullSourceLoc(SrcMgr->getImmediateMacroCallerLoc(*this), *SrcMgr);
177 std::pair<FullSourceLoc, StringRef> FullSourceLoc::getModuleImportLoc() const {
179 return std::make_pair(FullSourceLoc(), StringRef());
181 std::pair<SourceLocation, StringRef> ImportLoc =
182 SrcMgr->getModuleImportLoc(*this);
183 return std::make_pair(FullSourceLoc(ImportLoc.first, *SrcMgr),
187 unsigned FullSourceLoc::getFileOffset() const {
189 return SrcMgr->getFileOffset(*this);
192 unsigned FullSourceLoc::getLineNumber(bool *Invalid) const {
194 return SrcMgr->getLineNumber(getFileID(), getFileOffset(), Invalid);
197 unsigned FullSourceLoc::getColumnNumber(bool *Invalid) const {
199 return SrcMgr->getColumnNumber(getFileID(), getFileOffset(), Invalid);
202 const FileEntry *FullSourceLoc::getFileEntry() const {
204 return SrcMgr->getFileEntryForID(getFileID());
207 unsigned FullSourceLoc::getExpansionLineNumber(bool *Invalid) const {
209 return SrcMgr->getExpansionLineNumber(*this, Invalid);
212 unsigned FullSourceLoc::getExpansionColumnNumber(bool *Invalid) const {
214 return SrcMgr->getExpansionColumnNumber(*this, Invalid);
217 unsigned FullSourceLoc::getSpellingLineNumber(bool *Invalid) const {
219 return SrcMgr->getSpellingLineNumber(*this, Invalid);
222 unsigned FullSourceLoc::getSpellingColumnNumber(bool *Invalid) const {
224 return SrcMgr->getSpellingColumnNumber(*this, Invalid);
227 bool FullSourceLoc::isInSystemHeader() const {
229 return SrcMgr->isInSystemHeader(*this);
232 bool FullSourceLoc::isBeforeInTranslationUnitThan(SourceLocation Loc) const {
234 return SrcMgr->isBeforeInTranslationUnit(*this, Loc);
237 LLVM_DUMP_METHOD void FullSourceLoc::dump() const {
238 SourceLocation::dump(*SrcMgr);
241 const char *FullSourceLoc::getCharacterData(bool *Invalid) const {
243 return SrcMgr->getCharacterData(*this, Invalid);
246 StringRef FullSourceLoc::getBufferData(bool *Invalid) const {
248 return SrcMgr->getBuffer(SrcMgr->getFileID(*this), Invalid)->getBuffer();
251 std::pair<FileID, unsigned> FullSourceLoc::getDecomposedLoc() const {
252 return SrcMgr->getDecomposedLoc(*this);