123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- #ifndef BASE_LOCATION_H_
- #define BASE_LOCATION_H_
- #include <stddef.h>
- #include <cassert>
- #include <functional>
- #include <string>
- #include "base/base_export.h"
- #include "base/debug/debugging_buildflags.h"
- #include "base/hash/hash.h"
- #include "build/build_config.h"
- namespace base {
- #if defined(__clang__)
- #define SUPPORTS_LOCATION_BUILTINS \
- (__has_builtin(__builtin_FUNCTION) && __has_builtin(__builtin_FILE) && \
- __has_builtin(__builtin_LINE))
- #elif defined(COMPILER_GCC) && __GNUC__ >= 7
- #define SUPPORTS_LOCATION_BUILTINS 1
- #else
- #define SUPPORTS_LOCATION_BUILTINS 0
- #endif
- class BASE_EXPORT Location {
- public:
- Location();
- Location(const Location& other);
-
-
-
- Location(const char* file_name, const void* program_counter);
-
-
-
- Location(const char* function_name,
- const char* file_name,
- int line_number,
- const void* program_counter);
-
-
- bool operator==(const Location& other) const {
- return program_counter_ == other.program_counter_;
- }
-
-
-
- bool has_source_info() const { return function_name_ && file_name_; }
-
-
- const char* function_name() const { return function_name_; }
-
-
- const char* file_name() const { return file_name_; }
-
-
- int line_number() const { return line_number_; }
-
-
-
- const void* program_counter() const { return program_counter_; }
-
-
- std::string ToString() const;
- #if !BUILDFLAG(FROM_HERE_USES_LOCATION_BUILTINS)
- #if !BUILDFLAG(ENABLE_LOCATION_SOURCE)
- static Location CreateFromHere(const char* file_name);
- #else
- static Location CreateFromHere(const char* function_name,
- const char* file_name,
- int line_number);
- #endif
- #endif
- #if SUPPORTS_LOCATION_BUILTINS && BUILDFLAG(ENABLE_LOCATION_SOURCE)
- static Location Current(const char* function_name = __builtin_FUNCTION(),
- const char* file_name = __builtin_FILE(),
- int line_number = __builtin_LINE());
- #elif SUPPORTS_LOCATION_BUILTINS
- static Location Current(const char* file_name = __builtin_FILE());
- #else
- static Location Current();
- #endif
- private:
- const char* function_name_ = nullptr;
- const char* file_name_ = nullptr;
- int line_number_ = -1;
- const void* program_counter_ = nullptr;
- };
- BASE_EXPORT const void* GetProgramCounter();
- #if BUILDFLAG(FROM_HERE_USES_LOCATION_BUILTINS)
- #define FROM_HERE ::base::Location::Current()
- #elif BUILDFLAG(ENABLE_LOCATION_SOURCE)
- #define FROM_HERE ::base::Location::CreateFromHere(__func__, __FILE__, __LINE__)
- #else
- #define FROM_HERE ::base::Location::CreateFromHere(__FILE__)
- #endif
- }
- namespace std {
- template <>
- struct hash<::base::Location> {
- std::size_t operator()(const ::base::Location& loc) const {
- const void* program_counter = loc.program_counter();
- return base::FastHash(base::as_bytes(base::make_span(&program_counter, 1)));
- }
- };
- }
- #endif
|