#include "clang/AST/ASTConsumer.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Rewrite/Core/HTMLRewrite.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "clang/Rewrite/Frontend/ASTConsumers.h"
#include "llvm/Support/raw_ostream.h"
using namespace clang;
namespace {
class HTMLPrinter : public ASTConsumer {
Rewriter R;
std::unique_ptr<raw_ostream> Out;
Preprocessor &PP;
bool SyntaxHighlight, HighlightMacros;
public:
HTMLPrinter(std::unique_ptr<raw_ostream> OS, Preprocessor &pp,
bool _SyntaxHighlight, bool _HighlightMacros)
: Out(std::move(OS)), PP(pp), SyntaxHighlight(_SyntaxHighlight),
HighlightMacros(_HighlightMacros) {}
void Initialize(ASTContext &context) override;
void HandleTranslationUnit(ASTContext &Ctx) override;
};
}
std::unique_ptr<ASTConsumer>
clang::CreateHTMLPrinter(std::unique_ptr<raw_ostream> OS, Preprocessor &PP,
bool SyntaxHighlight, bool HighlightMacros) {
return std::make_unique<HTMLPrinter>(std::move(OS), PP, SyntaxHighlight,
HighlightMacros);
}
void HTMLPrinter::Initialize(ASTContext &context) {
R.setSourceMgr(context.getSourceManager(), context.getLangOpts());
}
void HTMLPrinter::HandleTranslationUnit(ASTContext &Ctx) {
if (PP.getDiagnostics().hasErrorOccurred())
return;
FileID FID = R.getSourceMgr().getMainFileID();
const FileEntry* Entry = R.getSourceMgr().getFileEntryForID(FID);
StringRef Name;
if (Entry)
Name = Entry->getName();
else
Name = R.getSourceMgr().getBufferOrFake(FID).getBufferIdentifier();
html::AddLineNumbers(R, FID);
html::AddHeaderFooterInternalBuiltinCSS(R, FID, Name);
if (SyntaxHighlight) html::SyntaxHighlight(R, FID, PP);
if (HighlightMacros) html::HighlightMacros(R, FID, PP);
html::EscapeText(R, FID, false, true);
const RewriteBuffer &RewriteBuf = R.getEditBuffer(FID);
std::unique_ptr<char[]> Buffer(new char[RewriteBuf.size()]);
std::copy(RewriteBuf.begin(), RewriteBuf.end(), Buffer.get());
Out->write(Buffer.get(), RewriteBuf.size());
}