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 #include "base/location.h"
7 // location.h is a widely included header and its size can significantly impact
8 // build time. Try not to raise this limit unless absolutely necessary. See
9 // https://chromium.googlesource.com/chromium/src/+/HEAD/docs/wmax_tokens.md
11 #pragma clang max_tokens_here 240000
14 #if defined(COMPILER_MSVC)
18 #include "base/compiler_specific.h"
19 #include "base/strings/string_number_conversions.h"
20 #include "base/strings/stringprintf.h"
21 #include "build/build_config.h"
27 // Returns the length of the given null terminated c-string.
28 constexpr size_t StrLen(const char* str) {
30 for (str_len = 0; str[str_len] != '\0'; ++str_len)
35 // Finds the length of the build folder prefix from the file path.
36 // TODO(ssid): Strip prefixes from stored strings in the binary. This code only
37 // skips the prefix while reading the file name strings at runtime.
38 constexpr size_t StrippedFilePathPrefixLength() {
39 constexpr char path[] = __FILE__;
40 // Only keep the file path starting from the src directory.
41 constexpr char stripped[] = "base/location.cc";
42 constexpr size_t path_len = StrLen(path);
43 constexpr size_t stripped_len = StrLen(stripped);
44 static_assert(path_len >= stripped_len,
45 "Invalid file path for base/location.cc.");
46 return path_len - stripped_len;
49 constexpr size_t kStrippedPrefixLength = StrippedFilePathPrefixLength();
51 // Returns true if the |name| string has |prefix_len| characters in the prefix
52 // and the suffix matches the |expected| string.
53 // TODO(ssid): With C++20 we can make base::EndsWith() constexpr and use it
55 constexpr bool StrEndsWith(const char* name,
57 const char* expected) {
58 const size_t name_len = StrLen(name);
59 const size_t expected_len = StrLen(expected);
60 if (name_len != prefix_len + expected_len)
62 for (size_t i = 0; i < expected_len; ++i) {
63 if (name[i + prefix_len] != expected[i])
69 static_assert(StrEndsWith(__FILE__, kStrippedPrefixLength, "base/location.cc"),
70 "The file name does not match the expected prefix format.");
74 Location::Location() = default;
75 Location::Location(const Location& other) = default;
76 Location& Location::operator=(const Location& other) = default;
78 Location::Location(const char* file_name, const void* program_counter)
79 : file_name_(file_name), program_counter_(program_counter) {}
81 Location::Location(const char* function_name,
82 const char* file_name,
84 const void* program_counter)
85 : function_name_(function_name),
86 file_name_(file_name),
87 line_number_(line_number),
88 program_counter_(program_counter) {
90 // The program counter should not be null except in a default constructed
91 // (empty) Location object. This value is used for identity, so if it doesn't
92 // uniquely identify a location, things will break.
94 // The program counter isn't supported in NaCl so location objects won't work
95 // properly in that context.
96 DCHECK(program_counter);
100 std::string Location::ToString() const {
101 if (has_source_info()) {
102 return std::string(function_name_) + "@" + file_name_ + ":" +
103 NumberToString(line_number_);
105 return StringPrintf("pc:%p", program_counter_);
108 #if defined(COMPILER_MSVC)
109 #define RETURN_ADDRESS() _ReturnAddress()
110 #elif defined(COMPILER_GCC) && !defined(OS_NACL)
111 #define RETURN_ADDRESS() \
112 __builtin_extract_return_addr(__builtin_return_address(0))
114 #define RETURN_ADDRESS() nullptr
117 #if !BUILDFLAG(FROM_HERE_USES_LOCATION_BUILTINS)
118 #if !BUILDFLAG(ENABLE_LOCATION_SOURCE)
121 NOINLINE Location Location::CreateFromHere(const char* file_name) {
122 return Location(file_name + kStrippedPrefixLength, RETURN_ADDRESS());
128 NOINLINE Location Location::CreateFromHere(const char* function_name,
129 const char* file_name,
131 return Location(function_name, file_name + kStrippedPrefixLength, line_number,
138 #if SUPPORTS_LOCATION_BUILTINS && BUILDFLAG(ENABLE_LOCATION_SOURCE)
140 NOINLINE Location Location::Current(const char* function_name,
141 const char* file_name,
143 return Location(function_name, file_name + kStrippedPrefixLength, line_number,
146 #elif SUPPORTS_LOCATION_BUILTINS
148 NOINLINE Location Location::Current(const char* file_name) {
149 return Location(file_name + kStrippedPrefixLength, RETURN_ADDRESS());
153 NOINLINE Location Location::Current() {
154 return Location(nullptr, RETURN_ADDRESS());
158 //------------------------------------------------------------------------------
159 NOINLINE const void* GetProgramCounter() {
160 return RETURN_ADDRESS();