CXStoredDiagnostic.cpp revision aca3e56144308f97f676fa30b07179e1203f7822
1/*===-- 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;
29
30CXDiagnosticSeverity 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");
40}
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 {
51  return cxstring::createRef(Diag.getMessage());
52}
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)
59      *Disable = cxstring::createDup((Twine("-Wno-") + Option).str());
60    return cxstring::createDup((Twine("-W") + Option).str());
61  }
62
63  if (ID == diag::fatal_too_many_errors) {
64    if (Disable)
65      *Disable = cxstring::createRef("-ferror-limit=0");
66    return cxstring::createRef("-ferror-limit=");
67  }
68
69  return cxstring::createEmpty();
70}
71
72unsigned CXStoredDiagnostic::getCategory() const {
73  return DiagnosticIDs::getCategoryNumberForDiag(Diag.getID());
74}
75
76CXString CXStoredDiagnostic::getCategoryText() const {
77  unsigned catID = DiagnosticIDs::getCategoryNumberForDiag(Diag.getID());
78  return cxstring::createRef(DiagnosticIDs::getCategoryNameFromID(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(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  }
111  return cxstring::createDup(Hint.CodeToInsert);
112}
113
114