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 { |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 41 | assert (isValid()); |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 42 | return FullSourceLoc(SrcMgr->getInstantiationLoc(Loc), *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()); |
| 47 | return FullSourceLoc(SrcMgr->getSpellingLoc(Loc), *SrcMgr); |
Chris Lattner | 5c38b63 | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | FullSourceLoc FullSourceLoc::getIncludeLoc() const { |
| 51 | assert (isValid()); |
| 52 | return FullSourceLoc(SrcMgr->getIncludeLoc(Loc), *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 { |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 56 | assert (isValid()); |
| 57 | return SrcMgr->getLineNumber(Loc); |
| 58 | } |
| 59 | |
Ted Kremenek | 1758b07 | 2008-04-03 17:55:15 +0000 | [diff] [blame] | 60 | unsigned FullSourceLoc::getColumnNumber() const { |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 61 | assert (isValid()); |
| 62 | return SrcMgr->getColumnNumber(Loc); |
| 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 { |
Ted Kremenek | 1758b07 | 2008-04-03 17:55:15 +0000 | [diff] [blame] | 67 | assert (isValid()); |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 68 | return SrcMgr->getInstantiationLineNumber(Loc); |
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 { |
Ted Kremenek | 1758b07 | 2008-04-03 17:55:15 +0000 | [diff] [blame] | 72 | assert (isValid()); |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 73 | return SrcMgr->getInstantiationColumnNumber(Loc); |
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 | 5c38b63 | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 77 | assert (isValid()); |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 78 | return SrcMgr->getSpellingLineNumber(Loc); |
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 | 5c38b63 | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 82 | assert (isValid()); |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 83 | return SrcMgr->getSpellingColumnNumber(Loc); |
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 { |
| 87 | assert (isValid()); |
| 88 | return SrcMgr->getSourceName(Loc); |
| 89 | } |
| 90 | |
| 91 | const FileEntry* FullSourceLoc::getFileEntryForLoc() const { |
| 92 | assert (isValid()); |
| 93 | return SrcMgr->getFileEntryForLoc(Loc); |
| 94 | } |
| 95 | |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 96 | bool FullSourceLoc::isInSystemHeader() const { |
| 97 | assert (isValid()); |
| 98 | return SrcMgr->isInSystemHeader(Loc); |
| 99 | } |
| 100 | |
| 101 | |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 102 | const char * FullSourceLoc::getCharacterData() const { |
| 103 | assert (isValid()); |
| 104 | return SrcMgr->getCharacterData(Loc); |
| 105 | } |
| 106 | |
| 107 | const llvm::MemoryBuffer* FullSourceLoc::getBuffer() const { |
| 108 | assert (isValid()); |
| 109 | return SrcMgr->getBuffer(Loc.getFileID()); |
| 110 | } |
Ted Kremenek | 9fd87b1 | 2008-04-14 21:04:18 +0000 | [diff] [blame] | 111 | |
| 112 | unsigned FullSourceLoc::getCanonicalFileID() const { |
| 113 | return SrcMgr->getCanonicalFileID(Loc); |
| 114 | } |
Chris Lattner | 5c38b63 | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 115 | |
| 116 | void FullSourceLoc::dump() const { |
| 117 | if (!isValid()) { |
| 118 | fprintf(stderr, "Invalid Loc\n"); |
| 119 | return; |
| 120 | } |
| 121 | |
Chris Lattner | 0cf7bb9 | 2009-01-16 22:53:56 +0000 | [diff] [blame^] | 122 | if (Loc.isFileID()) { |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 123 | // The instantiation and spelling pos is identical for file locs. |
Chris Lattner | 5c38b63 | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 124 | fprintf(stderr, "File Loc from '%s': %d: %d\n", |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 125 | getSourceName(), getInstantiationLineNumber(), |
| 126 | getInstantiationColumnNumber()); |
Chris Lattner | 5c38b63 | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 127 | } else { |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 128 | fprintf(stderr, "Macro Loc (\n Spelling: "); |
| 129 | getSpellingLoc().dump(); |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 130 | fprintf(stderr, " Instantiation: "); |
| 131 | getInstantiationLoc().dump(); |
Chris Lattner | 5c38b63 | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 132 | fprintf(stderr, ")\n"); |
| 133 | } |
| 134 | } |