summaryrefslogtreecommitdiffstats
path: root/clang/tools/CIndex/CIndexDiagnostic.cpp
diff options
context:
space:
mode:
authorDouglas Gregor <dgregor@apple.com>2010-02-18 19:08:21 +0000
committerDouglas Gregor <dgregor@apple.com>2010-02-18 19:08:21 +0000
commit06dcf0375b6e4f6c5c63e79b4c644c96c8c2ce7a (patch)
treecc72d985de786cd9e77f0e973857cca2de1839b1 /clang/tools/CIndex/CIndexDiagnostic.cpp
parentfcb3db7d689d6f91e240f60714c5bc334fe34896 (diff)
downloadbcm5719-llvm-06dcf0375b6e4f6c5c63e79b4c644c96c8c2ce7a.tar.gz
bcm5719-llvm-06dcf0375b6e4f6c5c63e79b4c644c96c8c2ce7a.zip
Introduce CIndex API functions for displaying a diagnostic, with some
knobs to control formatting. Eventually, I'd like to merge the implementation of this code with the TextDiagnosticPrinter, so that it's easy for CIndex clients to produce beautiful diagnostics like the clang compiler does. Use this new function to display diagnostics within c-index-test. llvm-svn: 96603
Diffstat (limited to 'clang/tools/CIndex/CIndexDiagnostic.cpp')
-rw-r--r--clang/tools/CIndex/CIndexDiagnostic.cpp75
1 files changed, 75 insertions, 0 deletions
diff --git a/clang/tools/CIndex/CIndexDiagnostic.cpp b/clang/tools/CIndex/CIndexDiagnostic.cpp
index 70676f0e141..2933e6fd259 100644
--- a/clang/tools/CIndex/CIndexDiagnostic.cpp
+++ b/clang/tools/CIndex/CIndexDiagnostic.cpp
@@ -47,6 +47,81 @@ void clang_disposeDiagnostic(CXDiagnostic Diagnostic) {
delete Stored;
}
+void clang_displayDiagnostic(CXDiagnostic Diagnostic, FILE *Out,
+ unsigned Options) {
+ if (!Diagnostic || !Out)
+ return;
+
+ CXDiagnosticSeverity Severity = clang_getDiagnosticSeverity(Diagnostic);
+
+ // Ignore diagnostics that should be ignored.
+ if (Severity == CXDiagnostic_Ignored)
+ return;
+
+ if (Options & CXDiagnostic_DisplaySourceLocation) {
+ // Print source location (file:line), along with optional column
+ // and source ranges.
+ CXFile File;
+ unsigned Line, Column;
+ clang_getInstantiationLocation(clang_getDiagnosticLocation(Diagnostic),
+ &File, &Line, &Column, 0);
+ if (File) {
+ CXString FName = clang_getFileName(File);
+ fprintf(Out, "%s:%d:", clang_getCString(FName), Line);
+ clang_disposeString(FName);
+ if (Options & CXDiagnostic_DisplayColumn)
+ fprintf(Out, "%d:", Column);
+
+ if (Options & CXDiagnostic_DisplaySourceRanges) {
+ unsigned N = clang_getDiagnosticNumRanges(Diagnostic);
+ bool PrintedRange = false;
+ for (unsigned I = 0; I != N; ++I) {
+ CXFile StartFile, EndFile;
+ CXSourceRange Range = clang_getDiagnosticRange(Diagnostic, I);
+
+ unsigned StartLine, StartColumn, EndLine, EndColumn;
+ clang_getInstantiationLocation(clang_getRangeStart(Range),
+ &StartFile, &StartLine, &StartColumn,
+ 0);
+ clang_getInstantiationLocation(clang_getRangeEnd(Range),
+ &EndFile, &EndLine, &EndColumn, 0);
+
+ if (StartFile != EndFile || StartFile != File)
+ continue;
+
+ fprintf(Out, "{%d:%d-%d:%d}", StartLine, StartColumn,
+ EndLine, EndColumn);
+ PrintedRange = true;
+ }
+ if (PrintedRange)
+ fprintf(Out, ":");
+ }
+ }
+
+ fprintf(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;
+ }
+
+ CXString Text = clang_getDiagnosticSpelling(Diagnostic);
+ if (clang_getCString(Text))
+ fprintf(Out, "%s\n", clang_getCString(Text));
+ else
+ fprintf(Out, "<no diagnostic text>\n");
+ clang_disposeString(Text);
+}
+
+unsigned clang_defaultDiagnosticDisplayOptions() {
+ return CXDiagnostic_DisplaySourceLocation | CXDiagnostic_DisplayColumn;
+}
+
enum CXDiagnosticSeverity clang_getDiagnosticSeverity(CXDiagnostic Diag) {
CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
if (!StoredDiag)
OpenPOWER on IntegriCloud