blob: faaf746a1e7038d4fc8bee5c7f22b1df7c44cb70 [file] [log] [blame]
Ted Kremenek1edabbc2011-10-31 21:40:19 +00001/*===-- 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
27using namespace clang;
28using namespace clang::cxloc;
Ted Kremenek1edabbc2011-10-31 21:40:19 +000029
Ted Kremenek1edabbc2011-10-31 21:40:19 +000030CXDiagnosticSeverity CXStoredDiagnostic::getSeverity() const {
31 switch (Diag.getLevel()) {
32 case DiagnosticsEngine::Ignored: return CXDiagnostic_Ignored;
33 case DiagnosticsEngine::Note: return CXDiagnostic_Note;
Stephen Hines6bcf27b2014-05-29 04:14:42 -070034 case DiagnosticsEngine::Remark:
35 // The 'Remark' level isn't represented in the stable API.
Ted Kremenek1edabbc2011-10-31 21:40:19 +000036 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 Kremenek1edabbc2011-10-31 21:40:19 +000042}
43
44CXSourceLocation 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
52CXString CXStoredDiagnostic::getSpelling() const {
Dmitri Gribenko5595ded2013-02-02 02:19:29 +000053 return cxstring::createRef(Diag.getMessage());
Ted Kremenek1edabbc2011-10-31 21:40:19 +000054}
55
56CXString CXStoredDiagnostic::getDiagnosticOption(CXString *Disable) const {
57 unsigned ID = Diag.getID();
58 StringRef Option = DiagnosticIDs::getWarningOptionForDiag(ID);
59 if (!Option.empty()) {
60 if (Disable)
Dmitri Gribenko5595ded2013-02-02 02:19:29 +000061 *Disable = cxstring::createDup((Twine("-Wno-") + Option).str());
62 return cxstring::createDup((Twine("-W") + Option).str());
Ted Kremenek1edabbc2011-10-31 21:40:19 +000063 }
64
65 if (ID == diag::fatal_too_many_errors) {
66 if (Disable)
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +000067 *Disable = cxstring::createRef("-ferror-limit=0");
68 return cxstring::createRef("-ferror-limit=");
Ted Kremenek1edabbc2011-10-31 21:40:19 +000069 }
Ted Kremenek1edabbc2011-10-31 21:40:19 +000070
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +000071 return cxstring::createEmpty();
Ted Kremenek1edabbc2011-10-31 21:40:19 +000072}
73
74unsigned CXStoredDiagnostic::getCategory() const {
75 return DiagnosticIDs::getCategoryNumberForDiag(Diag.getID());
76}
77
Ted Kremenek78d5d3b2012-04-12 00:03:31 +000078CXString CXStoredDiagnostic::getCategoryText() const {
79 unsigned catID = DiagnosticIDs::getCategoryNumberForDiag(Diag.getID());
Dmitri Gribenko5595ded2013-02-02 02:19:29 +000080 return cxstring::createRef(DiagnosticIDs::getCategoryNameFromID(catID));
Ted Kremenek78d5d3b2012-04-12 00:03:31 +000081}
82
Ted Kremenek1edabbc2011-10-31 21:40:19 +000083unsigned CXStoredDiagnostic::getNumRanges() const {
84 if (Diag.getLocation().isInvalid())
85 return 0;
86
87 return Diag.range_size();
88}
89
90CXSourceRange 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
97unsigned CXStoredDiagnostic::getNumFixIts() const {
98 if (Diag.getLocation().isInvalid())
99 return 0;
100 return Diag.fixit_size();
101}
102
103CXString 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 Gribenko5595ded2013-02-02 02:19:29 +0000113 return cxstring::createDup(Hint.CodeToInsert);
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000114}
115