Ted Kremenek | 5e0020e | 2008-03-18 22:21:07 +0000 | [diff] [blame] | 1 | //===--- RewriteTest.cpp - Playground for the code rewriter ---------------===// |
| 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 | // |
| 10 | // Hacks and fun related to the code rewriter. |
| 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" |
| 18 | #include "clang/Basic/SourceManager.h" |
| 19 | #include "llvm/Support/MemoryBuffer.h" |
| 20 | #include "clang/AST/ASTContext.h" |
Ted Kremenek | 1b3188c | 2008-03-18 23:55:46 +0000 | [diff] [blame] | 21 | #include <sstream> |
Ted Kremenek | 5e0020e | 2008-03-18 22:21:07 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace clang; |
| 24 | |
| 25 | namespace { |
| 26 | class HTMLPrinter : public ASTConsumer { |
| 27 | Rewriter R; |
| 28 | public: |
| 29 | HTMLPrinter() {} |
| 30 | virtual ~HTMLPrinter(); |
| 31 | |
| 32 | void Initialize(ASTContext &context); |
| 33 | }; |
| 34 | } |
| 35 | |
| 36 | ASTConsumer* clang::CreateHTMLPrinter() { return new HTMLPrinter(); } |
| 37 | |
| 38 | void HTMLPrinter::Initialize(ASTContext &context) { |
| 39 | R.setSourceMgr(context.getSourceManager()); |
| 40 | } |
| 41 | |
| 42 | HTMLPrinter::~HTMLPrinter() { |
| 43 | unsigned FileID = R.getSourceMgr().getMainFileID(); |
| 44 | |
| 45 | const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID); |
| 46 | const char* FileStart = Buf->getBufferStart(); |
| 47 | const char* FileEnd = Buf->getBufferEnd(); |
| 48 | SourceLocation StartLoc = SourceLocation::getFileLoc(FileID, 0); |
| 49 | SourceLocation EndLoc = SourceLocation::getFileLoc(FileID, FileEnd-FileStart); |
| 50 | |
| 51 | html::EscapeText(R, FileID); |
| 52 | html::AddLineNumbers(R, FileID); |
Ted Kremenek | 1b3188c | 2008-03-18 23:55:46 +0000 | [diff] [blame] | 53 | |
Ted Kremenek | f830997 | 2008-03-19 01:30:02 +0000 | [diff] [blame] | 54 | // Generate header |
Ted Kremenek | 1b3188c | 2008-03-18 23:55:46 +0000 | [diff] [blame] | 55 | |
Ted Kremenek | f830997 | 2008-03-19 01:30:02 +0000 | [diff] [blame] | 56 | { |
| 57 | std::ostringstream os; |
Ted Kremenek | 1b3188c | 2008-03-18 23:55:46 +0000 | [diff] [blame] | 58 | |
Ted Kremenek | f830997 | 2008-03-19 01:30:02 +0000 | [diff] [blame] | 59 | os << "<html>\n<head>\n" |
Ted Kremenek | d6c1360 | 2008-03-19 05:07:26 +0000 | [diff] [blame] | 60 | << " <style type=\"text/css\">\n" |
| 61 | << " .codeblock { width:100% }\n" |
| 62 | << " .codeline { font-family: \"Monaco\", fixed; font-size:11pt }\n" |
| 63 | << " .codeline { height:1.5em; line-height:1.5em }\n" |
| 64 | << " .nums, .lines { float:left; height:100% }\n" |
| 65 | << " .nums { background-color: #eeeeee }\n" |
| 66 | << " .nums { font-family: \"Andale Mono\", fixed; font-size:smaller }\n" |
| 67 | << " .nums { width:2.5em; padding-right:2ex; text-align:right }\n" |
| 68 | << " .lines { padding-left: 1ex; border-left: 3px solid #ccc }\n" |
| 69 | << " .lines { white-space: pre }\n" |
Ted Kremenek | f830997 | 2008-03-19 01:30:02 +0000 | [diff] [blame] | 70 | << " </style>\n" |
| 71 | << "</head>\n" |
Ted Kremenek | d6c1360 | 2008-03-19 05:07:26 +0000 | [diff] [blame] | 72 | << "<body>"; |
Ted Kremenek | f830997 | 2008-03-19 01:30:02 +0000 | [diff] [blame] | 73 | |
Ted Kremenek | d6c1360 | 2008-03-19 05:07:26 +0000 | [diff] [blame] | 74 | R.InsertStrBefore(StartLoc, os.str()); |
Ted Kremenek | f830997 | 2008-03-19 01:30:02 +0000 | [diff] [blame] | 75 | } |
Ted Kremenek | 1b3188c | 2008-03-18 23:55:46 +0000 | [diff] [blame] | 76 | |
Ted Kremenek | f830997 | 2008-03-19 01:30:02 +0000 | [diff] [blame] | 77 | // Generate footer |
| 78 | |
| 79 | { |
| 80 | std::ostringstream os; |
| 81 | |
Ted Kremenek | d6c1360 | 2008-03-19 05:07:26 +0000 | [diff] [blame] | 82 | os << "</body></html>\n"; |
| 83 | R.InsertStrAfter(EndLoc, os.str()); |
Ted Kremenek | f830997 | 2008-03-19 01:30:02 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Ted Kremenek | 5e0020e | 2008-03-18 22:21:07 +0000 | [diff] [blame] | 86 | |
| 87 | // Emit the HTML. |
| 88 | |
| 89 | if (const RewriteBuffer *RewriteBuf = R.getRewriteBufferFor(FileID)) { |
| 90 | std::string S(RewriteBuf->begin(), RewriteBuf->end()); |
| 91 | printf("%s\n", S.c_str()); |
| 92 | } |
| 93 | } |