Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-21 10:05:26

0001 // Copyright 2020 the V8 project authors. All rights reserved.
0002 // Use of this source code is governed by a BSD-style license that can be
0003 // found in the LICENSE file.
0004 
0005 #ifndef INCLUDE_SOURCE_LOCATION_H_
0006 #define INCLUDE_SOURCE_LOCATION_H_
0007 
0008 #include <cstddef>
0009 #include <string>
0010 
0011 #include "v8config.h"  // NOLINT(build/include_directory)
0012 
0013 #if defined(__has_builtin)
0014 #define V8_SUPPORTS_SOURCE_LOCATION                                      \
0015   (__has_builtin(__builtin_FUNCTION) && __has_builtin(__builtin_FILE) && \
0016    __has_builtin(__builtin_LINE))  // NOLINT
0017 #elif defined(V8_CC_GNU) && __GNUC__ >= 7
0018 #define V8_SUPPORTS_SOURCE_LOCATION 1
0019 #elif defined(V8_CC_INTEL) && __ICC >= 1800
0020 #define V8_SUPPORTS_SOURCE_LOCATION 1
0021 #else
0022 #define V8_SUPPORTS_SOURCE_LOCATION 0
0023 #endif
0024 
0025 namespace v8 {
0026 
0027 /**
0028  * Encapsulates source location information. Mimics C++20's
0029  * `std::source_location`.
0030  */
0031 class V8_EXPORT SourceLocation final {
0032  public:
0033   /**
0034    * Construct source location information corresponding to the location of the
0035    * call site.
0036    */
0037 #if V8_SUPPORTS_SOURCE_LOCATION
0038   static constexpr SourceLocation Current(
0039       const char* function = __builtin_FUNCTION(),
0040       const char* file = __builtin_FILE(), size_t line = __builtin_LINE()) {
0041     return SourceLocation(function, file, line);
0042   }
0043 #else
0044   static constexpr SourceLocation Current() { return SourceLocation(); }
0045 #endif  // V8_SUPPORTS_SOURCE_LOCATION
0046 
0047   /**
0048    * Constructs unspecified source location information.
0049    */
0050   constexpr SourceLocation() = default;
0051 
0052   /**
0053    * Returns the name of the function associated with the position represented
0054    * by this object, if any.
0055    *
0056    * \returns the function name as cstring.
0057    */
0058   constexpr const char* Function() const { return function_; }
0059 
0060   /**
0061    * Returns the name of the current source file represented by this object.
0062    *
0063    * \returns the file name as cstring.
0064    */
0065   constexpr const char* FileName() const { return file_; }
0066 
0067   /**
0068    * Returns the line number represented by this object.
0069    *
0070    * \returns the line number.
0071    */
0072   constexpr size_t Line() const { return line_; }
0073 
0074   /**
0075    * Returns a human-readable string representing this object.
0076    *
0077    * \returns a human-readable string representing source location information.
0078    */
0079   std::string ToString() const;
0080 
0081  private:
0082   constexpr SourceLocation(const char* function, const char* file, size_t line)
0083       : function_(function), file_(file), line_(line) {}
0084 
0085   const char* function_ = nullptr;
0086   const char* file_ = nullptr;
0087   size_t line_ = 0u;
0088 };
0089 
0090 }  // namespace v8
0091 
0092 #endif  // INCLUDE_SOURCE_LOCATION_H_