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" |
Ted Kremenek | 7473b1c | 2012-02-14 02:46:03 +0000 | [diff] [blame^] | 21 | #include "clang/Frontend/DiagnosticRenderer.h" |
| 22 | #include "clang/Frontend/DiagnosticOptions.h" |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/Twine.h" |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MemoryBuffer.h" |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 27 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 28 | using namespace clang; |
| 29 | using namespace clang::cxloc; |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 30 | using namespace clang::cxstring; |
Argyrios Kyrtzidis | 996e6e5 | 2011-12-01 02:42:50 +0000 | [diff] [blame] | 31 | using namespace clang::cxdiag; |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 32 | using namespace llvm; |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 33 | |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 34 | |
| 35 | CXDiagnosticSetImpl::~CXDiagnosticSetImpl() { |
| 36 | for (std::vector<CXDiagnosticImpl *>::iterator it = Diagnostics.begin(), |
| 37 | et = Diagnostics.end(); |
| 38 | it != et; ++it) { |
| 39 | delete *it; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | CXDiagnosticImpl::~CXDiagnosticImpl() {} |
| 44 | |
Ted Kremenek | 7473b1c | 2012-02-14 02:46:03 +0000 | [diff] [blame^] | 45 | namespace { |
| 46 | class CXDiagnosticCustomNoteImpl : public CXDiagnosticImpl { |
| 47 | CXString Message; |
| 48 | CXSourceLocation Loc; |
| 49 | public: |
| 50 | CXDiagnosticCustomNoteImpl(StringRef Msg, CXSourceLocation L) |
| 51 | : CXDiagnosticImpl(CustomNoteDiagnosticKind), |
| 52 | Message(createCXString(Msg)), Loc(L) {} |
| 53 | |
| 54 | virtual ~CXDiagnosticCustomNoteImpl() { |
| 55 | clang_disposeString(Message); |
| 56 | } |
| 57 | |
| 58 | CXDiagnosticSeverity getSeverity() const { |
| 59 | return CXDiagnostic_Note; |
| 60 | } |
| 61 | |
| 62 | CXSourceLocation getLocation() const { |
| 63 | return Loc; |
| 64 | } |
| 65 | |
| 66 | CXString getSpelling() const { |
| 67 | return Message; |
| 68 | } |
| 69 | |
| 70 | CXString getDiagnosticOption(CXString *Disable) const { |
| 71 | if (Disable) |
| 72 | *Disable = createCXString("", false); |
| 73 | return createCXString("", false); |
| 74 | } |
| 75 | |
| 76 | unsigned getCategory() const { return 0; } |
| 77 | 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 | |
| 87 | class CXDiagnosticRenderer : public DiagnosticNoteRenderer { |
| 88 | public: |
| 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, |
| 135 | ArrayRef<FixItHint> Hints) {}; |
| 136 | |
| 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 Kyrtzidis | 996e6e5 | 2011-12-01 02:42:50 +0000 | [diff] [blame] | 148 | CXDiagnosticSetImpl *cxdiag::lazyCreateDiags(CXTranslationUnit TU, |
| 149 | bool checkIfChanged) { |
Argyrios Kyrtzidis | 220b45c | 2011-11-16 02:34:55 +0000 | [diff] [blame] | 150 | ASTUnit *AU = static_cast<ASTUnit *>(TU->TUData); |
| 151 | |
| 152 | if (TU->Diagnostics && checkIfChanged) { |
Argyrios Kyrtzidis | c88e58c | 2011-11-16 08:59:00 +0000 | [diff] [blame] | 153 | // 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 Kyrtzidis | 220b45c | 2011-11-16 02:34:55 +0000 | [diff] [blame] | 170 | 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 Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 180 | if (!TU->Diagnostics) { |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 181 | CXDiagnosticSetImpl *Set = new CXDiagnosticSetImpl(); |
| 182 | TU->Diagnostics = Set; |
Ted Kremenek | 7473b1c | 2012-02-14 02:46:03 +0000 | [diff] [blame^] | 183 | DiagnosticOptions DOpts; |
| 184 | CXDiagnosticRenderer Renderer(AU->getSourceManager(), |
| 185 | AU->getASTContext().getLangOptions(), |
| 186 | DOpts, Set); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 187 | |
| 188 | for (ASTUnit::stored_diag_iterator it = AU->stored_diag_begin(), |
| 189 | ei = AU->stored_diag_end(); it != ei; ++it) { |
Ted Kremenek | 7473b1c | 2012-02-14 02:46:03 +0000 | [diff] [blame^] | 190 | Renderer.emitStoredDiagnostic(*it); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | return static_cast<CXDiagnosticSetImpl*>(TU->Diagnostics); |
| 194 | } |
| 195 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 196 | //----------------------------------------------------------------------------- |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 197 | // C Interface Routines |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 198 | //----------------------------------------------------------------------------- |
| 199 | extern "C" { |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 200 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 201 | unsigned clang_getNumDiagnostics(CXTranslationUnit Unit) { |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 202 | if (!Unit->TUData) |
| 203 | return 0; |
Argyrios Kyrtzidis | 220b45c | 2011-11-16 02:34:55 +0000 | [diff] [blame] | 204 | return lazyCreateDiags(Unit, /*checkIfChanged=*/true)->getNumDiagnostics(); |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | CXDiagnostic clang_getDiagnostic(CXTranslationUnit Unit, unsigned Index) { |
Ted Kremenek | 0373fcc | 2011-12-09 22:28:32 +0000 | [diff] [blame] | 208 | CXDiagnosticSet D = clang_getDiagnosticSetFromTU(Unit); |
| 209 | if (!D) |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 210 | return 0; |
| 211 | |
Ted Kremenek | 0373fcc | 2011-12-09 22:28:32 +0000 | [diff] [blame] | 212 | CXDiagnosticSetImpl *Diags = static_cast<CXDiagnosticSetImpl*>(D); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 213 | if (Index >= Diags->getNumDiagnostics()) |
| 214 | return 0; |
| 215 | |
| 216 | return Diags->getDiagnostic(Index); |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 217 | } |
Ted Kremenek | 0373fcc | 2011-12-09 22:28:32 +0000 | [diff] [blame] | 218 | |
| 219 | CXDiagnosticSet clang_getDiagnosticSetFromTU(CXTranslationUnit Unit) { |
| 220 | if (!Unit->TUData) |
| 221 | return 0; |
| 222 | return static_cast<CXDiagnostic>(lazyCreateDiags(Unit)); |
| 223 | } |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 224 | |
| 225 | void clang_disposeDiagnostic(CXDiagnostic Diagnostic) { |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 226 | // No-op. Kept as a legacy API. CXDiagnostics are now managed |
| 227 | // by the enclosing CXDiagnosticSet. |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 230 | CXString clang_formatDiagnostic(CXDiagnostic Diagnostic, unsigned Options) { |
| 231 | if (!Diagnostic) |
| 232 | return createCXString(""); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 233 | |
| 234 | CXDiagnosticSeverity Severity = clang_getDiagnosticSeverity(Diagnostic); |
| 235 | |
Dylan Noblesmith | 36d5927 | 2012-02-13 12:32:26 +0000 | [diff] [blame] | 236 | SmallString<256> Str; |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 237 | llvm::raw_svector_ostream Out(Str); |
| 238 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 239 | 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 Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 244 | clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic), |
| 245 | &File, &Line, &Column, 0); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 246 | if (File) { |
| 247 | CXString FName = clang_getFileName(File); |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 248 | Out << clang_getCString(FName) << ":" << Line << ":"; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 249 | clang_disposeString(FName); |
| 250 | if (Options & CXDiagnostic_DisplayColumn) |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 251 | Out << Column << ":"; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 252 | |
| 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 Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 261 | clang_getSpellingLocation(clang_getRangeStart(Range), |
| 262 | &StartFile, &StartLine, &StartColumn, |
| 263 | 0); |
| 264 | clang_getSpellingLocation(clang_getRangeEnd(Range), |
| 265 | &EndFile, &EndLine, &EndColumn, 0); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 266 | |
| 267 | if (StartFile != EndFile || StartFile != File) |
| 268 | continue; |
| 269 | |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 270 | Out << "{" << StartLine << ":" << StartColumn << "-" |
| 271 | << EndLine << ":" << EndColumn << "}"; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 272 | PrintedRange = true; |
| 273 | } |
| 274 | if (PrintedRange) |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 275 | Out << ":"; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 276 | } |
Douglas Gregor | 4cd912a | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 277 | |
| 278 | Out << " "; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 279 | } |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | /* Print warning/error/etc. */ |
| 283 | switch (Severity) { |
David Blaikie | eb2d1f1 | 2011-09-23 20:26:49 +0000 | [diff] [blame] | 284 | case CXDiagnostic_Ignored: llvm_unreachable("impossible"); |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 285 | 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 Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | CXString Text = clang_getDiagnosticSpelling(Diagnostic); |
| 292 | if (clang_getCString(Text)) |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 293 | Out << clang_getCString(Text); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 294 | else |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 295 | Out << "<no diagnostic text>"; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 296 | clang_disposeString(Text); |
Douglas Gregor | aa5f135 | 2010-11-19 16:18:16 +0000 | [diff] [blame] | 297 | |
| 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) { |
| 329 | CXString CategoryName = clang_getDiagnosticCategoryName(CategoryID); |
| 330 | 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 | } |
| 341 | |
| 342 | if (!NeedBracket) |
| 343 | Out << "]"; |
| 344 | } |
| 345 | |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 346 | return createCXString(Out.str(), true); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | unsigned clang_defaultDiagnosticDisplayOptions() { |
Douglas Gregor | aa5f135 | 2010-11-19 16:18:16 +0000 | [diff] [blame] | 350 | return CXDiagnostic_DisplaySourceLocation | CXDiagnostic_DisplayColumn | |
| 351 | CXDiagnostic_DisplayOption; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 354 | enum CXDiagnosticSeverity clang_getDiagnosticSeverity(CXDiagnostic Diag) { |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 355 | if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl*>(Diag)) |
| 356 | return D->getSeverity(); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 357 | return CXDiagnostic_Ignored; |
| 358 | } |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 359 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 360 | CXSourceLocation clang_getDiagnosticLocation(CXDiagnostic Diag) { |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 361 | if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl*>(Diag)) |
| 362 | return D->getLocation(); |
| 363 | return clang_getNullLocation(); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | CXString clang_getDiagnosticSpelling(CXDiagnostic Diag) { |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 367 | if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag)) |
| 368 | return D->getSpelling(); |
| 369 | return createCXString(""); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Douglas Gregor | aa5f135 | 2010-11-19 16:18:16 +0000 | [diff] [blame] | 372 | CXString clang_getDiagnosticOption(CXDiagnostic Diag, CXString *Disable) { |
| 373 | if (Disable) |
| 374 | *Disable = createCXString(""); |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 375 | |
| 376 | if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag)) |
| 377 | return D->getDiagnosticOption(Disable); |
Douglas Gregor | aa5f135 | 2010-11-19 16:18:16 +0000 | [diff] [blame] | 378 | |
| 379 | return createCXString(""); |
| 380 | } |
| 381 | |
| 382 | unsigned clang_getDiagnosticCategory(CXDiagnostic Diag) { |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 383 | if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag)) |
| 384 | return D->getCategory(); |
| 385 | return 0; |
Douglas Gregor | aa5f135 | 2010-11-19 16:18:16 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | CXString clang_getDiagnosticCategoryName(unsigned Category) { |
| 389 | return createCXString(DiagnosticIDs::getCategoryNameFromID(Category)); |
| 390 | } |
| 391 | |
Douglas Gregor | a3890ba | 2010-02-08 23:11:56 +0000 | [diff] [blame] | 392 | unsigned clang_getDiagnosticNumRanges(CXDiagnostic Diag) { |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 393 | if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag)) |
| 394 | return D->getNumRanges(); |
| 395 | return 0; |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 396 | } |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 397 | |
Douglas Gregor | a3890ba | 2010-02-08 23:11:56 +0000 | [diff] [blame] | 398 | CXSourceRange clang_getDiagnosticRange(CXDiagnostic Diag, unsigned Range) { |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 399 | CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag); |
| 400 | if (!D || Range >= D->getNumRanges()) |
Douglas Gregor | a3890ba | 2010-02-08 23:11:56 +0000 | [diff] [blame] | 401 | return clang_getNullRange(); |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 402 | return D->getRange(Range); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | unsigned clang_getDiagnosticNumFixIts(CXDiagnostic Diag) { |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 406 | if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag)) |
| 407 | return D->getNumFixIts(); |
| 408 | return 0; |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 411 | CXString clang_getDiagnosticFixIt(CXDiagnostic Diag, unsigned FixIt, |
Douglas Gregor | 473d701 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 412 | CXSourceRange *ReplacementRange) { |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 413 | CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag); |
| 414 | if (!D || FixIt >= D->getNumFixIts()) { |
Douglas Gregor | 473d701 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 415 | if (ReplacementRange) |
| 416 | *ReplacementRange = clang_getNullRange(); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 417 | return createCXString(""); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 418 | } |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 419 | return D->getFixIt(FixIt, ReplacementRange); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 420 | } |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 421 | |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 422 | void clang_disposeDiagnosticSet(CXDiagnosticSet Diags) { |
| 423 | CXDiagnosticSetImpl *D = static_cast<CXDiagnosticSetImpl*>(Diags); |
| 424 | if (D->isExternallyManaged()) |
| 425 | delete D; |
| 426 | } |
| 427 | |
| 428 | CXDiagnostic clang_getDiagnosticInSet(CXDiagnosticSet Diags, |
| 429 | unsigned Index) { |
| 430 | if (CXDiagnosticSetImpl *D = static_cast<CXDiagnosticSetImpl*>(Diags)) |
| 431 | if (Index < D->getNumDiagnostics()) |
| 432 | return D->getDiagnostic(Index); |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | CXDiagnosticSet clang_getChildDiagnostics(CXDiagnostic Diag) { |
| 437 | if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag)) { |
| 438 | CXDiagnosticSetImpl &ChildDiags = D->getChildDiagnostics(); |
| 439 | return ChildDiags.empty() ? 0 : (CXDiagnosticSet) &ChildDiags; |
| 440 | } |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | unsigned clang_getNumDiagnosticsInSet(CXDiagnosticSet Diags) { |
| 445 | if (CXDiagnosticSetImpl *D = static_cast<CXDiagnosticSetImpl*>(Diags)) |
| 446 | return D->getNumDiagnostics(); |
| 447 | return 0; |
| 448 | } |
| 449 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 450 | } // end extern "C" |