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 { |
Ted Kremenek | 7b8290f | 2012-02-14 06:54:46 +0000 | [diff] [blame] | 47 | std::string Message; |
Ted Kremenek | 7473b1c | 2012-02-14 02:46:03 +0000 | [diff] [blame] | 48 | CXSourceLocation Loc; |
| 49 | public: |
| 50 | CXDiagnosticCustomNoteImpl(StringRef Msg, CXSourceLocation L) |
| 51 | : CXDiagnosticImpl(CustomNoteDiagnosticKind), |
Ted Kremenek | 7b8290f | 2012-02-14 06:54:46 +0000 | [diff] [blame] | 52 | Message(Msg), Loc(L) {} |
Ted Kremenek | 7473b1c | 2012-02-14 02:46:03 +0000 | [diff] [blame] | 53 | |
Ted Kremenek | 7b8290f | 2012-02-14 06:54:46 +0000 | [diff] [blame] | 54 | virtual ~CXDiagnosticCustomNoteImpl() {} |
Ted Kremenek | 7473b1c | 2012-02-14 02:46:03 +0000 | [diff] [blame] | 55 | |
| 56 | CXDiagnosticSeverity getSeverity() const { |
| 57 | return CXDiagnostic_Note; |
| 58 | } |
| 59 | |
| 60 | CXSourceLocation getLocation() const { |
| 61 | return Loc; |
| 62 | } |
| 63 | |
| 64 | CXString getSpelling() const { |
Ted Kremenek | 7b8290f | 2012-02-14 06:54:46 +0000 | [diff] [blame] | 65 | return createCXString(StringRef(Message), false); |
Ted Kremenek | 7473b1c | 2012-02-14 02:46:03 +0000 | [diff] [blame] | 66 | } |
| 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; } |
| 75 | unsigned getNumRanges() const { return 0; } |
| 76 | CXSourceRange getRange(unsigned Range) const { return clang_getNullRange(); } |
| 77 | unsigned getNumFixIts() const { return 0; } |
| 78 | CXString getFixIt(unsigned FixIt, CXSourceRange *ReplacementRange) const { |
| 79 | if (ReplacementRange) |
| 80 | *ReplacementRange = clang_getNullRange(); |
| 81 | return createCXString("", false); |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | class CXDiagnosticRenderer : public DiagnosticNoteRenderer { |
| 86 | public: |
| 87 | CXDiagnosticRenderer(const SourceManager &SM, |
| 88 | const LangOptions &LangOpts, |
| 89 | const DiagnosticOptions &DiagOpts, |
| 90 | CXDiagnosticSetImpl *mainSet) |
| 91 | : DiagnosticNoteRenderer(SM, LangOpts, DiagOpts), |
| 92 | CurrentSet(mainSet), MainSet(mainSet) {} |
| 93 | |
| 94 | virtual ~CXDiagnosticRenderer() {} |
| 95 | |
| 96 | virtual void beginDiagnostic(DiagOrStoredDiag D, |
| 97 | DiagnosticsEngine::Level Level) { |
| 98 | |
| 99 | const StoredDiagnostic *SD = D.dyn_cast<const StoredDiagnostic*>(); |
| 100 | if (!SD) |
| 101 | return; |
| 102 | |
| 103 | if (Level != DiagnosticsEngine::Note) |
| 104 | CurrentSet = MainSet; |
| 105 | |
| 106 | CXStoredDiagnostic *CD = new CXStoredDiagnostic(*SD, LangOpts); |
| 107 | CurrentSet->appendDiagnostic(CD); |
| 108 | |
| 109 | if (Level != DiagnosticsEngine::Note) |
| 110 | CurrentSet = &CD->getChildDiagnostics(); |
| 111 | } |
| 112 | |
| 113 | virtual void emitDiagnosticMessage(SourceLocation Loc, PresumedLoc PLoc, |
| 114 | DiagnosticsEngine::Level Level, |
| 115 | StringRef Message, |
| 116 | ArrayRef<CharSourceRange> Ranges, |
| 117 | DiagOrStoredDiag D) { |
| 118 | if (!D.isNull()) |
| 119 | return; |
| 120 | |
| 121 | CXSourceLocation L = translateSourceLocation(SM, LangOpts, Loc); |
| 122 | CXDiagnosticImpl *CD = new CXDiagnosticCustomNoteImpl(Message, L); |
| 123 | CurrentSet->appendDiagnostic(CD); |
| 124 | } |
| 125 | |
| 126 | virtual void emitDiagnosticLoc(SourceLocation Loc, PresumedLoc PLoc, |
| 127 | DiagnosticsEngine::Level Level, |
| 128 | ArrayRef<CharSourceRange> Ranges) {} |
| 129 | |
| 130 | virtual void emitCodeContext(SourceLocation Loc, |
| 131 | DiagnosticsEngine::Level Level, |
| 132 | SmallVectorImpl<CharSourceRange>& Ranges, |
Daniel Dunbar | fc399c7 | 2012-02-29 00:20:42 +0000 | [diff] [blame] | 133 | ArrayRef<FixItHint> Hints) {} |
Ted Kremenek | 7473b1c | 2012-02-14 02:46:03 +0000 | [diff] [blame] | 134 | |
| 135 | virtual void emitNote(SourceLocation Loc, StringRef Message) { |
| 136 | CXSourceLocation L = translateSourceLocation(SM, LangOpts, Loc); |
| 137 | CurrentSet->appendDiagnostic(new CXDiagnosticCustomNoteImpl(Message, |
| 138 | L)); |
| 139 | } |
| 140 | |
| 141 | CXDiagnosticSetImpl *CurrentSet; |
| 142 | CXDiagnosticSetImpl *MainSet; |
| 143 | }; |
| 144 | } |
| 145 | |
Argyrios Kyrtzidis | 996e6e5 | 2011-12-01 02:42:50 +0000 | [diff] [blame] | 146 | CXDiagnosticSetImpl *cxdiag::lazyCreateDiags(CXTranslationUnit TU, |
| 147 | bool checkIfChanged) { |
Argyrios Kyrtzidis | 220b45c | 2011-11-16 02:34:55 +0000 | [diff] [blame] | 148 | ASTUnit *AU = static_cast<ASTUnit *>(TU->TUData); |
| 149 | |
| 150 | if (TU->Diagnostics && checkIfChanged) { |
Argyrios Kyrtzidis | c88e58c | 2011-11-16 08:59:00 +0000 | [diff] [blame] | 151 | // In normal use, ASTUnit's diagnostics should not change unless we reparse. |
| 152 | // Currently they can only change by using the internal testing flag |
| 153 | // '-error-on-deserialized-decl' which will error during deserialization of |
| 154 | // a declaration. What will happen is: |
| 155 | // |
| 156 | // -c-index-test gets a CXTranslationUnit |
| 157 | // -checks the diagnostics, the diagnostics set is lazily created, |
| 158 | // no errors are reported |
| 159 | // -later does an operation, like annotation of tokens, that triggers |
| 160 | // -error-on-deserialized-decl, that will emit a diagnostic error, |
| 161 | // that ASTUnit will catch and add to its stored diagnostics vector. |
| 162 | // -c-index-test wants to check whether an error occurred after performing |
| 163 | // the operation but can only query the lazily created set. |
| 164 | // |
| 165 | // We check here if a new diagnostic was appended since the last time the |
| 166 | // diagnostic set was created, in which case we reset it. |
| 167 | |
Argyrios Kyrtzidis | 220b45c | 2011-11-16 02:34:55 +0000 | [diff] [blame] | 168 | CXDiagnosticSetImpl * |
| 169 | Set = static_cast<CXDiagnosticSetImpl*>(TU->Diagnostics); |
| 170 | if (AU->stored_diag_size() != Set->getNumDiagnostics()) { |
| 171 | // Diagnostics in the ASTUnit were updated, reset the associated |
| 172 | // diagnostics. |
| 173 | delete Set; |
| 174 | TU->Diagnostics = 0; |
| 175 | } |
| 176 | } |
| 177 | |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 178 | if (!TU->Diagnostics) { |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 179 | CXDiagnosticSetImpl *Set = new CXDiagnosticSetImpl(); |
| 180 | TU->Diagnostics = Set; |
Ted Kremenek | 7473b1c | 2012-02-14 02:46:03 +0000 | [diff] [blame] | 181 | DiagnosticOptions DOpts; |
| 182 | CXDiagnosticRenderer Renderer(AU->getSourceManager(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 183 | AU->getASTContext().getLangOpts(), |
Ted Kremenek | 7473b1c | 2012-02-14 02:46:03 +0000 | [diff] [blame] | 184 | DOpts, Set); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 185 | |
| 186 | for (ASTUnit::stored_diag_iterator it = AU->stored_diag_begin(), |
| 187 | ei = AU->stored_diag_end(); it != ei; ++it) { |
Ted Kremenek | 7473b1c | 2012-02-14 02:46:03 +0000 | [diff] [blame] | 188 | Renderer.emitStoredDiagnostic(*it); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | return static_cast<CXDiagnosticSetImpl*>(TU->Diagnostics); |
| 192 | } |
| 193 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 194 | //----------------------------------------------------------------------------- |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 195 | // C Interface Routines |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 196 | //----------------------------------------------------------------------------- |
| 197 | extern "C" { |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 198 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 199 | unsigned clang_getNumDiagnostics(CXTranslationUnit Unit) { |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 200 | if (!Unit->TUData) |
| 201 | return 0; |
Argyrios Kyrtzidis | 220b45c | 2011-11-16 02:34:55 +0000 | [diff] [blame] | 202 | return lazyCreateDiags(Unit, /*checkIfChanged=*/true)->getNumDiagnostics(); |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | CXDiagnostic clang_getDiagnostic(CXTranslationUnit Unit, unsigned Index) { |
Ted Kremenek | 0373fcc | 2011-12-09 22:28:32 +0000 | [diff] [blame] | 206 | CXDiagnosticSet D = clang_getDiagnosticSetFromTU(Unit); |
| 207 | if (!D) |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 208 | return 0; |
| 209 | |
Ted Kremenek | 0373fcc | 2011-12-09 22:28:32 +0000 | [diff] [blame] | 210 | CXDiagnosticSetImpl *Diags = static_cast<CXDiagnosticSetImpl*>(D); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 211 | if (Index >= Diags->getNumDiagnostics()) |
| 212 | return 0; |
| 213 | |
| 214 | return Diags->getDiagnostic(Index); |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 215 | } |
Ted Kremenek | 0373fcc | 2011-12-09 22:28:32 +0000 | [diff] [blame] | 216 | |
| 217 | CXDiagnosticSet clang_getDiagnosticSetFromTU(CXTranslationUnit Unit) { |
| 218 | if (!Unit->TUData) |
| 219 | return 0; |
| 220 | return static_cast<CXDiagnostic>(lazyCreateDiags(Unit)); |
| 221 | } |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 222 | |
| 223 | void clang_disposeDiagnostic(CXDiagnostic Diagnostic) { |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 224 | // No-op. Kept as a legacy API. CXDiagnostics are now managed |
| 225 | // by the enclosing CXDiagnosticSet. |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 228 | CXString clang_formatDiagnostic(CXDiagnostic Diagnostic, unsigned Options) { |
| 229 | if (!Diagnostic) |
| 230 | return createCXString(""); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 231 | |
| 232 | CXDiagnosticSeverity Severity = clang_getDiagnosticSeverity(Diagnostic); |
| 233 | |
Dylan Noblesmith | 36d5927 | 2012-02-13 12:32:26 +0000 | [diff] [blame] | 234 | SmallString<256> Str; |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 235 | llvm::raw_svector_ostream Out(Str); |
| 236 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 237 | if (Options & CXDiagnostic_DisplaySourceLocation) { |
| 238 | // Print source location (file:line), along with optional column |
| 239 | // and source ranges. |
| 240 | CXFile File; |
| 241 | unsigned Line, Column; |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 242 | clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic), |
| 243 | &File, &Line, &Column, 0); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 244 | if (File) { |
| 245 | CXString FName = clang_getFileName(File); |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 246 | Out << clang_getCString(FName) << ":" << Line << ":"; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 247 | clang_disposeString(FName); |
| 248 | if (Options & CXDiagnostic_DisplayColumn) |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 249 | Out << Column << ":"; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 250 | |
| 251 | if (Options & CXDiagnostic_DisplaySourceRanges) { |
| 252 | unsigned N = clang_getDiagnosticNumRanges(Diagnostic); |
| 253 | bool PrintedRange = false; |
| 254 | for (unsigned I = 0; I != N; ++I) { |
| 255 | CXFile StartFile, EndFile; |
| 256 | CXSourceRange Range = clang_getDiagnosticRange(Diagnostic, I); |
| 257 | |
| 258 | unsigned StartLine, StartColumn, EndLine, EndColumn; |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 259 | clang_getSpellingLocation(clang_getRangeStart(Range), |
| 260 | &StartFile, &StartLine, &StartColumn, |
| 261 | 0); |
| 262 | clang_getSpellingLocation(clang_getRangeEnd(Range), |
| 263 | &EndFile, &EndLine, &EndColumn, 0); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 264 | |
| 265 | if (StartFile != EndFile || StartFile != File) |
| 266 | continue; |
| 267 | |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 268 | Out << "{" << StartLine << ":" << StartColumn << "-" |
| 269 | << EndLine << ":" << EndColumn << "}"; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 270 | PrintedRange = true; |
| 271 | } |
| 272 | if (PrintedRange) |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 273 | Out << ":"; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 274 | } |
Douglas Gregor | 4cd912a | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 275 | |
| 276 | Out << " "; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 277 | } |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /* Print warning/error/etc. */ |
| 281 | switch (Severity) { |
David Blaikie | eb2d1f1 | 2011-09-23 20:26:49 +0000 | [diff] [blame] | 282 | case CXDiagnostic_Ignored: llvm_unreachable("impossible"); |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 283 | case CXDiagnostic_Note: Out << "note: "; break; |
| 284 | case CXDiagnostic_Warning: Out << "warning: "; break; |
| 285 | case CXDiagnostic_Error: Out << "error: "; break; |
| 286 | case CXDiagnostic_Fatal: Out << "fatal error: "; break; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | CXString Text = clang_getDiagnosticSpelling(Diagnostic); |
| 290 | if (clang_getCString(Text)) |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 291 | Out << clang_getCString(Text); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 292 | else |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 293 | Out << "<no diagnostic text>"; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 294 | clang_disposeString(Text); |
Douglas Gregor | aa5f135 | 2010-11-19 16:18:16 +0000 | [diff] [blame] | 295 | |
| 296 | if (Options & (CXDiagnostic_DisplayOption | CXDiagnostic_DisplayCategoryId | |
| 297 | CXDiagnostic_DisplayCategoryName)) { |
| 298 | bool NeedBracket = true; |
| 299 | bool NeedComma = false; |
| 300 | |
| 301 | if (Options & CXDiagnostic_DisplayOption) { |
| 302 | CXString OptionName = clang_getDiagnosticOption(Diagnostic, 0); |
| 303 | if (const char *OptionText = clang_getCString(OptionName)) { |
| 304 | if (OptionText[0]) { |
| 305 | Out << " [" << OptionText; |
| 306 | NeedBracket = false; |
| 307 | NeedComma = true; |
| 308 | } |
| 309 | } |
| 310 | clang_disposeString(OptionName); |
| 311 | } |
| 312 | |
| 313 | if (Options & (CXDiagnostic_DisplayCategoryId | |
| 314 | CXDiagnostic_DisplayCategoryName)) { |
| 315 | if (unsigned CategoryID = clang_getDiagnosticCategory(Diagnostic)) { |
| 316 | if (Options & CXDiagnostic_DisplayCategoryId) { |
| 317 | if (NeedBracket) |
| 318 | Out << " ["; |
| 319 | if (NeedComma) |
| 320 | Out << ", "; |
| 321 | Out << CategoryID; |
| 322 | NeedBracket = false; |
| 323 | NeedComma = true; |
| 324 | } |
| 325 | |
| 326 | if (Options & CXDiagnostic_DisplayCategoryName) { |
| 327 | CXString CategoryName = clang_getDiagnosticCategoryName(CategoryID); |
| 328 | if (NeedBracket) |
| 329 | Out << " ["; |
| 330 | if (NeedComma) |
| 331 | Out << ", "; |
| 332 | Out << clang_getCString(CategoryName); |
| 333 | NeedBracket = false; |
| 334 | NeedComma = true; |
| 335 | clang_disposeString(CategoryName); |
| 336 | } |
| 337 | } |
| 338 | } |
Ted Kremenek | 8e59838 | 2012-04-04 00:55:33 +0000 | [diff] [blame] | 339 | |
| 340 | (void) NeedComma; // Silence dead store warning. |
Douglas Gregor | aa5f135 | 2010-11-19 16:18:16 +0000 | [diff] [blame] | 341 | if (!NeedBracket) |
| 342 | Out << "]"; |
| 343 | } |
| 344 | |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 345 | return createCXString(Out.str(), true); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | unsigned clang_defaultDiagnosticDisplayOptions() { |
Douglas Gregor | aa5f135 | 2010-11-19 16:18:16 +0000 | [diff] [blame] | 349 | return CXDiagnostic_DisplaySourceLocation | CXDiagnostic_DisplayColumn | |
| 350 | CXDiagnostic_DisplayOption; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 353 | enum CXDiagnosticSeverity clang_getDiagnosticSeverity(CXDiagnostic Diag) { |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 354 | if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl*>(Diag)) |
| 355 | return D->getSeverity(); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 356 | return CXDiagnostic_Ignored; |
| 357 | } |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 358 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 359 | CXSourceLocation clang_getDiagnosticLocation(CXDiagnostic Diag) { |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 360 | if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl*>(Diag)) |
| 361 | return D->getLocation(); |
| 362 | return clang_getNullLocation(); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | CXString clang_getDiagnosticSpelling(CXDiagnostic Diag) { |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 366 | if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag)) |
| 367 | return D->getSpelling(); |
| 368 | return createCXString(""); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Douglas Gregor | aa5f135 | 2010-11-19 16:18:16 +0000 | [diff] [blame] | 371 | CXString clang_getDiagnosticOption(CXDiagnostic Diag, CXString *Disable) { |
| 372 | if (Disable) |
| 373 | *Disable = createCXString(""); |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 374 | |
| 375 | if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag)) |
| 376 | return D->getDiagnosticOption(Disable); |
Douglas Gregor | aa5f135 | 2010-11-19 16:18:16 +0000 | [diff] [blame] | 377 | |
| 378 | return createCXString(""); |
| 379 | } |
| 380 | |
| 381 | unsigned clang_getDiagnosticCategory(CXDiagnostic Diag) { |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 382 | if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag)) |
| 383 | return D->getCategory(); |
| 384 | return 0; |
Douglas Gregor | aa5f135 | 2010-11-19 16:18:16 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | CXString clang_getDiagnosticCategoryName(unsigned Category) { |
| 388 | return createCXString(DiagnosticIDs::getCategoryNameFromID(Category)); |
| 389 | } |
| 390 | |
Douglas Gregor | a3890ba | 2010-02-08 23:11:56 +0000 | [diff] [blame] | 391 | unsigned clang_getDiagnosticNumRanges(CXDiagnostic Diag) { |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 392 | if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag)) |
| 393 | return D->getNumRanges(); |
| 394 | return 0; |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 395 | } |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 396 | |
Douglas Gregor | a3890ba | 2010-02-08 23:11:56 +0000 | [diff] [blame] | 397 | CXSourceRange clang_getDiagnosticRange(CXDiagnostic Diag, unsigned Range) { |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 398 | CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag); |
| 399 | if (!D || Range >= D->getNumRanges()) |
Douglas Gregor | a3890ba | 2010-02-08 23:11:56 +0000 | [diff] [blame] | 400 | return clang_getNullRange(); |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 401 | return D->getRange(Range); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | unsigned clang_getDiagnosticNumFixIts(CXDiagnostic Diag) { |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 405 | if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag)) |
| 406 | return D->getNumFixIts(); |
| 407 | return 0; |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 410 | CXString clang_getDiagnosticFixIt(CXDiagnostic Diag, unsigned FixIt, |
Douglas Gregor | 473d701 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 411 | CXSourceRange *ReplacementRange) { |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 412 | CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag); |
| 413 | if (!D || FixIt >= D->getNumFixIts()) { |
Douglas Gregor | 473d701 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 414 | if (ReplacementRange) |
| 415 | *ReplacementRange = clang_getNullRange(); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 416 | return createCXString(""); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 417 | } |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 418 | return D->getFixIt(FixIt, ReplacementRange); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 419 | } |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 420 | |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 421 | void clang_disposeDiagnosticSet(CXDiagnosticSet Diags) { |
| 422 | CXDiagnosticSetImpl *D = static_cast<CXDiagnosticSetImpl*>(Diags); |
| 423 | if (D->isExternallyManaged()) |
| 424 | delete D; |
| 425 | } |
| 426 | |
| 427 | CXDiagnostic clang_getDiagnosticInSet(CXDiagnosticSet Diags, |
| 428 | unsigned Index) { |
| 429 | if (CXDiagnosticSetImpl *D = static_cast<CXDiagnosticSetImpl*>(Diags)) |
| 430 | if (Index < D->getNumDiagnostics()) |
| 431 | return D->getDiagnostic(Index); |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | CXDiagnosticSet clang_getChildDiagnostics(CXDiagnostic Diag) { |
| 436 | if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag)) { |
| 437 | CXDiagnosticSetImpl &ChildDiags = D->getChildDiagnostics(); |
| 438 | return ChildDiags.empty() ? 0 : (CXDiagnosticSet) &ChildDiags; |
| 439 | } |
| 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | unsigned clang_getNumDiagnosticsInSet(CXDiagnosticSet Diags) { |
| 444 | if (CXDiagnosticSetImpl *D = static_cast<CXDiagnosticSetImpl*>(Diags)) |
| 445 | return D->getNumDiagnostics(); |
| 446 | return 0; |
| 447 | } |
| 448 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 449 | } // end extern "C" |