diff options
author | Eli Friedman <eli.friedman@gmail.com> | 2009-05-18 22:50:54 +0000 |
---|---|---|
committer | Eli Friedman <eli.friedman@gmail.com> | 2009-05-18 22:50:54 +0000 |
commit | 9f30fc33d936877f8dca3b577550680edc0548cd (patch) | |
tree | 56c64f76123663103c2e92eca66a12c7ee9c5f94 /clang/lib/Frontend/HTMLPrint.cpp | |
parent | f22439a7097646e40251a41f4ae0139baa1885e2 (diff) | |
download | bcm5719-llvm-9f30fc33d936877f8dca3b577550680edc0548cd.tar.gz bcm5719-llvm-9f30fc33d936877f8dca3b577550680edc0548cd.zip |
Move ASTConsumers.h to include/clang/Frontend, and move the associated
.cpp files to lib/Frontend. (As proposed on cfe-dev.)
llvm-svn: 72060
Diffstat (limited to 'clang/lib/Frontend/HTMLPrint.cpp')
-rw-r--r-- | clang/lib/Frontend/HTMLPrint.cpp | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/clang/lib/Frontend/HTMLPrint.cpp b/clang/lib/Frontend/HTMLPrint.cpp new file mode 100644 index 00000000000..a4ce9e522df --- /dev/null +++ b/clang/lib/Frontend/HTMLPrint.cpp @@ -0,0 +1,83 @@ +//===--- HTMLPrint.cpp - Source code -> HTML pretty-printing --------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// Pretty-printing of source code to HTML. +// +//===----------------------------------------------------------------------===// + +#include "clang/Frontend/ASTConsumers.h" +#include "clang/AST/ASTConsumer.h" +#include "clang/AST/Decl.h" +#include "clang/Rewrite/Rewriter.h" +#include "clang/Rewrite/HTMLRewrite.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Basic/FileManager.h" +#include "clang/AST/ASTContext.h" + +using namespace clang; + +//===----------------------------------------------------------------------===// +// Functional HTML pretty-printing. +//===----------------------------------------------------------------------===// + +namespace { + class HTMLPrinter : public ASTConsumer { + Rewriter R; + llvm::raw_ostream *Out; + Diagnostic &Diags; + Preprocessor *PP; + PreprocessorFactory *PPF; + public: + HTMLPrinter(llvm::raw_ostream *OS, Diagnostic &D, Preprocessor *pp, + PreprocessorFactory* ppf) + : Out(OS), Diags(D), PP(pp), PPF(ppf) {} + virtual ~HTMLPrinter(); + + void Initialize(ASTContext &context); + }; +} + +ASTConsumer* clang::CreateHTMLPrinter(llvm::raw_ostream *OS, + Diagnostic &D, Preprocessor *PP, + PreprocessorFactory* PPF) { + + return new HTMLPrinter(OS, D, PP, PPF); +} + +void HTMLPrinter::Initialize(ASTContext &context) { + R.setSourceMgr(context.getSourceManager(), context.getLangOptions()); +} + +HTMLPrinter::~HTMLPrinter() { + if (Diags.hasErrorOccurred()) + return; + + // Format the file. + FileID FID = R.getSourceMgr().getMainFileID(); + const FileEntry* Entry = R.getSourceMgr().getFileEntryForID(FID); + + html::AddLineNumbers(R, FID); + html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName()); + + // If we have a preprocessor, relex the file and syntax highlight. + // We might not have a preprocessor if we come from a deserialized AST file, + // for example. + + if (PP) html::SyntaxHighlight(R, FID, *PP); + if (PPF) html::HighlightMacros(R, FID, *PP); + html::EscapeText(R, FID, false, true); + + // Emit the HTML. + const RewriteBuffer &RewriteBuf = R.getEditBuffer(FID); + char *Buffer = (char*)malloc(RewriteBuf.size()); + std::copy(RewriteBuf.begin(), RewriteBuf.end(), Buffer); + Out->write(Buffer, RewriteBuf.size()); + free(Buffer); +} |