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