Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 1 | /*===-- CIndexDiagnostics.cpp - Diagnostics C Interface ---------*- C++ -*-===*\ |
Douglas Gregor | 5352ac0 | 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 | 0a90d32 | 2010-11-17 23:24:11 +0000 | [diff] [blame] | 15 | #include "CXTranslationUnit.h" |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 16 | #include "CXSourceLocation.h" |
Ted Kremenek | ed12273 | 2010-11-16 01:56:27 +0000 | [diff] [blame] | 17 | #include "CXString.h" |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 18 | |
Benjamin Kramer | b846deb | 2010-04-12 19:45:50 +0000 | [diff] [blame] | 19 | #include "clang/Frontend/ASTUnit.h" |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 20 | #include "clang/Frontend/FrontendDiagnostic.h" |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/Twine.h" |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MemoryBuffer.h" |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 25 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | using namespace clang::cxloc; |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 28 | using namespace clang::cxstring; |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 29 | using namespace llvm; |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 30 | |
| 31 | //----------------------------------------------------------------------------- |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 32 | // C Interface Routines |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 33 | //----------------------------------------------------------------------------- |
| 34 | extern "C" { |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 35 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 36 | unsigned clang_getNumDiagnostics(CXTranslationUnit Unit) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 37 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(Unit->TUData); |
Douglas Gregor | 405634b | 2010-04-05 18:10:21 +0000 | [diff] [blame] | 38 | return CXXUnit? CXXUnit->stored_diag_size() : 0; |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | CXDiagnostic clang_getDiagnostic(CXTranslationUnit Unit, unsigned Index) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 42 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(Unit->TUData); |
Douglas Gregor | 405634b | 2010-04-05 18:10:21 +0000 | [diff] [blame] | 43 | if (!CXXUnit || Index >= CXXUnit->stored_diag_size()) |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 44 | return 0; |
| 45 | |
Douglas Gregor | 405634b | 2010-04-05 18:10:21 +0000 | [diff] [blame] | 46 | return new CXStoredDiagnostic(CXXUnit->stored_diag_begin()[Index], |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 47 | CXXUnit->getASTContext().getLangOptions()); |
| 48 | } |
| 49 | |
| 50 | void clang_disposeDiagnostic(CXDiagnostic Diagnostic) { |
| 51 | CXStoredDiagnostic *Stored = static_cast<CXStoredDiagnostic *>(Diagnostic); |
| 52 | delete Stored; |
| 53 | } |
| 54 | |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 55 | CXString clang_formatDiagnostic(CXDiagnostic Diagnostic, unsigned Options) { |
| 56 | if (!Diagnostic) |
| 57 | return createCXString(""); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 58 | |
| 59 | CXDiagnosticSeverity Severity = clang_getDiagnosticSeverity(Diagnostic); |
| 60 | |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 61 | llvm::SmallString<256> Str; |
| 62 | llvm::raw_svector_ostream Out(Str); |
| 63 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 64 | if (Options & CXDiagnostic_DisplaySourceLocation) { |
| 65 | // Print source location (file:line), along with optional column |
| 66 | // and source ranges. |
| 67 | CXFile File; |
| 68 | unsigned Line, Column; |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 69 | clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic), |
| 70 | &File, &Line, &Column, 0); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 71 | if (File) { |
| 72 | CXString FName = clang_getFileName(File); |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 73 | Out << clang_getCString(FName) << ":" << Line << ":"; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 74 | clang_disposeString(FName); |
| 75 | if (Options & CXDiagnostic_DisplayColumn) |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 76 | Out << Column << ":"; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 77 | |
| 78 | if (Options & CXDiagnostic_DisplaySourceRanges) { |
| 79 | unsigned N = clang_getDiagnosticNumRanges(Diagnostic); |
| 80 | bool PrintedRange = false; |
| 81 | for (unsigned I = 0; I != N; ++I) { |
| 82 | CXFile StartFile, EndFile; |
| 83 | CXSourceRange Range = clang_getDiagnosticRange(Diagnostic, I); |
| 84 | |
| 85 | unsigned StartLine, StartColumn, EndLine, EndColumn; |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 86 | clang_getSpellingLocation(clang_getRangeStart(Range), |
| 87 | &StartFile, &StartLine, &StartColumn, |
| 88 | 0); |
| 89 | clang_getSpellingLocation(clang_getRangeEnd(Range), |
| 90 | &EndFile, &EndLine, &EndColumn, 0); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 91 | |
| 92 | if (StartFile != EndFile || StartFile != File) |
| 93 | continue; |
| 94 | |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 95 | Out << "{" << StartLine << ":" << StartColumn << "-" |
| 96 | << EndLine << ":" << EndColumn << "}"; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 97 | PrintedRange = true; |
| 98 | } |
| 99 | if (PrintedRange) |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 100 | Out << ":"; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 101 | } |
Douglas Gregor | 4cd912a | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 102 | |
| 103 | Out << " "; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 104 | } |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* Print warning/error/etc. */ |
| 108 | switch (Severity) { |
| 109 | case CXDiagnostic_Ignored: assert(0 && "impossible"); break; |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 110 | case CXDiagnostic_Note: Out << "note: "; break; |
| 111 | case CXDiagnostic_Warning: Out << "warning: "; break; |
| 112 | case CXDiagnostic_Error: Out << "error: "; break; |
| 113 | case CXDiagnostic_Fatal: Out << "fatal error: "; break; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | CXString Text = clang_getDiagnosticSpelling(Diagnostic); |
| 117 | if (clang_getCString(Text)) |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 118 | Out << clang_getCString(Text); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 119 | else |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 120 | Out << "<no diagnostic text>"; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 121 | clang_disposeString(Text); |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 122 | return createCXString(Out.str(), true); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | unsigned clang_defaultDiagnosticDisplayOptions() { |
| 126 | return CXDiagnostic_DisplaySourceLocation | CXDiagnostic_DisplayColumn; |
| 127 | } |
| 128 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 129 | enum CXDiagnosticSeverity clang_getDiagnosticSeverity(CXDiagnostic Diag) { |
| 130 | CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag); |
| 131 | if (!StoredDiag) |
| 132 | return CXDiagnostic_Ignored; |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 133 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 134 | switch (StoredDiag->Diag.getLevel()) { |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 135 | case Diagnostic::Ignored: return CXDiagnostic_Ignored; |
| 136 | case Diagnostic::Note: return CXDiagnostic_Note; |
| 137 | case Diagnostic::Warning: return CXDiagnostic_Warning; |
| 138 | case Diagnostic::Error: return CXDiagnostic_Error; |
| 139 | case Diagnostic::Fatal: return CXDiagnostic_Fatal; |
| 140 | } |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 141 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 142 | llvm_unreachable("Invalid diagnostic level"); |
| 143 | return CXDiagnostic_Ignored; |
| 144 | } |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 145 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 146 | CXSourceLocation clang_getDiagnosticLocation(CXDiagnostic Diag) { |
| 147 | CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag); |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 148 | if (!StoredDiag || StoredDiag->Diag.getLocation().isInvalid()) |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 149 | return clang_getNullLocation(); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 150 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 151 | return translateSourceLocation(StoredDiag->Diag.getLocation().getManager(), |
| 152 | StoredDiag->LangOpts, |
| 153 | StoredDiag->Diag.getLocation()); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | CXString clang_getDiagnosticSpelling(CXDiagnostic Diag) { |
| 157 | CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag); |
| 158 | if (!StoredDiag) |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 159 | return createCXString(""); |
| 160 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 161 | return createCXString(StoredDiag->Diag.getMessage(), false); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Douglas Gregor | a3890ba | 2010-02-08 23:11:56 +0000 | [diff] [blame] | 164 | unsigned clang_getDiagnosticNumRanges(CXDiagnostic Diag) { |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 165 | CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag); |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 166 | if (!StoredDiag || StoredDiag->Diag.getLocation().isInvalid()) |
Douglas Gregor | a3890ba | 2010-02-08 23:11:56 +0000 | [diff] [blame] | 167 | return 0; |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 168 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 169 | return StoredDiag->Diag.range_size(); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 170 | } |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 171 | |
Douglas Gregor | a3890ba | 2010-02-08 23:11:56 +0000 | [diff] [blame] | 172 | CXSourceRange clang_getDiagnosticRange(CXDiagnostic Diag, unsigned Range) { |
| 173 | CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag); |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 174 | if (!StoredDiag || Range >= StoredDiag->Diag.range_size() || |
| 175 | StoredDiag->Diag.getLocation().isInvalid()) |
Douglas Gregor | a3890ba | 2010-02-08 23:11:56 +0000 | [diff] [blame] | 176 | return clang_getNullRange(); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 177 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 178 | return translateSourceRange(StoredDiag->Diag.getLocation().getManager(), |
| 179 | StoredDiag->LangOpts, |
| 180 | StoredDiag->Diag.range_begin()[Range]); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | unsigned clang_getDiagnosticNumFixIts(CXDiagnostic Diag) { |
| 184 | CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag); |
| 185 | if (!StoredDiag) |
| 186 | return 0; |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 187 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 188 | return StoredDiag->Diag.fixit_size(); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Douglas Gregor | 473d701 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 191 | CXString clang_getDiagnosticFixIt(CXDiagnostic Diagnostic, unsigned FixIt, |
| 192 | CXSourceRange *ReplacementRange) { |
| 193 | CXStoredDiagnostic *StoredDiag |
| 194 | = static_cast<CXStoredDiagnostic *>(Diagnostic); |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 195 | if (!StoredDiag || FixIt >= StoredDiag->Diag.fixit_size() || |
| 196 | StoredDiag->Diag.getLocation().isInvalid()) { |
Douglas Gregor | 473d701 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 197 | if (ReplacementRange) |
| 198 | *ReplacementRange = clang_getNullRange(); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 199 | |
| 200 | return createCXString(""); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 201 | } |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 202 | |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 203 | const FixItHint &Hint = StoredDiag->Diag.fixit_begin()[FixIt]; |
Douglas Gregor | 473d701 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 204 | if (ReplacementRange) { |
Douglas Gregor | 783c56f | 2010-08-18 14:24:02 +0000 | [diff] [blame] | 205 | // Create a range that covers the entire replacement (or |
| 206 | // removal) range, adjusting the end of the range to point to |
| 207 | // the end of the token. |
| 208 | *ReplacementRange |
| 209 | = translateSourceRange(StoredDiag->Diag.getLocation().getManager(), |
| 210 | StoredDiag->LangOpts, |
| 211 | Hint.RemoveRange); |
Douglas Gregor | 473d701 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 214 | return createCXString(Hint.CodeToInsert); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 215 | } |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 216 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 217 | } // end extern "C" |