blob: e6e6ab7edf764bf6f1e6ea845803c6820603f326 [file] [log] [blame]
Ted Kremenekfdbe6792008-04-16 04:38:45 +00001//===--- HTMLPrint.cpp - Source code -> HTML pretty-printing --------------===//
Ted Kremenek5e0020e2008-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 Kremenekfdbe6792008-04-16 04:38:45 +000010// Pretty-printing of source code to HTML.
Ted Kremenek5e0020e2008-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 Lattner8ac661c2008-04-16 05:21:09 +000018#include "clang/Basic/Diagnostic.h"
Ted Kremenek5e0020e2008-03-18 22:21:07 +000019#include "clang/Basic/SourceManager.h"
Ted Kremenek5e0020e2008-03-18 22:21:07 +000020#include "clang/AST/ASTContext.h"
21
22using namespace clang;
23
Ted Kremenek13e479b2008-03-19 07:53:42 +000024//===----------------------------------------------------------------------===//
25// Functional HTML pretty-printing.
26//===----------------------------------------------------------------------===//
27
Ted Kremenek5e0020e2008-03-18 22:21:07 +000028namespace {
29 class HTMLPrinter : public ASTConsumer {
30 Rewriter R;
Chris Lattner8ac661c2008-04-16 05:21:09 +000031 std::string OutFilename;
32 Diagnostic &Diags;
Chris Lattner3245a0a2008-04-16 06:11:58 +000033 Preprocessor *PP;
Ted Kremenek339b9c22008-04-17 22:31:54 +000034 PreprocessorFactory *PPF;
Ted Kremenek5e0020e2008-03-18 22:21:07 +000035 public:
Ted Kremenek339b9c22008-04-17 22:31:54 +000036 HTMLPrinter(const std::string &OutFile, Diagnostic &D, Preprocessor *pp,
37 PreprocessorFactory* ppf)
38 : OutFilename(OutFile), Diags(D), PP(pp), PPF(ppf) {}
Ted Kremenek5e0020e2008-03-18 22:21:07 +000039 virtual ~HTMLPrinter();
40
41 void Initialize(ASTContext &context);
42 };
43}
44
Chris Lattner8ac661c2008-04-16 05:21:09 +000045ASTConsumer* clang::CreateHTMLPrinter(const std::string &OutFile,
Ted Kremenek339b9c22008-04-17 22:31:54 +000046 Diagnostic &D, Preprocessor *PP,
47 PreprocessorFactory* PPF) {
48
49 return new HTMLPrinter(OutFile, D, PP, PPF);
Chris Lattner8ac661c2008-04-16 05:21:09 +000050}
Ted Kremenek5e0020e2008-03-18 22:21:07 +000051
52void HTMLPrinter::Initialize(ASTContext &context) {
53 R.setSourceMgr(context.getSourceManager());
54}
55
56HTMLPrinter::~HTMLPrinter() {
Chris Lattner8ac661c2008-04-16 05:21:09 +000057 if (Diags.hasErrorOccurred())
58 return;
59
60 // Format the file.
Ted Kremenek13e479b2008-03-19 07:53:42 +000061 unsigned FileID = R.getSourceMgr().getMainFileID();
Ted Kremenek5e0020e2008-03-18 22:21:07 +000062 html::AddLineNumbers(R, FileID);
Ted Kremenekad0a2032008-03-19 06:14:37 +000063 html::AddHeaderFooterInternalBuiltinCSS(R, FileID);
Chris Lattner8ac661c2008-04-16 05:21:09 +000064
Ted Kremenek47abe762008-04-16 16:39:56 +000065 // If we have a preprocessor, relex the file and syntax highlight.
66 // We might not have a preprocessor if we come from a deserialized AST file,
67 // for example.
68
Ted Kremenek339b9c22008-04-17 22:31:54 +000069 if (PP) html::SyntaxHighlight(R, FileID, *PP);
Chris Lattneraa391972008-04-19 23:09:31 +000070 if (PPF) html::HighlightMacros(R, FileID, *PP);
Chris Lattner1ee8d6f2008-04-17 22:24:51 +000071 html::EscapeText(R, FileID, false, true);
Chris Lattner3245a0a2008-04-16 06:11:58 +000072
Chris Lattner8ac661c2008-04-16 05:21:09 +000073 // Open the output.
74 FILE *OutputFILE;
75 if (OutFilename.empty() || OutFilename == "-")
76 OutputFILE = stdout;
77 else {
78 OutputFILE = fopen(OutFilename.c_str(), "w+");
79 if (OutputFILE == 0) {
80 fprintf(stderr, "Error opening output file '%s'.\n", OutFilename.c_str());
81 exit(1);
82 }
83 }
Ted Kremenek5e0020e2008-03-18 22:21:07 +000084
85 // Emit the HTML.
Chris Lattner8ac661c2008-04-16 05:21:09 +000086 const RewriteBuffer &RewriteBuf = R.getEditBuffer(FileID);
87 char *Buffer = (char*)malloc(RewriteBuf.size());
88 std::copy(RewriteBuf.begin(), RewriteBuf.end(), Buffer);
89 fwrite(Buffer, 1, RewriteBuf.size(), OutputFILE);
90 free(Buffer);
Ted Kremenek5e0020e2008-03-18 22:21:07 +000091
Chris Lattner8ac661c2008-04-16 05:21:09 +000092 if (OutputFILE != stdout) fclose(OutputFILE);
Ted Kremenek5e0020e2008-03-18 22:21:07 +000093}