From d1999a1ccc41d4a8c3f399051a2a1e55c1e4f6ad Mon Sep 17 00:00:00 2001 From: Alexey Samsonov Date: Thu, 14 Mar 2013 15:15:35 +0000 Subject: [PATCH] [Sanitizer] Fix compiler warnings and style issues in sanitizer_common tests. Use -Werror=sign-compare when building them. llvm-svn: 177077 --- .../sanitizer_thread_registry.cc | 2 + .../sanitizer_thread_registry.h | 3 +- .../lib/sanitizer_common/tests/CMakeLists.txt | 2 +- .../tests/sanitizer_linux_test.cc | 2 +- .../tests/sanitizer_thread_registry_test.cc | 46 ++++++++++--------- 5 files changed, 30 insertions(+), 25 deletions(-) diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_thread_registry.cc b/compiler-rt/lib/sanitizer_common/sanitizer_thread_registry.cc index d900212972c3..3454c51fb082 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_thread_registry.cc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_thread_registry.cc @@ -80,6 +80,8 @@ void ThreadContextBase::Reset(void *arg) { // ThreadRegistry implementation. +const u32 ThreadRegistry::kUnknownTid = -1U; + ThreadRegistry::ThreadRegistry(ThreadContextFactory factory, u32 max_threads, u32 thread_quarantine_size) : context_factory_(factory), diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_thread_registry.h b/compiler-rt/lib/sanitizer_common/sanitizer_thread_registry.h index ca352dd5fdd8..a9ce771c87a9 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_thread_registry.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_thread_registry.h @@ -77,7 +77,6 @@ class ThreadRegistry { const u32 max_threads_; const u32 thread_quarantine_size_; - static const u32 kUnknownTid = -1U; BlockingMutex mtx_; u32 n_contexts_; // Number of created thread contexts, @@ -92,6 +91,8 @@ class ThreadRegistry { IntrusiveList dead_threads_; public: + static const u32 kUnknownTid; + ThreadRegistry(ThreadContextFactory factory, u32 max_threads, u32 thread_quarantine_size); void GetNumberOfThreads(uptr *total = 0, uptr *running = 0, uptr *alive = 0); diff --git a/compiler-rt/lib/sanitizer_common/tests/CMakeLists.txt b/compiler-rt/lib/sanitizer_common/tests/CMakeLists.txt index 2c749d169f8b..74f08c569346 100644 --- a/compiler-rt/lib/sanitizer_common/tests/CMakeLists.txt +++ b/compiler-rt/lib/sanitizer_common/tests/CMakeLists.txt @@ -56,7 +56,7 @@ macro(add_sanitizer_tests_for_arch arch) -I${COMPILER_RT_SOURCE_DIR}/include -I${COMPILER_RT_SOURCE_DIR}/lib -I${COMPILER_RT_SOURCE_DIR}/lib/sanitizer_common - -O2 -g -Wall -Werror ${TARGET_FLAGS}) + -O2 -g -Wall -Werror -Werror=sign-compare ${TARGET_FLAGS}) set(SANITIZER_TEST_LINK_FLAGS -lstdc++ -lpthread -ldl ${TARGET_FLAGS}) set(SANITIZER_TEST_OBJECTS) foreach(source ${SANITIZER_TEST_SOURCES}) diff --git a/compiler-rt/lib/sanitizer_common/tests/sanitizer_linux_test.cc b/compiler-rt/lib/sanitizer_common/tests/sanitizer_linux_test.cc index 4dd0288f8603..035c11fa4622 100644 --- a/compiler-rt/lib/sanitizer_common/tests/sanitizer_linux_test.cc +++ b/compiler-rt/lib/sanitizer_common/tests/sanitizer_linux_test.cc @@ -118,7 +118,7 @@ void ThreadListerTest::SpawnTidReporter(pthread_t *pthread_id, *tid = thread_arg.reported_tid; } -std::vector ReadTidsToVector(ThreadLister *thread_lister) { +static std::vector ReadTidsToVector(ThreadLister *thread_lister) { std::vector listed_tids; pid_t tid; while ((tid = thread_lister->GetNextTID()) >= 0) diff --git a/compiler-rt/lib/sanitizer_common/tests/sanitizer_thread_registry_test.cc b/compiler-rt/lib/sanitizer_common/tests/sanitizer_thread_registry_test.cc index ab6324037ccc..e080403fb56c 100644 --- a/compiler-rt/lib/sanitizer_common/tests/sanitizer_thread_registry_test.cc +++ b/compiler-rt/lib/sanitizer_common/tests/sanitizer_thread_registry_test.cc @@ -39,11 +39,11 @@ static void CheckThreadQuantity(ThreadRegistry *registry, uptr exp_total, EXPECT_EQ(exp_alive, alive); } -static bool is_detached(int tid) { +static bool is_detached(u32 tid) { return (tid % 2 == 0); } -static uptr get_uid(int tid) { +static uptr get_uid(u32 tid) { return tid * 2; } @@ -64,50 +64,52 @@ static void MarkUidAsPresent(ThreadContextBase *tctx, void *arg) { static void TestRegistry(ThreadRegistry *registry, bool has_quarantine) { // Create and start a main thread. - EXPECT_EQ(0, registry->CreateThread(get_uid(0), true, -1, 0)); + EXPECT_EQ(0U, registry->CreateThread(get_uid(0), true, -1, 0)); registry->StartThread(0, 0, 0); // Create a bunch of threads. - for (int i = 1; i <= 10; i++) { + for (u32 i = 1; i <= 10; i++) { EXPECT_EQ(i, registry->CreateThread(get_uid(i), is_detached(i), 0, 0)); } CheckThreadQuantity(registry, 11, 1, 11); // Start some of them. - for (int i = 1; i <= 5; i++) { + for (u32 i = 1; i <= 5; i++) { registry->StartThread(i, 0, 0); } CheckThreadQuantity(registry, 11, 6, 11); // Finish, create and start more threads. - for (int i = 1; i <= 5; i++) { + for (u32 i = 1; i <= 5; i++) { registry->FinishThread(i); if (!is_detached(i)) registry->JoinThread(i, 0); } - for (int i = 6; i <= 10; i++) { + for (u32 i = 6; i <= 10; i++) { registry->StartThread(i, 0, 0); } - std::vector new_tids; - for (int i = 11; i <= 15; i++) { + std::vector new_tids; + for (u32 i = 11; i <= 15; i++) { new_tids.push_back( registry->CreateThread(get_uid(i), is_detached(i), 0, 0)); } - ASSERT_LE(kRegistryQuarantine, 5); - int exp_total = 16 - (has_quarantine ? 5 - kRegistryQuarantine : 0); + ASSERT_LE(kRegistryQuarantine, 5U); + u32 exp_total = 16 - (has_quarantine ? 5 - kRegistryQuarantine : 0); CheckThreadQuantity(registry, exp_total, 6, 11); // Test SetThreadName and FindThread. registry->SetThreadName(6, "six"); registry->SetThreadName(7, "seven"); - EXPECT_EQ(7, registry->FindThread(HasName, (void*)"seven")); - EXPECT_EQ(-1, registry->FindThread(HasName, (void*)"none")); - EXPECT_EQ(0, registry->FindThread(HasUid, (void*)get_uid(0))); - EXPECT_EQ(10, registry->FindThread(HasUid, (void*)get_uid(10))); - EXPECT_EQ(-1, registry->FindThread(HasUid, (void*)0x1234)); + EXPECT_EQ(7U, registry->FindThread(HasName, (void*)"seven")); + EXPECT_EQ(ThreadRegistry::kUnknownTid, + registry->FindThread(HasName, (void*)"none")); + EXPECT_EQ(0U, registry->FindThread(HasUid, (void*)get_uid(0))); + EXPECT_EQ(10U, registry->FindThread(HasUid, (void*)get_uid(10))); + EXPECT_EQ(ThreadRegistry::kUnknownTid, + registry->FindThread(HasUid, (void*)0x1234)); // Detach and finish and join remaining threads. - for (int i = 6; i <= 10; i++) { + for (u32 i = 6; i <= 10; i++) { registry->DetachThread(i); registry->FinishThread(i); } - for (int i = 0; i < new_tids.size(); i++) { - int tid = new_tids[i]; + for (u32 i = 0; i < new_tids.size(); i++) { + u32 tid = new_tids[i]; registry->StartThread(tid, 0, 0); registry->DetachThread(tid); registry->FinishThread(tid); @@ -120,7 +122,7 @@ static void TestRegistry(ThreadRegistry *registry, bool has_quarantine) { ThreadRegistryLock l(registry); registry->RunCallbackForEachThreadLocked(MarkUidAsPresent, &has_tid[0]); } - for (int i = 0; i < exp_total; i++) { + for (u32 i = 0; i < exp_total; i++) { EXPECT_TRUE(has_tid[i]); } { @@ -130,7 +132,7 @@ static void TestRegistry(ThreadRegistry *registry, bool has_quarantine) { EXPECT_EQ(main_thread, registry->FindThreadContextLocked( HasUid, (void*)get_uid(0))); } - EXPECT_EQ(11, registry->GetMaxAliveThreads()); + EXPECT_EQ(11U, registry->GetMaxAliveThreads()); } TEST(SanitizerCommon, ThreadRegistryTest) { @@ -195,7 +197,7 @@ void *RunThread(void *arg) { static void ThreadedTestRegistry(ThreadRegistry *registry) { // Create and start a main thread. - EXPECT_EQ(0, registry->CreateThread(0, true, -1, 0)); + EXPECT_EQ(0U, registry->CreateThread(0, true, -1, 0)); registry->StartThread(0, 0, 0); pthread_t threads[kNumShards]; RunThreadArgs args[kNumShards];