aboutsummaryrefslogtreecommitdiff
path: root/Driver/HTMLPrint.cpp
blob: 93e544fc9662d24cc79783ff3018f4308304215f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//===--- 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"
#include <sstream>

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);
  
  // Generate header

  {
    std::ostringstream os;
  
    os << "<html>\n<head>\n"
       << " <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"
       << " </style>\n"
       << "</head>\n"
       << "<body>";

    R.InsertStrBefore(StartLoc, os.str());
  }
  
  // Generate footer
  
  {
    std::ostringstream os;
    
    os << "</body></html>\n";
    R.InsertStrAfter(EndLoc, os.str());
  }
    
  
  // Emit the HTML.
  
  if (const RewriteBuffer *RewriteBuf = R.getRewriteBufferFor(FileID)) {
    std::string S(RewriteBuf->begin(), RewriteBuf->end());
    printf("%s\n", S.c_str());
  }
}