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.
5 #ifndef BASE_LOCATION_H_
6 #define BASE_LOCATION_H_
10 #include "base/base_export.h"
11 #include "base/debug/debugging_buildflags.h"
12 #include "build/build_config.h"
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
26 #define SUPPORTS_LOCATION_BUILTINS 0
29 // Location provides basic info where of an object was constructed, or was
30 // significantly brought to life.
31 class BASE_EXPORT Location {
34 Location(const Location& other);
35 Location& operator=(const Location& other);
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);
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,
48 const void* program_counter);
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_;
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_; }
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_; }
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_; }
69 // Will be -1 for default initialized Location objects and when source names
71 int line_number() const { return line_number_; }
73 // The address of the code generating this Location object. Should always be
74 // valid except for default initialized Location objects, which will be
76 const void* program_counter() const { return program_counter_; }
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;
82 #if !BUILDFLAG(FROM_HERE_USES_LOCATION_BUILTINS)
83 #if !BUILDFLAG(ENABLE_LOCATION_SOURCE)
84 static Location CreateFromHere(const char* file_name);
86 static Location CreateFromHere(const char* function_name,
87 const char* file_name,
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());
99 static Location Current();
103 const char* function_name_ = nullptr;
104 const char* file_name_ = nullptr;
105 int line_number_ = -1;
106 const void* program_counter_ = nullptr;
109 BASE_EXPORT const void* GetProgramCounter();
111 #if BUILDFLAG(FROM_HERE_USES_LOCATION_BUILTINS)
113 #define FROM_HERE ::base::Location::Current()
115 // The macros defined here will expand to the current function.
116 #elif BUILDFLAG(ENABLE_LOCATION_SOURCE)
118 // Full source information should be included.
119 #define FROM_HERE ::base::Location::CreateFromHere(__func__, __FILE__, __LINE__)
123 // TODO(http://crbug.com/760702) remove the __FILE__ argument from these calls.
124 #define FROM_HERE ::base::Location::CreateFromHere(__FILE__)
130 #endif // BASE_LOCATION_H_