Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 1 | // |
| 2 | // Copyright 2020 The Abseil Authors. |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | #include "absl/flags/reflection.h" |
| 17 | |
| 18 | #include <assert.h> |
| 19 | |
| 20 | #include <atomic> |
| 21 | #include <map> |
| 22 | #include <string> |
| 23 | |
| 24 | #include "absl/base/config.h" |
| 25 | #include "absl/base/thread_annotations.h" |
| 26 | #include "absl/flags/commandlineflag.h" |
| 27 | #include "absl/flags/internal/private_handle_accessor.h" |
| 28 | #include "absl/flags/internal/registry.h" |
| 29 | #include "absl/flags/usage_config.h" |
| 30 | #include "absl/strings/str_cat.h" |
| 31 | #include "absl/strings/string_view.h" |
| 32 | #include "absl/synchronization/mutex.h" |
| 33 | |
| 34 | namespace absl { |
| 35 | ABSL_NAMESPACE_BEGIN |
| 36 | namespace flags_internal { |
| 37 | |
| 38 | // -------------------------------------------------------------------- |
| 39 | // FlagRegistry |
| 40 | // A FlagRegistry singleton object holds all flag objects indexed by their |
| 41 | // names so that if you know a flag's name, you can access or set it. If the |
| 42 | // function is named FooLocked(), you must own the registry lock before |
| 43 | // calling the function; otherwise, you should *not* hold the lock, and the |
| 44 | // function will acquire it itself if needed. |
| 45 | // -------------------------------------------------------------------- |
| 46 | |
| 47 | class FlagRegistry { |
| 48 | public: |
| 49 | FlagRegistry() = default; |
| 50 | ~FlagRegistry() = default; |
| 51 | |
| 52 | // Store a flag in this registry. Takes ownership of *flag. |
| 53 | void RegisterFlag(CommandLineFlag& flag); |
| 54 | |
| 55 | void Lock() ABSL_EXCLUSIVE_LOCK_FUNCTION(lock_) { lock_.Lock(); } |
| 56 | void Unlock() ABSL_UNLOCK_FUNCTION(lock_) { lock_.Unlock(); } |
| 57 | |
| 58 | // Returns the flag object for the specified name, or nullptr if not found. |
| 59 | // Will emit a warning if a 'retired' flag is specified. |
| 60 | CommandLineFlag* FindFlag(absl::string_view name); |
| 61 | |
| 62 | static FlagRegistry& GlobalRegistry(); // returns a singleton registry |
| 63 | |
| 64 | private: |
| 65 | friend class flags_internal::FlagSaverImpl; // reads all the flags in order |
| 66 | // to copy them |
| 67 | friend void ForEachFlag(std::function<void(CommandLineFlag&)> visitor); |
| 68 | friend void FinalizeRegistry(); |
| 69 | |
| 70 | // The map from name to flag, for FindFlag(). |
| 71 | using FlagMap = std::map<absl::string_view, CommandLineFlag*>; |
| 72 | using FlagIterator = FlagMap::iterator; |
| 73 | using FlagConstIterator = FlagMap::const_iterator; |
| 74 | FlagMap flags_; |
| 75 | std::vector<CommandLineFlag*> flat_flags_; |
| 76 | std::atomic<bool> finalized_flags_{false}; |
| 77 | |
| 78 | absl::Mutex lock_; |
| 79 | |
| 80 | // Disallow |
| 81 | FlagRegistry(const FlagRegistry&); |
| 82 | FlagRegistry& operator=(const FlagRegistry&); |
| 83 | }; |
| 84 | |
| 85 | namespace { |
| 86 | |
| 87 | class FlagRegistryLock { |
| 88 | public: |
| 89 | explicit FlagRegistryLock(FlagRegistry& fr) : fr_(fr) { fr_.Lock(); } |
| 90 | ~FlagRegistryLock() { fr_.Unlock(); } |
| 91 | |
| 92 | private: |
| 93 | FlagRegistry& fr_; |
| 94 | }; |
| 95 | |
| 96 | } // namespace |
| 97 | |
| 98 | CommandLineFlag* FlagRegistry::FindFlag(absl::string_view name) { |
| 99 | if (finalized_flags_.load(std::memory_order_acquire)) { |
| 100 | // We could save some gcus here if we make `Name()` be non-virtual. |
| 101 | // We could move the `const char*` name to the base class. |
| 102 | auto it = std::partition_point( |
| 103 | flat_flags_.begin(), flat_flags_.end(), |
| 104 | [=](CommandLineFlag* f) { return f->Name() < name; }); |
| 105 | if (it != flat_flags_.end() && (*it)->Name() == name) return *it; |
| 106 | } |
| 107 | |
| 108 | FlagRegistryLock frl(*this); |
| 109 | auto it = flags_.find(name); |
| 110 | return it != flags_.end() ? it->second : nullptr; |
| 111 | } |
| 112 | |
| 113 | void FlagRegistry::RegisterFlag(CommandLineFlag& flag) { |
| 114 | FlagRegistryLock registry_lock(*this); |
| 115 | |
| 116 | std::pair<FlagIterator, bool> ins = |
| 117 | flags_.insert(FlagMap::value_type(flag.Name(), &flag)); |
| 118 | if (ins.second == false) { // means the name was already in the map |
| 119 | CommandLineFlag& old_flag = *ins.first->second; |
| 120 | if (flag.IsRetired() != old_flag.IsRetired()) { |
| 121 | // All registrations must agree on the 'retired' flag. |
| 122 | flags_internal::ReportUsageError( |
| 123 | absl::StrCat( |
| 124 | "Retired flag '", flag.Name(), "' was defined normally in file '", |
| 125 | (flag.IsRetired() ? old_flag.Filename() : flag.Filename()), "'."), |
| 126 | true); |
| 127 | } else if (flags_internal::PrivateHandleAccessor::TypeId(flag) != |
| 128 | flags_internal::PrivateHandleAccessor::TypeId(old_flag)) { |
| 129 | flags_internal::ReportUsageError( |
| 130 | absl::StrCat("Flag '", flag.Name(), |
| 131 | "' was defined more than once but with " |
| 132 | "differing types. Defined in files '", |
| 133 | old_flag.Filename(), "' and '", flag.Filename(), "'."), |
| 134 | true); |
| 135 | } else if (old_flag.IsRetired()) { |
| 136 | return; |
| 137 | } else if (old_flag.Filename() != flag.Filename()) { |
| 138 | flags_internal::ReportUsageError( |
| 139 | absl::StrCat("Flag '", flag.Name(), |
| 140 | "' was defined more than once (in files '", |
| 141 | old_flag.Filename(), "' and '", flag.Filename(), "')."), |
| 142 | true); |
| 143 | } else { |
| 144 | flags_internal::ReportUsageError( |
| 145 | absl::StrCat( |
| 146 | "Something is wrong with flag '", flag.Name(), "' in file '", |
| 147 | flag.Filename(), "'. One possibility: file '", flag.Filename(), |
| 148 | "' is being linked both statically and dynamically into this " |
| 149 | "executable. e.g. some files listed as srcs to a test and also " |
| 150 | "listed as srcs of some shared lib deps of the same test."), |
| 151 | true); |
| 152 | } |
| 153 | // All cases above are fatal, except for the retired flags. |
| 154 | std::exit(1); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | FlagRegistry& FlagRegistry::GlobalRegistry() { |
| 159 | static FlagRegistry* global_registry = new FlagRegistry; |
| 160 | return *global_registry; |
| 161 | } |
| 162 | |
| 163 | // -------------------------------------------------------------------- |
| 164 | |
| 165 | void ForEachFlag(std::function<void(CommandLineFlag&)> visitor) { |
| 166 | FlagRegistry& registry = FlagRegistry::GlobalRegistry(); |
| 167 | |
| 168 | if (registry.finalized_flags_.load(std::memory_order_acquire)) { |
| 169 | for (const auto& i : registry.flat_flags_) visitor(*i); |
| 170 | } |
| 171 | |
| 172 | FlagRegistryLock frl(registry); |
| 173 | for (const auto& i : registry.flags_) visitor(*i.second); |
| 174 | } |
| 175 | |
| 176 | // -------------------------------------------------------------------- |
| 177 | |
| 178 | bool RegisterCommandLineFlag(CommandLineFlag& flag) { |
| 179 | FlagRegistry::GlobalRegistry().RegisterFlag(flag); |
| 180 | return true; |
| 181 | } |
| 182 | |
| 183 | void FinalizeRegistry() { |
| 184 | auto& registry = FlagRegistry::GlobalRegistry(); |
| 185 | FlagRegistryLock frl(registry); |
| 186 | if (registry.finalized_flags_.load(std::memory_order_relaxed)) { |
| 187 | // Was already finalized. Ignore the second time. |
| 188 | return; |
| 189 | } |
| 190 | registry.flat_flags_.reserve(registry.flags_.size()); |
| 191 | for (const auto& f : registry.flags_) { |
| 192 | registry.flat_flags_.push_back(f.second); |
| 193 | } |
| 194 | registry.flags_.clear(); |
| 195 | registry.finalized_flags_.store(true, std::memory_order_release); |
| 196 | } |
| 197 | |
| 198 | // -------------------------------------------------------------------- |
| 199 | |
| 200 | namespace { |
| 201 | |
| 202 | class RetiredFlagObj final : public CommandLineFlag { |
| 203 | public: |
| 204 | constexpr RetiredFlagObj(const char* name, FlagFastTypeId type_id) |
| 205 | : name_(name), type_id_(type_id) {} |
| 206 | |
| 207 | private: |
| 208 | absl::string_view Name() const override { return name_; } |
| 209 | std::string Filename() const override { |
| 210 | OnAccess(); |
| 211 | return "RETIRED"; |
| 212 | } |
| 213 | FlagFastTypeId TypeId() const override { return type_id_; } |
| 214 | std::string Help() const override { |
| 215 | OnAccess(); |
| 216 | return ""; |
| 217 | } |
| 218 | bool IsRetired() const override { return true; } |
| 219 | bool IsSpecifiedOnCommandLine() const override { |
| 220 | OnAccess(); |
| 221 | return false; |
| 222 | } |
| 223 | std::string DefaultValue() const override { |
| 224 | OnAccess(); |
| 225 | return ""; |
| 226 | } |
| 227 | std::string CurrentValue() const override { |
| 228 | OnAccess(); |
| 229 | return ""; |
| 230 | } |
| 231 | |
| 232 | // Any input is valid |
| 233 | bool ValidateInputValue(absl::string_view) const override { |
| 234 | OnAccess(); |
| 235 | return true; |
| 236 | } |
| 237 | |
| 238 | std::unique_ptr<flags_internal::FlagStateInterface> SaveState() override { |
| 239 | return nullptr; |
| 240 | } |
| 241 | |
| 242 | bool ParseFrom(absl::string_view, flags_internal::FlagSettingMode, |
| 243 | flags_internal::ValueSource, std::string&) override { |
| 244 | OnAccess(); |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | void CheckDefaultValueParsingRoundtrip() const override { OnAccess(); } |
| 249 | |
| 250 | void Read(void*) const override { OnAccess(); } |
| 251 | |
| 252 | void OnAccess() const { |
| 253 | flags_internal::ReportUsageError( |
| 254 | absl::StrCat("Accessing retired flag '", name_, "'"), false); |
| 255 | } |
| 256 | |
| 257 | // Data members |
| 258 | const char* const name_; |
| 259 | const FlagFastTypeId type_id_; |
| 260 | }; |
| 261 | |
| 262 | } // namespace |
| 263 | |
| 264 | void Retire(const char* name, FlagFastTypeId type_id, char* buf) { |
| 265 | static_assert(sizeof(RetiredFlagObj) == kRetiredFlagObjSize, ""); |
| 266 | static_assert(alignof(RetiredFlagObj) == kRetiredFlagObjAlignment, ""); |
| 267 | auto* flag = ::new (static_cast<void*>(buf)) |
| 268 | flags_internal::RetiredFlagObj(name, type_id); |
| 269 | FlagRegistry::GlobalRegistry().RegisterFlag(*flag); |
| 270 | } |
| 271 | |
| 272 | // -------------------------------------------------------------------- |
| 273 | |
| 274 | class FlagSaverImpl { |
| 275 | public: |
| 276 | FlagSaverImpl() = default; |
| 277 | FlagSaverImpl(const FlagSaverImpl&) = delete; |
| 278 | void operator=(const FlagSaverImpl&) = delete; |
| 279 | |
| 280 | // Saves the flag states from the flag registry into this object. |
| 281 | // It's an error to call this more than once. |
| 282 | void SaveFromRegistry() { |
| 283 | assert(backup_registry_.empty()); // call only once! |
| 284 | flags_internal::ForEachFlag([&](CommandLineFlag& flag) { |
| 285 | if (auto flag_state = |
| 286 | flags_internal::PrivateHandleAccessor::SaveState(flag)) { |
| 287 | backup_registry_.emplace_back(std::move(flag_state)); |
| 288 | } |
| 289 | }); |
| 290 | } |
| 291 | |
| 292 | // Restores the saved flag states into the flag registry. |
| 293 | void RestoreToRegistry() { |
| 294 | for (const auto& flag_state : backup_registry_) { |
| 295 | flag_state->Restore(); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | private: |
| 300 | std::vector<std::unique_ptr<flags_internal::FlagStateInterface>> |
| 301 | backup_registry_; |
| 302 | }; |
| 303 | |
| 304 | } // namespace flags_internal |
| 305 | |
| 306 | FlagSaver::FlagSaver() : impl_(new flags_internal::FlagSaverImpl) { |
| 307 | impl_->SaveFromRegistry(); |
| 308 | } |
| 309 | |
| 310 | FlagSaver::~FlagSaver() { |
| 311 | if (!impl_) return; |
| 312 | |
| 313 | impl_->RestoreToRegistry(); |
| 314 | delete impl_; |
| 315 | } |
| 316 | |
| 317 | // -------------------------------------------------------------------- |
| 318 | |
| 319 | CommandLineFlag* FindCommandLineFlag(absl::string_view name) { |
| 320 | if (name.empty()) return nullptr; |
| 321 | flags_internal::FlagRegistry& registry = |
| 322 | flags_internal::FlagRegistry::GlobalRegistry(); |
| 323 | return registry.FindFlag(name); |
| 324 | } |
| 325 | |
| 326 | // -------------------------------------------------------------------- |
| 327 | |
| 328 | absl::flat_hash_map<absl::string_view, absl::CommandLineFlag*> GetAllFlags() { |
| 329 | absl::flat_hash_map<absl::string_view, absl::CommandLineFlag*> res; |
| 330 | flags_internal::ForEachFlag([&](CommandLineFlag& flag) { |
| 331 | if (!flag.IsRetired()) res.insert({flag.Name(), &flag}); |
| 332 | }); |
| 333 | return res; |
| 334 | } |
| 335 | |
| 336 | ABSL_NAMESPACE_END |
| 337 | } // namespace absl |