Skip to content

fix(gtest): use unique identifier in SCOPED_TRACE to avoid redefinition and shadow warnings - #5146

Open
Aryan9891 wants to merge 1 commit into
google:mainfrom
Aryan9891:fix/scoped-trace-unique-id-5143
Open

Aryan9891 wants to merge 1 commit into
google:mainfrom
Aryan9891:fix/scoped-trace-unique-id-5143

Conversation

@Aryan9891

Copy link
Copy Markdown

Description

When SCOPED_TRACE is invoked multiple times on the same line (e.g. inside macro expansions, multiple assertions in one macro, or compact test assertions), concatenating __LINE__ results in identical variable names (gtest_trace_<line>), producing compiler errors (redefinition of 'gtest_trace_<line>') or -Wshadow warnings under -Werror.

This PR introduces GTEST_UNIQUE_IDENTIFIER_ in gtest-internal.h utilizing __COUNTER__ when supported across GCC, Clang, and MSVC (with fallback to __LINE__), ensuring distinct variable names across multiple invocations on the same line and across nested scopes.

Changes

  • googletest/include/gtest/internal/gtest-internal.h: Introduce GTEST_UNIQUE_IDENTIFIER_(name) using __COUNTER__ where available.
  • googletest/include/gtest/gtest.h: Update SCOPED_TRACE to declare its ScopedTrace dummy variable using GTEST_UNIQUE_IDENTIFIER_(gtest_trace_).
  • googletest/test/gtest_unittest.cc: Add regression test ScopedTraceTest.CompilesMultipleTracesOnSameLine asserting two SCOPED_TRACE invocations compile cleanly on the same line.

Closes #5143

…on and shadow warnings

When SCOPED_TRACE is invoked multiple times on the same line (e.g. inside macro expansions or compact test assertions), concatenating __LINE__ resulted in duplicate variable names ('gtest_trace_<line>'), leading to compiler errors or -Wshadow warnings.

Introduce GTEST_UNIQUE_IDENTIFIER_ in gtest-internal.h using __COUNTER__ when available (falling back to __LINE__), ensuring distinct dummy variable names across multiple invocations on the same line and across nested scopes.

Closes google#5143

Signed-off-by: Aryan Sachin <aryansachinixd@gmail.com>
Aryan9891 added a commit to Aryan9891/Aryan9891 that referenced this pull request Nov 2, 2026
Signed-off-by: Aryan Sachin <aryansachinixd@gmail.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: 2 SCOPED_TRACE calls on the same line results in a -Wshadow warning

1 participant