aboutsummaryrefslogtreecommitdiff
path: root/tools
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
commit4c58923ca904733d588ddb1cec58549b6bc7dfa9 (patch)
treeccd6bd7ac9795850ca25ad5919f13a822d3c1c61 /tools
parentc4174cc4b9b657abb77d0825de473ea29cf48297 (diff)
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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@96603 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools')
-rw-r--r--tools/CIndex/CIndex.exports2
-rw-r--r--tools/CIndex/CIndexDiagnostic.cpp75
-rw-r--r--tools/c-index-test/c-index-test.c159
3 files changed, 133 insertions, 103 deletions
diff --git a/tools/CIndex/CIndex.exports b/tools/CIndex/CIndex.exports
index 1c445b74aa..0b4ef21bac 100644
--- a/tools/CIndex/CIndex.exports
+++ b/tools/CIndex/CIndex.exports
@@ -5,6 +5,8 @@ _clang_codeCompleteGetNumDiagnostics
_clang_createIndex
_clang_createTranslationUnit
_clang_createTranslationUnitFromSourceFile
+_clang_defaultDiagnosticDisplayOptions
+_clang_displayDiagnostic
_clang_disposeCodeCompleteResults
_clang_disposeDiagnostic
_clang_disposeIndex
diff --git a/tools/CIndex/CIndexDiagnostic.cpp b/tools/CIndex/CIndexDiagnostic.cpp
index 70676f0e14..2933e6fd25 100644
--- a/tools/CIndex/CIndexDiagnostic.cpp
+++ b/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)
diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c
index b983d3a4de..99d8b5d5fa 100644
--- a/tools/c-index-test/c-index-test.c
+++ b/tools/c-index-test/c-index-test.c
@@ -198,120 +198,73 @@ typedef void (*PostVisitTU)(CXTranslationUnit);
void PrintDiagnostic(CXDiagnostic Diagnostic) {
FILE *out = stderr;
CXFile file;
- unsigned line, column;
CXString text;
- enum CXDiagnosticSeverity severity = clang_getDiagnosticSeverity(Diagnostic);
+ unsigned display_opts = CXDiagnostic_DisplaySourceLocation
+ | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges;
+ unsigned i, num_fixits;
- /* Ignore diagnostics that should be ignored. */
- if (severity == CXDiagnostic_Ignored)
+ clang_displayDiagnostic(Diagnostic, out, display_opts);
+ if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
return;
- /* Print file:line:column. */
clang_getInstantiationLocation(clang_getDiagnosticLocation(Diagnostic),
- &file, &line, &column, 0);
- if (file) {
- unsigned i, n;
- unsigned printed_any_ranges = 0;
- CXString fname;
-
- fname = clang_getFileName(file);
- fprintf(out, "%s:%d:%d:", clang_getCString(fname), line, column);
- clang_disposeString(fname);
+ &file, 0, 0, 0);
+ if (!file)
+ return;
- n = clang_getDiagnosticNumRanges(Diagnostic);
- for (i = 0; i != n; ++i) {
+ num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
+ for (i = 0; i != num_fixits; ++i) {
+ switch (clang_getDiagnosticFixItKind(Diagnostic, i)) {
+ case CXFixIt_Insertion: {
+ CXSourceLocation insertion_loc;
+ CXFile insertion_file;
+ unsigned insertion_line, insertion_column;
+ text = clang_getDiagnosticFixItInsertion(Diagnostic, i, &insertion_loc);
+ clang_getInstantiationLocation(insertion_loc, &insertion_file,
+ &insertion_line, &insertion_column, 0);
+ if (insertion_file == file)
+ fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
+ clang_getCString(text), insertion_line, insertion_column);
+ clang_disposeString(text);
+ break;
+ }
+
+ case CXFixIt_Removal: {
CXFile start_file, end_file;
- CXSourceRange range = clang_getDiagnosticRange(Diagnostic, i);
-
unsigned start_line, start_column, end_line, end_column;
- clang_getInstantiationLocation(clang_getRangeStart(range),
- &start_file, &start_line, &start_column,0);
- clang_getInstantiationLocation(clang_getRangeEnd(range),
+ CXSourceRange remove_range
+ = clang_getDiagnosticFixItRemoval(Diagnostic, i);
+ clang_getInstantiationLocation(clang_getRangeStart(remove_range),
+ &start_file, &start_line, &start_column,
+ 0);
+ clang_getInstantiationLocation(clang_getRangeEnd(remove_range),
&end_file, &end_line, &end_column, 0);
-
- if (start_file != end_file || start_file != file)
- continue;
-
- PrintExtent(out, start_line, start_column, end_line, end_column);
- printed_any_ranges = 1;
- }
- if (printed_any_ranges)
- 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;
- }
-
- 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);
-
- if (file) {
- unsigned i, num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
- for (i = 0; i != num_fixits; ++i) {
- switch (clang_getDiagnosticFixItKind(Diagnostic, i)) {
- case CXFixIt_Insertion: {
- CXSourceLocation insertion_loc;
- CXFile insertion_file;
- unsigned insertion_line, insertion_column;
- text = clang_getDiagnosticFixItInsertion(Diagnostic, i, &insertion_loc);
- clang_getInstantiationLocation(insertion_loc, &insertion_file,
- &insertion_line, &insertion_column, 0);
- if (insertion_file == file)
- fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
- clang_getCString(text), insertion_line, insertion_column);
- clang_disposeString(text);
- break;
- }
-
- case CXFixIt_Removal: {
- CXFile start_file, end_file;
- unsigned start_line, start_column, end_line, end_column;
- CXSourceRange remove_range
- = clang_getDiagnosticFixItRemoval(Diagnostic, i);
- clang_getInstantiationLocation(clang_getRangeStart(remove_range),
- &start_file, &start_line, &start_column,
- 0);
- clang_getInstantiationLocation(clang_getRangeEnd(remove_range),
- &end_file, &end_line, &end_column, 0);
- if (start_file == file && end_file == file) {
- fprintf(out, "FIX-IT: Remove ");
- PrintExtent(out, start_line, start_column, end_line, end_column);
- fprintf(out, "\n");
- }
- break;
- }
-
- case CXFixIt_Replacement: {
- CXFile start_file, end_file;
- unsigned start_line, start_column, end_line, end_column;
- CXSourceRange remove_range;
- text = clang_getDiagnosticFixItReplacement(Diagnostic, i,&remove_range);
- clang_getInstantiationLocation(clang_getRangeStart(remove_range),
- &start_file, &start_line, &start_column,
- 0);
- clang_getInstantiationLocation(clang_getRangeEnd(remove_range),
- &end_file, &end_line, &end_column, 0);
- if (start_file == end_file) {
- fprintf(out, "FIX-IT: Replace ");
- PrintExtent(out, start_line, start_column, end_line, end_column);
- fprintf(out, " with \"%s\"\n", clang_getCString(text));
- }
- clang_disposeString(text);
- break;
+ if (start_file == file && end_file == file) {
+ fprintf(out, "FIX-IT: Remove ");
+ PrintExtent(out, start_line, start_column, end_line, end_column);
+ fprintf(out, "\n");
}
+ break;
+ }
+
+ case CXFixIt_Replacement: {
+ CXFile start_file, end_file;
+ unsigned start_line, start_column, end_line, end_column;
+ CXSourceRange remove_range;
+ text = clang_getDiagnosticFixItReplacement(Diagnostic, i,&remove_range);
+ clang_getInstantiationLocation(clang_getRangeStart(remove_range),
+ &start_file, &start_line, &start_column,
+ 0);
+ clang_getInstantiationLocation(clang_getRangeEnd(remove_range),
+ &end_file, &end_line, &end_column, 0);
+ if (start_file == end_file) {
+ fprintf(out, "FIX-IT: Replace ");
+ PrintExtent(out, start_line, start_column, end_line, end_column);
+ fprintf(out, " with \"%s\"\n", clang_getCString(text));
}
+ clang_disposeString(text);
+ break;
+ }
}
}
}