From a73c2cd28801056dd80da67538a09dc9025077a5 Mon Sep 17 00:00:00 2001 From: Morten Borup Petersen Date: Tue, 6 Aug 2024 10:59:54 +0000 Subject: [PATCH] Add lock --- include/circt/Support/Namespace.h | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/include/circt/Support/Namespace.h b/include/circt/Support/Namespace.h index 29fc53a2e065..652426d6b117 100644 --- a/include/circt/Support/Namespace.h +++ b/include/circt/Support/Namespace.h @@ -61,7 +61,11 @@ class Namespace { /// Removes a symbol from the namespace. Returns true if the symbol was /// removed, false if the symbol was not found. - bool erase(llvm::StringRef symbol) { return nextIndex.erase(symbol); } + /// This is only allowed to be called _before_ any call to newName. + bool erase(llvm::StringRef symbol) { + assert(!locked && "Cannot erase names from a locked namespace"); + return nextIndex.erase(symbol); + } /// Empty the namespace. void clear() { nextIndex.clear(); } @@ -74,6 +78,7 @@ class Namespace { /// 2. The name is given a `_` suffix where `` is a number starting from /// `0` and incrementing by one each time (`_0`, ...). StringRef newName(const Twine &name) { + locked = true; // Special case the situation where there is no name collision to avoid // messing with the SmallString allocation below. llvm::SmallString<64> tryName; @@ -146,6 +151,11 @@ class Namespace { // namespace. It follows that all values less than the "next index" value are // already used. llvm::StringMap nextIndex; + + // When true, no names can be erased from the namespace. This is to prevent + // erasing names after they have been used, thus leaving users of the + // namespace in an inconsistent state. + bool locked = false; }; } // namespace circt