CXStoredDiagnostic.cpp revision 6bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89
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::Remark:
35    // The 'Remark' level isn't represented in the stable API.
36    case DiagnosticsEngine::Warning: return CXDiagnostic_Warning;
37    case DiagnosticsEngine::Error:   return CXDiagnostic_Error;
38    case DiagnosticsEngine::Fatal:   return CXDiagnostic_Fatal;
39  }
40
41  llvm_unreachable("Invalid diagnostic level");
42}
43
44CXSourceLocation CXStoredDiagnostic::getLocation() const {
45  if (Diag.getLocation().isInvalid())
46    return clang_getNullLocation();
47
48  return translateSourceLocation(Diag.getLocation().getManager(),
49                                 LangOpts, Diag.getLocation());
50}
51
52CXString CXStoredDiagnostic::getSpelling() const {
53  return cxstring::createRef(Diag.getMessage());
54}
55
56CXString CXStoredDiagnostic::getDiagnosticOption(CXString *Disable) const {
57  unsigned ID = Diag.getID();
58  StringRef Option = DiagnosticIDs::getWarningOptionForDiag(ID);
59  if (!Option.empty()) {
60    if (Disable)
61      *Disable = cxstring::createDup((Twine("-Wno-") + Option).str());
62    return cxstring::createDup((Twine("-W") + Option).str());
63  }
64
65  if (ID == diag::fatal_too_many_errors) {
66    if (Disable)
67      *Disable = cxstring::createRef("-ferror-limit=0");
68    return cxstring::createRef("-ferror-limit=");
69  }
70
71  return cxstring::createEmpty();
72}
73
74unsigned CXStoredDiagnostic::getCategory() const {
75  return DiagnosticIDs::getCategoryNumberForDiag(Diag.getID());
76}
77
78CXString CXStoredDiagnostic::getCategoryText() const {
79  unsigned catID = DiagnosticIDs::getCategoryNumberForDiag(Diag.getID());
80  return cxstring::createRef(DiagnosticIDs::getCategoryNameFromID(catID));
81}
82
83unsigned CXStoredDiagnostic::getNumRanges() const {
84  if (Diag.getLocation().isInvalid())
85    return 0;
86
87  return Diag.range_size();
88}
89
90CXSourceRange CXStoredDiagnostic::getRange(unsigned int Range) const {
91  assert(Diag.getLocation().isValid());
92  return translateSourceRange(Diag.getLocation().getManager(),
93                              LangOpts,
94                              Diag.range_begin()[Range]);
95}
96
97unsigned CXStoredDiagnostic::getNumFixIts() const {
98  if (Diag.getLocation().isInvalid())
99    return 0;
100  return Diag.fixit_size();
101}
102
103CXString CXStoredDiagnostic::getFixIt(unsigned FixIt,
104                                      CXSourceRange *ReplacementRange) const {
105  const FixItHint &Hint = Diag.fixit_begin()[FixIt];
106  if (ReplacementRange) {
107    // Create a range that covers the entire replacement (or
108    // removal) range, adjusting the end of the range to point to
109    // the end of the token.
110    *ReplacementRange = translateSourceRange(Diag.getLocation().getManager(),
111                                             LangOpts, Hint.RemoveRange);
112  }
113  return cxstring::createDup(Hint.CodeToInsert);
114}
115
116