From: Ted Kremenek Date: Tue, 18 Mar 2008 22:21:07 +0000 (+0000) Subject: Added HTML pretty-printer. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5e0020ee6182f63cd5f4a53e240dc08bd02d7728;p=clang Added HTML pretty-printer. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@48507 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/Driver/HTMLPrint.cpp b/Driver/HTMLPrint.cpp new file mode 100644 index 0000000000..d347901e8b --- /dev/null +++ b/Driver/HTMLPrint.cpp @@ -0,0 +1,62 @@ +//===--- 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" + +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); +// html::InsertTag(R, html::HEAD, StartLoc, EndLoc, true); +// html::InsertTag(R, html::BODY, StartLoc, EndLoc, true); +// html::InsertTag(R, html::PRE, StartLoc, EndLoc); + + // Emit the HTML. + + if (const RewriteBuffer *RewriteBuf = R.getRewriteBufferFor(FileID)) { + std::string S(RewriteBuf->begin(), RewriteBuf->end()); + printf("%s\n", S.c_str()); + } +}