blob: f2e9c1da28f6b8efbcb8f27546f8dd393af7f639 [file] [log] [blame]
Ted Kremenekbb2c7102011-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"
Ted Kremenekbb2c7102011-10-31 21:40:19 +000022#include "llvm/ADT/Twine.h"
Ted Kremenekbb2c7102011-10-31 21:40:19 +000023
24using namespace clang;
25using namespace clang::cxloc;
Ted Kremenekbb2c7102011-10-31 21:40:19 +000026
Ted Kremenekbb2c7102011-10-31 21:40:19 +000027CXDiagnosticSeverity CXStoredDiagnostic::getSeverity() const {
28 switch (Diag.getLevel()) {
29 case DiagnosticsEngine::Ignored: return CXDiagnostic_Ignored;
30 case DiagnosticsEngine::Note: return CXDiagnostic_Note;
Alp Toker87d39752014-04-26 14:43:53 +000031 case DiagnosticsEngine::Remark:
32 // The 'Remark' level isn't represented in the stable API.
Ted Kremenekbb2c7102011-10-31 21:40:19 +000033 case DiagnosticsEngine::Warning: return CXDiagnostic_Warning;
34 case DiagnosticsEngine::Error: return CXDiagnostic_Error;
35 case DiagnosticsEngine::Fatal: return CXDiagnostic_Fatal;
36 }
37
38 llvm_unreachable("Invalid diagnostic level");
Ted Kremenekbb2c7102011-10-31 21:40:19 +000039}
40
41CXSourceLocation CXStoredDiagnostic::getLocation() const {
42 if (Diag.getLocation().isInvalid())
43 return clang_getNullLocation();
44
45 return translateSourceLocation(Diag.getLocation().getManager(),
46 LangOpts, Diag.getLocation());
47}
48
49CXString CXStoredDiagnostic::getSpelling() const {
Dmitri Gribenko2f23e9c2013-02-02 02:19:29 +000050 return cxstring::createRef(Diag.getMessage());
Ted Kremenekbb2c7102011-10-31 21:40:19 +000051}
52
53CXString CXStoredDiagnostic::getDiagnosticOption(CXString *Disable) const {
54 unsigned ID = Diag.getID();
55 StringRef Option = DiagnosticIDs::getWarningOptionForDiag(ID);
56 if (!Option.empty()) {
57 if (Disable)
Dmitri Gribenko2f23e9c2013-02-02 02:19:29 +000058 *Disable = cxstring::createDup((Twine("-Wno-") + Option).str());
59 return cxstring::createDup((Twine("-W") + Option).str());
Ted Kremenekbb2c7102011-10-31 21:40:19 +000060 }
61
62 if (ID == diag::fatal_too_many_errors) {
63 if (Disable)
Dmitri Gribenko3c66b0b2013-02-02 00:02:12 +000064 *Disable = cxstring::createRef("-ferror-limit=0");
65 return cxstring::createRef("-ferror-limit=");
Ted Kremenekbb2c7102011-10-31 21:40:19 +000066 }
Ted Kremenekbb2c7102011-10-31 21:40:19 +000067
Dmitri Gribenko36a6dd02013-02-01 14:21:22 +000068 return cxstring::createEmpty();
Ted Kremenekbb2c7102011-10-31 21:40:19 +000069}
70
71unsigned CXStoredDiagnostic::getCategory() const {
72 return DiagnosticIDs::getCategoryNumberForDiag(Diag.getID());
73}
74
Ted Kremenek26a6d492012-04-12 00:03:31 +000075CXString CXStoredDiagnostic::getCategoryText() const {
76 unsigned catID = DiagnosticIDs::getCategoryNumberForDiag(Diag.getID());
Dmitri Gribenko2f23e9c2013-02-02 02:19:29 +000077 return cxstring::createRef(DiagnosticIDs::getCategoryNameFromID(catID));
Ted Kremenek26a6d492012-04-12 00:03:31 +000078}
79
Ted Kremenekbb2c7102011-10-31 21:40:19 +000080unsigned CXStoredDiagnostic::getNumRanges() const {
81 if (Diag.getLocation().isInvalid())
82 return 0;
83
84 return Diag.range_size();
85}
86
87CXSourceRange CXStoredDiagnostic::getRange(unsigned int Range) const {
88 assert(Diag.getLocation().isValid());
89 return translateSourceRange(Diag.getLocation().getManager(),
90 LangOpts,
91 Diag.range_begin()[Range]);
92}
93
94unsigned CXStoredDiagnostic::getNumFixIts() const {
95 if (Diag.getLocation().isInvalid())
96 return 0;
97 return Diag.fixit_size();
98}
99
100CXString CXStoredDiagnostic::getFixIt(unsigned FixIt,
101 CXSourceRange *ReplacementRange) const {
102 const FixItHint &Hint = Diag.fixit_begin()[FixIt];
103 if (ReplacementRange) {
104 // Create a range that covers the entire replacement (or
105 // removal) range, adjusting the end of the range to point to
106 // the end of the token.
107 *ReplacementRange = translateSourceRange(Diag.getLocation().getManager(),
108 LangOpts, Hint.RemoveRange);
109 }
Dmitri Gribenko2f23e9c2013-02-02 02:19:29 +0000110 return cxstring::createDup(Hint.CodeToInsert);
Ted Kremenekbb2c7102011-10-31 21:40:19 +0000111}
112