ladybird/AK/SourceLocation.h
Brian Gianforcaro 0ad29bc3c9 AK: Add SourceLocation support
C++20 added std::source_location, which lets you capture the
callers __FILE__ / __LINE__ / __FUNCTION__ etc as a default
argument to functions.
See: https://en.cppreference.com/w/cpp/utility/source_location

During a bug investigation @ADKaster suggested we could use this
to make the LOCK_DEBUG feature of the kernel more user friendly
and allow it to automatically instrument all call sites.

We then implemented / tested it over discord. :^)

Co-Authored-by: Andrew Kaster <andrewdkaster@gmail.com>
2021-04-24 20:33:00 +02:00

41 lines
1.1 KiB
C++

/*
* Copyright (c) 2021, Andrew Kaster <andrewdkaster@gmail.com>
* Copyright (c) 2021, Brian Gianforcaro <bgianf@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/StringView.h>
#include <AK/Types.h>
namespace AK {
class SourceLocation {
public:
[[nodiscard]] constexpr StringView function_name() const { return StringView(m_function); }
[[nodiscard]] constexpr StringView file_name() const { return StringView(m_file); }
[[nodiscard]] constexpr u32 line_number() const { return m_line; }
[[nodiscard]] static constexpr SourceLocation current(const char* const file = __builtin_FILE(), u32 line = __builtin_LINE(), const char* const function = __builtin_FUNCTION())
{
return SourceLocation(file, line, function);
}
private:
constexpr SourceLocation(const char* const file, u32 line, const char* const function)
: m_function(function)
, m_file(file)
, m_line(line)
{
}
const char* const m_function { nullptr };
const char* const m_file { nullptr };
const u32 m_line { 0 };
};
}
using AK::SourceLocation;