2008-04-16 04:38:45 +00:00
|
|
|
//===--- HTMLPrint.cpp - Source code -> HTML pretty-printing --------------===//
|
2008-03-18 22:21:07 +00: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 04:38:45 +00:00
|
|
|
// Pretty-printing of source code to HTML.
|
2008-03-18 22:21:07 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-05-18 22:50:54 +00:00
|
|
|
#include "clang/Frontend/ASTConsumers.h"
|
2008-03-18 22:21:07 +00:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2008-08-11 04:54:23 +00:00
|
|
|
#include "clang/AST/Decl.h"
|
2008-03-18 22:21:07 +00:00
|
|
|
#include "clang/Rewrite/Rewriter.h"
|
|
|
|
#include "clang/Rewrite/HTMLRewrite.h"
|
2008-04-16 05:21:09 +00:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2008-03-18 22:21:07 +00:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2008-07-07 18:31:05 +00:00
|
|
|
#include "clang/Basic/FileManager.h"
|
2008-03-18 22:21:07 +00:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2009-05-19 05:28:52 +00:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2008-03-18 22:21:07 +00:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2008-03-19 07:53:42 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Functional HTML pretty-printing.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-03-18 22:21:07 +00:00
|
|
|
namespace {
|
|
|
|
class HTMLPrinter : public ASTConsumer {
|
|
|
|
Rewriter R;
|
2009-05-18 22:20:00 +00:00
|
|
|
llvm::raw_ostream *Out;
|
2008-04-16 05:21:09 +00:00
|
|
|
Diagnostic &Diags;
|
2008-04-16 06:11:58 +00:00
|
|
|
Preprocessor *PP;
|
2008-04-17 22:31:54 +00:00
|
|
|
PreprocessorFactory *PPF;
|
2008-03-18 22:21:07 +00:00
|
|
|
public:
|
2009-05-18 22:20:00 +00:00
|
|
|
HTMLPrinter(llvm::raw_ostream *OS, Diagnostic &D, Preprocessor *pp,
|
2008-04-17 22:31:54 +00:00
|
|
|
PreprocessorFactory* ppf)
|
2009-05-18 22:20:00 +00:00
|
|
|
: Out(OS), Diags(D), PP(pp), PPF(ppf) {}
|
2008-03-18 22:21:07 +00:00
|
|
|
virtual ~HTMLPrinter();
|
|
|
|
|
|
|
|
void Initialize(ASTContext &context);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2009-05-18 22:20:00 +00:00
|
|
|
ASTConsumer* clang::CreateHTMLPrinter(llvm::raw_ostream *OS,
|
2008-04-17 22:31:54 +00:00
|
|
|
Diagnostic &D, Preprocessor *PP,
|
|
|
|
PreprocessorFactory* PPF) {
|
|
|
|
|
2009-05-18 22:20:00 +00:00
|
|
|
return new HTMLPrinter(OS, D, PP, PPF);
|
2008-04-16 05:21:09 +00:00
|
|
|
}
|
2008-03-18 22:21:07 +00:00
|
|
|
|
|
|
|
void HTMLPrinter::Initialize(ASTContext &context) {
|
2009-04-14 23:22:57 +00:00
|
|
|
R.setSourceMgr(context.getSourceManager(), context.getLangOptions());
|
2008-03-18 22:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HTMLPrinter::~HTMLPrinter() {
|
2008-04-16 05:21:09 +00:00
|
|
|
if (Diags.hasErrorOccurred())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Format the file.
|
2009-01-17 06:22:33 +00:00
|
|
|
FileID FID = R.getSourceMgr().getMainFileID();
|
|
|
|
const FileEntry* Entry = R.getSourceMgr().getFileEntryForID(FID);
|
2009-05-19 05:28:52 +00: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 06:22:33 +00:00
|
|
|
html::AddLineNumbers(R, FID);
|
2009-05-19 05:28:52 +00:00
|
|
|
html::AddHeaderFooterInternalBuiltinCSS(R, FID, Name);
|
2008-04-16 05:21:09 +00:00
|
|
|
|
2008-04-16 16:39:56 +00: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-01-17 06:22:33 +00:00
|
|
|
if (PP) html::SyntaxHighlight(R, FID, *PP);
|
|
|
|
if (PPF) html::HighlightMacros(R, FID, *PP);
|
|
|
|
html::EscapeText(R, FID, false, true);
|
2009-05-18 22:20:00 +00:00
|
|
|
|
2008-03-19 07:53:42 +00:00
|
|
|
// Emit the HTML.
|
2009-01-17 06:22:33 +00:00
|
|
|
const RewriteBuffer &RewriteBuf = R.getEditBuffer(FID);
|
2008-04-16 05:21:09 +00:00
|
|
|
char *Buffer = (char*)malloc(RewriteBuf.size());
|
|
|
|
std::copy(RewriteBuf.begin(), RewriteBuf.end(), Buffer);
|
2009-05-18 22:20:00 +00:00
|
|
|
Out->write(Buffer, RewriteBuf.size());
|
2008-04-16 05:21:09 +00:00
|
|
|
free(Buffer);
|
2008-03-19 07:53:42 +00:00
|
|
|
}
|