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