2008-03-19 06:21:07 +08:00
|
|
|
//===--- RewriteTest.cpp - Playground for the code rewriter ---------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Hacks and fun related to the code rewriter.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ASTConsumers.h"
|
|
|
|
#include "clang/AST/ASTConsumer.h"
|
|
|
|
#include "clang/Rewrite/Rewriter.h"
|
|
|
|
#include "clang/Rewrite/HTMLRewrite.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
2008-03-19 07:55:46 +08:00
|
|
|
#include <sstream>
|
2008-03-19 06:21:07 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class HTMLPrinter : public ASTConsumer {
|
|
|
|
Rewriter R;
|
|
|
|
public:
|
|
|
|
HTMLPrinter() {}
|
|
|
|
virtual ~HTMLPrinter();
|
|
|
|
|
|
|
|
void Initialize(ASTContext &context);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTConsumer* clang::CreateHTMLPrinter() { return new HTMLPrinter(); }
|
|
|
|
|
|
|
|
void HTMLPrinter::Initialize(ASTContext &context) {
|
|
|
|
R.setSourceMgr(context.getSourceManager());
|
|
|
|
}
|
|
|
|
|
|
|
|
HTMLPrinter::~HTMLPrinter() {
|
|
|
|
unsigned FileID = R.getSourceMgr().getMainFileID();
|
|
|
|
|
|
|
|
const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID);
|
|
|
|
const char* FileStart = Buf->getBufferStart();
|
|
|
|
const char* FileEnd = Buf->getBufferEnd();
|
|
|
|
SourceLocation StartLoc = SourceLocation::getFileLoc(FileID, 0);
|
|
|
|
SourceLocation EndLoc = SourceLocation::getFileLoc(FileID, FileEnd-FileStart);
|
|
|
|
|
|
|
|
html::EscapeText(R, FileID);
|
|
|
|
html::AddLineNumbers(R, FileID);
|
2008-03-19 07:55:46 +08:00
|
|
|
|
2008-03-19 09:30:02 +08:00
|
|
|
// Generate header
|
|
|
|
|
|
|
|
{
|
|
|
|
std::ostringstream os;
|
2008-03-19 07:55:46 +08:00
|
|
|
|
2008-03-19 09:30:02 +08:00
|
|
|
os << "<html>\n<head>\n"
|
2008-03-19 13:07:26 +08:00
|
|
|
<< " <style type=\"text/css\">\n"
|
|
|
|
<< " .codeblock { width:100% }\n"
|
|
|
|
<< " .codeline { font-family: \"Monaco\", fixed; font-size:11pt }\n"
|
|
|
|
<< " .codeline { height:1.5em; line-height:1.5em }\n"
|
|
|
|
<< " .nums, .lines { float:left; height:100% }\n"
|
|
|
|
<< " .nums { background-color: #eeeeee }\n"
|
|
|
|
<< " .nums { font-family: \"Andale Mono\", fixed; font-size:smaller }\n"
|
|
|
|
<< " .nums { width:2.5em; padding-right:2ex; text-align:right }\n"
|
|
|
|
<< " .lines { padding-left: 1ex; border-left: 3px solid #ccc }\n"
|
|
|
|
<< " .lines { white-space: pre }\n"
|
2008-03-19 09:30:02 +08:00
|
|
|
<< " </style>\n"
|
|
|
|
<< "</head>\n"
|
2008-03-19 13:07:26 +08:00
|
|
|
<< "<body>";
|
2008-03-19 07:55:46 +08:00
|
|
|
|
2008-03-19 13:07:26 +08:00
|
|
|
R.InsertStrBefore(StartLoc, os.str());
|
2008-03-19 09:30:02 +08:00
|
|
|
}
|
2008-03-19 07:55:46 +08:00
|
|
|
|
2008-03-19 09:30:02 +08:00
|
|
|
// Generate footer
|
2008-03-19 07:55:46 +08:00
|
|
|
|
2008-03-19 09:30:02 +08:00
|
|
|
{
|
|
|
|
std::ostringstream os;
|
|
|
|
|
2008-03-19 13:07:26 +08:00
|
|
|
os << "</body></html>\n";
|
|
|
|
R.InsertStrAfter(EndLoc, os.str());
|
2008-03-19 09:30:02 +08:00
|
|
|
}
|
|
|
|
|
2008-03-19 06:21:07 +08:00
|
|
|
|
|
|
|
// Emit the HTML.
|
|
|
|
|
|
|
|
if (const RewriteBuffer *RewriteBuf = R.getRewriteBufferFor(FileID)) {
|
|
|
|
std::string S(RewriteBuf->begin(), RewriteBuf->end());
|
|
|
|
printf("%s\n", S.c_str());
|
|
|
|
}
|
|
|
|
}
|