From abd7c4f31719e96b8d27fe9d6ddb19d3d652da5c Mon Sep 17 00:00:00 2001 From: Lukas Bergdoll Date: Mon, 29 Sep 2025 18:49:56 +0200 Subject: [PATCH 1/2] Suppress variable shadowing warnings This disables the warnings for MSVC, GCC and Clang in the ZoneScopedXX macros. The warnings produced are both a false positive since they didn't find a bug *and* they don't happen in user written code, so the user couldn't even do much about it. The previous workaround of using ZoneNamedXXX is a poor solution since the Zone(Text|Name|etc.) macros all rely on the `___tracy_scoped_zone` name. --- public/tracy/Tracy.hpp | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/public/tracy/Tracy.hpp b/public/tracy/Tracy.hpp index 98957f6d..6c8a80b1 100644 --- a/public/tracy/Tracy.hpp +++ b/public/tracy/Tracy.hpp @@ -149,10 +149,32 @@ #define ZoneTransientN( varname, name, active ) tracy::ScopedZone varname( TracyLine, TracyFile, strlen( TracyFile ), TracyFunction, strlen( TracyFunction ), name, strlen( name ), TRACY_CALLSTACK, active ) #define ZoneTransientNC( varname, name, color, active ) tracy::ScopedZone varname( TracyLine, TracyFile, strlen( TracyFile ), TracyFunction, strlen( TracyFunction ), name, strlen( name ), color, TRACY_CALLSTACK, active ) -#define ZoneScoped ZoneNamed( ___tracy_scoped_zone, true ) -#define ZoneScopedN( name ) ZoneNamedN( ___tracy_scoped_zone, name, true ) -#define ZoneScopedC( color ) ZoneNamedC( ___tracy_scoped_zone, color, true ) -#define ZoneScopedNC( name, color ) ZoneNamedNC( ___tracy_scoped_zone, name, color, true ) +#if defined(__clang__) + #define SuppressVarShadowWarning(Expr) \ + _Pragma("clang diagnostic push"); \ + _Pragma("clang diagnostic ignored \"-Wshadow\""); \ + Expr; \ + _Pragma("clang diagnostic pop"); +#elif defined(__GNU__) + #define SuppressVarShadowWarning(Expr) \ + _Pragma("GCC diagnostic push"); \ + _Pragma("GCC diagnostic ignored \"-Wshadow\""); \ + Expr; \ + _Pragma("GCC diagnostic pop"); +#elif defined(_MSC_VER) + #define SuppressVarShadowWarning(Expr) \ + _Pragma("warning(push)"); \ + _Pragma("warning(disable : 4456)"); \ + Expr; \ + _Pragma("warning(pop)"); +#else + #define SuppressVarShadowWarning(Expr) Expr +#endif + +#define ZoneScoped SuppressVarShadowWarning( ZoneNamed( ___tracy_scoped_zone, true ) ) +#define ZoneScopedN( name ) SuppressVarShadowWarning( ZoneNamedN( ___tracy_scoped_zone, name, true ) ) +#define ZoneScopedC( color ) SuppressVarShadowWarning( ZoneNamedC( ___tracy_scoped_zone, color, true ) ) +#define ZoneScopedNC( name, color ) SuppressVarShadowWarning( ZoneNamedNC( ___tracy_scoped_zone, name, color, true ) ) #define ZoneText( txt, size ) ___tracy_scoped_zone.Text( txt, size ) #define ZoneTextV( varname, txt, size ) varname.Text( txt, size ) From 5002c045e28a4843f40b828ce2a48e39f2369094 Mon Sep 17 00:00:00 2001 From: Lukas Bergdoll Date: Thu, 2 Oct 2025 18:31:13 +0200 Subject: [PATCH 2/2] Make variable shadowing warning suppression optional --- manual/tracy.tex | 2 +- public/tracy/Tracy.hpp | 28 +++++++++++++++------------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/manual/tracy.tex b/manual/tracy.tex index 88925c33..79b73c71 100644 --- a/manual/tracy.tex +++ b/manual/tracy.tex @@ -1397,7 +1397,7 @@ void Function() } \end{lstlisting} -This doesn't stop some compilers from dispensing \emph{fashion advice} about variable shadowing (as both \texttt{ZoneScoped} calls create a variable with the same name, with the inner scope one shadowing the one in the outer scope). If you want to avoid these warnings, you will also need to use the \texttt{ZoneNamed} macros. +This doesn't stop some compilers from dispensing \emph{fashion advice} about variable shadowing (as both \texttt{ZoneScoped} calls create a variable with the same name, with the inner scope one shadowing the one in the outer scope). By default the produced warnings are suppressed when using clang, gcc or MSVC. This behavior can be opted out of by defining \texttt{TRACY\_ALLOW\_SHADOW\_WARNING}. An alternative approach avoids variable name shadowing by manually defining zone names with \texttt{ZoneNamed}. Using this approach requires using the V variants of zone macros like \texttt{ZoneTextV}. \subsubsection{Exiting program from within a zone} diff --git a/public/tracy/Tracy.hpp b/public/tracy/Tracy.hpp index 6c8a80b1..31289b89 100644 --- a/public/tracy/Tracy.hpp +++ b/public/tracy/Tracy.hpp @@ -149,24 +149,26 @@ #define ZoneTransientN( varname, name, active ) tracy::ScopedZone varname( TracyLine, TracyFile, strlen( TracyFile ), TracyFunction, strlen( TracyFunction ), name, strlen( name ), TRACY_CALLSTACK, active ) #define ZoneTransientNC( varname, name, color, active ) tracy::ScopedZone varname( TracyLine, TracyFile, strlen( TracyFile ), TracyFunction, strlen( TracyFunction ), name, strlen( name ), color, TRACY_CALLSTACK, active ) -#if defined(__clang__) +#if defined(TRACY_ALLOW_SHADOW_WARNING) + #define SuppressVarShadowWarning(Expr) Expr +#elif defined(__clang__) #define SuppressVarShadowWarning(Expr) \ - _Pragma("clang diagnostic push"); \ - _Pragma("clang diagnostic ignored \"-Wshadow\""); \ - Expr; \ - _Pragma("clang diagnostic pop"); + _Pragma("clang diagnostic push") \ + _Pragma("clang diagnostic ignored \"-Wshadow\"") \ + Expr \ + _Pragma("clang diagnostic pop") #elif defined(__GNU__) #define SuppressVarShadowWarning(Expr) \ - _Pragma("GCC diagnostic push"); \ - _Pragma("GCC diagnostic ignored \"-Wshadow\""); \ - Expr; \ - _Pragma("GCC diagnostic pop"); + _Pragma("GCC diagnostic push") \ + _Pragma("GCC diagnostic ignored \"-Wshadow\"") \ + Expr \ + _Pragma("GCC diagnostic pop") #elif defined(_MSC_VER) #define SuppressVarShadowWarning(Expr) \ - _Pragma("warning(push)"); \ - _Pragma("warning(disable : 4456)"); \ - Expr; \ - _Pragma("warning(pop)"); + _Pragma("warning(push)") \ + _Pragma("warning(disable : 4456)") \ + Expr \ + _Pragma("warning(pop)") #else #define SuppressVarShadowWarning(Expr) Expr #endif