aboutsummaryrefslogtreecommitdiff
path: root/Driver/HTMLPrint.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2008-04-16 05:21:09 +0000
committerChris Lattner <sabre@nondot.org>2008-04-16 05:21:09 +0000
commit8ac661c3c5ffaeedfb3268994ad864ade77b3ba0 (patch)
tree2eccd403417c19214e99b27c56f2f3d257757a8a /Driver/HTMLPrint.cpp
parentfdbe6799cae249c8b29cb7f7d4032d2f2468df06 (diff)
Add -o support for -emit-html, make it not produce a file on an error.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@49777 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'Driver/HTMLPrint.cpp')
-rw-r--r--Driver/HTMLPrint.cpp40
1 files changed, 31 insertions, 9 deletions
diff --git a/Driver/HTMLPrint.cpp b/Driver/HTMLPrint.cpp
index a0a76a566e..44cd6244ec 100644
--- a/Driver/HTMLPrint.cpp
+++ b/Driver/HTMLPrint.cpp
@@ -15,6 +15,7 @@
#include "clang/AST/ASTConsumer.h"
#include "clang/Rewrite/Rewriter.h"
#include "clang/Rewrite/HTMLRewrite.h"
+#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/SourceManager.h"
#include "clang/AST/ASTContext.h"
@@ -27,33 +28,54 @@ using namespace clang;
namespace {
class HTMLPrinter : public ASTConsumer {
Rewriter R;
+ std::string OutFilename;
+ Diagnostic &Diags;
public:
- HTMLPrinter() {}
+ HTMLPrinter(const std::string &OutFile, Diagnostic &D)
+ : OutFilename(OutFile), Diags(D) {}
virtual ~HTMLPrinter();
void Initialize(ASTContext &context);
};
}
-ASTConsumer* clang::CreateHTMLPrinter() { return new HTMLPrinter(); }
+ASTConsumer* clang::CreateHTMLPrinter(const std::string &OutFile,
+ Diagnostic &D) {
+ return new HTMLPrinter(OutFile, D);
+}
void HTMLPrinter::Initialize(ASTContext &context) {
R.setSourceMgr(context.getSourceManager());
}
HTMLPrinter::~HTMLPrinter() {
-
+ if (Diags.hasErrorOccurred())
+ return;
+
+ // Format the file.
unsigned FileID = R.getSourceMgr().getMainFileID();
html::EscapeText(R, FileID, false, true);
html::AddLineNumbers(R, FileID);
html::AddHeaderFooterInternalBuiltinCSS(R, FileID);
+
+ // Open the output.
+ FILE *OutputFILE;
+ if (OutFilename.empty() || OutFilename == "-")
+ OutputFILE = stdout;
+ else {
+ OutputFILE = fopen(OutFilename.c_str(), "w+");
+ if (OutputFILE == 0) {
+ fprintf(stderr, "Error opening output file '%s'.\n", OutFilename.c_str());
+ exit(1);
+ }
+ }
// Emit the HTML.
+ const RewriteBuffer &RewriteBuf = R.getEditBuffer(FileID);
+ char *Buffer = (char*)malloc(RewriteBuf.size());
+ std::copy(RewriteBuf.begin(), RewriteBuf.end(), Buffer);
+ fwrite(Buffer, 1, RewriteBuf.size(), OutputFILE);
+ free(Buffer);
- if (const RewriteBuffer *RewriteBuf = R.getRewriteBufferFor(FileID)) {
- char *Buffer = (char*)malloc(RewriteBuf->size());
- std::copy(RewriteBuf->begin(), RewriteBuf->end(), Buffer);
- fwrite(Buffer, 1, RewriteBuf->size(), stdout);
- free(Buffer);
- }
+ if (OutputFILE != stdout) fclose(OutputFILE);
}