blob: cf9dc6f2407bdf1d07d433e046041e4d235f972c [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;
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +000030using namespace clang::cxdiag;
Douglas Gregora88084b2010-02-18 18:08:43 +000031using namespace llvm;
Douglas Gregor5352ac02010-01-28 00:27:43 +000032
Ted Kremenek15322172011-11-10 08:43:12 +000033
34CXDiagnosticSetImpl::~CXDiagnosticSetImpl() {
35 for (std::vector<CXDiagnosticImpl *>::iterator it = Diagnostics.begin(),
36 et = Diagnostics.end();
37 it != et; ++it) {
38 delete *it;
39 }
40}
41
42CXDiagnosticImpl::~CXDiagnosticImpl() {}
43
Ted Kremenek7473b1c2012-02-14 02:46:03 +000044namespace {
45class CXDiagnosticCustomNoteImpl : public CXDiagnosticImpl {
Ted Kremenek7b8290f2012-02-14 06:54:46 +000046 std::string Message;
Ted Kremenek7473b1c2012-02-14 02:46:03 +000047 CXSourceLocation Loc;
48public:
49 CXDiagnosticCustomNoteImpl(StringRef Msg, CXSourceLocation L)
50 : CXDiagnosticImpl(CustomNoteDiagnosticKind),
Ted Kremenek7b8290f2012-02-14 06:54:46 +000051 Message(Msg), Loc(L) {}
Ted Kremenek7473b1c2012-02-14 02:46:03 +000052
Ted Kremenek7b8290f2012-02-14 06:54:46 +000053 virtual ~CXDiagnosticCustomNoteImpl() {}
Stephen Hines651f13c2014-04-23 16:59:28 -070054
55 CXDiagnosticSeverity getSeverity() const override {
Ted Kremenek7473b1c2012-02-14 02:46:03 +000056 return CXDiagnostic_Note;
57 }
Stephen Hines651f13c2014-04-23 16:59:28 -070058
59 CXSourceLocation getLocation() const override {
Ted Kremenek7473b1c2012-02-14 02:46:03 +000060 return Loc;
61 }
Stephen Hines651f13c2014-04-23 16:59:28 -070062
63 CXString getSpelling() const override {
Dmitri Gribenko5595ded2013-02-02 02:19:29 +000064 return cxstring::createRef(Message.c_str());
Ted Kremenek7473b1c2012-02-14 02:46:03 +000065 }
Stephen Hines651f13c2014-04-23 16:59:28 -070066
67 CXString getDiagnosticOption(CXString *Disable) const override {
Ted Kremenek7473b1c2012-02-14 02:46:03 +000068 if (Disable)
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +000069 *Disable = cxstring::createEmpty();
70 return cxstring::createEmpty();
Ted Kremenek7473b1c2012-02-14 02:46:03 +000071 }
Ted Kremenek78d5d3b2012-04-12 00:03:31 +000072
Stephen Hines651f13c2014-04-23 16:59:28 -070073 unsigned getCategory() const override { return 0; }
74 CXString getCategoryText() const override { return cxstring::createEmpty(); }
75
76 unsigned getNumRanges() const override { return 0; }
77 CXSourceRange getRange(unsigned Range) const override {
78 return clang_getNullRange();
79 }
80 unsigned getNumFixIts() const override { return 0; }
81 CXString getFixIt(unsigned FixIt,
82 CXSourceRange *ReplacementRange) const override {
Ted Kremenek7473b1c2012-02-14 02:46:03 +000083 if (ReplacementRange)
84 *ReplacementRange = clang_getNullRange();
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +000085 return cxstring::createEmpty();
Ted Kremenek7473b1c2012-02-14 02:46:03 +000086 }
87};
88
89class CXDiagnosticRenderer : public DiagnosticNoteRenderer {
90public:
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +000091 CXDiagnosticRenderer(const LangOptions &LangOpts,
Douglas Gregor02c23eb2012-10-23 22:26:28 +000092 DiagnosticOptions *DiagOpts,
Ted Kremenek7473b1c2012-02-14 02:46:03 +000093 CXDiagnosticSetImpl *mainSet)
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +000094 : DiagnosticNoteRenderer(LangOpts, DiagOpts),
Ted Kremenek7473b1c2012-02-14 02:46:03 +000095 CurrentSet(mainSet), MainSet(mainSet) {}
96
97 virtual ~CXDiagnosticRenderer() {}
98
Stephen Hines651f13c2014-04-23 16:59:28 -070099 void beginDiagnostic(DiagOrStoredDiag D,
100 DiagnosticsEngine::Level Level) override {
101
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000102 const StoredDiagnostic *SD = D.dyn_cast<const StoredDiagnostic*>();
103 if (!SD)
104 return;
105
106 if (Level != DiagnosticsEngine::Note)
107 CurrentSet = MainSet;
108
109 CXStoredDiagnostic *CD = new CXStoredDiagnostic(*SD, LangOpts);
110 CurrentSet->appendDiagnostic(CD);
111
112 if (Level != DiagnosticsEngine::Note)
113 CurrentSet = &CD->getChildDiagnostics();
114 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700115
116 void emitDiagnosticMessage(SourceLocation Loc, PresumedLoc PLoc,
117 DiagnosticsEngine::Level Level,
118 StringRef Message,
119 ArrayRef<CharSourceRange> Ranges,
120 const SourceManager *SM,
121 DiagOrStoredDiag D) override {
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000122 if (!D.isNull())
123 return;
124
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000125 CXSourceLocation L;
126 if (SM)
127 L = translateSourceLocation(*SM, LangOpts, Loc);
128 else
129 L = clang_getNullLocation();
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000130 CXDiagnosticImpl *CD = new CXDiagnosticCustomNoteImpl(Message, L);
131 CurrentSet->appendDiagnostic(CD);
132 }
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000133
Stephen Hines651f13c2014-04-23 16:59:28 -0700134 void emitDiagnosticLoc(SourceLocation Loc, PresumedLoc PLoc,
135 DiagnosticsEngine::Level Level,
136 ArrayRef<CharSourceRange> Ranges,
137 const SourceManager &SM) override {}
138
139 void emitCodeContext(SourceLocation Loc,
140 DiagnosticsEngine::Level Level,
141 SmallVectorImpl<CharSourceRange>& Ranges,
142 ArrayRef<FixItHint> Hints,
143 const SourceManager &SM) override {}
144
145 void emitNote(SourceLocation Loc, StringRef Message,
146 const SourceManager *SM) override {
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000147 CXSourceLocation L;
148 if (SM)
149 L = translateSourceLocation(*SM, LangOpts, Loc);
150 else
151 L = clang_getNullLocation();
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000152 CurrentSet->appendDiagnostic(new CXDiagnosticCustomNoteImpl(Message,
153 L));
154 }
155
156 CXDiagnosticSetImpl *CurrentSet;
157 CXDiagnosticSetImpl *MainSet;
158};
159}
160
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +0000161CXDiagnosticSetImpl *cxdiag::lazyCreateDiags(CXTranslationUnit TU,
162 bool checkIfChanged) {
Dmitri Gribenko5694feb2013-01-26 18:53:38 +0000163 ASTUnit *AU = cxtu::getASTUnit(TU);
Argyrios Kyrtzidis220b45c2011-11-16 02:34:55 +0000164
165 if (TU->Diagnostics && checkIfChanged) {
Argyrios Kyrtzidisc88e58c2011-11-16 08:59:00 +0000166 // In normal use, ASTUnit's diagnostics should not change unless we reparse.
167 // Currently they can only change by using the internal testing flag
168 // '-error-on-deserialized-decl' which will error during deserialization of
169 // a declaration. What will happen is:
170 //
171 // -c-index-test gets a CXTranslationUnit
172 // -checks the diagnostics, the diagnostics set is lazily created,
173 // no errors are reported
174 // -later does an operation, like annotation of tokens, that triggers
175 // -error-on-deserialized-decl, that will emit a diagnostic error,
176 // that ASTUnit will catch and add to its stored diagnostics vector.
177 // -c-index-test wants to check whether an error occurred after performing
178 // the operation but can only query the lazily created set.
179 //
180 // We check here if a new diagnostic was appended since the last time the
181 // diagnostic set was created, in which case we reset it.
182
Argyrios Kyrtzidis220b45c2011-11-16 02:34:55 +0000183 CXDiagnosticSetImpl *
184 Set = static_cast<CXDiagnosticSetImpl*>(TU->Diagnostics);
185 if (AU->stored_diag_size() != Set->getNumDiagnostics()) {
186 // Diagnostics in the ASTUnit were updated, reset the associated
187 // diagnostics.
188 delete Set;
189 TU->Diagnostics = 0;
190 }
191 }
192
Ted Kremenek15322172011-11-10 08:43:12 +0000193 if (!TU->Diagnostics) {
Ted Kremenek15322172011-11-10 08:43:12 +0000194 CXDiagnosticSetImpl *Set = new CXDiagnosticSetImpl();
195 TU->Diagnostics = Set;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000196 IntrusiveRefCntPtr<DiagnosticOptions> DOpts = new DiagnosticOptions;
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000197 CXDiagnosticRenderer Renderer(AU->getASTContext().getLangOpts(),
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000198 &*DOpts, Set);
Ted Kremenek15322172011-11-10 08:43:12 +0000199
200 for (ASTUnit::stored_diag_iterator it = AU->stored_diag_begin(),
201 ei = AU->stored_diag_end(); it != ei; ++it) {
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000202 Renderer.emitStoredDiagnostic(*it);
Ted Kremenek15322172011-11-10 08:43:12 +0000203 }
204 }
205 return static_cast<CXDiagnosticSetImpl*>(TU->Diagnostics);
206}
207
Douglas Gregor5352ac02010-01-28 00:27:43 +0000208//-----------------------------------------------------------------------------
Ted Kremenekee4db4f2010-02-17 00:41:08 +0000209// C Interface Routines
Douglas Gregor5352ac02010-01-28 00:27:43 +0000210//-----------------------------------------------------------------------------
211extern "C" {
Ted Kremenekee4db4f2010-02-17 00:41:08 +0000212
Douglas Gregora88084b2010-02-18 18:08:43 +0000213unsigned clang_getNumDiagnostics(CXTranslationUnit Unit) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700214 if (cxtu::isNotUsableTU(Unit)) {
215 LOG_BAD_TU(Unit);
216 return 0;
217 }
Dmitri Gribenkoe42e5782013-01-26 21:32:42 +0000218 if (!cxtu::getASTUnit(Unit))
Ted Kremenek15322172011-11-10 08:43:12 +0000219 return 0;
Argyrios Kyrtzidis220b45c2011-11-16 02:34:55 +0000220 return lazyCreateDiags(Unit, /*checkIfChanged=*/true)->getNumDiagnostics();
Douglas Gregora88084b2010-02-18 18:08:43 +0000221}
222
223CXDiagnostic clang_getDiagnostic(CXTranslationUnit Unit, unsigned Index) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700224 if (cxtu::isNotUsableTU(Unit)) {
225 LOG_BAD_TU(Unit);
226 return 0;
227 }
228
Ted Kremenek0373fcc2011-12-09 22:28:32 +0000229 CXDiagnosticSet D = clang_getDiagnosticSetFromTU(Unit);
230 if (!D)
Douglas Gregora88084b2010-02-18 18:08:43 +0000231 return 0;
232
Ted Kremenek0373fcc2011-12-09 22:28:32 +0000233 CXDiagnosticSetImpl *Diags = static_cast<CXDiagnosticSetImpl*>(D);
Ted Kremenek15322172011-11-10 08:43:12 +0000234 if (Index >= Diags->getNumDiagnostics())
235 return 0;
236
237 return Diags->getDiagnostic(Index);
Douglas Gregora88084b2010-02-18 18:08:43 +0000238}
Stephen Hines651f13c2014-04-23 16:59:28 -0700239
Ted Kremenek0373fcc2011-12-09 22:28:32 +0000240CXDiagnosticSet clang_getDiagnosticSetFromTU(CXTranslationUnit Unit) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700241 if (cxtu::isNotUsableTU(Unit)) {
242 LOG_BAD_TU(Unit);
243 return 0;
244 }
Dmitri Gribenkoe42e5782013-01-26 21:32:42 +0000245 if (!cxtu::getASTUnit(Unit))
Ted Kremenek0373fcc2011-12-09 22:28:32 +0000246 return 0;
247 return static_cast<CXDiagnostic>(lazyCreateDiags(Unit));
248}
Douglas Gregora88084b2010-02-18 18:08:43 +0000249
250void clang_disposeDiagnostic(CXDiagnostic Diagnostic) {
Ted Kremenek15322172011-11-10 08:43:12 +0000251 // No-op. Kept as a legacy API. CXDiagnostics are now managed
252 // by the enclosing CXDiagnosticSet.
Douglas Gregora88084b2010-02-18 18:08:43 +0000253}
254
Douglas Gregor274f1902010-02-22 23:17:23 +0000255CXString clang_formatDiagnostic(CXDiagnostic Diagnostic, unsigned Options) {
256 if (!Diagnostic)
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +0000257 return cxstring::createEmpty();
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000258
259 CXDiagnosticSeverity Severity = clang_getDiagnosticSeverity(Diagnostic);
260
Dylan Noblesmith36d59272012-02-13 12:32:26 +0000261 SmallString<256> Str;
Douglas Gregor274f1902010-02-22 23:17:23 +0000262 llvm::raw_svector_ostream Out(Str);
263
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000264 if (Options & CXDiagnostic_DisplaySourceLocation) {
265 // Print source location (file:line), along with optional column
266 // and source ranges.
267 CXFile File;
268 unsigned Line, Column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000269 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
270 &File, &Line, &Column, 0);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000271 if (File) {
272 CXString FName = clang_getFileName(File);
Douglas Gregor274f1902010-02-22 23:17:23 +0000273 Out << clang_getCString(FName) << ":" << Line << ":";
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000274 clang_disposeString(FName);
275 if (Options & CXDiagnostic_DisplayColumn)
Douglas Gregor274f1902010-02-22 23:17:23 +0000276 Out << Column << ":";
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000277
278 if (Options & CXDiagnostic_DisplaySourceRanges) {
279 unsigned N = clang_getDiagnosticNumRanges(Diagnostic);
280 bool PrintedRange = false;
281 for (unsigned I = 0; I != N; ++I) {
282 CXFile StartFile, EndFile;
283 CXSourceRange Range = clang_getDiagnosticRange(Diagnostic, I);
284
285 unsigned StartLine, StartColumn, EndLine, EndColumn;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000286 clang_getSpellingLocation(clang_getRangeStart(Range),
287 &StartFile, &StartLine, &StartColumn,
288 0);
289 clang_getSpellingLocation(clang_getRangeEnd(Range),
290 &EndFile, &EndLine, &EndColumn, 0);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000291
292 if (StartFile != EndFile || StartFile != File)
293 continue;
294
Douglas Gregor274f1902010-02-22 23:17:23 +0000295 Out << "{" << StartLine << ":" << StartColumn << "-"
296 << EndLine << ":" << EndColumn << "}";
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000297 PrintedRange = true;
298 }
299 if (PrintedRange)
Douglas Gregor274f1902010-02-22 23:17:23 +0000300 Out << ":";
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000301 }
Douglas Gregor4cd912a2010-10-12 00:50:20 +0000302
303 Out << " ";
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000304 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000305 }
306
307 /* Print warning/error/etc. */
308 switch (Severity) {
David Blaikieeb2d1f12011-09-23 20:26:49 +0000309 case CXDiagnostic_Ignored: llvm_unreachable("impossible");
Douglas Gregor274f1902010-02-22 23:17:23 +0000310 case CXDiagnostic_Note: Out << "note: "; break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700311 case CXDiagnostic_Remark: Out << "remark: "; break;
Douglas Gregor274f1902010-02-22 23:17:23 +0000312 case CXDiagnostic_Warning: Out << "warning: "; break;
313 case CXDiagnostic_Error: Out << "error: "; break;
314 case CXDiagnostic_Fatal: Out << "fatal error: "; break;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000315 }
316
317 CXString Text = clang_getDiagnosticSpelling(Diagnostic);
318 if (clang_getCString(Text))
Douglas Gregor274f1902010-02-22 23:17:23 +0000319 Out << clang_getCString(Text);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000320 else
Douglas Gregor274f1902010-02-22 23:17:23 +0000321 Out << "<no diagnostic text>";
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000322 clang_disposeString(Text);
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000323
324 if (Options & (CXDiagnostic_DisplayOption | CXDiagnostic_DisplayCategoryId |
325 CXDiagnostic_DisplayCategoryName)) {
326 bool NeedBracket = true;
327 bool NeedComma = false;
328
329 if (Options & CXDiagnostic_DisplayOption) {
330 CXString OptionName = clang_getDiagnosticOption(Diagnostic, 0);
331 if (const char *OptionText = clang_getCString(OptionName)) {
332 if (OptionText[0]) {
333 Out << " [" << OptionText;
334 NeedBracket = false;
335 NeedComma = true;
336 }
337 }
338 clang_disposeString(OptionName);
339 }
340
341 if (Options & (CXDiagnostic_DisplayCategoryId |
342 CXDiagnostic_DisplayCategoryName)) {
343 if (unsigned CategoryID = clang_getDiagnosticCategory(Diagnostic)) {
344 if (Options & CXDiagnostic_DisplayCategoryId) {
345 if (NeedBracket)
346 Out << " [";
347 if (NeedComma)
348 Out << ", ";
349 Out << CategoryID;
350 NeedBracket = false;
351 NeedComma = true;
352 }
353
354 if (Options & CXDiagnostic_DisplayCategoryName) {
Ted Kremenek78d5d3b2012-04-12 00:03:31 +0000355 CXString CategoryName = clang_getDiagnosticCategoryText(Diagnostic);
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000356 if (NeedBracket)
357 Out << " [";
358 if (NeedComma)
359 Out << ", ";
360 Out << clang_getCString(CategoryName);
361 NeedBracket = false;
362 NeedComma = true;
363 clang_disposeString(CategoryName);
364 }
365 }
366 }
Ted Kremenek8e598382012-04-04 00:55:33 +0000367
368 (void) NeedComma; // Silence dead store warning.
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000369 if (!NeedBracket)
370 Out << "]";
371 }
372
Dmitri Gribenko5595ded2013-02-02 02:19:29 +0000373 return cxstring::createDup(Out.str());
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000374}
375
376unsigned clang_defaultDiagnosticDisplayOptions() {
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000377 return CXDiagnostic_DisplaySourceLocation | CXDiagnostic_DisplayColumn |
378 CXDiagnostic_DisplayOption;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000379}
380
Douglas Gregor5352ac02010-01-28 00:27:43 +0000381enum CXDiagnosticSeverity clang_getDiagnosticSeverity(CXDiagnostic Diag) {
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000382 if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl*>(Diag))
383 return D->getSeverity();
Douglas Gregor5352ac02010-01-28 00:27:43 +0000384 return CXDiagnostic_Ignored;
385}
Ted Kremenekee4db4f2010-02-17 00:41:08 +0000386
Douglas Gregor5352ac02010-01-28 00:27:43 +0000387CXSourceLocation clang_getDiagnosticLocation(CXDiagnostic Diag) {
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000388 if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl*>(Diag))
389 return D->getLocation();
390 return clang_getNullLocation();
Douglas Gregor5352ac02010-01-28 00:27:43 +0000391}
392
393CXString clang_getDiagnosticSpelling(CXDiagnostic Diag) {
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000394 if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
395 return D->getSpelling();
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +0000396 return cxstring::createEmpty();
Douglas Gregor5352ac02010-01-28 00:27:43 +0000397}
398
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000399CXString clang_getDiagnosticOption(CXDiagnostic Diag, CXString *Disable) {
400 if (Disable)
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +0000401 *Disable = cxstring::createEmpty();
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000402
403 if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
404 return D->getDiagnosticOption(Disable);
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000405
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +0000406 return cxstring::createEmpty();
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000407}
408
409unsigned clang_getDiagnosticCategory(CXDiagnostic Diag) {
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000410 if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
411 return D->getCategory();
412 return 0;
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000413}
414
415CXString clang_getDiagnosticCategoryName(unsigned Category) {
Ted Kremenek78d5d3b2012-04-12 00:03:31 +0000416 // Kept for backwards compatibility.
Dmitri Gribenko5595ded2013-02-02 02:19:29 +0000417 return cxstring::createRef(DiagnosticIDs::getCategoryNameFromID(Category));
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000418}
419
Ted Kremenek78d5d3b2012-04-12 00:03:31 +0000420CXString clang_getDiagnosticCategoryText(CXDiagnostic Diag) {
421 if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
422 return D->getCategoryText();
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +0000423 return cxstring::createEmpty();
Ted Kremenek78d5d3b2012-04-12 00:03:31 +0000424}
425
Douglas Gregora3890ba2010-02-08 23:11:56 +0000426unsigned clang_getDiagnosticNumRanges(CXDiagnostic Diag) {
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000427 if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
428 return D->getNumRanges();
429 return 0;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000430}
Ted Kremenekee4db4f2010-02-17 00:41:08 +0000431
Douglas Gregora3890ba2010-02-08 23:11:56 +0000432CXSourceRange clang_getDiagnosticRange(CXDiagnostic Diag, unsigned Range) {
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000433 CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag);
434 if (!D || Range >= D->getNumRanges())
Douglas Gregora3890ba2010-02-08 23:11:56 +0000435 return clang_getNullRange();
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000436 return D->getRange(Range);
Douglas Gregor5352ac02010-01-28 00:27:43 +0000437}
438
439unsigned clang_getDiagnosticNumFixIts(CXDiagnostic Diag) {
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000440 if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
441 return D->getNumFixIts();
442 return 0;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000443}
444
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000445CXString clang_getDiagnosticFixIt(CXDiagnostic Diag, unsigned FixIt,
Douglas Gregor473d7012010-02-19 18:16:06 +0000446 CXSourceRange *ReplacementRange) {
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000447 CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag);
448 if (!D || FixIt >= D->getNumFixIts()) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000449 if (ReplacementRange)
450 *ReplacementRange = clang_getNullRange();
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +0000451 return cxstring::createEmpty();
Douglas Gregor5352ac02010-01-28 00:27:43 +0000452 }
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000453 return D->getFixIt(FixIt, ReplacementRange);
Douglas Gregor5352ac02010-01-28 00:27:43 +0000454}
Ted Kremenekee4db4f2010-02-17 00:41:08 +0000455
Ted Kremenek15322172011-11-10 08:43:12 +0000456void clang_disposeDiagnosticSet(CXDiagnosticSet Diags) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700457 if (CXDiagnosticSetImpl *D = static_cast<CXDiagnosticSetImpl *>(Diags)) {
458 if (D->isExternallyManaged())
459 delete D;
460 }
Ted Kremenek15322172011-11-10 08:43:12 +0000461}
462
463CXDiagnostic clang_getDiagnosticInSet(CXDiagnosticSet Diags,
464 unsigned Index) {
465 if (CXDiagnosticSetImpl *D = static_cast<CXDiagnosticSetImpl*>(Diags))
466 if (Index < D->getNumDiagnostics())
467 return D->getDiagnostic(Index);
468 return 0;
469}
470
471CXDiagnosticSet clang_getChildDiagnostics(CXDiagnostic Diag) {
472 if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag)) {
473 CXDiagnosticSetImpl &ChildDiags = D->getChildDiagnostics();
474 return ChildDiags.empty() ? 0 : (CXDiagnosticSet) &ChildDiags;
475 }
476 return 0;
477}
478
479unsigned clang_getNumDiagnosticsInSet(CXDiagnosticSet Diags) {
480 if (CXDiagnosticSetImpl *D = static_cast<CXDiagnosticSetImpl*>(Diags))
481 return D->getNumDiagnostics();
482 return 0;
483}
484
Douglas Gregor5352ac02010-01-28 00:27:43 +0000485} // end extern "C"