blob: f66bfcb2dfc4e374dc70a031b71848af56afbe58 [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
Daniel Dunbar9b414d32010-06-15 17:48:49 +000014#include "clang/Rewrite/ASTConsumers.h"
Ted Kremenek5e0020e2008-03-18 22:21:07 +000015#include "clang/AST/ASTConsumer.h"
Ted Kremenek5e0020e2008-03-18 22:21:07 +000016#include "clang/AST/ASTContext.h"
Daniel Dunbar90b18272009-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"
22#include "clang/Rewrite/HTMLRewrite.h"
23#include "clang/Rewrite/Rewriter.h"
Eli Friedman24200912009-05-19 05:28:52 +000024#include "llvm/Support/MemoryBuffer.h"
Chris Lattner0fa0daa2009-08-24 04:11:30 +000025#include "llvm/Support/raw_ostream.h"
Ted Kremenek5e0020e2008-03-18 22:21:07 +000026using namespace clang;
27
Ted Kremenek13e479b2008-03-19 07:53:42 +000028//===----------------------------------------------------------------------===//
29// Functional HTML pretty-printing.
Mike Stump1eb44332009-09-09 15:08:12 +000030//===----------------------------------------------------------------------===//
Ted Kremenek13e479b2008-03-19 07:53:42 +000031
Ted Kremenek5e0020e2008-03-18 22:21:07 +000032namespace {
33 class HTMLPrinter : public ASTConsumer {
34 Rewriter R;
Eli Friedman66d6f042009-05-18 22:20:00 +000035 llvm::raw_ostream *Out;
Daniel Dunbar90b18272009-11-04 23:56:25 +000036 Preprocessor &PP;
37 bool SyntaxHighlight, HighlightMacros;
38
Ted Kremenek5e0020e2008-03-18 22:21:07 +000039 public:
Daniel Dunbar90b18272009-11-04 23:56:25 +000040 HTMLPrinter(llvm::raw_ostream *OS, Preprocessor &pp,
41 bool _SyntaxHighlight, bool _HighlightMacros)
42 : Out(OS), PP(pp), SyntaxHighlight(_SyntaxHighlight),
43 HighlightMacros(_HighlightMacros) {}
Mike Stump1eb44332009-09-09 15:08:12 +000044
Ted Kremenek5e0020e2008-03-18 22:21:07 +000045 void Initialize(ASTContext &context);
Eli Friedmande2baa72009-12-12 03:36:52 +000046 void HandleTranslationUnit(ASTContext &Ctx);
Ted Kremenek5e0020e2008-03-18 22:21:07 +000047 };
48}
49
Eli Friedman66d6f042009-05-18 22:20:00 +000050ASTConsumer* clang::CreateHTMLPrinter(llvm::raw_ostream *OS,
Daniel Dunbar90b18272009-11-04 23:56:25 +000051 Preprocessor &PP,
52 bool SyntaxHighlight,
53 bool HighlightMacros) {
54 return new HTMLPrinter(OS, PP, SyntaxHighlight, HighlightMacros);
Chris Lattner8ac661c2008-04-16 05:21:09 +000055}
Ted Kremenek5e0020e2008-03-18 22:21:07 +000056
57void HTMLPrinter::Initialize(ASTContext &context) {
Chris Lattner2c78b872009-04-14 23:22:57 +000058 R.setSourceMgr(context.getSourceManager(), context.getLangOptions());
Ted Kremenek5e0020e2008-03-18 22:21:07 +000059}
60
Eli Friedmande2baa72009-12-12 03:36:52 +000061void HTMLPrinter::HandleTranslationUnit(ASTContext &Ctx) {
Daniel Dunbar90b18272009-11-04 23:56:25 +000062 if (PP.getDiagnostics().hasErrorOccurred())
Chris Lattner8ac661c2008-04-16 05:21:09 +000063 return;
64
65 // Format the file.
Chris Lattner2b2453a2009-01-17 06:22:33 +000066 FileID FID = R.getSourceMgr().getMainFileID();
67 const FileEntry* Entry = R.getSourceMgr().getFileEntryForID(FID);
Eli Friedman24200912009-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 Lattner2b2453a2009-01-17 06:22:33 +000077 html::AddLineNumbers(R, FID);
Eli Friedman24200912009-05-19 05:28:52 +000078 html::AddHeaderFooterInternalBuiltinCSS(R, FID, Name);
Chris Lattner8ac661c2008-04-16 05:21:09 +000079
Ted Kremenek47abe762008-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 Stump1eb44332009-09-09 15:08:12 +000083
Daniel Dunbar90b18272009-11-04 23:56:25 +000084 if (SyntaxHighlight) html::SyntaxHighlight(R, FID, PP);
85 if (HighlightMacros) html::HighlightMacros(R, FID, PP);
Chris Lattner2b2453a2009-01-17 06:22:33 +000086 html::EscapeText(R, FID, false, true);
Eli Friedman66d6f042009-05-18 22:20:00 +000087
Ted Kremenek5e0020e2008-03-18 22:21:07 +000088 // Emit the HTML.
Chris Lattner2b2453a2009-01-17 06:22:33 +000089 const RewriteBuffer &RewriteBuf = R.getEditBuffer(FID);
Chris Lattner8ac661c2008-04-16 05:21:09 +000090 char *Buffer = (char*)malloc(RewriteBuf.size());
91 std::copy(RewriteBuf.begin(), RewriteBuf.end(), Buffer);
Eli Friedman66d6f042009-05-18 22:20:00 +000092 Out->write(Buffer, RewriteBuf.size());
Chris Lattner8ac661c2008-04-16 05:21:09 +000093 free(Buffer);
Ted Kremenek5e0020e2008-03-18 22:21:07 +000094}