[M94 Dev][Tizen] Fix for errors for generating ninja files
[platform/framework/web/chromium-efl.git] / base / location.cc
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 #include "base/location.h"
6
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
10 #ifndef NACL_TC_REV
11 #pragma clang max_tokens_here 240000
12 #endif
13
14 #if defined(COMPILER_MSVC)
15 #include <intrin.h>
16 #endif
17
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"
22
23 namespace base {
24
25 namespace {
26
27 // Returns the length of the given null terminated c-string.
28 constexpr size_t StrLen(const char* str) {
29   size_t str_len = 0;
30   for (str_len = 0; str[str_len] != '\0'; ++str_len)
31     ;
32   return str_len;
33 }
34
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;
47 }
48
49 constexpr size_t kStrippedPrefixLength = StrippedFilePathPrefixLength();
50
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
54 //  instead.
55 constexpr bool StrEndsWith(const char* name,
56                            size_t prefix_len,
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)
61     return false;
62   for (size_t i = 0; i < expected_len; ++i) {
63     if (name[i + prefix_len] != expected[i])
64       return false;
65   }
66   return true;
67 }
68
69 static_assert(StrEndsWith(__FILE__, kStrippedPrefixLength, "base/location.cc"),
70               "The file name does not match the expected prefix format.");
71
72 }  // namespace
73
74 Location::Location() = default;
75 Location::Location(const Location& other) = default;
76 Location& Location::operator=(const Location& other) = default;
77
78 Location::Location(const char* file_name, const void* program_counter)
79     : file_name_(file_name), program_counter_(program_counter) {}
80
81 Location::Location(const char* function_name,
82                    const char* file_name,
83                    int line_number,
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) {
89 #if !defined(OS_NACL)
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.
93   //
94   // The program counter isn't supported in NaCl so location objects won't work
95   // properly in that context.
96   DCHECK(program_counter);
97 #endif
98 }
99
100 std::string Location::ToString() const {
101   if (has_source_info()) {
102     return std::string(function_name_) + "@" + file_name_ + ":" +
103            NumberToString(line_number_);
104   }
105   return StringPrintf("pc:%p", program_counter_);
106 }
107
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))
113 #else
114 #define RETURN_ADDRESS() nullptr
115 #endif
116
117 #if !BUILDFLAG(FROM_HERE_USES_LOCATION_BUILTINS)
118 #if !BUILDFLAG(ENABLE_LOCATION_SOURCE)
119
120 // static
121 NOINLINE Location Location::CreateFromHere(const char* file_name) {
122   return Location(file_name + kStrippedPrefixLength, RETURN_ADDRESS());
123 }
124
125 #else
126
127 // static
128 NOINLINE Location Location::CreateFromHere(const char* function_name,
129                                            const char* file_name,
130                                            int line_number) {
131   return Location(function_name, file_name + kStrippedPrefixLength, line_number,
132                   RETURN_ADDRESS());
133 }
134
135 #endif
136 #endif
137
138 #if SUPPORTS_LOCATION_BUILTINS && BUILDFLAG(ENABLE_LOCATION_SOURCE)
139 // static
140 NOINLINE Location Location::Current(const char* function_name,
141                                     const char* file_name,
142                                     int line_number) {
143   return Location(function_name, file_name + kStrippedPrefixLength, line_number,
144                   RETURN_ADDRESS());
145 }
146 #elif SUPPORTS_LOCATION_BUILTINS
147 // static
148 NOINLINE Location Location::Current(const char* file_name) {
149   return Location(file_name + kStrippedPrefixLength, RETURN_ADDRESS());
150 }
151 #else
152 // static
153 NOINLINE Location Location::Current() {
154   return Location(nullptr, RETURN_ADDRESS());
155 }
156 #endif
157
158 //------------------------------------------------------------------------------
159 NOINLINE const void* GetProgramCounter() {
160   return RETURN_ADDRESS();
161 }
162
163 }  // namespace base