blob: d254e8620a96eca8335c9b40d00d39c6bf0e8f9b [file] [log] [blame]
Ted Kremenek4edcb4d2007-10-25 16:02:43 +00001//==--- SourceLocation.cpp - Compact identifier for Source Files -*- C++ -*-==//
2//
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"
Chris Lattnerbd61a952009-03-05 00:00:31 +000015#include "clang/Basic/PrettyStackTrace.h"
Ted Kremenek1daa3cf2007-12-12 22:39:36 +000016#include "clang/Basic/SourceManager.h"
Ted Kremenekbf165542009-01-28 20:46:26 +000017#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerbd61a952009-03-05 00:00:31 +000018#include "llvm/Support/raw_ostream.h"
Chris Lattnerc25d8a72009-03-02 22:20:04 +000019#include <cstdio>
Ted Kremenek4edcb4d2007-10-25 16:02:43 +000020using namespace clang;
21
Chris Lattnerbd61a952009-03-05 00:00:31 +000022//===----------------------------------------------------------------------===//
23// PrettyStackTraceLoc
24//===----------------------------------------------------------------------===//
25
Chris Lattner0e62c1c2011-07-23 10:55:15 +000026void PrettyStackTraceLoc::print(raw_ostream &OS) const {
Chris Lattnerbd61a952009-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 Lattner0e62c1c2011-07-23 10:55:15 +000038void SourceLocation::print(raw_ostream &OS, const SourceManager &SM)const{
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000039 if (!isValid()) {
Chris Lattnerbd61a952009-03-05 00:00:31 +000040 OS << "<invalid loc>";
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000041 return;
42 }
Mike Stump11289f42009-09-09 15:08:12 +000043
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000044 if (isFileID()) {
45 PresumedLoc PLoc = SM.getPresumedLoc(*this);
Douglas Gregor453b0122010-11-12 07:15:47 +000046
47 if (PLoc.isInvalid()) {
48 OS << "<invalid>";
49 return;
50 }
Chandler Carruth64ee7822011-07-26 05:17:23 +000051 // The macro expansion and spelling pos is identical for file locs.
Chris Lattnerbd61a952009-03-05 00:00:31 +000052 OS << PLoc.getFilename() << ':' << PLoc.getLine()
53 << ':' << PLoc.getColumn();
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000054 return;
55 }
Mike Stump11289f42009-09-09 15:08:12 +000056
Chandler Carruth35f53202011-07-25 16:49:02 +000057 SM.getExpansionLoc(*this).print(OS, SM);
Chris Lattnerbd61a952009-03-05 00:00:31 +000058
59 OS << " <Spelling=";
60 SM.getSpellingLoc(*this).print(OS, SM);
61 OS << '>';
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000062}
63
Alp Tokeref6b0072014-01-04 13:47:14 +000064LLVM_DUMP_METHOD std::string
65SourceLocation::printToString(const SourceManager &SM) const {
Argyrios Kyrtzidisdc6bb492012-11-09 19:40:48 +000066 std::string S;
67 llvm::raw_string_ostream OS(S);
68 print(OS, SM);
Benjamin Krameradaf7952012-12-12 14:17:17 +000069 return OS.str();
Argyrios Kyrtzidisdc6bb492012-11-09 19:40:48 +000070}
71
Alp Tokeref6b0072014-01-04 13:47:14 +000072LLVM_DUMP_METHOD void SourceLocation::dump(const SourceManager &SM) const {
Chris Lattnerbd61a952009-03-05 00:00:31 +000073 print(llvm::errs(), SM);
Chris Lattnerbd61a952009-03-05 00:00:31 +000074}
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000075
Chris Lattnerbd61a952009-03-05 00:00:31 +000076//===----------------------------------------------------------------------===//
77// FullSourceLoc
78//===----------------------------------------------------------------------===//
79
Chris Lattner71dc14b2009-01-17 08:45:21 +000080FileID FullSourceLoc::getFileID() const {
81 assert(isValid());
Chris Lattnercbc35ecb2009-01-19 07:46:45 +000082 return SrcMgr->getFileID(*this);
Chris Lattner71dc14b2009-01-17 08:45:21 +000083}
84
85
Chandler Carruth35f53202011-07-25 16:49:02 +000086FullSourceLoc FullSourceLoc::getExpansionLoc() const {
Chris Lattnerfb1fd912009-01-16 23:03:56 +000087 assert(isValid());
Chandler Carruth35f53202011-07-25 16:49:02 +000088 return FullSourceLoc(SrcMgr->getExpansionLoc(*this), *SrcMgr);
Ted Kremenek1daa3cf2007-12-12 22:39:36 +000089}
90
Chris Lattner53e384f2009-01-16 07:00:02 +000091FullSourceLoc FullSourceLoc::getSpellingLoc() const {
92 assert(isValid());
Chris Lattnerfb1fd912009-01-16 23:03:56 +000093 return FullSourceLoc(SrcMgr->getSpellingLoc(*this), *SrcMgr);
Chris Lattnerd9a663a2008-09-29 21:46:13 +000094}
95
Chandler Carruthd48db212011-07-25 21:09:52 +000096unsigned FullSourceLoc::getExpansionLineNumber(bool *Invalid) const {
Chris Lattnerfb1fd912009-01-16 23:03:56 +000097 assert(isValid());
Chandler Carruthd48db212011-07-25 21:09:52 +000098 return SrcMgr->getExpansionLineNumber(*this, Invalid);
Ted Kremenek406192d2008-04-03 17:55:15 +000099}
100
Chandler Carruth42f35f92011-07-25 20:57:57 +0000101unsigned FullSourceLoc::getExpansionColumnNumber(bool *Invalid) const {
Chris Lattnerfb1fd912009-01-16 23:03:56 +0000102 assert(isValid());
Chandler Carruth42f35f92011-07-25 20:57:57 +0000103 return SrcMgr->getExpansionColumnNumber(*this, Invalid);
Ted Kremenek406192d2008-04-03 17:55:15 +0000104}
105
Douglas Gregora71b9d02010-03-16 20:53:17 +0000106unsigned FullSourceLoc::getSpellingLineNumber(bool *Invalid) const {
Chris Lattnerfb1fd912009-01-16 23:03:56 +0000107 assert(isValid());
Douglas Gregora71b9d02010-03-16 20:53:17 +0000108 return SrcMgr->getSpellingLineNumber(*this, Invalid);
Chris Lattnerd9a663a2008-09-29 21:46:13 +0000109}
110
Douglas Gregora71b9d02010-03-16 20:53:17 +0000111unsigned FullSourceLoc::getSpellingColumnNumber(bool *Invalid) const {
Chris Lattnerfb1fd912009-01-16 23:03:56 +0000112 assert(isValid());
Douglas Gregora71b9d02010-03-16 20:53:17 +0000113 return SrcMgr->getSpellingColumnNumber(*this, Invalid);
Chris Lattnerd9a663a2008-09-29 21:46:13 +0000114}
115
Nico Weber4c311642008-08-10 19:59:06 +0000116bool FullSourceLoc::isInSystemHeader() const {
Chris Lattnerfb1fd912009-01-16 23:03:56 +0000117 assert(isValid());
118 return SrcMgr->isInSystemHeader(*this);
Nico Weber4c311642008-08-10 19:59:06 +0000119}
120
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000121bool FullSourceLoc::isBeforeInTranslationUnitThan(SourceLocation Loc) const {
122 assert(isValid());
123 return SrcMgr->isBeforeInTranslationUnit(*this, Loc);
124}
125
Alp Tokeref6b0072014-01-04 13:47:14 +0000126LLVM_DUMP_METHOD void FullSourceLoc::dump() const {
Benjamin Kramere60db7b2012-02-26 16:55:50 +0000127 SourceLocation::dump(*SrcMgr);
128}
129
Douglas Gregor42fe8582010-03-16 20:46:42 +0000130const char *FullSourceLoc::getCharacterData(bool *Invalid) const {
Chris Lattnerfcc0a5a2009-01-16 22:59:51 +0000131 assert(isValid());
Douglas Gregor42fe8582010-03-16 20:46:42 +0000132 return SrcMgr->getCharacterData(*this, Invalid);
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000133}
134
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000135StringRef FullSourceLoc::getBufferData(bool *Invalid) const {
Rafael Espindolab694a0d2014-08-18 18:17:32 +0000136 assert(isValid());
Aaron Ballman825fb3c2015-06-09 12:04:17 +0000137 return SrcMgr->getBuffer(SrcMgr->getFileID(*this), Invalid)->getBuffer();
Ted Kremenekbf165542009-01-28 20:46:26 +0000138}
Ted Kremenek3ceb7ca2009-03-10 05:13:43 +0000139
140std::pair<FileID, unsigned> FullSourceLoc::getDecomposedLoc() const {
141 return SrcMgr->getDecomposedLoc(*this);
142}