Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:40

0001 //===- OptionalDiagnostic.h - An optional diagnostic ------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 /// \file
0010 /// Implements a partial diagnostic which may not be emitted.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_CLANG_AST_OPTIONALDIAGNOSTIC_H
0015 #define LLVM_CLANG_AST_OPTIONALDIAGNOSTIC_H
0016 
0017 #include "clang/AST/APValue.h"
0018 #include "clang/Basic/PartialDiagnostic.h"
0019 #include "llvm/ADT/APFloat.h"
0020 #include "llvm/ADT/APSInt.h"
0021 #include "llvm/ADT/SmallVector.h"
0022 #include "llvm/ADT/StringRef.h"
0023 
0024 namespace clang {
0025 
0026 /// A partial diagnostic which we might know in advance that we are not going
0027 /// to emit.
0028 class OptionalDiagnostic {
0029   PartialDiagnostic *Diag;
0030 
0031 public:
0032   explicit OptionalDiagnostic(PartialDiagnostic *Diag = nullptr) : Diag(Diag) {}
0033 
0034   template <typename T> OptionalDiagnostic &operator<<(const T &v) {
0035     if (Diag)
0036       *Diag << v;
0037     return *this;
0038   }
0039 
0040   OptionalDiagnostic &operator<<(const llvm::APSInt &I) {
0041     if (Diag) {
0042       SmallVector<char, 32> Buffer;
0043       I.toString(Buffer);
0044       *Diag << StringRef(Buffer.data(), Buffer.size());
0045     }
0046     return *this;
0047   }
0048 
0049   OptionalDiagnostic &operator<<(const llvm::APFloat &F) {
0050     if (Diag) {
0051       // FIXME: Force the precision of the source value down so we don't
0052       // print digits which are usually useless (we don't really care here if
0053       // we truncate a digit by accident in edge cases).  Ideally,
0054       // APFloat::toString would automatically print the shortest
0055       // representation which rounds to the correct value, but it's a bit
0056       // tricky to implement. Could use std::to_chars.
0057       unsigned precision = llvm::APFloat::semanticsPrecision(F.getSemantics());
0058       precision = (precision * 59 + 195) / 196;
0059       SmallVector<char, 32> Buffer;
0060       F.toString(Buffer, precision);
0061       *Diag << StringRef(Buffer.data(), Buffer.size());
0062     }
0063     return *this;
0064   }
0065 
0066   OptionalDiagnostic &operator<<(const llvm::APFixedPoint &FX) {
0067     if (Diag) {
0068       SmallVector<char, 32> Buffer;
0069       FX.toString(Buffer);
0070       *Diag << StringRef(Buffer.data(), Buffer.size());
0071     }
0072     return *this;
0073   }
0074 };
0075 
0076 } // namespace clang
0077 
0078 #endif