SourceLocation.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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/SourceManager.h"
  16. #include "llvm/Bitcode/Serialize.h"
  17. #include "llvm/Bitcode/Deserialize.h"
  18. using namespace clang;
  19. void SourceLocation::Emit(llvm::Serializer& S) const {
  20. S.EmitInt(getRawEncoding());
  21. }
  22. SourceLocation SourceLocation::ReadVal(llvm::Deserializer& D) {
  23. return SourceLocation::getFromRawEncoding(D.ReadInt());
  24. }
  25. void SourceRange::Emit(llvm::Serializer& S) const {
  26. B.Emit(S);
  27. E.Emit(S);
  28. }
  29. SourceRange SourceRange::ReadVal(llvm::Deserializer& D) {
  30. SourceLocation A = SourceLocation::ReadVal(D);
  31. SourceLocation B = SourceLocation::ReadVal(D);
  32. return SourceRange(A,B);
  33. }
  34. FileID FullSourceLoc::getFileID() const {
  35. assert(isValid());
  36. return SrcMgr->getFileID(*this);
  37. }
  38. FullSourceLoc FullSourceLoc::getInstantiationLoc() const {
  39. assert(isValid());
  40. return FullSourceLoc(SrcMgr->getInstantiationLoc(*this), *SrcMgr);
  41. }
  42. FullSourceLoc FullSourceLoc::getSpellingLoc() const {
  43. assert(isValid());
  44. return FullSourceLoc(SrcMgr->getSpellingLoc(*this), *SrcMgr);
  45. }
  46. FullSourceLoc FullSourceLoc::getIncludeLoc() const {
  47. assert(isValid());
  48. return FullSourceLoc(SrcMgr->getIncludeLoc(*this), *SrcMgr);
  49. }
  50. unsigned FullSourceLoc::getLineNumber() const {
  51. assert(isValid());
  52. return SrcMgr->getLineNumber(*this);
  53. }
  54. unsigned FullSourceLoc::getColumnNumber() const {
  55. assert(isValid());
  56. return SrcMgr->getColumnNumber(*this);
  57. }
  58. unsigned FullSourceLoc::getInstantiationLineNumber() const {
  59. assert(isValid());
  60. return SrcMgr->getInstantiationLineNumber(*this);
  61. }
  62. unsigned FullSourceLoc::getInstantiationColumnNumber() const {
  63. assert(isValid());
  64. return SrcMgr->getInstantiationColumnNumber(*this);
  65. }
  66. unsigned FullSourceLoc::getSpellingLineNumber() const {
  67. assert(isValid());
  68. return SrcMgr->getSpellingLineNumber(*this);
  69. }
  70. unsigned FullSourceLoc::getSpellingColumnNumber() const {
  71. assert(isValid());
  72. return SrcMgr->getSpellingColumnNumber(*this);
  73. }
  74. const char* FullSourceLoc::getSourceName() const {
  75. assert(isValid());
  76. return SrcMgr->getSourceName(*this);
  77. }
  78. bool FullSourceLoc::isInSystemHeader() const {
  79. assert(isValid());
  80. return SrcMgr->isInSystemHeader(*this);
  81. }
  82. const char *FullSourceLoc::getCharacterData() const {
  83. assert(isValid());
  84. return SrcMgr->getCharacterData(*this);
  85. }
  86. const llvm::MemoryBuffer* FullSourceLoc::getBuffer() const {
  87. assert(isValid());
  88. return SrcMgr->getBuffer(SrcMgr->getFileID(*this));
  89. }
  90. void FullSourceLoc::dump() const {
  91. if (!isValid()) {
  92. fprintf(stderr, "Invalid Loc\n");
  93. return;
  94. }
  95. if (isFileID()) {
  96. // The instantiation and spelling pos is identical for file locs.
  97. fprintf(stderr, "File Loc from '%s': %d: %d\n",
  98. getSourceName(), getInstantiationLineNumber(),
  99. getInstantiationColumnNumber());
  100. } else {
  101. fprintf(stderr, "Macro Loc (\n Spelling: ");
  102. getSpellingLoc().dump();
  103. fprintf(stderr, " Instantiation: ");
  104. getInstantiationLoc().dump();
  105. fprintf(stderr, ")\n");
  106. }
  107. }