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