[cfe-commits] r124772 - in /cfe/trunk: include/clang/Basic/DiagnosticFrontendKinds.td lib/Frontend/HeaderIncludeGen.cpp

Daniel Dunbar daniel at zuster.org
Wed Feb 2 19:45:00 PST 2011


Author: ddunbar
Date: Wed Feb  2 21:45:00 2011
New Revision: 124772

URL: http://llvm.org/viewvc/llvm-project?rev=124772&view=rev
Log:
Frontend: Switch -header-include-file output to use unbuffered raw_ostreams with
the atomic writes option, since the intent is that this option be set for an
entire build, which may have any number of compiler instances writing to the
same output file.

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticFrontendKinds.td
    cfe/trunk/lib/Frontend/HeaderIncludeGen.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticFrontendKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticFrontendKinds.td?rev=124772&r1=124771&r2=124772&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticFrontendKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticFrontendKinds.td Wed Feb  2 21:45:00 2011
@@ -77,6 +77,8 @@
     "PTH file '%0' does not designate an original source header file for -include-pth">;
 def warn_fe_macro_contains_embedded_newline : Warning<
     "macro '%0' contains embedded newline, text after the newline is ignored.">;
+def warn_fe_cc_print_header_failure : Warning<
+    "unable to open CC_PRINT_HEADERS file: %0 (using stderr)">;
 
 def err_verify_missing_start : Error<
     "cannot find start ('{{') of expected %0">;

Modified: cfe/trunk/lib/Frontend/HeaderIncludeGen.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/HeaderIncludeGen.cpp?rev=124772&r1=124771&r2=124772&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/HeaderIncludeGen.cpp (original)
+++ cfe/trunk/lib/Frontend/HeaderIncludeGen.cpp Wed Feb  2 21:45:00 2011
@@ -9,14 +9,15 @@
 
 #include "clang/Frontend/Utils.h"
 #include "clang/Basic/SourceManager.h"
+#include "clang/Frontend/FrontendDiagnostic.h"
 #include "clang/Lex/Preprocessor.h"
-#include <cstdio>
+#include "llvm/Support/raw_ostream.h"
 using namespace clang;
 
 namespace {
 class HeaderIncludesCallback : public PPCallbacks {
   SourceManager &SM;
-  FILE *OutputFile;
+  llvm::raw_ostream *OutputFile;
   unsigned CurrentIncludeDepth;
   bool HasProcessedPredefines;
   bool OwnsOutputFile;
@@ -24,14 +25,14 @@
 
 public:
   HeaderIncludesCallback(const Preprocessor *PP, bool ShowAllHeaders_,
-                         FILE *OutputFile_, bool OwnsOutputFile_)
+                         llvm::raw_ostream *OutputFile_, bool OwnsOutputFile_)
     : SM(PP->getSourceManager()), OutputFile(OutputFile_),
       CurrentIncludeDepth(0), HasProcessedPredefines(false),
       OwnsOutputFile(OwnsOutputFile_), ShowAllHeaders(ShowAllHeaders_) {}
 
   ~HeaderIncludesCallback() {
     if (OwnsOutputFile)
-      fclose(OutputFile);
+      delete OutputFile;
   }
 
   virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
@@ -41,16 +42,24 @@
 
 void clang::AttachHeaderIncludeGen(Preprocessor &PP, bool ShowAllHeaders,
                                    llvm::StringRef OutputPath) {
-  FILE *OutputFile;
-  bool OwnsOutputFile;
+  llvm::raw_ostream *OutputFile = &llvm::errs();
+  bool OwnsOutputFile = false;
 
   // Open the output file, if used.
-  if (OutputPath.empty()) {
-    OutputFile = stderr;
-    OwnsOutputFile = false;
-  } else {
-    OutputFile = fopen(OutputPath.str().c_str(), "a");
-    OwnsOutputFile = true;
+  if (!OutputPath.empty()) {
+    std::string Error;
+    llvm::raw_fd_ostream *OS = new llvm::raw_fd_ostream(
+      OutputPath.str().c_str(), Error, llvm::raw_fd_ostream::F_Append);
+    if (!Error.empty()) {
+      PP.getDiagnostics().Report(
+        clang::diag::warn_fe_cc_print_header_failure) << Error;
+      delete OS;
+    } else {
+      OS->SetUnbuffered();
+      OS->SetUseAtomicWrites(true);
+      OutputFile = OS;
+      OwnsOutputFile = true;
+    }
   }
 
   PP.addPPCallbacks(new HeaderIncludesCallback(&PP, ShowAllHeaders,
@@ -99,7 +108,6 @@
     Msg += Filename;
     Msg += '\n';
 
-    fwrite(Msg.data(), Msg.size(), 1, OutputFile);
+    OutputFile->write(Msg.data(), Msg.size());
   }
 }
-





More information about the cfe-commits mailing list