Ted Kremenek | fdbe679 | 2008-04-16 04:38:45 +0000 | [diff] [blame] | 1 | //===--- HTMLPrint.cpp - Source code -> HTML pretty-printing --------------===// |
Ted Kremenek | 5e0020e | 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 | fdbe679 | 2008-04-16 04:38:45 +0000 | [diff] [blame] | 10 | // Pretty-printing of source code to HTML. |
Ted Kremenek | 5e0020e | 2008-03-18 22:21:07 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "ASTConsumers.h" |
| 15 | #include "clang/AST/ASTConsumer.h" |
| 16 | #include "clang/Rewrite/Rewriter.h" |
| 17 | #include "clang/Rewrite/HTMLRewrite.h" |
Chris Lattner | 8ac661c | 2008-04-16 05:21:09 +0000 | [diff] [blame] | 18 | #include "clang/Basic/Diagnostic.h" |
Ted Kremenek | 5e0020e | 2008-03-18 22:21:07 +0000 | [diff] [blame] | 19 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | 5e0020e | 2008-03-18 22:21:07 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
| 21 | |
| 22 | using namespace clang; |
| 23 | |
Ted Kremenek | 13e479b | 2008-03-19 07:53:42 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
| 25 | // Functional HTML pretty-printing. |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
Ted Kremenek | 5e0020e | 2008-03-18 22:21:07 +0000 | [diff] [blame] | 28 | namespace { |
| 29 | class HTMLPrinter : public ASTConsumer { |
| 30 | Rewriter R; |
Chris Lattner | 8ac661c | 2008-04-16 05:21:09 +0000 | [diff] [blame] | 31 | std::string OutFilename; |
| 32 | Diagnostic &Diags; |
Chris Lattner | 3245a0a | 2008-04-16 06:11:58 +0000 | [diff] [blame] | 33 | Preprocessor *PP; |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 34 | PreprocessorFactory *PPF; |
Ted Kremenek | 5e0020e | 2008-03-18 22:21:07 +0000 | [diff] [blame] | 35 | public: |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 36 | HTMLPrinter(const std::string &OutFile, Diagnostic &D, Preprocessor *pp, |
| 37 | PreprocessorFactory* ppf) |
| 38 | : OutFilename(OutFile), Diags(D), PP(pp), PPF(ppf) {} |
Ted Kremenek | 5e0020e | 2008-03-18 22:21:07 +0000 | [diff] [blame] | 39 | virtual ~HTMLPrinter(); |
| 40 | |
| 41 | void Initialize(ASTContext &context); |
| 42 | }; |
| 43 | } |
| 44 | |
Chris Lattner | 8ac661c | 2008-04-16 05:21:09 +0000 | [diff] [blame] | 45 | ASTConsumer* clang::CreateHTMLPrinter(const std::string &OutFile, |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 46 | Diagnostic &D, Preprocessor *PP, |
| 47 | PreprocessorFactory* PPF) { |
| 48 | |
| 49 | return new HTMLPrinter(OutFile, D, PP, PPF); |
Chris Lattner | 8ac661c | 2008-04-16 05:21:09 +0000 | [diff] [blame] | 50 | } |
Ted Kremenek | 5e0020e | 2008-03-18 22:21:07 +0000 | [diff] [blame] | 51 | |
| 52 | void HTMLPrinter::Initialize(ASTContext &context) { |
| 53 | R.setSourceMgr(context.getSourceManager()); |
| 54 | } |
| 55 | |
| 56 | HTMLPrinter::~HTMLPrinter() { |
Chris Lattner | 8ac661c | 2008-04-16 05:21:09 +0000 | [diff] [blame] | 57 | if (Diags.hasErrorOccurred()) |
| 58 | return; |
| 59 | |
| 60 | // Format the file. |
Ted Kremenek | 13e479b | 2008-03-19 07:53:42 +0000 | [diff] [blame] | 61 | unsigned FileID = R.getSourceMgr().getMainFileID(); |
Ted Kremenek | 5e0020e | 2008-03-18 22:21:07 +0000 | [diff] [blame] | 62 | html::AddLineNumbers(R, FileID); |
Ted Kremenek | ad0a203 | 2008-03-19 06:14:37 +0000 | [diff] [blame] | 63 | html::AddHeaderFooterInternalBuiltinCSS(R, FileID); |
Chris Lattner | 8ac661c | 2008-04-16 05:21:09 +0000 | [diff] [blame] | 64 | |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 65 | // If we have a preprocessor, relex the file and syntax highlight. |
| 66 | // We might not have a preprocessor if we come from a deserialized AST file, |
| 67 | // for example. |
| 68 | |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 69 | if (PP) html::SyntaxHighlight(R, FileID, *PP); |
Chris Lattner | aa39197 | 2008-04-19 23:09:31 +0000 | [diff] [blame] | 70 | if (PPF) html::HighlightMacros(R, FileID, *PP); |
Chris Lattner | 1ee8d6f | 2008-04-17 22:24:51 +0000 | [diff] [blame] | 71 | html::EscapeText(R, FileID, false, true); |
Chris Lattner | 3245a0a | 2008-04-16 06:11:58 +0000 | [diff] [blame] | 72 | |
Chris Lattner | 8ac661c | 2008-04-16 05:21:09 +0000 | [diff] [blame] | 73 | // Open the output. |
| 74 | FILE *OutputFILE; |
| 75 | if (OutFilename.empty() || OutFilename == "-") |
| 76 | OutputFILE = stdout; |
| 77 | else { |
| 78 | OutputFILE = fopen(OutFilename.c_str(), "w+"); |
| 79 | if (OutputFILE == 0) { |
| 80 | fprintf(stderr, "Error opening output file '%s'.\n", OutFilename.c_str()); |
| 81 | exit(1); |
| 82 | } |
| 83 | } |
Ted Kremenek | 5e0020e | 2008-03-18 22:21:07 +0000 | [diff] [blame] | 84 | |
| 85 | // Emit the HTML. |
Chris Lattner | 8ac661c | 2008-04-16 05:21:09 +0000 | [diff] [blame] | 86 | const RewriteBuffer &RewriteBuf = R.getEditBuffer(FileID); |
| 87 | char *Buffer = (char*)malloc(RewriteBuf.size()); |
| 88 | std::copy(RewriteBuf.begin(), RewriteBuf.end(), Buffer); |
| 89 | fwrite(Buffer, 1, RewriteBuf.size(), OutputFILE); |
| 90 | free(Buffer); |
Ted Kremenek | 5e0020e | 2008-03-18 22:21:07 +0000 | [diff] [blame] | 91 | |
Chris Lattner | 8ac661c | 2008-04-16 05:21:09 +0000 | [diff] [blame] | 92 | if (OutputFILE != stdout) fclose(OutputFILE); |
Ted Kremenek | 5e0020e | 2008-03-18 22:21:07 +0000 | [diff] [blame] | 93 | } |