};
/**
- * \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
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;
}
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) {
#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;
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.
&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);
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() {