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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ASTConsumers.h"
|
|
|
|
#include "clang/AST/ASTConsumer.h"
|
|
|
|
#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"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
|
|
|
|
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;
|
2008-04-16 05:21:09 +00:00
|
|
|
std::string OutFilename;
|
|
|
|
Diagnostic &Diags;
|
2008-04-16 06:11:58 +00:00
|
|
|
Preprocessor *PP;
|
2008-03-18 22:21:07 +00:00
|
|
|
public:
|
2008-04-16 06:11:58 +00:00
|
|
|
HTMLPrinter(const std::string &OutFile, Diagnostic &D, Preprocessor *pp)
|
|
|
|
: OutFilename(OutFile), Diags(D), PP(pp) {}
|
2008-03-18 22:21:07 +00:00
|
|
|
virtual ~HTMLPrinter();
|
|
|
|
|
|
|
|
void Initialize(ASTContext &context);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2008-04-16 05:21:09 +00:00
|
|
|
ASTConsumer* clang::CreateHTMLPrinter(const std::string &OutFile,
|
2008-04-16 06:11:58 +00:00
|
|
|
Diagnostic &D, Preprocessor *PP) {
|
|
|
|
return new HTMLPrinter(OutFile, D, PP);
|
2008-04-16 05:21:09 +00:00
|
|
|
}
|
2008-03-18 22:21:07 +00:00
|
|
|
|
|
|
|
void HTMLPrinter::Initialize(ASTContext &context) {
|
|
|
|
R.setSourceMgr(context.getSourceManager());
|
|
|
|
}
|
|
|
|
|
|
|
|
HTMLPrinter::~HTMLPrinter() {
|
2008-04-16 05:21:09 +00:00
|
|
|
if (Diags.hasErrorOccurred())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Format the file.
|
2008-03-18 22:21:07 +00:00
|
|
|
unsigned FileID = R.getSourceMgr().getMainFileID();
|
2008-04-08 23:25:54 +00:00
|
|
|
html::EscapeText(R, FileID, false, true);
|
2008-03-19 07:53:42 +00:00
|
|
|
html::AddLineNumbers(R, FileID);
|
|
|
|
html::AddHeaderFooterInternalBuiltinCSS(R, FileID);
|
2008-04-16 05:21:09 +00:00
|
|
|
|
2008-04-16 06:11:58 +00:00
|
|
|
// If we have a preprocessor, relex the file and syntax hilight. We might not
|
|
|
|
// have a preprocessor if we come from a deserialized AST file, for example.
|
2008-04-16 06:32:08 +00:00
|
|
|
if (PP) {
|
2008-04-16 06:11:58 +00:00
|
|
|
html::SyntaxHighlight(R, FileID, *PP);
|
2008-04-16 06:32:08 +00:00
|
|
|
html::HighlightMacros(R, FileID, *PP);
|
|
|
|
}
|
2008-04-16 06:11:58 +00:00
|
|
|
|
|
|
|
|
2008-04-16 05:21:09 +00:00
|
|
|
// Open the output.
|
|
|
|
FILE *OutputFILE;
|
|
|
|
if (OutFilename.empty() || OutFilename == "-")
|
|
|
|
OutputFILE = stdout;
|
|
|
|
else {
|
|
|
|
OutputFILE = fopen(OutFilename.c_str(), "w+");
|
|
|
|
if (OutputFILE == 0) {
|
|
|
|
fprintf(stderr, "Error opening output file '%s'.\n", OutFilename.c_str());
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2008-03-18 22:21:07 +00:00
|
|
|
|
2008-03-19 07:53:42 +00:00
|
|
|
// Emit the HTML.
|
2008-04-16 05:21:09 +00:00
|
|
|
const RewriteBuffer &RewriteBuf = R.getEditBuffer(FileID);
|
|
|
|
char *Buffer = (char*)malloc(RewriteBuf.size());
|
|
|
|
std::copy(RewriteBuf.begin(), RewriteBuf.end(), Buffer);
|
|
|
|
fwrite(Buffer, 1, RewriteBuf.size(), OutputFILE);
|
|
|
|
free(Buffer);
|
2008-03-19 07:53:42 +00:00
|
|
|
|
2008-04-16 05:21:09 +00:00
|
|
|
if (OutputFILE != stdout) fclose(OutputFILE);
|
2008-03-19 07:53:42 +00:00
|
|
|
}
|