blob: 22ccfe6936b7ad6a8fa9d4657c676708b91cfe53 [file] [log] [blame]
Ted Kremenek088d12e2008-04-16 04:38:45 +00001//===--- HTMLPrint.cpp - Source code -> HTML pretty-printing --------------===//
Ted Kremenekb0c409a2008-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 Kremenek088d12e2008-04-16 04:38:45 +000010// Pretty-printing of source code to HTML.
Ted Kremenekb0c409a2008-03-18 22:21:07 +000011//
12//===----------------------------------------------------------------------===//
13
Ted Kremenekcdf81492012-09-01 05:09:24 +000014#include "clang/Rewrite/Frontend/ASTConsumers.h"
Ted Kremenekb0c409a2008-03-18 22:21:07 +000015#include "clang/AST/ASTConsumer.h"
Ted Kremenekb0c409a2008-03-18 22:21:07 +000016#include "clang/AST/ASTContext.h"
Daniel Dunbard0ba0e62009-11-04 23:56:25 +000017#include "clang/AST/Decl.h"
18#include "clang/Basic/Diagnostic.h"
19#include "clang/Basic/FileManager.h"
20#include "clang/Basic/SourceManager.h"
21#include "clang/Lex/Preprocessor.h"
Ted Kremenekcdf81492012-09-01 05:09:24 +000022#include "clang/Rewrite/Core/HTMLRewrite.h"
23#include "clang/Rewrite/Core/Rewriter.h"
Eli Friedman69329a52009-05-19 05:28:52 +000024#include "llvm/Support/MemoryBuffer.h"
Chris Lattner72bb4f62009-08-24 04:11:30 +000025#include "llvm/Support/raw_ostream.h"
Ted Kremenekb0c409a2008-03-18 22:21:07 +000026using namespace clang;
27
Ted Kremenek216624c2008-03-19 07:53:42 +000028//===----------------------------------------------------------------------===//
29// Functional HTML pretty-printing.
Mike Stump11289f42009-09-09 15:08:12 +000030//===----------------------------------------------------------------------===//
Ted Kremenek216624c2008-03-19 07:53:42 +000031
Ted Kremenekb0c409a2008-03-18 22:21:07 +000032namespace {
33 class HTMLPrinter : public ASTConsumer {
34 Rewriter R;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000035 raw_ostream *Out;
Daniel Dunbard0ba0e62009-11-04 23:56:25 +000036 Preprocessor &PP;
37 bool SyntaxHighlight, HighlightMacros;
38
Ted Kremenekb0c409a2008-03-18 22:21:07 +000039 public:
Chris Lattner0e62c1c2011-07-23 10:55:15 +000040 HTMLPrinter(raw_ostream *OS, Preprocessor &pp,
Daniel Dunbard0ba0e62009-11-04 23:56:25 +000041 bool _SyntaxHighlight, bool _HighlightMacros)
42 : Out(OS), PP(pp), SyntaxHighlight(_SyntaxHighlight),
43 HighlightMacros(_HighlightMacros) {}
Mike Stump11289f42009-09-09 15:08:12 +000044
Craig Topperfb6b25b2014-03-15 04:29:04 +000045 void Initialize(ASTContext &context) override;
46 void HandleTranslationUnit(ASTContext &Ctx) override;
Ted Kremenekb0c409a2008-03-18 22:21:07 +000047 };
48}
49
David Blaikie6beb6aa2014-08-10 19:56:51 +000050std::unique_ptr<ASTConsumer> clang::CreateHTMLPrinter(raw_ostream *OS,
51 Preprocessor &PP,
52 bool SyntaxHighlight,
53 bool HighlightMacros) {
54 return llvm::make_unique<HTMLPrinter>(OS, PP, SyntaxHighlight,
55 HighlightMacros);
Chris Lattnerdffe0f72008-04-16 05:21:09 +000056}
Ted Kremenekb0c409a2008-03-18 22:21:07 +000057
58void HTMLPrinter::Initialize(ASTContext &context) {
David Blaikiebbafb8a2012-03-11 07:00:24 +000059 R.setSourceMgr(context.getSourceManager(), context.getLangOpts());
Ted Kremenekb0c409a2008-03-18 22:21:07 +000060}
61
Eli Friedman3950e8c2009-12-12 03:36:52 +000062void HTMLPrinter::HandleTranslationUnit(ASTContext &Ctx) {
Daniel Dunbard0ba0e62009-11-04 23:56:25 +000063 if (PP.getDiagnostics().hasErrorOccurred())
Chris Lattnerdffe0f72008-04-16 05:21:09 +000064 return;
65
66 // Format the file.
Chris Lattnerd32480d2009-01-17 06:22:33 +000067 FileID FID = R.getSourceMgr().getMainFileID();
68 const FileEntry* Entry = R.getSourceMgr().getFileEntryForID(FID);
Eli Friedman69329a52009-05-19 05:28:52 +000069 const char* Name;
70 // In some cases, in particular the case where the input is from stdin,
71 // there is no entry. Fall back to the memory buffer for a name in those
72 // cases.
73 if (Entry)
74 Name = Entry->getName();
75 else
76 Name = R.getSourceMgr().getBuffer(FID)->getBufferIdentifier();
77
Chris Lattnerd32480d2009-01-17 06:22:33 +000078 html::AddLineNumbers(R, FID);
Eli Friedman69329a52009-05-19 05:28:52 +000079 html::AddHeaderFooterInternalBuiltinCSS(R, FID, Name);
Chris Lattnerdffe0f72008-04-16 05:21:09 +000080
Ted Kremenekf2e6fcf2008-04-16 16:39:56 +000081 // If we have a preprocessor, relex the file and syntax highlight.
82 // We might not have a preprocessor if we come from a deserialized AST file,
83 // for example.
Mike Stump11289f42009-09-09 15:08:12 +000084
Daniel Dunbard0ba0e62009-11-04 23:56:25 +000085 if (SyntaxHighlight) html::SyntaxHighlight(R, FID, PP);
86 if (HighlightMacros) html::HighlightMacros(R, FID, PP);
Chris Lattnerd32480d2009-01-17 06:22:33 +000087 html::EscapeText(R, FID, false, true);
Eli Friedman94cf21e2009-05-18 22:20:00 +000088
Ted Kremenekb0c409a2008-03-18 22:21:07 +000089 // Emit the HTML.
Chris Lattnerd32480d2009-01-17 06:22:33 +000090 const RewriteBuffer &RewriteBuf = R.getEditBuffer(FID);
Chris Lattnerdffe0f72008-04-16 05:21:09 +000091 char *Buffer = (char*)malloc(RewriteBuf.size());
92 std::copy(RewriteBuf.begin(), RewriteBuf.end(), Buffer);
Eli Friedman94cf21e2009-05-18 22:20:00 +000093 Out->write(Buffer, RewriteBuf.size());
Chris Lattnerdffe0f72008-04-16 05:21:09 +000094 free(Buffer);
Ted Kremenekb0c409a2008-03-18 22:21:07 +000095}