Loading...
Searching...
No Matches
v8-source-location.h
Go to the documentation of this file.
1// Copyright 2020 the V8 project 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 INCLUDE_SOURCE_LOCATION_H_
6#define INCLUDE_SOURCE_LOCATION_H_
7
8#include <cstddef>
9#include <source_location>
10#include <string>
11
12#include "v8config.h" // NOLINT(build/include_directory)
13
14#define V8_SUPPORTS_SOURCE_LOCATION 1
15
16namespace v8 {
17
23 public:
28 static constexpr SourceLocation Current(
29 const std::source_location& loc = std::source_location::current()) {
30 return SourceLocation(loc);
31 }
32#ifdef DEBUG
33 static constexpr SourceLocation CurrentIfDebug(
34 const std::source_location& loc = std::source_location::current()) {
35 return SourceLocation(loc);
36 }
37#else
38 static constexpr SourceLocation CurrentIfDebug() { return {}; }
39#endif
40
44 constexpr SourceLocation() = default;
45
52 constexpr const char* Function() const { return loc_.function_name(); }
53
59 constexpr const char* FileName() const { return loc_.file_name(); }
60
66 constexpr size_t Line() const { return loc_.line(); }
67
73 std::string ToString() const {
74 if (loc_.line() == 0) {
75 return {};
76 }
77 return std::string(loc_.function_name()) + "@" + loc_.file_name() + ":" +
78 std::to_string(loc_.line());
79 }
80
81 private:
82 constexpr explicit SourceLocation(const std::source_location& loc)
83 : loc_(loc) {}
84
85 std::source_location loc_;
86};
87
88} // namespace v8
89
90#endif // INCLUDE_SOURCE_LOCATION_H_
Definition: v8-source-location.h:22
std::string ToString() const
Definition: v8-source-location.h:73
static constexpr SourceLocation CurrentIfDebug()
Definition: v8-source-location.h:38
constexpr size_t Line() const
Definition: v8-source-location.h:66
constexpr SourceLocation()=default
constexpr const char * FileName() const
Definition: v8-source-location.h:59
constexpr const char * Function() const
Definition: v8-source-location.h:52
static constexpr SourceLocation Current(const std::source_location &loc=std::source_location::current())
Definition: v8-source-location.h:28
Definition: libplatform.h:15
#define V8_EXPORT
Definition: v8config.h:860