blob: 578a4eb34bab50a54a8c13daab275fd15a3dbd00 [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
26void PrettyStackTraceLoc::print(llvm::raw_ostream &OS) const {
27 if (Loc.isValid()) {
28 Loc.print(OS, SM);
29 OS << ": ";
30 }
31 OS << Message << '\n';
32}
33
34//===----------------------------------------------------------------------===//
35// SourceLocation
36//===----------------------------------------------------------------------===//
37
Chris Lattnerae50fa02009-03-05 00:00:31 +000038void SourceLocation::print(llvm::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);
Chris Lattnerb9c3f962009-01-27 07:57:44 +000046 // The instantiation and spelling pos is identical for file locs.
Chris Lattnerae50fa02009-03-05 00:00:31 +000047 OS << PLoc.getFilename() << ':' << PLoc.getLine()
48 << ':' << PLoc.getColumn();
Chris Lattnerb9c3f962009-01-27 07:57:44 +000049 return;
50 }
Mike Stump1eb44332009-09-09 15:08:12 +000051
Chris Lattnerae50fa02009-03-05 00:00:31 +000052 SM.getInstantiationLoc(*this).print(OS, SM);
53
54 OS << " <Spelling=";
55 SM.getSpellingLoc(*this).print(OS, SM);
56 OS << '>';
Chris Lattnerb9c3f962009-01-27 07:57:44 +000057}
58
Chris Lattnerae50fa02009-03-05 00:00:31 +000059void SourceLocation::dump(const SourceManager &SM) const {
60 print(llvm::errs(), SM);
Chris Lattnerae50fa02009-03-05 00:00:31 +000061}
Chris Lattnerb9c3f962009-01-27 07:57:44 +000062
Chris Lattnerae50fa02009-03-05 00:00:31 +000063//===----------------------------------------------------------------------===//
64// FullSourceLoc
65//===----------------------------------------------------------------------===//
66
Chris Lattner3b4d5e92009-01-17 08:45:21 +000067FileID FullSourceLoc::getFileID() const {
68 assert(isValid());
Chris Lattnera11d6172009-01-19 07:46:45 +000069 return SrcMgr->getFileID(*this);
Chris Lattner3b4d5e92009-01-17 08:45:21 +000070}
71
72
Chris Lattnerf7cf85b2009-01-16 07:36:28 +000073FullSourceLoc FullSourceLoc::getInstantiationLoc() const {
Chris Lattnera50bd542009-01-16 23:03:56 +000074 assert(isValid());
75 return FullSourceLoc(SrcMgr->getInstantiationLoc(*this), *SrcMgr);
Ted Kremenek9c728dc2007-12-12 22:39:36 +000076}
77
Chris Lattnerdf7c17a2009-01-16 07:00:02 +000078FullSourceLoc FullSourceLoc::getSpellingLoc() const {
79 assert(isValid());
Chris Lattnera50bd542009-01-16 23:03:56 +000080 return FullSourceLoc(SrcMgr->getSpellingLoc(*this), *SrcMgr);
Chris Lattner5c38b632008-09-29 21:46:13 +000081}
82
Chris Lattnerf7cf85b2009-01-16 07:36:28 +000083unsigned FullSourceLoc::getInstantiationLineNumber() const {
Chris Lattnera50bd542009-01-16 23:03:56 +000084 assert(isValid());
85 return SrcMgr->getInstantiationLineNumber(*this);
Ted Kremenek1758b072008-04-03 17:55:15 +000086}
87
Chris Lattnerf7cf85b2009-01-16 07:36:28 +000088unsigned FullSourceLoc::getInstantiationColumnNumber() const {
Chris Lattnera50bd542009-01-16 23:03:56 +000089 assert(isValid());
90 return SrcMgr->getInstantiationColumnNumber(*this);
Ted Kremenek1758b072008-04-03 17:55:15 +000091}
92
Chris Lattnerdf7c17a2009-01-16 07:00:02 +000093unsigned FullSourceLoc::getSpellingLineNumber() const {
Chris Lattnera50bd542009-01-16 23:03:56 +000094 assert(isValid());
95 return SrcMgr->getSpellingLineNumber(*this);
Chris Lattner5c38b632008-09-29 21:46:13 +000096}
97
Chris Lattnerdf7c17a2009-01-16 07:00:02 +000098unsigned FullSourceLoc::getSpellingColumnNumber() const {
Chris Lattnera50bd542009-01-16 23:03:56 +000099 assert(isValid());
100 return SrcMgr->getSpellingColumnNumber(*this);
Chris Lattner5c38b632008-09-29 21:46:13 +0000101}
102
Nico Weber7bfaaae2008-08-10 19:59:06 +0000103bool FullSourceLoc::isInSystemHeader() const {
Chris Lattnera50bd542009-01-16 23:03:56 +0000104 assert(isValid());
105 return SrcMgr->isInSystemHeader(*this);
Nico Weber7bfaaae2008-08-10 19:59:06 +0000106}
107
Chris Lattner4abb87e2009-01-16 22:59:51 +0000108const char *FullSourceLoc::getCharacterData() const {
109 assert(isValid());
Chris Lattnera50bd542009-01-16 23:03:56 +0000110 return SrcMgr->getCharacterData(*this);
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000111}
112
113const llvm::MemoryBuffer* FullSourceLoc::getBuffer() const {
Chris Lattnera50bd542009-01-16 23:03:56 +0000114 assert(isValid());
Chris Lattnera11d6172009-01-19 07:46:45 +0000115 return SrcMgr->getBuffer(SrcMgr->getFileID(*this));
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000116}
Ted Kremenek9fd87b12008-04-14 21:04:18 +0000117
Ted Kremenek3632a352009-01-28 20:46:26 +0000118std::pair<const char*, const char*> FullSourceLoc::getBufferData() const {
119 const llvm::MemoryBuffer *Buf = getBuffer();
120 return std::make_pair(Buf->getBufferStart(), Buf->getBufferEnd());
121}
Ted Kremenek321abd42009-03-10 05:13:43 +0000122
123std::pair<FileID, unsigned> FullSourceLoc::getDecomposedLoc() const {
124 return SrcMgr->getDecomposedLoc(*this);
125}