blob: ff04b0e609773243feec269b82494cf08c908a66 [file] [log] [blame]
Ted Kremenek33245d32008-04-16 04:38:45 +00001//===--- HTMLPrint.cpp - Source code -> HTML pretty-printing --------------===//
Ted Kremenekd1cf5592008-03-18 22:21:07 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Ted Kremenek33245d32008-04-16 04:38:45 +000010// Pretty-printing of source code to HTML.
Ted Kremenekd1cf5592008-03-18 22:21:07 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "ASTConsumers.h"
15#include "clang/AST/ASTConsumer.h"
16#include "clang/Rewrite/Rewriter.h"
17#include "clang/Rewrite/HTMLRewrite.h"
Chris Lattner5fdcc302008-04-16 05:21:09 +000018#include "clang/Basic/Diagnostic.h"
Ted Kremenekd1cf5592008-03-18 22:21:07 +000019#include "clang/Basic/SourceManager.h"
Ted Kremenekd1cf5592008-03-18 22:21:07 +000020#include "clang/AST/ASTContext.h"
21
22using namespace clang;
23
Ted Kremeneke1a79d82008-03-19 07:53:42 +000024//===----------------------------------------------------------------------===//
25// Functional HTML pretty-printing.
26//===----------------------------------------------------------------------===//
27
Ted Kremenekd1cf5592008-03-18 22:21:07 +000028namespace {
29 class HTMLPrinter : public ASTConsumer {
30 Rewriter R;
Chris Lattner5fdcc302008-04-16 05:21:09 +000031 std::string OutFilename;
32 Diagnostic &Diags;
Chris Lattner21f72d62008-04-16 06:11:58 +000033 Preprocessor *PP;
Ted Kremenekd1cf5592008-03-18 22:21:07 +000034 public:
Chris Lattner21f72d62008-04-16 06:11:58 +000035 HTMLPrinter(const std::string &OutFile, Diagnostic &D, Preprocessor *pp)
36 : OutFilename(OutFile), Diags(D), PP(pp) {}
Ted Kremenekd1cf5592008-03-18 22:21:07 +000037 virtual ~HTMLPrinter();
38
39 void Initialize(ASTContext &context);
40 };
41}
42
Chris Lattner5fdcc302008-04-16 05:21:09 +000043ASTConsumer* clang::CreateHTMLPrinter(const std::string &OutFile,
Chris Lattner21f72d62008-04-16 06:11:58 +000044 Diagnostic &D, Preprocessor *PP) {
45 return new HTMLPrinter(OutFile, D, PP);
Chris Lattner5fdcc302008-04-16 05:21:09 +000046}
Ted Kremenekd1cf5592008-03-18 22:21:07 +000047
48void HTMLPrinter::Initialize(ASTContext &context) {
49 R.setSourceMgr(context.getSourceManager());
50}
51
52HTMLPrinter::~HTMLPrinter() {
Chris Lattner5fdcc302008-04-16 05:21:09 +000053 if (Diags.hasErrorOccurred())
54 return;
55
56 // Format the file.
Ted Kremeneke1a79d82008-03-19 07:53:42 +000057 unsigned FileID = R.getSourceMgr().getMainFileID();
Ted Kremenekd1cf5592008-03-18 22:21:07 +000058 html::AddLineNumbers(R, FileID);
Ted Kremenek0735c172008-03-19 06:14:37 +000059 html::AddHeaderFooterInternalBuiltinCSS(R, FileID);
Chris Lattner5fdcc302008-04-16 05:21:09 +000060
Ted Kremenekfe82a542008-04-16 16:39:56 +000061 // If we have a preprocessor, relex the file and syntax highlight.
62 // We might not have a preprocessor if we come from a deserialized AST file,
63 // for example.
64
Chris Lattner9de65d02008-04-16 06:32:08 +000065 if (PP) {
Chris Lattner21f72d62008-04-16 06:11:58 +000066 html::SyntaxHighlight(R, FileID, *PP);
Chris Lattner9de65d02008-04-16 06:32:08 +000067 html::HighlightMacros(R, FileID, *PP);
68 }
Chris Lattner31001ed2008-04-17 22:24:51 +000069 html::EscapeText(R, FileID, false, true);
Chris Lattner21f72d62008-04-16 06:11:58 +000070
71
Chris Lattner5fdcc302008-04-16 05:21:09 +000072 // Open the output.
73 FILE *OutputFILE;
74 if (OutFilename.empty() || OutFilename == "-")
75 OutputFILE = stdout;
76 else {
77 OutputFILE = fopen(OutFilename.c_str(), "w+");
78 if (OutputFILE == 0) {
79 fprintf(stderr, "Error opening output file '%s'.\n", OutFilename.c_str());
80 exit(1);
81 }
82 }
Ted Kremenekd1cf5592008-03-18 22:21:07 +000083
84 // Emit the HTML.
Chris Lattner5fdcc302008-04-16 05:21:09 +000085 const RewriteBuffer &RewriteBuf = R.getEditBuffer(FileID);
86 char *Buffer = (char*)malloc(RewriteBuf.size());
87 std::copy(RewriteBuf.begin(), RewriteBuf.end(), Buffer);
88 fwrite(Buffer, 1, RewriteBuf.size(), OutputFILE);
89 free(Buffer);
Ted Kremenekd1cf5592008-03-18 22:21:07 +000090
Chris Lattner5fdcc302008-04-16 05:21:09 +000091 if (OutputFILE != stdout) fclose(OutputFILE);
Ted Kremenekd1cf5592008-03-18 22:21:07 +000092}