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 | // |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 10 | // This file defines accessor methods for the FullSourceLoc class. |
Ted Kremenek | 19a95bc | 2007-10-25 16:02:43 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Basic/SourceLocation.h" |
Chris Lattner | ae50fa0 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 15 | #include "clang/Basic/PrettyStackTrace.h" |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 16 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | 3632a35 | 2009-01-28 20:46:26 +0000 | [diff] [blame] | 17 | #include "llvm/Support/MemoryBuffer.h" |
Chris Lattner | ae50fa0 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 3daed52 | 2009-03-02 22:20:04 +0000 | [diff] [blame] | 19 | #include <cstdio> |
Ted Kremenek | 19a95bc | 2007-10-25 16:02:43 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
Chris Lattner | ae50fa0 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 22 | //===----------------------------------------------------------------------===// |
| 23 | // PrettyStackTraceLoc |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | |
| 26 | void PrettyStackTraceLoc::print(llvm::raw_ostream &OS) const { |
| 27 | if (Loc.isValid()) { |
| 28 | Loc.print(OS, SM); |
| 29 | OS << ": "; |
| 30 | } |
| 31 | OS << Message << '\n'; |
| 32 | } |
| 33 | |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | // SourceLocation |
| 36 | //===----------------------------------------------------------------------===// |
| 37 | |
Chris Lattner | ae50fa0 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 38 | void SourceLocation::print(llvm::raw_ostream &OS, const SourceManager &SM)const{ |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 39 | if (!isValid()) { |
Chris Lattner | ae50fa0 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 40 | OS << "<invalid loc>"; |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 41 | return; |
| 42 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 43 | |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 44 | if (isFileID()) { |
| 45 | PresumedLoc PLoc = SM.getPresumedLoc(*this); |
Douglas Gregor | cb7b1e1 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 46 | |
| 47 | if (PLoc.isInvalid()) { |
| 48 | OS << "<invalid>"; |
| 49 | return; |
| 50 | } |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 51 | // The instantiation and spelling pos is identical for file locs. |
Chris Lattner | ae50fa0 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 52 | OS << PLoc.getFilename() << ':' << PLoc.getLine() |
| 53 | << ':' << PLoc.getColumn(); |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 54 | return; |
| 55 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 56 | |
Chris Lattner | ae50fa0 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 57 | SM.getInstantiationLoc(*this).print(OS, SM); |
| 58 | |
| 59 | OS << " <Spelling="; |
| 60 | SM.getSpellingLoc(*this).print(OS, SM); |
| 61 | OS << '>'; |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Chris Lattner | ae50fa0 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 64 | void SourceLocation::dump(const SourceManager &SM) const { |
| 65 | print(llvm::errs(), SM); |
Chris Lattner | ae50fa0 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 66 | } |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 67 | |
Chris Lattner | ae50fa0 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 68 | //===----------------------------------------------------------------------===// |
| 69 | // FullSourceLoc |
| 70 | //===----------------------------------------------------------------------===// |
| 71 | |
Chris Lattner | 3b4d5e9 | 2009-01-17 08:45:21 +0000 | [diff] [blame] | 72 | FileID FullSourceLoc::getFileID() const { |
| 73 | assert(isValid()); |
Chris Lattner | a11d617 | 2009-01-19 07:46:45 +0000 | [diff] [blame] | 74 | return SrcMgr->getFileID(*this); |
Chris Lattner | 3b4d5e9 | 2009-01-17 08:45:21 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 78 | FullSourceLoc FullSourceLoc::getInstantiationLoc() const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 79 | assert(isValid()); |
| 80 | return FullSourceLoc(SrcMgr->getInstantiationLoc(*this), *SrcMgr); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 83 | FullSourceLoc FullSourceLoc::getSpellingLoc() const { |
| 84 | assert(isValid()); |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 85 | return FullSourceLoc(SrcMgr->getSpellingLoc(*this), *SrcMgr); |
Chris Lattner | 5c38b63 | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Douglas Gregor | 64e462d | 2010-03-16 20:53:17 +0000 | [diff] [blame] | 88 | unsigned FullSourceLoc::getInstantiationLineNumber(bool *Invalid) const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 89 | assert(isValid()); |
Douglas Gregor | 64e462d | 2010-03-16 20:53:17 +0000 | [diff] [blame] | 90 | return SrcMgr->getInstantiationLineNumber(*this, Invalid); |
Ted Kremenek | 1758b07 | 2008-04-03 17:55:15 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Douglas Gregor | 64e462d | 2010-03-16 20:53:17 +0000 | [diff] [blame] | 93 | unsigned FullSourceLoc::getInstantiationColumnNumber(bool *Invalid) const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 94 | assert(isValid()); |
Douglas Gregor | 64e462d | 2010-03-16 20:53:17 +0000 | [diff] [blame] | 95 | return SrcMgr->getInstantiationColumnNumber(*this, Invalid); |
Ted Kremenek | 1758b07 | 2008-04-03 17:55:15 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Douglas Gregor | 64e462d | 2010-03-16 20:53:17 +0000 | [diff] [blame] | 98 | unsigned FullSourceLoc::getSpellingLineNumber(bool *Invalid) const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 99 | assert(isValid()); |
Douglas Gregor | 64e462d | 2010-03-16 20:53:17 +0000 | [diff] [blame] | 100 | return SrcMgr->getSpellingLineNumber(*this, Invalid); |
Chris Lattner | 5c38b63 | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Douglas Gregor | 64e462d | 2010-03-16 20:53:17 +0000 | [diff] [blame] | 103 | unsigned FullSourceLoc::getSpellingColumnNumber(bool *Invalid) const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 104 | assert(isValid()); |
Douglas Gregor | 64e462d | 2010-03-16 20:53:17 +0000 | [diff] [blame] | 105 | return SrcMgr->getSpellingColumnNumber(*this, Invalid); |
Chris Lattner | 5c38b63 | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 108 | bool FullSourceLoc::isInSystemHeader() const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 109 | assert(isValid()); |
| 110 | return SrcMgr->isInSystemHeader(*this); |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame^] | 113 | bool FullSourceLoc::isBeforeInTranslationUnitThan(SourceLocation Loc) const { |
| 114 | assert(isValid()); |
| 115 | return SrcMgr->isBeforeInTranslationUnit(*this, Loc); |
| 116 | } |
| 117 | |
Douglas Gregor | a543016 | 2010-03-16 20:46:42 +0000 | [diff] [blame] | 118 | const char *FullSourceLoc::getCharacterData(bool *Invalid) const { |
Chris Lattner | 4abb87e | 2009-01-16 22:59:51 +0000 | [diff] [blame] | 119 | assert(isValid()); |
Douglas Gregor | a543016 | 2010-03-16 20:46:42 +0000 | [diff] [blame] | 120 | return SrcMgr->getCharacterData(*this, Invalid); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Douglas Gregor | aae58b0 | 2010-03-16 20:01:30 +0000 | [diff] [blame] | 123 | const llvm::MemoryBuffer* FullSourceLoc::getBuffer(bool *Invalid) const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 124 | assert(isValid()); |
Douglas Gregor | aae58b0 | 2010-03-16 20:01:30 +0000 | [diff] [blame] | 125 | return SrcMgr->getBuffer(SrcMgr->getFileID(*this), Invalid); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 126 | } |
Ted Kremenek | 9fd87b1 | 2008-04-14 21:04:18 +0000 | [diff] [blame] | 127 | |
Douglas Gregor | aae58b0 | 2010-03-16 20:01:30 +0000 | [diff] [blame] | 128 | llvm::StringRef FullSourceLoc::getBufferData(bool *Invalid) const { |
| 129 | return getBuffer(Invalid)->getBuffer(); |
Ted Kremenek | 3632a35 | 2009-01-28 20:46:26 +0000 | [diff] [blame] | 130 | } |
Ted Kremenek | 321abd4 | 2009-03-10 05:13:43 +0000 | [diff] [blame] | 131 | |
| 132 | std::pair<FileID, unsigned> FullSourceLoc::getDecomposedLoc() const { |
| 133 | return SrcMgr->getDecomposedLoc(*this); |
| 134 | } |