Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-20 09:15:51

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 <source_location>
0010 #include <string>
0011 
0012 #include "v8config.h"  // NOLINT(build/include_directory)
0013 
0014 #define V8_SUPPORTS_SOURCE_LOCATION 1
0015 
0016 namespace v8 {
0017 
0018 /**
0019  * Encapsulates source location information. Mimics C++20's
0020  * `std::source_location`.
0021  */
0022 class V8_EXPORT SourceLocation final {
0023  public:
0024   /**
0025    * Constructs source location information corresponding to the location of the
0026    * call site.
0027    */
0028   static constexpr SourceLocation Current(
0029       const std::source_location& loc = std::source_location::current()) {
0030     return SourceLocation(loc);
0031   }
0032 #ifdef DEBUG
0033   static constexpr SourceLocation CurrentIfDebug(
0034       const std::source_location& loc = std::source_location::current()) {
0035     return SourceLocation(loc);
0036   }
0037 #else
0038   static constexpr SourceLocation CurrentIfDebug() { return {}; }
0039 #endif
0040 
0041   /**
0042    * Constructs unspecified source location information.
0043    */
0044   constexpr SourceLocation() = default;
0045 
0046   /**
0047    * Returns the name of the function associated with the position represented
0048    * by this object, if any.
0049    *
0050    * \returns the function name as cstring.
0051    */
0052   constexpr const char* Function() const { return loc_.function_name(); }
0053 
0054   /**
0055    * Returns the name of the current source file represented by this object.
0056    *
0057    * \returns the file name as cstring.
0058    */
0059   constexpr const char* FileName() const { return loc_.file_name(); }
0060 
0061   /**
0062    * Returns the line number represented by this object.
0063    *
0064    * \returns the line number.
0065    */
0066   constexpr size_t Line() const { return loc_.line(); }
0067 
0068   /**
0069    * Returns a human-readable string representing this object.
0070    *
0071    * \returns a human-readable string representing source location information.
0072    */
0073   std::string ToString() const;
0074 
0075   /**
0076    * Checks whether this object is initialized.
0077    *
0078    * \returns true if this object is initialized, false otherwise.
0079    */
0080   operator bool() const { return loc_.line() != 0; }
0081 
0082  private:
0083   constexpr explicit SourceLocation(const std::source_location& loc)
0084       : loc_(loc) {}
0085 
0086   std::source_location loc_;
0087 };
0088 
0089 }  // namespace v8
0090 
0091 #endif  // INCLUDE_SOURCE_LOCATION_H_