[llvm] [SandboxIR] Clean up tracking code with the help of tryTrack() (PR #102406)

via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 9 10:47:14 PDT 2024


================
@@ -383,7 +383,23 @@ class Tracker {
   Context &getContext() const { return Ctx; }
   /// Record \p Change and take ownership. This is the main function used to
   /// track Sandbox IR changes.
-  void track(std::unique_ptr<IRChangeBase> &&Change);
+  void track(std::unique_ptr<IRChangeBase> &&Change) {
+    assert(State == TrackerState::Record && "The tracker should be tracking!");
+    Changes.push_back(std::move(Change));
+
+#ifndef NDEBUG
+    InMiddleOfCreatingChange = false;
+#endif
+  }
+  /// A convenience wrapper for `track()` that constructs and tracks the Change
+  /// object if tracking is enabled. \Returns true if tracking is enabled.
+  template <typename ChangeT, typename... ArgsT>
+  bool emplaceIfTracking(ArgsT... ChangeArgs) {
+    if (!isTracking())
+      return false;
+    track(std::make_unique<ChangeT>(ChangeArgs..., *this));
----------------
vporpo wrote:

An alternative would be to remove the Tracer argument from the constructors and set the parent within track(). This is not as ugly as it seems at first glance because the change objects are only created within the track() function. And btw their constructors should be made private since the user won't create these objects directly.
Wdyt?

https://github.com/llvm/llvm-project/pull/102406


More information about the llvm-commits mailing list