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 "ASTConsumers.h"
|
|
|
|
#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"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2008-03-19 15:53:42 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Functional HTML pretty-printing.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-03-19 06:21:07 +08:00
|
|
|
namespace {
|
|
|
|
class HTMLPrinter : public ASTConsumer {
|
|
|
|
Rewriter R;
|
2008-04-16 13:21:09 +08:00
|
|
|
std::string OutFilename;
|
|
|
|
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:
|
2008-04-18 06:31:54 +08:00
|
|
|
HTMLPrinter(const std::string &OutFile, Diagnostic &D, Preprocessor *pp,
|
|
|
|
PreprocessorFactory* ppf)
|
|
|
|
: OutFilename(OutFile), Diags(D), PP(pp), PPF(ppf) {}
|
2008-03-19 06:21:07 +08:00
|
|
|
virtual ~HTMLPrinter();
|
|
|
|
|
|
|
|
void Initialize(ASTContext &context);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2008-04-16 13:21:09 +08:00
|
|
|
ASTConsumer* clang::CreateHTMLPrinter(const std::string &OutFile,
|
2008-04-18 06:31:54 +08:00
|
|
|
Diagnostic &D, Preprocessor *PP,
|
|
|
|
PreprocessorFactory* PPF) {
|
|
|
|
|
|
|
|
return new HTMLPrinter(OutFile, D, PP, PPF);
|
2008-04-16 13:21:09 +08:00
|
|
|
}
|
2008-03-19 06:21:07 +08:00
|
|
|
|
|
|
|
void HTMLPrinter::Initialize(ASTContext &context) {
|
|
|
|
R.setSourceMgr(context.getSourceManager());
|
|
|
|
}
|
|
|
|
|
|
|
|
HTMLPrinter::~HTMLPrinter() {
|
2008-04-16 13:21:09 +08:00
|
|
|
if (Diags.hasErrorOccurred())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Format the file.
|
2008-03-19 06:21:07 +08:00
|
|
|
unsigned FileID = R.getSourceMgr().getMainFileID();
|
2008-07-08 02:31:05 +08:00
|
|
|
const FileEntry* Entry = R.getSourceMgr().getFileEntryForID(FileID);
|
|
|
|
|
2008-03-19 15:53:42 +08:00
|
|
|
html::AddLineNumbers(R, FileID);
|
2008-07-08 02:31:05 +08:00
|
|
|
html::AddHeaderFooterInternalBuiltinCSS(R, FileID, Entry->getName());
|
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.
|
|
|
|
|
2008-04-18 06:31:54 +08:00
|
|
|
if (PP) html::SyntaxHighlight(R, FileID, *PP);
|
2008-04-20 07:09:31 +08:00
|
|
|
if (PPF) html::HighlightMacros(R, FileID, *PP);
|
2008-04-18 06:24:51 +08:00
|
|
|
html::EscapeText(R, FileID, false, true);
|
2008-04-16 14:11:58 +08:00
|
|
|
|
2008-04-16 13:21:09 +08: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-19 06:21:07 +08:00
|
|
|
|
2008-03-19 15:53:42 +08:00
|
|
|
// Emit the HTML.
|
2008-04-16 13:21:09 +08: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 15:53:42 +08:00
|
|
|
|
2008-04-16 13:21:09 +08:00
|
|
|
if (OutputFILE != stdout) fclose(OutputFILE);
|
2008-03-19 15:53:42 +08:00
|
|
|
}
|