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