Commit aecd1e58 authored by Anton Bikineev's avatar Anton Bikineev Committed by Commit Bot

cppgc: Add SourceLocation class

SourceLocation is needed to track Persistents.

Bug: chromium:1056170
Change-Id: I4d5cf151a4d27b4c29582ff8195cdcb7453afe1c
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2132790
Commit-Queue: Anton Bikineev <bikineev@chromium.org>
Reviewed-by: 's avatarOmer Katz <omerkatz@chromium.org>
Reviewed-by: 's avatarMichael Lippautz <mlippautz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#66974}
parent 51c00e9f
...@@ -3948,6 +3948,7 @@ v8_source_set("cppgc_base") { ...@@ -3948,6 +3948,7 @@ v8_source_set("cppgc_base") {
"include/cppgc/heap.h", "include/cppgc/heap.h",
"include/cppgc/member.h", "include/cppgc/member.h",
"include/cppgc/platform.h", "include/cppgc/platform.h",
"include/cppgc/source-location.h",
"include/cppgc/type_traits.h", "include/cppgc/type_traits.h",
"include/v8config.h", "include/v8config.h",
"src/heap/cppgc/allocation.cc", "src/heap/cppgc/allocation.cc",
...@@ -3963,6 +3964,7 @@ v8_source_set("cppgc_base") { ...@@ -3963,6 +3964,7 @@ v8_source_set("cppgc_base") {
"src/heap/cppgc/member.cc", "src/heap/cppgc/member.cc",
"src/heap/cppgc/platform.cc", "src/heap/cppgc/platform.cc",
"src/heap/cppgc/sanitizers.h", "src/heap/cppgc/sanitizers.h",
"src/heap/cppgc/source-location.cc",
"src/heap/cppgc/stack.cc", "src/heap/cppgc/stack.cc",
"src/heap/cppgc/stack.h", "src/heap/cppgc/stack.h",
] ]
......
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef INCLUDE_CPPGC_SOURCE_LOCATION_H_
#define INCLUDE_CPPGC_SOURCE_LOCATION_H_
#include <string>
#include "include/v8config.h"
#if defined(__has_builtin)
#define CPPGC_SUPPORTS_SOURCE_LOCATION \
(__has_builtin(__builtin_FUNCTION) && __has_builtin(__builtin_FILE) && \
__has_builtin(__builtin_LINE)) // NOLINT
#elif defined(V8_CC_GNU) && __GNUC__ >= 5
#define CPPGC_SUPPORTS_SOURCE_LOCATION 1
#elif defined(V8_CC_INTEL) && __ICC >= 1800
#define CPPGC_SUPPORTS_SOURCE_LOCATION 1
#else
#define CPPGC_SUPPORTS_SOURCE_LOCATION 0
#endif
namespace cppgc {
// Encapsulates source location information. Mimics C++20's
// std::source_location.
class V8_EXPORT SourceLocation final {
public:
#if CPPGC_SUPPORTS_SOURCE_LOCATION
static constexpr SourceLocation Current(
const char* function = __builtin_FUNCTION(),
const char* file = __builtin_FILE(), size_t line = __builtin_LINE()) {
return SourceLocation(function, file, line);
}
#else
static constexpr SourceLocation Current() { return SourceLocation(); }
#endif // CPPGC_SUPPORTS_SOURCE_LOCATION
constexpr SourceLocation() = default;
constexpr const char* Function() const { return function_; }
constexpr const char* FileName() const { return file_; }
constexpr size_t Line() const { return line_; }
std::string ToString() const;
private:
constexpr SourceLocation(const char* function, const char* file, size_t line)
: function_(function), file_(file), line_(line) {}
const char* function_ = nullptr;
const char* file_ = nullptr;
size_t line_ = 0u;
};
} // namespace cppgc
#endif // INCLUDE_CPPGC_SOURCE_LOCATION_H_
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "include/cppgc/source-location.h"
namespace cppgc {
std::string SourceLocation::ToString() const {
if (!file_) {
return {};
}
return std::string(function_) + "@" + file_ + ":" + std::to_string(line_);
}
} // namespace cppgc
...@@ -50,6 +50,7 @@ v8_source_set("cppgc_unittests_sources") { ...@@ -50,6 +50,7 @@ v8_source_set("cppgc_unittests_sources") {
"heap/cppgc/gc-info_unittest.cc", "heap/cppgc/gc-info_unittest.cc",
"heap/cppgc/heap-object-header_unittest.cc", "heap/cppgc/heap-object-header_unittest.cc",
"heap/cppgc/member_unittests.cc", "heap/cppgc/member_unittests.cc",
"heap/cppgc/source-location_unittest.cc",
"heap/cppgc/stack_unittest.cc", "heap/cppgc/stack_unittest.cc",
"heap/cppgc/tests.cc", "heap/cppgc/tests.cc",
"heap/cppgc/tests.h", "heap/cppgc/tests.h",
......
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "include/cppgc/source-location.h"
#include "src/base/macros.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace cppgc {
namespace internal {
namespace {
constexpr char kFileName[] = "source-location_unittest.cc";
bool Contains(const std::string& base_string, const std::string& substring) {
return base_string.find(substring) != std::string::npos;
}
} // namespace
TEST(SourceLocationTest, DefaultCtor) {
constexpr SourceLocation loc;
EXPECT_EQ(nullptr, loc.Function());
EXPECT_EQ(nullptr, loc.FileName());
EXPECT_EQ(0u, loc.Line());
}
void TestSourceLocationCurrent() {
static constexpr char kFunctionName[] = "TestSourceLocationCurrent";
static constexpr size_t kNextLine = __LINE__ + 1;
constexpr auto loc = SourceLocation::Current();
#if !CPPGC_SUPPORTS_SOURCE_LOCATION
EXPECT_EQ(nullptr, loc.Function());
EXPECT_EQ(nullptr, loc.FileName());
EXPECT_EQ(0u, loc.Line());
USE(kNextLine);
return;
#endif
EXPECT_EQ(kNextLine, loc.Line());
EXPECT_TRUE(Contains(loc.FileName(), kFileName));
EXPECT_TRUE(Contains(loc.Function(), kFunctionName));
}
TEST(SourceLocationTest, Current) { TestSourceLocationCurrent(); }
void TestToString() {
static const std::string kDescriptor = std::string(__func__) + "@" +
__FILE__ + ":" +
std::to_string(__LINE__ + 1);
constexpr auto loc = SourceLocation::Current();
const auto string = loc.ToString();
EXPECT_EQ(kDescriptor, string);
}
#if CPPGC_SUPPORTS_SOURCE_LOCATION
TEST(SourceLocationTest, ToString) { TestToString(); }
#endif
} // namespace internal
} // namespace cppgc
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment