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