ClangCommentHTMLNamedCharacterReferenceEmitter.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //===--- ClangCommentHTMLNamedCharacterReferenceEmitter.cpp -----------------=//
  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 tablegen backend emits an fficient function to translate HTML named
  11. // character references to UTF-8 sequences.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/ADT/SmallString.h"
  15. #include "llvm/Support/ConvertUTF.h"
  16. #include "llvm/TableGen/Error.h"
  17. #include "llvm/TableGen/Record.h"
  18. #include "llvm/TableGen/StringMatcher.h"
  19. #include "llvm/TableGen/TableGenBackend.h"
  20. #include <vector>
  21. using namespace llvm;
  22. /// \brief Convert a code point to the corresponding UTF-8 sequence represented
  23. /// as a C string literal.
  24. ///
  25. /// \returns true on success.
  26. static bool translateCodePointToUTF8(unsigned CodePoint,
  27. SmallVectorImpl<char> &CLiteral) {
  28. char Translated[UNI_MAX_UTF8_BYTES_PER_CODE_POINT];
  29. char *TranslatedPtr = Translated;
  30. if (!ConvertCodePointToUTF8(CodePoint, TranslatedPtr))
  31. return false;
  32. StringRef UTF8(Translated, TranslatedPtr - Translated);
  33. raw_svector_ostream OS(CLiteral);
  34. OS << "\"";
  35. for (size_t i = 0, e = UTF8.size(); i != e; ++i) {
  36. OS << "\\x";
  37. OS.write_hex(static_cast<unsigned char>(UTF8[i]));
  38. }
  39. OS << "\"";
  40. return true;
  41. }
  42. namespace clang {
  43. void EmitClangCommentHTMLNamedCharacterReferences(RecordKeeper &Records,
  44. raw_ostream &OS) {
  45. std::vector<Record *> Tags = Records.getAllDerivedDefinitions("NCR");
  46. std::vector<StringMatcher::StringPair> NameToUTF8;
  47. SmallString<32> CLiteral;
  48. for (std::vector<Record *>::iterator I = Tags.begin(), E = Tags.end();
  49. I != E; ++I) {
  50. Record &Tag = **I;
  51. std::string Spelling = Tag.getValueAsString("Spelling");
  52. uint64_t CodePoint = Tag.getValueAsInt("CodePoint");
  53. CLiteral.clear();
  54. CLiteral.append("return ");
  55. if (!translateCodePointToUTF8(CodePoint, CLiteral)) {
  56. SrcMgr.PrintMessage(Tag.getLoc().front(),
  57. SourceMgr::DK_Error,
  58. Twine("invalid code point"));
  59. continue;
  60. }
  61. CLiteral.append(";");
  62. StringMatcher::StringPair Match(Spelling, CLiteral.str());
  63. NameToUTF8.push_back(Match);
  64. }
  65. emitSourceFileHeader("HTML named character reference to UTF-8 "
  66. "translation", OS);
  67. OS << "StringRef translateHTMLNamedCharacterReferenceToUTF8(\n"
  68. " StringRef Name) {\n";
  69. StringMatcher("Name", NameToUTF8, OS).Emit();
  70. OS << " return StringRef();\n"
  71. << "}\n\n";
  72. }
  73. } // end namespace clang