| 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-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
/// |
| 9 |
/// \file |
| 10 |
/// This file contains the declaration of TrapReasonBuilder and related classes. |
| 11 |
/// |
| 12 |
//===----------------------------------------------------------------------===// |
| 13 |
#ifndef LLVM_CLANG_CODEGEN_TRAP_REASON_BUILDER_H |
| 14 |
#define LLVM_CLANG_CODEGEN_TRAP_REASON_BUILDER_H |
| 15 |
#include "clang/Basic/Diagnostic.h" |
| 16 |
|
| 17 |
namespace clang { |
| 18 |
namespace CodeGen { |
| 19 |
|
| 20 |
/// Helper class for \class TrapReasonBuilder. \class TrapReason stores the |
| 21 |
/// "trap reason" built by \class TrapReasonBuilder. This consists of |
| 22 |
/// a trap message and trap category. |
| 23 |
/// |
| 24 |
/// It is intended that this object be allocated on the stack. |
| 25 |
class TrapReason { |
| 26 |
public: |
| 27 |
TrapReason() = default; |
| 28 |
/// \return The trap message. Note the lifetime of the underlying storage for |
| 29 |
/// the returned StringRef lives in this class which means the returned |
| 30 |
/// 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 for |
| 38 |
// the Message to be empty but for the Category to be non-empty when the |
| 39 |
// trap category is known but the specific reason is not available during |
| 40 |
// codegen. |
| 41 |
return Message.size() == 0 && Category.size() == 0; |
| 42 |
} |
| 43 |
|
| 44 |
private: |
| 45 |
llvm::SmallString<64> Message; |
| 46 |
// The Category doesn't need its own storage because the StringRef points |
| 47 |
// 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 be |
| 55 |
/// used to attach the "trap reason" to trap instructions. |
| 56 |
/// |
| 57 |
/// Although this class inherits from \class DiagnosticBuilder it has slightly |
| 58 |
/// different semantics. |
| 59 |
/// |
| 60 |
/// * This class should only be used with trap diagnostics (declared in |
| 61 |
/// `DiagnosticTrapKinds.td`). |
| 62 |
/// * The `TrapReasonBuilder` does not emit diagnostics to the normal |
| 63 |
/// diagnostics consumers on destruction like normal Diagnostic builders. |
| 64 |
/// Instead on destruction it assigns to the TrapReason object passed into |
| 65 |
/// the constructor. |
| 66 |
/// |
| 67 |
/// Given that this class inherits from `DiagnosticBuilder` it inherits all of |
| 68 |
/// its abilities to format diagnostic messages and consume various types in |
| 69 |
/// class (e.g. Type, Exprs, etc.). This makes it particularly suited to |
| 70 |
/// printing types and expressions from the AST while codegen-ing runtime |
| 71 |
/// checks. |
| 72 |
/// |
| 73 |
/// |
| 74 |
/// Example use via the `CodeGenModule::BuildTrapReason` helper. |
| 75 |
/// |
| 76 |
/// \code |
| 77 |
/// { |
| 78 |
/// TrapReason TR; |
| 79 |
/// CGM.BuildTrapReason(diag::trap_diagnostic, TR) << 0 << SomeExpr; |
| 80 |
/// consume(&TR); |
| 81 |
/// } |
| 82 |
/// \endcode |
| 83 |
/// |
| 84 |
/// |
| 85 |
class TrapReasonBuilder : public DiagnosticBuilder { |
| 86 |
public: |
| 87 |
TrapReasonBuilder(DiagnosticsEngine *DiagObj, unsigned DiagID, |
| 88 |
TrapReason &TR); |
| 89 |
~TrapReasonBuilder(); |
| 90 |
|
| 91 |
// Prevent accidentally copying or assigning |
| 92 |
TrapReasonBuilder &operator=(const TrapReasonBuilder &) = delete; |
| 93 |
TrapReasonBuilder &operator=(const TrapReasonBuilder &&) = delete; |
| 94 |
TrapReasonBuilder(const TrapReasonBuilder &) = delete; |
| 95 |
TrapReasonBuilder(const TrapReasonBuilder &&) = delete; |
| 96 |
|
| 97 |
private: |
| 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` property |
| 102 |
/// of `trap` diagnostics declared in `DiagnosticTrapKinds.td`. |
| 103 |
StringRef getCategory(); |
| 104 |
|
| 105 |
private: |
| 106 |
TrapReason &TR; |
| 107 |
}; |
| 108 |
|
| 109 |
} // namespace CodeGen |
| 110 |
} // namespace clang |
| 111 |
|
| 112 |
#endif |