113 lines · c
1//===----------------------------------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8///9/// \file10/// This file contains the declaration of TrapReasonBuilder and related classes.11///12//===----------------------------------------------------------------------===//13#ifndef LLVM_CLANG_CODEGEN_TRAP_REASON_BUILDER_H14#define LLVM_CLANG_CODEGEN_TRAP_REASON_BUILDER_H15#include "clang/Basic/Diagnostic.h"16 17namespace clang {18namespace CodeGen {19 20/// Helper class for \class TrapReasonBuilder. \class TrapReason stores the21/// "trap reason" built by \class TrapReasonBuilder. This consists of22/// a trap message and trap category.23///24/// It is intended that this object be allocated on the stack.25class TrapReason {26public:27 TrapReason() = default;28 /// \return The trap message. Note the lifetime of the underlying storage for29 /// the returned StringRef lives in this class which means the returned30 /// StringRef should not be used after this class is destroyed.31 StringRef getMessage() const { return Message; }32 33 /// \return the trap category (e.g. "Undefined Behavior Sanitizer")34 StringRef getCategory() const { return Category; }35 36 bool isEmpty() const {37 // Note both Message and Category are checked because it is legitimate for38 // the Message to be empty but for the Category to be non-empty when the39 // trap category is known but the specific reason is not available during40 // codegen.41 return Message.size() == 0 && Category.size() == 0;42 }43 44private:45 llvm::SmallString<64> Message;46 // The Category doesn't need its own storage because the StringRef points47 // to a global constant string.48 StringRef Category;49 50 // Only this class can set the private fields.51 friend class TrapReasonBuilder;52};53 54/// Class to make it convenient to initialize TrapReason objects which can be55/// used to attach the "trap reason" to trap instructions.56///57/// Although this class inherits from \class DiagnosticBuilder it has slightly58/// different semantics.59///60/// * This class should only be used with trap diagnostics (declared in61/// `DiagnosticTrapKinds.td`).62/// * The `TrapReasonBuilder` does not emit diagnostics to the normal63/// diagnostics consumers on destruction like normal Diagnostic builders.64/// Instead on destruction it assigns to the TrapReason object passed into65/// the constructor.66///67/// Given that this class inherits from `DiagnosticBuilder` it inherits all of68/// its abilities to format diagnostic messages and consume various types in69/// class (e.g. Type, Exprs, etc.). This makes it particularly suited to70/// printing types and expressions from the AST while codegen-ing runtime71/// checks.72///73///74/// Example use via the `CodeGenModule::BuildTrapReason` helper.75///76/// \code77/// {78/// TrapReason TR;79/// CGM.BuildTrapReason(diag::trap_diagnostic, TR) << 0 << SomeExpr;80/// consume(&TR);81/// }82/// \endcode83///84///85class TrapReasonBuilder : public DiagnosticBuilder {86public:87 TrapReasonBuilder(DiagnosticsEngine *DiagObj, unsigned DiagID,88 TrapReason &TR);89 ~TrapReasonBuilder();90 91 // Prevent accidentally copying or assigning92 TrapReasonBuilder &operator=(const TrapReasonBuilder &) = delete;93 TrapReasonBuilder &operator=(const TrapReasonBuilder &&) = delete;94 TrapReasonBuilder(const TrapReasonBuilder &) = delete;95 TrapReasonBuilder(const TrapReasonBuilder &&) = delete;96 97private:98 /// \return Format the trap message into `Storage`.99 void getMessage(SmallVectorImpl<char> &Storage);100 101 /// \return Return the trap category. These are the `CategoryName` property102 /// of `trap` diagnostics declared in `DiagnosticTrapKinds.td`.103 StringRef getCategory();104 105private:106 TrapReason &TR;107};108 109} // namespace CodeGen110} // namespace clang111 112#endif113