location.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_LOCATION_H_
  5. #define BASE_LOCATION_H_
  6. #include <stddef.h>
  7. #include <cassert>
  8. #include <functional>
  9. #include <string>
  10. #include "base/base_export.h"
  11. #include "base/debug/debugging_buildflags.h"
  12. #include "base/hash/hash.h"
  13. #include "build/build_config.h"
  14. namespace base {
  15. #if defined(__clang__)
  16. // Clang allows detection of these builtins.
  17. #define SUPPORTS_LOCATION_BUILTINS \
  18. (__has_builtin(__builtin_FUNCTION) && __has_builtin(__builtin_FILE) && \
  19. __has_builtin(__builtin_LINE))
  20. #elif defined(COMPILER_GCC) && __GNUC__ >= 7
  21. // GCC has supported these for a long time, but they point at the function
  22. // declaration in the case of default arguments, rather than at the call site.
  23. #define SUPPORTS_LOCATION_BUILTINS 1
  24. #else
  25. #define SUPPORTS_LOCATION_BUILTINS 0
  26. #endif
  27. // Location provides basic info where of an object was constructed, or was
  28. // significantly brought to life.
  29. class BASE_EXPORT Location {
  30. public:
  31. Location();
  32. Location(const Location& other);
  33. // Only initializes the file name and program counter, the source information
  34. // will be null for the strings, and -1 for the line number.
  35. // TODO(http://crbug.com/760702) remove file name from this constructor.
  36. Location(const char* file_name, const void* program_counter);
  37. // Constructor should be called with a long-lived char*, such as __FILE__.
  38. // It assumes the provided value will persist as a global constant, and it
  39. // will not make a copy of it.
  40. Location(const char* function_name,
  41. const char* file_name,
  42. int line_number,
  43. const void* program_counter);
  44. // Comparator for hash map insertion. The program counter should uniquely
  45. // identify a location.
  46. bool operator==(const Location& other) const {
  47. return program_counter_ == other.program_counter_;
  48. }
  49. // Returns true if there is source code location info. If this is false,
  50. // the Location object only contains a program counter or is
  51. // default-initialized (the program counter is also null).
  52. bool has_source_info() const { return function_name_ && file_name_; }
  53. // Will be nullptr for default initialized Location objects and when source
  54. // names are disabled.
  55. const char* function_name() const { return function_name_; }
  56. // Will be nullptr for default initialized Location objects and when source
  57. // names are disabled.
  58. const char* file_name() const { return file_name_; }
  59. // Will be -1 for default initialized Location objects and when source names
  60. // are disabled.
  61. int line_number() const { return line_number_; }
  62. // The address of the code generating this Location object. Should always be
  63. // valid except for default initialized Location objects, which will be
  64. // nullptr.
  65. const void* program_counter() const { return program_counter_; }
  66. // Converts to the most user-readable form possible. If function and filename
  67. // are not available, this will return "pc:<hex address>".
  68. std::string ToString() const;
  69. #if !BUILDFLAG(FROM_HERE_USES_LOCATION_BUILTINS)
  70. #if !BUILDFLAG(ENABLE_LOCATION_SOURCE)
  71. static Location CreateFromHere(const char* file_name);
  72. #else
  73. static Location CreateFromHere(const char* function_name,
  74. const char* file_name,
  75. int line_number);
  76. #endif
  77. #endif
  78. #if SUPPORTS_LOCATION_BUILTINS && BUILDFLAG(ENABLE_LOCATION_SOURCE)
  79. static Location Current(const char* function_name = __builtin_FUNCTION(),
  80. const char* file_name = __builtin_FILE(),
  81. int line_number = __builtin_LINE());
  82. #elif SUPPORTS_LOCATION_BUILTINS
  83. static Location Current(const char* file_name = __builtin_FILE());
  84. #else
  85. static Location Current();
  86. #endif
  87. private:
  88. const char* function_name_ = nullptr;
  89. const char* file_name_ = nullptr;
  90. int line_number_ = -1;
  91. const void* program_counter_ = nullptr;
  92. };
  93. BASE_EXPORT const void* GetProgramCounter();
  94. #if BUILDFLAG(FROM_HERE_USES_LOCATION_BUILTINS)
  95. #define FROM_HERE ::base::Location::Current()
  96. // The macros defined here will expand to the current function.
  97. #elif BUILDFLAG(ENABLE_LOCATION_SOURCE)
  98. // Full source information should be included.
  99. #define FROM_HERE ::base::Location::CreateFromHere(__func__, __FILE__, __LINE__)
  100. #else
  101. // TODO(http://crbug.com/760702) remove the __FILE__ argument from these calls.
  102. #define FROM_HERE ::base::Location::CreateFromHere(__FILE__)
  103. #endif
  104. } // namespace base
  105. namespace std {
  106. // Specialization for using Location in hash tables.
  107. template <>
  108. struct hash<::base::Location> {
  109. std::size_t operator()(const ::base::Location& loc) const {
  110. const void* program_counter = loc.program_counter();
  111. return base::FastHash(base::as_bytes(base::make_span(&program_counter, 1)));
  112. }
  113. };
  114. } // namespace std
  115. #endif // BASE_LOCATION_H_