Skip to content

Commit

Permalink
YT-18571: Polish TSourceLocation
Browse files Browse the repository at this point in the history
81b1c42ea62adf4f2a7e2e7ba3601ca9687e29ed
  • Loading branch information
maxim-babenko committed Jul 2, 2024
1 parent eeae0a4 commit a53d801
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 36 deletions.
25 changes: 25 additions & 0 deletions library/cpp/yt/misc/source_location-inl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef SOURCE_LOCATION_INL_H_
#error "Direct inclusion of this file is not allowed, include source_location.h"
// For the sake of sane code completion.
#include "source_location.h"
#endif

namespace NYT {

////////////////////////////////////////////////////////////////////////////////

inline TSourceLocation::TSourceLocation(const char* fileName, int line)
: FileName_(fileName)
, Line_(line)
{ }

#ifdef __cpp_lib_source_location
inline TSourceLocation::TSourceLocation(const std::source_location& location)
: FileName_(location.file_name())
, Line_(location.line())
{ }
#endif // __cpp_lib_source_location

////////////////////////////////////////////////////////////////////////////////

} // namespace std
7 changes: 0 additions & 7 deletions library/cpp/yt/misc/source_location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,6 @@ bool TSourceLocation::operator==(const TSourceLocation& other) const
Line_ == other.Line_;
}

#ifdef __cpp_lib_source_location
TSourceLocation TSourceLocation::FromStd(const std::source_location& location)
{
return TSourceLocation(location.file_name(), location.line());
}
#endif // __cpp_lib_source_location

