blob: b2bb29c9eb31ef32661596e0ca60db512e46abfc [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"
Daniel Dunbare91593e2008-08-11 04:54:23 +000016#include "clang/AST/Decl.h"
Ted Kremenek5e0020e2008-03-18 22:21:07 +000017#include "clang/Rewrite/Rewriter.h"
18#include "clang/Rewrite/HTMLRewrite.h"
Chris Lattner8ac661c2008-04-16 05:21:09 +000019#include "clang/Basic/Diagnostic.h"
Ted Kremenek5e0020e2008-03-18 22:21:07 +000020#include "clang/Basic/SourceManager.h"
Ted Kremenekf6f593f2008-07-07 18:31:05 +000021#include "clang/Basic/FileManager.h"
Ted Kremenek5e0020e2008-03-18 22:21:07 +000022#include "clang/AST/ASTContext.h"
23
24using namespace clang;
25
Ted Kremenek13e479b2008-03-19 07:53:42 +000026//===----------------------------------------------------------------------===//
27// Functional HTML pretty-printing.
28//===----------------------------------------------------------------------===//
29
Ted Kremenek5e0020e2008-03-18 22:21:07 +000030namespace {
31 class HTMLPrinter : public ASTConsumer {
32 Rewriter R;
Chris Lattner8ac661c2008-04-16 05:21:09 +000033 std::string OutFilename;
34 Diagnostic &Diags;
Chris Lattner3245a0a2008-04-16 06:11:58 +000035 Preprocessor *PP;
Ted Kremenek339b9c22008-04-17 22:31:54 +000036 PreprocessorFactory *PPF;
Ted Kremenek5e0020e2008-03-18 22:21:07 +000037 public:
Ted Kremenek339b9c22008-04-17 22:31:54 +000038 HTMLPrinter(const std::string &OutFile, Diagnostic &D, Preprocessor *pp,
39 PreprocessorFactory* ppf)
40 : OutFilename(OutFile), Diags(D), PP(pp), PPF(ppf) {}
Ted Kremenek5e0020e2008-03-18 22:21:07 +000041 virtual ~HTMLPrinter();
42
43 void Initialize(ASTContext &context);
44 };
45}
46
Chris Lattner8ac661c2008-04-16 05:21:09 +000047ASTConsumer* clang::CreateHTMLPrinter(const std::string &OutFile,
Ted Kremenek339b9c22008-04-17 22:31:54 +000048 Diagnostic &D, Preprocessor *PP,
49 PreprocessorFactory* PPF) {
50
51 return new HTMLPrinter(OutFile, D, PP, PPF);
Chris Lattner8ac661c2008-04-16 05:21:09 +000052}
Ted Kremenek5e0020e2008-03-18 22:21:07 +000053
54void HTMLPrinter::Initialize(ASTContext &context) {
55 R.setSourceMgr(context.getSourceManager());
56}
57
58HTMLPrinter::~HTMLPrinter() {
Chris Lattner8ac661c2008-04-16 05:21:09 +000059 if (Diags.hasErrorOccurred())
60 return;
61
62 // Format the file.
Chris Lattner2b2453a2009-01-17 06:22:33 +000063 FileID FID = R.getSourceMgr().getMainFileID();
64 const FileEntry* Entry = R.getSourceMgr().getFileEntryForID(FID);
Ted Kremenekf6f593f2008-07-07 18:31:05 +000065
Chris Lattner2b2453a2009-01-17 06:22:33 +000066 html::AddLineNumbers(R, FID);
67 html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName());
Chris Lattner8ac661c2008-04-16 05:21:09 +000068
Ted Kremenek47abe762008-04-16 16:39:56 +000069 // If we have a preprocessor, relex the file and syntax highlight.
70 // We might not have a preprocessor if we come from a deserialized AST file,
71 // for example.
72
Chris Lattner2b2453a2009-01-17 06:22:33 +000073 if (PP) html::SyntaxHighlight(R, FID, *PP);
74 if (PPF) html::HighlightMacros(R, FID, *PP);
75 html::EscapeText(R, FID, false, true);
Chris Lattner3245a0a2008-04-16 06:11:58 +000076
Chris Lattner8ac661c2008-04-16 05:21:09 +000077 // Open the output.
78 FILE *OutputFILE;
79 if (OutFilename.empty() || OutFilename == "-")
80 OutputFILE = stdout;
81 else {
82 OutputFILE = fopen(OutFilename.c_str(), "w+");
83 if (OutputFILE == 0) {
84 fprintf(stderr, "Error opening output file '%s'.\n", OutFilename.c_str());
85 exit(1);
86 }
87 }
Ted Kremenek5e0020e2008-03-18 22:21:07 +000088
89 // Emit the HTML.
Chris Lattner2b2453a2009-01-17 06:22:33 +000090 const RewriteBuffer &RewriteBuf = R.getEditBuffer(FID);
Chris Lattner8ac661c2008-04-16 05:21:09 +000091 char *Buffer = (char*)malloc(RewriteBuf.size());
92 std::copy(RewriteBuf.begin(), RewriteBuf.end(), Buffer);
93 fwrite(Buffer, 1, RewriteBuf.size(), OutputFILE);
94 free(Buffer);
Ted Kremenek5e0020e2008-03-18 22:21:07 +000095
Chris Lattner8ac661c2008-04-16 05:21:09 +000096 if (OutputFILE != stdout) fclose(OutputFILE);
Ted Kremenek5e0020e2008-03-18 22:21:07 +000097}