Commit da3a8769 by Abseil Team Committed by Gennadiy Rozental

Export of internal Abseil changes

--
b2b94b9f533e4f9ae2a2df9de56ccb3b18f31d0d by Gennadiy Rozental <rogeeff@google.com>:

Internal change

PiperOrigin-RevId: 314366138

--
7a5ac6be82741aec5b327d7b67efd14d69deed6a by Gennadiy Rozental <rogeeff@google.com>:

Introduce Abseil prefixed dynamic annotation macros.

PiperOrigin-RevId: 314228914

--
33e7c605cb1be9bd48bd3590b1eedccd68d3bd1b by Gennadiy Rozental <rogeeff@google.com>:

Fixing missing includes and remove unnecessary ones.

PiperOrigin-RevId: 314217909

--
6a3c5c26bfa13317bf0a880f13d0e4be0b971b76 by Gennadiy Rozental <rogeeff@google.com>:

Import of CCTZ from GitHub.

PiperOrigin-RevId: 314209576

--
f7795aa68020af4a6b4905531ba951e04b88966a by Abseil Team <absl-team@google.com>:

absl::FormatTime() and absl::ParseTime() format specifiers:
- %EZ now accepts 'z' in addition to 'Z' as a synonym for +00:00.
- %ET is introduced, producing 'T' on output, and accepting 'T'
  or 't' on input.  This is for the RFC3339 date-time separator.

PiperOrigin-RevId: 313945137

--
87c437ce3aab3f59a7546e44a28cd1c8aaa152c3 by Laramie Leavitt <lar@google.com>:

Rollback

PiperOrigin-RevId: 313868206

--
8049b74349486a0026932b86d29c380b195e1cba by Laramie Leavitt <lar@google.com>:

Remove the MockingBitGenBase base class in favor of type-erasure in BitGenRef.

In Abseil random, mocking was split across two different classes,
MockingBitGenBase and MockingBitGen. This split existed because Google Mock is a
test-only library that we don't link into production, so MockingBitGenBase
provided a low-overhead scaffold used to lookup mocks when in test code, but
which is unused in production code.

That has been replaced by type-erasure which looks for a method named
CallImpl with the correct signature.

Weaken the coupling between MockingBitGen, DistributionCaller, and MockOverloadSet.

Rename CallImpl to InvokeMock()

Previously, the implementation of DistributionCaller was also split across different files using explicit instantiation of the DistributionCaller struct and some details in the Mocking classes. Now Distribution caller uses the presence of the InvokeMock() method to choose whether to use the mockable call path or the default call path.

PiperOrigin-RevId: 313848695

--
1741d80e08050e1939605f70ca6ff64809785c85 by Gennadiy Rozental <rogeeff@google.com>:

Introduce public interface to access reflection handle corresponding the flag.

This interface will be the only official way to access flag reflection information from the flag object. The other internal methods will eventually disappear.

PiperOrigin-RevId: 313734006

--
c375bead457de29d9c29595d16c66d3e5125b585 by Abseil Team <absl-team@google.com>:

Improve documentation of absl::c_partial_sort_copy.

This function takes no middle parameter, instead using the size of the result
container to determine the size of the partial sort.

PiperOrigin-RevId: 313656062

--
bbc759d43656b1b996ad558f23c852a9f14129d2 by Gennadiy Rozental <rogeeff@google.com>:

Eliminate dynamic annotation symbols in the library.

