]> granicus.if.org Git - clang/commitdiff
Add -o support for -emit-html, make it not produce a file on an error.
authorChris Lattner <sabre@nondot.org>
Wed, 16 Apr 2008 05:21:09 +0000 (05:21 +0000)
committerChris Lattner <sabre@nondot.org>
Wed, 16 Apr 2008 05:21:09 +0000 (05:21 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@49777 91177308-0d34-0410-b5e6-96231b3b80d8

Driver/ASTConsumers.h
Driver/HTMLPrint.cpp
Driver/clang.cpp
clang.xcodeproj/project.pbxproj

index 30dac34292f1b1ac704ce3cedd2d7b79b9de2209..91ee8fbf938fc85dc4b2dab6a91409c63803d7d6 100644 (file)
@@ -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, 
index a0a76a566e76043d41b42d622936f5604566b23a..44cd6244ecccf2aea7684bcf3ec65d719ca533a8 100644 (file)
@@ -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);
 }
index 7a2996a30981cf379c82e3248ab081de780d5479..09140c942e0dfba21d6ccabbd9accc24ec335a9d 100644 (file)
@@ -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:
index 889cab22498557de69694c10500af8257da14c52..c7f2e14b9a94729ae1e87c5e08c97760f6405cab 100644 (file)
                DEAEECAE0A5AF0FA0045101B /* Driver */ = {
                        isa = PBXGroup;
                        children = (
-                               035611E10DB40C8100D2EF2A /* RewriteObjC.cpp */,
                                DE5932CD0AD60FF400BC794C /* clang.cpp */,
                                DE5932CE0AD60FF400BC794C /* clang.h */,
                                DE3985780CB8ADC800223765 /* ASTConsumers.h */,
                                3574BC290D9B531D00DF491A /* HTMLDiagnostics.h */,
                                3574BC2A0D9B531D00DF491A /* HTMLDiagnostics.cpp */,
                                72D16C210D9975EA00E6DA4A /* HTMLPrint.cpp */,
+                               035611E10DB40C8100D2EF2A /* RewriteObjC.cpp */,
                                DEB0AEBA0C2087AB00718A22 /* TextDiagnostics.cpp */,
                                DEB0AEB80C2087A700718A22 /* TextDiagnostics.h */,
                                F0226FD00C18084500141F42 /* TextDiagnosticPrinter.cpp */,