[cfe-commits] r129089 - in /cfe/trunk: include/clang/Frontend/LogDiagnosticPrinter.h lib/Frontend/CMakeLists.txt lib/Frontend/CompilerInstance.cpp lib/Frontend/LogDiagnosticPrinter.cpp

Daniel Dunbar daniel at zuster.org
Thu Apr 7 11:31:10 PDT 2011


Author: ddunbar
Date: Thu Apr  7 13:31:10 2011
New Revision: 129089

URL: http://llvm.org/viewvc/llvm-project?rev=129089&view=rev
Log:
Frontend: Sketch a LogDiagnosticPrinter object, and wire CC_LOG_DIAGNOSTICS to
it.

Added:
    cfe/trunk/include/clang/Frontend/LogDiagnosticPrinter.h
    cfe/trunk/lib/Frontend/LogDiagnosticPrinter.cpp
Modified:
    cfe/trunk/lib/Frontend/CMakeLists.txt
    cfe/trunk/lib/Frontend/CompilerInstance.cpp

Added: cfe/trunk/include/clang/Frontend/LogDiagnosticPrinter.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/LogDiagnosticPrinter.h?rev=129089&view=auto
==============================================================================
--- cfe/trunk/include/clang/Frontend/LogDiagnosticPrinter.h (added)
+++ cfe/trunk/include/clang/Frontend/LogDiagnosticPrinter.h Thu Apr  7 13:31:10 2011
@@ -0,0 +1,48 @@
+//===--- LogDiagnosticPrinter.h - Log Diagnostic Client ---------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_FRONTEND_LOG_DIAGNOSTIC_PRINTER_H_
+#define LLVM_CLANG_FRONTEND_LOG_DIAGNOSTIC_PRINTER_H_
+
+#include "clang/Basic/Diagnostic.h"
+#include "clang/Basic/SourceLocation.h"
+
+namespace clang {
+class DiagnosticOptions;
+class LangOptions;
+
+class LogDiagnosticPrinter : public DiagnosticClient {
+  llvm::raw_ostream &OS;
+  const LangOptions *LangOpts;
+  const DiagnosticOptions *DiagOpts;
+
+  SourceLocation LastWarningLoc;
+  FullSourceLoc LastLoc;
+  unsigned OwnsOutputStream : 1;
+
+public:
+  LogDiagnosticPrinter(llvm::raw_ostream &OS, const DiagnosticOptions &Diags,
+                       bool OwnsOutputStream = false);
+  virtual ~LogDiagnosticPrinter();
+
+  void BeginSourceFile(const LangOptions &LO, const Preprocessor *PP) {
+    LangOpts = &LO;
+  }
+
+  void EndSourceFile() {
+    LangOpts = 0;
+  }
+
+  virtual void HandleDiagnostic(Diagnostic::Level DiagLevel,
+                                const DiagnosticInfo &Info);
+};
+
+} // end namespace clang
+
+#endif

Modified: cfe/trunk/lib/Frontend/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CMakeLists.txt?rev=129089&r1=129088&r2=129089&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/CMakeLists.txt (original)
+++ cfe/trunk/lib/Frontend/CMakeLists.txt Thu Apr  7 13:31:10 2011
@@ -26,6 +26,7 @@
   InitHeaderSearch.cpp
   InitPreprocessor.cpp
   LangStandards.cpp
+  LogDiagnosticPrinter.cpp
   MultiplexConsumer.cpp
   PrintPreprocessedOutput.cpp
   TextDiagnosticBuffer.cpp

Modified: cfe/trunk/lib/Frontend/CompilerInstance.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CompilerInstance.cpp?rev=129089&r1=129088&r2=129089&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/CompilerInstance.cpp (original)
+++ cfe/trunk/lib/Frontend/CompilerInstance.cpp Thu Apr  7 13:31:10 2011
@@ -22,6 +22,7 @@
 #include "clang/Frontend/ChainedDiagnosticClient.h"
 #include "clang/Frontend/FrontendAction.h"
 #include "clang/Frontend/FrontendDiagnostic.h"
