• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, const char* filename);
54 
Lock()55   void Lock() ABSL_EXCLUSIVE_LOCK_FUNCTION(lock_) { lock_.Lock(); }
Unlock()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:
FlagRegistryLock(FlagRegistry & fr)89   explicit FlagRegistryLock(FlagRegistry& fr) : fr_(fr) { fr_.Lock(); }
~FlagRegistryLock()90   ~FlagRegistryLock() { fr_.Unlock(); }
91 
92  private:
93   FlagRegistry& fr_;
94 };
95 
96 }  // namespace
97 
FindFlag(absl::string_view name)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 
RegisterFlag(CommandLineFlag & flag,const char * filename)113 void FlagRegistry::RegisterFlag(CommandLineFlag& flag, const char* filename) {
114   if (filename != nullptr &&
115       flag.Filename() != GetUsageConfig().normalize_filename(filename)) {
116     flags_internal::ReportUsageError(
117         absl::StrCat(
118             "Inconsistency between flag object and registration for flag '",
119             flag.Name(),
120             "', likely due to duplicate flags or an ODR violation. Relevant "
121             "files: ",
122             flag.Filename(), " and ", filename),
123         true);
124     std::exit(1);
125   }
126 
127   FlagRegistryLock registry_lock(*this);
128 
129   std::pair<FlagIterator, bool> ins =
130       flags_.insert(FlagMap::value_type(flag.Name(), &flag));
131   if (ins.second == false) {  // means the name was already in the map
132     CommandLineFlag& old_flag = *ins.first->second;
133     if (flag.IsRetired() != old_flag.IsRetired()) {
134       // All registrations must agree on the 'retired' flag.
135       flags_internal::ReportUsageError(
136           absl::StrCat(
137               "Retired flag '", flag.Name(), "' was defined normally in file '",
138               (flag.IsRetired() ? old_flag.Filename() : flag.Filename()), "'."),
139           true);
140     } else if (flags_internal::PrivateHandleAccessor::TypeId(flag) !=
141                flags_internal::PrivateHandleAccessor::TypeId(old_flag)) {
142       flags_internal::ReportUsageError(
143           absl::StrCat("Flag '", flag.Name(),
144                        "' was defined more than once but with "
145                        "differing types. Defined in files '",
146                        old_flag.Filename(), "' and '", flag.Filename(), "'."),
147           true);
148     } else if (old_flag.IsRetired()) {
149       return;
150     } else if (old_flag.Filename() != flag.Filename()) {
151       flags_internal::ReportUsageError(
152           absl::StrCat("Flag '", flag.Name(),
153                        "' was defined more than once (in files '",
154                        old_flag.Filename(), "' and '", flag.Filename(), "')."),
155           true);
156     } else {
157       flags_internal::ReportUsageError(
158           absl::StrCat(
159               "Something is wrong with flag '", flag.Name(), "' in file '",
160               flag.Filename(), "'. One possibility: file '", flag.Filename(),
161               "' is being linked both statically and dynamically into this "
162               "executable. e.g. some files listed as srcs to a test and also "
163               "listed as srcs of some shared lib deps of the same test."),
164           true);
165     }
166     // All cases above are fatal, except for the retired flags.
167     std::exit(1);
168   }
169 }
170 
GlobalRegistry()171 FlagRegistry& FlagRegistry::GlobalRegistry() {
172   static FlagRegistry* global_registry = new FlagRegistry;
173   return *global_registry;
174 }
175 
176 // --------------------------------------------------------------------
177 
ForEachFlag(std::function<void (CommandLineFlag &)> visitor)178 void ForEachFlag(std::function<void(CommandLineFlag&)> visitor) {
179   FlagRegistry& registry = FlagRegistry::GlobalRegistry();
180 
181   if (registry.finalized_flags_.load(std::memory_order_acquire)) {
182     for (const auto& i : registry.flat_flags_) visitor(*i);
183   }
184 
185   FlagRegistryLock frl(registry);
186   for (const auto& i : registry.flags_) visitor(*i.second);
187 }
188 
189 // --------------------------------------------------------------------
190 
RegisterCommandLineFlag(CommandLineFlag & flag,const char * filename)191 bool RegisterCommandLineFlag(CommandLineFlag& flag, const char* filename) {
192   FlagRegistry::GlobalRegistry().RegisterFlag(flag, filename);
193   return true;
194 }
195 
FinalizeRegistry()196 void FinalizeRegistry() {
197   auto& registry = FlagRegistry::GlobalRegistry();
198   FlagRegistryLock frl(registry);
199   if (registry.finalized_flags_.load(std::memory_order_relaxed)) {
200     // Was already finalized. Ignore the second time.
201     return;
202   }
203   registry.flat_flags_.reserve(registry.flags_.size());
204   for (const auto& f : registry.flags_) {
205     registry.flat_flags_.push_back(f.second);
206   }
207   registry.flags_.clear();
208   registry.finalized_flags_.store(true, std::memory_order_release);
209 }
210 
211 // --------------------------------------------------------------------
212 
213 namespace {
214 
215 class RetiredFlagObj final : public CommandLineFlag {
216  public:
RetiredFlagObj(const char * name,FlagFastTypeId type_id)217   constexpr RetiredFlagObj(const char* name, FlagFastTypeId type_id)
218       : name_(name), type_id_(type_id) {}
219 
220  private:
Name() const221   absl::string_view Name() const override { return name_; }
Filename() const222   std::string Filename() const override {
223     OnAccess();
224     return "RETIRED";
225   }
TypeId() const226   FlagFastTypeId TypeId() const override { return type_id_; }
Help() const227   std::string Help() const override {
228     OnAccess();
229     return "";
230   }
IsRetired() const231   bool IsRetired() const override { return true; }
IsSpecifiedOnCommandLine() const232   bool IsSpecifiedOnCommandLine() const override {
233     OnAccess();
234     return false;
235   }
DefaultValue() const236   std::string DefaultValue() const override {
237     OnAccess();
238     return "";
239   }
CurrentValue() const240   std::string CurrentValue() const override {
241     OnAccess();
242     return "";
243   }
244 
245   // Any input is valid
ValidateInputValue(absl::string_view) const246   bool ValidateInputValue(absl::string_view) const override {
247     OnAccess();
248     return true;
249   }
250 
SaveState()251   std::unique_ptr<flags_internal::FlagStateInterface> SaveState() override {
252     return nullptr;
253   }
254 
ParseFrom(absl::string_view,flags_internal::FlagSettingMode,flags_internal::ValueSource,std::string &)255   bool ParseFrom(absl::string_view, flags_internal::FlagSettingMode,
256                  flags_internal::ValueSource, std::string&) override {
257     OnAccess();
258     return false;
259   }
260 
CheckDefaultValueParsingRoundtrip() const261   void CheckDefaultValueParsingRoundtrip() const override { OnAccess(); }
262 
Read(void *) const263   void Read(void*) const override { OnAccess(); }
264 
OnAccess() const265   void OnAccess() const {
266     flags_internal::ReportUsageError(
267         absl::StrCat("Accessing retired flag '", name_, "'"), false);
268   }
269 
270   // Data members
271   const char* const name_;
272   const FlagFastTypeId type_id_;
273 };
274 
275 }  // namespace
276 
Retire(const char * name,FlagFastTypeId type_id,char * buf)277 void Retire(const char* name, FlagFastTypeId type_id, char* buf) {
278   static_assert(sizeof(RetiredFlagObj) == kRetiredFlagObjSize, "");
279   static_assert(alignof(RetiredFlagObj) == kRetiredFlagObjAlignment, "");
280   auto* flag = ::new (static_cast<void*>(buf))
281       flags_internal::RetiredFlagObj(name, type_id);
282   FlagRegistry::GlobalRegistry().RegisterFlag(*flag, nullptr);
283 }
284 
285 // --------------------------------------------------------------------
286 
287 class FlagSaverImpl {
288  public:
289   FlagSaverImpl() = default;
290   FlagSaverImpl(const FlagSaverImpl&) = delete;
291   void operator=(const FlagSaverImpl&) = delete;
292 
293   // Saves the flag states from the flag registry into this object.
294   // It's an error to call this more than once.
SaveFromRegistry()295   void SaveFromRegistry() {
296     assert(backup_registry_.empty());  // call only once!
297     flags_internal::ForEachFlag([&](CommandLineFlag& flag) {
298       if (auto flag_state =
299               flags_internal::PrivateHandleAccessor::SaveState(flag)) {
300         backup_registry_.emplace_back(std::move(flag_state));
301       }
302     });
303   }
304 
305   // Restores the saved flag states into the flag registry.
RestoreToRegistry()306   void RestoreToRegistry() {
307     for (const auto& flag_state : backup_registry_) {
308       flag_state->Restore();
309     }
310   }
311 
312  private:
313   std::vector<std::unique_ptr<flags_internal::FlagStateInterface>>
314       backup_registry_;
315 };
316 
317 }  // namespace flags_internal
318 
FlagSaver()319 FlagSaver::FlagSaver() : impl_(new flags_internal::FlagSaverImpl) {
320   impl_->SaveFromRegistry();
321 }
322 
~FlagSaver()323 FlagSaver::~FlagSaver() {
324   if (!impl_) return;
325 
326   impl_->RestoreToRegistry();
327   delete impl_;
328 }
329 
330 // --------------------------------------------------------------------
331 
FindCommandLineFlag(absl::string_view name)332 CommandLineFlag* FindCommandLineFlag(absl::string_view name) {
333   if (name.empty()) return nullptr;
334   flags_internal::FlagRegistry& registry =
335       flags_internal::FlagRegistry::GlobalRegistry();
336   return registry.FindFlag(name);
337 }
338 
339 // --------------------------------------------------------------------
340 
GetAllFlags()341 absl::flat_hash_map<absl::string_view, absl::CommandLineFlag*> GetAllFlags() {
342   absl::flat_hash_map<absl::string_view, absl::CommandLineFlag*> res;
343   flags_internal::ForEachFlag([&](CommandLineFlag& flag) {
344     if (!flag.IsRetired()) res.insert({flag.Name(), &flag});
345   });
346   return res;
347 }
348 
349 ABSL_NAMESPACE_END
350 }  // namespace absl
351