1 //===- CXStoredDiagnostic.cpp - Diagnostics C Interface -------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Implements part of the diagnostic functions of the Clang C interface.
11 //===----------------------------------------------------------------------===//
13 #include "CIndexDiagnostic.h"
15 #include "CXTranslationUnit.h"
16 #include "CXSourceLocation.h"
19 #include "clang/Basic/DiagnosticIDs.h"
20 #include "clang/Frontend/ASTUnit.h"
21 #include "llvm/ADT/Twine.h"
23 using namespace clang
;
24 using namespace clang::cxloc
;
26 CXDiagnosticSeverity
CXStoredDiagnostic::getSeverity() const {
27 switch (Diag
.getLevel()) {
28 case DiagnosticsEngine::Ignored
: return CXDiagnostic_Ignored
;
29 case DiagnosticsEngine::Note
: return CXDiagnostic_Note
;
30 case DiagnosticsEngine::Remark
:
31 // The 'Remark' level isn't represented in the stable API.
32 case DiagnosticsEngine::Warning
: return CXDiagnostic_Warning
;
33 case DiagnosticsEngine::Error
: return CXDiagnostic_Error
;
34 case DiagnosticsEngine::Fatal
: return CXDiagnostic_Fatal
;
37 llvm_unreachable("Invalid diagnostic level");
40 CXSourceLocation
CXStoredDiagnostic::getLocation() const {
41 if (Diag
.getLocation().isInvalid())
42 return clang_getNullLocation();
44 return translateSourceLocation(Diag
.getLocation().getManager(),
45 LangOpts
, Diag
.getLocation());
48 CXString
CXStoredDiagnostic::getSpelling() const {
49 return cxstring::createRef(Diag
.getMessage());
52 CXString
CXStoredDiagnostic::getDiagnosticOption(CXString
*Disable
) const {
53 unsigned ID
= Diag
.getID();
54 StringRef Option
= DiagnosticIDs::getWarningOptionForDiag(ID
);
55 if (!Option
.empty()) {
57 *Disable
= cxstring::createDup((Twine("-Wno-") + Option
).str());
58 return cxstring::createDup((Twine("-W") + Option
).str());
61 if (ID
== diag::fatal_too_many_errors
) {
63 *Disable
= cxstring::createRef("-ferror-limit=0");
64 return cxstring::createRef("-ferror-limit=");
67 return cxstring::createEmpty();
70 unsigned CXStoredDiagnostic::getCategory() const {
71 return DiagnosticIDs::getCategoryNumberForDiag(Diag
.getID());
74 CXString
CXStoredDiagnostic::getCategoryText() const {
75 unsigned catID
= DiagnosticIDs::getCategoryNumberForDiag(Diag
.getID());
76 return cxstring::createRef(DiagnosticIDs::getCategoryNameFromID(catID
));
79 unsigned CXStoredDiagnostic::getNumRanges() const {
80 if (Diag
.getLocation().isInvalid())
83 return Diag
.range_size();
86 CXSourceRange
CXStoredDiagnostic::getRange(unsigned int Range
) const {
87 assert(Diag
.getLocation().isValid());
88 return translateSourceRange(Diag
.getLocation().getManager(),
90 Diag
.range_begin()[Range
]);
93 unsigned CXStoredDiagnostic::getNumFixIts() const {
94 if (Diag
.getLocation().isInvalid())
96 return Diag
.fixit_size();
99 CXString
CXStoredDiagnostic::getFixIt(unsigned FixIt
,
100 CXSourceRange
*ReplacementRange
) const {
101 const FixItHint
&Hint
= Diag
.fixit_begin()[FixIt
];
102 if (ReplacementRange
) {
103 // Create a range that covers the entire replacement (or
104 // removal) range, adjusting the end of the range to point to
105 // the end of the token.
106 *ReplacementRange
= translateSourceRange(Diag
.getLocation().getManager(),
107 LangOpts
, Hint
.RemoveRange
);
109 return cxstring::createDup(Hint
.CodeToInsert
);