blob: c4c24876e70de46c31685c13511854d9c5a2385b [file] [log] [blame]
Fangrui Song524b3c12019-03-01 06:49:51 +00001//===- 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//===----------------------------------------------------------------------===//
Ted Kremenekbb2c7102011-10-31 21:40:19 +000012
13#include "CIndexDiagnostic.h"
14#include "CIndexer.h"
15#include "CXTranslationUnit.h"
16#include "CXSourceLocation.h"
17#include "CXString.h"
18
Richard Trieud2e5acc2018-12-15 04:25:19 +000019#include "clang/Basic/DiagnosticIDs.h"
Ted Kremenekbb2c7102011-10-31 21:40:19 +000020#include "clang/Frontend/ASTUnit.h"
Ted Kremenekbb2c7102011-10-31 21:40:19 +000021#include "llvm/ADT/Twine.h"
Ted Kremenekbb2c7102011-10-31 21:40:19 +000022
23using namespace clang;
24using namespace clang::cxloc;
Ted Kremenekbb2c7102011-10-31 21:40:19 +000025
Ted Kremenekbb2c7102011-10-31 21:40:19 +000026CXDiagnosticSeverity CXStoredDiagnostic::getSeverity() const {
27 switch (Diag.getLevel()) {
28 case DiagnosticsEngine::Ignored: return CXDiagnostic_Ignored;
29 case DiagnosticsEngine::Note: return CXDiagnostic_Note;
Alp Toker87d39752014-04-26 14:43:53 +000030 case DiagnosticsEngine::Remark:
31 // The 'Remark' level isn't represented in the stable API.
Ted Kremenekbb2c7102011-10-31 21:40:19 +000032 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");
Ted Kremenekbb2c7102011-10-31 21:40:19 +000038}
39
40CXSourceLocation CXStoredDiagnostic::getLocation() const {
41 if (Diag.getLocation().isInvalid())
42 return clang_getNullLocation();
43
44 return translateSourceLocation(Diag.getLocation().getManager(),
45 LangOpts, Diag.getLocation());
46}
47
48CXString CXStoredDiagnostic::getSpelling() const {
Dmitri Gribenko2f23e9c2013-02-02 02:19:29 +000049 return cxstring::createRef(Diag.getMessage());
Ted Kremenekbb2c7102011-10-31 21:40:19 +000050}
51
52CXString CXStoredDiagnostic::getDiagnosticOption(CXString *Disable) const {
53 unsigned ID = Diag.getID();
54 StringRef Option = DiagnosticIDs::getWarningOptionForDiag(ID);
55 if (!Option.empty()) {
56 if (Disable)
Dmitri Gribenko2f23e9c2013-02-02 02:19:29 +000057 *Disable = cxstring::createDup((Twine("-Wno-") + Option).str());
58 return cxstring::createDup((Twine("-W") + Option).str());
Ted Kremenekbb2c7102011-10-31 21:40:19 +000059 }
60
61 if (ID == diag::fatal_too_many_errors) {
62 if (Disable)
Dmitri Gribenko3c66b0b2013-02-02 00:02:12 +000063 *Disable = cxstring::createRef("-ferror-limit=0");
64 return cxstring::createRef("-ferror-limit=");
Ted Kremenekbb2c7102011-10-31 21:40:19 +000065 }
Ted Kremenekbb2c7102011-10-31 21:40:19 +000066
Dmitri Gribenko36a6dd02013-02-01 14:21:22 +000067 return cxstring::createEmpty();
Ted Kremenekbb2c7102011-10-31 21:40:19 +000068}
69
70unsigned CXStoredDiagnostic::getCategory() const {
71 return DiagnosticIDs::getCategoryNumberForDiag(Diag.getID());
72}
73
Ted Kremenek26a6d492012-04-12 00:03:31 +000074CXString CXStoredDiagnostic::getCategoryText() const {
75 unsigned catID = DiagnosticIDs::getCategoryNumberForDiag(Diag.getID());
Dmitri Gribenko2f23e9c2013-02-02 02:19:29 +000076 return cxstring::createRef(DiagnosticIDs::getCategoryNameFromID(catID));
Ted Kremenek26a6d492012-04-12 00:03:31 +000077}
78
Ted Kremenekbb2c7102011-10-31 21:40:19 +000079unsigned CXStoredDiagnostic::getNumRanges() const {
80 if (Diag.getLocation().isInvalid())
81 return 0;
82
83 return Diag.range_size();
84}
85
86CXSourceRange CXStoredDiagnostic::getRange(unsigned int Range) const {
87 assert(Diag.getLocation().isValid());
88 return translateSourceRange(Diag.getLocation().getManager(),
89 LangOpts,
90 Diag.range_begin()[Range]);
91}
92
93unsigned CXStoredDiagnostic::getNumFixIts() const {
94 if (Diag.getLocation().isInvalid())
95 return 0;
96 return Diag.fixit_size();
97}
98
99CXString 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);
108 }
Dmitri Gribenko2f23e9c2013-02-02 02:19:29 +0000109 return cxstring::createDup(Hint.CodeToInsert);
Ted Kremenekbb2c7102011-10-31 21:40:19 +0000110}
111