blob: a29e3a60e04b604388b700760492339a7ef57767 [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"
Douglas Gregor274f1902010-02-22 23:17:23 +000021#include "llvm/ADT/SmallString.h"
Douglas Gregora88084b2010-02-18 18:08:43 +000022#include "llvm/ADT/Twine.h"
Douglas Gregord93256e2010-01-28 06:00:51 +000023#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor274f1902010-02-22 23:17:23 +000024#include "llvm/Support/raw_ostream.h"
Douglas Gregord93256e2010-01-28 06:00:51 +000025
Douglas Gregor5352ac02010-01-28 00:27:43 +000026using namespace clang;
27using namespace clang::cxloc;
Ted Kremenekee4db4f2010-02-17 00:41:08 +000028using namespace clang::cxstring;
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +000029using namespace clang::cxdiag;
Douglas Gregora88084b2010-02-18 18:08:43 +000030using namespace llvm;
Douglas Gregor5352ac02010-01-28 00:27:43 +000031
Ted Kremenek15322172011-11-10 08:43:12 +000032
33CXDiagnosticSetImpl::~CXDiagnosticSetImpl() {
34 for (std::vector<CXDiagnosticImpl *>::iterator it = Diagnostics.begin(),
35 et = Diagnostics.end();
36 it != et; ++it) {
37 delete *it;
38 }
39}
40
41CXDiagnosticImpl::~CXDiagnosticImpl() {}
42
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +000043CXDiagnosticSetImpl *cxdiag::lazyCreateDiags(CXTranslationUnit TU,
44 bool checkIfChanged) {
Argyrios Kyrtzidis220b45c2011-11-16 02:34:55 +000045 ASTUnit *AU = static_cast<ASTUnit *>(TU->TUData);
46
47 if (TU->Diagnostics && checkIfChanged) {
Argyrios Kyrtzidisc88e58c2011-11-16 08:59:00 +000048 // 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 Kyrtzidis220b45c2011-11-16 02:34:55 +000065 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 Kremenek15322172011-11-10 08:43:12 +000075 if (!TU->Diagnostics) {
Ted Kremenek15322172011-11-10 08:43:12 +000076 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 Gregor5352ac02010-01-28 00:27:43 +000089//-----------------------------------------------------------------------------
Ted Kremenekee4db4f2010-02-17 00:41:08 +000090// C Interface Routines
Douglas Gregor5352ac02010-01-28 00:27:43 +000091//-----------------------------------------------------------------------------
92extern "C" {
Ted Kremenekee4db4f2010-02-17 00:41:08 +000093
Douglas Gregora88084b2010-02-18 18:08:43 +000094unsigned clang_getNumDiagnostics(CXTranslationUnit Unit) {
Ted Kremenek15322172011-11-10 08:43:12 +000095 if (!Unit->TUData)
96 return 0;
Argyrios Kyrtzidis220b45c2011-11-16 02:34:55 +000097 return lazyCreateDiags(Unit, /*checkIfChanged=*/true)->getNumDiagnostics();
Douglas Gregora88084b2010-02-18 18:08:43 +000098}
99
100CXDiagnostic clang_getDiagnostic(CXTranslationUnit Unit, unsigned Index) {
Ted Kremenek15322172011-11-10 08:43:12 +0000101 if (!Unit->TUData)
Douglas Gregora88084b2010-02-18 18:08:43 +0000102 return 0;
103
Ted Kremenek15322172011-11-10 08:43:12 +0000104 CXDiagnosticSetImpl *Diags = lazyCreateDiags(Unit);
105 if (Index >= Diags->getNumDiagnostics())
106 return 0;
107
108 return Diags->getDiagnostic(Index);
Douglas Gregora88084b2010-02-18 18:08:43 +0000109}
110
111void clang_disposeDiagnostic(CXDiagnostic Diagnostic) {
Ted Kremenek15322172011-11-10 08:43:12 +0000112 // No-op. Kept as a legacy API. CXDiagnostics are now managed
113 // by the enclosing CXDiagnosticSet.
Douglas Gregora88084b2010-02-18 18:08:43 +0000114}
115
Douglas Gregor274f1902010-02-22 23:17:23 +0000116CXString clang_formatDiagnostic(CXDiagnostic Diagnostic, unsigned Options) {
117 if (!Diagnostic)
118 return createCXString("");
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000119
120 CXDiagnosticSeverity Severity = clang_getDiagnosticSeverity(Diagnostic);
121
Douglas Gregor274f1902010-02-22 23:17:23 +0000122 llvm::SmallString<256> Str;
123 llvm::raw_svector_ostream Out(Str);
124
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000125 if (Options & CXDiagnostic_DisplaySourceLocation) {
126 // Print source location (file:line), along with optional column
127 // and source ranges.
128 CXFile File;
129 unsigned Line, Column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000130 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
131 &File, &Line, &Column, 0);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000132 if (File) {
133 CXString FName = clang_getFileName(File);
Douglas Gregor274f1902010-02-22 23:17:23 +0000134 Out << clang_getCString(FName) << ":" << Line << ":";
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000135 clang_disposeString(FName);
136 if (Options & CXDiagnostic_DisplayColumn)
Douglas Gregor274f1902010-02-22 23:17:23 +0000137 Out << Column << ":";
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000138
139 if (Options & CXDiagnostic_DisplaySourceRanges) {
140 unsigned N = clang_getDiagnosticNumRanges(Diagnostic);
141 bool PrintedRange = false;
142 for (unsigned I = 0; I != N; ++I) {
143 CXFile StartFile, EndFile;
144 CXSourceRange Range = clang_getDiagnosticRange(Diagnostic, I);
145
146 unsigned StartLine, StartColumn, EndLine, EndColumn;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000147 clang_getSpellingLocation(clang_getRangeStart(Range),
148 &StartFile, &StartLine, &StartColumn,
149 0);
150 clang_getSpellingLocation(clang_getRangeEnd(Range),
151 &EndFile, &EndLine, &EndColumn, 0);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000152
153 if (StartFile != EndFile || StartFile != File)
154 continue;
155
Douglas Gregor274f1902010-02-22 23:17:23 +0000156 Out << "{" << StartLine << ":" << StartColumn << "-"
157 << EndLine << ":" << EndColumn << "}";
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000158 PrintedRange = true;
159 }
160 if (PrintedRange)
Douglas Gregor274f1902010-02-22 23:17:23 +0000161 Out << ":";
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000162 }
Douglas Gregor4cd912a2010-10-12 00:50:20 +0000163
164 Out << " ";
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000165 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000166 }
167
168 /* Print warning/error/etc. */
169 switch (Severity) {
David Blaikieeb2d1f12011-09-23 20:26:49 +0000170 case CXDiagnostic_Ignored: llvm_unreachable("impossible");
Douglas Gregor274f1902010-02-22 23:17:23 +0000171 case CXDiagnostic_Note: Out << "note: "; break;
172 case CXDiagnostic_Warning: Out << "warning: "; break;
173 case CXDiagnostic_Error: Out << "error: "; break;
174 case CXDiagnostic_Fatal: Out << "fatal error: "; break;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000175 }
176
177 CXString Text = clang_getDiagnosticSpelling(Diagnostic);
178 if (clang_getCString(Text))
Douglas Gregor274f1902010-02-22 23:17:23 +0000179 Out << clang_getCString(Text);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000180 else
Douglas Gregor274f1902010-02-22 23:17:23 +0000181 Out << "<no diagnostic text>";
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000182 clang_disposeString(Text);
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000183
184 if (Options & (CXDiagnostic_DisplayOption | CXDiagnostic_DisplayCategoryId |
185 CXDiagnostic_DisplayCategoryName)) {
186 bool NeedBracket = true;
187 bool NeedComma = false;
188
189 if (Options & CXDiagnostic_DisplayOption) {
190 CXString OptionName = clang_getDiagnosticOption(Diagnostic, 0);
191 if (const char *OptionText = clang_getCString(OptionName)) {
192 if (OptionText[0]) {
193 Out << " [" << OptionText;
194 NeedBracket = false;
195 NeedComma = true;
196 }
197 }
198 clang_disposeString(OptionName);
199 }
200
201 if (Options & (CXDiagnostic_DisplayCategoryId |
202 CXDiagnostic_DisplayCategoryName)) {
203 if (unsigned CategoryID = clang_getDiagnosticCategory(Diagnostic)) {
204 if (Options & CXDiagnostic_DisplayCategoryId) {
205 if (NeedBracket)
206 Out << " [";
207 if (NeedComma)
208 Out << ", ";
209 Out << CategoryID;
210 NeedBracket = false;
211 NeedComma = true;
212 }
213
214 if (Options & CXDiagnostic_DisplayCategoryName) {
215 CXString CategoryName = clang_getDiagnosticCategoryName(CategoryID);
216 if (NeedBracket)
217 Out << " [";
218 if (NeedComma)
219 Out << ", ";
220 Out << clang_getCString(CategoryName);
221 NeedBracket = false;
222 NeedComma = true;
223 clang_disposeString(CategoryName);
224 }
225 }
226 }
227
228 if (!NeedBracket)
229 Out << "]";
230 }
231
Douglas Gregor274f1902010-02-22 23:17:23 +0000232 return createCXString(Out.str(), true);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000233}
234
235unsigned clang_defaultDiagnosticDisplayOptions() {
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000236 return CXDiagnostic_DisplaySourceLocation | CXDiagnostic_DisplayColumn |
237 CXDiagnostic_DisplayOption;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000238}
239
Douglas Gregor5352ac02010-01-28 00:27:43 +0000240enum CXDiagnosticSeverity clang_getDiagnosticSeverity(CXDiagnostic Diag) {
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000241 if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl*>(Diag))
242 return D->getSeverity();
Douglas Gregor5352ac02010-01-28 00:27:43 +0000243 return CXDiagnostic_Ignored;
244}
Ted Kremenekee4db4f2010-02-17 00:41:08 +0000245
Douglas Gregor5352ac02010-01-28 00:27:43 +0000246CXSourceLocation clang_getDiagnosticLocation(CXDiagnostic Diag) {
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000247 if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl*>(Diag))
248 return D->getLocation();
249 return clang_getNullLocation();
Douglas Gregor5352ac02010-01-28 00:27:43 +0000250}
251
252CXString clang_getDiagnosticSpelling(CXDiagnostic Diag) {
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000253 if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
254 return D->getSpelling();
255 return createCXString("");
Douglas Gregor5352ac02010-01-28 00:27:43 +0000256}
257
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000258CXString clang_getDiagnosticOption(CXDiagnostic Diag, CXString *Disable) {
259 if (Disable)
260 *Disable = createCXString("");
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000261
262 if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
263 return D->getDiagnosticOption(Disable);
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000264
265 return createCXString("");
266}
267
268unsigned clang_getDiagnosticCategory(CXDiagnostic Diag) {
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000269 if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
270 return D->getCategory();
271 return 0;
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000272}
273
274CXString clang_getDiagnosticCategoryName(unsigned Category) {
275 return createCXString(DiagnosticIDs::getCategoryNameFromID(Category));
276}
277
Douglas Gregora3890ba2010-02-08 23:11:56 +0000278unsigned clang_getDiagnosticNumRanges(CXDiagnostic Diag) {
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000279 if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
280 return D->getNumRanges();
281 return 0;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000282}
Ted Kremenekee4db4f2010-02-17 00:41:08 +0000283
Douglas Gregora3890ba2010-02-08 23:11:56 +0000284CXSourceRange clang_getDiagnosticRange(CXDiagnostic Diag, unsigned Range) {
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000285 CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag);
286 if (!D || Range >= D->getNumRanges())
Douglas Gregora3890ba2010-02-08 23:11:56 +0000287 return clang_getNullRange();
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000288 return D->getRange(Range);
Douglas Gregor5352ac02010-01-28 00:27:43 +0000289}
290
291unsigned clang_getDiagnosticNumFixIts(CXDiagnostic Diag) {
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000292 if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
293 return D->getNumFixIts();
294 return 0;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000295}
296
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000297CXString clang_getDiagnosticFixIt(CXDiagnostic Diag, unsigned FixIt,
Douglas Gregor473d7012010-02-19 18:16:06 +0000298 CXSourceRange *ReplacementRange) {
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000299 CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag);
300 if (!D || FixIt >= D->getNumFixIts()) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000301 if (ReplacementRange)
302 *ReplacementRange = clang_getNullRange();
Ted Kremenekee4db4f2010-02-17 00:41:08 +0000303 return createCXString("");
Douglas Gregor5352ac02010-01-28 00:27:43 +0000304 }
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000305 return D->getFixIt(FixIt, ReplacementRange);
Douglas Gregor5352ac02010-01-28 00:27:43 +0000306}
Ted Kremenekee4db4f2010-02-17 00:41:08 +0000307
Ted Kremenek15322172011-11-10 08:43:12 +0000308void clang_disposeDiagnosticSet(CXDiagnosticSet Diags) {
309 CXDiagnosticSetImpl *D = static_cast<CXDiagnosticSetImpl*>(Diags);
310 if (D->isExternallyManaged())
311 delete D;
312}
313
314CXDiagnostic clang_getDiagnosticInSet(CXDiagnosticSet Diags,
315 unsigned Index) {
316 if (CXDiagnosticSetImpl *D = static_cast<CXDiagnosticSetImpl*>(Diags))
317 if (Index < D->getNumDiagnostics())
318 return D->getDiagnostic(Index);
319 return 0;
320}
321
322CXDiagnosticSet clang_getChildDiagnostics(CXDiagnostic Diag) {
323 if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag)) {
324 CXDiagnosticSetImpl &ChildDiags = D->getChildDiagnostics();
325 return ChildDiags.empty() ? 0 : (CXDiagnosticSet) &ChildDiags;
326 }
327 return 0;
328}
329
330unsigned clang_getNumDiagnosticsInSet(CXDiagnosticSet Diags) {
331 if (CXDiagnosticSetImpl *D = static_cast<CXDiagnosticSetImpl*>(Diags))
332 return D->getNumDiagnostics();
333 return 0;
334}
335
Douglas Gregor5352ac02010-01-28 00:27:43 +0000336} // end extern "C"