blob: 79e44470ada56551e74a24bf0539f7eaf7052a93 [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
Ted Kremenekb0c409a2008-03-18 22:21:07 +000045 void Initialize(ASTContext &context);
Eli Friedman3950e8c2009-12-12 03:36:52 +000046 void HandleTranslationUnit(ASTContext &Ctx);
Ted Kremenekb0c409a2008-03-18 22:21:07 +000047 };
48}
49
Chris Lattner0e62c1c2011-07-23 10:55:15 +000050ASTConsumer* clang::CreateHTMLPrinter(raw_ostream *OS,
Daniel Dunbard0ba0e62009-11-04 23:56:25 +000051 Preprocessor &PP,
52 bool SyntaxHighlight,
53 bool HighlightMacros) {
54 return new HTMLPrinter(OS, PP, SyntaxHighlight, HighlightMacros);
Chris Lattnerdffe0f72008-04-16 05:21:09 +000055}
Ted Kremenekb0c409a2008-03-18 22:21:07 +000056
57void HTMLPrinter::Initialize(ASTContext &context) {
David Blaikiebbafb8a2012-03-11 07:00:24 +000058 R.setSourceMgr(context.getSourceManager(), context.getLangOpts());
Ted Kremenekb0c409a2008-03-18 22:21:07 +000059}
60
Eli Friedman3950e8c2009-12-12 03:36:52 +000061void HTMLPrinter::HandleTranslationUnit(ASTContext &Ctx) {
Daniel Dunbard0ba0e62009-11-04 23:56:25 +000062 if (PP.getDiagnostics().hasErrorOccurred())
Chris Lattnerdffe0f72008-04-16 05:21:09 +000063 return;
64
65 // Format the file.
Chris Lattnerd32480d2009-01-17 06:22:33 +000066 FileID FID = R.getSourceMgr().getMainFileID();
67 const FileEntry* Entry = R.getSourceMgr().getFileEntryForID(FID);
Eli Friedman69329a52009-05-19 05:28:52 +000068 const char* Name;
69 // In some cases, in particular the case where the input is from stdin,
70 // there is no entry. Fall back to the memory buffer for a name in those
71 // cases.
72 if (Entry)
73 Name = Entry->getName();
74 else
75 Name = R.getSourceMgr().getBuffer(FID)->getBufferIdentifier();
76
Chris Lattnerd32480d2009-01-17 06:22:33 +000077 html::AddLineNumbers(R, FID);
Eli Friedman69329a52009-05-19 05:28:52 +000078 html::AddHeaderFooterInternalBuiltinCSS(R, FID, Name);
Chris Lattnerdffe0f72008-04-16 05:21:09 +000079
Ted Kremenekf2e6fcf2008-04-16 16:39:56 +000080 // If we have a preprocessor, relex the file and syntax highlight.
81 // We might not have a preprocessor if we come from a deserialized AST file,
82 // for example.
Mike Stump11289f42009-09-09 15:08:12 +000083
Daniel Dunbard0ba0e62009-11-04 23:56:25 +000084 if (SyntaxHighlight) html::SyntaxHighlight(R, FID, PP);
85 if (HighlightMacros) html::HighlightMacros(R, FID, PP);
Chris Lattnerd32480d2009-01-17 06:22:33 +000086 html::EscapeText(R, FID, false, true);
Eli Friedman94cf21e2009-05-18 22:20:00 +000087
Ted Kremenekb0c409a2008-03-18 22:21:07 +000088 // Emit the HTML.
Chris Lattnerd32480d2009-01-17 06:22:33 +000089 const RewriteBuffer &RewriteBuf = R.getEditBuffer(FID);
Chris Lattnerdffe0f72008-04-16 05:21:09 +000090 char *Buffer = (char*)malloc(RewriteBuf.size());
91 std::copy(RewriteBuf.begin(), RewriteBuf.end(), Buffer);
Eli Friedman94cf21e2009-05-18 22:20:00 +000092 Out->write(Buffer, RewriteBuf.size());
Chris Lattnerdffe0f72008-04-16 05:21:09 +000093 free(Buffer);
Ted Kremenekb0c409a2008-03-18 22:21:07 +000094}