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