[cfe-commits] r96823 - in /cfe/trunk: include/clang-c/Index.h tools/CIndex/CIndex.cpp tools/CIndex/CIndex.exports tools/CIndex/CIndexDiagnostic.cpp tools/c-index-test/c-index-test.c

Douglas Gregor dgregor at apple.com
Mon Feb 22 15:17:23 PST 2010


Author: dgregor
Date: Mon Feb 22 17:17:23 2010
New Revision: 96823

URL: http://llvm.org/viewvc/llvm-project?rev=96823&view=rev
Log:
Rework the CIndex API for displaying diagnostics. Instead of printing
the diagnostics to a FILE*, return a CXString containing the formatted
diagnostic.

Modified:
    cfe/trunk/include/clang-c/Index.h
    cfe/trunk/tools/CIndex/CIndex.cpp
    cfe/trunk/tools/CIndex/CIndex.exports
    cfe/trunk/tools/CIndex/CIndexDiagnostic.cpp
    cfe/trunk/tools/c-index-test/c-index-test.c

Modified: cfe/trunk/include/clang-c/Index.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=96823&r1=96822&r2=96823&view=diff
==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Mon Feb 22 17:17:23 2010
@@ -460,23 +460,22 @@
 };
 
 /**
- * \brief Display the given diagnostic by printing it to the given file.
+ * \brief Format the given diagnostic in a manner that is suitable for display.
  *
- * This routine will display the given diagnostic to a file, rendering
+ * This routine will format the given diagnostic to a string, rendering
  * the diagnostic according to the various options given. The 
  * \c clang_defaultDiagnosticDisplayOptions() function returns the set of 
  * options that most closely mimics the behavior of the clang compiler.
  *
  * \param Diagnostic The diagnostic to print.
  *
- * \param File The file to print to (e.g., \c stderr).
- *
  * \param Options A set of options that control the diagnostic display, 
  * created by combining \c CXDiagnosticDisplayOptions values.
+ *
+ * \returns A new string containing for formatted diagnostic.
  */
