Path: blob/main/contrib/llvm-project/clang/lib/Frontend/Rewrite/HTMLPrint.cpp
35266 views
//===--- HTMLPrint.cpp - Source code -> HTML pretty-printing --------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// Pretty-printing of source code to HTML.9//10//===----------------------------------------------------------------------===//1112#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"23using namespace clang;2425//===----------------------------------------------------------------------===//26// Functional HTML pretty-printing.27//===----------------------------------------------------------------------===//2829namespace {30class HTMLPrinter : public ASTConsumer {31Rewriter R;32std::unique_ptr<raw_ostream> Out;33Preprocessor &PP;34bool SyntaxHighlight, HighlightMacros;3536public:37HTMLPrinter(std::unique_ptr<raw_ostream> OS, Preprocessor &pp,38bool _SyntaxHighlight, bool _HighlightMacros)39: Out(std::move(OS)), PP(pp), SyntaxHighlight(_SyntaxHighlight),40HighlightMacros(_HighlightMacros) {}4142void Initialize(ASTContext &context) override;43void HandleTranslationUnit(ASTContext &Ctx) override;44};45}4647std::unique_ptr<ASTConsumer>48clang::CreateHTMLPrinter(std::unique_ptr<raw_ostream> OS, Preprocessor &PP,49bool SyntaxHighlight, bool HighlightMacros) {50return std::make_unique<HTMLPrinter>(std::move(OS), PP, SyntaxHighlight,51HighlightMacros);52}5354void HTMLPrinter::Initialize(ASTContext &context) {55R.setSourceMgr(context.getSourceManager(), context.getLangOpts());56}5758void HTMLPrinter::HandleTranslationUnit(ASTContext &Ctx) {59if (PP.getDiagnostics().hasErrorOccurred())60return;6162// Format the file.63FileID FID = R.getSourceMgr().getMainFileID();64OptionalFileEntryRef Entry = R.getSourceMgr().getFileEntryRefForID(FID);65StringRef Name;66// In some cases, in particular the case where the input is from stdin,67// there is no entry. Fall back to the memory buffer for a name in those68// cases.69if (Entry)70Name = Entry->getName();71else72Name = R.getSourceMgr().getBufferOrFake(FID).getBufferIdentifier();7374html::AddLineNumbers(R, FID);75html::AddHeaderFooterInternalBuiltinCSS(R, FID, Name);7677// If we have a preprocessor, relex the file and syntax highlight.78// We might not have a preprocessor if we come from a deserialized AST file,79// for example.8081if (SyntaxHighlight) html::SyntaxHighlight(R, FID, PP);82if (HighlightMacros) html::HighlightMacros(R, FID, PP);83html::EscapeText(R, FID, false, true);8485// Emit the HTML.86const RewriteBuffer &RewriteBuf = R.getEditBuffer(FID);87std::unique_ptr<char[]> Buffer(new char[RewriteBuf.size()]);88std::copy(RewriteBuf.begin(), RewriteBuf.end(), Buffer.get());89Out->write(Buffer.get(), RewriteBuf.size());90}919293