about summary refs log tree commit diff
path: root/absl/flags
diff options
context:
space:
mode:
Diffstat (limited to 'absl/flags')
-rw-r--r--absl/flags/declare.h2
-rw-r--r--absl/flags/flag.cc2
-rw-r--r--absl/flags/flag.h2
-rw-r--r--absl/flags/flag_test_defs.cc2
-rw-r--r--absl/flags/internal/commandlineflag.cc2
-rw-r--r--absl/flags/internal/commandlineflag.h4
-rw-r--r--absl/flags/internal/flag.cc113
-rw-r--r--absl/flags/internal/flag.h54
-rw-r--r--absl/flags/internal/parse.h2
-rw-r--r--absl/flags/internal/path_util.h2
-rw-r--r--absl/flags/internal/program_name.cc2
-rw-r--r--absl/flags/internal/program_name.h2
-rw-r--r--absl/flags/internal/registry.cc4
-rw-r--r--absl/flags/internal/registry.h2
-rw-r--r--absl/flags/internal/type_erased.cc2
-rw-r--r--absl/flags/internal/type_erased.h2
-rw-r--r--absl/flags/internal/usage.cc2
-rw-r--r--absl/flags/internal/usage.h2
-rw-r--r--absl/flags/marshalling.cc2
-rw-r--r--absl/flags/marshalling.h2
-rw-r--r--absl/flags/parse.cc4
-rw-r--r--absl/flags/parse.h2
-rw-r--r--absl/flags/usage.cc2
-rw-r--r--absl/flags/usage.h2
-rw-r--r--absl/flags/usage_config.cc2
-rw-r--r--absl/flags/usage_config.h2
26 files changed, 156 insertions, 65 deletions
diff --git a/absl/flags/declare.h b/absl/flags/declare.h
index 4926a09ef4dd..f7509ce77ffc 100644
--- a/absl/flags/declare.h
+++ b/absl/flags/declare.h
@@ -28,6 +28,7 @@
 #include "absl/strings/string_view.h"
 
 namespace absl {
+ABSL_NAMESPACE_BEGIN
 namespace flags_internal {
 
 // absl::Flag<T> represents a flag of type 'T' created by ABSL_FLAG.
@@ -47,6 +48,7 @@ template <typename T>
 using Flag = flags_internal::Flag<T>;
 #endif
 
+ABSL_NAMESPACE_END
 }  // namespace absl
 
 // ABSL_DECLARE_FLAG()
diff --git a/absl/flags/flag.cc b/absl/flags/flag.cc
index 37bbce233117..7faa7ade0d0d 100644
--- a/absl/flags/flag.cc
+++ b/absl/flags/flag.cc
@@ -18,6 +18,7 @@
 #include <cstring>
 
 namespace absl {
+ABSL_NAMESPACE_BEGIN
 
 // We want to validate the type mismatch between type definition and
 // declaration. The lock-free implementation does not allow us to do it,
@@ -55,4 +56,5 @@ absl::Mutex* GetGlobalConstructionGuard() { return &construction_guard; }
 
 #endif
 
+ABSL_NAMESPACE_END
 }  // namespace absl
diff --git a/absl/flags/flag.h b/absl/flags/flag.h
index 52c3cede690a..b6fbd116ed2f 100644
--- a/absl/flags/flag.h
+++ b/absl/flags/flag.h
@@ -38,6 +38,7 @@
 #include "absl/flags/marshalling.h"
 
 namespace absl {
+ABSL_NAMESPACE_BEGIN
 
 // Flag
 //
@@ -219,6 +220,7 @@ void SetFlag(absl::Flag<T>* flag, const V& v) {
   flag->Set(value);
 }
 
+ABSL_NAMESPACE_END
 }  // namespace absl
 
 
diff --git a/absl/flags/flag_test_defs.cc b/absl/flags/flag_test_defs.cc
index 3366c5803279..49f91dee3922 100644
--- a/absl/flags/flag_test_defs.cc
+++ b/absl/flags/flag_test_defs.cc
@@ -20,3 +20,5 @@
 
 ABSL_FLAG(int, mistyped_int_flag, 0, "");
 ABSL_FLAG(std::string, mistyped_string_flag, "", "");
+ABSL_RETIRED_FLAG(bool, old_bool_flag, true,
+                  "repetition of retired flag definition");
diff --git a/absl/flags/internal/commandlineflag.cc b/absl/flags/internal/commandlineflag.cc
index 88f91e16d196..09249274c3e7 100644
--- a/absl/flags/internal/commandlineflag.cc
+++ b/absl/flags/internal/commandlineflag.cc
@@ -18,6 +18,7 @@
 #include "absl/flags/usage_config.h"
 
 namespace absl {
+ABSL_NAMESPACE_BEGIN
 namespace flags_internal {
 
 // The help message indicating that the commandline flag has been
@@ -57,4 +58,5 @@ std::string CommandLineFlag::Filename() const {
 }
 
 }  // namespace flags_internal
+ABSL_NAMESPACE_END
 }  // namespace absl
diff --git a/absl/flags/internal/commandlineflag.h b/absl/flags/internal/commandlineflag.h
index 7964f1bf8c4a..49e13d1e726b 100644
--- a/absl/flags/internal/commandlineflag.h
+++ b/absl/flags/internal/commandlineflag.h
@@ -23,6 +23,7 @@
 #include "absl/types/optional.h"
 
 namespace absl {
+ABSL_NAMESPACE_BEGIN
 namespace flags_internal {
 
 // Type-specific operations, eg., parsing, copying, etc. are provided
@@ -158,7 +159,7 @@ class CommandLineFlag {
       : name_(name), filename_(filename) {}
 
   // Virtual destructor
-  virtual void Destroy() const = 0;
+  virtual void Destroy() = 0;
 
   // Not copyable/assignable.
   CommandLineFlag(const CommandLineFlag&) = delete;
@@ -280,6 +281,7 @@ class CommandLineFlag {
   A(float)
 
 }  // namespace flags_internal
+ABSL_NAMESPACE_END
 }  // namespace absl
 
 #endif  // ABSL_FLAGS_INTERNAL_COMMANDLINEFLAG_H_
diff --git a/absl/flags/internal/flag.cc b/absl/flags/internal/flag.cc
index 435cc362478f..599bd7a45561 100644
--- a/absl/flags/internal/flag.cc
+++ b/absl/flags/internal/flag.cc
@@ -19,6 +19,7 @@
 #include "absl/synchronization/mutex.h"
 
 namespace absl {
+ABSL_NAMESPACE_BEGIN
 namespace flags_internal {
 namespace {
 
@@ -34,20 +35,40 @@ bool ShouldValidateFlagValue(const CommandLineFlag& flag) {
   return true;
 }
 
+// RAII helper used to temporarily unlock and relock `absl::Mutex`.
+// This is used when we need to ensure that locks are released while
+// invoking user supplied callbacks and then reacquired, since callbacks may
+// need to acquire these locks themselves.
+class MutexRelock {
+ public:
+  explicit MutexRelock(absl::Mutex* mu) : mu_(mu) { mu_->Unlock(); }
+  ~MutexRelock() { mu_->Lock(); }
+
+  MutexRelock(const MutexRelock&) = delete;
+  MutexRelock& operator=(const MutexRelock&) = delete;
+
+ private:
+  absl::Mutex* mu_;
+};
+
+// This global lock guards the initialization and destruction of data_guard_,
+// which is used to guard the other Flag data.
+ABSL_CONST_INIT static absl::Mutex flag_mutex_lifetime_guard(absl::kConstInit);
+
 }  // namespace
 
 void FlagImpl::Init() {
-  ABSL_CONST_INIT static absl::Mutex init_lock(absl::kConstInit);
-
   {
-    absl::MutexLock lock(&init_lock);
+    absl::MutexLock lock(&flag_mutex_lifetime_guard);
 
-    if (locks_ == nullptr) {  // Must initialize Mutexes for this flag.
-      locks_ = new FlagImpl::CommandLineFlagLocks;
+    // Must initialize data guard for this flag.
+    if (!is_data_guard_inited_) {
+      new (&data_guard_) absl::Mutex;
+      is_data_guard_inited_ = true;
     }
   }
 
-  absl::MutexLock lock(&locks_->primary_mu);
+  absl::MutexLock lock(reinterpret_cast<absl::Mutex*>(&data_guard_));
 
   if (cur_ != nullptr) {
     inited_.store(true, std::memory_order_release);
@@ -67,21 +88,29 @@ absl::Mutex* FlagImpl::DataGuard() const {
     const_cast<FlagImpl*>(this)->Init();
   }
 
-  // All fields initialized; locks_ is therefore safe to read.
-  return &locks_->primary_mu;
+  // data_guard_ is initialized.
+  return reinterpret_cast<absl::Mutex*>(&data_guard_);
 }
 
-void FlagImpl::Destroy() const {
+void FlagImpl::Destroy() {
   {
     absl::MutexLock l(DataGuard());
 
     // Values are heap allocated for Abseil Flags.
     if (cur_) Delete(op_, cur_);
-    if (def_kind_ == FlagDefaultSrcKind::kDynamicValue)
+
+    // Release the dynamically allocated default value if any.
+    if (def_kind_ == FlagDefaultSrcKind::kDynamicValue) {
       Delete(op_, default_src_.dynamic_value);
+    }
+
+    // If this flag has an assigned callback, release callback data.
+    if (callback_data_) delete callback_data_;
   }
 
-  delete locks_;
+  absl::MutexLock l(&flag_mutex_lifetime_guard);
+  DataGuard()->~Mutex();
+  is_data_guard_inited_ = false;
 }
 
 std::unique_ptr<void, DynValueDeleter> FlagImpl::MakeInitValue() const {
@@ -126,13 +155,20 @@ void FlagImpl::SetCallback(
     const flags_internal::FlagCallback mutation_callback) {
   absl::MutexLock l(DataGuard());
 
-  callback_ = mutation_callback;
+  if (callback_data_ == nullptr) {
+    callback_data_ = new CallbackData;
+  }
+  callback_data_->func = mutation_callback;
 
   InvokeCallback();
 }
 
 void FlagImpl::InvokeCallback() const {
-  if (!callback_) return;
+  if (!callback_data_) return;
+
+  // Make a copy of the C-style function pointer that we are about to invoke
+  // before we release the lock guarding it.
+  FlagCallback cb = callback_data_->func;
 
   // If the flag has a mutation callback this function invokes it. While the
   // callback is being invoked the primary flag's mutex is unlocked and it is
@@ -145,14 +181,9 @@ void FlagImpl::InvokeCallback() const {
   // and it also can be different by the time the callback invocation is
   // completed. Requires that *primary_lock be held in exclusive mode; it may be
   // released and reacquired by the implementation.
-  DataGuard()->Unlock();
-
-  {
-    absl::MutexLock lock(&locks_->callback_mu);
-    callback_();
-  }
-
-  DataGuard()->Lock();
+  MutexRelock relock(DataGuard());
+  absl::MutexLock lock(&callback_data_->guard);
+  cb();
 }
 
 bool FlagImpl::RestoreState(const CommandLineFlag& flag, const void* value,
@@ -177,12 +208,13 @@ bool FlagImpl::RestoreState(const CommandLineFlag& flag, const void* value,
 }
 
 // Attempts to parse supplied `value` string using parsing routine in the `flag`
-// argument. If parsing successful, this function stores the parsed value in
-// 'dst' assuming it is a pointer to the flag's value type. In case if any error
-// is encountered in either step, the error message is stored in 'err'
-bool FlagImpl::TryParse(const CommandLineFlag& flag, void* dst,
+// argument. If parsing successful, this function replaces the dst with newly
+// parsed value. In case if any error is encountered in either step, the error
+// message is stored in 'err'
+bool FlagImpl::TryParse(const CommandLineFlag& flag, void** dst,
                         absl::string_view value, std::string* err) const {
   auto tentative_value = MakeInitValue();
+
   std::string parse_err;
   if (!Parse(marshalling_op_, value, tentative_value.get(), &parse_err)) {
     auto type_name = flag.Typename();
@@ -194,7 +226,10 @@ bool FlagImpl::TryParse(const CommandLineFlag& flag, void* dst,
     return false;
   }
 
-  Copy(op_, tentative_value.get(), dst);
+  void* old_val = *dst;
+  *dst = tentative_value.release();
+  tentative_value.reset(old_val);
+
   return true;
 }
 
@@ -274,7 +309,7 @@ bool FlagImpl::SetFromString(const CommandLineFlag& flag,
   switch (set_mode) {
     case SET_FLAGS_VALUE: {
       // set or modify the flag's value
-      if (!TryParse(flag, cur_, value, err)) return false;
+      if (!TryParse(flag, &cur_, value, err)) return false;
       modified_ = true;
       counter_++;
       StoreAtomic();
@@ -288,7 +323,7 @@ bool FlagImpl::SetFromString(const CommandLineFlag& flag,
     case SET_FLAG_IF_DEFAULT: {
       // set the flag's value, but only if it hasn't been set by someone else
       if (!modified_) {
-        if (!TryParse(flag, cur_, value, err)) return false;
+        if (!TryParse(flag, &cur_, value, err)) return false;
         modified_ = true;
         counter_++;
         StoreAtomic();
@@ -305,18 +340,19 @@ bool FlagImpl::SetFromString(const CommandLineFlag& flag,
       break;
     }
     case SET_FLAGS_DEFAULT: {
-      // Flag's new default-value.
-      auto new_default_value = MakeInitValue();
-
-      if (!TryParse(flag, new_default_value.get(), value, err)) return false;
-
       if (def_kind_ == FlagDefaultSrcKind::kDynamicValue) {
-        // Release old default value.
-        Delete(op_, default_src_.dynamic_value);
-      }
+        if (!TryParse(flag, &default_src_.dynamic_value, value, err)) {
+          return false;
+        }
+      } else {
+        void* new_default_val = nullptr;
+        if (!TryParse(flag, &new_default_val, value, err)) {
+          return false;
+        }
 
-      default_src_.dynamic_value = new_default_value.release();
-      def_kind_ = FlagDefaultSrcKind::kDynamicValue;
+        default_src_.dynamic_value = new_default_val;
+        def_kind_ = FlagDefaultSrcKind::kDynamicValue;
+      }
 
       if (!modified_) {
         // Need to set both default value *and* current, in this case
@@ -361,4 +397,5 @@ bool FlagImpl::ValidateInputValue(absl::string_view value) const {
 }
 
 }  // namespace flags_internal
+ABSL_NAMESPACE_END
 }  // namespace absl
diff --git a/absl/flags/internal/flag.h b/absl/flags/internal/flag.h
index 947a8cadaf2d..20de406f4615 100644
--- a/absl/flags/internal/flag.h
+++ b/absl/flags/internal/flag.h
@@ -27,6 +27,7 @@
 #include "absl/synchronization/mutex.h"
 
 namespace absl {
+ABSL_NAMESPACE_BEGIN
 namespace flags_internal {
 
 constexpr int64_t AtomicInit() { return 0xababababababababll; }
@@ -156,10 +157,11 @@ class FlagImpl {
         help_(help.source),
         help_source_kind_(help.kind),
         def_kind_(flags_internal::FlagDefaultSrcKind::kGenFunc),
-        default_src_(default_value_gen) {}
+        default_src_(default_value_gen),
+        data_guard_{} {}
 
   // Forces destruction of the Flag's data.
-  void Destroy() const;
+  void Destroy();
 
   // Constant access methods
   std::string Help() const;
@@ -170,9 +172,10 @@ class FlagImpl {
   void Read(const CommandLineFlag& flag, void* dst,
             const flags_internal::FlagOpFn dst_op) const
       ABSL_LOCKS_EXCLUDED(*DataGuard());
-  // Attempts to parse supplied `value` std::string.
-  bool TryParse(const CommandLineFlag& flag, void* dst, absl::string_view value,
-                std::string* err) const
+  // Attempts to parse supplied `value` std::string. If parsing is successful, then
+  // it replaces `dst` with the new value.
+  bool TryParse(const CommandLineFlag& flag, void** dst,
+                absl::string_view value, std::string* err) const
       ABSL_EXCLUSIVE_LOCKS_REQUIRED(*DataGuard());
   template <typename T>
   bool AtomicGet(T* v) const {
@@ -226,8 +229,7 @@ class FlagImpl {
   void Init();
   // Ensures that the lazily initialized data is initialized,
   // and returns pointer to the mutex guarding flags data.
-  absl::Mutex* DataGuard() const ABSL_LOCK_RETURNED(locks_->primary_mu);
-
+  absl::Mutex* DataGuard() const ABSL_LOCK_RETURNED((absl::Mutex*)&data_guard_);
   // Returns heap allocated value of type T initialized with default value.
   std::unique_ptr<void, DynValueDeleter> MakeInitValue() const
       ABSL_EXCLUSIVE_LOCKS_REQUIRED(*DataGuard());
@@ -239,9 +241,12 @@ class FlagImpl {
   // Indicates if help message was supplied as literal or generator func.
   const FlagHelpSrcKind help_source_kind_;
 
-  // Mutable Flag's data. (guarded by DataGuard()).
-  // Indicates that locks_ and cur_ fields have been lazily initialized.
+  // Indicates that the Flag state is initialized.
   std::atomic<bool> inited_{false};
+  // Mutable Flag state (guarded by data_guard_).
+  // Additional bool to protect against multiple concurrent constructions
+  // of `data_guard_`.
+  bool is_data_guard_inited_ = false;
   // Has flag value been modified?
   bool modified_ ABSL_GUARDED_BY(*DataGuard()) = false;
   // Specified on command line.
@@ -261,22 +266,20 @@ class FlagImpl {
   // For some types, a copy of the current value is kept in an atomically
   // accessible field.
   std::atomic<int64_t> atomic_{flags_internal::AtomicInit()};
-  // Mutation callback
-  FlagCallback callback_ = nullptr;
-
-  // Lazily initialized mutexes for this flag value.  We cannot inline a
-  // SpinLock or Mutex here because those have non-constexpr constructors and
-  // so would prevent constant initialization of this type.
-  // TODO(rogeeff): fix it once Mutex has constexpr constructor
-  // The following struct contains the locks in a CommandLineFlag struct.
-  // They are in a separate struct that is lazily allocated to avoid problems
-  // with static initialization and to avoid multiple allocations.
-  struct CommandLineFlagLocks {
-    absl::Mutex primary_mu;   // protects several fields in CommandLineFlag
-    absl::Mutex callback_mu;  // used to serialize callbacks
-  };
 
-  CommandLineFlagLocks* locks_ = nullptr;  // locks, laziliy allocated.
+  struct CallbackData {
+    FlagCallback func;
+    absl::Mutex guard;  // Guard for concurrent callback invocations.
+  };
+  CallbackData* callback_data_ ABSL_GUARDED_BY(*DataGuard()) = nullptr;
+  // This is reserved space for an absl::Mutex to guard flag data. It will be
+  // initialized in FlagImpl::Init via placement new.
+  // We can't use "absl::Mutex data_guard_", since this class is not literal.
+  // We do not want to use "absl::Mutex* data_guard_", since this would require
+  // heap allocation during initialization, which is both slows program startup
+  // and can fail. Using reserved space + placement new allows us to avoid both
+  // problems.
+  alignas(absl::Mutex) mutable char data_guard_[sizeof(absl::Mutex)];
 };
 
 // This is "unspecified" implementation of absl::Flag<T> type.
@@ -354,7 +357,7 @@ class Flag final : public flags_internal::CommandLineFlag {
  private:
   friend class FlagState<T>;
 
-  void Destroy() const override { impl_.Destroy(); }
+  void Destroy() override { impl_.Destroy(); }
 
   void Read(void* dst) const override {
     impl_.Read(*this, dst, &flags_internal::FlagOps<T>);
@@ -414,6 +417,7 @@ T* MakeFromDefaultValue(EmptyBraces) {
 }
 
 }  // namespace flags_internal
+ABSL_NAMESPACE_END
 }  // namespace absl
 
 #endif  // ABSL_FLAGS_INTERNAL_FLAG_H_
diff --git a/absl/flags/internal/parse.h b/absl/flags/internal/parse.h
index fd3aca61f408..e534635b700a 100644
--- a/absl/flags/internal/parse.h
+++ b/absl/flags/internal/parse.h
@@ -27,6 +27,7 @@ ABSL_DECLARE_FLAG(std::vector<std::string>, tryfromenv);
 ABSL_DECLARE_FLAG(std::vector<std::string>, undefok);
 
 namespace absl {
+ABSL_NAMESPACE_BEGIN
 namespace flags_internal {
 
 enum class ArgvListAction { kRemoveParsedArgs, kKeepParsedArgs };
@@ -43,6 +44,7 @@ std::vector<char*> ParseCommandLineImpl(int argc, char* argv[],
                                         OnUndefinedFlag on_undef_flag);
 
 }  // namespace flags_internal
+ABSL_NAMESPACE_END
 }  // namespace absl
 
 #endif  // ABSL_FLAGS_INTERNAL_PARSE_H_
diff --git a/absl/flags/internal/path_util.h b/absl/flags/internal/path_util.h
index 5615c0e6c590..4169637731e5 100644
--- a/absl/flags/internal/path_util.h
+++ b/absl/flags/internal/path_util.h
@@ -20,6 +20,7 @@
 #include "absl/strings/string_view.h"
 
 namespace absl {
+ABSL_NAMESPACE_BEGIN
 namespace flags_internal {
 
 // A portable interface that returns the basename of the filename passed as an
@@ -55,6 +56,7 @@ inline absl::string_view Package(absl::string_view filename) {
 }
 
 }  // namespace flags_internal
+ABSL_NAMESPACE_END
 }  // namespace absl
 
 #endif  // ABSL_FLAGS_INTERNAL_PATH_UTIL_H_
diff --git a/absl/flags/internal/program_name.cc b/absl/flags/internal/program_name.cc
index f0811f144868..df0c3309d37c 100644
--- a/absl/flags/internal/program_name.cc
+++ b/absl/flags/internal/program_name.cc
@@ -21,6 +21,7 @@
 #include "absl/synchronization/mutex.h"
 
 namespace absl {
+ABSL_NAMESPACE_BEGIN
 namespace flags_internal {
 
 ABSL_CONST_INIT static absl::Mutex program_name_guard(absl::kConstInit);
@@ -50,4 +51,5 @@ void SetProgramInvocationName(absl::string_view prog_name_str) {
 }
 
 }  // namespace flags_internal
+ABSL_NAMESPACE_END
 }  // namespace absl
diff --git a/absl/flags/internal/program_name.h b/absl/flags/internal/program_name.h
index 326f24bb6533..317a7c5c9169 100644
--- a/absl/flags/internal/program_name.h
+++ b/absl/flags/internal/program_name.h
@@ -24,6 +24,7 @@
 // Program name
 
 namespace absl {
+ABSL_NAMESPACE_BEGIN
 namespace flags_internal {
 
 // Returns program invocation name or "UNKNOWN" if `SetProgramInvocationName()`
@@ -42,6 +43,7 @@ std::string ShortProgramInvocationName();
 void SetProgramInvocationName(absl::string_view prog_name_str);
 
 }  // namespace flags_internal
+ABSL_NAMESPACE_END
 }  // namespace absl
 
 #endif  // ABSL_FLAGS_INTERNAL_PROGRAM_NAME_H_
diff --git a/absl/flags/internal/registry.cc b/absl/flags/internal/registry.cc
index 52d9f3c716f3..5eae933c3fa3 100644
--- a/absl/flags/internal/registry.cc
+++ b/absl/flags/internal/registry.cc
@@ -30,6 +30,7 @@
 //    set it.
 
 namespace absl {
+ABSL_NAMESPACE_BEGIN
 namespace flags_internal {
 
 // --------------------------------------------------------------------
@@ -281,7 +282,7 @@ class RetiredFlagObj final : public flags_internal::CommandLineFlag {
         op_(ops) {}
 
  private:
-  void Destroy() const override {
+  void Destroy() override {
     // Values are heap allocated for Retired Flags.
     delete this;
   }
@@ -336,4 +337,5 @@ bool IsRetiredFlag(absl::string_view name, bool* type_is_bool) {
 }
 
 }  // namespace flags_internal
+ABSL_NAMESPACE_END
 }  // namespace absl
diff --git a/absl/flags/internal/registry.h b/absl/flags/internal/registry.h
index 1889f3a076dd..d2145a8acfa0 100644
--- a/absl/flags/internal/registry.h
+++ b/absl/flags/internal/registry.h
@@ -27,6 +27,7 @@
 // Global flags registry API.
 
 namespace absl {
+ABSL_NAMESPACE_BEGIN
 namespace flags_internal {
 
 CommandLineFlag* FindCommandLineFlag(absl::string_view name);
@@ -115,6 +116,7 @@ class FlagSaver {
 };
 
 }  // namespace flags_internal
+ABSL_NAMESPACE_END
 }  // namespace absl
 
 #endif  // ABSL_FLAGS_INTERNAL_REGISTRY_H_
diff --git a/absl/flags/internal/type_erased.cc b/absl/flags/internal/type_erased.cc
index a1650fcf71e4..7910db8fd86e 100644
--- a/absl/flags/internal/type_erased.cc
+++ b/absl/flags/internal/type_erased.cc
@@ -21,6 +21,7 @@
 #include "absl/strings/str_cat.h"
 
 namespace absl {
+ABSL_NAMESPACE_BEGIN
 namespace flags_internal {
 
 bool GetCommandLineOption(absl::string_view name, std::string* value) {
@@ -79,4 +80,5 @@ bool SpecifiedOnCommandLine(absl::string_view name) {
 }
 
 }  // namespace flags_internal
+ABSL_NAMESPACE_END
 }  // namespace absl
diff --git a/absl/flags/internal/type_erased.h b/absl/flags/internal/type_erased.h
index a955116628ff..6cbd84cd1996 100644
--- a/absl/flags/internal/type_erased.h
+++ b/absl/flags/internal/type_erased.h
@@ -25,6 +25,7 @@
 // Registry interfaces operating on type erased handles.
 
 namespace absl {
+ABSL_NAMESPACE_BEGIN
 namespace flags_internal {
 
 // If a flag named "name" exists, store its current value in *OUTPUT
@@ -81,6 +82,7 @@ inline bool GetByName(absl::string_view name, T* dst) {
 }
 
 }  // namespace flags_internal
+ABSL_NAMESPACE_END
 }  // namespace absl
 
 #endif  // ABSL_FLAGS_INTERNAL_TYPE_ERASED_H_
diff --git a/absl/flags/internal/usage.cc b/absl/flags/internal/usage.cc
index dc12e32f04cb..4602c0196924 100644
--- a/absl/flags/internal/usage.cc
+++ b/absl/flags/internal/usage.cc
@@ -44,6 +44,7 @@ ABSL_FLAG(std::string, helpmatch, "",
           "show help on modules whose name contains the specified substr");
 
 namespace absl {
+ABSL_NAMESPACE_BEGIN
 namespace flags_internal {
 namespace {
 
@@ -404,4 +405,5 @@ int HandleUsageFlags(std::ostream& out,
 }
 
 }  // namespace flags_internal
+ABSL_NAMESPACE_END
 }  // namespace absl
diff --git a/absl/flags/internal/usage.h b/absl/flags/internal/usage.h
index 76075b0829d9..5e8ca6a1f81f 100644
--- a/absl/flags/internal/usage.h
+++ b/absl/flags/internal/usage.h
@@ -27,6 +27,7 @@
 // Usage reporting interfaces
 
 namespace absl {
+ABSL_NAMESPACE_BEGIN
 namespace flags_internal {
 
 // The format to report the help messages in.
@@ -64,6 +65,7 @@ int HandleUsageFlags(std::ostream& out,
                      absl::string_view program_usage_message);
 
 }  // namespace flags_internal
+ABSL_NAMESPACE_END
 }  // namespace absl
 
 ABSL_DECLARE_FLAG(bool, help);
diff --git a/absl/flags/marshalling.cc b/absl/flags/marshalling.cc
index f4ebe0e34330..87020a2741c8 100644
--- a/absl/flags/marshalling.cc
+++ b/absl/flags/marshalling.cc
@@ -27,6 +27,7 @@
 #include "absl/strings/str_split.h"
 
 namespace absl {
+ABSL_NAMESPACE_BEGIN
 namespace flags_internal {
 
 // --------------------------------------------------------------------
@@ -226,4 +227,5 @@ std::string AbslUnparseFlag(absl::LogSeverity v) {
   return absl::UnparseFlag(static_cast<int>(v));
 }
 
+ABSL_NAMESPACE_END
 }  // namespace absl
diff --git a/absl/flags/marshalling.h b/absl/flags/marshalling.h
index 6d3918043704..b9fca7524026 100644
--- a/absl/flags/marshalling.h
+++ b/absl/flags/marshalling.h
@@ -168,6 +168,7 @@
 #include "absl/strings/string_view.h"
 
 namespace absl {
+ABSL_NAMESPACE_BEGIN
 namespace flags_internal {
 
 // Overloads of `AbslParseFlag()` and `AbslUnparseFlag()` for fundamental types.
@@ -256,6 +257,7 @@ enum class LogSeverity : int;
 bool AbslParseFlag(absl::string_view, absl::LogSeverity*, std::string*);
 std::string AbslUnparseFlag(absl::LogSeverity);
 
+ABSL_NAMESPACE_END
 }  // namespace absl
 
 #endif  // ABSL_FLAGS_MARSHALLING_H_
diff --git a/absl/flags/parse.cc b/absl/flags/parse.cc
index 16c4d68fc270..5a56a35625df 100644
--- a/absl/flags/parse.cc
+++ b/absl/flags/parse.cc
@@ -38,6 +38,7 @@
 // --------------------------------------------------------------------
 
 namespace absl {
+ABSL_NAMESPACE_BEGIN
 namespace flags_internal {
 namespace {
 
@@ -52,6 +53,7 @@ ABSL_CONST_INIT bool tryfromenv_needs_processing
 
 }  // namespace
 }  // namespace flags_internal
+ABSL_NAMESPACE_END
 }  // namespace absl
 
 ABSL_FLAG(std::vector<std::string>, flagfile, {},
@@ -109,6 +111,7 @@ ABSL_FLAG(std::vector<std::string>, undefok, {},
           "with that name");
 
 namespace absl {
+ABSL_NAMESPACE_BEGIN
 namespace flags_internal {
 
 namespace {
@@ -748,4 +751,5 @@ std::vector<char*> ParseCommandLine(int argc, char* argv[]) {
       flags_internal::OnUndefinedFlag::kAbortIfUndefined);
 }
 
+ABSL_NAMESPACE_END
 }  // namespace absl
diff --git a/absl/flags/parse.h b/absl/flags/parse.h
index dbb751015686..871fc993b616 100644
--- a/absl/flags/parse.h
+++ b/absl/flags/parse.h
@@ -29,6 +29,7 @@
 #include "absl/flags/internal/parse.h"
 
 namespace absl {
+ABSL_NAMESPACE_BEGIN
 
 // ParseCommandLine()
 //
@@ -53,6 +54,7 @@ namespace absl {
 // help messages and then exits the program.
 std::vector<char*> ParseCommandLine(int argc, char* argv[]);
 
+ABSL_NAMESPACE_END
 }  // namespace absl
 
 #endif  // ABSL_FLAGS_PARSE_H_
diff --git a/absl/flags/usage.cc b/absl/flags/usage.cc
index dff7a3dab39c..60459bc2a476 100644
--- a/absl/flags/usage.cc
+++ b/absl/flags/usage.cc
@@ -20,6 +20,7 @@
 #include "absl/synchronization/mutex.h"
 
 namespace absl {
+ABSL_NAMESPACE_BEGIN
 namespace flags_internal {
 namespace {
 ABSL_CONST_INIT absl::Mutex usage_message_guard(absl::kConstInit);
@@ -53,4 +54,5 @@ absl::string_view ProgramUsageMessage() {
              : "Warning: SetProgramUsageMessage() never called";
 }
 
+ABSL_NAMESPACE_END
 }  // namespace absl
diff --git a/absl/flags/usage.h b/absl/flags/usage.h
index 3a12107137ad..299e5c3414f5 100644
--- a/absl/flags/usage.h
+++ b/absl/flags/usage.h
@@ -22,6 +22,7 @@
 // Usage reporting interfaces
 
 namespace absl {
+ABSL_NAMESPACE_BEGIN
 
 // Sets the "usage" message to be used by help reporting routines.
 // For example:
@@ -35,6 +36,7 @@ void SetProgramUsageMessage(absl::string_view new_usage_message);
 // Returns the usage message set by SetProgramUsageMessage().
 absl::string_view ProgramUsageMessage();
 
+ABSL_NAMESPACE_END
 }  // namespace absl
 
 #endif  // ABSL_FLAGS_USAGE_H_
diff --git a/absl/flags/usage_config.cc b/absl/flags/usage_config.cc
index f97bf300dde7..21a2dd018b4a 100644
--- a/absl/flags/usage_config.cc
+++ b/absl/flags/usage_config.cc
@@ -34,6 +34,7 @@ ABSL_ATTRIBUTE_WEAK void AbslInternalReportFatalUsageError(absl::string_view) {}
 }  // extern "C"
 
 namespace absl {
+ABSL_NAMESPACE_BEGIN
 namespace flags_internal {
 
 namespace {
@@ -149,4 +150,5 @@ void SetFlagsUsageConfig(FlagsUsageConfig usage_config) {
     flags_internal::custom_usage_config = new FlagsUsageConfig(usage_config);
 }
 
+ABSL_NAMESPACE_END
 }  // namespace absl
diff --git a/absl/flags/usage_config.h b/absl/flags/usage_config.h
index bfd0eedbc4a7..e6428e0a6267 100644
--- a/absl/flags/usage_config.h
+++ b/absl/flags/usage_config.h
@@ -54,6 +54,7 @@
 //     Shows help on modules whose name contains the specified substring
 
 namespace absl {
+ABSL_NAMESPACE_BEGIN
 
 namespace flags_internal {
 using FlagKindFilter = std::function<bool (absl::string_view)>;
@@ -118,6 +119,7 @@ FlagsUsageConfig GetUsageConfig();
 void ReportUsageError(absl::string_view msg, bool is_fatal);
 
 }  // namespace flags_internal
+ABSL_NAMESPACE_END
 }  // namespace absl
 
 extern "C" {