blob: b06d52df86ea259d2cdfcc74951d66aa9814fb41 [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 Kremenekf6f593f2008-07-07 18:31:05 +000020#include "clang/Basic/FileManager.h"
Ted Kremenek5e0020e2008-03-18 22:21:07 +000021#include "clang/AST/ASTContext.h"
22
23using namespace clang;
24
Ted Kremenek13e479b2008-03-19 07:53:42 +000025//===----------------------------------------------------------------------===//
26// Functional HTML pretty-printing.
27//===----------------------------------------------------------------------===//
28
Ted Kremenek5e0020e2008-03-18 22:21:07 +000029namespace {
30 class HTMLPrinter : public ASTConsumer {
31 Rewriter R;
Chris Lattner8ac661c2008-04-16 05:21:09 +000032 std::string OutFilename;
33 Diagnostic &Diags;
Chris Lattner3245a0a2008-04-16 06:11:58 +000034 Preprocessor *PP;
Ted Kremenek339b9c22008-04-17 22:31:54 +000035 PreprocessorFactory *PPF;
Ted Kremenek5e0020e2008-03-18 22:21:07 +000036 public:
Ted Kremenek339b9c22008-04-17 22:31:54 +000037 HTMLPrinter(const std::string &OutFile, Diagnostic &D, Preprocessor *pp,
38 PreprocessorFactory* ppf)
39 : OutFilename(OutFile), Diags(D), PP(pp), PPF(ppf) {}
Ted Kremenek5e0020e2008-03-18 22:21:07 +000040 virtual ~HTMLPrinter();
41
42 void Initialize(ASTContext &context);
43 };
44}
45
Chris Lattner8ac661c2008-04-16 05:21:09 +000046ASTConsumer* clang::CreateHTMLPrinter(const std::string &OutFile,
Ted Kremenek339b9c22008-04-17 22:31:54 +000047 Diagnostic &D, Preprocessor *PP,
48 PreprocessorFactory* PPF) {
49
50 return new HTMLPrinter(OutFile, D, PP, PPF);
Chris Lattner8ac661c2008-04-16 05:21:09 +000051}
Ted Kremenek5e0020e2008-03-18 22:21:07 +000052
53void HTMLPrinter::Initialize(ASTContext &context) {
54 R.setSourceMgr(context.getSourceManager());
55}
56
57HTMLPrinter::~HTMLPrinter() {
Chris Lattner8ac661c2008-04-16 05:21:09 +000058 if (Diags.hasErrorOccurred())
59 return;
60
61 // Format the file.
Ted Kremenek13e479b2008-03-19 07:53:42 +000062 unsigned FileID = R.getSourceMgr().getMainFileID();
Ted Kremenekf6f593f2008-07-07 18:31:05 +000063 const FileEntry* Entry = R.getSourceMgr().getFileEntryForID(FileID);
64
Ted Kremenek5e0020e2008-03-18 22:21:07 +000065 html::AddLineNumbers(R, FileID);
Ted Kremenekf6f593f2008-07-07 18:31:05 +000066 html::AddHeaderFooterInternalBuiltinCSS(R, FileID, Entry->getName());
Chris Lattner8ac661c2008-04-16 05:21:09 +000067
Ted Kremenek47abe762008-04-16 16:39:56 +000068 // If we have a preprocessor, relex the file and syntax highlight.
69 // We might not have a preprocessor if we come from a deserialized AST file,
70 // for example.
71
Ted Kremenek339b9c22008-04-17 22:31:54 +000072 if (PP) html::SyntaxHighlight(R, FileID, *PP);
Chris Lattneraa391972008-04-19 23:09:31 +000073 if (PPF) html::HighlightMacros(R, FileID, *PP);
Chris Lattner1ee8d6f2008-04-17 22:24:51 +000074 html::EscapeText(R, FileID, false, true);
Chris Lattner3245a0a2008-04-16 06:11:58 +000075
Chris Lattner8ac661c2008-04-16 05:21:09 +000076 // Open the output.
77 FILE *OutputFILE;
78 if (OutFilename.empty() || OutFilename == "-")
79 OutputFILE = stdout;
80 else {
81 OutputFILE = fopen(OutFilename.c_str(), "w+");
82 if (OutputFILE == 0) {
83 fprintf(stderr, "Error opening output file '%s'.\n", OutFilename.c_str());
84 exit(1);
85 }
86 }
Ted Kremenek5e0020e2008-03-18 22:21:07 +000087
88 // Emit the HTML.
Chris Lattner8ac661c2008-04-16 05:21:09 +000089 const RewriteBuffer &RewriteBuf = R.getEditBuffer(FileID);
90 char *Buffer = (char*)malloc(RewriteBuf.size());
91 std::copy(RewriteBuf.begin(), RewriteBuf.end(), Buffer);
92 fwrite(Buffer, 1, RewriteBuf.size(), OutputFILE);
93 free(Buffer);
Ted Kremenek5e0020e2008-03-18 22:21:07 +000094
Chris Lattner8ac661c2008-04-16 05:21:09 +000095 if (OutputFILE != stdout) fclose(OutputFILE);
Ted Kremenek5e0020e2008-03-18 22:21:07 +000096}