PiperOrigin-RevId: 313650817
GitOrigin-RevId: b2b94b9f533e4f9ae2a2df9de56ccb3b18f31d0d
Change-Id: Ic7a11bbcb723f3ff6a7e2f214bff0a92c6f8ab4d
parent 8faf2046
...@@ -15,6 +15,7 @@ set(ABSL_INTERNAL_DLL_FILES ...@@ -15,6 +15,7 @@ set(ABSL_INTERNAL_DLL_FILES
"base/internal/cycleclock.cc" "base/internal/cycleclock.cc"
"base/internal/cycleclock.h" "base/internal/cycleclock.h"
"base/internal/direct_mmap.h" "base/internal/direct_mmap.h"
"base/internal/dynamic_annotations.h"
"base/internal/endian.h" "base/internal/endian.h"
"base/internal/errno_saver.h" "base/internal/errno_saver.h"
"base/internal/exponential_biased.cc" "base/internal/exponential_biased.cc"
......
...@@ -174,5 +174,5 @@ if(ABSL_ENABLE_INSTALL) ...@@ -174,5 +174,5 @@ if(ABSL_ENABLE_INSTALL)
PATTERN "*.h" PATTERN "*.h"
PATTERN "copts" EXCLUDE PATTERN "copts" EXCLUDE
PATTERN "testdata" EXCLUDE PATTERN "testdata" EXCLUDE
) )
endif() # ABSL_ENABLE_INSTALL endif() # ABSL_ENABLE_INSTALL
...@@ -943,9 +943,10 @@ void c_partial_sort( ...@@ -943,9 +943,10 @@ void c_partial_sort(
// c_partial_sort_copy() // c_partial_sort_copy()
// //
// Container-based version of the <algorithm> `std::partial_sort_copy()` // Container-based version of the <algorithm> `std::partial_sort_copy()`
// function to sort elements within a container such that elements before // function to sort the elements in the given range `result` within the larger
// `middle` are sorted in ascending order, and return the result within an // `sequence` in ascending order (and using `result` as the output parameter).
// iterator. // At most min(result.last - result.first, sequence.last - sequence.first)
// elements from the sequence will be stored in the result.
template <typename C, typename RandomAccessContainer> template <typename C, typename RandomAccessContainer>
container_algorithm_internal::ContainerIter<RandomAccessContainer> container_algorithm_internal::ContainerIter<RandomAccessContainer>
c_partial_sort_copy(const C& sequence, RandomAccessContainer& result) { c_partial_sort_copy(const C& sequence, RandomAccessContainer& result) {
......
...@@ -115,10 +115,18 @@ cc_library( ...@@ -115,10 +115,18 @@ cc_library(
cc_library( cc_library(
name = "dynamic_annotations", name = "dynamic_annotations",
srcs = ["dynamic_annotations.cc"], srcs = [
hdrs = ["dynamic_annotations.h"], "dynamic_annotations.cc",
"internal/dynamic_annotations.h",
],
hdrs = [
"dynamic_annotations.h",
],
copts = ABSL_DEFAULT_COPTS, copts = ABSL_DEFAULT_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS, linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
":config",
],
) )
cc_library( cc_library(
......
...@@ -106,8 +106,11 @@ absl_cc_library( ...@@ -106,8 +106,11 @@ absl_cc_library(
"dynamic_annotations.h" "dynamic_annotations.h"
SRCS SRCS
"dynamic_annotations.cc" "dynamic_annotations.cc"
"internal/dynamic_annotations.h"
COPTS COPTS
${ABSL_DEFAULT_COPTS} ${ABSL_DEFAULT_COPTS}
DEPS
absl::config
PUBLIC PUBLIC
) )
......
...@@ -17,72 +17,17 @@ ...@@ -17,72 +17,17 @@
#include "absl/base/dynamic_annotations.h" #include "absl/base/dynamic_annotations.h"
#ifndef __has_feature // Compiler-based ThreadSanitizer defines
#define __has_feature(x) 0 // DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL = 1
#endif // and provides its own definitions of the functions.
/* Compiler-based ThreadSanitizer defines
DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL = 1
and provides its own definitions of the functions. */
#ifndef DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL #ifndef DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL
# define DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL 0 # define DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL 0
#endif #endif
/* Each function is empty and called (via a macro) only in debug mode.
The arguments are captured by dynamic tools at runtime. */
#if DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL == 0 && !defined(__native_client__) #if DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL == 0 && !defined(__native_client__)
#if __has_feature(memory_sanitizer)
#include <sanitizer/msan_interface.h>
#endif
#ifdef __cplusplus
extern "C" { extern "C" {
#endif
void AnnotateRWLockCreate(const char *, int,
const volatile void *){}
void AnnotateRWLockDestroy(const char *, int,
const volatile void *){}
void AnnotateRWLockAcquired(const char *, int,
const volatile void *, long){}
void AnnotateRWLockReleased(const char *, int,
const volatile void *, long){}
void AnnotateBenignRace(const char *, int,
const volatile void *,
const char *){}
void AnnotateBenignRaceSized(const char *, int,
const volatile void *,
size_t,
const char *) {}
void AnnotateThreadName(const char *, int,
const char *){}
void AnnotateIgnoreReadsBegin(const char *, int){}
void AnnotateIgnoreReadsEnd(const char *, int){}
void AnnotateIgnoreWritesBegin(const char *, int){}
void AnnotateIgnoreWritesEnd(const char *, int){}
void AnnotateEnableRaceDetection(const char *, int, int){}
void AnnotateMemoryIsInitialized(const char *, int,
const volatile void *mem, size_t size) {
#if __has_feature(memory_sanitizer)
__msan_unpoison(mem, size);
#else
(void)mem;
(void)size;
#endif
}
void AnnotateMemoryIsUninitialized(const char *, int,
const volatile void *mem, size_t size) {
#if __has_feature(memory_sanitizer)
__msan_allocated_memory(mem, size);
#else
(void)mem;
(void)size;
#endif
}
static int GetRunningOnValgrind(void) { static int GetRunningOnValgrind(void) {
#ifdef RUNNING_ON_VALGRIND #ifdef RUNNING_ON_VALGRIND
...@@ -95,21 +40,21 @@ static int GetRunningOnValgrind(void) { ...@@ -95,21 +40,21 @@ static int GetRunningOnValgrind(void) {
return 0; return 0;
} }
/* See the comments in dynamic_annotations.h */ // See the comments in dynamic_annotations.h
int RunningOnValgrind(void) { int RunningOnValgrind(void) {
static volatile int running_on_valgrind = -1; static volatile int running_on_valgrind = -1;
int local_running_on_valgrind = running_on_valgrind; int local_running_on_valgrind = running_on_valgrind;
/* C doesn't have thread-safe initialization of statics, and we // C doesn't have thread-safe initialization of statics, and we
don't want to depend on pthread_once here, so hack it. */ // don't want to depend on pthread_once here, so hack it.
ANNOTATE_BENIGN_RACE(&running_on_valgrind, "safe hack"); ANNOTATE_BENIGN_RACE(&running_on_valgrind, "safe hack");
if (local_running_on_valgrind == -1) if (local_running_on_valgrind == -1)
running_on_valgrind = local_running_on_valgrind = GetRunningOnValgrind(); running_on_valgrind = local_running_on_valgrind = GetRunningOnValgrind();
return local_running_on_valgrind; return local_running_on_valgrind;
} }
/* See the comments in dynamic_annotations.h */ // See the comments in dynamic_annotations.h
double ValgrindSlowdown(void) { double ValgrindSlowdown(void) {
/* Same initialization hack as in RunningOnValgrind(). */ // Same initialization hack as in RunningOnValgrind().
static volatile double slowdown = 0.0; static volatile double slowdown = 0.0;
double local_slowdown = slowdown; double local_slowdown = slowdown;
ANNOTATE_BENIGN_RACE(&slowdown, "safe hack"); ANNOTATE_BENIGN_RACE(&slowdown, "safe hack");
...@@ -123,7 +68,5 @@ double ValgrindSlowdown(void) { ...@@ -123,7 +68,5 @@ double ValgrindSlowdown(void) {
return local_slowdown; return local_slowdown;
} }
#ifdef __cplusplus
} // extern "C" } // extern "C"
#endif #endif // DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL == 0
#endif /* DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL == 0 */
/* // Copyright 2017 The Abseil Authors.
* Copyright 2017 The Abseil Authors. //
* // Licensed under the Apache License, Version 2.0 (the "License");
* Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License.
* you may not use this file except in compliance with the License. // You may obtain a copy of the License at
* You may obtain a copy of the License at //
* // https://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0 //
* // Unless required by applicable law or agreed to in writing, software
* Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS,
* distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and
* See the License for the specific language governing permissions and // limitations under the License.
* limitations under the License.
*/ // This file defines dynamic annotations for use with dynamic analysis tool
/* This file defines dynamic annotations for use with dynamic analysis // such as valgrind, PIN, etc.
tool such as valgrind, PIN, etc. //
// Dynamic annotation is a source code annotation that affects the generated
Dynamic annotation is a source code annotation that affects // code (that is, the annotation is not a comment). Each such annotation is
the generated code (that is, the annotation is not a comment). // attached to a particular instruction and/or to a particular object (address)
Each such annotation is attached to a particular // in the program.
instruction and/or to a particular object (address) in the program. //
// The annotations that should be used by users are macros in all upper-case
The annotations that should be used by users are macros in all upper-case // (e.g., ABSL_ANNOTATE_THREAD_NAME).
(e.g., ANNOTATE_THREAD_NAME). //
// Actual implementation of these macros may differ depending on the dynamic
Actual implementation of these macros may differ depending on the // analysis tool being used.
dynamic analysis tool being used. //
// This file supports the following configurations:
This file supports the following configurations: // - Dynamic Annotations enabled (with static thread-safety warnings disabled).
- Dynamic Annotations enabled (with static thread-safety warnings disabled). // In this case, macros expand to functions implemented by Thread Sanitizer,
In this case, macros expand to functions implemented by Thread Sanitizer, // when building with TSan. When not provided an external implementation,
when building with TSan. When not provided an external implementation, // dynamic_annotations.cc provides no-op implementations.
dynamic_annotations.cc provides no-op implementations. //
// - Static Clang thread-safety warnings enabled.
- Static Clang thread-safety warnings enabled. // When building with a Clang compiler that supports thread-safety warnings,
When building with a Clang compiler that supports thread-safety warnings, // a subset of annotations can be statically-checked at compile-time. We
a subset of annotations can be statically-checked at compile-time. We // expand these macros to static-inline functions that can be analyzed for
expand these macros to static-inline functions that can be analyzed for // thread-safety, but afterwards elided when building the final binary.
thread-safety, but afterwards elided when building the final binary. //
// - All annotations are disabled.
- All annotations are disabled. // If neither Dynamic Annotations nor Clang thread-safety warnings are
If neither Dynamic Annotations nor Clang thread-safety warnings are // enabled, then all annotation-macros expand to empty.
enabled, then all annotation-macros expand to empty. */
#ifndef ABSL_BASE_DYNAMIC_ANNOTATIONS_H_ #ifndef ABSL_BASE_DYNAMIC_ANNOTATIONS_H_
#define ABSL_BASE_DYNAMIC_ANNOTATIONS_H_ #define ABSL_BASE_DYNAMIC_ANNOTATIONS_H_
#include <stddef.h>
#include "absl/base/config.h"
// TODO(rogeeff): Remove after the backward compatibility period.
#include "absl/base/internal/dynamic_annotations.h" // IWYU pragma: export
// -------------------------------------------------------------------------
// Decide which features are enabled
#ifndef DYNAMIC_ANNOTATIONS_ENABLED #ifndef DYNAMIC_ANNOTATIONS_ENABLED
# define DYNAMIC_ANNOTATIONS_ENABLED 0 #define DYNAMIC_ANNOTATIONS_ENABLED 0
#endif
#if defined(__clang__) && !defined(SWIG)
#define ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED 1
#else
#define ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED 0
#endif #endif
#if DYNAMIC_ANNOTATIONS_ENABLED != 0 #if DYNAMIC_ANNOTATIONS_ENABLED != 0
/* ------------------------------------------------------------- #define ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED 1
Annotations that suppress errors. It is usually better to express the #define ABSL_INTERNAL_READS_ANNOTATIONS_ENABLED 1
program's synchronization using the other annotations, but these can #define ABSL_INTERNAL_WRITES_ANNOTATIONS_ENABLED 1
be used when all else fails. */ #define ABSL_INTERNAL_ANNOTALYSIS_ENABLED 0
#define ABSL_INTERNAL_READS_WRITES_ANNOTATIONS_ENABLED 1
/* Report that we may have a benign race at "pointer", with size
"sizeof(*(pointer))". "pointer" must be a non-void* pointer. Insert at the #else
point where "pointer" has been allocated, preferably close to the point
where the race happens. See also ANNOTATE_BENIGN_RACE_STATIC. */ #define ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED 0
#define ANNOTATE_BENIGN_RACE(pointer, description) \ #define ABSL_INTERNAL_READS_ANNOTATIONS_ENABLED 0
AnnotateBenignRaceSized(__FILE__, __LINE__, pointer, \ #define ABSL_INTERNAL_WRITES_ANNOTATIONS_ENABLED 0
sizeof(*(pointer)), description)
// Clang provides limited support for static thread-safety analysis through a
/* Same as ANNOTATE_BENIGN_RACE(address, description), but applies to // feature called Annotalysis. We configure macro-definitions according to
the memory range [address, address+size). */ // whether Annotalysis support is available. When running in opt-mode, GCC
#define ANNOTATE_BENIGN_RACE_SIZED(address, size, description) \ // will issue a warning, if these attributes are compiled. Only include them
AnnotateBenignRaceSized(__FILE__, __LINE__, address, size, description) // when compiling using Clang.
/* Enable (enable!=0) or disable (enable==0) race detection for all threads. // ANNOTALYSIS_ENABLED == 1 when IGNORE_READ_ATTRIBUTE_ENABLED == 1
This annotation could be useful if you want to skip expensive race analysis #define ABSL_INTERNAL_ANNOTALYSIS_ENABLED \
during some period of program execution, e.g. during initialization. */ ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED
#define ANNOTATE_ENABLE_RACE_DETECTION(enable) \ // Read/write annotations are enabled in Annotalysis mode; disabled otherwise.
AnnotateEnableRaceDetection(__FILE__, __LINE__, enable) #define ABSL_INTERNAL_READS_WRITES_ANNOTATIONS_ENABLED \
ABSL_INTERNAL_ANNOTALYSIS_ENABLED
/* ------------------------------------------------------------- #endif
Annotations useful for debugging. */
// Memory annotations are also made available to LLVM's Memory Sanitizer
/* Report the current thread name to a race detector. */ #if defined(MEMORY_SANITIZER) && defined(__has_feature) && \
#define ANNOTATE_THREAD_NAME(name) \ !defined(__native_client__)
AnnotateThreadName(__FILE__, __LINE__, name) #if __has_feature(memory_sanitizer)
#define ABSL_INTERNAL_MEMORY_ANNOTATIONS_ENABLED 1
/* ------------------------------------------------------------- #endif
Annotations useful when implementing locks. They are not #endif
normally needed by modules that merely use locks.
The "lock" argument is a pointer to the lock object. */ #ifndef ABSL_INTERNAL_MEMORY_ANNOTATIONS_ENABLED
#define ABSL_INTERNAL_MEMORY_ANNOTATIONS_ENABLED 0
/* Report that a lock has been created at address "lock". */ #endif
#define ANNOTATE_RWLOCK_CREATE(lock) \
AnnotateRWLockCreate(__FILE__, __LINE__, lock) #ifdef __cplusplus
#define ABSL_INTERNAL_BEGIN_EXTERN_C extern "C" {
/* Report that a linker initialized lock has been created at address "lock". #define ABSL_INTERNAL_END_EXTERN_C } // extern "C"
*/ #define ABSL_INTERNAL_GLOBAL_SCOPED(F) ::F
#ifdef THREAD_SANITIZER #define ABSL_INTERNAL_STATIC_INLINE inline
#define ANNOTATE_RWLOCK_CREATE_STATIC(lock) \
AnnotateRWLockCreateStatic(__FILE__, __LINE__, lock)
#else #else
#define ANNOTATE_RWLOCK_CREATE_STATIC(lock) ANNOTATE_RWLOCK_CREATE(lock) #define ABSL_INTERNAL_BEGIN_EXTERN_C // empty
#define ABSL_INTERNAL_END_EXTERN_C // empty
#define ABSL_INTERNAL_GLOBAL_SCOPED(F) F
#define ABSL_INTERNAL_STATIC_INLINE static inline
#endif #endif
/* Report that the lock at address "lock" is about to be destroyed. */ // -------------------------------------------------------------------------
#define ANNOTATE_RWLOCK_DESTROY(lock) \ // Define race annotations.
AnnotateRWLockDestroy(__FILE__, __LINE__, lock)
#if ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED == 1
// -------------------------------------------------------------
// Annotations that suppress errors. It is usually better to express the
// program's synchronization using the other annotations, but these can be used
// when all else fails.
// Report that we may have a benign race at `pointer`, with size
// "sizeof(*(pointer))". `pointer` must be a non-void* pointer. Insert at the
// point where `pointer` has been allocated, preferably close to the point
// where the race happens. See also ABSL_ANNOTATE_BENIGN_RACE_STATIC.
#define ABSL_ANNOTATE_BENIGN_RACE(pointer, description) \
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateBenignRaceSized) \
(__FILE__, __LINE__, pointer, sizeof(*(pointer)), description)
// Same as ABSL_ANNOTATE_BENIGN_RACE(`address`, `description`), but applies to
// the memory range [`address`, `address`+`size`).
#define ABSL_ANNOTATE_BENIGN_RACE_SIZED(address, size, description) \
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateBenignRaceSized) \
(__FILE__, __LINE__, address, size, description)
// Enable (`enable`!=0) or disable (`enable`==0) race detection for all threads.
// This annotation could be useful if you want to skip expensive race analysis
// during some period of program execution, e.g. during initialization.
#define ABSL_ANNOTATE_ENABLE_RACE_DETECTION(enable) \
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateEnableRaceDetection) \
(__FILE__, __LINE__, enable)
// -------------------------------------------------------------
// Annotations useful for debugging.
// Report the current thread `name` to a race detector.
#define ABSL_ANNOTATE_THREAD_NAME(name) \
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateThreadName)(__FILE__, __LINE__, name)
// -------------------------------------------------------------
// Annotations useful when implementing locks. They are not normally needed by
// modules that merely use locks. The `lock` argument is a pointer to the lock
// object.
// Report that a lock has been created at address `lock`.
#define ABSL_ANNOTATE_RWLOCK_CREATE(lock) \
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockCreate)(__FILE__, __LINE__, lock)
// Report that a linker initialized lock has been created at address `lock`.
#ifdef THREAD_SANITIZER
#define ABSL_ANNOTATE_RWLOCK_CREATE_STATIC(lock) \
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockCreateStatic) \
(__FILE__, __LINE__, lock)
#else
#define ABSL_ANNOTATE_RWLOCK_CREATE_STATIC(lock) \
ABSL_ANNOTATE_RWLOCK_CREATE(lock)
#endif
/* Report that the lock at address "lock" has been acquired. // Report that the lock at address `lock` is about to be destroyed.
is_w=1 for writer lock, is_w=0 for reader lock. */ #define ABSL_ANNOTATE_RWLOCK_DESTROY(lock) \
#define ANNOTATE_RWLOCK_ACQUIRED(lock, is_w) \ ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockDestroy)(__FILE__, __LINE__, lock)
AnnotateRWLockAcquired(__FILE__, __LINE__, lock, is_w)
// Report that the lock at address `lock` has been acquired.
// `is_w`=1 for writer lock, `is_w`=0 for reader lock.
#define ABSL_ANNOTATE_RWLOCK_ACQUIRED(lock, is_w) \
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockAcquired) \
(__FILE__, __LINE__, lock, is_w)
// Report that the lock at address `lock` is about to be released.
// `is_w`=1 for writer lock, `is_w`=0 for reader lock.
#define ABSL_ANNOTATE_RWLOCK_RELEASED(lock, is_w) \
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockReleased) \
(__FILE__, __LINE__, lock, is_w)
// Apply ABSL_ANNOTATE_BENIGN_RACE_SIZED to a static variable `static_var`.
#define ABSL_ANNOTATE_BENIGN_RACE_STATIC(static_var, description) \
namespace { \
class static_var##_annotator { \
public: \
static_var##_annotator() { \
ABSL_ANNOTATE_BENIGN_RACE_SIZED(&static_var, sizeof(static_var), \
#static_var ": " description); \
} \
}; \
static static_var##_annotator the##static_var##_annotator; \
} // namespace
// Function prototypes of annotations provided by the compiler-based sanitizer
// implementation.
ABSL_INTERNAL_BEGIN_EXTERN_C
void AnnotateRWLockCreate(const char* file, int line,
const volatile void* lock);
void AnnotateRWLockCreateStatic(const char* file, int line,
const volatile void* lock);
void AnnotateRWLockDestroy(const char* file, int line,
const volatile void* lock);
void AnnotateRWLockAcquired(const char* file, int line,
const volatile void* lock, long is_w); // NOLINT
void AnnotateRWLockReleased(const char* file, int line,
const volatile void* lock, long is_w); // NOLINT
void AnnotateBenignRace(const char* file, int line,
const volatile void* address, const char* description);
void AnnotateBenignRaceSized(const char* file, int line,
const volatile void* address, size_t size,
const char* description);
void AnnotateThreadName(const char* file, int line, const char* name);
void AnnotateEnableRaceDetection(const char* file, int line, int enable);
ABSL_INTERNAL_END_EXTERN_C
#else // ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED == 0
#define ABSL_ANNOTATE_RWLOCK_CREATE(lock) // empty
#define ABSL_ANNOTATE_RWLOCK_CREATE_STATIC(lock) // empty
#define ABSL_ANNOTATE_RWLOCK_DESTROY(lock) // empty
#define ABSL_ANNOTATE_RWLOCK_ACQUIRED(lock, is_w) // empty
#define ABSL_ANNOTATE_RWLOCK_RELEASED(lock, is_w) // empty
#define ABSL_ANNOTATE_BENIGN_RACE(address, description) // empty
#define ABSL_ANNOTATE_BENIGN_RACE_SIZED(address, size, description) // empty
#define ABSL_ANNOTATE_THREAD_NAME(name) // empty
#define ABSL_ANNOTATE_ENABLE_RACE_DETECTION(enable) // empty
#define ABSL_ANNOTATE_BENIGN_RACE_STATIC(static_var, description) // empty
#endif // ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED
// -------------------------------------------------------------------------
// Define memory annotations.
#if ABSL_INTERNAL_MEMORY_ANNOTATIONS_ENABLED == 1
#include <sanitizer/msan_interface.h>
#define ABSL_ANNOTATE_MEMORY_IS_INITIALIZED(address, size) \
__msan_unpoison(address, size)
#define ABSL_ANNOTATE_MEMORY_IS_UNINITIALIZED(address, size) \
__msan_allocated_memory(address, size)
#else // ABSL_INTERNAL_MEMORY_ANNOTATIONS_ENABLED == 0
#if DYNAMIC_ANNOTATIONS_ENABLED == 1
#define ABSL_ANNOTATE_MEMORY_IS_INITIALIZED(address, size) \
do { \
(void)(address); \
(void)(size); \
} while (0)
#define ABSL_ANNOTATE_MEMORY_IS_UNINITIALIZED(address, size) \
do { \
(void)(address); \
(void)(size); \
} while (0)
#else
/* Report that the lock at address "lock" is about to be released. */ #define ABSL_ANNOTATE_MEMORY_IS_INITIALIZED(address, size) // empty
#define ANNOTATE_RWLOCK_RELEASED(lock, is_w) \ #define ABSL_ANNOTATE_MEMORY_IS_UNINITIALIZED(address, size) // empty
AnnotateRWLockReleased(__FILE__, __LINE__, lock, is_w)
#else /* DYNAMIC_ANNOTATIONS_ENABLED == 0 */ #endif
#define ANNOTATE_RWLOCK_CREATE(lock) /* empty */ #endif // ABSL_INTERNAL_MEMORY_ANNOTATIONS_ENABLED
#define ANNOTATE_RWLOCK_CREATE_STATIC(lock) /* empty */
#define ANNOTATE_RWLOCK_DESTROY(lock) /* empty */
#define ANNOTATE_RWLOCK_ACQUIRED(lock, is_w) /* empty */
#define ANNOTATE_RWLOCK_RELEASED(lock, is_w) /* empty */
#define ANNOTATE_BENIGN_RACE(address, description) /* empty */
#define ANNOTATE_BENIGN_RACE_SIZED(address, size, description) /* empty */
#define ANNOTATE_THREAD_NAME(name) /* empty */
#define ANNOTATE_ENABLE_RACE_DETECTION(enable) /* empty */
#endif /* DYNAMIC_ANNOTATIONS_ENABLED */ // -------------------------------------------------------------------------
// Define IGNORE_READS_BEGIN/_END attributes.
/* These annotations are also made available to LLVM's Memory Sanitizer */ #if ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED == 1
#if DYNAMIC_ANNOTATIONS_ENABLED == 1 || defined(MEMORY_SANITIZER)
#define ANNOTATE_MEMORY_IS_INITIALIZED(address, size) \
AnnotateMemoryIsInitialized(__FILE__, __LINE__, address, size)
#define ANNOTATE_MEMORY_IS_UNINITIALIZED(address, size) \ #define ABSL_INTERNAL_IGNORE_READS_BEGIN_ATTRIBUTE \
AnnotateMemoryIsUninitialized(__FILE__, __LINE__, address, size) __attribute((exclusive_lock_function("*")))
#else #define ABSL_INTERNAL_IGNORE_READS_END_ATTRIBUTE \
#define ANNOTATE_MEMORY_IS_INITIALIZED(address, size) /* empty */ __attribute((unlock_function("*")))
#define ANNOTATE_MEMORY_IS_UNINITIALIZED(address, size) /* empty */
#endif /* DYNAMIC_ANNOTATIONS_ENABLED || MEMORY_SANITIZER */
#if defined(__clang__) && !defined(SWIG) #else // ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED == 0
#if DYNAMIC_ANNOTATIONS_ENABLED == 0 #define ABSL_INTERNAL_IGNORE_READS_BEGIN_ATTRIBUTE // empty
#define ANNOTALYSIS_ENABLED #define ABSL_INTERNAL_IGNORE_READS_END_ATTRIBUTE // empty
#endif
/* When running in opt-mode, GCC will issue a warning, if these attributes are #endif // ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED
compiled. Only include them when compiling using Clang. */
#define ATTRIBUTE_IGNORE_READS_BEGIN \
__attribute((exclusive_lock_function("*")))
#define ATTRIBUTE_IGNORE_READS_END \
__attribute((unlock_function("*")))
#else
#define ATTRIBUTE_IGNORE_READS_BEGIN /* empty */
#define ATTRIBUTE_IGNORE_READS_END /* empty */
#endif /* defined(__clang__) && ... */
#if (DYNAMIC_ANNOTATIONS_ENABLED != 0) || defined(ANNOTALYSIS_ENABLED) // -------------------------------------------------------------------------
#define ANNOTATIONS_ENABLED // Define IGNORE_READS_BEGIN/_END annotations.
#endif
#if (DYNAMIC_ANNOTATIONS_ENABLED != 0) #if ABSL_INTERNAL_READS_ANNOTATIONS_ENABLED == 1
/* Request the analysis tool to ignore all reads in the current thread // Request the analysis tool to ignore all reads in the current thread until
until ANNOTATE_IGNORE_READS_END is called. // ABSL_ANNOTATE_IGNORE_READS_END is called. Useful to ignore intentional racey
Useful to ignore intentional racey reads, while still checking // reads, while still checking other reads and all writes.
other reads and all writes. // See also ABSL_ANNOTATE_UNPROTECTED_READ.
See also ANNOTATE_UNPROTECTED_READ. */ #define ABSL_ANNOTATE_IGNORE_READS_BEGIN() \
#define ANNOTATE_IGNORE_READS_BEGIN() \ ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateIgnoreReadsBegin)(__FILE__, __LINE__)
AnnotateIgnoreReadsBegin(__FILE__, __LINE__)
/* Stop ignoring reads. */ // Stop ignoring reads.
#define ANNOTATE_IGNORE_READS_END() \ #define ABSL_ANNOTATE_IGNORE_READS_END() \
AnnotateIgnoreReadsEnd(__FILE__, __LINE__) ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateIgnoreReadsEnd)(__FILE__, __LINE__)
/* Similar to ANNOTATE_IGNORE_READS_BEGIN, but ignore writes instead. */ // Function prototypes of annotations provided by the compiler-based sanitizer
#define ANNOTATE_IGNORE_WRITES_BEGIN() \ // implementation.
AnnotateIgnoreWritesBegin(__FILE__, __LINE__) ABSL_INTERNAL_BEGIN_EXTERN_C
void AnnotateIgnoreReadsBegin(const char* file, int line)
ABSL_INTERNAL_IGNORE_READS_BEGIN_ATTRIBUTE;
void AnnotateIgnoreReadsEnd(const char* file,
int line) ABSL_INTERNAL_IGNORE_READS_END_ATTRIBUTE;
ABSL_INTERNAL_END_EXTERN_C
/* Stop ignoring writes. */ #elif defined(ABSL_INTERNAL_ANNOTALYSIS_ENABLED)
#define ANNOTATE_IGNORE_WRITES_END() \
AnnotateIgnoreWritesEnd(__FILE__, __LINE__)
/* Clang provides limited support for static thread-safety analysis // When Annotalysis is enabled without Dynamic Annotations, the use of
through a feature called Annotalysis. We configure macro-definitions // static-inline functions allows the annotations to be read at compile-time,
according to whether Annotalysis support is available. */ // while still letting the compiler elide the functions from the final build.
#elif defined(ANNOTALYSIS_ENABLED) //
// TODO(delesley) -- The exclusive lock here ignores writes as well, but
// allows IGNORE_READS_AND_WRITES to work properly.
#define ANNOTATE_IGNORE_READS_BEGIN() \ #define ABSL_ANNOTATE_IGNORE_READS_BEGIN() \
StaticAnnotateIgnoreReadsBegin(__FILE__, __LINE__) ABSL_INTERNAL_GLOBAL_SCOPED(AbslInternalAnnotateIgnoreReadsBegin)()
#define ANNOTATE_IGNORE_READS_END() \ #define ABSL_ANNOTATE_IGNORE_READS_END() \
StaticAnnotateIgnoreReadsEnd(__FILE__, __LINE__) ABSL_INTERNAL_GLOBAL_SCOPED(AbslInternalAnnotateIgnoreReadsEnd)()
#define ANNOTATE_IGNORE_WRITES_BEGIN() \ ABSL_INTERNAL_STATIC_INLINE void AbslInternalAnnotateIgnoreReadsBegin()
StaticAnnotateIgnoreWritesBegin(__FILE__, __LINE__) ABSL_INTERNAL_IGNORE_READS_BEGIN_ATTRIBUTE {}
#define ANNOTATE_IGNORE_WRITES_END() \ ABSL_INTERNAL_STATIC_INLINE void AbslInternalAnnotateIgnoreReadsEnd()
StaticAnnotateIgnoreWritesEnd(__FILE__, __LINE__) ABSL_INTERNAL_IGNORE_READS_END_ATTRIBUTE {}
#else #else
#define ANNOTATE_IGNORE_READS_BEGIN() /* empty */
#define ANNOTATE_IGNORE_READS_END() /* empty */ #define ABSL_ANNOTATE_IGNORE_READS_BEGIN() // empty
#define ANNOTATE_IGNORE_WRITES_BEGIN() /* empty */ #define ABSL_ANNOTATE_IGNORE_READS_END() // empty
#define ANNOTATE_IGNORE_WRITES_END() /* empty */
#endif #endif
/* Implement the ANNOTATE_IGNORE_READS_AND_WRITES_* annotations using the more // -------------------------------------------------------------------------
primitive annotations defined above. */ // Define IGNORE_WRITES_BEGIN/_END annotations.
#if defined(ANNOTATIONS_ENABLED)
#if ABSL_INTERNAL_WRITES_ANNOTATIONS_ENABLED == 1
// Similar to ABSL_ANNOTATE_IGNORE_READS_BEGIN, but ignore writes instead.
#define ABSL_ANNOTATE_IGNORE_WRITES_BEGIN() \
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateIgnoreWritesBegin)(__FILE__, __LINE__)
/* Start ignoring all memory accesses (both reads and writes). */ // Stop ignoring writes.
#define ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN() \ #define ABSL_ANNOTATE_IGNORE_WRITES_END() \
do { \ ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateIgnoreWritesEnd)(__FILE__, __LINE__)
ANNOTATE_IGNORE_READS_BEGIN(); \
ANNOTATE_IGNORE_WRITES_BEGIN(); \
}while (0)
/* Stop ignoring both reads and writes. */ // Function prototypes of annotations provided by the compiler-based sanitizer
#define ANNOTATE_IGNORE_READS_AND_WRITES_END() \ // implementation.
do { \ ABSL_INTERNAL_BEGIN_EXTERN_C
ANNOTATE_IGNORE_WRITES_END(); \ void AnnotateIgnoreWritesBegin(const char* file, int line);
ANNOTATE_IGNORE_READS_END(); \ void AnnotateIgnoreWritesEnd(const char* file, int line);
}while (0) ABSL_INTERNAL_END_EXTERN_C
#else #else
#define ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN() /* empty */
#define ANNOTATE_IGNORE_READS_AND_WRITES_END() /* empty */
#endif
/* Use the macros above rather than using these functions directly. */ #define ABSL_ANNOTATE_IGNORE_WRITES_BEGIN() // empty
#include <stddef.h> #define ABSL_ANNOTATE_IGNORE_WRITES_END() // empty
#ifdef __cplusplus
extern "C" {
#endif
void AnnotateRWLockCreate(const char *file, int line,
const volatile void *lock);
void AnnotateRWLockCreateStatic(const char *file, int line,
const volatile void *lock);
void AnnotateRWLockDestroy(const char *file, int line,
const volatile void *lock);
void AnnotateRWLockAcquired(const char *file, int line,
const volatile void *lock, long is_w); /* NOLINT */
void AnnotateRWLockReleased(const char *file, int line,
const volatile void *lock, long is_w); /* NOLINT */
void AnnotateBenignRace(const char *file, int line,
const volatile void *address,
const char *description);
void AnnotateBenignRaceSized(const char *file, int line,
const volatile void *address,
size_t size,
const char *description);
void AnnotateThreadName(const char *file, int line,
const char *name);
void AnnotateEnableRaceDetection(const char *file, int line, int enable);
void AnnotateMemoryIsInitialized(const char *file, int line,
const volatile void *mem, size_t size);
void AnnotateMemoryIsUninitialized(const char *file, int line,
const volatile void *mem, size_t size);
/* Annotations expand to these functions, when Dynamic Annotations are enabled.
These functions are either implemented as no-op calls, if no Sanitizer is
attached, or provided with externally-linked implementations by a library
like ThreadSanitizer. */
void AnnotateIgnoreReadsBegin(const char *file, int line)
ATTRIBUTE_IGNORE_READS_BEGIN;
void AnnotateIgnoreReadsEnd(const char *file, int line)
ATTRIBUTE_IGNORE_READS_END;
void AnnotateIgnoreWritesBegin(const char *file, int line);
void AnnotateIgnoreWritesEnd(const char *file, int line);
#if defined(ANNOTALYSIS_ENABLED)
/* When Annotalysis is enabled without Dynamic Annotations, the use of
static-inline functions allows the annotations to be read at compile-time,
while still letting the compiler elide the functions from the final build.
TODO(delesley) -- The exclusive lock here ignores writes as well, but
allows IGNORE_READS_AND_WRITES to work properly. */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
static inline void StaticAnnotateIgnoreReadsBegin(const char *file, int line)
ATTRIBUTE_IGNORE_READS_BEGIN { (void)file; (void)line; }
static inline void StaticAnnotateIgnoreReadsEnd(const char *file, int line)
ATTRIBUTE_IGNORE_READS_END { (void)file; (void)line; }
static inline void StaticAnnotateIgnoreWritesBegin(
const char *file, int line) { (void)file; (void)line; }
static inline void StaticAnnotateIgnoreWritesEnd(
const char *file, int line) { (void)file; (void)line; }
#pragma GCC diagnostic pop
#endif
/* Return non-zero value if running under valgrind. #endif
If "valgrind.h" is included into dynamic_annotations.cc,
the regular valgrind mechanism will be used.
See http://valgrind.org/docs/manual/manual-core-adv.html about
RUNNING_ON_VALGRIND and other valgrind "client requests".
The file "valgrind.h" may be obtained by doing
svn co svn://svn.valgrind.org/valgrind/trunk/include
If for some reason you can't use "valgrind.h" or want to fake valgrind,
there are two ways to make this function return non-zero:
- Use environment variable: export RUNNING_ON_VALGRIND=1
- Make your tool intercept the function RunningOnValgrind() and
change its return value.
*/
int RunningOnValgrind(void);
/* ValgrindSlowdown returns: // -------------------------------------------------------------------------
* 1.0, if (RunningOnValgrind() == 0) // Define the ABSL_ANNOTATE_IGNORE_READS_AND_WRITES_* annotations using the more
* 50.0, if (RunningOnValgrind() != 0 && getenv("VALGRIND_SLOWDOWN") == NULL) // primitive annotations defined above.
* atof(getenv("VALGRIND_SLOWDOWN")) otherwise //
This function can be used to scale timeout values: // Instead of doing
EXAMPLE: // ABSL_ANNOTATE_IGNORE_READS_BEGIN();
for (;;) { // ... = x;
DoExpensiveBackgroundTask(); // ABSL_ANNOTATE_IGNORE_READS_END();
SleepForSeconds(5 * ValgrindSlowdown()); // one can use
} // ... = ABSL_ANNOTATE_UNPROTECTED_READ(x);
*/
double ValgrindSlowdown(void); #if defined(ABSL_INTERNAL_READS_WRITES_ANNOTATIONS_ENABLED)
// Start ignoring all memory accesses (both reads and writes).
#define ABSL_ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN() \
do { \
ABSL_ANNOTATE_IGNORE_READS_BEGIN(); \
ABSL_ANNOTATE_IGNORE_WRITES_BEGIN(); \
} while (0)
// Stop ignoring both reads and writes.
#define ABSL_ANNOTATE_IGNORE_READS_AND_WRITES_END() \
do { \
ABSL_ANNOTATE_IGNORE_WRITES_END(); \
ABSL_ANNOTATE_IGNORE_READS_END(); \
} while (0)
#ifdef __cplusplus #ifdef __cplusplus
} // ABSL_ANNOTATE_UNPROTECTED_READ is the preferred way to annotate racey reads.
#endif #define ABSL_ANNOTATE_UNPROTECTED_READ(x) \
absl::base_internal::AnnotateUnprotectedRead(x)
/* ANNOTATE_UNPROTECTED_READ is the preferred way to annotate racey reads. namespace absl {
ABSL_NAMESPACE_BEGIN
namespace base_internal {
Instead of doing
ANNOTATE_IGNORE_READS_BEGIN();
... = x;
ANNOTATE_IGNORE_READS_END();
one can use
... = ANNOTATE_UNPROTECTED_READ(x); */
#if defined(__cplusplus) && defined(ANNOTATIONS_ENABLED)
template <typename T> template <typename T>
inline T ANNOTATE_UNPROTECTED_READ(const volatile T &x) { /* NOLINT */ inline T AnnotateUnprotectedRead(const volatile T& x) { // NOLINT
ANNOTATE_IGNORE_READS_BEGIN(); ABSL_ANNOTATE_IGNORE_READS_BEGIN();
T res = x; T res = x;
ANNOTATE_IGNORE_READS_END(); ABSL_ANNOTATE_IGNORE_READS_END();
return res; return res;
} }
} // namespace base_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif
#else #else
#define ANNOTATE_UNPROTECTED_READ(x) (x)
#define ABSL_ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN() // empty
#define ABSL_ANNOTATE_IGNORE_READS_AND_WRITES_END() // empty
#define ABSL_ANNOTATE_UNPROTECTED_READ(x) (x)
#endif #endif
#if DYNAMIC_ANNOTATIONS_ENABLED != 0 && defined(__cplusplus) ABSL_INTERNAL_BEGIN_EXTERN_C
/* Apply ANNOTATE_BENIGN_RACE_SIZED to a static variable. */
#define ANNOTATE_BENIGN_RACE_STATIC(static_var, description) \ // -------------------------------------------------------------------------
namespace { \ // Return non-zero value if running under valgrind.
class static_var ## _annotator { \ //
public: \ // If "valgrind.h" is included into dynamic_annotations.cc,
static_var ## _annotator() { \ // the regular valgrind mechanism will be used.
ANNOTATE_BENIGN_RACE_SIZED(&static_var, \ // See http://valgrind.org/docs/manual/manual-core-adv.html about
sizeof(static_var), \ // RUNNING_ON_VALGRIND and other valgrind "client requests".
# static_var ": " description); \ // The file "valgrind.h" may be obtained by doing
} \ // svn co svn://svn.valgrind.org/valgrind/trunk/include
}; \ //
static static_var ## _annotator the ## static_var ## _annotator;\ // If for some reason you can't use "valgrind.h" or want to fake valgrind,
} // namespace // there are two ways to make this function return non-zero:
#else /* DYNAMIC_ANNOTATIONS_ENABLED == 0 */ // - Use environment variable: export RUNNING_ON_VALGRIND=1
#define ANNOTATE_BENIGN_RACE_STATIC(static_var, description) /* empty */ // - Make your tool intercept the function RunningOnValgrind() and
#endif /* DYNAMIC_ANNOTATIONS_ENABLED */ // change its return value.
//
int RunningOnValgrind(void);
// ValgrindSlowdown returns:
// * 1.0, if (RunningOnValgrind() == 0)
// * 50.0, if (RunningOnValgrind() != 0 && getenv("VALGRIND_SLOWDOWN") ==
// NULL)
// * atof(getenv("VALGRIND_SLOWDOWN")) otherwise
// This function can be used to scale timeout values:
// EXAMPLE:
// for (;;) {
// DoExpensiveBackgroundTask();
// SleepForSeconds(5 * ValgrindSlowdown());
// }
//
double ValgrindSlowdown(void);
ABSL_INTERNAL_END_EXTERN_C
// -------------------------------------------------------------------------
// Address sanitizer annotations
#ifdef ADDRESS_SANITIZER #ifdef ADDRESS_SANITIZER
/* Describe the current state of a contiguous container such as e.g. // Describe the current state of a contiguous container such as e.g.
* std::vector or std::string. For more details see // std::vector or std::string. For more details see
* sanitizer/common_interface_defs.h, which is provided by the compiler. */ // sanitizer/common_interface_defs.h, which is provided by the compiler.
#include <sanitizer/common_interface_defs.h> #include <sanitizer/common_interface_defs.h>
#define ANNOTATE_CONTIGUOUS_CONTAINER(beg, end, old_mid, new_mid) \
#define ABSL_ANNOTATE_CONTIGUOUS_CONTAINER(beg, end, old_mid, new_mid) \
__sanitizer_annotate_contiguous_container(beg, end, old_mid, new_mid) __sanitizer_annotate_contiguous_container(beg, end, old_mid, new_mid)
#define ADDRESS_SANITIZER_REDZONE(name) \ #define ABSL_ADDRESS_SANITIZER_REDZONE(name) \
struct { char x[8] __attribute__ ((aligned (8))); } name struct { \
char x[8] __attribute__((aligned(8))); \
} name
#else #else
#define ANNOTATE_CONTIGUOUS_CONTAINER(beg, end, old_mid, new_mid)
#define ADDRESS_SANITIZER_REDZONE(name) static_assert(true, "") #define ABSL_ANNOTATE_CONTIGUOUS_CONTAINER(beg, end, old_mid, new_mid)
#define ABSL_ADDRESS_SANITIZER_REDZONE(name) static_assert(true, "")
#endif // ADDRESS_SANITIZER #endif // ADDRESS_SANITIZER
/* Undefine the macros intended only in this file. */ // -------------------------------------------------------------------------
#undef ANNOTALYSIS_ENABLED // Undefine the macros intended only for this file.
#undef ANNOTATIONS_ENABLED
#undef ATTRIBUTE_IGNORE_READS_BEGIN #undef ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED
#undef ATTRIBUTE_IGNORE_READS_END #undef ABSL_INTERNAL_MEMORY_ANNOTATIONS_ENABLED
#undef ABSL_INTERNAL_READS_ANNOTATIONS_ENABLED
#undef ABSL_INTERNAL_WRITES_ANNOTATIONS_ENABLED
#undef ABSL_INTERNAL_ANNOTALYSIS_ENABLED
#undef ABSL_INTERNAL_READS_WRITES_ANNOTATIONS_ENABLED
#undef ABSL_INTERNAL_BEGIN_EXTERN_C
#undef ABSL_INTERNAL_END_EXTERN_C
#undef ABSL_INTERNAL_STATIC_INLINE
#endif /* ABSL_BASE_DYNAMIC_ANNOTATIONS_H_ */ #endif // ABSL_BASE_DYNAMIC_ANNOTATIONS_H_
// Copyright 2017 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This file defines dynamic annotations for use with dynamic analysis tool
// such as valgrind, PIN, etc.
//
// Dynamic annotation is a source code annotation that affects the generated
// code (that is, the annotation is not a comment). Each such annotation is
// attached to a particular instruction and/or to a particular object (address)
// in the program.
//
// The annotations that should be used by users are macros in all upper-case
// (e.g., ANNOTATE_THREAD_NAME).
//
// Actual implementation of these macros may differ depending on the dynamic
// analysis tool being used.
//
// This file supports the following configurations:
// - Dynamic Annotations enabled (with static thread-safety warnings disabled).
// In this case, macros expand to functions implemented by Thread Sanitizer,
// when building with TSan. When not provided an external implementation,
// dynamic_annotations.cc provides no-op implementations.
//
// - Static Clang thread-safety warnings enabled.
// When building with a Clang compiler that supports thread-safety warnings,
// a subset of annotations can be statically-checked at compile-time. We
// expand these macros to static-inline functions that can be analyzed for
// thread-safety, but afterwards elided when building the final binary.
//
// - All annotations are disabled.
// If neither Dynamic Annotations nor Clang thread-safety warnings are
// enabled, then all annotation-macros expand to empty.
#ifndef ABSL_BASE_INTERNAL_DYNAMIC_ANNOTATIONS_H_
#define ABSL_BASE_INTERNAL_DYNAMIC_ANNOTATIONS_H_
#include <stddef.h>
#include "absl/base/config.h"
// -------------------------------------------------------------------------
// Decide which features are enabled
#ifndef DYNAMIC_ANNOTATIONS_ENABLED
#define DYNAMIC_ANNOTATIONS_ENABLED 0
#endif
#if defined(__clang__) && !defined(SWIG)
#define ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED 1
#else
#define ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED 0
#endif
#if DYNAMIC_ANNOTATIONS_ENABLED != 0
#define ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED 1
#define ABSL_INTERNAL_READS_ANNOTATIONS_ENABLED 1
#define ABSL_INTERNAL_WRITES_ANNOTATIONS_ENABLED 1
#define ABSL_INTERNAL_ANNOTALYSIS_ENABLED 0
#define ABSL_INTERNAL_READS_WRITES_ANNOTATIONS_ENABLED 1
#else
#define ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED 0
#define ABSL_INTERNAL_READS_ANNOTATIONS_ENABLED 0
#define ABSL_INTERNAL_WRITES_ANNOTATIONS_ENABLED 0
// Clang provides limited support for static thread-safety analysis through a
// feature called Annotalysis. We configure macro-definitions according to
// whether Annotalysis support is available. When running in opt-mode, GCC
// will issue a warning, if these attributes are compiled. Only include them
// when compiling using Clang.
// ANNOTALYSIS_ENABLED == 1 when IGNORE_READ_ATTRIBUTE_ENABLED == 1
#define ABSL_INTERNAL_ANNOTALYSIS_ENABLED \
ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED
// Read/write annotations are enabled in Annotalysis mode; disabled otherwise.
#define ABSL_INTERNAL_READS_WRITES_ANNOTATIONS_ENABLED \
ABSL_INTERNAL_ANNOTALYSIS_ENABLED
#endif
// Memory annotations are also made available to LLVM's Memory Sanitizer
#if defined(MEMORY_SANITIZER) && defined(__has_feature) && \
!defined(__native_client__)
#if __has_feature(memory_sanitizer)
#define ABSL_INTERNAL_MEMORY_ANNOTATIONS_ENABLED 1
#endif
#endif
#ifndef ABSL_INTERNAL_MEMORY_ANNOTATIONS_ENABLED
#define ABSL_INTERNAL_MEMORY_ANNOTATIONS_ENABLED 0
#endif
#ifdef __cplusplus
#define ABSL_INTERNAL_BEGIN_EXTERN_C extern "C" {
#define ABSL_INTERNAL_END_EXTERN_C } // extern "C"
#define ABSL_INTERNAL_GLOBAL_SCOPED(F) ::F
#define ABSL_INTERNAL_STATIC_INLINE inline
#else
#define ABSL_INTERNAL_BEGIN_EXTERN_C // empty
#define ABSL_INTERNAL_END_EXTERN_C // empty
#define ABSL_INTERNAL_GLOBAL_SCOPED(F) F
#define ABSL_INTERNAL_STATIC_INLINE static inline
#endif
// -------------------------------------------------------------------------
// Define race annotations.
#if ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED == 1
// -------------------------------------------------------------
// Annotations that suppress errors. It is usually better to express the
// program's synchronization using the other annotations, but these can be used
// when all else fails.
// Report that we may have a benign race at `pointer`, with size
// "sizeof(*(pointer))". `pointer` must be a non-void* pointer. Insert at the
// point where `pointer` has been allocated, preferably close to the point
// where the race happens. See also ANNOTATE_BENIGN_RACE_STATIC.
#define ANNOTATE_BENIGN_RACE(pointer, description) \
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateBenignRaceSized) \
(__FILE__, __LINE__, pointer, sizeof(*(pointer)), description)
// Same as ANNOTATE_BENIGN_RACE(`address`, `description`), but applies to
// the memory range [`address`, `address`+`size`).
#define ANNOTATE_BENIGN_RACE_SIZED(address, size, description) \
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateBenignRaceSized) \
(__FILE__, __LINE__, address, size, description)
// Enable (`enable`!=0) or disable (`enable`==0) race detection for all threads.
// This annotation could be useful if you want to skip expensive race analysis
// during some period of program execution, e.g. during initialization.
#define ANNOTATE_ENABLE_RACE_DETECTION(enable) \
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateEnableRaceDetection) \
(__FILE__, __LINE__, enable)
// -------------------------------------------------------------
// Annotations useful for debugging.
// Report the current thread `name` to a race detector.
#define ANNOTATE_THREAD_NAME(name) \
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateThreadName)(__FILE__, __LINE__, name)
// -------------------------------------------------------------
// Annotations useful when implementing locks. They are not normally needed by
// modules that merely use locks. The `lock` argument is a pointer to the lock
// object.
// Report that a lock has been created at address `lock`.
#define ANNOTATE_RWLOCK_CREATE(lock) \
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockCreate)(__FILE__, __LINE__, lock)
// Report that a linker initialized lock has been created at address `lock`.
#ifdef THREAD_SANITIZER
#define ANNOTATE_RWLOCK_CREATE_STATIC(lock) \
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockCreateStatic) \
(__FILE__, __LINE__, lock)
#else
#define ANNOTATE_RWLOCK_CREATE_STATIC(lock) ANNOTATE_RWLOCK_CREATE(lock)
#endif
// Report that the lock at address `lock` is about to be destroyed.
#define ANNOTATE_RWLOCK_DESTROY(lock) \
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockDestroy)(__FILE__, __LINE__, lock)
// Report that the lock at address `lock` has been acquired.
// `is_w`=1 for writer lock, `is_w`=0 for reader lock.
#define ANNOTATE_RWLOCK_ACQUIRED(lock, is_w) \
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockAcquired) \
(__FILE__, __LINE__, lock, is_w)
// Report that the lock at address `lock` is about to be released.
// `is_w`=1 for writer lock, `is_w`=0 for reader lock.
#define ANNOTATE_RWLOCK_RELEASED(lock, is_w) \
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockReleased) \
(__FILE__, __LINE__, lock, is_w)
// Apply ANNOTATE_BENIGN_RACE_SIZED to a static variable `static_var`.
#define ANNOTATE_BENIGN_RACE_STATIC(static_var, description) \
namespace { \
class static_var##_annotator { \
public: \
static_var##_annotator() { \
ANNOTATE_BENIGN_RACE_SIZED(&static_var, sizeof(static_var), \
#static_var ": " description); \
} \
}; \
static static_var##_annotator the##static_var##_annotator; \
} // namespace
#else // ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED == 0
#define ANNOTATE_RWLOCK_CREATE(lock) // empty
#define ANNOTATE_RWLOCK_CREATE_STATIC(lock) // empty
#define ANNOTATE_RWLOCK_DESTROY(lock) // empty
#define ANNOTATE_RWLOCK_ACQUIRED(lock, is_w) // empty
#define ANNOTATE_RWLOCK_RELEASED(lock, is_w) // empty
#define ANNOTATE_BENIGN_RACE(address, description) // empty
#define ANNOTATE_BENIGN_RACE_SIZED(address, size, description) // empty
#define ANNOTATE_THREAD_NAME(name) // empty
#define ANNOTATE_ENABLE_RACE_DETECTION(enable) // empty
#define ANNOTATE_BENIGN_RACE_STATIC(static_var, description) // empty
#endif // ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED
// -------------------------------------------------------------------------
// Define memory annotations.
#if ABSL_INTERNAL_MEMORY_ANNOTATIONS_ENABLED == 1
#include <sanitizer/msan_interface.h>
#define ANNOTATE_MEMORY_IS_INITIALIZED(address, size) \
__msan_unpoison(address, size)
#define ANNOTATE_MEMORY_IS_UNINITIALIZED(address, size) \
__msan_allocated_memory(address, size)
#else // ABSL_INTERNAL_MEMORY_ANNOTATIONS_ENABLED == 0
#if DYNAMIC_ANNOTATIONS_ENABLED == 1
#define ANNOTATE_MEMORY_IS_INITIALIZED(address, size) \
do { \
(void)(address); \
(void)(size); \
} while (0)
#define ANNOTATE_MEMORY_IS_UNINITIALIZED(address, size) \
do { \
(void)(address); \
(void)(size); \
} while (0)
#else
#define ANNOTATE_MEMORY_IS_INITIALIZED(address, size) // empty
#define ANNOTATE_MEMORY_IS_UNINITIALIZED(address, size) // empty
#endif
#endif // ABSL_INTERNAL_MEMORY_ANNOTATIONS_ENABLED
// -------------------------------------------------------------------------
// Define IGNORE_READS_BEGIN/_END attributes.
#if ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED == 1
#define ABSL_INTERNAL_IGNORE_READS_BEGIN_ATTRIBUTE \
__attribute((exclusive_lock_function("*")))
#define ABSL_INTERNAL_IGNORE_READS_END_ATTRIBUTE \
__attribute((unlock_function("*")))
#else // ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED == 0
#define ABSL_INTERNAL_IGNORE_READS_BEGIN_ATTRIBUTE // empty
#define ABSL_INTERNAL_IGNORE_READS_END_ATTRIBUTE // empty
#endif // ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED
// -------------------------------------------------------------------------
// Define IGNORE_READS_BEGIN/_END annotations.
#if ABSL_INTERNAL_READS_ANNOTATIONS_ENABLED == 1
// Request the analysis tool to ignore all reads in the current thread until
// ANNOTATE_IGNORE_READS_END is called. Useful to ignore intentional racey
// reads, while still checking other reads and all writes.
// See also ANNOTATE_UNPROTECTED_READ.
#define ANNOTATE_IGNORE_READS_BEGIN() \
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateIgnoreReadsBegin)(__FILE__, __LINE__)
// Stop ignoring reads.
#define ANNOTATE_IGNORE_READS_END() \
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateIgnoreReadsEnd)(__FILE__, __LINE__)
#elif defined(ABSL_INTERNAL_ANNOTALYSIS_ENABLED)
// When Annotalysis is enabled without Dynamic Annotations, the use of
// static-inline functions allows the annotations to be read at compile-time,
// while still letting the compiler elide the functions from the final build.
//
// TODO(delesley) -- The exclusive lock here ignores writes as well, but
// allows IGNORE_READS_AND_WRITES to work properly.
#define ANNOTATE_IGNORE_READS_BEGIN() \
ABSL_INTERNAL_GLOBAL_SCOPED(AbslInternalAnnotateIgnoreReadsBegin)()
#define ANNOTATE_IGNORE_READS_END() \
ABSL_INTERNAL_GLOBAL_SCOPED(AbslInternalAnnotateIgnoreReadsEnd)()
#else
#define ANNOTATE_IGNORE_READS_BEGIN() // empty
#define ANNOTATE_IGNORE_READS_END() // empty
#endif
// -------------------------------------------------------------------------
// Define IGNORE_WRITES_BEGIN/_END annotations.
#if ABSL_INTERNAL_WRITES_ANNOTATIONS_ENABLED == 1
// Similar to ANNOTATE_IGNORE_READS_BEGIN, but ignore writes instead.
#define ANNOTATE_IGNORE_WRITES_BEGIN() \
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateIgnoreWritesBegin)(__FILE__, __LINE__)
// Stop ignoring writes.
#define ANNOTATE_IGNORE_WRITES_END() \
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateIgnoreWritesEnd)(__FILE__, __LINE__)
#else
#define ANNOTATE_IGNORE_WRITES_BEGIN() // empty
#define ANNOTATE_IGNORE_WRITES_END() // empty
#endif
// -------------------------------------------------------------------------
// Define the ANNOTATE_IGNORE_READS_AND_WRITES_* annotations using the more
// primitive annotations defined above.
//
// Instead of doing
// ANNOTATE_IGNORE_READS_BEGIN();
// ... = x;
// ANNOTATE_IGNORE_READS_END();
// one can use
// ... = ANNOTATE_UNPROTECTED_READ(x);
#if defined(ABSL_INTERNAL_READS_WRITES_ANNOTATIONS_ENABLED)
// Start ignoring all memory accesses (both reads and writes).
#define ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN() \
do { \
ANNOTATE_IGNORE_READS_BEGIN(); \
ANNOTATE_IGNORE_WRITES_BEGIN(); \
} while (0)
// Stop ignoring both reads and writes.
#define ANNOTATE_IGNORE_READS_AND_WRITES_END() \
do { \
ANNOTATE_IGNORE_WRITES_END(); \
ANNOTATE_IGNORE_READS_END(); \
} while (0)
#ifdef __cplusplus
// ANNOTATE_UNPROTECTED_READ is the preferred way to annotate racey reads.
#define ANNOTATE_UNPROTECTED_READ(x) \
absl::base_internal::AnnotateUnprotectedRead(x)
#endif
#else
#define ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN() // empty
#define ANNOTATE_IGNORE_READS_AND_WRITES_END() // empty
#define ANNOTATE_UNPROTECTED_READ(x) (x)
#endif
// -------------------------------------------------------------------------
// Address sanitizer annotations
#ifdef ADDRESS_SANITIZER
// Describe the current state of a contiguous container such as e.g.
// std::vector or std::string. For more details see
// sanitizer/common_interface_defs.h, which is provided by the compiler.
#include <sanitizer/common_interface_defs.h>
#define ANNOTATE_CONTIGUOUS_CONTAINER(beg, end, old_mid, new_mid) \
__sanitizer_annotate_contiguous_container(beg, end, old_mid, new_mid)
#define ADDRESS_SANITIZER_REDZONE(name) \
struct { \
char x[8] __attribute__((aligned(8))); \
} name
#else
#define ANNOTATE_CONTIGUOUS_CONTAINER(beg, end, old_mid, new_mid)
#define ADDRESS_SANITIZER_REDZONE(name) static_assert(true, "")
#endif // ADDRESS_SANITIZER
// -------------------------------------------------------------------------
// Undefine the macros intended only for this file.
#undef ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED
#undef ABSL_INTERNAL_MEMORY_ANNOTATIONS_ENABLED
#undef ABSL_INTERNAL_READS_ANNOTATIONS_ENABLED
#undef ABSL_INTERNAL_WRITES_ANNOTATIONS_ENABLED
#undef ABSL_INTERNAL_ANNOTALYSIS_ENABLED
#undef ABSL_INTERNAL_READS_WRITES_ANNOTATIONS_ENABLED
#undef ABSL_INTERNAL_BEGIN_EXTERN_C
#undef ABSL_INTERNAL_END_EXTERN_C
#undef ABSL_INTERNAL_STATIC_INLINE
#endif // ABSL_BASE_INTERNAL_DYNAMIC_ANNOTATIONS_H_
...@@ -114,9 +114,7 @@ cc_library( ...@@ -114,9 +114,7 @@ cc_library(
visibility = ["//visibility:private"], visibility = ["//visibility:private"],
deps = [ deps = [
"//absl/base:config", "//absl/base:config",
"//absl/base:core_headers",
"//absl/base:fast_type_id", "//absl/base:fast_type_id",
"//absl/strings",
], ],
) )
...@@ -132,6 +130,9 @@ cc_library( ...@@ -132,6 +130,9 @@ cc_library(
linkopts = ABSL_DEFAULT_LINKOPTS, linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [ deps = [
":commandlineflag_internal", ":commandlineflag_internal",
"//absl/base:config",
"//absl/base:fast_type_id",
"//absl/strings",
"//absl/types:optional", "//absl/types:optional",
], ],
) )
...@@ -149,7 +150,12 @@ cc_library( ...@@ -149,7 +150,12 @@ cc_library(
visibility = [ visibility = [
"//absl/flags:__pkg__", "//absl/flags:__pkg__",
], ],
deps = [":commandlineflag"], deps = [
":commandlineflag",
":commandlineflag_internal",
"//absl/base:config",
"//absl/strings",
],
) )
cc_library( cc_library(
...@@ -193,6 +199,7 @@ cc_library( ...@@ -193,6 +199,7 @@ cc_library(
visibility = ["//absl/base:__subpackages__"], visibility = ["//absl/base:__subpackages__"],
deps = [ deps = [
":commandlineflag", ":commandlineflag",
":commandlineflag_internal",
":config", ":config",
":marshalling", ":marshalling",
":registry", ":registry",
...@@ -203,6 +210,7 @@ cc_library( ...@@ -203,6 +210,7 @@ cc_library(
"//absl/meta:type_traits", "//absl/meta:type_traits",
"//absl/strings", "//absl/strings",
"//absl/synchronization", "//absl/synchronization",
"//absl/utility",
], ],
) )
...@@ -220,7 +228,6 @@ cc_library( ...@@ -220,7 +228,6 @@ cc_library(
deps = [ deps = [
":config", ":config",
":flag_internal", ":flag_internal",
":marshalling",
":registry", ":registry",
"//absl/base", "//absl/base",
"//absl/base:config", "//absl/base:config",
...@@ -287,6 +294,7 @@ cc_library( ...@@ -287,6 +294,7 @@ cc_library(
linkopts = ABSL_DEFAULT_LINKOPTS, linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [ deps = [
":commandlineflag", ":commandlineflag",
":commandlineflag_internal",
":config", ":config",
":flag", ":flag",
":flag_internal", ":flag_internal",
...@@ -315,6 +323,7 @@ cc_test( ...@@ -315,6 +323,7 @@ cc_test(
linkopts = ABSL_DEFAULT_LINKOPTS, linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [ deps = [
":commandlineflag", ":commandlineflag",
":commandlineflag_internal",
":config", ":config",
":flag", ":flag",
":private_handle_accessor", ":private_handle_accessor",
...@@ -352,6 +361,7 @@ cc_test( ...@@ -352,6 +361,7 @@ cc_test(
":config", ":config",
":flag", ":flag",
":flag_internal", ":flag_internal",
":marshalling",
":registry", ":registry",
"//absl/base:core_headers", "//absl/base:core_headers",
"//absl/base:malloc_internal", "//absl/base:malloc_internal",
...@@ -372,6 +382,8 @@ cc_binary( ...@@ -372,6 +382,8 @@ cc_binary(
visibility = ["//visibility:private"], visibility = ["//visibility:private"],
deps = [ deps = [
":flag", ":flag",
":marshalling",
"//absl/strings",
"//absl/time", "//absl/time",
"//absl/types:optional", "//absl/types:optional",
"@com_github_google_benchmark//:benchmark_main", "@com_github_google_benchmark//:benchmark_main",
...@@ -450,8 +462,8 @@ cc_test( ...@@ -450,8 +462,8 @@ cc_test(
copts = ABSL_TEST_COPTS, copts = ABSL_TEST_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS, linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [ deps = [
":commandlineflag_internal",
":flag", ":flag",
":marshalling",
":registry", ":registry",
"//absl/memory", "//absl/memory",
"@com_google_googletest//:gtest_main", "@com_google_googletest//:gtest_main",
...@@ -492,7 +504,6 @@ cc_test( ...@@ -492,7 +504,6 @@ cc_test(
":registry", ":registry",
":usage", ":usage",
":usage_internal", ":usage_internal",
"//absl/memory",
"//absl/strings", "//absl/strings",
"@com_google_googletest//:gtest", "@com_google_googletest//:gtest",
], ],
......
...@@ -103,9 +103,7 @@ absl_cc_library( ...@@ -103,9 +103,7 @@ absl_cc_library(
${ABSL_DEFAULT_LINKOPTS} ${ABSL_DEFAULT_LINKOPTS}
DEPS DEPS
absl::config absl::config
absl::core_headers
absl::fast_type_id absl::fast_type_id
absl::strings
) )
absl_cc_library( absl_cc_library(
...@@ -120,8 +118,11 @@ absl_cc_library( ...@@ -120,8 +118,11 @@ absl_cc_library(
LINKOPTS LINKOPTS
${ABSL_DEFAULT_LINKOPTS} ${ABSL_DEFAULT_LINKOPTS}
DEPS DEPS
absl::config
absl::fast_type_id
absl::flags_commandlineflag_internal absl::flags_commandlineflag_internal
absl::optional absl::optional
absl::strings
) )
# Internal-only target, do not depend on directly. # Internal-only target, do not depend on directly.
...@@ -137,7 +138,10 @@ absl_cc_library( ...@@ -137,7 +138,10 @@ absl_cc_library(
LINKOPTS LINKOPTS
${ABSL_DEFAULT_LINKOPTS} ${ABSL_DEFAULT_LINKOPTS}
DEPS DEPS
absl::config
absl::flags_commandlineflag absl::flags_commandlineflag
absl::flags_commandlineflag_internal
absl::strings
) )
# Internal-only target, do not depend on directly. # Internal-only target, do not depend on directly.
...@@ -180,11 +184,13 @@ absl_cc_library( ...@@ -180,11 +184,13 @@ absl_cc_library(
DEPS DEPS
absl::base absl::base
absl::config absl::config
absl::flags_commandlineflag_internal
absl::flags_config absl::flags_config
absl::flags_marshalling absl::flags_marshalling
absl::flags_registry absl::flags_registry
absl::synchronization absl::synchronization
absl::meta absl::meta
absl::utility
PUBLIC PUBLIC
) )
...@@ -205,7 +211,6 @@ absl_cc_library( ...@@ -205,7 +211,6 @@ absl_cc_library(
absl::flags_commandlineflag absl::flags_commandlineflag
absl::flags_config absl::flags_config
absl::flags_internal absl::flags_internal
absl::flags_marshalling
absl::flags_registry absl::flags_registry
absl::base absl::base
absl::core_headers absl::core_headers
...@@ -275,6 +280,7 @@ absl_cc_library( ...@@ -275,6 +280,7 @@ absl_cc_library(
absl::flags_config absl::flags_config
absl::flags absl::flags
absl::flags_commandlineflag absl::flags_commandlineflag
absl::flags_commandlineflag_internal
absl::flags_internal absl::flags_internal
absl::flags_private_handle_accessor absl::flags_private_handle_accessor
absl::flags_program_name absl::flags_program_name
...@@ -297,6 +303,7 @@ absl_cc_test( ...@@ -297,6 +303,7 @@ absl_cc_test(
DEPS DEPS
absl::flags absl::flags
absl::flags_commandlineflag absl::flags_commandlineflag
absl::flags_commandlineflag_internal
absl::flags_config absl::flags_config
absl::flags_private_handle_accessor absl::flags_private_handle_accessor
absl::flags_registry absl::flags_registry
...@@ -330,6 +337,7 @@ absl_cc_test( ...@@ -330,6 +337,7 @@ absl_cc_test(
absl::flags absl::flags
absl::flags_config absl::flags_config
absl::flags_internal absl::flags_internal
absl::flags_marshalling
absl::flags_registry absl::flags_registry
absl::strings absl::strings
absl::time absl::time
...@@ -399,8 +407,8 @@ absl_cc_test( ...@@ -399,8 +407,8 @@ absl_cc_test(
COPTS COPTS
${ABSL_TEST_COPTS} ${ABSL_TEST_COPTS}
DEPS DEPS
absl::flags_commandlineflag_internal
absl::flags absl::flags
absl::flags_marshalling
absl::flags_registry absl::flags_registry
absl::memory absl::memory
absl::strings absl::strings
...@@ -437,7 +445,6 @@ absl_cc_test( ...@@ -437,7 +445,6 @@ absl_cc_test(
absl::flags_parse absl::flags_parse
absl::flags_registry absl::flags_registry
absl::flags_usage absl::flags_usage
absl::memory
absl::strings absl::strings
gtest gtest
) )
...@@ -15,6 +15,12 @@ ...@@ -15,6 +15,12 @@
#include "absl/flags/commandlineflag.h" #include "absl/flags/commandlineflag.h"
#include <string>
#include "absl/base/config.h"
#include "absl/flags/internal/commandlineflag.h"
#include "absl/strings/string_view.h"
namespace absl { namespace absl {
ABSL_NAMESPACE_BEGIN ABSL_NAMESPACE_BEGIN
......
...@@ -26,7 +26,13 @@ ...@@ -26,7 +26,13 @@
#ifndef ABSL_FLAGS_COMMANDLINEFLAG_H_ #ifndef ABSL_FLAGS_COMMANDLINEFLAG_H_
#define ABSL_FLAGS_COMMANDLINEFLAG_H_ #define ABSL_FLAGS_COMMANDLINEFLAG_H_
#include <memory>
#include <string>
#include "absl/base/config.h"
#include "absl/base/internal/fast_type_id.h"
#include "absl/flags/internal/commandlineflag.h" #include "absl/flags/internal/commandlineflag.h"
#include "absl/strings/string_view.h"
#include "absl/types/optional.h" #include "absl/types/optional.h"
namespace absl { namespace absl {
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "absl/flags/flag.h" #include "absl/flags/flag.h"
#include "absl/flags/internal/commandlineflag.h"
#include "absl/flags/internal/private_handle_accessor.h" #include "absl/flags/internal/private_handle_accessor.h"
#include "absl/flags/internal/registry.h" #include "absl/flags/internal/registry.h"
#include "absl/flags/usage_config.h" #include "absl/flags/usage_config.h"
......
...@@ -26,7 +26,6 @@ ...@@ -26,7 +26,6 @@
#define ABSL_FLAGS_DECLARE_H_ #define ABSL_FLAGS_DECLARE_H_
#include "absl/base/config.h" #include "absl/base/config.h"
#include "absl/strings/string_view.h"
namespace absl { namespace absl {
ABSL_NAMESPACE_BEGIN ABSL_NAMESPACE_BEGIN
......
...@@ -33,13 +33,11 @@ ...@@ -33,13 +33,11 @@
#include <type_traits> #include <type_traits>
#include "absl/base/attributes.h" #include "absl/base/attributes.h"
#include "absl/base/casts.h"
#include "absl/base/config.h" #include "absl/base/config.h"
#include "absl/base/optimization.h"
#include "absl/flags/config.h" #include "absl/flags/config.h"
#include "absl/flags/declare.h"
#include "absl/flags/internal/flag.h" #include "absl/flags/internal/flag.h"
#include "absl/flags/internal/registry.h" #include "absl/flags/internal/registry.h"
#include "absl/flags/marshalling.h"
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
namespace absl { namespace absl {
...@@ -150,6 +148,8 @@ class Flag { ...@@ -150,6 +148,8 @@ class Flag {
void Set(const T& v) { GetImpl().Set(v); } void Set(const T& v) { GetImpl().Set(v); }
void InvokeCallback() { GetImpl().InvokeCallback(); } void InvokeCallback() { GetImpl().InvokeCallback(); }
const CommandLineFlag& Reflect() const { return GetImpl().Reflect(); }
// The data members are logically private, but they need to be public for // The data members are logically private, but they need to be public for
// this to be an aggregate type. // this to be an aggregate type.
const char* name_; const char* name_;
...@@ -204,6 +204,21 @@ void SetFlag(absl::Flag<T>* flag, const V& v) { ...@@ -204,6 +204,21 @@ void SetFlag(absl::Flag<T>* flag, const V& v) {
flag->Set(value); flag->Set(value);
} }
// GetFlagReflectionHandle()
//
// Returns the reflection handle corresponding to specified Abseil Flag
// instance. Use this handle to access flag's reflection information, like name,
// location, default value etc.
//
// Example:
//
// std::string = absl::GetFlagReflectionHandle(FLAGS_count).DefaultValue();
template <typename T>
const CommandLineFlag& GetFlagReflectionHandle(const absl::Flag<T>& f) {
return f.Reflect();
}
ABSL_NAMESPACE_END ABSL_NAMESPACE_END
} // namespace absl } // namespace absl
......
...@@ -13,7 +13,14 @@ ...@@ -13,7 +13,14 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include <stdint.h>
#include <string>
#include <vector>
#include "absl/flags/flag.h" #include "absl/flags/flag.h"
#include "absl/flags/marshalling.h"
#include "absl/strings/string_view.h"
#include "absl/time/time.h" #include "absl/time/time.h"
#include "absl/types/optional.h" #include "absl/types/optional.h"
#include "benchmark/benchmark.h" #include "benchmark/benchmark.h"
......
...@@ -15,9 +15,11 @@ ...@@ -15,9 +15,11 @@
#include "absl/flags/flag.h" #include "absl/flags/flag.h"
#include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <cmath> #include <cmath>
#include <new>
#include <string> #include <string>
#include <thread> // NOLINT #include <thread> // NOLINT
#include <vector> #include <vector>
...@@ -28,6 +30,7 @@ ...@@ -28,6 +30,7 @@
#include "absl/flags/declare.h" #include "absl/flags/declare.h"
#include "absl/flags/internal/flag.h" #include "absl/flags/internal/flag.h"
#include "absl/flags/internal/registry.h" #include "absl/flags/internal/registry.h"
#include "absl/flags/marshalling.h"
#include "absl/flags/usage_config.h" #include "absl/flags/usage_config.h"
#include "absl/strings/match.h" #include "absl/strings/match.h"
#include "absl/strings/numbers.h" #include "absl/strings/numbers.h"
...@@ -44,6 +47,9 @@ namespace { ...@@ -44,6 +47,9 @@ namespace {
namespace flags = absl::flags_internal; namespace flags = absl::flags_internal;
std::string TestHelpMsg() { return "dynamic help"; } std::string TestHelpMsg() { return "dynamic help"; }
#if defined(_MSC_VER) && !defined(__clang__)
std::string TestLiteralHelpMsg() { return "literal help"; }
#endif
template <typename T> template <typename T>
void TestMakeDflt(void* dst) { void TestMakeDflt(void* dst) {
new (dst) T{}; new (dst) T{};
...@@ -127,15 +133,29 @@ constexpr flags::FlagHelpArg help_arg{flags::FlagHelpMsg("literal help"), ...@@ -127,15 +133,29 @@ constexpr flags::FlagHelpArg help_arg{flags::FlagHelpMsg("literal help"),
using String = std::string; using String = std::string;
#define DEFINE_CONSTRUCTED_FLAG(T, dflt, dflt_kind) \ #if !defined(_MSC_VER) || defined(__clang__)
constexpr flags::FlagDefaultArg f1default##T{ \ #define DEFINE_CONSTRUCTED_FLAG(T, dflt, dflt_kind) \
flags::FlagDefaultSrc{dflt}, flags::FlagDefaultKind::dflt_kind}; \ constexpr flags::FlagDefaultArg f1default##T{ \
constexpr flags::Flag<T> f1##T("f1", "file", help_arg, f1default##T); \ flags::FlagDefaultSrc{dflt}, flags::FlagDefaultKind::dflt_kind}; \
ABSL_CONST_INIT flags::Flag<T> f2##T( \ constexpr absl::Flag<T> f1##T{"f1", "file", help_arg, f1default##T}; \
"f2", "file", \ ABSL_CONST_INIT absl::Flag<T> f2##T { \
{flags::FlagHelpMsg(&TestHelpMsg), flags::FlagHelpKind::kGenFunc}, \ "f2", "file", \
flags::FlagDefaultArg{flags::FlagDefaultSrc(&TestMakeDflt<T>), \ {flags::FlagHelpMsg(&TestHelpMsg), flags::FlagHelpKind::kGenFunc}, \
flags::FlagDefaultKind::kGenFunc}) flags::FlagDefaultArg { \
flags::FlagDefaultSrc(&TestMakeDflt<T>), \
flags::FlagDefaultKind::kGenFunc \
} \
}
#else
#define DEFINE_CONSTRUCTED_FLAG(T, dflt, dflt_kind) \
constexpr flags::FlagDefaultArg f1default##T{ \
flags::FlagDefaultSrc{dflt}, flags::FlagDefaultKind::dflt_kind}; \
constexpr absl::Flag<T> f1##T{"f1", "file", &TestLiteralHelpMsg, \
&TestMakeDflt<T>}; \
ABSL_CONST_INIT absl::Flag<T> f2##T { \
"f2", "file", &TestHelpMsg, &TestMakeDflt<T> \
}
#endif
DEFINE_CONSTRUCTED_FLAG(bool, true, kOneWord); DEFINE_CONSTRUCTED_FLAG(bool, true, kOneWord);
DEFINE_CONSTRUCTED_FLAG(int16_t, 1, kOneWord); DEFINE_CONSTRUCTED_FLAG(int16_t, 1, kOneWord);
...@@ -150,16 +170,17 @@ DEFINE_CONSTRUCTED_FLAG(String, &TestMakeDflt<String>, kGenFunc); ...@@ -150,16 +170,17 @@ DEFINE_CONSTRUCTED_FLAG(String, &TestMakeDflt<String>, kGenFunc);
DEFINE_CONSTRUCTED_FLAG(UDT, &TestMakeDflt<UDT>, kGenFunc); DEFINE_CONSTRUCTED_FLAG(UDT, &TestMakeDflt<UDT>, kGenFunc);
template <typename T> template <typename T>
bool TestConstructionFor(const flags::Flag<T>& f1, flags::Flag<T>& f2) { bool TestConstructionFor(const absl::Flag<T>& f1, absl::Flag<T>& f2) {
EXPECT_EQ(f1.Name(), "f1"); EXPECT_EQ(absl::GetFlagReflectionHandle(f1).Name(), "f1");
EXPECT_EQ(f1.Help(), "literal help"); EXPECT_EQ(absl::GetFlagReflectionHandle(f1).Help(), "literal help");
EXPECT_EQ(f1.Filename(), "file"); EXPECT_EQ(absl::GetFlagReflectionHandle(f1).Filename(), "file");
flags::FlagRegistrar<T, false>(f2).OnUpdate(TestCallback); flags::FlagRegistrar<T, false>(ABSL_FLAG_IMPL_FLAG_PTR(f2))
.OnUpdate(TestCallback);
EXPECT_EQ(f2.Name(), "f2"); EXPECT_EQ(absl::GetFlagReflectionHandle(f2).Name(), "f2");
EXPECT_EQ(f2.Help(), "dynamic help"); EXPECT_EQ(absl::GetFlagReflectionHandle(f2).Help(), "dynamic help");
EXPECT_EQ(f2.Filename(), "file"); EXPECT_EQ(absl::GetFlagReflectionHandle(f2).Filename(), "file");
return true; return true;
} }
...@@ -203,18 +224,30 @@ namespace { ...@@ -203,18 +224,30 @@ namespace {
TEST_F(FlagTest, TestFlagDeclaration) { TEST_F(FlagTest, TestFlagDeclaration) {
// test that we can access flag objects. // test that we can access flag objects.
EXPECT_EQ(FLAGS_test_flag_01.Name(), "test_flag_01"); EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_01).Name(),
EXPECT_EQ(FLAGS_test_flag_02.Name(), "test_flag_02"); "test_flag_01");
EXPECT_EQ(FLAGS_test_flag_03.Name(), "test_flag_03"); EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_02).Name(),
EXPECT_EQ(FLAGS_test_flag_04.Name(), "test_flag_04"); "test_flag_02");
EXPECT_EQ(FLAGS_test_flag_05.Name(), "test_flag_05"); EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_03).Name(),
EXPECT_EQ(FLAGS_test_flag_06.Name(), "test_flag_06"); "test_flag_03");
EXPECT_EQ(FLAGS_test_flag_07.Name(), "test_flag_07"); EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_04).Name(),
EXPECT_EQ(FLAGS_test_flag_08.Name(), "test_flag_08"); "test_flag_04");
EXPECT_EQ(FLAGS_test_flag_09.Name(), "test_flag_09"); EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_05).Name(),
EXPECT_EQ(FLAGS_test_flag_10.Name(), "test_flag_10"); "test_flag_05");
EXPECT_EQ(FLAGS_test_flag_11.Name(), "test_flag_11"); EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_06).Name(),
EXPECT_EQ(FLAGS_test_flag_12.Name(), "test_flag_12"); "test_flag_06");
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_07).Name(),
"test_flag_07");
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_08).Name(),
"test_flag_08");
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_09).Name(),
"test_flag_09");
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_10).Name(),
"test_flag_10");
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_11).Name(),
"test_flag_11");
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_12).Name(),
"test_flag_12");
} }
#endif // !ABSL_FLAGS_STRIP_NAMES #endif // !ABSL_FLAGS_STRIP_NAMES
...@@ -241,96 +274,168 @@ namespace { ...@@ -241,96 +274,168 @@ namespace {
TEST_F(FlagTest, TestFlagDefinition) { TEST_F(FlagTest, TestFlagDefinition) {
absl::string_view expected_file_name = "absl/flags/flag_test.cc"; absl::string_view expected_file_name = "absl/flags/flag_test.cc";
EXPECT_EQ(FLAGS_test_flag_01.Name(), "test_flag_01"); EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_01).Name(),
EXPECT_EQ(FLAGS_test_flag_01.Help(), "test flag 01"); "test_flag_01");
EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_01.Filename(), expected_file_name)) EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_01).Help(),
<< FLAGS_test_flag_01.Filename(); "test flag 01");
EXPECT_TRUE(absl::EndsWith(
EXPECT_EQ(FLAGS_test_flag_02.Name(), "test_flag_02"); absl::GetFlagReflectionHandle(FLAGS_test_flag_01).Filename(),
EXPECT_EQ(FLAGS_test_flag_02.Help(), "test flag 02"); expected_file_name))
EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_02.Filename(), expected_file_name)) << absl::GetFlagReflectionHandle(FLAGS_test_flag_01).Filename();
<< FLAGS_test_flag_02.Filename();
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_02).Name(),
EXPECT_EQ(FLAGS_test_flag_03.Name(), "test_flag_03"); "test_flag_02");
EXPECT_EQ(FLAGS_test_flag_03.Help(), "test flag 03"); EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_02).Help(),
EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_03.Filename(), expected_file_name)) "test flag 02");
<< FLAGS_test_flag_03.Filename(); EXPECT_TRUE(absl::EndsWith(
absl::GetFlagReflectionHandle(FLAGS_test_flag_02).Filename(),
EXPECT_EQ(FLAGS_test_flag_04.Name(), "test_flag_04"); expected_file_name))
EXPECT_EQ(FLAGS_test_flag_04.Help(), "test flag 04"); << absl::GetFlagReflectionHandle(FLAGS_test_flag_02).Filename();
EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_04.Filename(), expected_file_name))
<< FLAGS_test_flag_04.Filename(); EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_03).Name(),
"test_flag_03");
EXPECT_EQ(FLAGS_test_flag_05.Name(), "test_flag_05"); EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_03).Help(),
EXPECT_EQ(FLAGS_test_flag_05.Help(), "test flag 05"); "test flag 03");
EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_05.Filename(), expected_file_name)) EXPECT_TRUE(absl::EndsWith(
<< FLAGS_test_flag_05.Filename(); absl::GetFlagReflectionHandle(FLAGS_test_flag_03).Filename(),
expected_file_name))
EXPECT_EQ(FLAGS_test_flag_06.Name(), "test_flag_06"); << absl::GetFlagReflectionHandle(FLAGS_test_flag_03).Filename();
EXPECT_EQ(FLAGS_test_flag_06.Help(), "test flag 06");
EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_06.Filename(), expected_file_name)) EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_04).Name(),
<< FLAGS_test_flag_06.Filename(); "test_flag_04");
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_04).Help(),
EXPECT_EQ(FLAGS_test_flag_07.Name(), "test_flag_07"); "test flag 04");
EXPECT_EQ(FLAGS_test_flag_07.Help(), "test flag 07"); EXPECT_TRUE(absl::EndsWith(
EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_07.Filename(), expected_file_name)) absl::GetFlagReflectionHandle(FLAGS_test_flag_04).Filename(),
<< FLAGS_test_flag_07.Filename(); expected_file_name))
<< absl::GetFlagReflectionHandle(FLAGS_test_flag_04).Filename();
EXPECT_EQ(FLAGS_test_flag_08.Name(), "test_flag_08");
EXPECT_EQ(FLAGS_test_flag_08.Help(), "test flag 08"); EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_05).Name(),
EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_08.Filename(), expected_file_name)) "test_flag_05");
<< FLAGS_test_flag_08.Filename(); EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_05).Help(),
"test flag 05");
EXPECT_EQ(FLAGS_test_flag_09.Name(), "test_flag_09"); EXPECT_TRUE(absl::EndsWith(
EXPECT_EQ(FLAGS_test_flag_09.Help(), "test flag 09"); absl::GetFlagReflectionHandle(FLAGS_test_flag_05).Filename(),
EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_09.Filename(), expected_file_name)) expected_file_name))
<< FLAGS_test_flag_09.Filename(); << absl::GetFlagReflectionHandle(FLAGS_test_flag_05).Filename();
EXPECT_EQ(FLAGS_test_flag_10.Name(), "test_flag_10"); EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_06).Name(),
EXPECT_EQ(FLAGS_test_flag_10.Help(), "test flag 10"); "test_flag_06");
EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_10.Filename(), expected_file_name)) EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_06).Help(),
<< FLAGS_test_flag_10.Filename(); "test flag 06");
EXPECT_TRUE(absl::EndsWith(
EXPECT_EQ(FLAGS_test_flag_11.Name(), "test_flag_11"); absl::GetFlagReflectionHandle(FLAGS_test_flag_06).Filename(),
EXPECT_EQ(FLAGS_test_flag_11.Help(), "test flag 11"); expected_file_name))
EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_11.Filename(), expected_file_name)) << absl::GetFlagReflectionHandle(FLAGS_test_flag_06).Filename();
<< FLAGS_test_flag_11.Filename();
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_07).Name(),
EXPECT_EQ(FLAGS_test_flag_12.Name(), "test_flag_12"); "test_flag_07");
EXPECT_EQ(FLAGS_test_flag_12.Help(), "test flag 12"); EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_07).Help(),
EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_12.Filename(), expected_file_name)) "test flag 07");
<< FLAGS_test_flag_12.Filename(); EXPECT_TRUE(absl::EndsWith(
absl::GetFlagReflectionHandle(FLAGS_test_flag_07).Filename(),
expected_file_name))
<< absl::GetFlagReflectionHandle(FLAGS_test_flag_07).Filename();
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_08).Name(),
"test_flag_08");
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_08).Help(),
"test flag 08");
EXPECT_TRUE(absl::EndsWith(
absl::GetFlagReflectionHandle(FLAGS_test_flag_08).Filename(),
expected_file_name))
<< absl::GetFlagReflectionHandle(FLAGS_test_flag_08).Filename();
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_09).Name(),
"test_flag_09");
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_09).Help(),
"test flag 09");
EXPECT_TRUE(absl::EndsWith(
absl::GetFlagReflectionHandle(FLAGS_test_flag_09).Filename(),
expected_file_name))
<< absl::GetFlagReflectionHandle(FLAGS_test_flag_09).Filename();
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_10).Name(),
"test_flag_10");
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_10).Help(),
"test flag 10");
EXPECT_TRUE(absl::EndsWith(
absl::GetFlagReflectionHandle(FLAGS_test_flag_10).Filename(),
expected_file_name))
<< absl::GetFlagReflectionHandle(FLAGS_test_flag_10).Filename();
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_11).Name(),
"test_flag_11");
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_11).Help(),
"test flag 11");
EXPECT_TRUE(absl::EndsWith(
absl::GetFlagReflectionHandle(FLAGS_test_flag_11).Filename(),
expected_file_name))
<< absl::GetFlagReflectionHandle(FLAGS_test_flag_11).Filename();
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_12).Name(),
"test_flag_12");
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_12).Help(),
"test flag 12");
EXPECT_TRUE(absl::EndsWith(
absl::GetFlagReflectionHandle(FLAGS_test_flag_12).Filename(),
expected_file_name))
<< absl::GetFlagReflectionHandle(FLAGS_test_flag_12).Filename();
} }
#endif // !ABSL_FLAGS_STRIP_NAMES #endif // !ABSL_FLAGS_STRIP_NAMES
// -------------------------------------------------------------------- // --------------------------------------------------------------------
TEST_F(FlagTest, TestDefault) { TEST_F(FlagTest, TestDefault) {
EXPECT_EQ(FLAGS_test_flag_01.DefaultValue(), "true"); EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_01).DefaultValue(),
EXPECT_EQ(FLAGS_test_flag_02.DefaultValue(), "1234"); "true");
EXPECT_EQ(FLAGS_test_flag_03.DefaultValue(), "-34"); EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_02).DefaultValue(),
EXPECT_EQ(FLAGS_test_flag_04.DefaultValue(), "189"); "1234");
EXPECT_EQ(FLAGS_test_flag_05.DefaultValue(), "10765"); EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_03).DefaultValue(),
EXPECT_EQ(FLAGS_test_flag_06.DefaultValue(), "40000"); "-34");
EXPECT_EQ(FLAGS_test_flag_07.DefaultValue(), "-1234567"); EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_04).DefaultValue(),
EXPECT_EQ(FLAGS_test_flag_08.DefaultValue(), "9876543"); "189");
EXPECT_EQ(FLAGS_test_flag_09.DefaultValue(), "-9.876e-50"); EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_05).DefaultValue(),
EXPECT_EQ(FLAGS_test_flag_10.DefaultValue(), "1.234e+12"); "10765");
EXPECT_EQ(FLAGS_test_flag_11.DefaultValue(), ""); EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_06).DefaultValue(),
EXPECT_EQ(FLAGS_test_flag_12.DefaultValue(), "10m"); "40000");
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_07).DefaultValue(),
EXPECT_EQ(FLAGS_test_flag_01.CurrentValue(), "true"); "-1234567");
EXPECT_EQ(FLAGS_test_flag_02.CurrentValue(), "1234"); EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_08).DefaultValue(),
EXPECT_EQ(FLAGS_test_flag_03.CurrentValue(), "-34"); "9876543");
EXPECT_EQ(FLAGS_test_flag_04.CurrentValue(), "189"); EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_09).DefaultValue(),
EXPECT_EQ(FLAGS_test_flag_05.CurrentValue(), "10765"); "-9.876e-50");
EXPECT_EQ(FLAGS_test_flag_06.CurrentValue(), "40000"); EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_10).DefaultValue(),
EXPECT_EQ(FLAGS_test_flag_07.CurrentValue(), "-1234567"); "1.234e+12");
EXPECT_EQ(FLAGS_test_flag_08.CurrentValue(), "9876543"); EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_11).DefaultValue(),
EXPECT_EQ(FLAGS_test_flag_09.CurrentValue(), "-9.876e-50"); "");
EXPECT_EQ(FLAGS_test_flag_10.CurrentValue(), "1.234e+12"); EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_12).DefaultValue(),
EXPECT_EQ(FLAGS_test_flag_11.CurrentValue(), ""); "10m");
EXPECT_EQ(FLAGS_test_flag_12.CurrentValue(), "10m");
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_01).CurrentValue(),
"true");
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_02).CurrentValue(),
"1234");
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_03).CurrentValue(),
"-34");
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_04).CurrentValue(),
"189");
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_05).CurrentValue(),
"10765");
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_06).CurrentValue(),
"40000");
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_07).CurrentValue(),
"-1234567");
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_08).CurrentValue(),
"9876543");
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_09).CurrentValue(),
"-9.876e-50");
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_10).CurrentValue(),
"1.234e+12");
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_11).CurrentValue(),
"");
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_12).CurrentValue(),
"10m");
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_01), true); EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_01), true);
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_02), 1234); EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_02), 1234);
...@@ -385,12 +490,18 @@ ABSL_FLAG(NonTriviallyCopyableAggregate, test_flag_eb_06, {}, ""); ...@@ -385,12 +490,18 @@ ABSL_FLAG(NonTriviallyCopyableAggregate, test_flag_eb_06, {}, "");
namespace { namespace {
TEST_F(FlagTest, TestEmptyBracesDefault) { TEST_F(FlagTest, TestEmptyBracesDefault) {
EXPECT_EQ(FLAGS_test_flag_eb_01.DefaultValue(), "false"); EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_eb_01).DefaultValue(),
EXPECT_EQ(FLAGS_test_flag_eb_02.DefaultValue(), "0"); "false");
EXPECT_EQ(FLAGS_test_flag_eb_03.DefaultValue(), "0"); EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_eb_02).DefaultValue(),
EXPECT_EQ(FLAGS_test_flag_eb_04.DefaultValue(), "0"); "0");
EXPECT_EQ(FLAGS_test_flag_eb_05.DefaultValue(), ""); EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_eb_03).DefaultValue(),
EXPECT_EQ(FLAGS_test_flag_eb_06.DefaultValue(), "0"); "0");
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_eb_04).DefaultValue(),
"0");
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_eb_05).DefaultValue(),
"");
EXPECT_EQ(absl::GetFlagReflectionHandle(FLAGS_test_flag_eb_06).DefaultValue(),
"0");
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_eb_01), false); EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_eb_01), false);
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_eb_02), 0); EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_eb_02), 0);
...@@ -500,8 +611,9 @@ namespace { ...@@ -500,8 +611,9 @@ namespace {
#if !ABSL_FLAGS_STRIP_HELP #if !ABSL_FLAGS_STRIP_HELP
TEST_F(FlagTest, TestNonConstexprHelp) { TEST_F(FlagTest, TestNonConstexprHelp) {
EXPECT_EQ(FLAGS_test_flag_with_non_const_help.Help(), EXPECT_EQ(
"test flag non const help"); absl::GetFlagReflectionHandle(FLAGS_test_flag_with_non_const_help).Help(),
"test flag non const help");
} }
#endif //! ABSL_FLAGS_STRIP_HELP #endif //! ABSL_FLAGS_STRIP_HELP
......
...@@ -16,13 +16,8 @@ ...@@ -16,13 +16,8 @@
#ifndef ABSL_FLAGS_INTERNAL_COMMANDLINEFLAG_H_ #ifndef ABSL_FLAGS_INTERNAL_COMMANDLINEFLAG_H_
#define ABSL_FLAGS_INTERNAL_COMMANDLINEFLAG_H_ #define ABSL_FLAGS_INTERNAL_COMMANDLINEFLAG_H_
#include <memory>
#include <string>
#include "absl/base/config.h" #include "absl/base/config.h"
#include "absl/base/internal/fast_type_id.h" #include "absl/base/internal/fast_type_id.h"
#include "absl/base/macros.h"
#include "absl/strings/string_view.h"
namespace absl { namespace absl {
ABSL_NAMESPACE_BEGIN ABSL_NAMESPACE_BEGIN
...@@ -33,7 +28,7 @@ namespace flags_internal { ...@@ -33,7 +28,7 @@ namespace flags_internal {
// cases this id is enough to uniquely identify the flag's value type. In a few // cases this id is enough to uniquely identify the flag's value type. In a few
// cases we'll have to resort to using actual RTTI implementation if it is // cases we'll have to resort to using actual RTTI implementation if it is
// available. // available.
using FlagFastTypeId = base_internal::FastTypeIdType; using FlagFastTypeId = absl::base_internal::FastTypeIdType;
// Options that control SetCommandLineOptionWithMode. // Options that control SetCommandLineOptionWithMode.
enum FlagSettingMode { enum FlagSettingMode {
......
...@@ -15,21 +15,26 @@ ...@@ -15,21 +15,26 @@
#include "absl/flags/internal/flag.h" #include "absl/flags/internal/flag.h"
#include <assert.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <array>
#include <atomic> #include <atomic>
#include <memory> #include <memory>
#include <new>
#include <string> #include <string>
#include <vector> #include <typeinfo>
#include "absl/base/attributes.h" #include "absl/base/call_once.h"
#include "absl/base/casts.h" #include "absl/base/casts.h"
#include "absl/base/config.h" #include "absl/base/config.h"
#include "absl/base/const_init.h"
#include "absl/base/optimization.h" #include "absl/base/optimization.h"
#include "absl/flags/config.h"
#include "absl/flags/internal/commandlineflag.h"
#include "absl/flags/usage_config.h" #include "absl/flags/usage_config.h"
#include "absl/memory/memory.h"
#include "absl/strings/str_cat.h" #include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
#include "absl/synchronization/mutex.h" #include "absl/synchronization/mutex.h"
......
...@@ -16,31 +16,36 @@ ...@@ -16,31 +16,36 @@
#ifndef ABSL_FLAGS_INTERNAL_FLAG_H_ #ifndef ABSL_FLAGS_INTERNAL_FLAG_H_
#define ABSL_FLAGS_INTERNAL_FLAG_H_ #define ABSL_FLAGS_INTERNAL_FLAG_H_
#include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <atomic> #include <atomic>
#include <cstring> #include <cstring>
#include <memory> #include <memory>
#include <new>
#include <string> #include <string>
#include <type_traits> #include <type_traits>
#include <typeinfo> #include <typeinfo>
#include "absl/base/attributes.h"
#include "absl/base/call_once.h" #include "absl/base/call_once.h"
#include "absl/base/config.h" #include "absl/base/config.h"
#include "absl/base/optimization.h"
#include "absl/base/thread_annotations.h" #include "absl/base/thread_annotations.h"
#include "absl/flags/commandlineflag.h" #include "absl/flags/commandlineflag.h"
#include "absl/flags/config.h" #include "absl/flags/config.h"
#include "absl/flags/internal/commandlineflag.h"
#include "absl/flags/internal/registry.h" #include "absl/flags/internal/registry.h"
#include "absl/flags/marshalling.h" #include "absl/flags/marshalling.h"
#include "absl/memory/memory.h"
#include "absl/meta/type_traits.h" #include "absl/meta/type_traits.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
#include "absl/synchronization/mutex.h" #include "absl/synchronization/mutex.h"
#include "absl/utility/utility.h"
namespace absl { namespace absl {
ABSL_NAMESPACE_BEGIN ABSL_NAMESPACE_BEGIN
///////////////////////////////////////////////////////////////////////////////
// Forward declaration of absl::Flag<T> public API. // Forward declaration of absl::Flag<T> public API.
namespace flags_internal { namespace flags_internal {
template <typename T> template <typename T>
...@@ -64,12 +69,15 @@ void SetFlag(absl::Flag<T>* flag, const T& v); ...@@ -64,12 +69,15 @@ void SetFlag(absl::Flag<T>* flag, const T& v);
template <typename T, typename V> template <typename T, typename V>
void SetFlag(absl::Flag<T>* flag, const V& v); void SetFlag(absl::Flag<T>* flag, const V& v);
namespace flags_internal { template <typename U>
const CommandLineFlag& GetFlagReflectionHandle(const absl::Flag<U>& f);
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Flag value type operations, eg., parsing, copying, etc. are provided // Flag value type operations, eg., parsing, copying, etc. are provided
// by function specific to that type with a signature matching FlagOpFn. // by function specific to that type with a signature matching FlagOpFn.
namespace flags_internal {
enum class FlagOp { enum class FlagOp {
kAlloc, kAlloc,
kDelete, kDelete,
...@@ -659,6 +667,13 @@ class Flag { ...@@ -659,6 +667,13 @@ class Flag {
impl_.Write(&v); impl_.Write(&v);
} }
template <typename U>
friend const CommandLineFlag& absl::GetFlagReflectionHandle(
const absl::Flag<U>& f);
// Access to the reflection.
const CommandLineFlag& Reflect() const { return impl_; }
// Flag's data // Flag's data
// The implementation depends on value_ field to be placed exactly after the // The implementation depends on value_ field to be placed exactly after the
// impl_ field, so that impl_ can figure out the offset to the value and // impl_ field, so that impl_ can figure out the offset to the value and
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "absl/base/config.h" #include "absl/base/config.h"
#include "absl/flags/declare.h" #include "absl/flags/declare.h"
#include "absl/strings/string_view.h"
ABSL_DECLARE_FLAG(std::vector<std::string>, flagfile); ABSL_DECLARE_FLAG(std::vector<std::string>, flagfile);
ABSL_DECLARE_FLAG(std::vector<std::string>, fromenv); ABSL_DECLARE_FLAG(std::vector<std::string>, fromenv);
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
#define ABSL_FLAGS_INTERNAL_PATH_UTIL_H_ #define ABSL_FLAGS_INTERNAL_PATH_UTIL_H_
#include "absl/base/config.h" #include "absl/base/config.h"
#include "absl/strings/match.h"
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
namespace absl { namespace absl {
......
...@@ -15,6 +15,14 @@ ...@@ -15,6 +15,14 @@
#include "absl/flags/internal/private_handle_accessor.h" #include "absl/flags/internal/private_handle_accessor.h"
#include <memory>
#include <string>
#include "absl/base/config.h"
#include "absl/flags/commandlineflag.h"
#include "absl/flags/internal/commandlineflag.h"
#include "absl/strings/string_view.h"
namespace absl { namespace absl {
ABSL_NAMESPACE_BEGIN ABSL_NAMESPACE_BEGIN
namespace flags_internal { namespace flags_internal {
......
...@@ -16,7 +16,13 @@ ...@@ -16,7 +16,13 @@
#ifndef ABSL_FLAGS_INTERNAL_PRIVATE_HANDLE_ACCESSOR_H_ #ifndef ABSL_FLAGS_INTERNAL_PRIVATE_HANDLE_ACCESSOR_H_
#define ABSL_FLAGS_INTERNAL_PRIVATE_HANDLE_ACCESSOR_H_ #define ABSL_FLAGS_INTERNAL_PRIVATE_HANDLE_ACCESSOR_H_
#include <memory>
#include <string>
#include "absl/base/config.h"
#include "absl/flags/commandlineflag.h" #include "absl/flags/commandlineflag.h"
#include "absl/flags/internal/commandlineflag.h"
#include "absl/strings/string_view.h"
namespace absl { namespace absl {
ABSL_NAMESPACE_BEGIN ABSL_NAMESPACE_BEGIN
......
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include "absl/base/internal/raw_logging.h" #include "absl/base/internal/raw_logging.h"
#include "absl/base/thread_annotations.h" #include "absl/base/thread_annotations.h"
#include "absl/flags/commandlineflag.h" #include "absl/flags/commandlineflag.h"
#include "absl/flags/internal/commandlineflag.h"
#include "absl/flags/internal/private_handle_accessor.h" #include "absl/flags/internal/private_handle_accessor.h"
#include "absl/flags/usage_config.h" #include "absl/flags/usage_config.h"
#include "absl/strings/str_cat.h" #include "absl/strings/str_cat.h"
......
...@@ -17,11 +17,9 @@ ...@@ -17,11 +17,9 @@
#define ABSL_FLAGS_INTERNAL_REGISTRY_H_ #define ABSL_FLAGS_INTERNAL_REGISTRY_H_
#include <functional> #include <functional>
#include <map>
#include <string>
#include "absl/base/config.h" #include "absl/base/config.h"
#include "absl/base/macros.h" #include "absl/flags/commandlineflag.h"
#include "absl/flags/internal/commandlineflag.h" #include "absl/flags/internal/commandlineflag.h"
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
...@@ -30,8 +28,6 @@ ...@@ -30,8 +28,6 @@
namespace absl { namespace absl {
ABSL_NAMESPACE_BEGIN ABSL_NAMESPACE_BEGIN
class CommandLineFlag;
namespace flags_internal { namespace flags_internal {
CommandLineFlag* FindCommandLineFlag(absl::string_view name); CommandLineFlag* FindCommandLineFlag(absl::string_view name);
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "absl/base/config.h" #include "absl/base/config.h"
#include "absl/flags/commandlineflag.h" #include "absl/flags/commandlineflag.h"
#include "absl/flags/internal/commandlineflag.h"
#include "absl/flags/internal/registry.h" #include "absl/flags/internal/registry.h"
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
......
...@@ -20,8 +20,8 @@ ...@@ -20,8 +20,8 @@
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "absl/flags/flag.h" #include "absl/flags/flag.h"
#include "absl/flags/internal/commandlineflag.h"
#include "absl/flags/internal/registry.h" #include "absl/flags/internal/registry.h"
#include "absl/flags/marshalling.h"
#include "absl/memory/memory.h" #include "absl/memory/memory.h"
ABSL_FLAG(int, int_flag, 1, "int_flag help"); ABSL_FLAG(int, int_flag, 1, "int_flag help");
......
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
#include "absl/flags/internal/usage.h" #include "absl/flags/internal/usage.h"
#include <stdint.h>
#include <functional> #include <functional>
#include <map> #include <map>
#include <ostream> #include <ostream>
......
...@@ -21,7 +21,6 @@ ...@@ -21,7 +21,6 @@
#include <string> #include <string>
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "absl/flags/declare.h"
#include "absl/flags/flag.h" #include "absl/flags/flag.h"
#include "absl/flags/internal/parse.h" #include "absl/flags/internal/parse.h"
#include "absl/flags/internal/path_util.h" #include "absl/flags/internal/path_util.h"
...@@ -29,7 +28,6 @@ ...@@ -29,7 +28,6 @@
#include "absl/flags/internal/registry.h" #include "absl/flags/internal/registry.h"
#include "absl/flags/usage.h" #include "absl/flags/usage.h"
#include "absl/flags/usage_config.h" #include "absl/flags/usage_config.h"
#include "absl/memory/memory.h"
#include "absl/strings/match.h" #include "absl/strings/match.h"
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
......
...@@ -37,6 +37,7 @@ ...@@ -37,6 +37,7 @@
#include "absl/flags/commandlineflag.h" #include "absl/flags/commandlineflag.h"
#include "absl/flags/config.h" #include "absl/flags/config.h"
#include "absl/flags/flag.h" #include "absl/flags/flag.h"
#include "absl/flags/internal/commandlineflag.h"
#include "absl/flags/internal/flag.h" #include "absl/flags/internal/flag.h"
#include "absl/flags/internal/parse.h" #include "absl/flags/internal/parse.h"
#include "absl/flags/internal/private_handle_accessor.h" #include "absl/flags/internal/private_handle_accessor.h"
......
...@@ -23,7 +23,6 @@ ...@@ -23,7 +23,6 @@
#ifndef ABSL_FLAGS_PARSE_H_ #ifndef ABSL_FLAGS_PARSE_H_
#define ABSL_FLAGS_PARSE_H_ #define ABSL_FLAGS_PARSE_H_
#include <string>
#include <vector> #include <vector>
#include "absl/base/config.h" #include "absl/base/config.h"
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "absl/flags/usage_config.h" #include "absl/flags/usage_config.h"
#include <functional>
#include <iostream> #include <iostream>
#include <string> #include <string>
......
...@@ -98,26 +98,26 @@ bool ParseLenient(string_view s, CivilT* c) { ...@@ -98,26 +98,26 @@ bool ParseLenient(string_view s, CivilT* c) {
} // namespace } // namespace
std::string FormatCivilTime(CivilSecond c) { std::string FormatCivilTime(CivilSecond c) {
return FormatYearAnd("-%m-%dT%H:%M:%S", c); return FormatYearAnd("-%m-%d%ET%H:%M:%S", c);
} }
std::string FormatCivilTime(CivilMinute c) { std::string FormatCivilTime(CivilMinute c) {
return FormatYearAnd("-%m-%dT%H:%M", c); return FormatYearAnd("-%m-%d%ET%H:%M", c);
} }
std::string FormatCivilTime(CivilHour c) { std::string FormatCivilTime(CivilHour c) {
return FormatYearAnd("-%m-%dT%H", c); return FormatYearAnd("-%m-%d%ET%H", c);
} }
std::string FormatCivilTime(CivilDay c) { return FormatYearAnd("-%m-%d", c); } std::string FormatCivilTime(CivilDay c) { return FormatYearAnd("-%m-%d", c); }
std::string FormatCivilTime(CivilMonth c) { return FormatYearAnd("-%m", c); } std::string FormatCivilTime(CivilMonth c) { return FormatYearAnd("-%m", c); }
std::string FormatCivilTime(CivilYear c) { return FormatYearAnd("", c); } std::string FormatCivilTime(CivilYear c) { return FormatYearAnd("", c); }
bool ParseCivilTime(string_view s, CivilSecond* c) { bool ParseCivilTime(string_view s, CivilSecond* c) {
return ParseYearAnd("-%m-%dT%H:%M:%S", s, c); return ParseYearAnd("-%m-%d%ET%H:%M:%S", s, c);
} }
bool ParseCivilTime(string_view s, CivilMinute* c) { bool ParseCivilTime(string_view s, CivilMinute* c) {
return ParseYearAnd("-%m-%dT%H:%M", s, c); return ParseYearAnd("-%m-%d%ET%H:%M", s, c);
} }
bool ParseCivilTime(string_view s, CivilHour* c) { bool ParseCivilTime(string_view s, CivilHour* c) {
return ParseYearAnd("-%m-%dT%H", s, c); return ParseYearAnd("-%m-%d%ET%H", s, c);
} }
bool ParseCivilTime(string_view s, CivilDay* c) { bool ParseCivilTime(string_view s, CivilDay* c) {
return ParseYearAnd("-%m-%d", s, c); return ParseYearAnd("-%m-%d", s, c);
......
...@@ -27,14 +27,11 @@ namespace cctz = absl::time_internal::cctz; ...@@ -27,14 +27,11 @@ namespace cctz = absl::time_internal::cctz;
namespace absl { namespace absl {
ABSL_NAMESPACE_BEGIN ABSL_NAMESPACE_BEGIN
ABSL_DLL extern const char RFC3339_full[] = ABSL_DLL extern const char RFC3339_full[] = "%Y-%m-%d%ET%H:%M:%E*S%Ez";
"%Y-%m-%dT%H:%M:%E*S%Ez"; ABSL_DLL extern const char RFC3339_sec[] = "%Y-%m-%d%ET%H:%M:%S%Ez";
ABSL_DLL extern const char RFC3339_sec[] = "%Y-%m-%dT%H:%M:%S%Ez";
ABSL_DLL extern const char RFC1123_full[] = "%a, %d %b %E4Y %H:%M:%S %z";
ABSL_DLL extern const char RFC1123_full[] = ABSL_DLL extern const char RFC1123_no_wday[] = "%d %b %E4Y %H:%M:%S %z";
"%a, %d %b %E4Y %H:%M:%S %z";
ABSL_DLL extern const char RFC1123_no_wday[] =
"%d %b %E4Y %H:%M:%S %z";
namespace { namespace {
......
...@@ -26,7 +26,7 @@ const char* const kFormats[] = { ...@@ -26,7 +26,7 @@ const char* const kFormats[] = {
absl::RFC1123_no_wday, // 1 absl::RFC1123_no_wday, // 1
absl::RFC3339_full, // 2 absl::RFC3339_full, // 2
absl::RFC3339_sec, // 3 absl::RFC3339_sec, // 3
"%Y-%m-%dT%H:%M:%S", // 4 "%Y-%m-%d%ET%H:%M:%S", // 4
"%Y-%m-%d", // 5 "%Y-%m-%d", // 5
}; };
const int kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]); const int kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]);
......
...@@ -322,7 +322,8 @@ inline std::string format(const std::string& fmt, const time_point<D>& tp, ...@@ -322,7 +322,8 @@ inline std::string format(const std::string& fmt, const time_point<D>& tp,
// returns the corresponding time_point. Uses strftime()-like formatting // returns the corresponding time_point. Uses strftime()-like formatting
// options, with the same extensions as cctz::format(), but with the // options, with the same extensions as cctz::format(), but with the
// exceptions that %E#S is interpreted as %E*S, and %E#f as %E*f. %Ez // exceptions that %E#S is interpreted as %E*S, and %E#f as %E*f. %Ez
// and %E*z also accept the same inputs. %ET accepts either 'T' or 't'. // and %E*z also accept the same inputs, which (along with %z) includes
// 'z' and 'Z' as synonyms for +00:00. %ET accepts either 'T' or 't'.
// //
// %Y consumes as many numeric characters as it can, so the matching data // %Y consumes as many numeric characters as it can, so the matching data
// should always be terminated with a non-numeric. %E4Y always consumes // should always be terminated with a non-numeric. %E4Y always consumes
......
...@@ -1203,18 +1203,15 @@ struct tm ToTM(Time t, TimeZone tz); ...@@ -1203,18 +1203,15 @@ struct tm ToTM(Time t, TimeZone tz);
// time with UTC offset. Also note the use of "%Y": RFC3339 mandates that // time with UTC offset. Also note the use of "%Y": RFC3339 mandates that
// years have exactly four digits, but we allow them to take their natural // years have exactly four digits, but we allow them to take their natural
// width. // width.
ABSL_DLL extern const char ABSL_DLL extern const char RFC3339_full[]; // %Y-%m-%d%ET%H:%M:%E*S%Ez
RFC3339_full[]; // %Y-%m-%dT%H:%M:%E*S%Ez ABSL_DLL extern const char RFC3339_sec[]; // %Y-%m-%d%ET%H:%M:%S%Ez
ABSL_DLL extern const char RFC3339_sec[]; // %Y-%m-%dT%H:%M:%S%Ez
// RFC1123_full // RFC1123_full
// RFC1123_no_wday // RFC1123_no_wday
// //
// FormatTime()/ParseTime() format specifiers for RFC1123 date/time strings. // FormatTime()/ParseTime() format specifiers for RFC1123 date/time strings.
ABSL_DLL extern const char ABSL_DLL extern const char RFC1123_full[]; // %a, %d %b %E4Y %H:%M:%S %z
RFC1123_full[]; // %a, %d %b %E4Y %H:%M:%S %z ABSL_DLL extern const char RFC1123_no_wday[]; // %d %b %E4Y %H:%M:%S %z
ABSL_DLL extern const char
RFC1123_no_wday[]; // %d %b %E4Y %H:%M:%S %z
// FormatTime() // FormatTime()
// //
...@@ -1229,6 +1226,7 @@ ABSL_DLL extern const char ...@@ -1229,6 +1226,7 @@ ABSL_DLL extern const char
// - %E#f - Fractional seconds with # digits of precision // - %E#f - Fractional seconds with # digits of precision
// - %E*f - Fractional seconds with full precision (a literal '*') // - %E*f - Fractional seconds with full precision (a literal '*')
// - %E4Y - Four-character years (-999 ... -001, 0000, 0001 ... 9999) // - %E4Y - Four-character years (-999 ... -001, 0000, 0001 ... 9999)
// - %ET - The RFC3339 "date-time" separator "T"
// //
// Note that %E0S behaves like %S, and %E0f produces no characters. In // Note that %E0S behaves like %S, and %E0f produces no characters. In
// contrast %E*f always produces at least one digit, which may be '0'. // contrast %E*f always produces at least one digit, which may be '0'.
...@@ -1271,7 +1269,8 @@ inline std::ostream& operator<<(std::ostream& os, Time t) { ...@@ -1271,7 +1269,8 @@ inline std::ostream& operator<<(std::ostream& os, Time t) {
// returns the corresponding `absl::Time`. Uses strftime()-like formatting // returns the corresponding `absl::Time`. Uses strftime()-like formatting
// options, with the same extensions as FormatTime(), but with the // options, with the same extensions as FormatTime(), but with the
// exceptions that %E#S is interpreted as %E*S, and %E#f as %E*f. %Ez // exceptions that %E#S is interpreted as %E*S, and %E#f as %E*f. %Ez
// and %E*z also accept the same inputs. // and %E*z also accept the same inputs, which (along with %z) includes
// 'z' and 'Z' as synonyms for +00:00. %ET accepts either 'T' or 't'.
// //
// %Y consumes as many numeric characters as it can, so the matching data // %Y consumes as many numeric characters as it can, so the matching data
// should always be terminated with a non-numeric. %E4Y always consumes // should always be terminated with a non-numeric. %E4Y always consumes
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment