Diagnostic.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //===--- Diagnostic.cpp - Framework for clang diagnostics tools ----------===//
  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 classes to support/store diagnostics refactoring.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/Tooling/Core/Diagnostic.h"
  13. #include "clang/Basic/SourceLocation.h"
  14. #include "clang/Basic/SourceManager.h"
  15. #include "llvm/ADT/STLExtras.h"
  16. namespace clang {
  17. namespace tooling {
  18. DiagnosticMessage::DiagnosticMessage(llvm::StringRef Message)
  19. : Message(Message), FileOffset(0) {}
  20. DiagnosticMessage::DiagnosticMessage(llvm::StringRef Message,
  21. const SourceManager &Sources,
  22. SourceLocation Loc)
  23. : Message(Message), FileOffset(0) {
  24. assert(Loc.isValid() && Loc.isFileID());
  25. FilePath = std::string(Sources.getFilename(Loc));
  26. // Don't store offset in the scratch space. It doesn't tell anything to the
  27. // user. Moreover, it depends on the history of macro expansions and thus
  28. // prevents deduplication of warnings in headers.
  29. if (!FilePath.empty())
  30. FileOffset = Sources.getFileOffset(Loc);
  31. }
  32. FileByteRange::FileByteRange(
  33. const SourceManager &Sources, CharSourceRange Range)
  34. : FileOffset(0), Length(0) {
  35. FilePath = std::string(Sources.getFilename(Range.getBegin()));
  36. if (!FilePath.empty()) {
  37. FileOffset = Sources.getFileOffset(Range.getBegin());
  38. Length = Sources.getFileOffset(Range.getEnd()) - FileOffset;
  39. }
  40. }
  41. Diagnostic::Diagnostic(llvm::StringRef DiagnosticName,
  42. Diagnostic::Level DiagLevel, StringRef BuildDirectory)
  43. : DiagnosticName(DiagnosticName), DiagLevel(DiagLevel),
  44. BuildDirectory(BuildDirectory) {}
  45. Diagnostic::Diagnostic(llvm::StringRef DiagnosticName,
  46. const DiagnosticMessage &Message,
  47. const SmallVector<DiagnosticMessage, 1> &Notes,
  48. Level DiagLevel, llvm::StringRef BuildDirectory)
  49. : DiagnosticName(DiagnosticName), Message(Message), Notes(Notes),
  50. DiagLevel(DiagLevel), BuildDirectory(BuildDirectory) {}
  51. const llvm::StringMap<Replacements> *selectFirstFix(const Diagnostic& D) {
  52. if (!D.Message.Fix.empty())
  53. return &D.Message.Fix;
  54. auto Iter = llvm::find_if(D.Notes, [](const tooling::DiagnosticMessage &D) {
  55. return !D.Fix.empty();
  56. });
  57. if (Iter != D.Notes.end())
  58. return &Iter->Fix;
  59. return nullptr;
  60. }
  61. } // end namespace tooling
  62. } // end namespace clang