From 8ac661c3c5ffaeedfb3268994ad864ade77b3ba0 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Wed, 16 Apr 2008 05:21:09 +0000 Subject: [PATCH] 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 --- Driver/ASTConsumers.h | 2 +- Driver/HTMLPrint.cpp | 40 +++++++++++++++++++++++++-------- Driver/clang.cpp | 8 ++++--- clang.xcodeproj/project.pbxproj | 2 +- 4 files changed, 38 insertions(+), 14 deletions(-) diff --git a/Driver/ASTConsumers.h b/Driver/ASTConsumers.h index 30dac34292..91ee8fbf93 100644 --- a/Driver/ASTConsumers.h +++ b/Driver/ASTConsumers.h @@ -57,7 +57,7 @@ ASTConsumer *CreateCodeRewriterTest(const std::string& InFile, Diagnostic &Diags, const LangOptions &LOpts); -ASTConsumer* CreateHTMLPrinter(); + ASTConsumer* CreateHTMLPrinter(const std::string &OutFile, Diagnostic &D); ASTConsumer *CreateSerializationTest(Diagnostic &Diags, FileManager& FMgr, 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); } diff --git a/Driver/clang.cpp b/Driver/clang.cpp index 7a2996a309..09140c942e 100644 --- a/Driver/clang.cpp +++ b/Driver/clang.cpp @@ -918,8 +918,10 @@ static void InitializeIncludePaths(const char *Argv0, HeaderSearch &Headers, // Fedora 8 AddPath("/usr/include/c++/4.1.2", System, true, false, false, Headers); - AddPath("/usr/include/c++/4.1.2/i386-redhat-linux", System, true, false, false, Headers); - AddPath("/usr/include/c++/4.1.2/backward", System, true, false, false, Headers); + AddPath("/usr/include/c++/4.1.2/i386-redhat-linux", System, true, false, + false, Headers); + AddPath("/usr/include/c++/4.1.2/backward", System, true, false, false, + Headers); } AddPath("/usr/local/include", System, false, false, false, Headers); @@ -1046,7 +1048,7 @@ static ASTConsumer* CreateASTConsumer(const std::string& InFile, return CreateASTViewer(); case EmitHTML: - return CreateHTMLPrinter(); + return CreateHTMLPrinter(OutputFile, Diag); case ParseCFGDump: case ParseCFGView: diff --git a/clang.xcodeproj/project.pbxproj b/clang.xcodeproj/project.pbxproj index 889cab2249..c7f2e14b9a 100644 --- a/clang.xcodeproj/project.pbxproj +++ b/clang.xcodeproj/project.pbxproj @@ -720,7 +720,6 @@ DEAEECAE0A5AF0FA0045101B /* Driver */ = { isa = PBXGroup; children = ( - 035611E10DB40C8100D2EF2A /* RewriteObjC.cpp */, DE5932CD0AD60FF400BC794C /* clang.cpp */, DE5932CE0AD60FF400BC794C /* clang.h */, DE3985780CB8ADC800223765 /* ASTConsumers.h */, @@ -732,6 +731,7 @@ 3574BC290D9B531D00DF491A /* HTMLDiagnostics.h */, 3574BC2A0D9B531D00DF491A /* HTMLDiagnostics.cpp */, 72D16C210D9975EA00E6DA4A /* HTMLPrint.cpp */, + 035611E10DB40C8100D2EF2A /* RewriteObjC.cpp */, DEB0AEBA0C2087AB00718A22 /* TextDiagnostics.cpp */, DEB0AEB80C2087A700718A22 /* TextDiagnostics.h */, F0226FD00C18084500141F42 /* TextDiagnosticPrinter.cpp */, -- 2.40.0