1//===- CXStoredDiagnostic.cpp - Diagnostics C Interface -------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// Implements part of the diagnostic functions of the Clang C interface.
10//
11//===----------------------------------------------------------------------===//
12
13#include "CIndexDiagnostic.h"
14#include "CIndexer.h"
15#include "CXTranslationUnit.h"
16#include "CXSourceLocation.h"
17#include "CXString.h"
18
19#include "clang/Basic/DiagnosticIDs.h"
20#include "clang/Frontend/ASTUnit.h"
21#include "llvm/ADT/Twine.h"
22
23using namespace clang;
24using namespace clang::cxloc;
25
26CXDiagnosticSeverity 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;
35 }
36
37 llvm_unreachable("Invalid diagnostic level");
38}
39
40CXSourceLocation CXStoredDiagnostic::getLocation() const {
41 if (Diag.getLocation().isInvalid())
42 return clang_getNullLocation();
43
44 return translateSourceLocation(SM: Diag.getLocation().getManager(),
45 LangOpts, Loc: Diag.getLocation());
46}
47
48CXString CXStoredDiagnostic::getSpelling() const {
49 return cxstring::createRef(String: Diag.getMessage());
50}
51
52CXString CXStoredDiagnostic::getDiagnosticOption(CXString *Disable) const {
53 unsigned ID = Diag.getID();
54 if (DiagnosticIDs::IsCustomDiag(Diag: ID))
55 return cxstring::createEmpty();
56 StringRef Option = DiagnosticIDs{}.getWarningOptionForDiag(DiagID: ID);
57 if (!Option.empty()) {
58 if (Disable)
59 *Disable = cxstring::createDup(String: (Twine("-Wno-") + Option).str());
60 return cxstring::createDup(String: (Twine("-W") + Option).str());
61 }
62
63 if (ID == diag::fatal_too_many_errors) {
64 if (Disable)
65 *Disable = cxstring::createRef(String: "-ferror-limit=0");
66 return cxstring::createRef(String: "-ferror-limit=");
67 }
68
69 return cxstring::createEmpty();
70}
71
72unsigned CXStoredDiagnostic::getCategory() const {
73 return DiagnosticIDs::getCategoryNumberForDiag(DiagID: Diag.getID());
74}
75
76CXString CXStoredDiagnostic::getCategoryText() const {
77 unsigned catID = DiagnosticIDs::getCategoryNumberForDiag(DiagID: Diag.getID());
78 return cxstring::createRef(String: DiagnosticIDs::getCategoryNameFromID(CategoryID: catID));
79}
80
81unsigned CXStoredDiagnostic::getNumRanges() const {
82 if (Diag.getLocation().isInvalid())
83 return 0;
84
85 return Diag.range_size();
86}
87
88CXSourceRange CXStoredDiagnostic::getRange(unsigned int Range) const {
89 assert(Diag.getLocation().isValid());
90 return translateSourceRange(SM: Diag.getLocation().getManager(),
91 LangOpts,
92 R: Diag.range_begin()[Range]);
93}
94
95unsigned CXStoredDiagnostic::getNumFixIts() const {
96 if (Diag.getLocation().isInvalid())
97 return 0;
98 return Diag.fixit_size();
99}
100
101CXString CXStoredDiagnostic::getFixIt(unsigned FixIt,
102 CXSourceRange *ReplacementRange) const {
103 const FixItHint &Hint = Diag.fixit_begin()[FixIt];
104 if (ReplacementRange) {
105 // Create a range that covers the entire replacement (or
106 // removal) range, adjusting the end of the range to point to
107 // the end of the token.
108 *ReplacementRange = translateSourceRange(SM: Diag.getLocation().getManager(),
109 LangOpts, R: Hint.RemoveRange);
110 }
111 return cxstring::createDup(String: Hint.CodeToInsert);
112}
113

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

source code of clang/tools/libclang/CXStoredDiagnostic.cpp