Ted Kremenek | 088d12e | 2008-04-16 04:38:45 +0000 | [diff] [blame] | 1 | //===--- HTMLPrint.cpp - Source code -> HTML pretty-printing --------------===// |
Ted Kremenek | b0c409a | 2008-03-18 22:21:07 +0000 | [diff] [blame] | 2 | // |
| 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 Kremenek | 088d12e | 2008-04-16 04:38:45 +0000 | [diff] [blame] | 10 | // Pretty-printing of source code to HTML. |
Ted Kremenek | b0c409a | 2008-03-18 22:21:07 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Ted Kremenek | b0c409a | 2008-03-18 22:21:07 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTConsumer.h" |
Ted Kremenek | b0c409a | 2008-03-18 22:21:07 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | d0ba0e6 | 2009-11-04 23:56:25 +0000 | [diff] [blame] | 16 | #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 Kremenek | cdf8149 | 2012-09-01 05:09:24 +0000 | [diff] [blame] | 21 | #include "clang/Rewrite/Core/HTMLRewrite.h" |
| 22 | #include "clang/Rewrite/Core/Rewriter.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 23 | #include "clang/Rewrite/Frontend/ASTConsumers.h" |
Chris Lattner | 72bb4f6 | 2009-08-24 04:11:30 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | b0c409a | 2008-03-18 22:21:07 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | |
Ted Kremenek | 216624c | 2008-03-19 07:53:42 +0000 | [diff] [blame] | 27 | //===----------------------------------------------------------------------===// |
| 28 | // Functional HTML pretty-printing. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 216624c | 2008-03-19 07:53:42 +0000 | [diff] [blame] | 30 | |
Ted Kremenek | b0c409a | 2008-03-18 22:21:07 +0000 | [diff] [blame] | 31 | namespace { |
| 32 | class HTMLPrinter : public ASTConsumer { |
| 33 | Rewriter R; |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 34 | std::unique_ptr<raw_ostream> Out; |
Daniel Dunbar | d0ba0e6 | 2009-11-04 23:56:25 +0000 | [diff] [blame] | 35 | Preprocessor &PP; |
| 36 | bool SyntaxHighlight, HighlightMacros; |
| 37 | |
Ted Kremenek | b0c409a | 2008-03-18 22:21:07 +0000 | [diff] [blame] | 38 | public: |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 39 | HTMLPrinter(std::unique_ptr<raw_ostream> OS, Preprocessor &pp, |
Daniel Dunbar | d0ba0e6 | 2009-11-04 23:56:25 +0000 | [diff] [blame] | 40 | bool _SyntaxHighlight, bool _HighlightMacros) |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 41 | : Out(std::move(OS)), PP(pp), SyntaxHighlight(_SyntaxHighlight), |
Daniel Dunbar | d0ba0e6 | 2009-11-04 23:56:25 +0000 | [diff] [blame] | 42 | HighlightMacros(_HighlightMacros) {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 43 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 44 | void Initialize(ASTContext &context) override; |
| 45 | void HandleTranslationUnit(ASTContext &Ctx) override; |
Ted Kremenek | b0c409a | 2008-03-18 22:21:07 +0000 | [diff] [blame] | 46 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 47 | } |
Ted Kremenek | b0c409a | 2008-03-18 22:21:07 +0000 | [diff] [blame] | 48 | |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 49 | std::unique_ptr<ASTConsumer> |
| 50 | clang::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 Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 53 | HighlightMacros); |
Chris Lattner | dffe0f7 | 2008-04-16 05:21:09 +0000 | [diff] [blame] | 54 | } |
Ted Kremenek | b0c409a | 2008-03-18 22:21:07 +0000 | [diff] [blame] | 55 | |
| 56 | void HTMLPrinter::Initialize(ASTContext &context) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 57 | R.setSourceMgr(context.getSourceManager(), context.getLangOpts()); |
Ted Kremenek | b0c409a | 2008-03-18 22:21:07 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Eli Friedman | 3950e8c | 2009-12-12 03:36:52 +0000 | [diff] [blame] | 60 | void HTMLPrinter::HandleTranslationUnit(ASTContext &Ctx) { |
Daniel Dunbar | d0ba0e6 | 2009-11-04 23:56:25 +0000 | [diff] [blame] | 61 | if (PP.getDiagnostics().hasErrorOccurred()) |
Chris Lattner | dffe0f7 | 2008-04-16 05:21:09 +0000 | [diff] [blame] | 62 | return; |
| 63 | |
| 64 | // Format the file. |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 65 | FileID FID = R.getSourceMgr().getMainFileID(); |
| 66 | const FileEntry* Entry = R.getSourceMgr().getFileEntryForID(FID); |
Mehdi Amini | 99d1b29 | 2016-10-01 16:38:28 +0000 | [diff] [blame] | 67 | StringRef Name; |
Eli Friedman | 69329a5 | 2009-05-19 05:28:52 +0000 | [diff] [blame] | 68 | // 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 Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 76 | html::AddLineNumbers(R, FID); |
Eli Friedman | 69329a5 | 2009-05-19 05:28:52 +0000 | [diff] [blame] | 77 | html::AddHeaderFooterInternalBuiltinCSS(R, FID, Name); |
Chris Lattner | dffe0f7 | 2008-04-16 05:21:09 +0000 | [diff] [blame] | 78 | |
Ted Kremenek | f2e6fcf | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 79 | // 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 Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 82 | |
Daniel Dunbar | d0ba0e6 | 2009-11-04 23:56:25 +0000 | [diff] [blame] | 83 | if (SyntaxHighlight) html::SyntaxHighlight(R, FID, PP); |
| 84 | if (HighlightMacros) html::HighlightMacros(R, FID, PP); |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 85 | html::EscapeText(R, FID, false, true); |
Eli Friedman | 94cf21e | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 86 | |
Ted Kremenek | b0c409a | 2008-03-18 22:21:07 +0000 | [diff] [blame] | 87 | // Emit the HTML. |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 88 | const RewriteBuffer &RewriteBuf = R.getEditBuffer(FID); |
Chris Lattner | dffe0f7 | 2008-04-16 05:21:09 +0000 | [diff] [blame] | 89 | char *Buffer = (char*)malloc(RewriteBuf.size()); |
| 90 | std::copy(RewriteBuf.begin(), RewriteBuf.end(), Buffer); |
Eli Friedman | 94cf21e | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 91 | Out->write(Buffer, RewriteBuf.size()); |
Chris Lattner | dffe0f7 | 2008-04-16 05:21:09 +0000 | [diff] [blame] | 92 | free(Buffer); |
Ted Kremenek | b0c409a | 2008-03-18 22:21:07 +0000 | [diff] [blame] | 93 | } |