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 | 19a95bc | 2007-10-25 16:02:43 +0000 | [diff] [blame] | 19 | using namespace clang; |
| 20 | |
Ted Kremenek | beb7713 | 2007-11-01 22:25:41 +0000 | [diff] [blame] | 21 | void SourceLocation::Emit(llvm::Serializer& S) const { |
| 22 | S.EmitInt(getRawEncoding()); |
Ted Kremenek | 19a95bc | 2007-10-25 16:02:43 +0000 | [diff] [blame] | 23 | } |
| 24 | |
Ted Kremenek | beb7713 | 2007-11-01 22:25:41 +0000 | [diff] [blame] | 25 | SourceLocation SourceLocation::ReadVal(llvm::Deserializer& D) { |
Ted Kremenek | 19a95bc | 2007-10-25 16:02:43 +0000 | [diff] [blame] | 26 | return SourceLocation::getFromRawEncoding(D.ReadInt()); |
| 27 | } |
Ted Kremenek | beb7713 | 2007-11-01 22:25:41 +0000 | [diff] [blame] | 28 | |
| 29 | void SourceRange::Emit(llvm::Serializer& S) const { |
| 30 | B.Emit(S); |
| 31 | E.Emit(S); |
| 32 | } |
| 33 | |
| 34 | SourceRange SourceRange::ReadVal(llvm::Deserializer& D) { |
| 35 | SourceLocation A = SourceLocation::ReadVal(D); |
| 36 | SourceLocation B = SourceLocation::ReadVal(D); |
| 37 | return SourceRange(A,B); |
| 38 | } |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 39 | |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 40 | FullSourceLoc FullSourceLoc::getInstantiationLoc() const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame^] | 41 | assert(isValid()); |
| 42 | return FullSourceLoc(SrcMgr->getInstantiationLoc(*this), *SrcMgr); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 45 | FullSourceLoc FullSourceLoc::getSpellingLoc() const { |
| 46 | assert(isValid()); |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame^] | 47 | return FullSourceLoc(SrcMgr->getSpellingLoc(*this), *SrcMgr); |
Chris Lattner | 5c38b63 | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | FullSourceLoc FullSourceLoc::getIncludeLoc() const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame^] | 51 | assert(isValid()); |
| 52 | return FullSourceLoc(SrcMgr->getIncludeLoc(*this), *SrcMgr); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Ted Kremenek | 1758b07 | 2008-04-03 17:55:15 +0000 | [diff] [blame] | 55 | unsigned FullSourceLoc::getLineNumber() const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame^] | 56 | assert(isValid()); |
| 57 | return SrcMgr->getLineNumber(*this); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Ted Kremenek | 1758b07 | 2008-04-03 17:55:15 +0000 | [diff] [blame] | 60 | unsigned FullSourceLoc::getColumnNumber() const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame^] | 61 | assert(isValid()); |
| 62 | return SrcMgr->getColumnNumber(*this); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Ted Kremenek | 1758b07 | 2008-04-03 17:55:15 +0000 | [diff] [blame] | 65 | |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 66 | unsigned FullSourceLoc::getInstantiationLineNumber() const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame^] | 67 | assert(isValid()); |
| 68 | return SrcMgr->getInstantiationLineNumber(*this); |
Ted Kremenek | 1758b07 | 2008-04-03 17:55:15 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 71 | unsigned FullSourceLoc::getInstantiationColumnNumber() const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame^] | 72 | assert(isValid()); |
| 73 | return SrcMgr->getInstantiationColumnNumber(*this); |
Ted Kremenek | 1758b07 | 2008-04-03 17:55:15 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 76 | unsigned FullSourceLoc::getSpellingLineNumber() const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame^] | 77 | assert(isValid()); |
| 78 | return SrcMgr->getSpellingLineNumber(*this); |
Chris Lattner | 5c38b63 | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 81 | unsigned FullSourceLoc::getSpellingColumnNumber() const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame^] | 82 | assert(isValid()); |
| 83 | return SrcMgr->getSpellingColumnNumber(*this); |
Chris Lattner | 5c38b63 | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 86 | const char* FullSourceLoc::getSourceName() const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame^] | 87 | assert(isValid()); |
| 88 | return SrcMgr->getSourceName(*this); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | const FileEntry* FullSourceLoc::getFileEntryForLoc() const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame^] | 92 | assert(isValid()); |
| 93 | return SrcMgr->getFileEntryForLoc(*this); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 96 | bool FullSourceLoc::isInSystemHeader() const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame^] | 97 | assert(isValid()); |
| 98 | return SrcMgr->isInSystemHeader(*this); |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Chris Lattner | 4abb87e | 2009-01-16 22:59:51 +0000 | [diff] [blame] | 101 | const char *FullSourceLoc::getCharacterData() const { |
| 102 | assert(isValid()); |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame^] | 103 | return SrcMgr->getCharacterData(*this); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | const llvm::MemoryBuffer* FullSourceLoc::getBuffer() const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame^] | 107 | assert(isValid()); |
| 108 | return SrcMgr->getBuffer(getFileID()); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 109 | } |
Ted Kremenek | 9fd87b1 | 2008-04-14 21:04:18 +0000 | [diff] [blame] | 110 | |
Chris Lattner | 5c38b63 | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 111 | void FullSourceLoc::dump() const { |
| 112 | if (!isValid()) { |
| 113 | fprintf(stderr, "Invalid Loc\n"); |
| 114 | return; |
| 115 | } |
| 116 | |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame^] | 117 | if (isFileID()) { |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 118 | // The instantiation and spelling pos is identical for file locs. |
Chris Lattner | 5c38b63 | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 119 | fprintf(stderr, "File Loc from '%s': %d: %d\n", |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 120 | getSourceName(), getInstantiationLineNumber(), |
| 121 | getInstantiationColumnNumber()); |
Chris Lattner | 5c38b63 | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 122 | } else { |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 123 | fprintf(stderr, "Macro Loc (\n Spelling: "); |
| 124 | getSpellingLoc().dump(); |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 125 | fprintf(stderr, " Instantiation: "); |
| 126 | getInstantiationLoc().dump(); |
Chris Lattner | 5c38b63 | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 127 | fprintf(stderr, ")\n"); |
| 128 | } |
| 129 | } |