Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 1 | /*===-- CXStoreDiagnostic.cpp - Diagnostics C Interface ----------*- C++ -*-===*\ |
| 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 part of the diagnostic functions of the Clang C interface. *| |
| 11 | |* *| |
| 12 | \*===----------------------------------------------------------------------===*/ |
| 13 | |
| 14 | #include "CIndexDiagnostic.h" |
| 15 | #include "CIndexer.h" |
| 16 | #include "CXTranslationUnit.h" |
| 17 | #include "CXSourceLocation.h" |
| 18 | #include "CXString.h" |
| 19 | |
| 20 | #include "clang/Frontend/ASTUnit.h" |
| 21 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 22 | #include "llvm/ADT/SmallString.h" |
| 23 | #include "llvm/ADT/Twine.h" |
| 24 | #include "llvm/Support/MemoryBuffer.h" |
| 25 | #include "llvm/Support/raw_ostream.h" |
| 26 | |
| 27 | using namespace clang; |
| 28 | using namespace clang::cxloc; |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 29 | |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 30 | CXDiagnosticSeverity CXStoredDiagnostic::getSeverity() const { |
| 31 | switch (Diag.getLevel()) { |
| 32 | case DiagnosticsEngine::Ignored: return CXDiagnostic_Ignored; |
| 33 | case DiagnosticsEngine::Note: return CXDiagnostic_Note; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 34 | case DiagnosticsEngine::Remark: |
| 35 | // The 'Remark' level isn't represented in the stable API. |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 36 | case DiagnosticsEngine::Warning: return CXDiagnostic_Warning; |
| 37 | case DiagnosticsEngine::Error: return CXDiagnostic_Error; |
| 38 | case DiagnosticsEngine::Fatal: return CXDiagnostic_Fatal; |
| 39 | } |
| 40 | |
| 41 | llvm_unreachable("Invalid diagnostic level"); |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | CXSourceLocation CXStoredDiagnostic::getLocation() const { |
| 45 | if (Diag.getLocation().isInvalid()) |
| 46 | return clang_getNullLocation(); |
| 47 | |
| 48 | return translateSourceLocation(Diag.getLocation().getManager(), |
| 49 | LangOpts, Diag.getLocation()); |
| 50 | } |
| 51 | |
| 52 | CXString CXStoredDiagnostic::getSpelling() const { |
Dmitri Gribenko | 5595ded | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 53 | return cxstring::createRef(Diag.getMessage()); |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | CXString CXStoredDiagnostic::getDiagnosticOption(CXString *Disable) const { |
| 57 | unsigned ID = Diag.getID(); |
| 58 | StringRef Option = DiagnosticIDs::getWarningOptionForDiag(ID); |
| 59 | if (!Option.empty()) { |
| 60 | if (Disable) |
Dmitri Gribenko | 5595ded | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 61 | *Disable = cxstring::createDup((Twine("-Wno-") + Option).str()); |
| 62 | return cxstring::createDup((Twine("-W") + Option).str()); |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | if (ID == diag::fatal_too_many_errors) { |
| 66 | if (Disable) |
Dmitri Gribenko | 0c4394c | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 67 | *Disable = cxstring::createRef("-ferror-limit=0"); |
| 68 | return cxstring::createRef("-ferror-limit="); |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 69 | } |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 70 | |
Dmitri Gribenko | dc66adb | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 71 | return cxstring::createEmpty(); |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | unsigned CXStoredDiagnostic::getCategory() const { |
| 75 | return DiagnosticIDs::getCategoryNumberForDiag(Diag.getID()); |
| 76 | } |
| 77 | |
Ted Kremenek | 78d5d3b | 2012-04-12 00:03:31 +0000 | [diff] [blame] | 78 | CXString CXStoredDiagnostic::getCategoryText() const { |
| 79 | unsigned catID = DiagnosticIDs::getCategoryNumberForDiag(Diag.getID()); |
Dmitri Gribenko | 5595ded | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 80 | return cxstring::createRef(DiagnosticIDs::getCategoryNameFromID(catID)); |
Ted Kremenek | 78d5d3b | 2012-04-12 00:03:31 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 83 | unsigned CXStoredDiagnostic::getNumRanges() const { |
| 84 | if (Diag.getLocation().isInvalid()) |
| 85 | return 0; |
| 86 | |
| 87 | return Diag.range_size(); |
| 88 | } |
| 89 | |
| 90 | CXSourceRange CXStoredDiagnostic::getRange(unsigned int Range) const { |
| 91 | assert(Diag.getLocation().isValid()); |
| 92 | return translateSourceRange(Diag.getLocation().getManager(), |
| 93 | LangOpts, |
| 94 | Diag.range_begin()[Range]); |
| 95 | } |
| 96 | |
| 97 | unsigned CXStoredDiagnostic::getNumFixIts() const { |
| 98 | if (Diag.getLocation().isInvalid()) |
| 99 | return 0; |
| 100 | return Diag.fixit_size(); |
| 101 | } |
| 102 | |
| 103 | CXString CXStoredDiagnostic::getFixIt(unsigned FixIt, |
| 104 | CXSourceRange *ReplacementRange) const { |
| 105 | const FixItHint &Hint = Diag.fixit_begin()[FixIt]; |
| 106 | if (ReplacementRange) { |
| 107 | // Create a range that covers the entire replacement (or |
| 108 | // removal) range, adjusting the end of the range to point to |
| 109 | // the end of the token. |
| 110 | *ReplacementRange = translateSourceRange(Diag.getLocation().getManager(), |
| 111 | LangOpts, Hint.RemoveRange); |
| 112 | } |
Dmitri Gribenko | 5595ded | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 113 | return cxstring::createDup(Hint.CodeToInsert); |
Ted Kremenek | 1edabbc | 2011-10-31 21:40:19 +0000 | [diff] [blame] | 114 | } |
| 115 | |