blob: 71121095aed47e41e748fc8a47f43e86c5a9a124 [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;
Eli Friedman66d6f042009-05-18 22:20:00 +000033 llvm::raw_ostream *Out;
Chris Lattner8ac661c2008-04-16 05:21:09 +000034 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:
Eli Friedman66d6f042009-05-18 22:20:00 +000038 HTMLPrinter(llvm::raw_ostream *OS, Diagnostic &D, Preprocessor *pp,
Ted Kremenek339b9c22008-04-17 22:31:54 +000039 PreprocessorFactory* ppf)
Eli Friedman66d6f042009-05-18 22:20:00 +000040 : Out(OS), 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
Eli Friedman66d6f042009-05-18 22:20:00 +000047ASTConsumer* clang::CreateHTMLPrinter(llvm::raw_ostream *OS,
Ted Kremenek339b9c22008-04-17 22:31:54 +000048 Diagnostic &D, Preprocessor *PP,
49 PreprocessorFactory* PPF) {
50
Eli Friedman66d6f042009-05-18 22:20:00 +000051 return new HTMLPrinter(OS, D, PP, PPF);
Chris Lattner8ac661c2008-04-16 05:21:09 +000052}
Ted Kremenek5e0020e2008-03-18 22:21:07 +000053
54void HTMLPrinter::Initialize(ASTContext &context) {
Chris Lattner2c78b872009-04-14 23:22:57 +000055 R.setSourceMgr(context.getSourceManager(), context.getLangOptions());
Ted Kremenek5e0020e2008-03-18 22:21:07 +000056}
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);
Eli Friedman66d6f042009-05-18 22:20:00 +000076
Ted Kremenek5e0020e2008-03-18 22:21:07 +000077 // Emit the HTML.
Chris Lattner2b2453a2009-01-17 06:22:33 +000078 const RewriteBuffer &RewriteBuf = R.getEditBuffer(FID);
Chris Lattner8ac661c2008-04-16 05:21:09 +000079 char *Buffer = (char*)malloc(RewriteBuf.size());
80 std::copy(RewriteBuf.begin(), RewriteBuf.end(), Buffer);
Eli Friedman66d6f042009-05-18 22:20:00 +000081 Out->write(Buffer, RewriteBuf.size());
Chris Lattner8ac661c2008-04-16 05:21:09 +000082 free(Buffer);
Ted Kremenek5e0020e2008-03-18 22:21:07 +000083}