2008-04-16 12:38:45 +08:00
|
|
|
//===--- HTMLPrint.cpp - Source code -> HTML pretty-printing --------------===//
|
2008-03-19 06:21:07 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-04-16 12:38:45 +08:00
|
|
|
// Pretty-printing of source code to HTML.
|
2008-03-19 06:21:07 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2009-11-05 07:56:25 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2008-08-11 12:54:23 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
2008-04-16 13:21:09 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2008-07-08 02:31:05 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
2009-11-05 07:56:25 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2012-09-01 13:09:24 +08:00
|
|
|
#include "clang/Rewrite/Core/HTMLRewrite.h"
|
|
|
|
#include "clang/Rewrite/Core/Rewriter.h"
|
2016-07-19 03:02:11 +08:00
|
|
|
#include "clang/Rewrite/Frontend/ASTConsumers.h"
|
2009-08-24 12:11:30 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2008-03-19 06:21:07 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2008-03-19 15:53:42 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Functional HTML pretty-printing.
|
2009-09-09 23:08:12 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-03-19 15:53:42 +08:00
|
|
|
|
2008-03-19 06:21:07 +08:00
|
|
|
namespace {
|
|
|
|
class HTMLPrinter : public ASTConsumer {
|
|
|
|
Rewriter R;
|
2016-07-15 08:55:40 +08:00
|
|
|
std::unique_ptr<raw_ostream> Out;
|
2009-11-05 07:56:25 +08:00
|
|
|
Preprocessor &PP;
|
|
|
|
bool SyntaxHighlight, HighlightMacros;
|
|
|
|
|
2008-03-19 06:21:07 +08:00
|
|
|
public:
|
2016-07-15 08:55:40 +08:00
|
|
|
HTMLPrinter(std::unique_ptr<raw_ostream> OS, Preprocessor &pp,
|
2009-11-05 07:56:25 +08:00
|
|
|
bool _SyntaxHighlight, bool _HighlightMacros)
|
2016-07-15 08:55:40 +08:00
|
|
|
: Out(std::move(OS)), PP(pp), SyntaxHighlight(_SyntaxHighlight),
|
2009-11-05 07:56:25 +08:00
|
|
|
HighlightMacros(_HighlightMacros) {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-03-15 12:29:04 +08:00
|
|
|
void Initialize(ASTContext &context) override;
|
|
|
|
void HandleTranslationUnit(ASTContext &Ctx) override;
|
2008-03-19 06:21:07 +08:00
|
|
|
};
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2008-03-19 06:21:07 +08:00
|
|
|
|
2016-07-15 08:55:40 +08:00
|
|
|
std::unique_ptr<ASTConsumer>
|
|
|
|
clang::CreateHTMLPrinter(std::unique_ptr<raw_ostream> OS, Preprocessor &PP,
|
|
|
|
bool SyntaxHighlight, bool HighlightMacros) {
|
|
|
|
return llvm::make_unique<HTMLPrinter>(std::move(OS), PP, SyntaxHighlight,
|
2014-08-11 03:56:51 +08:00
|
|
|
HighlightMacros);
|
2008-04-16 13:21:09 +08:00
|
|
|
}
|
2008-03-19 06:21:07 +08:00
|
|
|
|
|
|
|
void HTMLPrinter::Initialize(ASTContext &context) {
|
2012-03-11 15:00:24 +08:00
|
|
|
R.setSourceMgr(context.getSourceManager(), context.getLangOpts());
|
2008-03-19 06:21:07 +08:00
|
|
|
}
|
|
|
|
|
2009-12-12 11:36:52 +08:00
|
|
|
void HTMLPrinter::HandleTranslationUnit(ASTContext &Ctx) {
|
2009-11-05 07:56:25 +08:00
|
|
|
if (PP.getDiagnostics().hasErrorOccurred())
|
2008-04-16 13:21:09 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Format the file.
|
2009-01-17 14:22:33 +08:00
|
|
|
FileID FID = R.getSourceMgr().getMainFileID();
|
|
|
|
const FileEntry* Entry = R.getSourceMgr().getFileEntryForID(FID);
|
2016-10-02 00:38:28 +08:00
|
|
|
StringRef Name;
|
2009-05-19 13:28:52 +08:00
|
|
|
// In some cases, in particular the case where the input is from stdin,
|
|
|
|
// there is no entry. Fall back to the memory buffer for a name in those
|
|
|
|
// cases.
|
|
|
|
if (Entry)
|
|
|
|
Name = Entry->getName();
|
|
|
|
else
|
|
|
|
Name = R.getSourceMgr().getBuffer(FID)->getBufferIdentifier();
|
|
|
|
|
2009-01-17 14:22:33 +08:00
|
|
|
html::AddLineNumbers(R, FID);
|
2009-05-19 13:28:52 +08:00
|
|
|
html::AddHeaderFooterInternalBuiltinCSS(R, FID, Name);
|
2008-04-16 13:21:09 +08:00
|
|
|
|
2008-04-17 00:39:56 +08:00
|
|
|
// If we have a preprocessor, relex the file and syntax highlight.
|
|
|
|
// We might not have a preprocessor if we come from a deserialized AST file,
|
|
|
|
// for example.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-05 07:56:25 +08:00
|
|
|
if (SyntaxHighlight) html::SyntaxHighlight(R, FID, PP);
|
|
|
|
if (HighlightMacros) html::HighlightMacros(R, FID, PP);
|
2009-01-17 14:22:33 +08:00
|
|
|
html::EscapeText(R, FID, false, true);
|
2009-05-19 06:20:00 +08:00
|
|
|
|
2008-03-19 15:53:42 +08:00
|
|
|
// Emit the HTML.
|
2009-01-17 14:22:33 +08:00
|
|
|
const RewriteBuffer &RewriteBuf = R.getEditBuffer(FID);
|
2008-04-16 13:21:09 +08:00
|
|
|
char *Buffer = (char*)malloc(RewriteBuf.size());
|
|
|
|
std::copy(RewriteBuf.begin(), RewriteBuf.end(), Buffer);
|
2009-05-19 06:20:00 +08:00
|
|
|
Out->write(Buffer, RewriteBuf.size());
|
2008-04-16 13:21:09 +08:00
|
|
|
free(Buffer);
|
2008-03-19 15:53:42 +08:00
|
|
|
}
|