This provides a class to wrap globals like input_location or cfun that should
not be used directly and will ideally go away.  This class tracks if access to
the global is currently blocked and asserts if accessed when that is not
allowed.  It also adds a class to mark access as blocked for the lifetime of the
scope.

bootstrapped and regtested on x86_64-linux-gnu, ok?

Trev

gcc/ChangeLog:

        * poison.h: New file.
---
 gcc/poison.h | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 88 insertions(+)
 create mode 100644 gcc/poison.h

diff --git a/gcc/poison.h b/gcc/poison.h
new file mode 100644
index 00000000000..239ab1cb91a
--- /dev/null
+++ b/gcc/poison.h
@@ -0,0 +1,88 @@
+/* Simple utility to poison globals that should be avoided.
+
+   Copyright (C) 2021 the GNU Toolchain Authors
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 3, or (at your option) any later
+version.
+
+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+#ifndef GCC_POISON_H
+#define GCC_POISON_H
+
+template<typename T> class auto_poison;
+
+/* This class is intended to be used as a transparent wrapper around the type 
of
+   a global object that we would like to stop using.  */
+template<typename T>
+class poisonable
+{
+public:
+  poisonable () : m_val (), m_poisoned (false) {}
+  explicit poisonable (T val) : m_val (val), m_poisoned (false) {}
+
+  operator T& ()
+    {
+      gcc_assert (!m_poisoned);
+      return m_val;
+    }
+
+  poisonable &operator= (T val)
+    {
+      gcc_assert (!m_poisoned);
+      m_val = val;
+      return *this;
+    }
+
+  T *operator& ()
+    {
+      gcc_assert (!m_poisoned);
+      return &m_val;
+    }
+
+  poisonable (const poisonable &) = delete;
+  poisonable (poisonable &&) = delete;
+  poisonable &operator= (const poisonable &) = delete;
+  poisonable &operator= (poisonable &&) = delete;
+
+private:
+  friend class auto_poison<T>;
+
+  T m_val;
+  bool m_poisoned;
+  };
+
+/* This class provides a way to make a global inaccessible in the given scope,
+   and any functions called within that scope.  */
+template<typename T>
+class auto_poison
+{
+public:
+  auto_poison (poisonable<T> &p) : m_target (p)
+  {
+    gcc_assert (!p.m_poisoned);
+    p.m_poisoned = true;
+  }
+  ~auto_poison () { m_target.m_poisoned = false; }
+
+  auto_poison (const auto_poison &) = delete;
+  auto_poison (auto_poison &&) = delete;
+  auto_poison &operator= (const auto_poison &) = delete;
+  auto_poison &operator= (auto_poison &&) = delete;
+
+private:
+  poisonable<T> &m_target;
+};
+
+#endif
-- 
2.20.1

Reply via email to