blob: fd90b5a2ce99dbe4e4fc0ce083adf7bc839741b5 [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//
10// This file defines serialization methods for the SourceLocation class.
Ted Kremenek9c728dc2007-12-12 22:39:36 +000011// This file defines accessor methods for the FullSourceLoc class.
Ted Kremenek19a95bc2007-10-25 16:02:43 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Basic/SourceLocation.h"
Chris Lattnerae50fa02009-03-05 00:00:31 +000016#include "clang/Basic/PrettyStackTrace.h"
Ted Kremenek9c728dc2007-12-12 22:39:36 +000017#include "clang/Basic/SourceManager.h"
Ted Kremenek19a95bc2007-10-25 16:02:43 +000018#include "llvm/Bitcode/Serialize.h"
19#include "llvm/Bitcode/Deserialize.h"
Ted Kremenek3632a352009-01-28 20:46:26 +000020#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerae50fa02009-03-05 00:00:31 +000021#include "llvm/Support/raw_ostream.h"
Chris Lattner3daed522009-03-02 22:20:04 +000022#include <cstdio>
Ted Kremenek19a95bc2007-10-25 16:02:43 +000023using namespace clang;
24
Chris Lattnerae50fa02009-03-05 00:00:31 +000025//===----------------------------------------------------------------------===//
26// PrettyStackTraceLoc
27//===----------------------------------------------------------------------===//
28
29void PrettyStackTraceLoc::print(llvm::raw_ostream &OS) const {
30 if (Loc.isValid()) {
31 Loc.print(OS, SM);
32 OS << ": ";
33 }
34 OS << Message << '\n';
35}
36
37//===----------------------------------------------------------------------===//
38// SourceLocation
39//===----------------------------------------------------------------------===//
40
Ted Kremenekbeb77132007-11-01 22:25:41 +000041void SourceLocation::Emit(llvm::Serializer& S) const {
42 S.EmitInt(getRawEncoding());
Ted Kremenek19a95bc2007-10-25 16:02:43 +000043}
44
Ted Kremenekbeb77132007-11-01 22:25:41 +000045SourceLocation SourceLocation::ReadVal(llvm::Deserializer& D) {
Ted Kremenek19a95bc2007-10-25 16:02:43 +000046 return SourceLocation::getFromRawEncoding(D.ReadInt());
47}
Ted Kremenekbeb77132007-11-01 22:25:41 +000048
Chris Lattnerae50fa02009-03-05 00:00:31 +000049void SourceLocation::print(llvm::raw_ostream &OS, const SourceManager &SM)const{
Chris Lattnerb9c3f962009-01-27 07:57:44 +000050 if (!isValid()) {
Chris Lattnerae50fa02009-03-05 00:00:31 +000051 OS << "<invalid loc>";
Chris Lattnerb9c3f962009-01-27 07:57:44 +000052 return;
53 }
54
55 if (isFileID()) {
56 PresumedLoc PLoc = SM.getPresumedLoc(*this);
Chris Lattnerb9c3f962009-01-27 07:57:44 +000057 // The instantiation and spelling pos is identical for file locs.
Chris Lattnerae50fa02009-03-05 00:00:31 +000058 OS << PLoc.getFilename() << ':' << PLoc.getLine()
59 << ':' << PLoc.getColumn();
Chris Lattnerb9c3f962009-01-27 07:57:44 +000060 return;
61 }
62
Chris Lattnerae50fa02009-03-05 00:00:31 +000063 SM.getInstantiationLoc(*this).print(OS, SM);
64
65 OS << " <Spelling=";
66 SM.getSpellingLoc(*this).print(OS, SM);
67 OS << '>';
Chris Lattnerb9c3f962009-01-27 07:57:44 +000068}
69
Chris Lattnerae50fa02009-03-05 00:00:31 +000070void SourceLocation::dump(const SourceManager &SM) const {
71 print(llvm::errs(), SM);
72 llvm::errs().flush();
73}
Chris Lattnerb9c3f962009-01-27 07:57:44 +000074
Ted Kremenekbeb77132007-11-01 22:25:41 +000075void SourceRange::Emit(llvm::Serializer& S) const {
76 B.Emit(S);
77 E.Emit(S);
78}
79
80SourceRange SourceRange::ReadVal(llvm::Deserializer& D) {
81 SourceLocation A = SourceLocation::ReadVal(D);
82 SourceLocation B = SourceLocation::ReadVal(D);
83 return SourceRange(A,B);
84}
Ted Kremenek9c728dc2007-12-12 22:39:36 +000085
Chris Lattnerae50fa02009-03-05 00:00:31 +000086//===----------------------------------------------------------------------===//
87// FullSourceLoc
88//===----------------------------------------------------------------------===//
89
Chris Lattner3b4d5e92009-01-17 08:45:21 +000090FileID FullSourceLoc::getFileID() const {
91 assert(isValid());
Chris Lattnera11d6172009-01-19 07:46:45 +000092 return SrcMgr->getFileID(*this);
Chris Lattner3b4d5e92009-01-17 08:45:21 +000093}
94
95
Chris Lattnerf7cf85b2009-01-16 07:36:28 +000096FullSourceLoc FullSourceLoc::getInstantiationLoc() const {
Chris Lattnera50bd542009-01-16 23:03:56 +000097 assert(isValid());
98 return FullSourceLoc(SrcMgr->getInstantiationLoc(*this), *SrcMgr);
Ted Kremenek9c728dc2007-12-12 22:39:36 +000099}
100
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000101FullSourceLoc FullSourceLoc::getSpellingLoc() const {
102 assert(isValid());
Chris Lattnera50bd542009-01-16 23:03:56 +0000103 return FullSourceLoc(SrcMgr->getSpellingLoc(*this), *SrcMgr);
Chris Lattner5c38b632008-09-29 21:46:13 +0000104}
105
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000106unsigned FullSourceLoc::getInstantiationLineNumber() const {
Chris Lattnera50bd542009-01-16 23:03:56 +0000107 assert(isValid());
108 return SrcMgr->getInstantiationLineNumber(*this);
Ted Kremenek1758b072008-04-03 17:55:15 +0000109}
110
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000111unsigned FullSourceLoc::getInstantiationColumnNumber() const {
Chris Lattnera50bd542009-01-16 23:03:56 +0000112 assert(isValid());
113 return SrcMgr->getInstantiationColumnNumber(*this);
Ted Kremenek1758b072008-04-03 17:55:15 +0000114}
115
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000116unsigned FullSourceLoc::getSpellingLineNumber() const {
Chris Lattnera50bd542009-01-16 23:03:56 +0000117 assert(isValid());
118 return SrcMgr->getSpellingLineNumber(*this);
Chris Lattner5c38b632008-09-29 21:46:13 +0000119}
120
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000121unsigned FullSourceLoc::getSpellingColumnNumber() const {
Chris Lattnera50bd542009-01-16 23:03:56 +0000122 assert(isValid());
123 return SrcMgr->getSpellingColumnNumber(*this);
Chris Lattner5c38b632008-09-29 21:46:13 +0000124}
125
Nico Weber7bfaaae2008-08-10 19:59:06 +0000126bool FullSourceLoc::isInSystemHeader() const {
Chris Lattnera50bd542009-01-16 23:03:56 +0000127 assert(isValid());
128 return SrcMgr->isInSystemHeader(*this);
Nico Weber7bfaaae2008-08-10 19:59:06 +0000129}
130
Chris Lattner4abb87e2009-01-16 22:59:51 +0000131const char *FullSourceLoc::getCharacterData() const {
132 assert(isValid());
Chris Lattnera50bd542009-01-16 23:03:56 +0000133 return SrcMgr->getCharacterData(*this);
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000134}
135
136const llvm::MemoryBuffer* FullSourceLoc::getBuffer() const {
Chris Lattnera50bd542009-01-16 23:03:56 +0000137 assert(isValid());
Chris Lattnera11d6172009-01-19 07:46:45 +0000138 return SrcMgr->getBuffer(SrcMgr->getFileID(*this));
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000139}
Ted Kremenek9fd87b12008-04-14 21:04:18 +0000140
Ted Kremenek3632a352009-01-28 20:46:26 +0000141std::pair<const char*, const char*> FullSourceLoc::getBufferData() const {
142 const llvm::MemoryBuffer *Buf = getBuffer();
143 return std::make_pair(Buf->getBufferStart(), Buf->getBufferEnd());
144}