CXStoredDiagnostic.cpp revision 1edabbc32a07654d9b9fe44f50533ff146abd03e
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;
29using namespace clang::cxstring;
30
31// Needed for vtable of CXPersisetntDiagnostic.
32CXDiagnosticImpl::~CXDiagnosticImpl() {}
33
34CXDiagnosticSeverity CXStoredDiagnostic::getSeverity() const {
35  switch (Diag.getLevel()) {
36    case DiagnosticsEngine::Ignored: return CXDiagnostic_Ignored;
37    case DiagnosticsEngine::Note:    return CXDiagnostic_Note;
38    case DiagnosticsEngine::Warning: return CXDiagnostic_Warning;
39    case DiagnosticsEngine::Error:   return CXDiagnostic_Error;
40    case DiagnosticsEngine::Fatal:   return CXDiagnostic_Fatal;
41  }
42
43  llvm_unreachable("Invalid diagnostic level");
44  return CXDiagnostic_Ignored;
45}
46
47CXSourceLocation CXStoredDiagnostic::getLocation() const {
48  if (Diag.getLocation().isInvalid())
49    return clang_getNullLocation();
50
51  return translateSourceLocation(Diag.getLocation().getManager(),
52                                 LangOpts, Diag.getLocation());
53}
54
55CXString CXStoredDiagnostic::getSpelling() const {
56  return createCXString(Diag.getMessage(), false);
57}
58
59CXString CXStoredDiagnostic::getDiagnosticOption(CXString *Disable) const {
60  unsigned ID = Diag.getID();
61  StringRef Option = DiagnosticIDs::getWarningOptionForDiag(ID);
62  if (!Option.empty()) {
63    if (Disable)
64      *Disable = createCXString((Twine("-Wno-") + Option).str());
65    return createCXString((Twine("-W") + Option).str());
66  }
67
68  if (ID == diag::fatal_too_many_errors) {
69    if (Disable)
70      *Disable = createCXString("-ferror-limit=0");
71    return createCXString("-ferror-limit=");
72  }
73
74  bool EnabledByDefault;
75  if (DiagnosticIDs::isBuiltinExtensionDiag(ID, EnabledByDefault) &&
76      !EnabledByDefault)
77    return createCXString("-pedantic");
78
79  return createCXString("");
80}
81
82unsigned CXStoredDiagnostic::getCategory() const {
83  return DiagnosticIDs::getCategoryNumberForDiag(Diag.getID());
84}
85
86unsigned CXStoredDiagnostic::getNumRanges() const {
87  if (Diag.getLocation().isInvalid())
88    return 0;
89
90  return Diag.range_size();
91}
92
93CXSourceRange CXStoredDiagnostic::getRange(unsigned int Range) const {
94  assert(Diag.getLocation().isValid());
95  return translateSourceRange(Diag.getLocation().getManager(),
96                              LangOpts,
97                              Diag.range_begin()[Range]);
98}
99
100unsigned CXStoredDiagnostic::getNumFixIts() const {
101  if (Diag.getLocation().isInvalid())
102    return 0;
103  return Diag.fixit_size();
104}
105
106CXString CXStoredDiagnostic::getFixIt(unsigned FixIt,
107                                      CXSourceRange *ReplacementRange) const {
108  const FixItHint &Hint = Diag.fixit_begin()[FixIt];
109  if (ReplacementRange) {
110    // Create a range that covers the entire replacement (or
111    // removal) range, adjusting the end of the range to point to
112    // the end of the token.
113    *ReplacementRange = translateSourceRange(Diag.getLocation().getManager(),
114                                             LangOpts, Hint.RemoveRange);
115  }
116  return createCXString(Hint.CodeToInsert);
117}
118
119