================
@@ -1824,6 +1825,12 @@ class CodeGenModule : public CodeGenTypeCache {
     return PAlign;
   }
 
+  /// Helper function to construct a RuntimeTrapDiagnosticBuilder
+  [[nodiscard]] std::unique_ptr<RuntimeTrapDiagnosticBuilder>
+  RuntimeDiag(unsigned DiagID) {
+    return std::make_unique<RuntimeTrapDiagnosticBuilder>(&getDiags(), DiagID);
+  }
+
----------------
Sirraide wrote:

So if I’m understanding this correctly, the reason why you can’t ‘just put it 
on the stack’ is because the act of creating the diagnostic builder also 
eventually formats the diagnostic (because that’s the whole point of it after 
all), and we only want to create the diagnostic if we actually need it. That 
makes sense, and having a separate class to hold the message would probably 
work. Another idea I just had is to use `std::optional`:
```
std::optional<RuntimeTrapDiagnosticBuilder> DB;
if (some_condition) {
    DB = CGM.RuntimeDiag(trap_ubsan_arith) << 0 << someExpr;
};
consume(DB);
```
I wonder if this avoids copying the string after we format if we can just hold 
on to the diagnostic builder; we’d even avoid formatting the diagnostic until 
it’s actually used (in case we ever decide to create but not use it), and we’d 
be able to add more diagnostic arguments to it later on—that said, I’m not sure 
how well this approach would work, so feel free to go with whatever approach 
you think makes this easier to implement and maintain. ;Þ

https://github.com/llvm/llvm-project/pull/154618
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to