Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 1 | /*===-- CIndexDiagnostics.cpp - Diagnostics C Interface ---------*- C++ -*-===*\ |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2 | |* *| |
| 3 | |* The LLVM Compiler Infrastructure *| |
| 4 | |* *| |
| 5 | |* This file is distributed under the University of Illinois Open Source *| |
| 6 | |* License. See LICENSE.TXT for details. *| |
| 7 | |* *| |
| 8 | |*===----------------------------------------------------------------------===*| |
| 9 | |* *| |
| 10 | |* Implements the diagnostic functions of the Clang C interface. *| |
| 11 | |* *| |
| 12 | \*===----------------------------------------------------------------------===*/ |
| 13 | #include "CIndexDiagnostic.h" |
| 14 | #include "CIndexer.h" |
| 15 | #include "CXSourceLocation.h" |
| 16 | |
Benjamin Kramer | b846deb | 2010-04-12 19:45:50 +0000 | [diff] [blame] | 17 | #include "clang/Frontend/ASTUnit.h" |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 18 | #include "clang/Frontend/FrontendDiagnostic.h" |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/Twine.h" |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 21 | #include "llvm/Support/MemoryBuffer.h" |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 23 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | using namespace clang::cxloc; |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 26 | using namespace clang::cxstring; |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 27 | using namespace llvm; |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 28 | |
| 29 | //----------------------------------------------------------------------------- |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 30 | // C Interface Routines |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 31 | //----------------------------------------------------------------------------- |
| 32 | extern "C" { |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 33 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 34 | unsigned clang_getNumDiagnostics(CXTranslationUnit Unit) { |
| 35 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(Unit); |
Douglas Gregor | 405634b | 2010-04-05 18:10:21 +0000 | [diff] [blame] | 36 | return CXXUnit? CXXUnit->stored_diag_size() : 0; |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | CXDiagnostic clang_getDiagnostic(CXTranslationUnit Unit, unsigned Index) { |
| 40 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(Unit); |
Douglas Gregor | 405634b | 2010-04-05 18:10:21 +0000 | [diff] [blame] | 41 | if (!CXXUnit || Index >= CXXUnit->stored_diag_size()) |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 42 | return 0; |
| 43 | |
Douglas Gregor | 405634b | 2010-04-05 18:10:21 +0000 | [diff] [blame] | 44 | return new CXStoredDiagnostic(CXXUnit->stored_diag_begin()[Index], |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 45 | CXXUnit->getASTContext().getLangOptions()); |
| 46 | } |
| 47 | |
| 48 | void clang_disposeDiagnostic(CXDiagnostic Diagnostic) { |
| 49 | CXStoredDiagnostic *Stored = static_cast<CXStoredDiagnostic *>(Diagnostic); |
| 50 | delete Stored; |
| 51 | } |
| 52 | |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 53 | CXString clang_formatDiagnostic(CXDiagnostic Diagnostic, unsigned Options) { |
| 54 | if (!Diagnostic) |
| 55 | return createCXString(""); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 56 | |
| 57 | CXDiagnosticSeverity Severity = clang_getDiagnosticSeverity(Diagnostic); |
| 58 | |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 59 | llvm::SmallString<256> Str; |
| 60 | llvm::raw_svector_ostream Out(Str); |
| 61 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 62 | if (Options & CXDiagnostic_DisplaySourceLocation) { |
| 63 | // Print source location (file:line), along with optional column |
| 64 | // and source ranges. |
| 65 | CXFile File; |
| 66 | unsigned Line, Column; |
Douglas Gregor | 27a31fe | 2010-11-09 05:52:02 +0000 | [diff] [blame^] | 67 | clang_getInstantiationLocation(clang_getDiagnosticLocation(Diagnostic), |
| 68 | &File, &Line, &Column, 0); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 69 | if (File) { |
| 70 | CXString FName = clang_getFileName(File); |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 71 | Out << clang_getCString(FName) << ":" << Line << ":"; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 72 | clang_disposeString(FName); |
| 73 | if (Options & CXDiagnostic_DisplayColumn) |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 74 | Out << Column << ":"; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 75 | |
| 76 | if (Options & CXDiagnostic_DisplaySourceRanges) { |
| 77 | unsigned N = clang_getDiagnosticNumRanges(Diagnostic); |
| 78 | bool PrintedRange = false; |
| 79 | for (unsigned I = 0; I != N; ++I) { |
| 80 | CXFile StartFile, EndFile; |
| 81 | CXSourceRange Range = clang_getDiagnosticRange(Diagnostic, I); |
| 82 | |
| 83 | unsigned StartLine, StartColumn, EndLine, EndColumn; |
Douglas Gregor | 27a31fe | 2010-11-09 05:52:02 +0000 | [diff] [blame^] | 84 | clang_getInstantiationLocation(clang_getRangeStart(Range), |
| 85 | &StartFile, &StartLine, &StartColumn, |
| 86 | 0); |
| 87 | clang_getInstantiationLocation(clang_getRangeEnd(Range), |
| 88 | &EndFile, &EndLine, &EndColumn, 0); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 89 | |
| 90 | if (StartFile != EndFile || StartFile != File) |
| 91 | continue; |
| 92 | |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 93 | Out << "{" << StartLine << ":" << StartColumn << "-" |
| 94 | << EndLine << ":" << EndColumn << "}"; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 95 | PrintedRange = true; |
| 96 | } |
| 97 | if (PrintedRange) |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 98 | Out << ":"; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 99 | } |
Douglas Gregor | 4cd912a | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 100 | |
| 101 | Out << " "; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 102 | } |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /* Print warning/error/etc. */ |
| 106 | switch (Severity) { |
| 107 | case CXDiagnostic_Ignored: assert(0 && "impossible"); break; |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 108 | case CXDiagnostic_Note: Out << "note: "; break; |
| 109 | case CXDiagnostic_Warning: Out << "warning: "; break; |
| 110 | case CXDiagnostic_Error: Out << "error: "; break; |
| 111 | case CXDiagnostic_Fatal: Out << "fatal error: "; break; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | CXString Text = clang_getDiagnosticSpelling(Diagnostic); |
| 115 | if (clang_getCString(Text)) |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 116 | Out << clang_getCString(Text); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 117 | else |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 118 | Out << "<no diagnostic text>"; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 119 | clang_disposeString(Text); |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 120 | return createCXString(Out.str(), true); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | unsigned clang_defaultDiagnosticDisplayOptions() { |
| 124 | return CXDiagnostic_DisplaySourceLocation | CXDiagnostic_DisplayColumn; |
| 125 | } |
| 126 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 127 | enum CXDiagnosticSeverity clang_getDiagnosticSeverity(CXDiagnostic Diag) { |
| 128 | CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag); |
| 129 | if (!StoredDiag) |
| 130 | return CXDiagnostic_Ignored; |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 131 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 132 | switch (StoredDiag->Diag.getLevel()) { |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 133 | case Diagnostic::Ignored: return CXDiagnostic_Ignored; |
| 134 | case Diagnostic::Note: return CXDiagnostic_Note; |
| 135 | case Diagnostic::Warning: return CXDiagnostic_Warning; |
| 136 | case Diagnostic::Error: return CXDiagnostic_Error; |
| 137 | case Diagnostic::Fatal: return CXDiagnostic_Fatal; |
| 138 | } |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 139 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 140 | llvm_unreachable("Invalid diagnostic level"); |
| 141 | return CXDiagnostic_Ignored; |
| 142 | } |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 143 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 144 | CXSourceLocation clang_getDiagnosticLocation(CXDiagnostic Diag) { |
| 145 | CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag); |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 146 | if (!StoredDiag || StoredDiag->Diag.getLocation().isInvalid()) |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 147 | return clang_getNullLocation(); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 148 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 149 | return translateSourceLocation(StoredDiag->Diag.getLocation().getManager(), |
| 150 | StoredDiag->LangOpts, |
| 151 | StoredDiag->Diag.getLocation()); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | CXString clang_getDiagnosticSpelling(CXDiagnostic Diag) { |
| 155 | CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag); |
| 156 | if (!StoredDiag) |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 157 | return createCXString(""); |
| 158 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 159 | return createCXString(StoredDiag->Diag.getMessage(), false); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Douglas Gregor | a3890ba | 2010-02-08 23:11:56 +0000 | [diff] [blame] | 162 | unsigned clang_getDiagnosticNumRanges(CXDiagnostic Diag) { |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 163 | CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag); |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 164 | if (!StoredDiag || StoredDiag->Diag.getLocation().isInvalid()) |
Douglas Gregor | a3890ba | 2010-02-08 23:11:56 +0000 | [diff] [blame] | 165 | return 0; |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 166 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 167 | return StoredDiag->Diag.range_size(); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 168 | } |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 169 | |
Douglas Gregor | a3890ba | 2010-02-08 23:11:56 +0000 | [diff] [blame] | 170 | CXSourceRange clang_getDiagnosticRange(CXDiagnostic Diag, unsigned Range) { |
| 171 | CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag); |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 172 | if (!StoredDiag || Range >= StoredDiag->Diag.range_size() || |
| 173 | StoredDiag->Diag.getLocation().isInvalid()) |
Douglas Gregor | a3890ba | 2010-02-08 23:11:56 +0000 | [diff] [blame] | 174 | return clang_getNullRange(); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 175 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 176 | return translateSourceRange(StoredDiag->Diag.getLocation().getManager(), |
| 177 | StoredDiag->LangOpts, |
| 178 | StoredDiag->Diag.range_begin()[Range]); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | unsigned clang_getDiagnosticNumFixIts(CXDiagnostic Diag) { |
| 182 | CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag); |
| 183 | if (!StoredDiag) |
| 184 | return 0; |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 185 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 186 | return StoredDiag->Diag.fixit_size(); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Douglas Gregor | 473d701 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 189 | CXString clang_getDiagnosticFixIt(CXDiagnostic Diagnostic, unsigned FixIt, |
| 190 | CXSourceRange *ReplacementRange) { |
| 191 | CXStoredDiagnostic *StoredDiag |
| 192 | = static_cast<CXStoredDiagnostic *>(Diagnostic); |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 193 | if (!StoredDiag || FixIt >= StoredDiag->Diag.fixit_size() || |
| 194 | StoredDiag->Diag.getLocation().isInvalid()) { |
Douglas Gregor | 473d701 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 195 | if (ReplacementRange) |
| 196 | *ReplacementRange = clang_getNullRange(); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 197 | |
| 198 | return createCXString(""); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 199 | } |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 200 | |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 201 | const FixItHint &Hint = StoredDiag->Diag.fixit_begin()[FixIt]; |
Douglas Gregor | 473d701 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 202 | if (ReplacementRange) { |
Douglas Gregor | 783c56f | 2010-08-18 14:24:02 +0000 | [diff] [blame] | 203 | // Create a range that covers the entire replacement (or |
| 204 | // removal) range, adjusting the end of the range to point to |
| 205 | // the end of the token. |
| 206 | *ReplacementRange |
| 207 | = translateSourceRange(StoredDiag->Diag.getLocation().getManager(), |
| 208 | StoredDiag->LangOpts, |
| 209 | Hint.RemoveRange); |
Douglas Gregor | 473d701 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 212 | return createCXString(Hint.CodeToInsert); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 213 | } |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 214 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 215 | } // end extern "C" |