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