blob: 940016afd2453115a9dc2f1e075aa6b092e9ba99 [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"
21
22using namespace clang;
23
24namespace {
25 class HTMLPrinter : public ASTConsumer {
26 Rewriter R;
27 public:
28 HTMLPrinter() {}
29 virtual ~HTMLPrinter();
30
31 void Initialize(ASTContext &context);
32 };
33}
34
35ASTConsumer* clang::CreateHTMLPrinter() { return new HTMLPrinter(); }
36
37void HTMLPrinter::Initialize(ASTContext &context) {
38 R.setSourceMgr(context.getSourceManager());
39}
40
41HTMLPrinter::~HTMLPrinter() {
42 unsigned FileID = R.getSourceMgr().getMainFileID();
43
44 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID);
45 const char* FileStart = Buf->getBufferStart();
46 const char* FileEnd = Buf->getBufferEnd();
47 SourceLocation StartLoc = SourceLocation::getFileLoc(FileID, 0);
48 SourceLocation EndLoc = SourceLocation::getFileLoc(FileID, FileEnd-FileStart);
49
50 html::EscapeText(R, FileID);
51 html::AddLineNumbers(R, FileID);
Ted Kremenekb485cd12008-03-18 23:08:51 +000052 html::InsertTag(R, html::PRE, StartLoc, EndLoc, 0, 0, true);
53 html::InsertTag(R, html::BODY, StartLoc, EndLoc, NULL, "\n", true);
54 html::InsertTag(R, html::HEAD, StartLoc, StartLoc, 0, 0, true);
55 html::InsertTag(R, html::HTML, StartLoc, EndLoc, NULL, "\n", true);
Ted Kremenek5e0020e2008-03-18 22:21:07 +000056
57 // Emit the HTML.
58
59 if (const RewriteBuffer *RewriteBuf = R.getRewriteBufferFor(FileID)) {
60 std::string S(RewriteBuf->begin(), RewriteBuf->end());
61 printf("%s\n", S.c_str());
62 }
63}