Use DoNotOptimize rather than rely on a volatile.
The previous code used a volatile to avoid optimizing away
the strlen, but it can trigger the unused variable warning.
Instead use DoNotOptimize to avoid the code being optimized away.
Break out the DoNotOptimize into it's own header since the entirety
of utils.h cannot be included here without getting compile errors.
Add the new include to all tests that use DoNotOptimize.
Test: Tests build and pass.
Change-Id: I292693bdc0269f8f0600bd8e9729420d63c4a919
diff --git a/tests/clang_fortify_tests.cpp b/tests/clang_fortify_tests.cpp
index 105c261..ea111c9 100644
--- a/tests/clang_fortify_tests.cpp
+++ b/tests/clang_fortify_tests.cpp
@@ -95,6 +95,8 @@
#include <android-base/silent_death_test.h>
#include <gtest/gtest.h>
+#include "DoNotOptimize.h"
+
#define CONCAT2(x, y) x##y
#define CONCAT(x, y) CONCAT2(x, y)
#define FORTIFY_TEST_NAME CONCAT(CONCAT(clang_fortify_test_, _FORTIFY_SOURCE), _DeathTest)
@@ -146,8 +148,7 @@
for (char& c : contents) {
c ^= always_zero;
}
- // Store in a volatile, so the strlen itself cannot be optimized out.
- volatile size_t _strlen_result = strlen(&contents.front());
+ DoNotOptimize(strlen(&contents.front()));
};
EXPECT_NO_DEATH(run_strlen_with_contents({'f', 'o', '\0'}));