-CINDEX_LINKAGE void clang_displayDiagnostic(CXDiagnostic Diagnostic,
-                                            FILE *File,
-                                            unsigned Options);
+CINDEX_LINKAGE CXString clang_formatDiagnostic(CXDiagnostic Diagnostic,
+                                               unsigned Options);
 
 /**
  * \brief Retrieve the set of display options most similar to the

Modified: cfe/trunk/tools/CIndex/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndex.cpp?rev=96823&r1=96822&r2=96823&view=diff
==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndex.cpp Mon Feb 22 17:17:23 2010
@@ -1007,9 +1007,17 @@
                                  DEnd = Unit->diag_end();
              D != DEnd; ++D) {
           CXStoredDiagnostic Diag(*D, Unit->getASTContext().getLangOptions());
-          clang_displayDiagnostic(&Diag, stderr,
-                                  clang_defaultDiagnosticDisplayOptions());
+          CXString Msg = clang_formatDiagnostic(&Diag,
+                                      clang_defaultDiagnosticDisplayOptions());
+          fprintf(stderr, "%s\n", clang_getCString(Msg));
+          clang_disposeString(Msg);
         }
+#ifdef LLVM_ON_WIN32
+        // On Windows, force a flush, since there may be multiple copies of
+        // stderr and stdout in the file system, all with different buffers
+        // but writing to the same device.
+        fflush(stderr);
+#endif        
       }
       return 0;
     }
@@ -1124,9 +1132,18 @@
                                                        DEnd = Diags.end();
          D != DEnd; ++D) {
       CXStoredDiagnostic Diag(*D, LangOpts);
-      clang_displayDiagnostic(&Diag, stderr,
-                              clang_defaultDiagnosticDisplayOptions());
+      CXString Msg = clang_formatDiagnostic(&Diag,
+                                      clang_defaultDiagnosticDisplayOptions());
+      fprintf(stderr, "%s\n", clang_getCString(Msg));
+      clang_disposeString(Msg);
     }
+    
+#ifdef LLVM_ON_WIN32
+    // On Windows, force a flush, since there may be multiple copies of
+    // stderr and stdout in the file system, all with different buffers
+    // but writing to the same device.
+    fflush(stderr);
+#endif    
   }
 
   if (ATU) {

Modified: cfe/trunk/tools/CIndex/CIndex.exports
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndex.exports?rev=96823&r1=96822&r2=96823&view=diff
==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.exports (original)
+++ cfe/trunk/tools/CIndex/CIndex.exports Mon Feb 22 17:17:23 2010
@@ -6,7 +6,6 @@
 _clang_createTranslationUnit
 _clang_createTranslationUnitFromSourceFile
 _clang_defaultDiagnosticDisplayOptions
-_clang_displayDiagnostic
 _clang_disposeCodeCompleteResults
 _clang_disposeDiagnostic
 _clang_disposeIndex
@@ -16,6 +15,7 @@
 _clang_enableStackTraces
 _clang_equalCursors
 _clang_equalLocations
+_clang_formatDiagnostic
 _clang_getClangVersion
 _clang_getCString
 _clang_getCompletionChunkCompletionString

Modified: cfe/trunk/tools/CIndex/CIndexDiagnostic.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndexDiagnostic.cpp?rev=96823&r1=96822&r2=96823&view=diff
==============================================================================
--- cfe/trunk/tools/CIndex/CIndexDiagnostic.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndexDiagnostic.cpp Mon Feb 22 17:17:23 2010
@@ -15,8 +15,10 @@
 #include "CXSourceLocation.h"
 
 #include "clang/Frontend/FrontendDiagnostic.h"
+#include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/Twine.h"
 #include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/raw_ostream.h"
 
 using namespace clang;
 using namespace clang::cxloc;
@@ -47,17 +49,19 @@
   delete Stored;
 }
 
-void clang_displayDiagnostic(CXDiagnostic Diagnostic, FILE *Out, 
-                             unsigned Options) {
-  if (!Diagnostic || !Out)
-    return;
+CXString clang_formatDiagnostic(CXDiagnostic Diagnostic, unsigned Options) {
+  if (!Diagnostic)
+    return createCXString("");
 
   CXDiagnosticSeverity Severity = clang_getDiagnosticSeverity(Diagnostic);
 
   // Ignore diagnostics that should be ignored.
   if (Severity == CXDiagnostic_Ignored)
-    return;
+    return createCXString("");
 
+  llvm::SmallString<256> Str;
+  llvm::raw_svector_ostream Out(Str);
+  
   if (Options & CXDiagnostic_DisplaySourceLocation) {
     // Print source location (file:line), along with optional column
     // and source ranges.
@@ -67,10 +71,10 @@
                                    &File, &Line, &Column, 0);
     if (File) {
       CXString FName = clang_getFileName(File);
-      fprintf(Out, "%s:%d:", clang_getCString(FName), Line);
+      Out << clang_getCString(FName) << ":" << Line << ":";
       clang_disposeString(FName);
       if (Options & CXDiagnostic_DisplayColumn)
-        fprintf(Out, "%d:", Column);
+        Out << Column << ":";
 
       if (Options & CXDiagnostic_DisplaySourceRanges) {
         unsigned N = clang_getDiagnosticNumRanges(Diagnostic);
@@ -89,40 +93,34 @@
           if (StartFile != EndFile || StartFile != File)
             continue;
           
-          fprintf(Out, "{%d:%d-%d:%d}", StartLine, StartColumn, 
-                  EndLine, EndColumn);
+          Out << "{" << StartLine << ":" << StartColumn << "-"
+              << EndLine << ":" << EndColumn << "}";
           PrintedRange = true;
         }
         if (PrintedRange)
-          fprintf(Out, ":");
+          Out << ":";
       }
     }
 
-    fprintf(Out, " ");
+    Out << " ";
   }
 
   /* Print warning/error/etc. */
   switch (Severity) {
   case CXDiagnostic_Ignored: assert(0 && "impossible"); break;
-  case CXDiagnostic_Note: fprintf(Out, "note: "); break;
-  case CXDiagnostic_Warning: fprintf(Out, "warning: "); break;
-  case CXDiagnostic_Error: fprintf(Out, "error: "); break;
-  case CXDiagnostic_Fatal: fprintf(Out, "fatal error: "); break;
+  case CXDiagnostic_Note: Out << "note: "; break;
+  case CXDiagnostic_Warning: Out << "warning: "; break;
+  case CXDiagnostic_Error: Out << "error: "; break;
+  case CXDiagnostic_Fatal: Out << "fatal error: "; break;
   }
 
   CXString Text = clang_getDiagnosticSpelling(Diagnostic);
   if (clang_getCString(Text))
-    fprintf(Out, "%s\n", clang_getCString(Text));
+    Out << clang_getCString(Text);
   else
-    fprintf(Out, "<no diagnostic text>\n");
+    Out << "<no diagnostic text>";
   clang_disposeString(Text);
-
-#ifdef LLVM_ON_WIN32
-  // On Windows, force a flush, since there may be multiple copies of
-  // stderr and stdout in the file system, all with different buffers
-  // but writing to the same device.
-  fflush(Out);
-#endif
+  return createCXString(Out.str(), true);
 }
 
 unsigned clang_defaultDiagnosticDisplayOptions() {

Modified: cfe/trunk/tools/c-index-test/c-index-test.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/c-index-test/c-index-test.c?rev=96823&r1=96822&r2=96823&view=diff
==============================================================================
--- cfe/trunk/tools/c-index-test/c-index-test.c (original)
+++ cfe/trunk/tools/c-index-test/c-index-test.c Mon Feb 22 17:17:23 2010
@@ -198,14 +198,18 @@
 void PrintDiagnostic(CXDiagnostic Diagnostic) {
   FILE *out = stderr;
   CXFile file;
+  CXString Msg;
   unsigned display_opts = CXDiagnostic_DisplaySourceLocation
     | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges;
   unsigned i, num_fixits;
-
-  clang_displayDiagnostic(Diagnostic, out, display_opts);
+  
   if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
     return;
 
+  Msg = clang_formatDiagnostic(Diagnostic, display_opts);
+  fprintf(stderr, "%s\n", clang_getCString(Msg));
+  clang_disposeString(Msg);
+  
   clang_getInstantiationLocation(clang_getDiagnosticLocation(Diagnostic),
                                  &file, 0, 0, 0);
   if (!file)





More information about the cfe-commits mailing list