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" |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 22 | #include "clang/Basic/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; } |
Ted Kremenek | 78d5d3b | 2012-04-12 00:03:31 +0000 | [diff] [blame] | 75 | CXString getCategoryText() const { return createCXString(""); } |
| 76 | |
Ted Kremenek | 7473b1c | 2012-02-14 02:46:03 +0000 | [diff] [blame] | 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: |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 89 | CXDiagnosticRenderer(const LangOptions &LangOpts, |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 90 | DiagnosticOptions *DiagOpts, |
Ted Kremenek | 7473b1c | 2012-02-14 02:46:03 +0000 | [diff] [blame] | 91 | CXDiagnosticSetImpl *mainSet) |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 92 | : DiagnosticNoteRenderer(LangOpts, DiagOpts), |
Ted Kremenek | 7473b1c | 2012-02-14 02:46:03 +0000 | [diff] [blame] | 93 | 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 Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 118 | const SourceManager *SM, |
Ted Kremenek | 7473b1c | 2012-02-14 02:46:03 +0000 | [diff] [blame] | 119 | DiagOrStoredDiag D) { |
| 120 | if (!D.isNull()) |
| 121 | return; |
| 122 | |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 123 | CXSourceLocation L; |
| 124 | if (SM) |
| 125 | L = translateSourceLocation(*SM, LangOpts, Loc); |
| 126 | else |
| 127 | L = clang_getNullLocation(); |
Ted Kremenek | 7473b1c | 2012-02-14 02:46:03 +0000 | [diff] [blame] | 128 | 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 Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 134 | ArrayRef<CharSourceRange> Ranges, |
| 135 | const SourceManager &SM) {} |
Ted Kremenek | 7473b1c | 2012-02-14 02:46:03 +0000 | [diff] [blame] | 136 | |
| 137 | virtual void emitCodeContext(SourceLocation Loc, |
| 138 | DiagnosticsEngine::Level Level, |
| 139 | SmallVectorImpl<CharSourceRange>& Ranges, |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 140 | ArrayRef<FixItHint> Hints, |
| 141 | const SourceManager &SM) {} |
Ted Kremenek | 7473b1c | 2012-02-14 02:46:03 +0000 | [diff] [blame] | 142 | |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 143 | 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 Kremenek | 7473b1c | 2012-02-14 02:46:03 +0000 | [diff] [blame] | 150 | CurrentSet->appendDiagnostic(new CXDiagnosticCustomNoteImpl(Message, |
| 151 | L)); |
| 152 | } |
| 153 | |
| 154 | CXDiagnosticSetImpl *CurrentSet; |
| 155 | CXDiagnosticSetImpl *MainSet; |
| 156 | }; |
| 157 | } |
| 158 | |
Argyrios Kyrtzidis | 996e6e5 | 2011-12-01 02:42:50 +0000 | [diff] [blame] | 159 | CXDiagnosticSetImpl *cxdiag::lazyCreateDiags(CXTranslationUnit TU, |
| 160 | bool checkIfChanged) { |
Argyrios Kyrtzidis | 220b45c | 2011-11-16 02:34:55 +0000 | [diff] [blame] | 161 | ASTUnit *AU = static_cast<ASTUnit *>(TU->TUData); |
| 162 | |
| 163 | if (TU->Diagnostics && checkIfChanged) { |
Argyrios Kyrtzidis | c88e58c | 2011-11-16 08:59:00 +0000 | [diff] [blame] | 164 | // 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 Kyrtzidis | 220b45c | 2011-11-16 02:34:55 +0000 | [diff] [blame] | 181 | 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 Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 191 | if (!TU->Diagnostics) { |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 192 | CXDiagnosticSetImpl *Set = new CXDiagnosticSetImpl(); |
| 193 | TU->Diagnostics = Set; |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame^] | 194 | IntrusiveRefCntPtr<DiagnosticOptions> DOpts = new DiagnosticOptions; |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 195 | CXDiagnosticRenderer Renderer(AU->getASTContext().getLangOpts(), |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 196 | &*DOpts, Set); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 197 | |
| 198 | for (ASTUnit::stored_diag_iterator it = AU->stored_diag_begin(), |
| 199 | ei = AU->stored_diag_end(); it != ei; ++it) { |
Ted Kremenek | 7473b1c | 2012-02-14 02:46:03 +0000 | [diff] [blame] | 200 | Renderer.emitStoredDiagnostic(*it); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | return static_cast<CXDiagnosticSetImpl*>(TU->Diagnostics); |
| 204 | } |
| 205 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 206 | //----------------------------------------------------------------------------- |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 207 | // C Interface Routines |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 208 | //----------------------------------------------------------------------------- |
| 209 | extern "C" { |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 210 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 211 | unsigned clang_getNumDiagnostics(CXTranslationUnit Unit) { |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 212 | if (!Unit->TUData) |
| 213 | return 0; |
Argyrios Kyrtzidis | 220b45c | 2011-11-16 02:34:55 +0000 | [diff] [blame] | 214 | return lazyCreateDiags(Unit, /*checkIfChanged=*/true)->getNumDiagnostics(); |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | CXDiagnostic clang_getDiagnostic(CXTranslationUnit Unit, unsigned Index) { |
Ted Kremenek | 0373fcc | 2011-12-09 22:28:32 +0000 | [diff] [blame] | 218 | CXDiagnosticSet D = clang_getDiagnosticSetFromTU(Unit); |
| 219 | if (!D) |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 220 | return 0; |
| 221 | |
Ted Kremenek | 0373fcc | 2011-12-09 22:28:32 +0000 | [diff] [blame] | 222 | CXDiagnosticSetImpl *Diags = static_cast<CXDiagnosticSetImpl*>(D); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 223 | if (Index >= Diags->getNumDiagnostics()) |
| 224 | return 0; |
| 225 | |
| 226 | return Diags->getDiagnostic(Index); |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 227 | } |
Ted Kremenek | 0373fcc | 2011-12-09 22:28:32 +0000 | [diff] [blame] | 228 | |
| 229 | CXDiagnosticSet clang_getDiagnosticSetFromTU(CXTranslationUnit Unit) { |
| 230 | if (!Unit->TUData) |
| 231 | return 0; |
| 232 | return static_cast<CXDiagnostic>(lazyCreateDiags(Unit)); |
| 233 | } |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 234 | |
| 235 | void clang_disposeDiagnostic(CXDiagnostic Diagnostic) { |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 236 | // No-op. Kept as a legacy API. CXDiagnostics are now managed |
| 237 | // by the enclosing CXDiagnosticSet. |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 240 | CXString clang_formatDiagnostic(CXDiagnostic Diagnostic, unsigned Options) { |
| 241 | if (!Diagnostic) |
| 242 | return createCXString(""); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 243 | |
| 244 | CXDiagnosticSeverity Severity = clang_getDiagnosticSeverity(Diagnostic); |
| 245 | |
Dylan Noblesmith | 36d5927 | 2012-02-13 12:32:26 +0000 | [diff] [blame] | 246 | SmallString<256> Str; |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 247 | llvm::raw_svector_ostream Out(Str); |
| 248 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 249 | 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 Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 254 | clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic), |
| 255 | &File, &Line, &Column, 0); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 256 | if (File) { |
| 257 | CXString FName = clang_getFileName(File); |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 258 | Out << clang_getCString(FName) << ":" << Line << ":"; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 259 | clang_disposeString(FName); |
| 260 | if (Options & CXDiagnostic_DisplayColumn) |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 261 | Out << Column << ":"; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 262 | |
| 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 Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 271 | clang_getSpellingLocation(clang_getRangeStart(Range), |
| 272 | &StartFile, &StartLine, &StartColumn, |
| 273 | 0); |
| 274 | clang_getSpellingLocation(clang_getRangeEnd(Range), |
| 275 | &EndFile, &EndLine, &EndColumn, 0); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 276 | |
| 277 | if (StartFile != EndFile || StartFile != File) |
| 278 | continue; |
| 279 | |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 280 | Out << "{" << StartLine << ":" << StartColumn << "-" |
| 281 | << EndLine << ":" << EndColumn << "}"; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 282 | PrintedRange = true; |
| 283 | } |
| 284 | if (PrintedRange) |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 285 | Out << ":"; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 286 | } |
Douglas Gregor | 4cd912a | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 287 | |
| 288 | Out << " "; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 289 | } |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | /* Print warning/error/etc. */ |
| 293 | switch (Severity) { |
David Blaikie | eb2d1f1 | 2011-09-23 20:26:49 +0000 | [diff] [blame] | 294 | case CXDiagnostic_Ignored: llvm_unreachable("impossible"); |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 295 | 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 Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | CXString Text = clang_getDiagnosticSpelling(Diagnostic); |
| 302 | if (clang_getCString(Text)) |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 303 | Out << clang_getCString(Text); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 304 | else |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 305 | Out << "<no diagnostic text>"; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 306 | clang_disposeString(Text); |
Douglas Gregor | aa5f135 | 2010-11-19 16:18:16 +0000 | [diff] [blame] | 307 | |
| 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 Kremenek | 78d5d3b | 2012-04-12 00:03:31 +0000 | [diff] [blame] | 339 | CXString CategoryName = clang_getDiagnosticCategoryText(Diagnostic); |
Douglas Gregor | aa5f135 | 2010-11-19 16:18:16 +0000 | [diff] [blame] | 340 | 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 Kremenek | 8e59838 | 2012-04-04 00:55:33 +0000 | [diff] [blame] | 351 | |
| 352 | (void) NeedComma; // Silence dead store warning. |
Douglas Gregor | aa5f135 | 2010-11-19 16:18:16 +0000 | [diff] [blame] | 353 | if (!NeedBracket) |
| 354 | Out << "]"; |
| 355 | } |
| 356 | |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 357 | return createCXString(Out.str(), true); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | unsigned clang_defaultDiagnosticDisplayOptions() { |
Douglas Gregor | aa5f135 | 2010-11-19 16:18:16 +0000 | [diff] [blame] | 361 | return CXDiagnostic_DisplaySourceLocation | CXDiagnostic_DisplayColumn | |
| 362 | CXDiagnostic_DisplayOption; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 365 | enum CXDiagnosticSeverity clang_getDiagnosticSeverity(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->getSeverity(); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 368 | return CXDiagnostic_Ignored; |
| 369 | } |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 370 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 371 | CXSourceLocation clang_getDiagnosticLocation(CXDiagnostic Diag) { |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 372 | if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl*>(Diag)) |
| 373 | return D->getLocation(); |
| 374 | return clang_getNullLocation(); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | CXString clang_getDiagnosticSpelling(CXDiagnostic Diag) { |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 378 | if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag)) |
| 379 | return D->getSpelling(); |
| 380 | return createCXString(""); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Douglas Gregor | aa5f135 | 2010-11-19 16:18:16 +0000 | [diff] [blame] | 383 | CXString clang_getDiagnosticOption(CXDiagnostic Diag, CXString *Disable) { |
| 384 | if (Disable) |
| 385 | *Disable = createCXString(""); |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 386 | |
| 387 | if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag)) |
| 388 | return D->getDiagnosticOption(Disable); |
Douglas Gregor | aa5f135 | 2010-11-19 16:18:16 +0000 | [diff] [blame] | 389 | |
| 390 | return createCXString(""); |
| 391 | } |
| 392 | |
| 393 | unsigned clang_getDiagnosticCategory(CXDiagnostic Diag) { |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 394 | if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag)) |
| 395 | return D->getCategory(); |
| 396 | return 0; |
Douglas Gregor | aa5f135 | 2010-11-19 16:18:16 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | CXString clang_getDiagnosticCategoryName(unsigned Category) { |
Ted Kremenek | 78d5d3b | 2012-04-12 00:03:31 +0000 | [diff] [blame] | 400 | // Kept for backwards compatibility. |
Douglas Gregor | aa5f135 | 2010-11-19 16:18:16 +0000 | [diff] [blame] | 401 | return createCXString(DiagnosticIDs::getCategoryNameFromID(Category)); |
| 402 | } |
| 403 | |
Ted Kremenek | 78d5d3b | 2012-04-12 00:03:31 +0000 | [diff] [blame] | 404 | CXString clang_getDiagnosticCategoryText(CXDiagnostic Diag) { |
| 405 | if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag)) |
| 406 | return D->getCategoryText(); |
| 407 | return createCXString(""); |
| 408 | } |
| 409 | |
Douglas Gregor | a3890ba | 2010-02-08 23:11:56 +0000 | [diff] [blame] | 410 | unsigned clang_getDiagnosticNumRanges(CXDiagnostic Diag) { |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 411 | if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag)) |
| 412 | return D->getNumRanges(); |
| 413 | return 0; |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 414 | } |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 415 | |
Douglas Gregor | a3890ba | 2010-02-08 23:11:56 +0000 | [diff] [blame] | 416 | CXSourceRange clang_getDiagnosticRange(CXDiagnostic Diag, unsigned Range) { |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 417 | CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag); |
| 418 | if (!D || Range >= D->getNumRanges()) |
Douglas Gregor | a3890ba | 2010-02-08 23:11:56 +0000 | [diff] [blame] | 419 | return clang_getNullRange(); |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 420 | return D->getRange(Range); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | unsigned clang_getDiagnosticNumFixIts(CXDiagnostic Diag) { |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 424 | if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag)) |
| 425 | return D->getNumFixIts(); |
| 426 | return 0; |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 429 | CXString clang_getDiagnosticFixIt(CXDiagnostic Diag, unsigned FixIt, |
Douglas Gregor | 473d701 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 430 | CXSourceRange *ReplacementRange) { |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 431 | CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag); |
| 432 | if (!D || FixIt >= D->getNumFixIts()) { |
Douglas Gregor | 473d701 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 433 | if (ReplacementRange) |
| 434 | *ReplacementRange = clang_getNullRange(); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 435 | return createCXString(""); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 436 | } |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 437 | return D->getFixIt(FixIt, ReplacementRange); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 438 | } |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 439 | |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 440 | void clang_disposeDiagnosticSet(CXDiagnosticSet Diags) { |
| 441 | CXDiagnosticSetImpl *D = static_cast<CXDiagnosticSetImpl*>(Diags); |
| 442 | if (D->isExternallyManaged()) |
| 443 | delete D; |
| 444 | } |
| 445 | |
| 446 | CXDiagnostic 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 | |
| 454 | CXDiagnosticSet 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 | |
| 462 | unsigned clang_getNumDiagnosticsInSet(CXDiagnosticSet Diags) { |
| 463 | if (CXDiagnosticSetImpl *D = static_cast<CXDiagnosticSetImpl*>(Diags)) |
| 464 | return D->getNumDiagnostics(); |
| 465 | return 0; |
| 466 | } |
| 467 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 468 | } // end extern "C" |