void FormatValue(TStringBuilderBase* builder, const TSourceLocation& location, TStringBuf /*spec*/)
{
if (location.GetFileName() != nullptr) {
Expand Down
33 changes: 13 additions & 20 deletions library/cpp/yt/misc/source_location.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,19 @@ class TStringBuilderBase;

// TODO(dgolear): Drop when LLVM-14 is eradicated.
#ifdef __cpp_lib_source_location

void FormatValue(TStringBuilderBase* builder, const std::source_location& location, TStringBuf /*spec*/);

#endif // __cpp_lib_source_location

////////////////////////////////////////////////////////////////////////////////

class TSourceLocation
{
public:
TSourceLocation()
: FileName_(nullptr)
, Line_(-1)
{ }

TSourceLocation(const char* fileName, int line)
: FileName_(fileName)
, Line_(line)
{ }
TSourceLocation() = default;
TSourceLocation(const char* fileName, int line);
#ifdef __cpp_lib_source_location
explicit TSourceLocation(const std::source_location& location);
#endif // __cpp_lib_source_location

const char* GetFileName() const;
int GetLine() const;
Expand All @@ -41,25 +35,24 @@ class TSourceLocation
bool operator<(const TSourceLocation& other) const;
bool operator==(const TSourceLocation& other) const;

#ifdef __cpp_lib_source_location
static TSourceLocation FromStd(const std::source_location& location);
#endif // __cpp_lib_source_location

private:
const char* FileName_;
int Line_;

const char* FileName_ = nullptr;
int Line_ = -1;
};

//! Defines a macro to record the current source location.
#ifdef __cpp_lib_source_location
#define FROM_HERE ::NYT::TSourceLocation::FromStd(std::source_location::current())
#define YT_CURRENT_SOURCE_LOCATION ::NYT::TSourceLocation(std::source_location::current())
#else
#define FROM_HERE ::NYT::TSourceLocation(__FILE__, __LINE__)
#define YT_CURRENT_SOURCE_LOCATION ::NYT::TSourceLocation(__FILE__, __LINE__)
#endif // __cpp_lib_source_location

void FormatValue(TStringBuilderBase* builder, const TSourceLocation& location, TStringBuf spec);

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT

#define SOURCE_LOCATION_INL_H_
#include "source_location-inl.h"
#undef SOURCE_LOCATION_INL_H_
2 changes: 1 addition & 1 deletion yt/yt/core/actions/bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ auto Bind(
////////////////////////////////////////////////////////////////////////////////

#ifdef YT_ENABLE_BIND_LOCATION_TRACKING
#define BIND_IMPL(propagate, ...) ::NYT::Bind<propagate, ::NYT::TCurrentTranslationUnitTag, __COUNTER__>(FROM_HERE, __VA_ARGS__)
#define BIND_IMPL(propagate, ...) ::NYT::Bind<propagate, ::NYT::TCurrentTranslationUnitTag, __COUNTER__>(YT_CURRENT_SOURCE_LOCATION, __VA_ARGS__)
#else
#define BIND_IMPL(propagate, ...) ::NYT::Bind<propagate>(__VA_ARGS__)
#endif
Expand Down
4 changes: 2 additions & 2 deletions yt/yt/core/concurrency/propagating_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class TPropagatingStorageGuard
{
public:
explicit TPropagatingStorageGuard(
TPropagatingStorage storage, TSourceLocation loc = FROM_HERE);
TPropagatingStorage storage, TSourceLocation loc = YT_CURRENT_SOURCE_LOCATION);
~TPropagatingStorageGuard();

TPropagatingStorageGuard(const TPropagatingStorageGuard& other) = delete;
Expand All @@ -128,7 +128,7 @@ class TNullPropagatingStorageGuard
: public TPropagatingStorageGuard
{
public:
TNullPropagatingStorageGuard(TSourceLocation loc = FROM_HERE);
TNullPropagatingStorageGuard(TSourceLocation loc = YT_CURRENT_SOURCE_LOCATION);
};

////////////////////////////////////////////////////////////////////////////////
Expand Down
4 changes: 2 additions & 2 deletions yt/yt/core/misc/unittests/callback_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct TBindState<true, void(), void(), void(TFakeInvoker)>
TBindState()
: TBindStateBase(
#ifdef YT_ENABLE_BIND_LOCATION_TRACKING
FROM_HERE
YT_CURRENT_SOURCE_LOCATION
#endif
)
{ }
Expand All @@ -56,7 +56,7 @@ struct TBindState<true, void(), void(), void(TFakeInvoker, TFakeInvoker)>
TBindState()
: TBindStateBase(
#ifdef YT_ENABLE_BIND_LOCATION_TRACKING
FROM_HERE
YT_CURRENT_SOURCE_LOCATION
#endif
)
{ }
Expand Down
4 changes: 2 additions & 2 deletions yt/yt/core/tracing/trace_context-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ Y_FORCE_INLINE bool TCurrentTraceContextGuard::IsActive() const
Y_FORCE_INLINE void TCurrentTraceContextGuard::Release()
{
if (Active_) {
NDetail::SwapTraceContext(std::move(OldTraceContext_), FROM_HERE);
NDetail::SwapTraceContext(std::move(OldTraceContext_), YT_CURRENT_SOURCE_LOCATION);
Active_ = false;
}
}
Expand Down Expand Up @@ -254,7 +254,7 @@ Y_FORCE_INLINE bool TNullTraceContextGuard::IsActive() const
Y_FORCE_INLINE void TNullTraceContextGuard::Release()
{
if (Active_) {
NDetail::SwapTraceContext(std::move(OldTraceContext_), FROM_HERE);
NDetail::SwapTraceContext(std::move(OldTraceContext_), YT_CURRENT_SOURCE_LOCATION);
Active_ = false;
}
}
Expand Down
4 changes: 2 additions & 2 deletions yt/yt/core/tracing/trace_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ class TCurrentTraceContextGuard
public:
explicit TCurrentTraceContextGuard(
TTraceContextPtr traceContext,
TSourceLocation location = FROM_HERE);
TSourceLocation location = YT_CURRENT_SOURCE_LOCATION);
TCurrentTraceContextGuard(TCurrentTraceContextGuard&& other);
~TCurrentTraceContextGuard();

Expand All @@ -324,7 +324,7 @@ class TCurrentTraceContextGuard
class TNullTraceContextGuard
{
public:
TNullTraceContextGuard(TSourceLocation location = FROM_HERE);
TNullTraceContextGuard(TSourceLocation location = YT_CURRENT_SOURCE_LOCATION);
TNullTraceContextGuard(TNullTraceContextGuard&& other);
~TNullTraceContextGuard();

Expand Down

0 comments on commit a53d801

Please sign in to comment.