SourceLocation.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //==--- SourceLocation.cpp - Compact identifier for Source Files -*- C++ -*-==//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines serialization methods for the SourceLocation class.
  11. // This file defines accessor methods for the FullSourceLoc class.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "clang/Basic/SourceLocation.h"
  15. #include "clang/Basic/PrettyStackTrace.h"
  16. #include "clang/Basic/SourceManager.h"
  17. #include "llvm/Bitcode/Serialize.h"
  18. #include "llvm/Bitcode/Deserialize.h"
  19. #include "llvm/Support/MemoryBuffer.h"
  20. #include "llvm/Support/raw_ostream.h"
  21. #include <cstdio>
  22. using namespace clang;
  23. //===----------------------------------------------------------------------===//
  24. // PrettyStackTraceLoc
  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. // SourceLocation
  35. //===----------------------------------------------------------------------===//
  36. void SourceLocation::Emit(llvm::Serializer& S) const {
  37. S.EmitInt(getRawEncoding());
  38. }
  39. SourceLocation SourceLocation::ReadVal(llvm::Deserializer& D) {
  40. return SourceLocation::getFromRawEncoding(D.ReadInt());
  41. }
  42. void SourceLocation::print(llvm::raw_ostream &OS, const SourceManager &SM)const{
  43. if (!isValid()) {
  44. OS << "<invalid loc>";
  45. return;
  46. }
  47. if (isFileID()) {
  48. PresumedLoc PLoc = SM.getPresumedLoc(*this);
  49. // The instantiation and spelling pos is identical for file locs.
  50. OS << PLoc.getFilename() << ':' << PLoc.getLine()
  51. << ':' << PLoc.getColumn();
  52. return;
  53. }
  54. SM.getInstantiationLoc(*this).print(OS, SM);
  55. OS << " <Spelling=";
  56. SM.getSpellingLoc(*this).print(OS, SM);
  57. OS << '>';
  58. }
  59. void SourceLocation::dump(const SourceManager &SM) const {
  60. print(llvm::errs(), SM);
  61. }
  62. void SourceRange::Emit(llvm::Serializer& S) const {
  63. B.Emit(S);
  64. E.Emit(S);
  65. }
  66. SourceRange SourceRange::ReadVal(llvm::Deserializer& D) {
  67. SourceLocation A = SourceLocation::ReadVal(D);
  68. SourceLocation B = SourceLocation::ReadVal(D);
  69. return SourceRange(A,B);
  70. }
  71. //===----------------------------------------------------------------------===//
  72. // FullSourceLoc
  73. //===----------------------------------------------------------------------===//
  74. FileID FullSourceLoc::getFileID() const {
  75. assert(isValid());
  76. return SrcMgr->getFileID(*this);
  77. }
  78. FullSourceLoc FullSourceLoc::getInstantiationLoc() const {
  79. assert(isValid());
  80. return FullSourceLoc(SrcMgr->getInstantiationLoc(*this), *SrcMgr);
  81. }
  82. FullSourceLoc FullSourceLoc::getSpellingLoc() const {
  83. assert(isValid());
  84. return FullSourceLoc(SrcMgr->getSpellingLoc(*this), *SrcMgr);
  85. }
  86. unsigned FullSourceLoc::getInstantiationLineNumber() const {
  87. assert(isValid());
  88. return SrcMgr->getInstantiationLineNumber(*this);
  89. }
  90. unsigned FullSourceLoc::getInstantiationColumnNumber() const {
  91. assert(isValid());
  92. return SrcMgr->getInstantiationColumnNumber(*this);
  93. }
  94. unsigned FullSourceLoc::getSpellingLineNumber() const {
  95. assert(isValid());
  96. return SrcMgr->getSpellingLineNumber(*this);
  97. }
  98. unsigned FullSourceLoc::getSpellingColumnNumber() const {
  99. assert(isValid());
  100. return SrcMgr->getSpellingColumnNumber(*this);
  101. }
  102. bool FullSourceLoc::isInSystemHeader() const {
  103. assert(isValid());
  104. return SrcMgr->isInSystemHeader(*this);
  105. }
  106. const char *FullSourceLoc::getCharacterData() const {
  107. assert(isValid());
  108. return SrcMgr->getCharacterData(*this);
  109. }
  110. const llvm::MemoryBuffer* FullSourceLoc::getBuffer() const {
  111. assert(isValid());
  112. return SrcMgr->getBuffer(SrcMgr->getFileID(*this));
  113. }
  114. std::pair<const char*, const char*> FullSourceLoc::getBufferData() const {
  115. const llvm::MemoryBuffer *Buf = getBuffer();
  116. return std::make_pair(Buf->getBufferStart(), Buf->getBufferEnd());
  117. }
  118. std::pair<FileID, unsigned> FullSourceLoc::getDecomposedLoc() const {
  119. return SrcMgr->getDecomposedLoc(*this);
  120. }