CXStoredDiagnostic.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //===- CXStoredDiagnostic.cpp - Diagnostics C Interface -------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Implements part of the diagnostic functions of the Clang C interface.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "CIndexDiagnostic.h"
  13. #include "CIndexer.h"
  14. #include "CXTranslationUnit.h"
  15. #include "CXSourceLocation.h"
  16. #include "CXString.h"
  17. #include "clang/Basic/DiagnosticIDs.h"
  18. #include "clang/Frontend/ASTUnit.h"
  19. #include "llvm/ADT/Twine.h"
  20. using namespace clang;
  21. using namespace clang::cxloc;
  22. CXDiagnosticSeverity CXStoredDiagnostic::getSeverity() const {
  23. switch (Diag.getLevel()) {
  24. case DiagnosticsEngine::Ignored: return CXDiagnostic_Ignored;
  25. case DiagnosticsEngine::Note: return CXDiagnostic_Note;
  26. case DiagnosticsEngine::Remark:
  27. // The 'Remark' level isn't represented in the stable API.
  28. case DiagnosticsEngine::Warning: return CXDiagnostic_Warning;
  29. case DiagnosticsEngine::Error: return CXDiagnostic_Error;
  30. case DiagnosticsEngine::Fatal: return CXDiagnostic_Fatal;
  31. }
  32. llvm_unreachable("Invalid diagnostic level");
  33. }
  34. CXSourceLocation CXStoredDiagnostic::getLocation() const {
  35. if (Diag.getLocation().isInvalid())
  36. return clang_getNullLocation();
  37. return translateSourceLocation(Diag.getLocation().getManager(),
  38. LangOpts, Diag.getLocation());
  39. }
  40. CXString CXStoredDiagnostic::getSpelling() const {
  41. return cxstring::createRef(Diag.getMessage());
  42. }
  43. CXString CXStoredDiagnostic::getDiagnosticOption(CXString *Disable) const {
  44. unsigned ID = Diag.getID();
  45. StringRef Option = DiagnosticIDs::getWarningOptionForDiag(ID);
  46. if (!Option.empty()) {
  47. if (Disable)
  48. *Disable = cxstring::createDup((Twine("-Wno-") + Option).str());
  49. return cxstring::createDup((Twine("-W") + Option).str());
  50. }
  51. if (ID == diag::fatal_too_many_errors) {
  52. if (Disable)
  53. *Disable = cxstring::createRef("-ferror-limit=0");
  54. return cxstring::createRef("-ferror-limit=");
  55. }
  56. return cxstring::createEmpty();
  57. }
  58. unsigned CXStoredDiagnostic::getCategory() const {
  59. return DiagnosticIDs::getCategoryNumberForDiag(Diag.getID());
  60. }
  61. CXString CXStoredDiagnostic::getCategoryText() const {
  62. unsigned catID = DiagnosticIDs::getCategoryNumberForDiag(Diag.getID());
  63. return cxstring::createRef(DiagnosticIDs::getCategoryNameFromID(catID));
  64. }
  65. unsigned CXStoredDiagnostic::getNumRanges() const {
  66. if (Diag.getLocation().isInvalid())
  67. return 0;
  68. return Diag.range_size();
  69. }
  70. CXSourceRange CXStoredDiagnostic::getRange(unsigned int Range) const {
  71. assert(Diag.getLocation().isValid());
  72. return translateSourceRange(Diag.getLocation().getManager(),
  73. LangOpts,
  74. Diag.range_begin()[Range]);
  75. }
  76. unsigned CXStoredDiagnostic::getNumFixIts() const {
  77. if (Diag.getLocation().isInvalid())
  78. return 0;
  79. return Diag.fixit_size();
  80. }
  81. CXString CXStoredDiagnostic::getFixIt(unsigned FixIt,
  82. CXSourceRange *ReplacementRange) const {
  83. const FixItHint &Hint = Diag.fixit_begin()[FixIt];
  84. if (ReplacementRange) {
  85. // Create a range that covers the entire replacement (or
  86. // removal) range, adjusting the end of the range to point to
  87. // the end of the token.
  88. *ReplacementRange = translateSourceRange(Diag.getLocation().getManager(),
  89. LangOpts, Hint.RemoveRange);
  90. }
  91. return cxstring::createDup(Hint.CodeToInsert);
  92. }