/src/node/deps/v8/include/v8-source-location.h
Line | Count | Source |
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 | | |
16 | | namespace v8 { |
17 | | |
18 | | /** |
19 | | * Encapsulates source location information. Mimics C++20's |
20 | | * `std::source_location`. |
21 | | */ |
22 | | class 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 | 0 | const std::source_location& loc = std::source_location::current()) { |
30 | 0 | return SourceLocation(loc); |
31 | 0 | } |
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 | 0 | static constexpr SourceLocation CurrentIfDebug() { return {}; } |
39 | | #endif |
40 | | |
41 | | /** |
42 | | * Constructs unspecified source location information. |
43 | | */ |
44 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | std::string ToString() const { |
74 | 0 | if (loc_.line() == 0) { |
75 | 0 | return {}; |
76 | 0 | } |
77 | 0 | return std::string(loc_.function_name()) + "@" + loc_.file_name() + ":" + |
78 | 0 | std::to_string(loc_.line()); |
79 | 0 | } |
80 | | |
81 | | private: |
82 | | constexpr explicit SourceLocation(const std::source_location& loc) |
83 | 0 | : loc_(loc) {} |
84 | | |
85 | | std::source_location loc_; |
86 | | }; |
87 | | |
88 | | } // namespace v8 |
89 | | |
90 | | #endif // INCLUDE_SOURCE_LOCATION_H_ |