v8 14.1.146 (node 25.0.0)
V8 is Google's open source JavaScript engine
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
18/**
19 * Encapsulates source location information. Mimics C++20's
20 * `std::source_location`.
21 */
22class V8_EXPORT SourceLocation final {
23 public:
24 /**
25 * Construct source location information corresponding to the location of the
26 * call site.
27 */
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
41 /**
42 * Constructs unspecified source location information.
43 */
44 constexpr SourceLocation() = default;
45
46 /**
47 * Returns the name of the function associated with the position represented
48 * by this object, if any.
49 *
50 * \returns the function name as cstring.
51 */
52 constexpr const char* Function() const { return loc_.function_name(); }
53
54 /**
55 * Returns the name of the current source file represented by this object.
56 *
57 * \returns the file name as cstring.
58 */
59 constexpr const char* FileName() const { return loc_.file_name(); }
60
61 /**
62 * Returns the line number represented by this object.
63 *
64 * \returns the line number.
65 */
66 constexpr size_t Line() const { return loc_.line(); }
67
68 /**
69 * Returns a human-readable string representing this object.
70 *
71 * \returns a human-readable string representing source location information.
72 */
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_
static constexpr SourceLocation CurrentIfDebug()
constexpr size_t Line() const
std::string ToString() const
constexpr SourceLocation()=default
static constexpr SourceLocation Current(const std::source_location &loc=std::source_location::current())
constexpr const char * FileName() const
constexpr const char * Function() const
#define V8_EXPORT
Definition v8config.h:860