blob: 8fbe3d8c3d38240616753f74f9987725c6040cca [file] [log] [blame]
Douglas Gregora88084b2010-02-18 18:08:43 +00001/*===-- CIndexDiagnostics.cpp - Diagnostics C Interface ---------*- C++ -*-===*\
Douglas Gregor5352ac02010-01-28 00:27:43 +00002|* *|
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 Kremenek0a90d322010-11-17 23:24:11 +000015#include "CXTranslationUnit.h"
Douglas Gregor5352ac02010-01-28 00:27:43 +000016#include "CXSourceLocation.h"
Ted Kremeneked122732010-11-16 01:56:27 +000017#include "CXString.h"
Douglas Gregor5352ac02010-01-28 00:27:43 +000018
Benjamin Kramerb846deb2010-04-12 19:45:50 +000019#include "clang/Frontend/ASTUnit.h"
Douglas Gregord93256e2010-01-28 06:00:51 +000020#include "clang/Frontend/FrontendDiagnostic.h"
Ted Kremenek7473b1c2012-02-14 02:46:03 +000021#include "clang/Frontend/DiagnosticRenderer.h"
22#include "clang/Frontend/DiagnosticOptions.h"
Douglas Gregor274f1902010-02-22 23:17:23 +000023#include "llvm/ADT/SmallString.h"
Douglas Gregora88084b2010-02-18 18:08:43 +000024#include "llvm/ADT/Twine.h"
Douglas Gregord93256e2010-01-28 06:00:51 +000025#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor274f1902010-02-22 23:17:23 +000026#include "llvm/Support/raw_ostream.h"
Douglas Gregord93256e2010-01-28 06:00:51 +000027
Douglas Gregor5352ac02010-01-28 00:27:43 +000028using namespace clang;
29using namespace clang::cxloc;
Ted Kremenekee4db4f2010-02-17 00:41:08 +000030using namespace clang::cxstring;
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +000031using namespace clang::cxdiag;
Douglas Gregora88084b2010-02-18 18:08:43 +000032using namespace llvm;
Douglas Gregor5352ac02010-01-28 00:27:43 +000033
Ted Kremenek15322172011-11-10 08:43:12 +000034
35CXDiagnosticSetImpl::~CXDiagnosticSetImpl() {
36 for (std::vector<CXDiagnosticImpl *>::iterator it = Diagnostics.begin(),
37 et = Diagnostics.end();
38 it != et; ++it) {
39 delete *it;
40 }
41}
42
43CXDiagnosticImpl::~CXDiagnosticImpl() {}
44
Ted Kremenek7473b1c2012-02-14 02:46:03 +000045namespace {
46class CXDiagnosticCustomNoteImpl : public CXDiagnosticImpl {
Ted Kremenek7b8290f2012-02-14 06:54:46 +000047 std::string Message;
Ted Kremenek7473b1c2012-02-14 02:46:03 +000048 CXSourceLocation Loc;
49public:
50 CXDiagnosticCustomNoteImpl(StringRef Msg, CXSourceLocation L)
51 : CXDiagnosticImpl(CustomNoteDiagnosticKind),
Ted Kremenek7b8290f2012-02-14 06:54:46 +000052 Message(Msg), Loc(L) {}
Ted Kremenek7473b1c2012-02-14 02:46:03 +000053
Ted Kremenek7b8290f2012-02-14 06:54:46 +000054 virtual ~CXDiagnosticCustomNoteImpl() {}
Ted Kremenek7473b1c2012-02-14 02:46:03 +000055
56 CXDiagnosticSeverity getSeverity() const {
57 return CXDiagnostic_Note;
58 }
59
60 CXSourceLocation getLocation() const {
61 return Loc;
62 }
63
64 CXString getSpelling() const {
Ted Kremenek7b8290f2012-02-14 06:54:46 +000065 return createCXString(StringRef(Message), false);
Ted Kremenek7473b1c2012-02-14 02:46:03 +000066 }
67
68 CXString getDiagnosticOption(CXString *Disable) const {
69 if (Disable)
70 *Disable = createCXString("", false);
71 return createCXString("", false);
72 }
73
74 unsigned getCategory() const { return 0; }
Ted Kremenek78d5d3b2012-04-12 00:03:31 +000075 CXString getCategoryText() const { return createCXString(""); }
76
Ted Kremenek7473b1c2012-02-14 02:46:03 +000077 unsigned getNumRanges() const { return 0; }
78 CXSourceRange getRange(unsigned Range) const { return clang_getNullRange(); }
79 unsigned getNumFixIts() const { return 0; }
80 CXString getFixIt(unsigned FixIt, CXSourceRange *ReplacementRange) const {
81 if (ReplacementRange)
82 *ReplacementRange = clang_getNullRange();
83 return createCXString("", false);
84 }
85};
86
87class CXDiagnosticRenderer : public DiagnosticNoteRenderer {
88public:
89 CXDiagnosticRenderer(const SourceManager &SM,
90 const LangOptions &LangOpts,
91 const DiagnosticOptions &DiagOpts,
92 CXDiagnosticSetImpl *mainSet)
93 : DiagnosticNoteRenderer(SM, LangOpts, DiagOpts),
94 CurrentSet(mainSet), MainSet(mainSet) {}
95
96 virtual ~CXDiagnosticRenderer() {}
97
98 virtual void beginDiagnostic(DiagOrStoredDiag D,
99 DiagnosticsEngine::Level Level) {
100
101 const StoredDiagnostic *SD = D.dyn_cast<const StoredDiagnostic*>();
102 if (!SD)
103 return;
104
105 if (Level != DiagnosticsEngine::Note)
106 CurrentSet = MainSet;
107
108 CXStoredDiagnostic *CD = new CXStoredDiagnostic(*SD, LangOpts);
109 CurrentSet->appendDiagnostic(CD);
110
111 if (Level != DiagnosticsEngine::Note)
112 CurrentSet = &CD->getChildDiagnostics();
113 }
114
115 virtual void emitDiagnosticMessage(SourceLocation Loc, PresumedLoc PLoc,
116 DiagnosticsEngine::Level Level,
117 StringRef Message,
118 ArrayRef<CharSourceRange> Ranges,
119 DiagOrStoredDiag D) {
120 if (!D.isNull())
121 return;
122
123 CXSourceLocation L = translateSourceLocation(SM, LangOpts, Loc);
124 CXDiagnosticImpl *CD = new CXDiagnosticCustomNoteImpl(Message, L);
125 CurrentSet->appendDiagnostic(CD);
126 }
127
128 virtual void emitDiagnosticLoc(SourceLocation Loc, PresumedLoc PLoc,
129 DiagnosticsEngine::Level Level,
130 ArrayRef<CharSourceRange> Ranges) {}
131
132 virtual void emitCodeContext(SourceLocation Loc,
133 DiagnosticsEngine::Level Level,
134 SmallVectorImpl<CharSourceRange>& Ranges,
Daniel Dunbarfc399c72012-02-29 00:20:42 +0000135 ArrayRef<FixItHint> Hints) {}
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000136
137 virtual void emitNote(SourceLocation Loc, StringRef Message) {
138 CXSourceLocation L = translateSourceLocation(SM, LangOpts, Loc);
139 CurrentSet->appendDiagnostic(new CXDiagnosticCustomNoteImpl(Message,
140 L));
141 }
142
143 CXDiagnosticSetImpl *CurrentSet;
144 CXDiagnosticSetImpl *MainSet;
145};
146}
147
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +0000148CXDiagnosticSetImpl *cxdiag::lazyCreateDiags(CXTranslationUnit TU,
149 bool checkIfChanged) {
Argyrios Kyrtzidis220b45c2011-11-16 02:34:55 +0000150 ASTUnit *AU = static_cast<ASTUnit *>(TU->TUData);
151
152 if (TU->Diagnostics && checkIfChanged) {
Argyrios Kyrtzidisc88e58c2011-11-16 08:59:00 +0000153 // In normal use, ASTUnit's diagnostics should not change unless we reparse.
154 // Currently they can only change by using the internal testing flag
155 // '-error-on-deserialized-decl' which will error during deserialization of
156 // a declaration. What will happen is:
157 //
158 // -c-index-test gets a CXTranslationUnit
159 // -checks the diagnostics, the diagnostics set is lazily created,
160 // no errors are reported
161 // -later does an operation, like annotation of tokens, that triggers
162 // -error-on-deserialized-decl, that will emit a diagnostic error,
163 // that ASTUnit will catch and add to its stored diagnostics vector.
164 // -c-index-test wants to check whether an error occurred after performing
165 // the operation but can only query the lazily created set.
166 //
167 // We check here if a new diagnostic was appended since the last time the
168 // diagnostic set was created, in which case we reset it.
169
Argyrios Kyrtzidis220b45c2011-11-16 02:34:55 +0000170 CXDiagnosticSetImpl *
171 Set = static_cast<CXDiagnosticSetImpl*>(TU->Diagnostics);
172 if (AU->stored_diag_size() != Set->getNumDiagnostics()) {
173 // Diagnostics in the ASTUnit were updated, reset the associated
174 // diagnostics.
175 delete Set;
176 TU->Diagnostics = 0;
177 }
178 }
179
Ted Kremenek15322172011-11-10 08:43:12 +0000180 if (!TU->Diagnostics) {
Ted Kremenek15322172011-11-10 08:43:12 +0000181 CXDiagnosticSetImpl *Set = new CXDiagnosticSetImpl();
182 TU->Diagnostics = Set;
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000183 DiagnosticOptions DOpts;
184 CXDiagnosticRenderer Renderer(AU->getSourceManager(),
David Blaikie4e4d0842012-03-11 07:00:24 +0000185 AU->getASTContext().getLangOpts(),
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000186 DOpts, Set);
Ted Kremenek15322172011-11-10 08:43:12 +0000187
188 for (ASTUnit::stored_diag_iterator it = AU->stored_diag_begin(),
189 ei = AU->stored_diag_end(); it != ei; ++it) {
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000190 Renderer.emitStoredDiagnostic(*it);
Ted Kremenek15322172011-11-10 08:43:12 +0000191 }
192 }
193 return static_cast<CXDiagnosticSetImpl*>(TU->Diagnostics);
194}
195
Douglas Gregor5352ac02010-01-28 00:27:43 +0000196//-----------------------------------------------------------------------------
Ted Kremenekee4db4f2010-02-17 00:41:08 +0000197// C Interface Routines
Douglas Gregor5352ac02010-01-28 00:27:43 +0000198//-----------------------------------------------------------------------------
199extern "C" {
Ted Kremenekee4db4f2010-02-17 00:41:08 +0000200
Douglas Gregora88084b2010-02-18 18:08:43 +0000201unsigned clang_getNumDiagnostics(CXTranslationUnit Unit) {
Ted Kremenek15322172011-11-10 08:43:12 +0000202 if (!Unit->TUData)
203 return 0;
Argyrios Kyrtzidis220b45c2011-11-16 02:34:55 +0000204 return lazyCreateDiags(Unit, /*checkIfChanged=*/true)->getNumDiagnostics();
Douglas Gregora88084b2010-02-18 18:08:43 +0000205}
206
207CXDiagnostic clang_getDiagnostic(CXTranslationUnit Unit, unsigned Index) {
Ted Kremenek0373fcc2011-12-09 22:28:32 +0000208 CXDiagnosticSet D = clang_getDiagnosticSetFromTU(Unit);
209 if (!D)
Douglas Gregora88084b2010-02-18 18:08:43 +0000210 return 0;
211
Ted Kremenek0373fcc2011-12-09 22:28:32 +0000212 CXDiagnosticSetImpl *Diags = static_cast<CXDiagnosticSetImpl*>(D);
Ted Kremenek15322172011-11-10 08:43:12 +0000213 if (Index >= Diags->getNumDiagnostics())
214 return 0;
215
216 return Diags->getDiagnostic(Index);
Douglas Gregora88084b2010-02-18 18:08:43 +0000217}
Ted Kremenek0373fcc2011-12-09 22:28:32 +0000218
219CXDiagnosticSet clang_getDiagnosticSetFromTU(CXTranslationUnit Unit) {
220 if (!Unit->TUData)
221 return 0;
222 return static_cast<CXDiagnostic>(lazyCreateDiags(Unit));
223}
Douglas Gregora88084b2010-02-18 18:08:43 +0000224
225void clang_disposeDiagnostic(CXDiagnostic Diagnostic) {
Ted Kremenek15322172011-11-10 08:43:12 +0000226 // No-op. Kept as a legacy API. CXDiagnostics are now managed
227 // by the enclosing CXDiagnosticSet.
Douglas Gregora88084b2010-02-18 18:08:43 +0000228}
229
Douglas Gregor274f1902010-02-22 23:17:23 +0000230CXString clang_formatDiagnostic(CXDiagnostic Diagnostic, unsigned Options) {
231 if (!Diagnostic)
232 return createCXString("");
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000233
234 CXDiagnosticSeverity Severity = clang_getDiagnosticSeverity(Diagnostic);
235
Dylan Noblesmith36d59272012-02-13 12:32:26 +0000236 SmallString<256> Str;
Douglas Gregor274f1902010-02-22 23:17:23 +0000237 llvm::raw_svector_ostream Out(Str);
238
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000239 if (Options & CXDiagnostic_DisplaySourceLocation) {
240 // Print source location (file:line), along with optional column
241 // and source ranges.
242 CXFile File;
243 unsigned Line, Column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000244 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
245 &File, &Line, &Column, 0);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000246 if (File) {
247 CXString FName = clang_getFileName(File);
Douglas Gregor274f1902010-02-22 23:17:23 +0000248 Out << clang_getCString(FName) << ":" << Line << ":";
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000249 clang_disposeString(FName);
250 if (Options & CXDiagnostic_DisplayColumn)
Douglas Gregor274f1902010-02-22 23:17:23 +0000251 Out << Column << ":";
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000252
253 if (Options & CXDiagnostic_DisplaySourceRanges) {
254 unsigned N = clang_getDiagnosticNumRanges(Diagnostic);
255 bool PrintedRange = false;
256 for (unsigned I = 0; I != N; ++I) {
257 CXFile StartFile, EndFile;
258 CXSourceRange Range = clang_getDiagnosticRange(Diagnostic, I);
259
260 unsigned StartLine, StartColumn, EndLine, EndColumn;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000261 clang_getSpellingLocation(clang_getRangeStart(Range),
262 &StartFile, &StartLine, &StartColumn,
263 0);
264 clang_getSpellingLocation(clang_getRangeEnd(Range),
265 &EndFile, &EndLine, &EndColumn, 0);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000266
267 if (StartFile != EndFile || StartFile != File)
268 continue;
269
Douglas Gregor274f1902010-02-22 23:17:23 +0000270 Out << "{" << StartLine << ":" << StartColumn << "-"
271 << EndLine << ":" << EndColumn << "}";
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000272 PrintedRange = true;
273 }
274 if (PrintedRange)
Douglas Gregor274f1902010-02-22 23:17:23 +0000275 Out << ":";
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000276 }
Douglas Gregor4cd912a2010-10-12 00:50:20 +0000277
278 Out << " ";
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000279 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000280 }
281
282 /* Print warning/error/etc. */
283 switch (Severity) {
David Blaikieeb2d1f12011-09-23 20:26:49 +0000284 case CXDiagnostic_Ignored: llvm_unreachable("impossible");
Douglas Gregor274f1902010-02-22 23:17:23 +0000285 case CXDiagnostic_Note: Out << "note: "; break;
286 case CXDiagnostic_Warning: Out << "warning: "; break;
287 case CXDiagnostic_Error: Out << "error: "; break;
288 case CXDiagnostic_Fatal: Out << "fatal error: "; break;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000289 }
290
291 CXString Text = clang_getDiagnosticSpelling(Diagnostic);
292 if (clang_getCString(Text))
Douglas Gregor274f1902010-02-22 23:17:23 +0000293 Out << clang_getCString(Text);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000294 else
Douglas Gregor274f1902010-02-22 23:17:23 +0000295 Out << "<no diagnostic text>";
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000296 clang_disposeString(Text);
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000297
298 if (Options & (CXDiagnostic_DisplayOption | CXDiagnostic_DisplayCategoryId |
299 CXDiagnostic_DisplayCategoryName)) {
300 bool NeedBracket = true;
301 bool NeedComma = false;
302
303 if (Options & CXDiagnostic_DisplayOption) {
304 CXString OptionName = clang_getDiagnosticOption(Diagnostic, 0);
305 if (const char *OptionText = clang_getCString(OptionName)) {
306 if (OptionText[0]) {
307 Out << " [" << OptionText;
308 NeedBracket = false;
309 NeedComma = true;
310 }
311 }
312 clang_disposeString(OptionName);
313 }
314
315 if (Options & (CXDiagnostic_DisplayCategoryId |
316 CXDiagnostic_DisplayCategoryName)) {
317 if (unsigned CategoryID = clang_getDiagnosticCategory(Diagnostic)) {
318 if (Options & CXDiagnostic_DisplayCategoryId) {
319 if (NeedBracket)
320 Out << " [";
321 if (NeedComma)
322 Out << ", ";
323 Out << CategoryID;
324 NeedBracket = false;
325 NeedComma = true;
326 }
327
328 if (Options & CXDiagnostic_DisplayCategoryName) {
Ted Kremenek78d5d3b2012-04-12 00:03:31 +0000329 CXString CategoryName = clang_getDiagnosticCategoryText(Diagnostic);
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000330 if (NeedBracket)
331 Out << " [";
332 if (NeedComma)
333 Out << ", ";
334 Out << clang_getCString(CategoryName);
335 NeedBracket = false;
336 NeedComma = true;
337 clang_disposeString(CategoryName);
338 }
339 }
340 }
Ted Kremenek8e598382012-04-04 00:55:33 +0000341
342 (void) NeedComma; // Silence dead store warning.
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000343 if (!NeedBracket)
344 Out << "]";
345 }
346
Douglas Gregor274f1902010-02-22 23:17:23 +0000347 return createCXString(Out.str(), true);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000348}
349
350unsigned clang_defaultDiagnosticDisplayOptions() {
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000351 return CXDiagnostic_DisplaySourceLocation | CXDiagnostic_DisplayColumn |
352 CXDiagnostic_DisplayOption;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000353}
354
Douglas Gregor5352ac02010-01-28 00:27:43 +0000355enum CXDiagnosticSeverity clang_getDiagnosticSeverity(CXDiagnostic Diag) {
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000356 if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl*>(Diag))
357 return D->getSeverity();
Douglas Gregor5352ac02010-01-28 00:27:43 +0000358 return CXDiagnostic_Ignored;
359}
Ted Kremenekee4db4f2010-02-17 00:41:08 +0000360
Douglas Gregor5352ac02010-01-28 00:27:43 +0000361CXSourceLocation clang_getDiagnosticLocation(CXDiagnostic Diag) {
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000362 if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl*>(Diag))
363 return D->getLocation();
364 return clang_getNullLocation();
Douglas Gregor5352ac02010-01-28 00:27:43 +0000365}
366
367CXString clang_getDiagnosticSpelling(CXDiagnostic Diag) {
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000368 if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
369 return D->getSpelling();
370 return createCXString("");
Douglas Gregor5352ac02010-01-28 00:27:43 +0000371}
372
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000373CXString clang_getDiagnosticOption(CXDiagnostic Diag, CXString *Disable) {
374 if (Disable)
375 *Disable = createCXString("");
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000376
377 if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
378 return D->getDiagnosticOption(Disable);
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000379
380 return createCXString("");
381}
382
383unsigned clang_getDiagnosticCategory(CXDiagnostic Diag) {
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000384 if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
385 return D->getCategory();
386 return 0;
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000387}
388
389CXString clang_getDiagnosticCategoryName(unsigned Category) {
Ted Kremenek78d5d3b2012-04-12 00:03:31 +0000390 // Kept for backwards compatibility.
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000391 return createCXString(DiagnosticIDs::getCategoryNameFromID(Category));
392}
393
Ted Kremenek78d5d3b2012-04-12 00:03:31 +0000394CXString clang_getDiagnosticCategoryText(CXDiagnostic Diag) {
395 if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
396 return D->getCategoryText();
397 return createCXString("");
398}
399
Douglas Gregora3890ba2010-02-08 23:11:56 +0000400unsigned clang_getDiagnosticNumRanges(CXDiagnostic Diag) {
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000401 if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
402 return D->getNumRanges();
403 return 0;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000404}
Ted Kremenekee4db4f2010-02-17 00:41:08 +0000405
Douglas Gregora3890ba2010-02-08 23:11:56 +0000406CXSourceRange clang_getDiagnosticRange(CXDiagnostic Diag, unsigned Range) {
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000407 CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag);
408 if (!D || Range >= D->getNumRanges())
Douglas Gregora3890ba2010-02-08 23:11:56 +0000409 return clang_getNullRange();
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000410 return D->getRange(Range);
Douglas Gregor5352ac02010-01-28 00:27:43 +0000411}
412
413unsigned clang_getDiagnosticNumFixIts(CXDiagnostic Diag) {
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000414 if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
415 return D->getNumFixIts();
416 return 0;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000417}
418
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000419CXString clang_getDiagnosticFixIt(CXDiagnostic Diag, unsigned FixIt,
Douglas Gregor473d7012010-02-19 18:16:06 +0000420 CXSourceRange *ReplacementRange) {
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000421 CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag);
422 if (!D || FixIt >= D->getNumFixIts()) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000423 if (ReplacementRange)
424 *ReplacementRange = clang_getNullRange();
Ted Kremenekee4db4f2010-02-17 00:41:08 +0000425 return createCXString("");
Douglas Gregor5352ac02010-01-28 00:27:43 +0000426 }
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000427 return D->getFixIt(FixIt, ReplacementRange);
Douglas Gregor5352ac02010-01-28 00:27:43 +0000428}
Ted Kremenekee4db4f2010-02-17 00:41:08 +0000429
Ted Kremenek15322172011-11-10 08:43:12 +0000430void clang_disposeDiagnosticSet(CXDiagnosticSet Diags) {
431 CXDiagnosticSetImpl *D = static_cast<CXDiagnosticSetImpl*>(Diags);
432 if (D->isExternallyManaged())
433 delete D;
434}
435
436CXDiagnostic clang_getDiagnosticInSet(CXDiagnosticSet Diags,
437 unsigned Index) {
438 if (CXDiagnosticSetImpl *D = static_cast<CXDiagnosticSetImpl*>(Diags))
439 if (Index < D->getNumDiagnostics())
440 return D->getDiagnostic(Index);
441 return 0;
442}
443
444CXDiagnosticSet clang_getChildDiagnostics(CXDiagnostic Diag) {
445 if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag)) {
446 CXDiagnosticSetImpl &ChildDiags = D->getChildDiagnostics();
447 return ChildDiags.empty() ? 0 : (CXDiagnosticSet) &ChildDiags;
448 }
449 return 0;
450}
451
452unsigned clang_getNumDiagnosticsInSet(CXDiagnosticSet Diags) {
453 if (CXDiagnosticSetImpl *D = static_cast<CXDiagnosticSetImpl*>(Diags))
454 return D->getNumDiagnostics();
455 return 0;
456}
457
Douglas Gregor5352ac02010-01-28 00:27:43 +0000458} // end extern "C"