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" |
Ted Kremenek | 0a90d32 | 2010-11-17 23:24:11 +0000 | [diff] [blame] | 15 | #include "CXTranslationUnit.h" |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 16 | #include "CXSourceLocation.h" |
Ted Kremenek | ed12273 | 2010-11-16 01:56:27 +0000 | [diff] [blame] | 17 | #include "CXString.h" |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 18 | |
Benjamin Kramer | b846deb | 2010-04-12 19:45:50 +0000 | [diff] [blame] | 19 | #include "clang/Frontend/ASTUnit.h" |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 20 | #include "clang/Frontend/FrontendDiagnostic.h" |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/Twine.h" |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MemoryBuffer.h" |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 25 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | using namespace clang::cxloc; |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 28 | using namespace clang::cxstring; |
Argyrios Kyrtzidis | 996e6e5 | 2011-12-01 02:42:50 +0000 | [diff] [blame] | 29 | using namespace clang::cxdiag; |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 30 | using namespace llvm; |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 31 | |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 32 | |
| 33 | CXDiagnosticSetImpl::~CXDiagnosticSetImpl() { |
| 34 | for (std::vector<CXDiagnosticImpl *>::iterator it = Diagnostics.begin(), |
| 35 | et = Diagnostics.end(); |
| 36 | it != et; ++it) { |
| 37 | delete *it; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | CXDiagnosticImpl::~CXDiagnosticImpl() {} |
| 42 | |
Argyrios Kyrtzidis | 996e6e5 | 2011-12-01 02:42:50 +0000 | [diff] [blame] | 43 | CXDiagnosticSetImpl *cxdiag::lazyCreateDiags(CXTranslationUnit TU, |
| 44 | bool checkIfChanged) { |
Argyrios Kyrtzidis | 220b45c | 2011-11-16 02:34:55 +0000 | [diff] [blame] | 45 | ASTUnit *AU = static_cast<ASTUnit *>(TU->TUData); |
| 46 | |
| 47 | if (TU->Diagnostics && checkIfChanged) { |
Argyrios Kyrtzidis | c88e58c | 2011-11-16 08:59:00 +0000 | [diff] [blame] | 48 | // In normal use, ASTUnit's diagnostics should not change unless we reparse. |
| 49 | // Currently they can only change by using the internal testing flag |
| 50 | // '-error-on-deserialized-decl' which will error during deserialization of |
| 51 | // a declaration. What will happen is: |
| 52 | // |
| 53 | // -c-index-test gets a CXTranslationUnit |
| 54 | // -checks the diagnostics, the diagnostics set is lazily created, |
| 55 | // no errors are reported |
| 56 | // -later does an operation, like annotation of tokens, that triggers |
| 57 | // -error-on-deserialized-decl, that will emit a diagnostic error, |
| 58 | // that ASTUnit will catch and add to its stored diagnostics vector. |
| 59 | // -c-index-test wants to check whether an error occurred after performing |
| 60 | // the operation but can only query the lazily created set. |
| 61 | // |
| 62 | // We check here if a new diagnostic was appended since the last time the |
| 63 | // diagnostic set was created, in which case we reset it. |
| 64 | |
Argyrios Kyrtzidis | 220b45c | 2011-11-16 02:34:55 +0000 | [diff] [blame] | 65 | CXDiagnosticSetImpl * |
| 66 | Set = static_cast<CXDiagnosticSetImpl*>(TU->Diagnostics); |
| 67 | if (AU->stored_diag_size() != Set->getNumDiagnostics()) { |
| 68 | // Diagnostics in the ASTUnit were updated, reset the associated |
| 69 | // diagnostics. |
| 70 | delete Set; |
| 71 | TU->Diagnostics = 0; |
| 72 | } |
| 73 | } |
| 74 | |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 75 | if (!TU->Diagnostics) { |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 76 | CXDiagnosticSetImpl *Set = new CXDiagnosticSetImpl(); |
| 77 | TU->Diagnostics = Set; |
| 78 | |
| 79 | for (ASTUnit::stored_diag_iterator it = AU->stored_diag_begin(), |
| 80 | ei = AU->stored_diag_end(); it != ei; ++it) { |
| 81 | CXStoredDiagnostic *D = |
| 82 | new CXStoredDiagnostic(*it, AU->getASTContext().getLangOptions()); |
| 83 | Set->appendDiagnostic(D); |
| 84 | } |
| 85 | } |
| 86 | return static_cast<CXDiagnosticSetImpl*>(TU->Diagnostics); |
| 87 | } |
| 88 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 89 | //----------------------------------------------------------------------------- |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 90 | // C Interface Routines |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 91 | //----------------------------------------------------------------------------- |
| 92 | extern "C" { |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 93 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 94 | unsigned clang_getNumDiagnostics(CXTranslationUnit Unit) { |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 95 | if (!Unit->TUData) |
| 96 | return 0; |
Argyrios Kyrtzidis | 220b45c | 2011-11-16 02:34:55 +0000 | [diff] [blame] | 97 | return lazyCreateDiags(Unit, /*checkIfChanged=*/true)->getNumDiagnostics(); |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | CXDiagnostic clang_getDiagnostic(CXTranslationUnit Unit, unsigned Index) { |
Ted Kremenek | 0373fcc | 2011-12-09 22:28:32 +0000 | [diff] [blame] | 101 | CXDiagnosticSet D = clang_getDiagnosticSetFromTU(Unit); |
| 102 | if (!D) |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 103 | return 0; |
| 104 | |
Ted Kremenek | 0373fcc | 2011-12-09 22:28:32 +0000 | [diff] [blame] | 105 | CXDiagnosticSetImpl *Diags = static_cast<CXDiagnosticSetImpl*>(D); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 106 | if (Index >= Diags->getNumDiagnostics()) |
| 107 | return 0; |
| 108 | |
| 109 | return Diags->getDiagnostic(Index); |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 110 | } |
Ted Kremenek | 0373fcc | 2011-12-09 22:28:32 +0000 | [diff] [blame] | 111 | |
| 112 | CXDiagnosticSet clang_getDiagnosticSetFromTU(CXTranslationUnit Unit) { |
| 113 | if (!Unit->TUData) |
| 114 | return 0; |
| 115 | return static_cast<CXDiagnostic>(lazyCreateDiags(Unit)); |
| 116 | } |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 117 | |
| 118 | void clang_disposeDiagnostic(CXDiagnostic Diagnostic) { |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 119 | // No-op. Kept as a legacy API. CXDiagnostics are now managed |
| 120 | // by the enclosing CXDiagnosticSet. |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 123 | CXString clang_formatDiagnostic(CXDiagnostic Diagnostic, unsigned Options) { |
| 124 | if (!Diagnostic) |
| 125 | return createCXString(""); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 126 | |
| 127 | CXDiagnosticSeverity Severity = clang_getDiagnosticSeverity(Diagnostic); |
| 128 | |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 129 | llvm::SmallString<256> Str; |
| 130 | llvm::raw_svector_ostream Out(Str); |
| 131 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 132 | if (Options & CXDiagnostic_DisplaySourceLocation) { |
| 133 | // Print source location (file:line), along with optional column |
| 134 | // and source ranges. |
| 135 | CXFile File; |
| 136 | unsigned Line, Column; |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 137 | clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic), |
| 138 | &File, &Line, &Column, 0); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 139 | if (File) { |
| 140 | CXString FName = clang_getFileName(File); |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 141 | Out << clang_getCString(FName) << ":" << Line << ":"; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 142 | clang_disposeString(FName); |
| 143 | if (Options & CXDiagnostic_DisplayColumn) |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 144 | Out << Column << ":"; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 145 | |
| 146 | if (Options & CXDiagnostic_DisplaySourceRanges) { |
| 147 | unsigned N = clang_getDiagnosticNumRanges(Diagnostic); |
| 148 | bool PrintedRange = false; |
| 149 | for (unsigned I = 0; I != N; ++I) { |
| 150 | CXFile StartFile, EndFile; |
| 151 | CXSourceRange Range = clang_getDiagnosticRange(Diagnostic, I); |
| 152 | |
| 153 | unsigned StartLine, StartColumn, EndLine, EndColumn; |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 154 | clang_getSpellingLocation(clang_getRangeStart(Range), |
| 155 | &StartFile, &StartLine, &StartColumn, |
| 156 | 0); |
| 157 | clang_getSpellingLocation(clang_getRangeEnd(Range), |
| 158 | &EndFile, &EndLine, &EndColumn, 0); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 159 | |
| 160 | if (StartFile != EndFile || StartFile != File) |
| 161 | continue; |
| 162 | |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 163 | Out << "{" << StartLine << ":" << StartColumn << "-" |
| 164 | << EndLine << ":" << EndColumn << "}"; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 165 | PrintedRange = true; |
| 166 | } |
| 167 | if (PrintedRange) |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 168 | Out << ":"; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 169 | } |
Douglas Gregor | 4cd912a | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 170 | |
| 171 | Out << " "; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 172 | } |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /* Print warning/error/etc. */ |
| 176 | switch (Severity) { |
David Blaikie | eb2d1f1 | 2011-09-23 20:26:49 +0000 | [diff] [blame] | 177 | case CXDiagnostic_Ignored: llvm_unreachable("impossible"); |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 178 | case CXDiagnostic_Note: Out << "note: "; break; |
| 179 | case CXDiagnostic_Warning: Out << "warning: "; break; |
| 180 | case CXDiagnostic_Error: Out << "error: "; break; |
| 181 | case CXDiagnostic_Fatal: Out << "fatal error: "; break; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | CXString Text = clang_getDiagnosticSpelling(Diagnostic); |
| 185 | if (clang_getCString(Text)) |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 186 | Out << clang_getCString(Text); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 187 | else |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 188 | Out << "<no diagnostic text>"; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 189 | clang_disposeString(Text); |
Douglas Gregor | aa5f135 | 2010-11-19 16:18:16 +0000 | [diff] [blame] | 190 | |
| 191 | if (Options & (CXDiagnostic_DisplayOption | CXDiagnostic_DisplayCategoryId | |
| 192 | CXDiagnostic_DisplayCategoryName)) { |
| 193 | bool NeedBracket = true; |
| 194 | bool NeedComma = false; |
| 195 | |
| 196 | if (Options & CXDiagnostic_DisplayOption) { |
| 197 | CXString OptionName = clang_getDiagnosticOption(Diagnostic, 0); |
| 198 | if (const char *OptionText = clang_getCString(OptionName)) { |
| 199 | if (OptionText[0]) { |
| 200 | Out << " [" << OptionText; |
| 201 | NeedBracket = false; |
| 202 | NeedComma = true; |
| 203 | } |
| 204 | } |
| 205 | clang_disposeString(OptionName); |
| 206 | } |
| 207 | |
| 208 | if (Options & (CXDiagnostic_DisplayCategoryId | |
| 209 | CXDiagnostic_DisplayCategoryName)) { |
| 210 | if (unsigned CategoryID = clang_getDiagnosticCategory(Diagnostic)) { |
| 211 | if (Options & CXDiagnostic_DisplayCategoryId) { |
| 212 | if (NeedBracket) |
| 213 | Out << " ["; |
| 214 | if (NeedComma) |
| 215 | Out << ", "; |
| 216 | Out << CategoryID; |
| 217 | NeedBracket = false; |
| 218 | NeedComma = true; |
| 219 | } |
| 220 | |
| 221 | if (Options & CXDiagnostic_DisplayCategoryName) { |
| 222 | CXString CategoryName = clang_getDiagnosticCategoryName(CategoryID); |
| 223 | if (NeedBracket) |
| 224 | Out << " ["; |
| 225 | if (NeedComma) |
| 226 | Out << ", "; |
| 227 | Out << clang_getCString(CategoryName); |
| 228 | NeedBracket = false; |
| 229 | NeedComma = true; |
| 230 | clang_disposeString(CategoryName); |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | if (!NeedBracket) |
| 236 | Out << "]"; |
| 237 | } |
| 238 | |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 239 | return createCXString(Out.str(), true); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | unsigned clang_defaultDiagnosticDisplayOptions() { |
Douglas Gregor | aa5f135 | 2010-11-19 16:18:16 +0000 | [diff] [blame] | 243 | return CXDiagnostic_DisplaySourceLocation | CXDiagnostic_DisplayColumn | |
| 244 | CXDiagnostic_DisplayOption; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 247 | enum CXDiagnosticSeverity clang_getDiagnosticSeverity(CXDiagnostic Diag) { |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 248 | if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl*>(Diag)) |
| 249 | return D->getSeverity(); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 250 | return CXDiagnostic_Ignored; |
| 251 | } |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 252 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 253 | CXSourceLocation clang_getDiagnosticLocation(CXDiagnostic Diag) { |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 254 | if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl*>(Diag)) |
| 255 | return D->getLocation(); |
| 256 | return clang_getNullLocation(); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | CXString clang_getDiagnosticSpelling(CXDiagnostic Diag) { |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 260 | if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag)) |
| 261 | return D->getSpelling(); |
| 262 | return createCXString(""); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Douglas Gregor | aa5f135 | 2010-11-19 16:18:16 +0000 | [diff] [blame] | 265 | CXString clang_getDiagnosticOption(CXDiagnostic Diag, CXString *Disable) { |
| 266 | if (Disable) |
| 267 | *Disable = createCXString(""); |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 268 | |
| 269 | if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag)) |
| 270 | return D->getDiagnosticOption(Disable); |
Douglas Gregor | aa5f135 | 2010-11-19 16:18:16 +0000 | [diff] [blame] | 271 | |
| 272 | return createCXString(""); |
| 273 | } |
| 274 | |
| 275 | unsigned clang_getDiagnosticCategory(CXDiagnostic Diag) { |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 276 | if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag)) |
| 277 | return D->getCategory(); |
| 278 | return 0; |
Douglas Gregor | aa5f135 | 2010-11-19 16:18:16 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | CXString clang_getDiagnosticCategoryName(unsigned Category) { |
| 282 | return createCXString(DiagnosticIDs::getCategoryNameFromID(Category)); |
| 283 | } |
| 284 | |
Douglas Gregor | a3890ba | 2010-02-08 23:11:56 +0000 | [diff] [blame] | 285 | unsigned clang_getDiagnosticNumRanges(CXDiagnostic Diag) { |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 286 | if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag)) |
| 287 | return D->getNumRanges(); |
| 288 | return 0; |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 289 | } |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 290 | |
Douglas Gregor | a3890ba | 2010-02-08 23:11:56 +0000 | [diff] [blame] | 291 | CXSourceRange clang_getDiagnosticRange(CXDiagnostic Diag, unsigned Range) { |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 292 | CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag); |
| 293 | if (!D || Range >= D->getNumRanges()) |
Douglas Gregor | a3890ba | 2010-02-08 23:11:56 +0000 | [diff] [blame] | 294 | return clang_getNullRange(); |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 295 | return D->getRange(Range); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | unsigned clang_getDiagnosticNumFixIts(CXDiagnostic Diag) { |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 299 | if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag)) |
| 300 | return D->getNumFixIts(); |
| 301 | return 0; |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 304 | CXString clang_getDiagnosticFixIt(CXDiagnostic Diag, unsigned FixIt, |
Douglas Gregor | 473d701 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 305 | CXSourceRange *ReplacementRange) { |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 306 | CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag); |
| 307 | if (!D || FixIt >= D->getNumFixIts()) { |
Douglas Gregor | 473d701 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 308 | if (ReplacementRange) |
| 309 | *ReplacementRange = clang_getNullRange(); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 310 | return createCXString(""); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 311 | } |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 312 | return D->getFixIt(FixIt, ReplacementRange); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 313 | } |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 314 | |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 315 | void clang_disposeDiagnosticSet(CXDiagnosticSet Diags) { |
| 316 | CXDiagnosticSetImpl *D = static_cast<CXDiagnosticSetImpl*>(Diags); |
| 317 | if (D->isExternallyManaged()) |
| 318 | delete D; |
| 319 | } |
| 320 | |
| 321 | CXDiagnostic clang_getDiagnosticInSet(CXDiagnosticSet Diags, |
| 322 | unsigned Index) { |
| 323 | if (CXDiagnosticSetImpl *D = static_cast<CXDiagnosticSetImpl*>(Diags)) |
| 324 | if (Index < D->getNumDiagnostics()) |
| 325 | return D->getDiagnostic(Index); |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | CXDiagnosticSet clang_getChildDiagnostics(CXDiagnostic Diag) { |
| 330 | if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag)) { |
| 331 | CXDiagnosticSetImpl &ChildDiags = D->getChildDiagnostics(); |
| 332 | return ChildDiags.empty() ? 0 : (CXDiagnosticSet) &ChildDiags; |
| 333 | } |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | unsigned clang_getNumDiagnosticsInSet(CXDiagnosticSet Diags) { |
| 338 | if (CXDiagnosticSetImpl *D = static_cast<CXDiagnosticSetImpl*>(Diags)) |
| 339 | return D->getNumDiagnostics(); |
| 340 | return 0; |
| 341 | } |
| 342 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 343 | } // end extern "C" |