blob: f2d102acb7f739e985ee513b7bd6db81622d0f29 [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;
29using namespace clang::cxstring;
30
Ted Kremenek1edabbc2011-10-31 21:40:19 +000031CXDiagnosticSeverity CXStoredDiagnostic::getSeverity() const {
32 switch (Diag.getLevel()) {
33 case DiagnosticsEngine::Ignored: return CXDiagnostic_Ignored;
34 case DiagnosticsEngine::Note: return CXDiagnostic_Note;
35 case DiagnosticsEngine::Warning: return CXDiagnostic_Warning;
36 case DiagnosticsEngine::Error: return CXDiagnostic_Error;
37 case DiagnosticsEngine::Fatal: return CXDiagnostic_Fatal;
38 }
39
40 llvm_unreachable("Invalid diagnostic level");
Ted Kremenek1edabbc2011-10-31 21:40:19 +000041}
42
43CXSourceLocation CXStoredDiagnostic::getLocation() const {
44 if (Diag.getLocation().isInvalid())
45 return clang_getNullLocation();
46
47 return translateSourceLocation(Diag.getLocation().getManager(),
48 LangOpts, Diag.getLocation());
49}
50
51CXString CXStoredDiagnostic::getSpelling() const {
Dmitri Gribenko5595ded2013-02-02 02:19:29 +000052 return cxstring::createRef(Diag.getMessage());
Ted Kremenek1edabbc2011-10-31 21:40:19 +000053}
54
55CXString CXStoredDiagnostic::getDiagnosticOption(CXString *Disable) const {
56 unsigned ID = Diag.getID();
57 StringRef Option = DiagnosticIDs::getWarningOptionForDiag(ID);
58 if (!Option.empty()) {
59 if (Disable)
Dmitri Gribenko5595ded2013-02-02 02:19:29 +000060 *Disable = cxstring::createDup((Twine("-Wno-") + Option).str());
61 return cxstring::createDup((Twine("-W") + Option).str());
Ted Kremenek1edabbc2011-10-31 21:40:19 +000062 }
63
64 if (ID == diag::fatal_too_many_errors) {
65 if (Disable)
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +000066 *Disable = cxstring::createRef("-ferror-limit=0");
67 return cxstring::createRef("-ferror-limit=");
Ted Kremenek1edabbc2011-10-31 21:40:19 +000068 }
Ted Kremenek1edabbc2011-10-31 21:40:19 +000069
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +000070 return cxstring::createEmpty();
Ted Kremenek1edabbc2011-10-31 21:40:19 +000071}
72
73unsigned CXStoredDiagnostic::getCategory() const {
74 return DiagnosticIDs::getCategoryNumberForDiag(Diag.getID());
75}
76
Ted Kremenek78d5d3b2012-04-12 00:03:31 +000077CXString CXStoredDiagnostic::getCategoryText() const {
78 unsigned catID = DiagnosticIDs::getCategoryNumberForDiag(Diag.getID());
Dmitri Gribenko5595ded2013-02-02 02:19:29 +000079 return cxstring::createRef(DiagnosticIDs::getCategoryNameFromID(catID));
Ted Kremenek78d5d3b2012-04-12 00:03:31 +000080}
81
Ted Kremenek1edabbc2011-10-31 21:40:19 +000082unsigned CXStoredDiagnostic::getNumRanges() const {
83 if (Diag.getLocation().isInvalid())
84 return 0;
85
86 return Diag.range_size();
87}
88
89CXSourceRange CXStoredDiagnostic::getRange(unsigned int Range) const {
90 assert(Diag.getLocation().isValid());
91 return translateSourceRange(Diag.getLocation().getManager(),
92 LangOpts,
93 Diag.range_begin()[Range]);
94}
95
96unsigned CXStoredDiagnostic::getNumFixIts() const {
97 if (Diag.getLocation().isInvalid())
98 return 0;
99 return Diag.fixit_size();
100}
101
102CXString CXStoredDiagnostic::getFixIt(unsigned FixIt,
103 CXSourceRange *ReplacementRange) const {
104 const FixItHint &Hint = Diag.fixit_begin()[FixIt];
105 if (ReplacementRange) {
106 // Create a range that covers the entire replacement (or
107 // removal) range, adjusting the end of the range to point to
108 // the end of the token.
109 *ReplacementRange = translateSourceRange(Diag.getLocation().getManager(),
110 LangOpts, Hint.RemoveRange);
111 }
Dmitri Gribenko5595ded2013-02-02 02:19:29 +0000112 return cxstring::createDup(Hint.CodeToInsert);
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000113}
114