blob: 9731616c6b5d05996bd28908cb8bb12f33d87343 [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;
Ted Kremenek1edabbc2011-10-31 21:40:19 +000029
Ted Kremenek1edabbc2011-10-31 21:40:19 +000030CXDiagnosticSeverity CXStoredDiagnostic::getSeverity() const {
31 switch (Diag.getLevel()) {
32 case DiagnosticsEngine::Ignored: return CXDiagnostic_Ignored;
33 case DiagnosticsEngine::Note: return CXDiagnostic_Note;
34 case DiagnosticsEngine::Warning: return CXDiagnostic_Warning;
35 case DiagnosticsEngine::Error: return CXDiagnostic_Error;
36 case DiagnosticsEngine::Fatal: return CXDiagnostic_Fatal;
37 }
38
39 llvm_unreachable("Invalid diagnostic level");
Ted Kremenek1edabbc2011-10-31 21:40:19 +000040}
41
42CXSourceLocation CXStoredDiagnostic::getLocation() const {
43 if (Diag.getLocation().isInvalid())
44 return clang_getNullLocation();
45
46 return translateSourceLocation(Diag.getLocation().getManager(),
47 LangOpts, Diag.getLocation());
48}
49
50CXString CXStoredDiagnostic::getSpelling() const {
Dmitri Gribenko5595ded2013-02-02 02:19:29 +000051 return cxstring::createRef(Diag.getMessage());
Ted Kremenek1edabbc2011-10-31 21:40:19 +000052}
53
54CXString CXStoredDiagnostic::getDiagnosticOption(CXString *Disable) const {
55 unsigned ID = Diag.getID();
56 StringRef Option = DiagnosticIDs::getWarningOptionForDiag(ID);
57 if (!Option.empty()) {
58 if (Disable)
Dmitri Gribenko5595ded2013-02-02 02:19:29 +000059 *Disable = cxstring::createDup((Twine("-Wno-") + Option).str());
60 return cxstring::createDup((Twine("-W") + Option).str());
Ted Kremenek1edabbc2011-10-31 21:40:19 +000061 }
62
63 if (ID == diag::fatal_too_many_errors) {
64 if (Disable)
Dmitri Gribenko0c4394c2013-02-02 00:02:12 +000065 *Disable = cxstring::createRef("-ferror-limit=0");
66 return cxstring::createRef("-ferror-limit=");
Ted Kremenek1edabbc2011-10-31 21:40:19 +000067 }
Ted Kremenek1edabbc2011-10-31 21:40:19 +000068
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +000069 return cxstring::createEmpty();
Ted Kremenek1edabbc2011-10-31 21:40:19 +000070}
71
72unsigned CXStoredDiagnostic::getCategory() const {
73 return DiagnosticIDs::getCategoryNumberForDiag(Diag.getID());
74}
75
Ted Kremenek78d5d3b2012-04-12 00:03:31 +000076CXString CXStoredDiagnostic::getCategoryText() const {
77 unsigned catID = DiagnosticIDs::getCategoryNumberForDiag(Diag.getID());
Dmitri Gribenko5595ded2013-02-02 02:19:29 +000078 return cxstring::createRef(DiagnosticIDs::getCategoryNameFromID(catID));
Ted Kremenek78d5d3b2012-04-12 00:03:31 +000079}
80
Ted Kremenek1edabbc2011-10-31 21:40:19 +000081unsigned 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(Diag.getLocation().getManager(),
91 LangOpts,
92 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(Diag.getLocation().getManager(),
109 LangOpts, Hint.RemoveRange);
110 }
Dmitri Gribenko5595ded2013-02-02 02:19:29 +0000111 return cxstring::createDup(Hint.CodeToInsert);
Ted Kremenek1edabbc2011-10-31 21:40:19 +0000112}
113