Skip to content

Commit

Permalink
GetCurrentOsStackTraceExceptTop (both the method of UnitTestImpl and …
Browse files Browse the repository at this point in the history
…the wrapper function in gtest.cc) rely on the fact that the inner call is not getting optimized.

This CL annotates them with the appropriate attributes.

PiperOrigin-RevId: 425663217
Change-Id: Ib9ec2a69a7dd98d37640b56d4d7798572da66669
  • Loading branch information
Abseil Team authored and Copybara-Service committed Feb 1, 2022
1 parent 4517697 commit 25ad42a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
14 changes: 14 additions & 0 deletions googletest/include/gtest/internal/gtest-port.h
Expand Up @@ -789,6 +789,20 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
# define GTEST_NO_INLINE_
#endif

#if defined(__clang__)
// Nested ifs to avoid triggering MSVC warning.
#if __has_attribute(disable_tail_calls)
// Ask the compiler not to perform tail call optimization inside
// the marked function.
#define GTEST_NO_TAIL_CALL_ __attribute__((disable_tail_calls))
#endif
#elif __GNUC__
#define GTEST_NO_TAIL_CALL_ \
__attribute__((optimize("no-optimize-sibling-calls")))
#else
#define GTEST_NO_TAIL_CALL_
#endif

// _LIBCPP_VERSION is defined by the libc++ library from the LLVM project.
#if !defined(GTEST_HAS_CXXABI_H_)
# if defined(__GLIBCXX__) || (defined(_LIBCPP_VERSION) && !defined(_MSC_VER))
Expand Down
3 changes: 2 additions & 1 deletion googletest/src/gtest-internal-inl.h
Expand Up @@ -623,7 +623,8 @@ class GTEST_API_ UnitTestImpl {
// For example, if Foo() calls Bar(), which in turn calls
// CurrentOsStackTraceExceptTop(1), Foo() will be included in the
// trace but Bar() and CurrentOsStackTraceExceptTop() won't.
std::string CurrentOsStackTraceExceptTop(int skip_count) GTEST_NO_INLINE_;
std::string CurrentOsStackTraceExceptTop(int skip_count)
GTEST_NO_INLINE_ GTEST_NO_TAIL_CALL_;

// Finds and returns a TestSuite with the given name. If one doesn't
// exist, creates one and returns it.
Expand Down
4 changes: 2 additions & 2 deletions googletest/src/gtest.cc
Expand Up @@ -6292,8 +6292,8 @@ void UnitTestImpl::UnshuffleTests() {
// For example, if Foo() calls Bar(), which in turn calls
// GetCurrentOsStackTraceExceptTop(..., 1), Foo() will be included in
// the trace but Bar() and GetCurrentOsStackTraceExceptTop() won't.
std::string GetCurrentOsStackTraceExceptTop(UnitTest* /*unit_test*/,
int skip_count) {
GTEST_NO_INLINE_ GTEST_NO_TAIL_CALL_ std::string
GetCurrentOsStackTraceExceptTop(UnitTest* /*unit_test*/, int skip_count) {
// We pass skip_count + 1 to skip this wrapper function in addition
// to what the user really wants to skip.
return GetUnitTestImpl()->CurrentOsStackTraceExceptTop(skip_count + 1);
Expand Down

0 comments on commit 25ad42a

Please sign in to comment.