blob: 11e431de0a31df75eeeaedd2163412f760e69f90 [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 Kremenekb0c409a2008-03-18 22:21:07 +000014#include "clang/AST/ASTConsumer.h"
Ted Kremenekb0c409a2008-03-18 22:21:07 +000015#include "clang/AST/ASTContext.h"
Daniel Dunbard0ba0e62009-11-04 23:56:25 +000016#include "clang/AST/Decl.h"
17#include "clang/Basic/Diagnostic.h"
18#include "clang/Basic/FileManager.h"
19#include "clang/Basic/SourceManager.h"
20#include "clang/Lex/Preprocessor.h"
Ted Kremenekcdf81492012-09-01 05:09:24 +000021#include "clang/Rewrite/Core/HTMLRewrite.h"
22#include "clang/Rewrite/Core/Rewriter.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000023#include "clang/Rewrite/Frontend/ASTConsumers.h"
Chris Lattner72bb4f62009-08-24 04:11:30 +000024#include "llvm/Support/raw_ostream.h"
Ted Kremenekb0c409a2008-03-18 22:21:07 +000025using namespace clang;
26
Ted Kremenek216624c2008-03-19 07:53:42 +000027//===----------------------------------------------------------------------===//
28// Functional HTML pretty-printing.
Mike Stump11289f42009-09-09 15:08:12 +000029//===----------------------------------------------------------------------===//
Ted Kremenek216624c2008-03-19 07:53:42 +000030
Ted Kremenekb0c409a2008-03-18 22:21:07 +000031namespace {
32 class HTMLPrinter : public ASTConsumer {
33 Rewriter R;
Peter Collingbourne03f89072016-07-15 00:55:40 +000034 std::unique_ptr<raw_ostream> Out;
Daniel Dunbard0ba0e62009-11-04 23:56:25 +000035 Preprocessor &PP;
36 bool SyntaxHighlight, HighlightMacros;
37
Ted Kremenekb0c409a2008-03-18 22:21:07 +000038 public:
Peter Collingbourne03f89072016-07-15 00:55:40 +000039 HTMLPrinter(std::unique_ptr<raw_ostream> OS, Preprocessor &pp,
Daniel Dunbard0ba0e62009-11-04 23:56:25 +000040 bool _SyntaxHighlight, bool _HighlightMacros)
Peter Collingbourne03f89072016-07-15 00:55:40 +000041 : Out(std::move(OS)), PP(pp), SyntaxHighlight(_SyntaxHighlight),
Daniel Dunbard0ba0e62009-11-04 23:56:25 +000042 HighlightMacros(_HighlightMacros) {}
Mike Stump11289f42009-09-09 15:08:12 +000043
Craig Topperfb6b25b2014-03-15 04:29:04 +000044 void Initialize(ASTContext &context) override;
45 void HandleTranslationUnit(ASTContext &Ctx) override;
Ted Kremenekb0c409a2008-03-18 22:21:07 +000046 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +000047}
Ted Kremenekb0c409a2008-03-18 22:21:07 +000048
Peter Collingbourne03f89072016-07-15 00:55:40 +000049std::unique_ptr<ASTConsumer>
50clang::CreateHTMLPrinter(std::unique_ptr<raw_ostream> OS, Preprocessor &PP,
51 bool SyntaxHighlight, bool HighlightMacros) {
52 return llvm::make_unique<HTMLPrinter>(std::move(OS), PP, SyntaxHighlight,
David Blaikie6beb6aa2014-08-10 19:56:51 +000053 HighlightMacros);
Chris Lattnerdffe0f72008-04-16 05:21:09 +000054}
Ted Kremenekb0c409a2008-03-18 22:21:07 +000055
56void HTMLPrinter::Initialize(ASTContext &context) {
David Blaikiebbafb8a2012-03-11 07:00:24 +000057 R.setSourceMgr(context.getSourceManager(), context.getLangOpts());
Ted Kremenekb0c409a2008-03-18 22:21:07 +000058}
59
Eli Friedman3950e8c2009-12-12 03:36:52 +000060void HTMLPrinter::HandleTranslationUnit(ASTContext &Ctx) {
Daniel Dunbard0ba0e62009-11-04 23:56:25 +000061 if (PP.getDiagnostics().hasErrorOccurred())
Chris Lattnerdffe0f72008-04-16 05:21:09 +000062 return;
63
64 // Format the file.
Chris Lattnerd32480d2009-01-17 06:22:33 +000065 FileID FID = R.getSourceMgr().getMainFileID();
66 const FileEntry* Entry = R.getSourceMgr().getFileEntryForID(FID);
Mehdi Amini99d1b292016-10-01 16:38:28 +000067 StringRef Name;
Eli Friedman69329a52009-05-19 05:28:52 +000068 // In some cases, in particular the case where the input is from stdin,
69 // there is no entry. Fall back to the memory buffer for a name in those
70 // cases.
71 if (Entry)
72 Name = Entry->getName();
73 else
74 Name = R.getSourceMgr().getBuffer(FID)->getBufferIdentifier();
75
Chris Lattnerd32480d2009-01-17 06:22:33 +000076 html::AddLineNumbers(R, FID);
Eli Friedman69329a52009-05-19 05:28:52 +000077 html::AddHeaderFooterInternalBuiltinCSS(R, FID, Name);
Chris Lattnerdffe0f72008-04-16 05:21:09 +000078
Ted Kremenekf2e6fcf2008-04-16 16:39:56 +000079 // If we have a preprocessor, relex the file and syntax highlight.
80 // We might not have a preprocessor if we come from a deserialized AST file,
81 // for example.
Mike Stump11289f42009-09-09 15:08:12 +000082
Daniel Dunbard0ba0e62009-11-04 23:56:25 +000083 if (SyntaxHighlight) html::SyntaxHighlight(R, FID, PP);
84 if (HighlightMacros) html::HighlightMacros(R, FID, PP);
Chris Lattnerd32480d2009-01-17 06:22:33 +000085 html::EscapeText(R, FID, false, true);
Eli Friedman94cf21e2009-05-18 22:20:00 +000086
Ted Kremenekb0c409a2008-03-18 22:21:07 +000087 // Emit the HTML.
Chris Lattnerd32480d2009-01-17 06:22:33 +000088 const RewriteBuffer &RewriteBuf = R.getEditBuffer(FID);
Chris Lattnerdffe0f72008-04-16 05:21:09 +000089 char *Buffer = (char*)malloc(RewriteBuf.size());
90 std::copy(RewriteBuf.begin(), RewriteBuf.end(), Buffer);
Eli Friedman94cf21e2009-05-18 22:20:00 +000091 Out->write(Buffer, RewriteBuf.size());
Chris Lattnerdffe0f72008-04-16 05:21:09 +000092 free(Buffer);
Ted Kremenekb0c409a2008-03-18 22:21:07 +000093}