+#include "clang/Frontend/LogDiagnosticPrinter.h"
 #include "clang/Frontend/TextDiagnosticPrinter.h"
 #include "clang/Frontend/VerifyDiagnosticsClient.h"
 #include "clang/Frontend/Utils.h"
@@ -106,6 +107,33 @@
   Diags.setClient(new ChainedDiagnosticClient(Diags.takeClient(), Logger));
 }
 
+static void SetUpDiagnosticLog(const DiagnosticOptions &DiagOpts,
+                               Diagnostic &Diags) {
+  std::string ErrorInfo;
+  bool OwnsStream = false;
+  llvm::raw_ostream *OS = &llvm::errs();
+  if (DiagOpts.DiagnosticLogFile != "-") {
+    // Create the output stream.
+    llvm::raw_fd_ostream *FileOS(
+      new llvm::raw_fd_ostream(DiagOpts.DiagnosticLogFile.c_str(),
+                               ErrorInfo));
+    if (!ErrorInfo.empty()) {
+      Diags.Report(diag::warn_fe_cc_log_diagnostics_failure)
+        << DiagOpts.DumpBuildInformation << ErrorInfo;
+    } else {
+      FileOS->SetUnbuffered();
+      FileOS->SetUseAtomicWrites(true);
+      OS = FileOS;
+      OwnsStream = true;
+    }
+  }
+
+  // Chain in the diagnostic client which will log the diagnostics.
+  DiagnosticClient *Logger = new LogDiagnosticPrinter(*OS, DiagOpts,
+                                                      OwnsStream);
+  Diags.setClient(new ChainedDiagnosticClient(Diags.takeClient(), Logger));
+}
+
 void CompilerInstance::createDiagnostics(int Argc, const char* const *Argv,
                                          DiagnosticClient *Client) {
   Diagnostics = createDiagnostics(getDiagnosticOpts(), Argc, Argv, Client);
@@ -129,6 +157,10 @@
   if (Opts.VerifyDiagnostics)
     Diags->setClient(new VerifyDiagnosticsClient(*Diags, Diags->takeClient()));
 
+  // Chain in -diagnostic-log-file dumper, if requested.
+  if (!Opts.DiagnosticLogFile.empty())
+    SetUpDiagnosticLog(Opts, *Diags);
+  
   if (!Opts.DumpBuildInformation.empty())
     SetUpBuildDumpLog(Opts, Argc, Argv, *Diags);
 

Added: cfe/trunk/lib/Frontend/LogDiagnosticPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/LogDiagnosticPrinter.cpp?rev=129089&view=auto
==============================================================================
--- cfe/trunk/lib/Frontend/LogDiagnosticPrinter.cpp (added)
+++ cfe/trunk/lib/Frontend/LogDiagnosticPrinter.cpp Thu Apr  7 13:31:10 2011
@@ -0,0 +1,39 @@
+//===--- LogDiagnosticPrinter.cpp - Log Diagnostic Printer ----------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Frontend/LogDiagnosticPrinter.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/Support/raw_ostream.h"
+using namespace clang;
+
+LogDiagnosticPrinter::LogDiagnosticPrinter(llvm::raw_ostream &os,
+                                           const DiagnosticOptions &diags,
+                                           bool _OwnsOutputStream)
+  : OS(os), LangOpts(0), DiagOpts(&diags),
+    OwnsOutputStream(_OwnsOutputStream) {
+}
+
+LogDiagnosticPrinter::~LogDiagnosticPrinter() {
+  if (OwnsOutputStream)
+    delete &OS;
+}
+
+void LogDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level,
+                                             const DiagnosticInfo &Info) {
+  // Default implementation (Warnings/errors count).
+  DiagnosticClient::HandleDiagnostic(Level, Info);
+
+  // Write to a temporary string to ensure atomic write of diagnostic object.
+  llvm::SmallString<512> Msg;
+  llvm::raw_svector_ostream OS(Msg);
+
+  OS << "hello!\n";
+
+  this->OS << OS.str();
+}





More information about the cfe-commits mailing list