PlistSupport.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- PlistSupport.h - Plist Output Utilities ------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CLANG_BASIC_PLISTSUPPORT_H
  14. #define LLVM_CLANG_BASIC_PLISTSUPPORT_H
  15. #include "clang/Basic/LLVM.h"
  16. #include "clang/Basic/SourceLocation.h"
  17. #include "clang/Basic/SourceManager.h"
  18. #include "llvm/ADT/DenseMap.h"
  19. #include "llvm/ADT/SmallVector.h"
  20. #include "llvm/ADT/StringRef.h"
  21. #include "llvm/Support/raw_ostream.h"
  22. #include <cassert>
  23. #include <cstdint>
  24. namespace clang {
  25. namespace markup {
  26. using FIDMap = llvm::DenseMap<FileID, unsigned>;
  27. inline unsigned AddFID(FIDMap &FIDs, SmallVectorImpl<FileID> &V,
  28. FileID FID) {
  29. FIDMap::iterator I = FIDs.find(FID);
  30. if (I != FIDs.end())
  31. return I->second;
  32. unsigned NewValue = V.size();
  33. FIDs[FID] = NewValue;
  34. V.push_back(FID);
  35. return NewValue;
  36. }
  37. inline unsigned AddFID(FIDMap &FIDs, SmallVectorImpl<FileID> &V,
  38. const SourceManager &SM, SourceLocation L) {
  39. FileID FID = SM.getFileID(SM.getExpansionLoc(L));
  40. return AddFID(FIDs, V, FID);
  41. }
  42. inline unsigned GetFID(const FIDMap &FIDs, FileID FID) {
  43. FIDMap::const_iterator I = FIDs.find(FID);
  44. assert(I != FIDs.end());
  45. return I->second;
  46. }
  47. inline unsigned GetFID(const FIDMap &FIDs, const SourceManager &SM,
  48. SourceLocation L) {
  49. FileID FID = SM.getFileID(SM.getExpansionLoc(L));
  50. return GetFID(FIDs, FID);
  51. }
  52. inline raw_ostream &Indent(raw_ostream &o, const unsigned indent) {
  53. for (unsigned i = 0; i < indent; ++i)
  54. o << ' ';
  55. return o;
  56. }
  57. inline raw_ostream &EmitPlistHeader(raw_ostream &o) {
  58. static const char *PlistHeader =
  59. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  60. "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
  61. "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
  62. "<plist version=\"1.0\">\n";
  63. return o << PlistHeader;
  64. }
  65. inline raw_ostream &EmitInteger(raw_ostream &o, int64_t value) {
  66. o << "<integer>";
  67. o << value;
  68. o << "</integer>";
  69. return o;
  70. }
  71. inline raw_ostream &EmitString(raw_ostream &o, StringRef s) {
  72. o << "<string>";
  73. for (StringRef::const_iterator I = s.begin(), E = s.end(); I != E; ++I) {
  74. char c = *I;
  75. switch (c) {
  76. default:
  77. o << c;
  78. break;
  79. case '&':
  80. o << "&amp;";
  81. break;
  82. case '<':
  83. o << "&lt;";
  84. break;
  85. case '>':
  86. o << "&gt;";
  87. break;
  88. case '\'':
  89. o << "&apos;";
  90. break;
  91. case '\"':
  92. o << "&quot;";
  93. break;
  94. }
  95. }
  96. o << "</string>";
  97. return o;
  98. }
  99. inline void EmitLocation(raw_ostream &o, const SourceManager &SM,
  100. SourceLocation L, const FIDMap &FM, unsigned indent) {
  101. if (L.isInvalid()) return;
  102. FullSourceLoc Loc(SM.getExpansionLoc(L), const_cast<SourceManager &>(SM));
  103. Indent(o, indent) << "<dict>\n";
  104. Indent(o, indent) << " <key>line</key>";
  105. EmitInteger(o, Loc.getExpansionLineNumber()) << '\n';
  106. Indent(o, indent) << " <key>col</key>";
  107. EmitInteger(o, Loc.getExpansionColumnNumber()) << '\n';
  108. Indent(o, indent) << " <key>file</key>";
  109. EmitInteger(o, GetFID(FM, SM, Loc)) << '\n';
  110. Indent(o, indent) << "</dict>\n";
  111. }
  112. inline void EmitRange(raw_ostream &o, const SourceManager &SM,
  113. CharSourceRange R, const FIDMap &FM, unsigned indent) {
  114. if (R.isInvalid()) return;
  115. assert(R.isCharRange() && "cannot handle a token range");
  116. Indent(o, indent) << "<array>\n";
  117. EmitLocation(o, SM, R.getBegin(), FM, indent + 1);
  118. // The ".getLocWithOffset(-1)" emulates the behavior of an off-by-one bug
  119. // in Lexer that is already fixed. It is here for backwards compatibility
  120. // even though it is incorrect.
  121. EmitLocation(o, SM, R.getEnd().getLocWithOffset(-1), FM, indent + 1);
  122. Indent(o, indent) << "</array>\n";
  123. }
  124. } // namespace markup
  125. } // namespace clang
  126. #endif // LLVM_CLANG_BASIC_PLISTSUPPORT_H
  127. #ifdef __GNUC__
  128. #pragma GCC diagnostic pop
  129. #endif