[cfe-commits] r48505 - in /cfe/trunk: Driver/ASTConsumers.h Driver/clang.cpp include/clang/Rewrite/HTMLRewrite.h lib/Rewrite/HTMLRewrite.cpp
Ted Kremenek
kremenek at apple.com
Tue Mar 18 14:19:50 PDT 2008
Author: kremenek
Date: Tue Mar 18 16:19:49 2008
New Revision: 48505
URL: http://llvm.org/viewvc/llvm-project?rev=48505&view=rev
Log:
Added the beginning of a set of rewriter functions for pretty-printing source
code as HTML. Added a new driver option "--emit-html" to dump the source
of the main input file as HTML.
Added:
cfe/trunk/include/clang/Rewrite/HTMLRewrite.h
cfe/trunk/lib/Rewrite/HTMLRewrite.cpp
Modified:
cfe/trunk/Driver/ASTConsumers.h
cfe/trunk/Driver/clang.cpp
Modified: cfe/trunk/Driver/ASTConsumers.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/ASTConsumers.h?rev=48505&r1=48504&r2=48505&view=diff
==============================================================================
--- cfe/trunk/Driver/ASTConsumers.h (original)
+++ cfe/trunk/Driver/ASTConsumers.h Tue Mar 18 16:19:49 2008
@@ -53,6 +53,9 @@
Diagnostic &Diags,
const LangOptions &LOpts);
+ASTConsumer* CreateHTMLPrinter();
+
+
ASTConsumer *CreateSerializationTest(Diagnostic &Diags,
FileManager& FMgr,
const LangOptions &LOpts);
Modified: cfe/trunk/Driver/clang.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/clang.cpp?rev=48505&r1=48504&r2=48505&view=diff
==============================================================================
--- cfe/trunk/Driver/clang.cpp (original)
+++ cfe/trunk/Driver/clang.cpp Tue Mar 18 16:19:49 2008
@@ -63,6 +63,7 @@
EmitLLVM, // Emit a .ll file.
EmitBC, // Emit a .bc file.
SerializeAST, // Emit a .ast file.
+ EmitHTML, // Translate input source into HTML.
ASTPrint, // Parse ASTs and print them.
ASTDump, // Parse ASTs and dump them.
ASTView, // Parse ASTs and view them in Graphviz.
@@ -100,6 +101,8 @@
"Run parser and perform semantic analysis"),
clEnumValN(ParsePrintCallbacks, "parse-print-callbacks",
"Run parser and print each callback invoked"),
+ clEnumValN(EmitHTML, "emit-html",
+ "Output input source as HTML"),
clEnumValN(ASTPrint, "ast-print",
"Build ASTs and then pretty-print them"),
clEnumValN(ASTDump, "ast-dump",
@@ -994,7 +997,10 @@
return CreateASTDumper();
case ASTView:
- return CreateASTViewer();
+ return CreateASTViewer();
+
+ case EmitHTML:
+ return CreateHTMLPrinter();
case ParseCFGDump:
case ParseCFGView:
Added: cfe/trunk/include/clang/Rewrite/HTMLRewrite.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Rewrite/HTMLRewrite.h?rev=48505&view=auto
==============================================================================
--- cfe/trunk/include/clang/Rewrite/HTMLRewrite.h (added)
+++ cfe/trunk/include/clang/Rewrite/HTMLRewrite.h Tue Mar 18 16:19:49 2008
@@ -0,0 +1,37 @@
+//==- HTMLRewrite.h - Translate source code into prettified HTML ---*- C++ -*-//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines a set of functions used for translating source code
+// into beautified HTML.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_HTMLREWRITER_H
+#define LLVM_CLANG_HTMLREWRITER_H
+
+#include "clang/Basic/SourceLocation.h"
+
+namespace clang {
+
+class Rewriter;
+
+namespace html {
+
+ enum Tags { PRE, HEAD, BODY };
+
+ void EscapeText(Rewriter& R, unsigned FileID, bool EscapeSpaces = false);
+
+ void InsertTag(Rewriter& R, Tags tag,
+ SourceLocation OpenLoc, SourceLocation CloseLoc,
+ bool NewlineOpen = false, bool NewlineClose = true);
+
+} // end html namespace
+} // end clang namespace
+
+#endif
Added: cfe/trunk/lib/Rewrite/HTMLRewrite.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Rewrite/HTMLRewrite.cpp?rev=48505&view=auto
==============================================================================
--- cfe/trunk/lib/Rewrite/HTMLRewrite.cpp (added)
+++ cfe/trunk/lib/Rewrite/HTMLRewrite.cpp Tue Mar 18 16:19:49 2008
@@ -0,0 +1,77 @@
+//== HTMLRewrite.cpp - Translate source code into prettified HTML --*- C++ -*-//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines the HTMLRewriter clas, which is used to translate the
+// text of a source file into prettified HTML.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Rewrite/Rewriter.h"
+#include "clang/Rewrite/HTMLRewrite.h"
+#include "clang/Basic/SourceManager.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include <sstream>
+
+using namespace clang;
+
+void html::EscapeText(Rewriter& R, unsigned FileID, bool EscapeSpaces) {
+
+ const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID);
+ const char* C = Buf->getBufferStart();
+ const char* FileEnd = Buf->getBufferEnd();
+
+ assert (C <= FileEnd);
+
+ for (unsigned FilePos = 0; C != FileEnd ; ++C, ++FilePos) {
+
+ SourceLocation Loc = SourceLocation::getFileLoc(FileID, FilePos);
+
+ switch (*C) {
+ default: break;
+
+ case ' ':
+ if (EscapeSpaces) R.ReplaceText(Loc, 1, " ", 5);
+ break;
+
+ case '<': R.ReplaceText(Loc, 1, "<", 4); break;
+ case '>': R.ReplaceText(Loc, 1, ">", 4); break;
+ case '&': R.ReplaceText(Loc, 1, "&", 5); break;
+ }
+ }
+}
+
+void html::InsertTag(Rewriter& R, html::Tags tag,
+ SourceLocation B, SourceLocation E,
+ bool NewlineOpen, bool NewlineClose) {
+
+ const char* TagStr = 0;
+
+ switch (tag) {
+ default: break;
+ case PRE: TagStr = "pre"; break;
+ case HEAD: TagStr = "head"; break;
+ case BODY: TagStr = "body"; break;
+ }
+
+ assert (TagStr && "Tag not supported.");
+
+ { // Generate the opening tag.
+ std::ostringstream os;
+ os << '<' << TagStr << '>';
+ if (NewlineOpen) os << '\n';
+ R.InsertTextAfter(B, os.str().c_str(), os.str().size());
+ }
+
+ { // Generate the closing tag.
+ std::ostringstream os;
+ os << "</" << TagStr << '>';
+ if (NewlineClose) os << '\n';
+ R.InsertTextBefore(E, os.str().c_str(), os.str().size());
+ }
+}
More information about the cfe-commits
mailing list