blob: 93e544fc9662d24cc79783ff3018f4308304215f [file] [log] [blame]
Ted Kremenek5e0020e2008-03-18 22:21:07 +00001//===--- 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 Kremenek1b3188c2008-03-18 23:55:46 +000021#include <sstream>
Ted Kremenek5e0020e2008-03-18 22:21:07 +000022
23using namespace clang;
24
25namespace {
26 class HTMLPrinter : public ASTConsumer {
27 Rewriter R;
28 public:
29 HTMLPrinter() {}
30 virtual ~HTMLPrinter();
31
32 void Initialize(ASTContext &context);
33 };
34}
35
36ASTConsumer* clang::CreateHTMLPrinter() { return new HTMLPrinter(); }
37
38void HTMLPrinter::Initialize(ASTContext &context) {
39 R.setSourceMgr(context.getSourceManager());
40}
41
42HTMLPrinter::~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 Kremenek1b3188c2008-03-18 23:55:46 +000053
Ted Kremenekf8309972008-03-19 01:30:02 +000054 // Generate header
Ted Kremenek1b3188c2008-03-18 23:55:46 +000055
Ted Kremenekf8309972008-03-19 01:30:02 +000056 {
57 std::ostringstream os;
Ted Kremenek1b3188c2008-03-18 23:55:46 +000058
Ted Kremenekf8309972008-03-19 01:30:02 +000059 os << "<html>\n<head>\n"
Ted Kremenekd6c13602008-03-19 05:07:26 +000060 << " <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 Kremenekf8309972008-03-19 01:30:02 +000070 << " </style>\n"
71 << "</head>\n"
Ted Kremenekd6c13602008-03-19 05:07:26 +000072 << "<body>";
Ted Kremenekf8309972008-03-19 01:30:02 +000073
Ted Kremenekd6c13602008-03-19 05:07:26 +000074 R.InsertStrBefore(StartLoc, os.str());
Ted Kremenekf8309972008-03-19 01:30:02 +000075 }
Ted Kremenek1b3188c2008-03-18 23:55:46 +000076
Ted Kremenekf8309972008-03-19 01:30:02 +000077 // Generate footer
78
79 {
80 std::ostringstream os;
81
Ted Kremenekd6c13602008-03-19 05:07:26 +000082 os << "</body></html>\n";
83 R.InsertStrAfter(EndLoc, os.str());
Ted Kremenekf8309972008-03-19 01:30:02 +000084 }
85
Ted Kremenek5e0020e2008-03-18 22:21:07 +000086
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}