From be057c5a011a7a9238abcdcec13eee3c93d8fc74 Mon Sep 17 00:00:00 2001 From: Matthew Woehlke Date: Thu, 23 Apr 2026 13:30:41 -0400 Subject: [PATCH] cmVariableWatch: Use enum for access type Adjust variable watches to pass the access type as the `enum` rather than as an `int`. --- Source/cmVariableWatch.cxx | 13 ++++++------ Source/cmVariableWatch.h | 34 +++++++++++++++---------------- Source/cmVariableWatchCommand.cxx | 11 +++++----- Source/cmake.cxx | 5 +++-- 4 files changed, 32 insertions(+), 31 deletions(-) diff --git a/Source/cmVariableWatch.cxx b/Source/cmVariableWatch.cxx index 5ad00fe604..b46bfa347d 100644 --- a/Source/cmVariableWatch.cxx +++ b/Source/cmVariableWatch.cxx @@ -7,16 +7,16 @@ #include #include -std::string const& cmVariableWatch::GetAccessAsString(int access_type) +std::string const& cmVariableWatch::GetAccessAsString(AccessType accessType) { static std::array const cmVariableWatchAccessStrings = { { "READ_ACCESS", "UNKNOWN_READ_ACCESS", "UNKNOWN_DEFINED_ACCESS", "MODIFIED_ACCESS", "REMOVED_ACCESS", "NO_ACCESS" } }; - if (access_type < 0 || access_type >= cmVariableWatch::NO_ACCESS) { - access_type = cmVariableWatch::NO_ACCESS; + if (accessType >= cmVariableWatch::NO_ACCESS) { + accessType = cmVariableWatch::NO_ACCESS; } - return cmVariableWatchAccessStrings[access_type]; + return cmVariableWatchAccessStrings[accessType]; } cmVariableWatch::cmVariableWatch() = default; @@ -63,7 +63,8 @@ void cmVariableWatch::RemoveWatch(std::string const& variable, } bool cmVariableWatch::VariableAccessed(std::string const& variable, - int access_type, char const* newValue, + AccessType accessType, + char const* newValue, cmMakefile const* mf) const { auto mit = this->WatchMap.find(variable); @@ -77,7 +78,7 @@ bool cmVariableWatch::VariableAccessed(std::string const& variable, // lockable, and so this ensures we don't attempt to call into freed // memory if (auto it = weak_it.lock()) { - it->Method(variable, access_type, it->ClientData, newValue, mf); + it->Method(variable, accessType, it->ClientData, newValue, mf); } } return true; diff --git a/Source/cmVariableWatch.h b/Source/cmVariableWatch.h index ffd7192341..0d8fff58ea 100644 --- a/Source/cmVariableWatch.h +++ b/Source/cmVariableWatch.h @@ -19,8 +19,21 @@ class cmMakefile; class cmVariableWatch { public: - using WatchMethod = void (*)(std::string const&, int, void*, char const*, - cmMakefile const*); + /** + * Different access types. + */ + enum AccessType : unsigned + { + VARIABLE_READ_ACCESS, + UNKNOWN_VARIABLE_READ_ACCESS, + UNKNOWN_VARIABLE_DEFINED_ACCESS, + VARIABLE_MODIFIED_ACCESS, + VARIABLE_REMOVED_ACCESS, + NO_ACCESS + }; + + using WatchMethod = void (*)(std::string const&, AccessType, void*, + char const*, cmMakefile const*); using DeleteData = void (*)(void*); cmVariableWatch(); @@ -37,26 +50,13 @@ public: /** * This method is called when variable is accessed */ - bool VariableAccessed(std::string const& variable, int access_type, + bool VariableAccessed(std::string const& variable, AccessType accessType, char const* newValue, cmMakefile const* mf) const; - /** - * Different access types. - */ - enum - { - VARIABLE_READ_ACCESS, - UNKNOWN_VARIABLE_READ_ACCESS, - UNKNOWN_VARIABLE_DEFINED_ACCESS, - VARIABLE_MODIFIED_ACCESS, - VARIABLE_REMOVED_ACCESS, - NO_ACCESS - }; - /** * Return the access as string */ - static std::string const& GetAccessAsString(int access_type); + static std::string const& GetAccessAsString(AccessType accessType); protected: struct Pair diff --git a/Source/cmVariableWatchCommand.cxx b/Source/cmVariableWatchCommand.cxx index f5d47c6866..930d5d6000 100644 --- a/Source/cmVariableWatchCommand.cxx +++ b/Source/cmVariableWatchCommand.cxx @@ -25,20 +25,19 @@ struct cmVariableWatchCallbackData std::string Command; }; -void cmVariableWatchCommandVariableAccessed(std::string const& variable, - int access_type, void* client_data, - char const* newValue, - cmMakefile const* mf) +void cmVariableWatchCommandVariableAccessed( + std::string const& variable, cmVariableWatch::AccessType accessType, + void* clientData, char const* newValue, cmMakefile const* mf) { cmVariableWatchCallbackData* data = - static_cast(client_data); + static_cast(clientData); if (data->InCallback) { return; } data->InCallback = true; - auto accessString = cmVariableWatch::GetAccessAsString(access_type); + auto accessString = cmVariableWatch::GetAccessAsString(accessType); /// Ultra bad!! cmMakefile* makefile = const_cast(mf); diff --git a/Source/cmake.cxx b/Source/cmake.cxx index c0afa44b3a..6a4bab6d17 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -166,8 +166,9 @@ using CommandArgument = cmCommandLineArgument; #ifndef CMAKE_BOOTSTRAP -void cmWarnUnusedCliWarning(std::string const& variable, int /*unused*/, - void* ctx, char const* /*unused*/, +void cmWarnUnusedCliWarning(std::string const& variable, + cmVariableWatch::AccessType /*unused*/, void* ctx, + char const* /*unused*/, cmMakefile const* /*unused*/) { cmake* cm = reinterpret_cast(ctx);