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