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 | ad0a203 | 2008-03-19 06:14:37 +0000 | [diff] [blame^] | 53 | html::AddHeaderFooterInternalBuiltinCSS(R, FileID); |
Ted Kremenek | 5e0020e | 2008-03-18 22:21:07 +0000 | [diff] [blame] | 54 | |
| 55 | // Emit the HTML. |
| 56 | |
| 57 | if (const RewriteBuffer *RewriteBuf = R.getRewriteBufferFor(FileID)) { |
| 58 | std::string S(RewriteBuf->begin(), RewriteBuf->end()); |
| 59 | printf("%s\n", S.c_str()); |
| 60 | } |
| 61 | } |