blob: 5062d43f58f25e3cfdb6971751a64ce8b0843170 [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);
Douglas Gregorcb7b1e12010-11-12 07:15:47 +000046
47 if (PLoc.isInvalid()) {
48 OS << "<invalid>";
49 return;
50 }
Chris Lattnerb9c3f962009-01-27 07:57:44 +000051 // The instantiation 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
Chris Lattnerae50fa02009-03-05 00:00:31 +000057 SM.getInstantiationLoc(*this).print(OS, SM);
58
59 OS << " <Spelling=";
60 SM.getSpellingLoc(*this).print(OS, SM);
61 OS << '>';
Chris Lattnerb9c3f962009-01-27 07:57:44 +000062}
63
Chris Lattnerae50fa02009-03-05 00:00:31 +000064void SourceLocation::dump(const SourceManager &SM) const {
65 print(llvm::errs(), SM);
Chris Lattnerae50fa02009-03-05 00:00:31 +000066}
Chris Lattnerb9c3f962009-01-27 07:57:44 +000067
Chris Lattnerae50fa02009-03-05 00:00:31 +000068//===----------------------------------------------------------------------===//
69// FullSourceLoc
70//===----------------------------------------------------------------------===//
71
Chris Lattner3b4d5e92009-01-17 08:45:21 +000072FileID FullSourceLoc::getFileID() const {
73 assert(isValid());
Chris Lattnera11d6172009-01-19 07:46:45 +000074 return SrcMgr->getFileID(*this);
Chris Lattner3b4d5e92009-01-17 08:45:21 +000075}
76
77
Chris Lattnerf7cf85b2009-01-16 07:36:28 +000078FullSourceLoc FullSourceLoc::getInstantiationLoc() const {
Chris Lattnera50bd542009-01-16 23:03:56 +000079 assert(isValid());
80 return FullSourceLoc(SrcMgr->getInstantiationLoc(*this), *SrcMgr);
Ted Kremenek9c728dc2007-12-12 22:39:36 +000081}
82
Chris Lattnerdf7c17a2009-01-16 07:00:02 +000083FullSourceLoc FullSourceLoc::getSpellingLoc() const {
84 assert(isValid());
Chris Lattnera50bd542009-01-16 23:03:56 +000085 return FullSourceLoc(SrcMgr->getSpellingLoc(*this), *SrcMgr);
Chris Lattner5c38b632008-09-29 21:46:13 +000086}
87
Douglas Gregor64e462d2010-03-16 20:53:17 +000088unsigned FullSourceLoc::getInstantiationLineNumber(bool *Invalid) const {
Chris Lattnera50bd542009-01-16 23:03:56 +000089 assert(isValid());
Douglas Gregor64e462d2010-03-16 20:53:17 +000090 return SrcMgr->getInstantiationLineNumber(*this, Invalid);
Ted Kremenek1758b072008-04-03 17:55:15 +000091}
92
Douglas Gregor64e462d2010-03-16 20:53:17 +000093unsigned FullSourceLoc::getInstantiationColumnNumber(bool *Invalid) const {
Chris Lattnera50bd542009-01-16 23:03:56 +000094 assert(isValid());
Douglas Gregor64e462d2010-03-16 20:53:17 +000095 return SrcMgr->getInstantiationColumnNumber(*this, Invalid);
Ted Kremenek1758b072008-04-03 17:55:15 +000096}
97
Douglas Gregor64e462d2010-03-16 20:53:17 +000098unsigned FullSourceLoc::getSpellingLineNumber(bool *Invalid) const {
Chris Lattnera50bd542009-01-16 23:03:56 +000099 assert(isValid());
Douglas Gregor64e462d2010-03-16 20:53:17 +0000100 return SrcMgr->getSpellingLineNumber(*this, Invalid);
Chris Lattner5c38b632008-09-29 21:46:13 +0000101}
102
Douglas Gregor64e462d2010-03-16 20:53:17 +0000103unsigned FullSourceLoc::getSpellingColumnNumber(bool *Invalid) const {
Chris Lattnera50bd542009-01-16 23:03:56 +0000104 assert(isValid());
Douglas Gregor64e462d2010-03-16 20:53:17 +0000105 return SrcMgr->getSpellingColumnNumber(*this, Invalid);
Chris Lattner5c38b632008-09-29 21:46:13 +0000106}
107
Nico Weber7bfaaae2008-08-10 19:59:06 +0000108bool FullSourceLoc::isInSystemHeader() const {
Chris Lattnera50bd542009-01-16 23:03:56 +0000109 assert(isValid());
110 return SrcMgr->isInSystemHeader(*this);
Nico Weber7bfaaae2008-08-10 19:59:06 +0000111}
112
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000113bool FullSourceLoc::isBeforeInTranslationUnitThan(SourceLocation Loc) const {
114 assert(isValid());
115 return SrcMgr->isBeforeInTranslationUnit(*this, Loc);
116}
117
Douglas Gregora5430162010-03-16 20:46:42 +0000118const char *FullSourceLoc::getCharacterData(bool *Invalid) const {
Chris Lattner4abb87e2009-01-16 22:59:51 +0000119 assert(isValid());
Douglas Gregora5430162010-03-16 20:46:42 +0000120 return SrcMgr->getCharacterData(*this, Invalid);
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000121}
122
Douglas Gregoraae58b02010-03-16 20:01:30 +0000123const llvm::MemoryBuffer* FullSourceLoc::getBuffer(bool *Invalid) const {
Chris Lattnera50bd542009-01-16 23:03:56 +0000124 assert(isValid());
Douglas Gregoraae58b02010-03-16 20:01:30 +0000125 return SrcMgr->getBuffer(SrcMgr->getFileID(*this), Invalid);
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000126}
Ted Kremenek9fd87b12008-04-14 21:04:18 +0000127
Douglas Gregoraae58b02010-03-16 20:01:30 +0000128llvm::StringRef FullSourceLoc::getBufferData(bool *Invalid) const {
129 return getBuffer(Invalid)->getBuffer();
Ted Kremenek3632a352009-01-28 20:46:26 +0000130}
Ted Kremenek321abd42009-03-10 05:13:43 +0000131
132std::pair<FileID, unsigned> FullSourceLoc::getDecomposedLoc() const {
133 return SrcMgr->getDecomposedLoc(*this);
134}