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 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 26 | void PrettyStackTraceLoc::print(raw_ostream &OS) const { |
Chris Lattner | ae50fa0 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 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 | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 38 | void SourceLocation::print(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 | } |
Chandler Carruth | 3201f38 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 51 | // The macro expansion 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 | |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 57 | SM.getExpansionLoc(*this).print(OS, SM); |
Chris Lattner | ae50fa0 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 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 | |
Argyrios Kyrtzidis | 9410928 | 2012-11-09 19:40:48 +0000 | [diff] [blame] | 64 | std::string SourceLocation::printToString(const SourceManager &SM) const { |
| 65 | std::string S; |
| 66 | llvm::raw_string_ostream OS(S); |
| 67 | print(OS, SM); |
Benjamin Kramer | 21fa5d1 | 2012-12-12 14:17:17 +0000 | [diff] [blame] | 68 | return OS.str(); |
Argyrios Kyrtzidis | 9410928 | 2012-11-09 19:40:48 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Chris Lattner | ae50fa0 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 71 | void SourceLocation::dump(const SourceManager &SM) const { |
| 72 | print(llvm::errs(), SM); |
Chris Lattner | ae50fa0 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 73 | } |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 74 | |
Chris Lattner | ae50fa0 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 75 | //===----------------------------------------------------------------------===// |
| 76 | // FullSourceLoc |
| 77 | //===----------------------------------------------------------------------===// |
| 78 | |
Chris Lattner | 3b4d5e9 | 2009-01-17 08:45:21 +0000 | [diff] [blame] | 79 | FileID FullSourceLoc::getFileID() const { |
| 80 | assert(isValid()); |
Chris Lattner | a11d617 | 2009-01-19 07:46:45 +0000 | [diff] [blame] | 81 | return SrcMgr->getFileID(*this); |
Chris Lattner | 3b4d5e9 | 2009-01-17 08:45:21 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 85 | FullSourceLoc FullSourceLoc::getExpansionLoc() const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 86 | assert(isValid()); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 87 | return FullSourceLoc(SrcMgr->getExpansionLoc(*this), *SrcMgr); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 90 | FullSourceLoc FullSourceLoc::getSpellingLoc() const { |
| 91 | assert(isValid()); |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 92 | return FullSourceLoc(SrcMgr->getSpellingLoc(*this), *SrcMgr); |
Chris Lattner | 5c38b63 | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Chandler Carruth | 6421162 | 2011-07-25 21:09:52 +0000 | [diff] [blame] | 95 | unsigned FullSourceLoc::getExpansionLineNumber(bool *Invalid) const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 96 | assert(isValid()); |
Chandler Carruth | 6421162 | 2011-07-25 21:09:52 +0000 | [diff] [blame] | 97 | return SrcMgr->getExpansionLineNumber(*this, Invalid); |
Ted Kremenek | 1758b07 | 2008-04-03 17:55:15 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Chandler Carruth | a77c031 | 2011-07-25 20:57:57 +0000 | [diff] [blame] | 100 | unsigned FullSourceLoc::getExpansionColumnNumber(bool *Invalid) const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 101 | assert(isValid()); |
Chandler Carruth | a77c031 | 2011-07-25 20:57:57 +0000 | [diff] [blame] | 102 | return SrcMgr->getExpansionColumnNumber(*this, Invalid); |
Ted Kremenek | 1758b07 | 2008-04-03 17:55:15 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Douglas Gregor | 64e462d | 2010-03-16 20:53:17 +0000 | [diff] [blame] | 105 | unsigned FullSourceLoc::getSpellingLineNumber(bool *Invalid) const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 106 | assert(isValid()); |
Douglas Gregor | 64e462d | 2010-03-16 20:53:17 +0000 | [diff] [blame] | 107 | return SrcMgr->getSpellingLineNumber(*this, Invalid); |
Chris Lattner | 5c38b63 | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Douglas Gregor | 64e462d | 2010-03-16 20:53:17 +0000 | [diff] [blame] | 110 | unsigned FullSourceLoc::getSpellingColumnNumber(bool *Invalid) const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 111 | assert(isValid()); |
Douglas Gregor | 64e462d | 2010-03-16 20:53:17 +0000 | [diff] [blame] | 112 | return SrcMgr->getSpellingColumnNumber(*this, Invalid); |
Chris Lattner | 5c38b63 | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 115 | bool FullSourceLoc::isInSystemHeader() const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 116 | assert(isValid()); |
| 117 | return SrcMgr->isInSystemHeader(*this); |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 120 | bool FullSourceLoc::isBeforeInTranslationUnitThan(SourceLocation Loc) const { |
| 121 | assert(isValid()); |
| 122 | return SrcMgr->isBeforeInTranslationUnit(*this, Loc); |
| 123 | } |
| 124 | |
Benjamin Kramer | a6c1068 | 2012-02-26 16:55:50 +0000 | [diff] [blame] | 125 | void FullSourceLoc::dump() const { |
| 126 | SourceLocation::dump(*SrcMgr); |
| 127 | } |
| 128 | |
Douglas Gregor | a543016 | 2010-03-16 20:46:42 +0000 | [diff] [blame] | 129 | const char *FullSourceLoc::getCharacterData(bool *Invalid) const { |
Chris Lattner | 4abb87e | 2009-01-16 22:59:51 +0000 | [diff] [blame] | 130 | assert(isValid()); |
Douglas Gregor | a543016 | 2010-03-16 20:46:42 +0000 | [diff] [blame] | 131 | return SrcMgr->getCharacterData(*this, Invalid); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Douglas Gregor | aae58b0 | 2010-03-16 20:01:30 +0000 | [diff] [blame] | 134 | const llvm::MemoryBuffer* FullSourceLoc::getBuffer(bool *Invalid) const { |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 135 | assert(isValid()); |
Douglas Gregor | aae58b0 | 2010-03-16 20:01:30 +0000 | [diff] [blame] | 136 | return SrcMgr->getBuffer(SrcMgr->getFileID(*this), Invalid); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 137 | } |
Ted Kremenek | 9fd87b1 | 2008-04-14 21:04:18 +0000 | [diff] [blame] | 138 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 139 | StringRef FullSourceLoc::getBufferData(bool *Invalid) const { |
Douglas Gregor | aae58b0 | 2010-03-16 20:01:30 +0000 | [diff] [blame] | 140 | return getBuffer(Invalid)->getBuffer(); |
Ted Kremenek | 3632a35 | 2009-01-28 20:46:26 +0000 | [diff] [blame] | 141 | } |
Ted Kremenek | 321abd4 | 2009-03-10 05:13:43 +0000 | [diff] [blame] | 142 | |
| 143 | std::pair<FileID, unsigned> FullSourceLoc::getDecomposedLoc() const { |
| 144 | return SrcMgr->getDecomposedLoc(*this); |
| 145 | } |