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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-05-19 06:50:54 +08:00
|
|
|
#include "clang/Frontend/ASTConsumers.h"
|
2008-03-19 06:21:07 +08:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2008-08-11 12:54:23 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
2008-03-19 06:21:07 +08:00
|
|
|
#include "clang/Rewrite/Rewriter.h"
|
|
|
|
#include "clang/Rewrite/HTMLRewrite.h"
|
2008-04-16 13:21:09 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2008-03-19 06:21:07 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2008-07-08 02:31:05 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
2008-03-19 06:21:07 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2009-05-19 13:28:52 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.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;
|
2009-05-19 06:20:00 +08:00
|
|
|
llvm::raw_ostream *Out;
|
2008-04-16 13:21:09 +08:00
|
|
|
Diagnostic &Diags;
|
2008-04-16 14:11:58 +08:00
|
|
|
Preprocessor *PP;
|
2008-04-18 06:31:54 +08:00
|
|
|
PreprocessorFactory *PPF;
|
2008-03-19 06:21:07 +08:00
|
|
|
public:
|
2009-05-19 06:20:00 +08:00
|
|
|
HTMLPrinter(llvm::raw_ostream *OS, Diagnostic &D, Preprocessor *pp,
|
2008-04-18 06:31:54 +08:00
|
|
|
PreprocessorFactory* ppf)
|
2009-05-19 06:20:00 +08:00
|
|
|
: Out(OS), Diags(D), PP(pp), PPF(ppf) {}
|
2008-03-19 06:21:07 +08:00
|
|
|
virtual ~HTMLPrinter();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-19 06:21:07 +08:00
|
|
|
void Initialize(ASTContext &context);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2009-05-19 06:20:00 +08:00
|
|
|
ASTConsumer* clang::CreateHTMLPrinter(llvm::raw_ostream *OS,
|
2008-04-18 06:31:54 +08:00
|
|
|
Diagnostic &D, Preprocessor *PP,
|
|
|
|
PreprocessorFactory* PPF) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-05-19 06:20:00 +08:00
|
|
|
return new HTMLPrinter(OS, D, PP, PPF);
|
2008-04-16 13:21:09 +08:00
|
|
|
}
|
2008-03-19 06:21:07 +08:00
|
|
|
|
|
|
|
void HTMLPrinter::Initialize(ASTContext &context) {
|
2009-04-15 07:22:57 +08:00
|
|
|
R.setSourceMgr(context.getSourceManager(), context.getLangOptions());
|
2008-03-19 06:21:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
HTMLPrinter::~HTMLPrinter() {
|
2008-04-16 13:21:09 +08:00
|
|
|
if (Diags.hasErrorOccurred())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Format the file.
|
2009-01-17 14:22:33 +08:00
|
|
|
FileID FID = R.getSourceMgr().getMainFileID();
|
|
|
|
const FileEntry* Entry = R.getSourceMgr().getFileEntryForID(FID);
|
2009-05-19 13:28:52 +08:00
|
|
|
const char* Name;
|
|
|
|
// 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-01-17 14:22:33 +08:00
|
|
|
if (PP) html::SyntaxHighlight(R, FID, *PP);
|
|
|
|
if (PPF) html::HighlightMacros(R, FID, *PP);
|
|
|
|
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
|
|
|
}
|