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