OptionalDiagnostic.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- OptionalDiagnostic.h - An optional diagnostic ------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. /// \file
  15. /// Implements a partial diagnostic which may not be emitted.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CLANG_AST_OPTIONALDIAGNOSTIC_H
  19. #define LLVM_CLANG_AST_OPTIONALDIAGNOSTIC_H
  20. #include "clang/AST/APValue.h"
  21. #include "clang/Basic/PartialDiagnostic.h"
  22. #include "llvm/ADT/APFloat.h"
  23. #include "llvm/ADT/APSInt.h"
  24. #include "llvm/ADT/SmallVector.h"
  25. #include "llvm/ADT/StringRef.h"
  26. namespace clang {
  27. /// A partial diagnostic which we might know in advance that we are not going
  28. /// to emit.
  29. class OptionalDiagnostic {
  30. PartialDiagnostic *Diag;
  31. public:
  32. explicit OptionalDiagnostic(PartialDiagnostic *Diag = nullptr) : Diag(Diag) {}
  33. template <typename T> OptionalDiagnostic &operator<<(const T &v) {
  34. if (Diag)
  35. *Diag << v;
  36. return *this;
  37. }
  38. OptionalDiagnostic &operator<<(const llvm::APSInt &I) {
  39. if (Diag) {
  40. SmallVector<char, 32> Buffer;
  41. I.toString(Buffer);
  42. *Diag << StringRef(Buffer.data(), Buffer.size());
  43. }
  44. return *this;
  45. }
  46. OptionalDiagnostic &operator<<(const llvm::APFloat &F) {
  47. if (Diag) {
  48. // FIXME: Force the precision of the source value down so we don't
  49. // print digits which are usually useless (we don't really care here if
  50. // we truncate a digit by accident in edge cases). Ideally,
  51. // APFloat::toString would automatically print the shortest
  52. // representation which rounds to the correct value, but it's a bit
  53. // tricky to implement. Could use std::to_chars.
  54. unsigned precision = llvm::APFloat::semanticsPrecision(F.getSemantics());
  55. precision = (precision * 59 + 195) / 196;
  56. SmallVector<char, 32> Buffer;
  57. F.toString(Buffer, precision);
  58. *Diag << StringRef(Buffer.data(), Buffer.size());
  59. }
  60. return *this;
  61. }
  62. OptionalDiagnostic &operator<<(const llvm::APFixedPoint &FX) {
  63. if (Diag) {
  64. SmallVector<char, 32> Buffer;
  65. FX.toString(Buffer);
  66. *Diag << StringRef(Buffer.data(), Buffer.size());
  67. }
  68. return *this;
  69. }
  70. };
  71. } // namespace clang
  72. #endif
  73. #ifdef __GNUC__
  74. #pragma GCC diagnostic pop
  75. #endif