blob: 182209117966f366c1602f27970cec632d9672db [file] [log] [blame]
Ted Kremenek19a95bc2007-10-25 16:02:43 +00001//==--- SourceLocation.cpp - Compact identifier for Source Files -*- C++ -*-==//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-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 Kremenek19a95bc2007-10-25 16:02:43 +00007//
8//===----------------------------------------------------------------------===//
9//
Ted Kremenek9c728dc2007-12-12 22:39:36 +000010// This file defines accessor methods for the FullSourceLoc class.
Ted Kremenek19a95bc2007-10-25 16:02:43 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Basic/SourceLocation.h"
Chris Lattnerae50fa02009-03-05 00:00:31 +000015#include "clang/Basic/PrettyStackTrace.h"
Ted Kremenek9c728dc2007-12-12 22:39:36 +000016#include "clang/Basic/SourceManager.h"
Ted Kremenek3632a352009-01-28 20:46:26 +000017#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerae50fa02009-03-05 00:00:31 +000018#include "llvm/Support/raw_ostream.h"
Chris Lattner3daed522009-03-02 22:20:04 +000019#include <cstdio>
Ted Kremenek19a95bc2007-10-25 16:02:43 +000020using namespace clang;
21
Chris Lattnerae50fa02009-03-05 00:00:31 +000022//===----------------------------------------------------------------------===//
23// PrettyStackTraceLoc
24//===----------------------------------------------------------------------===//
25
Chris Lattner5f9e2722011-07-23 10:55:15 +000026void PrettyStackTraceLoc::print(raw_ostream &OS) const {
Chris Lattnerae50fa02009-03-05 00:00:31 +000027 if (Loc.isValid()) {
28 Loc.print(OS, SM);
29 OS << ": ";
30 }
31 OS << Message << '\n';
32}
33
34//===----------------------------------------------------------------------===//
35// SourceLocation
36//===----------------------------------------------------------------------===//
37
Chris Lattner5f9e2722011-07-23 10:55:15 +000038void SourceLocation::print(raw_ostream &OS, const SourceManager &SM)const{
Chris Lattnerb9c3f962009-01-27 07:57:44 +000039 if (!isValid()) {
Chris Lattnerae50fa02009-03-05 00:00:31 +000040 OS << "<invalid loc>";
Chris Lattnerb9c3f962009-01-27 07:57:44 +000041 return;
42 }
Mike Stump1eb44332009-09-09 15:08:12 +000043
Chris Lattnerb9c3f962009-01-27 07:57:44 +000044 if (isFileID()) {
45 PresumedLoc PLoc = SM.getPresumedLoc(*this);
Douglas Gregorcb7b1e12010-11-12 07:15:47 +000046
47 if (PLoc.isInvalid()) {
48 OS << "<invalid>";
49 return;
50 }
Chandler Carruth3201f382011-07-26 05:17:23 +000051 // The macro expansion and spelling pos is identical for file locs.
Chris Lattnerae50fa02009-03-05 00:00:31 +000052 OS << PLoc.getFilename() << ':' << PLoc.getLine()
53 << ':' << PLoc.getColumn();
Chris Lattnerb9c3f962009-01-27 07:57:44 +000054 return;
55 }
Mike Stump1eb44332009-09-09 15:08:12 +000056
Chandler Carruth40278532011-07-25 16:49:02 +000057 SM.getExpansionLoc(*this).print(OS, SM);
Chris Lattnerae50fa02009-03-05 00:00:31 +000058
59 OS << " <Spelling=";
60 SM.getSpellingLoc(*this).print(OS, SM);
61 OS << '>';
Chris Lattnerb9c3f962009-01-27 07:57:44 +000062}
63
Argyrios Kyrtzidis94109282012-11-09 19:40:48 +000064std::string SourceLocation::printToString(const SourceManager &SM) const {
65 std::string S;
66 llvm::raw_string_ostream OS(S);
67 print(OS, SM);
Benjamin Kramer21fa5d12012-12-12 14:17:17 +000068 return OS.str();
Argyrios Kyrtzidis94109282012-11-09 19:40:48 +000069}
70
Chris Lattnerae50fa02009-03-05 00:00:31 +000071void SourceLocation::dump(const SourceManager &SM) const {
72 print(llvm::errs(), SM);
Chris Lattnerae50fa02009-03-05 00:00:31 +000073}
Chris Lattnerb9c3f962009-01-27 07:57:44 +000074
Chris Lattnerae50fa02009-03-05 00:00:31 +000075//===----------------------------------------------------------------------===//
76// FullSourceLoc
77//===----------------------------------------------------------------------===//
78
Chris Lattner3b4d5e92009-01-17 08:45:21 +000079FileID FullSourceLoc::getFileID() const {
80 assert(isValid());
Chris Lattnera11d6172009-01-19 07:46:45 +000081 return SrcMgr->getFileID(*this);
Chris Lattner3b4d5e92009-01-17 08:45:21 +000082}
83
84
Chandler Carruth40278532011-07-25 16:49:02 +000085FullSourceLoc FullSourceLoc::getExpansionLoc() const {
Chris Lattnera50bd542009-01-16 23:03:56 +000086 assert(isValid());
Chandler Carruth40278532011-07-25 16:49:02 +000087 return FullSourceLoc(SrcMgr->getExpansionLoc(*this), *SrcMgr);
Ted Kremenek9c728dc2007-12-12 22:39:36 +000088}
89
Chris Lattnerdf7c17a2009-01-16 07:00:02 +000090FullSourceLoc FullSourceLoc::getSpellingLoc() const {
91 assert(isValid());
Chris Lattnera50bd542009-01-16 23:03:56 +000092 return FullSourceLoc(SrcMgr->getSpellingLoc(*this), *SrcMgr);
Chris Lattner5c38b632008-09-29 21:46:13 +000093}
94
Chandler Carruth64211622011-07-25 21:09:52 +000095unsigned FullSourceLoc::getExpansionLineNumber(bool *Invalid) const {
Chris Lattnera50bd542009-01-16 23:03:56 +000096 assert(isValid());
Chandler Carruth64211622011-07-25 21:09:52 +000097 return SrcMgr->getExpansionLineNumber(*this, Invalid);
Ted Kremenek1758b072008-04-03 17:55:15 +000098}
99
Chandler Carrutha77c0312011-07-25 20:57:57 +0000100unsigned FullSourceLoc::getExpansionColumnNumber(bool *Invalid) const {
Chris Lattnera50bd542009-01-16 23:03:56 +0000101 assert(isValid());
Chandler Carrutha77c0312011-07-25 20:57:57 +0000102 return SrcMgr->getExpansionColumnNumber(*this, Invalid);
Ted Kremenek1758b072008-04-03 17:55:15 +0000103}
104
Douglas Gregor64e462d2010-03-16 20:53:17 +0000105unsigned FullSourceLoc::getSpellingLineNumber(bool *Invalid) const {
Chris Lattnera50bd542009-01-16 23:03:56 +0000106 assert(isValid());
Douglas Gregor64e462d2010-03-16 20:53:17 +0000107 return SrcMgr->getSpellingLineNumber(*this, Invalid);
Chris Lattner5c38b632008-09-29 21:46:13 +0000108}
109
Douglas Gregor64e462d2010-03-16 20:53:17 +0000110unsigned FullSourceLoc::getSpellingColumnNumber(bool *Invalid) const {
Chris Lattnera50bd542009-01-16 23:03:56 +0000111 assert(isValid());
Douglas Gregor64e462d2010-03-16 20:53:17 +0000112 return SrcMgr->getSpellingColumnNumber(*this, Invalid);
Chris Lattner5c38b632008-09-29 21:46:13 +0000113}
114
Nico Weber7bfaaae2008-08-10 19:59:06 +0000115bool FullSourceLoc::isInSystemHeader() const {
Chris Lattnera50bd542009-01-16 23:03:56 +0000116 assert(isValid());
117 return SrcMgr->isInSystemHeader(*this);
Nico Weber7bfaaae2008-08-10 19:59:06 +0000118}
119
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000120bool FullSourceLoc::isBeforeInTranslationUnitThan(SourceLocation Loc) const {
121 assert(isValid());
122 return SrcMgr->isBeforeInTranslationUnit(*this, Loc);
123}
124
Benjamin Kramera6c10682012-02-26 16:55:50 +0000125void FullSourceLoc::dump() const {
126 SourceLocation::dump(*SrcMgr);
127}
128
Douglas Gregora5430162010-03-16 20:46:42 +0000129const char *FullSourceLoc::getCharacterData(bool *Invalid) const {
Chris Lattner4abb87e2009-01-16 22:59:51 +0000130 assert(isValid());
Douglas Gregora5430162010-03-16 20:46:42 +0000131 return SrcMgr->getCharacterData(*this, Invalid);
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000132}
133
Douglas Gregoraae58b02010-03-16 20:01:30 +0000134const llvm::MemoryBuffer* FullSourceLoc::getBuffer(bool *Invalid) const {
Chris Lattnera50bd542009-01-16 23:03:56 +0000135 assert(isValid());
Douglas Gregoraae58b02010-03-16 20:01:30 +0000136 return SrcMgr->getBuffer(SrcMgr->getFileID(*this), Invalid);
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000137}
Ted Kremenek9fd87b12008-04-14 21:04:18 +0000138
Chris Lattner5f9e2722011-07-23 10:55:15 +0000139StringRef FullSourceLoc::getBufferData(bool *Invalid) const {
Douglas Gregoraae58b02010-03-16 20:01:30 +0000140 return getBuffer(Invalid)->getBuffer();
Ted Kremenek3632a352009-01-28 20:46:26 +0000141}
Ted Kremenek321abd42009-03-10 05:13:43 +0000142
143std::pair<FileID, unsigned> FullSourceLoc::getDecomposedLoc() const {
144 return SrcMgr->getDecomposedLoc(*this);
145}