1 //===- OptionalDiagnostic.h - An optional diagnostic ------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// Implements a partial diagnostic which may not be emitted.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_OPTIONALDIAGNOSTIC_H
15 #define LLVM_CLANG_AST_OPTIONALDIAGNOSTIC_H
17 #include "clang/AST/APValue.h"
18 #include "clang/Basic/PartialDiagnostic.h"
19 #include "llvm/ADT/APFloat.h"
20 #include "llvm/ADT/APSInt.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/StringRef.h"
26 /// A partial diagnostic which we might know in advance that we are not going
28 class OptionalDiagnostic {
29 PartialDiagnostic *Diag;
32 explicit OptionalDiagnostic(PartialDiagnostic *Diag = nullptr) : Diag(Diag) {}
34 template <typename T> OptionalDiagnostic &operator<<(const T &v) {
40 OptionalDiagnostic &operator<<(const llvm::APSInt &I) {
42 SmallVector<char, 32> Buffer;
44 *Diag << StringRef(Buffer.data(), Buffer.size());
49 OptionalDiagnostic &operator<<(const llvm::APFloat &F) {
51 // FIXME: Force the precision of the source value down so we don't
52 // print digits which are usually useless (we don't really care here if
53 // we truncate a digit by accident in edge cases). Ideally,
54 // APFloat::toString would automatically print the shortest
55 // representation which rounds to the correct value, but it's a bit
56 // tricky to implement. Could use std::to_chars.
57 unsigned precision = llvm::APFloat::semanticsPrecision(F.getSemantics());
58 precision = (precision * 59 + 195) / 196;
59 SmallVector<char, 32> Buffer;
60 F.toString(Buffer, precision);
61 *Diag << StringRef(Buffer.data(), Buffer.size());
66 OptionalDiagnostic &operator<<(const llvm::APFixedPoint &FX) {
68 SmallVector<char, 32> Buffer;
70 *Diag << StringRef(Buffer.data(), Buffer.size());