Ted Kremenek | 19a95bc | 2007-10-25 16:02:43 +0000 | [diff] [blame] | 1 | //==--- SourceLocation.cpp - Compact identifier for Source Files -*- C++ -*-==// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Ted Kremenek | 19a95bc | 2007-10-25 16:02:43 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines serialization methods for the SourceLocation class. |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 11 | // This file defines accessor methods for the FullSourceLoc class. |
Ted Kremenek | 19a95bc | 2007-10-25 16:02:43 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Basic/SourceLocation.h" |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 16 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | 19a95bc | 2007-10-25 16:02:43 +0000 | [diff] [blame] | 17 | #include "llvm/Bitcode/Serialize.h" |
| 18 | #include "llvm/Bitcode/Deserialize.h" |
Ted Kremenek | 3632a35 | 2009-01-28 20:46:26 +0000 | [diff] [blame] | 19 | #include "llvm/Support/MemoryBuffer.h" |
Chris Lattner | 3daed52 | 2009-03-02 22:20:04 +0000 | [diff] [blame^] | 20 | #include <cstdio> |
Ted Kremenek | 3632a35 | 2009-01-28 20:46:26 +0000 | [diff] [blame] | 21 | |
Ted Kremenek | 19a95bc | 2007-10-25 16:02:43 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
Ted Kremenek | beb7713 | 2007-11-01 22:25:41 +0000 | [diff] [blame] | 24 | void SourceLocation::Emit(llvm::Serializer& S) const { |
| 25 | S.EmitInt(getRawEncoding()); |
Ted Kremenek | 19a95bc | 2007-10-25 16:02:43 +0000 | [diff] [blame] | 26 | } |
| 27 | |
Ted Kremenek | beb7713 | 2007-11-01 22:25:41 +0000 | [diff] [blame] | 28 | SourceLocation SourceLocation::ReadVal(llvm::Deserializer& D) { |
Ted Kremenek | 19a95bc | 2007-10-25 16:02:43 +0000 | [diff] [blame] | 29 | return SourceLocation::getFromRawEncoding(D.ReadInt()); |
| 30 | } |
Ted Kremenek | beb7713 | 2007-11-01 22:25:41 +0000 | [diff] [blame] | 31 | |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 32 | void SourceLocation::dump(const SourceManager &SM) const { |
| 33 | if (!isValid()) { |
| 34 | fprintf(stderr, "<invalid loc>"); |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | if (isFileID()) { |
| 39 | PresumedLoc PLoc = SM.getPresumedLoc(*this); |
| 40 | |
| 41 | // The instantiation and spelling pos is identical for file locs. |
| 42 | fprintf(stderr, "%s:%d:%d", |
| 43 | PLoc.getFilename(), PLoc.getLine(), PLoc.getColumn()); |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | SM.getInstantiationLoc(*this).dump(SM); |
| 48 | |
| 49 | fprintf(stderr, " <Spelling="); |
| 50 | SM.getSpellingLoc(*this).dump(SM); |
| 51 | fprintf(stderr, ">"); |
| 52 | } |
| 53 | |
| 54 | |
Ted Kremenek | beb7713 | 2007-11-01 22:25:41 +0000 | [diff] [blame] | 55 | void SourceRange::Emit(llvm::Serializer& S) const { |
| 56 | B.Emit(S); |
| 57 | E.Emit(S); |
| 58 | } |
| 59 | |
| 60 | SourceRange SourceRange::ReadVal(llvm::Deserializer& D) { |
| 61 | SourceLocation A = SourceLocation::ReadVal(D); |
| 62 | SourceLocation B = SourceLocation::ReadVal(D); |
| 63 | return SourceRange(A,B); |
| 64 | } |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 65 | |
Chris Lattner | 3b4d5e9 | 2009-01-17 08:45:21 +0000 | [diff] [blame] | 66 | FileID FullSourceLoc::getFileID() const { |
| 67 | assert(isValid()); |
Chris Lattner | a11d617 | 2009-01-19 07:46:45 +0000 | [diff] [blame] | 68 | return SrcMgr->getFileID(*this); |
Chris Lattner | 3b4d5e9 | 2009-01-17 08:45:21 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 72 | FullSourceLoc FullSourceLoc::getInstantiationLoc() const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 73 | assert(isValid()); |
| 74 | return FullSourceLoc(SrcMgr->getInstantiationLoc(*this), *SrcMgr); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 77 | FullSourceLoc FullSourceLoc::getSpellingLoc() const { |
| 78 | assert(isValid()); |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 79 | return FullSourceLoc(SrcMgr->getSpellingLoc(*this), *SrcMgr); |
Chris Lattner | 5c38b63 | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 82 | unsigned FullSourceLoc::getInstantiationLineNumber() const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 83 | assert(isValid()); |
| 84 | return SrcMgr->getInstantiationLineNumber(*this); |
Ted Kremenek | 1758b07 | 2008-04-03 17:55:15 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 87 | unsigned FullSourceLoc::getInstantiationColumnNumber() const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 88 | assert(isValid()); |
| 89 | return SrcMgr->getInstantiationColumnNumber(*this); |
Ted Kremenek | 1758b07 | 2008-04-03 17:55:15 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 92 | unsigned FullSourceLoc::getSpellingLineNumber() const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 93 | assert(isValid()); |
| 94 | return SrcMgr->getSpellingLineNumber(*this); |
Chris Lattner | 5c38b63 | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 97 | unsigned FullSourceLoc::getSpellingColumnNumber() const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 98 | assert(isValid()); |
| 99 | return SrcMgr->getSpellingColumnNumber(*this); |
Chris Lattner | 5c38b63 | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 102 | bool FullSourceLoc::isInSystemHeader() const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 103 | assert(isValid()); |
| 104 | return SrcMgr->isInSystemHeader(*this); |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Chris Lattner | 4abb87e | 2009-01-16 22:59:51 +0000 | [diff] [blame] | 107 | const char *FullSourceLoc::getCharacterData() const { |
| 108 | assert(isValid()); |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 109 | return SrcMgr->getCharacterData(*this); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | const llvm::MemoryBuffer* FullSourceLoc::getBuffer() const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 113 | assert(isValid()); |
Chris Lattner | a11d617 | 2009-01-19 07:46:45 +0000 | [diff] [blame] | 114 | return SrcMgr->getBuffer(SrcMgr->getFileID(*this)); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 115 | } |
Ted Kremenek | 9fd87b1 | 2008-04-14 21:04:18 +0000 | [diff] [blame] | 116 | |
Ted Kremenek | 3632a35 | 2009-01-28 20:46:26 +0000 | [diff] [blame] | 117 | std::pair<const char*, const char*> FullSourceLoc::getBufferData() const { |
| 118 | const llvm::MemoryBuffer *Buf = getBuffer(); |
| 119 | return std::make_pair(Buf->getBufferStart(), Buf->getBufferEnd()); |
| 120 | } |