HTMLPrint.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //===--- HTMLPrint.cpp - Source code -> HTML pretty-printing --------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Pretty-printing of source code to HTML.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/AST/ASTConsumer.h"
  13. #include "clang/AST/ASTContext.h"
  14. #include "clang/AST/Decl.h"
  15. #include "clang/Basic/Diagnostic.h"
  16. #include "clang/Basic/FileManager.h"
  17. #include "clang/Basic/SourceManager.h"
  18. #include "clang/Lex/Preprocessor.h"
  19. #include "clang/Rewrite/Core/HTMLRewrite.h"
  20. #include "clang/Rewrite/Core/Rewriter.h"
  21. #include "clang/Rewrite/Frontend/ASTConsumers.h"
  22. #include "llvm/Support/raw_ostream.h"
  23. using namespace clang;
  24. //===----------------------------------------------------------------------===//
  25. // Functional HTML pretty-printing.
  26. //===----------------------------------------------------------------------===//
  27. namespace {
  28. class HTMLPrinter : public ASTConsumer {
  29. Rewriter R;
  30. std::unique_ptr<raw_ostream> Out;
  31. Preprocessor &PP;
  32. bool SyntaxHighlight, HighlightMacros;
  33. public:
  34. HTMLPrinter(std::unique_ptr<raw_ostream> OS, Preprocessor &pp,
  35. bool _SyntaxHighlight, bool _HighlightMacros)
  36. : Out(std::move(OS)), PP(pp), SyntaxHighlight(_SyntaxHighlight),
  37. HighlightMacros(_HighlightMacros) {}
  38. void Initialize(ASTContext &context) override;
  39. void HandleTranslationUnit(ASTContext &Ctx) override;
  40. };
  41. }
  42. std::unique_ptr<ASTConsumer>
  43. clang::CreateHTMLPrinter(std::unique_ptr<raw_ostream> OS, Preprocessor &PP,
  44. bool SyntaxHighlight, bool HighlightMacros) {
  45. return std::make_unique<HTMLPrinter>(std::move(OS), PP, SyntaxHighlight,
  46. HighlightMacros);
  47. }
  48. void HTMLPrinter::Initialize(ASTContext &context) {
  49. R.setSourceMgr(context.getSourceManager(), context.getLangOpts());
  50. }
  51. void HTMLPrinter::HandleTranslationUnit(ASTContext &Ctx) {
  52. if (PP.getDiagnostics().hasErrorOccurred())
  53. return;
  54. // Format the file.
  55. FileID FID = R.getSourceMgr().getMainFileID();
  56. const FileEntry* Entry = R.getSourceMgr().getFileEntryForID(FID);
  57. StringRef Name;
  58. // In some cases, in particular the case where the input is from stdin,
  59. // there is no entry. Fall back to the memory buffer for a name in those
  60. // cases.
  61. if (Entry)
  62. Name = Entry->getName();
  63. else
  64. Name = R.getSourceMgr().getBufferOrFake(FID).getBufferIdentifier();
  65. html::AddLineNumbers(R, FID);
  66. html::AddHeaderFooterInternalBuiltinCSS(R, FID, Name);
  67. // If we have a preprocessor, relex the file and syntax highlight.
  68. // We might not have a preprocessor if we come from a deserialized AST file,
  69. // for example.
  70. if (SyntaxHighlight) html::SyntaxHighlight(R, FID, PP);
  71. if (HighlightMacros) html::HighlightMacros(R, FID, PP);
  72. html::EscapeText(R, FID, false, true);
  73. // Emit the HTML.
  74. const RewriteBuffer &RewriteBuf = R.getEditBuffer(FID);
  75. std::unique_ptr<char[]> Buffer(new char[RewriteBuf.size()]);
  76. std::copy(RewriteBuf.begin(), RewriteBuf.end(), Buffer.get());
  77. Out->write(Buffer.get(), RewriteBuf.size());
  78. }