v8  8.6.395 (node 15.0.1)
V8 is Google's open source JavaScript engine
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_CPPGC_SOURCE_LOCATION_H_
6 #define INCLUDE_CPPGC_SOURCE_LOCATION_H_
7 
8 #include <string>
9 
10 #include "v8config.h" // NOLINT(build/include_directory)
11 
12 #if defined(__has_builtin)
13 #define CPPGC_SUPPORTS_SOURCE_LOCATION
14  (__has_builtin(__builtin_FUNCTION) && __has_builtin(__builtin_FILE) &&
15  __has_builtin(__builtin_LINE)) // NOLINT
16 #elif defined(V8_CC_GNU) && __GNUC__ >= 7
17 #define CPPGC_SUPPORTS_SOURCE_LOCATION 1
18 #elif defined(V8_CC_INTEL) && __ICC >= 1800
19 #define CPPGC_SUPPORTS_SOURCE_LOCATION 1
20 #else
21 #define CPPGC_SUPPORTS_SOURCE_LOCATION 0
22 #endif
23 
24 namespace cppgc {
25 
26 /**
27  * Encapsulates source location information. Mimics C++20's
28  * std::source_location.
29  */
31  public:
32  /**
33  * Construct source location information corresponding to the location of the
34  * call site.
35  */
37  static constexpr SourceLocation Current(
38  const char* function = __builtin_FUNCTION(),
39  const char* file = __builtin_FILE(), size_t line = __builtin_LINE()) {
40  return SourceLocation(function, file, line);
41  }
42 #else
43  static constexpr SourceLocation Current() { return SourceLocation(); }
44 #endif // CPPGC_SUPPORTS_SOURCE_LOCATION
45 
46  /**
47  * Constructs unspecified source location information.
48  */
49  constexpr SourceLocation() = default;
50 
51  /**
52  * Returns the name of the function associated with the position represented
53  * by this object, if any.
54  *
55  * \returns the function name as cstring.
56  */
57  constexpr const char* Function() const { return function_; }
58 
59  /**
60  * Returns the name of the current source file represented by this object.
61  *
62  * \returns the file name as cstring.
63  */
64  constexpr const char* FileName() const { return file_; }
65 
66  /**
67  * Returns the line number represented by this object.
68  *
69  * \returns the line number.
70  */
71  constexpr size_t Line() const { return line_; }
72 
73  /**
74  * Returns a human-readable string representing this object.
75  *
76  * \returns a human-readable string representing source location information.
77  */
78  std::string ToString() const;
79 
80  private:
81  constexpr SourceLocation(const char* function, const char* file, size_t line)
82  : function_(function), file_(file), line_(line) {}
83 
84  const char* function_ = nullptr;
85  const char* file_ = nullptr;
86  size_t line_ = 0u;
87 };
88 
89 } // namespace cppgc
90 
91 #endif // INCLUDE_CPPGC_SOURCE_LOCATION_H_