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