blob: f48034e30b64676157e96392c7790447162455a1 [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"
Douglas Gregor02c23eb2012-10-23 22:26:28 +000022#include "clang/Basic/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:
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +000089 CXDiagnosticRenderer(const LangOptions &LangOpts,
Douglas Gregor02c23eb2012-10-23 22:26:28 +000090 DiagnosticOptions *DiagOpts,
Ted Kremenek7473b1c2012-02-14 02:46:03 +000091 CXDiagnosticSetImpl *mainSet)
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +000092 : DiagnosticNoteRenderer(LangOpts, DiagOpts),
Ted Kremenek7473b1c2012-02-14 02:46:03 +000093 CurrentSet(mainSet), MainSet(mainSet) {}
94
95 virtual ~CXDiagnosticRenderer() {}
96
97 virtual void beginDiagnostic(DiagOrStoredDiag D,
98 DiagnosticsEngine::Level Level) {
99
100 const StoredDiagnostic *SD = D.dyn_cast<const StoredDiagnostic*>();
101 if (!SD)
102 return;
103
104 if (Level != DiagnosticsEngine::Note)
105 CurrentSet = MainSet;
106
107 CXStoredDiagnostic *CD = new CXStoredDiagnostic(*SD, LangOpts);
108 CurrentSet->appendDiagnostic(CD);
109
110 if (Level != DiagnosticsEngine::Note)
111 CurrentSet = &CD->getChildDiagnostics();
112 }
113
114 virtual void emitDiagnosticMessage(SourceLocation Loc, PresumedLoc PLoc,
115 DiagnosticsEngine::Level Level,
116 StringRef Message,
117 ArrayRef<CharSourceRange> Ranges,
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000118 const SourceManager *SM,
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000119 DiagOrStoredDiag D) {
120 if (!D.isNull())
121 return;
122
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000123 CXSourceLocation L;
124 if (SM)
125 L = translateSourceLocation(*SM, LangOpts, Loc);
126 else
127 L = clang_getNullLocation();
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000128 CXDiagnosticImpl *CD = new CXDiagnosticCustomNoteImpl(Message, L);
129 CurrentSet->appendDiagnostic(CD);
130 }
131
132 virtual void emitDiagnosticLoc(SourceLocation Loc, PresumedLoc PLoc,
133 DiagnosticsEngine::Level Level,
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000134 ArrayRef<CharSourceRange> Ranges,
135 const SourceManager &SM) {}
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000136
137 virtual void emitCodeContext(SourceLocation Loc,
138 DiagnosticsEngine::Level Level,
139 SmallVectorImpl<CharSourceRange>& Ranges,
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000140 ArrayRef<FixItHint> Hints,
141 const SourceManager &SM) {}
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000142
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000143 virtual void emitNote(SourceLocation Loc, StringRef Message,
144 const SourceManager *SM) {
145 CXSourceLocation L;
146 if (SM)
147 L = translateSourceLocation(*SM, LangOpts, Loc);
148 else
149 L = clang_getNullLocation();
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000150 CurrentSet->appendDiagnostic(new CXDiagnosticCustomNoteImpl(Message,
151 L));
152 }
153
154 CXDiagnosticSetImpl *CurrentSet;
155 CXDiagnosticSetImpl *MainSet;
156};
157}
158
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +0000159CXDiagnosticSetImpl *cxdiag::lazyCreateDiags(CXTranslationUnit TU,
160 bool checkIfChanged) {
Dmitri Gribenko5694feb2013-01-26 18:53:38 +0000161 ASTUnit *AU = cxtu::getASTUnit(TU);
Argyrios Kyrtzidis220b45c2011-11-16 02:34:55 +0000162
163 if (TU->Diagnostics && checkIfChanged) {
Argyrios Kyrtzidisc88e58c2011-11-16 08:59:00 +0000164 // In normal use, ASTUnit's diagnostics should not change unless we reparse.
165 // Currently they can only change by using the internal testing flag
166 // '-error-on-deserialized-decl' which will error during deserialization of
167 // a declaration. What will happen is:
168 //
169 // -c-index-test gets a CXTranslationUnit
170 // -checks the diagnostics, the diagnostics set is lazily created,
171 // no errors are reported
172 // -later does an operation, like annotation of tokens, that triggers
173 // -error-on-deserialized-decl, that will emit a diagnostic error,
174 // that ASTUnit will catch and add to its stored diagnostics vector.
175 // -c-index-test wants to check whether an error occurred after performing
176 // the operation but can only query the lazily created set.
177 //
178 // We check here if a new diagnostic was appended since the last time the
179 // diagnostic set was created, in which case we reset it.
180
Argyrios Kyrtzidis220b45c2011-11-16 02:34:55 +0000181 CXDiagnosticSetImpl *
182 Set = static_cast<CXDiagnosticSetImpl*>(TU->Diagnostics);
183 if (AU->stored_diag_size() != Set->getNumDiagnostics()) {
184 // Diagnostics in the ASTUnit were updated, reset the associated
185 // diagnostics.
186 delete Set;
187 TU->Diagnostics = 0;
188 }
189 }
190
Ted Kremenek15322172011-11-10 08:43:12 +0000191 if (!TU->Diagnostics) {
Ted Kremenek15322172011-11-10 08:43:12 +0000192 CXDiagnosticSetImpl *Set = new CXDiagnosticSetImpl();
193 TU->Diagnostics = Set;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000194 IntrusiveRefCntPtr<DiagnosticOptions> DOpts = new DiagnosticOptions;
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000195 CXDiagnosticRenderer Renderer(AU->getASTContext().getLangOpts(),
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000196 &*DOpts, Set);
Ted Kremenek15322172011-11-10 08:43:12 +0000197
198 for (ASTUnit::stored_diag_iterator it = AU->stored_diag_begin(),
199 ei = AU->stored_diag_end(); it != ei; ++it) {
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000200 Renderer.emitStoredDiagnostic(*it);
Ted Kremenek15322172011-11-10 08:43:12 +0000201 }
202 }
203 return static_cast<CXDiagnosticSetImpl*>(TU->Diagnostics);
204}
205
Douglas Gregor5352ac02010-01-28 00:27:43 +0000206//-----------------------------------------------------------------------------
Ted Kremenekee4db4f2010-02-17 00:41:08 +0000207// C Interface Routines
Douglas Gregor5352ac02010-01-28 00:27:43 +0000208//-----------------------------------------------------------------------------
209extern "C" {
Ted Kremenekee4db4f2010-02-17 00:41:08 +0000210
Douglas Gregora88084b2010-02-18 18:08:43 +0000211unsigned clang_getNumDiagnostics(CXTranslationUnit Unit) {
Dmitri Gribenkoe42e5782013-01-26 21:32:42 +0000212 if (!cxtu::getASTUnit(Unit))
Ted Kremenek15322172011-11-10 08:43:12 +0000213 return 0;
Argyrios Kyrtzidis220b45c2011-11-16 02:34:55 +0000214 return lazyCreateDiags(Unit, /*checkIfChanged=*/true)->getNumDiagnostics();
Douglas Gregora88084b2010-02-18 18:08:43 +0000215}
216
217CXDiagnostic clang_getDiagnostic(CXTranslationUnit Unit, unsigned Index) {
Ted Kremenek0373fcc2011-12-09 22:28:32 +0000218 CXDiagnosticSet D = clang_getDiagnosticSetFromTU(Unit);
219 if (!D)
Douglas Gregora88084b2010-02-18 18:08:43 +0000220 return 0;
221
Ted Kremenek0373fcc2011-12-09 22:28:32 +0000222 CXDiagnosticSetImpl *Diags = static_cast<CXDiagnosticSetImpl*>(D);
Ted Kremenek15322172011-11-10 08:43:12 +0000223 if (Index >= Diags->getNumDiagnostics())
224 return 0;
225
226 return Diags->getDiagnostic(Index);
Douglas Gregora88084b2010-02-18 18:08:43 +0000227}
Ted Kremenek0373fcc2011-12-09 22:28:32 +0000228
229CXDiagnosticSet clang_getDiagnosticSetFromTU(CXTranslationUnit Unit) {
Dmitri Gribenkoe42e5782013-01-26 21:32:42 +0000230 if (!cxtu::getASTUnit(Unit))
Ted Kremenek0373fcc2011-12-09 22:28:32 +0000231 return 0;
232 return static_cast<CXDiagnostic>(lazyCreateDiags(Unit));
233}
Douglas Gregora88084b2010-02-18 18:08:43 +0000234
235void clang_disposeDiagnostic(CXDiagnostic Diagnostic) {
Ted Kremenek15322172011-11-10 08:43:12 +0000236 // No-op. Kept as a legacy API. CXDiagnostics are now managed
237 // by the enclosing CXDiagnosticSet.
Douglas Gregora88084b2010-02-18 18:08:43 +0000238}
239
Douglas Gregor274f1902010-02-22 23:17:23 +0000240CXString clang_formatDiagnostic(CXDiagnostic Diagnostic, unsigned Options) {
241 if (!Diagnostic)
242 return createCXString("");
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000243
244 CXDiagnosticSeverity Severity = clang_getDiagnosticSeverity(Diagnostic);
245
Dylan Noblesmith36d59272012-02-13 12:32:26 +0000246 SmallString<256> Str;
Douglas Gregor274f1902010-02-22 23:17:23 +0000247 llvm::raw_svector_ostream Out(Str);
248
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000249 if (Options & CXDiagnostic_DisplaySourceLocation) {
250 // Print source location (file:line), along with optional column
251 // and source ranges.
252 CXFile File;
253 unsigned Line, Column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000254 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
255 &File, &Line, &Column, 0);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000256 if (File) {
257 CXString FName = clang_getFileName(File);
Douglas Gregor274f1902010-02-22 23:17:23 +0000258 Out << clang_getCString(FName) << ":" << Line << ":";
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000259 clang_disposeString(FName);
260 if (Options & CXDiagnostic_DisplayColumn)
Douglas Gregor274f1902010-02-22 23:17:23 +0000261 Out << Column << ":";
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000262
263 if (Options & CXDiagnostic_DisplaySourceRanges) {
264 unsigned N = clang_getDiagnosticNumRanges(Diagnostic);
265 bool PrintedRange = false;
266 for (unsigned I = 0; I != N; ++I) {
267 CXFile StartFile, EndFile;
268 CXSourceRange Range = clang_getDiagnosticRange(Diagnostic, I);
269
270 unsigned StartLine, StartColumn, EndLine, EndColumn;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000271 clang_getSpellingLocation(clang_getRangeStart(Range),
272 &StartFile, &StartLine, &StartColumn,
273 0);
274 clang_getSpellingLocation(clang_getRangeEnd(Range),
275 &EndFile, &EndLine, &EndColumn, 0);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000276
277 if (StartFile != EndFile || StartFile != File)
278 continue;
279
Douglas Gregor274f1902010-02-22 23:17:23 +0000280 Out << "{" << StartLine << ":" << StartColumn << "-"
281 << EndLine << ":" << EndColumn << "}";
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000282 PrintedRange = true;
283 }
284 if (PrintedRange)
Douglas Gregor274f1902010-02-22 23:17:23 +0000285 Out << ":";
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000286 }
Douglas Gregor4cd912a2010-10-12 00:50:20 +0000287
288 Out << " ";
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000289 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000290 }
291
292 /* Print warning/error/etc. */
293 switch (Severity) {
David Blaikieeb2d1f12011-09-23 20:26:49 +0000294 case CXDiagnostic_Ignored: llvm_unreachable("impossible");
Douglas Gregor274f1902010-02-22 23:17:23 +0000295 case CXDiagnostic_Note: Out << "note: "; break;
296 case CXDiagnostic_Warning: Out << "warning: "; break;
297 case CXDiagnostic_Error: Out << "error: "; break;
298 case CXDiagnostic_Fatal: Out << "fatal error: "; break;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000299 }
300
301 CXString Text = clang_getDiagnosticSpelling(Diagnostic);
302 if (clang_getCString(Text))
Douglas Gregor274f1902010-02-22 23:17:23 +0000303 Out << clang_getCString(Text);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000304 else
Douglas Gregor274f1902010-02-22 23:17:23 +0000305 Out << "<no diagnostic text>";
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000306 clang_disposeString(Text);
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000307
308 if (Options & (CXDiagnostic_DisplayOption | CXDiagnostic_DisplayCategoryId |
309 CXDiagnostic_DisplayCategoryName)) {
310 bool NeedBracket = true;
311 bool NeedComma = false;
312
313 if (Options & CXDiagnostic_DisplayOption) {
314 CXString OptionName = clang_getDiagnosticOption(Diagnostic, 0);
315 if (const char *OptionText = clang_getCString(OptionName)) {
316 if (OptionText[0]) {
317 Out << " [" << OptionText;
318 NeedBracket = false;
319 NeedComma = true;
320 }
321 }
322 clang_disposeString(OptionName);
323 }
324
325 if (Options & (CXDiagnostic_DisplayCategoryId |
326 CXDiagnostic_DisplayCategoryName)) {
327 if (unsigned CategoryID = clang_getDiagnosticCategory(Diagnostic)) {
328 if (Options & CXDiagnostic_DisplayCategoryId) {
329 if (NeedBracket)
330 Out << " [";
331 if (NeedComma)
332 Out << ", ";
333 Out << CategoryID;
334 NeedBracket = false;
335 NeedComma = true;
336 }
337
338 if (Options & CXDiagnostic_DisplayCategoryName) {
Ted Kremenek78d5d3b2012-04-12 00:03:31 +0000339 CXString CategoryName = clang_getDiagnosticCategoryText(Diagnostic);
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000340 if (NeedBracket)
341 Out << " [";
342 if (NeedComma)
343 Out << ", ";
344 Out << clang_getCString(CategoryName);
345 NeedBracket = false;
346 NeedComma = true;
347 clang_disposeString(CategoryName);
348 }
349 }
350 }
Ted Kremenek8e598382012-04-04 00:55:33 +0000351
352 (void) NeedComma; // Silence dead store warning.
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000353 if (!NeedBracket)
354 Out << "]";
355 }
356
Douglas Gregor274f1902010-02-22 23:17:23 +0000357 return createCXString(Out.str(), true);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000358}
359
360unsigned clang_defaultDiagnosticDisplayOptions() {
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000361 return CXDiagnostic_DisplaySourceLocation | CXDiagnostic_DisplayColumn |
362 CXDiagnostic_DisplayOption;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000363}
364
Douglas Gregor5352ac02010-01-28 00:27:43 +0000365enum CXDiagnosticSeverity clang_getDiagnosticSeverity(CXDiagnostic Diag) {
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000366 if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl*>(Diag))
367 return D->getSeverity();
Douglas Gregor5352ac02010-01-28 00:27:43 +0000368 return CXDiagnostic_Ignored;
369}
Ted Kremenekee4db4f2010-02-17 00:41:08 +0000370
Douglas Gregor5352ac02010-01-28 00:27:43 +0000371CXSourceLocation clang_getDiagnosticLocation(CXDiagnostic Diag) {
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000372 if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl*>(Diag))
373 return D->getLocation();
374 return clang_getNullLocation();
Douglas Gregor5352ac02010-01-28 00:27:43 +0000375}
376
377CXString clang_getDiagnosticSpelling(CXDiagnostic Diag) {
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000378 if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
379 return D->getSpelling();
380 return createCXString("");
Douglas Gregor5352ac02010-01-28 00:27:43 +0000381}
382
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000383CXString clang_getDiagnosticOption(CXDiagnostic Diag, CXString *Disable) {
384 if (Disable)
385 *Disable = createCXString("");
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000386
387 if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
388 return D->getDiagnosticOption(Disable);
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000389
390 return createCXString("");
391}
392
393unsigned clang_getDiagnosticCategory(CXDiagnostic Diag) {
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000394 if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
395 return D->getCategory();
396 return 0;
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000397}
398
399CXString clang_getDiagnosticCategoryName(unsigned Category) {
Ted Kremenek78d5d3b2012-04-12 00:03:31 +0000400 // Kept for backwards compatibility.
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000401 return createCXString(DiagnosticIDs::getCategoryNameFromID(Category));
402}
403
Ted Kremenek78d5d3b2012-04-12 00:03:31 +0000404CXString clang_getDiagnosticCategoryText(CXDiagnostic Diag) {
405 if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
406 return D->getCategoryText();
407 return createCXString("");
408}
409
Douglas Gregora3890ba2010-02-08 23:11:56 +0000410unsigned clang_getDiagnosticNumRanges(CXDiagnostic Diag) {
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000411 if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
412 return D->getNumRanges();
413 return 0;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000414}
Ted Kremenekee4db4f2010-02-17 00:41:08 +0000415
Douglas Gregora3890ba2010-02-08 23:11:56 +0000416CXSourceRange clang_getDiagnosticRange(CXDiagnostic Diag, unsigned Range) {
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000417 CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag);
418 if (!D || Range >= D->getNumRanges())
Douglas Gregora3890ba2010-02-08 23:11:56 +0000419 return clang_getNullRange();
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000420 return D->getRange(Range);
Douglas Gregor5352ac02010-01-28 00:27:43 +0000421}
422
423unsigned clang_getDiagnosticNumFixIts(CXDiagnostic Diag) {
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000424 if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
425 return D->getNumFixIts();
426 return 0;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000427}
428
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000429CXString clang_getDiagnosticFixIt(CXDiagnostic Diag, unsigned FixIt,
Douglas Gregor473d7012010-02-19 18:16:06 +0000430 CXSourceRange *ReplacementRange) {
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000431 CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag);
432 if (!D || FixIt >= D->getNumFixIts()) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000433 if (ReplacementRange)
434 *ReplacementRange = clang_getNullRange();
Ted Kremenekee4db4f2010-02-17 00:41:08 +0000435 return createCXString("");
Douglas Gregor5352ac02010-01-28 00:27:43 +0000436 }
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000437 return D->getFixIt(FixIt, ReplacementRange);
Douglas Gregor5352ac02010-01-28 00:27:43 +0000438}
Ted Kremenekee4db4f2010-02-17 00:41:08 +0000439
Ted Kremenek15322172011-11-10 08:43:12 +0000440void clang_disposeDiagnosticSet(CXDiagnosticSet Diags) {
441 CXDiagnosticSetImpl *D = static_cast<CXDiagnosticSetImpl*>(Diags);
442 if (D->isExternallyManaged())
443 delete D;
444}
445
446CXDiagnostic clang_getDiagnosticInSet(CXDiagnosticSet Diags,
447 unsigned Index) {
448 if (CXDiagnosticSetImpl *D = static_cast<CXDiagnosticSetImpl*>(Diags))
449 if (Index < D->getNumDiagnostics())
450 return D->getDiagnostic(Index);
451 return 0;
452}
453
454CXDiagnosticSet clang_getChildDiagnostics(CXDiagnostic Diag) {
455 if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag)) {
456 CXDiagnosticSetImpl &ChildDiags = D->getChildDiagnostics();
457 return ChildDiags.empty() ? 0 : (CXDiagnosticSet) &ChildDiags;
458 }
459 return 0;
460}
461
462unsigned clang_getNumDiagnosticsInSet(CXDiagnosticSet Diags) {
463 if (CXDiagnosticSetImpl *D = static_cast<CXDiagnosticSetImpl*>(Diags))
464 return D->getNumDiagnostics();
465 return 0;
466}
467
Douglas Gregor5352ac02010-01-28 00:27:43 +0000468} // end extern "C"