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 | 3b4d5e9 | 2009-01-17 08:45:21 +0000 | [diff] [blame^] | 40 | FileID FullSourceLoc::getFileID() const { |
| 41 | assert(isValid()); |
| 42 | return SrcMgr->getCanonicalFileID(*this); |
| 43 | } |
| 44 | |
| 45 | |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 46 | FullSourceLoc FullSourceLoc::getInstantiationLoc() const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 47 | assert(isValid()); |
| 48 | return FullSourceLoc(SrcMgr->getInstantiationLoc(*this), *SrcMgr); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 51 | FullSourceLoc FullSourceLoc::getSpellingLoc() const { |
| 52 | assert(isValid()); |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 53 | return FullSourceLoc(SrcMgr->getSpellingLoc(*this), *SrcMgr); |
Chris Lattner | 5c38b63 | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | FullSourceLoc FullSourceLoc::getIncludeLoc() const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 57 | assert(isValid()); |
| 58 | return FullSourceLoc(SrcMgr->getIncludeLoc(*this), *SrcMgr); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Ted Kremenek | 1758b07 | 2008-04-03 17:55:15 +0000 | [diff] [blame] | 61 | unsigned FullSourceLoc::getLineNumber() const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 62 | assert(isValid()); |
| 63 | return SrcMgr->getLineNumber(*this); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Ted Kremenek | 1758b07 | 2008-04-03 17:55:15 +0000 | [diff] [blame] | 66 | unsigned FullSourceLoc::getColumnNumber() const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 67 | assert(isValid()); |
| 68 | return SrcMgr->getColumnNumber(*this); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Ted Kremenek | 1758b07 | 2008-04-03 17:55:15 +0000 | [diff] [blame] | 71 | |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 72 | unsigned FullSourceLoc::getInstantiationLineNumber() const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 73 | assert(isValid()); |
| 74 | return SrcMgr->getInstantiationLineNumber(*this); |
Ted Kremenek | 1758b07 | 2008-04-03 17:55:15 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 77 | unsigned FullSourceLoc::getInstantiationColumnNumber() const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 78 | assert(isValid()); |
| 79 | return SrcMgr->getInstantiationColumnNumber(*this); |
Ted Kremenek | 1758b07 | 2008-04-03 17:55:15 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 82 | unsigned FullSourceLoc::getSpellingLineNumber() const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 83 | assert(isValid()); |
| 84 | return SrcMgr->getSpellingLineNumber(*this); |
Chris Lattner | 5c38b63 | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 87 | unsigned FullSourceLoc::getSpellingColumnNumber() const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 88 | assert(isValid()); |
| 89 | return SrcMgr->getSpellingColumnNumber(*this); |
Chris Lattner | 5c38b63 | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 92 | const char* FullSourceLoc::getSourceName() const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 93 | assert(isValid()); |
| 94 | return SrcMgr->getSourceName(*this); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | const FileEntry* FullSourceLoc::getFileEntryForLoc() const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 98 | assert(isValid()); |
| 99 | return SrcMgr->getFileEntryForLoc(*this); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +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 | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 114 | return SrcMgr->getBuffer(*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 | |
Chris Lattner | 5c38b63 | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 117 | void FullSourceLoc::dump() const { |
| 118 | if (!isValid()) { |
| 119 | fprintf(stderr, "Invalid Loc\n"); |
| 120 | return; |
| 121 | } |
| 122 | |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 123 | if (isFileID()) { |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 124 | // The instantiation and spelling pos is identical for file locs. |
Chris Lattner | 5c38b63 | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 125 | fprintf(stderr, "File Loc from '%s': %d: %d\n", |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 126 | getSourceName(), getInstantiationLineNumber(), |
| 127 | getInstantiationColumnNumber()); |
Chris Lattner | 5c38b63 | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 128 | } else { |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 129 | fprintf(stderr, "Macro Loc (\n Spelling: "); |
| 130 | getSpellingLoc().dump(); |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 131 | fprintf(stderr, " Instantiation: "); |
| 132 | getInstantiationLoc().dump(); |
Chris Lattner | 5c38b63 | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 133 | fprintf(stderr, ")\n"); |
| 134 | } |
| 135 | } |