From 5bed8036644f552210a7cfcbed2d6d20cf2981b0 Mon Sep 17 00:00:00 2001 From: Mark Salyzyn Date: Wed, 30 Apr 2014 11:10:46 -0700 Subject: [PATCH] libutils: turn on -Werror - Deal with some -Wunused issues - Override PRI macros (windows) - Revert use of PRI macros on off64_t (linux) - Deal with a gnu++11 complaince issue Change-Id: Ie66751293bd84477a5a6dfd8a57e700a16e36964 --- libutils/Android.mk | 6 ++++-- libutils/BlobCache.cpp | 4 ++-- libutils/FileMap.cpp | 10 ++++++++-- libutils/LinearAllocator.cpp | 4 ++-- libutils/RefBase.cpp | 28 +++++++++++++++++----------- libutils/StopWatch.cpp | 2 ++ libutils/String16.cpp | 2 -- libutils/String8.cpp | 1 - libutils/SystemClock.cpp | 16 +++++++--------- libutils/Threads.cpp | 30 ++++++++++++++++++------------ libutils/Timers.cpp | 7 +++++-- libutils/Unicode.cpp | 2 +- libutils/tests/BlobCache_test.cpp | 28 ++++++++++++++-------------- 13 files changed, 80 insertions(+), 60 deletions(-) diff --git a/libutils/Android.mk b/libutils/Android.mk index 1c4861902..3afc1ec4b 100644 --- a/libutils/Android.mk +++ b/libutils/Android.mk @@ -43,7 +43,7 @@ commonSources:= \ VectorImpl.cpp \ misc.cpp -host_commonCflags := -DLIBUTILS_NATIVE=1 $(TOOL_CFLAGS) +host_commonCflags := -DLIBUTILS_NATIVE=1 $(TOOL_CFLAGS) -Werror ifeq ($(HOST_OS),windows) ifeq ($(strip $(USE_CYGWIN),),) @@ -99,6 +99,7 @@ LOCAL_SRC_FILES:= \ ifeq ($(TARGET_ARCH),mips) LOCAL_CFLAGS += -DALIGN_DOUBLE endif +LOCAL_CFLAGS += -Werror LOCAL_C_INCLUDES += \ bionic/libc/private \ @@ -126,7 +127,8 @@ LOCAL_SHARED_LIBRARIES := \ libbacktrace \ libcutils \ libdl \ - liblog \ + liblog +LOCAL_CFLAGS := -Werror include external/stlport/libstlport.mk diff --git a/libutils/BlobCache.cpp b/libutils/BlobCache.cpp index 660917b0e..f00bf1469 100644 --- a/libutils/BlobCache.cpp +++ b/libutils/BlobCache.cpp @@ -28,7 +28,7 @@ namespace android { // BlobCache::Header::mMagicNumber value -static const uint32_t blobCacheMagic = '_Bb$'; +static const uint32_t blobCacheMagic = ('_' << 24) + ('B' << 16) + ('b' << 8) + '$'; // BlobCache::Header::mBlobCacheVersion value static const uint32_t blobCacheVersion = 1; @@ -49,7 +49,7 @@ BlobCache::BlobCache(size_t maxKeySize, size_t maxValueSize, size_t maxTotalSize mRandState[1] = (now >> 16) & 0xFFFF; mRandState[2] = (now >> 32) & 0xFFFF; #endif - ALOGV("initializing random seed using %lld", now); + ALOGV("initializing random seed using %lld", (unsigned long long)now); } void BlobCache::set(const void* key, size_t keySize, const void* value, diff --git a/libutils/FileMap.cpp b/libutils/FileMap.cpp index 933e7aa28..be4b14fed 100644 --- a/libutils/FileMap.cpp +++ b/libutils/FileMap.cpp @@ -23,7 +23,13 @@ #include #include +#if defined(HAVE_WIN32_FILEMAP) && !defined(__USE_MINGW_ANSI_STDIO) +# define PRId32 "I32d" +# define PRIx32 "I32x" +# define PRId64 "I64d" +#else #include +#endif #include #include @@ -169,8 +175,8 @@ try_again: goto try_again; } - ALOGE("mmap(%" PRId64 ",%zu) failed: %s\n", - adjOffset, adjLength, strerror(errno)); + ALOGE("mmap(%lld,%zu) failed: %s\n", + (long long)adjOffset, adjLength, strerror(errno)); return false; } mBasePtr = ptr; diff --git a/libutils/LinearAllocator.cpp b/libutils/LinearAllocator.cpp index a07a2916e..8b90696ff 100644 --- a/libutils/LinearAllocator.cpp +++ b/libutils/LinearAllocator.cpp @@ -92,7 +92,7 @@ public: : mNextPage(0) {} - void* operator new(size_t size, void* buf) { return buf; } + void* operator new(size_t /*size*/, void* buf) { return buf; } void* start() { return (void*) (((size_t)this) + sizeof(Page)); @@ -103,7 +103,7 @@ public: } private: - Page(const Page& other) {} + Page(const Page& /*other*/) {} Page* mNextPage; }; diff --git a/libutils/RefBase.cpp b/libutils/RefBase.cpp index 385c226fb..02907ad67 100644 --- a/libutils/RefBase.cpp +++ b/libutils/RefBase.cpp @@ -17,6 +17,14 @@ #define LOG_TAG "RefBase" // #define LOG_NDEBUG 0 +#include +#include +#include +#include +#include +#include +#include + #include #include @@ -24,13 +32,9 @@ #include #include -#include -#include -#include -#include -#include -#include -#include +#ifndef __unused +#define __unused __attribute__((__unused__)) +#endif // compile with refcounting debugging enabled #define DEBUG_REFS 0 @@ -388,7 +392,7 @@ void RefBase::weakref_type::incWeak(const void* id) { weakref_impl* const impl = static_cast(this); impl->addWeakRef(id); - const int32_t c = android_atomic_inc(&impl->mWeak); + const int32_t c __unused = android_atomic_inc(&impl->mWeak); ALOG_ASSERT(c >= 0, "incWeak called on %p after last weak ref", this); } @@ -615,7 +619,7 @@ void RefBase::onLastStrongRef(const void* /*id*/) { } -bool RefBase::onIncStrongAttempted(uint32_t flags, const void* id) +bool RefBase::onIncStrongAttempted(uint32_t flags, const void* /*id*/) { return (flags&FIRST_INC_STRONG) ? true : false; } @@ -626,13 +630,15 @@ void RefBase::onLastWeakRef(const void* /*id*/) // --------------------------------------------------------------------------- -void RefBase::renameRefs(size_t n, const ReferenceRenamer& renamer) { #if DEBUG_REFS +void RefBase::renameRefs(size_t n, const ReferenceRenamer& renamer) { for (size_t i=0 ; i /* for PRId64 */ +#ifndef __STDC_FORMAT_MACROS #define __STDC_FORMAT_MACROS 1 +#endif #include #include diff --git a/libutils/String16.cpp b/libutils/String16.cpp index 3bdc349ab..91efdaa39 100644 --- a/libutils/String16.cpp +++ b/libutils/String16.cpp @@ -66,8 +66,6 @@ static char16_t* allocFromUTF8(const char* u8str, size_t u8len) return getEmptyString(); } - const uint8_t* const u8end = u8cur + u8len; - SharedBuffer* buf = SharedBuffer::alloc(sizeof(char16_t)*(u16len+1)); if (buf) { u8cur = (const uint8_t*) u8str; diff --git a/libutils/String8.cpp b/libutils/String8.cpp index 8acb4d45f..49340bbb7 100644 --- a/libutils/String8.cpp +++ b/libutils/String8.cpp @@ -551,7 +551,6 @@ char* String8::find_extension(void) const { const char* lastSlash; const char* lastDot; - int extLen; const char* const str = mString; // only look at the filename diff --git a/libutils/SystemClock.cpp b/libutils/SystemClock.cpp index 413250f48..dbad581ff 100644 --- a/libutils/SystemClock.cpp +++ b/libutils/SystemClock.cpp @@ -68,13 +68,7 @@ int64_t elapsedRealtime() */ #define DEBUG_TIMESTAMP 0 -static const char *gettime_method_names[] = { - "clock_gettime", - "ioctl", - "systemTime", -}; - -#if DEBUG_TIMESTAMP +#if DEBUG_TIMESTAMP && defined(ARCH_ARM) static inline void checkTimeStamps(int64_t timestamp, int64_t volatile *prevTimestampPtr, int volatile *prevMethodPtr, @@ -85,11 +79,16 @@ static inline void checkTimeStamps(int64_t timestamp, * gettid, and int64_t is different on the ARM platform * (ie long vs long long). */ -#ifdef ARCH_ARM int64_t prevTimestamp = *prevTimestampPtr; int prevMethod = *prevMethodPtr; if (timestamp < prevTimestamp) { + static const char *gettime_method_names[] = { + "clock_gettime", + "ioctl", + "systemTime", + }; + ALOGW("time going backwards: prev %lld(%s) vs now %lld(%s), tid=%d", prevTimestamp, gettime_method_names[prevMethod], timestamp, gettime_method_names[curMethod], @@ -99,7 +98,6 @@ static inline void checkTimeStamps(int64_t timestamp, // write is interrupted or not observed as a whole. *prevTimestampPtr = timestamp; *prevMethodPtr = curMethod; -#endif } #else #define checkTimeStamps(timestamp, prevTimestampPtr, prevMethodPtr, curMethod) diff --git a/libutils/Threads.cpp b/libutils/Threads.cpp index ff74914cb..cc7fe8993 100644 --- a/libutils/Threads.cpp +++ b/libutils/Threads.cpp @@ -17,16 +17,11 @@ // #define LOG_NDEBUG 0 #define LOG_TAG "libutils.threads" -#include -#include - -#include - +#include +#include +#include #include #include -#include -#include -#include #include #if defined(HAVE_PTHREADS) @@ -47,6 +42,17 @@ #include #endif +#include +#include + +#include + +#ifdef HAVE_ANDROID_OS +# define __android_unused +#else +# define __android_unused __attribute__((__unused__)) +#endif + /* * =========================================================================== * Thread wrappers @@ -119,7 +125,7 @@ void androidSetThreadName(const char* name) { int androidCreateRawThreadEtc(android_thread_func_t entryFunction, void *userData, - const char* threadName, + const char* threadName __android_unused, int32_t threadPriority, size_t threadStackSize, android_thread_id_t *threadId) @@ -251,9 +257,9 @@ static bool doCreateThread(android_thread_func_t fn, void* arg, android_thread_i int androidCreateRawThreadEtc(android_thread_func_t fn, void *userData, - const char* threadName, - int32_t threadPriority, - size_t threadStackSize, + const char* /*threadName*/, + int32_t /*threadPriority*/, + size_t /*threadStackSize*/, android_thread_id_t *threadId) { return doCreateThread( fn, userData, threadId); diff --git a/libutils/Timers.cpp b/libutils/Timers.cpp index a431e9232..4687d4d59 100644 --- a/libutils/Timers.cpp +++ b/libutils/Timers.cpp @@ -32,9 +32,9 @@ #include #endif +#if defined(HAVE_ANDROID_OS) nsecs_t systemTime(int clock) { -#if defined(HAVE_ANDROID_OS) static const clockid_t clocks[] = { CLOCK_REALTIME, CLOCK_MONOTONIC, @@ -46,7 +46,10 @@ nsecs_t systemTime(int clock) t.tv_sec = t.tv_nsec = 0; clock_gettime(clocks[clock], &t); return nsecs_t(t.tv_sec)*1000000000LL + t.tv_nsec; +} #else +nsecs_t systemTime(int /*clock*/) +{ // Clock support varies widely across hosts. Mac OS doesn't support // posix clocks, older glibcs don't support CLOCK_BOOTTIME and Windows // is windows. @@ -54,8 +57,8 @@ nsecs_t systemTime(int clock) t.tv_sec = t.tv_usec = 0; gettimeofday(&t, NULL); return nsecs_t(t.tv_sec)*1000000000LL + nsecs_t(t.tv_usec)*1000LL; -#endif } +#endif int toMillisecondTimeoutDelay(nsecs_t referenceTime, nsecs_t timeoutTime) { diff --git a/libutils/Unicode.cpp b/libutils/Unicode.cpp index a66e3bbbb..fe8887d92 100644 --- a/libutils/Unicode.cpp +++ b/libutils/Unicode.cpp @@ -576,7 +576,7 @@ void utf8_to_utf16(const uint8_t* u8str, size_t u8len, char16_t* u16str) { char16_t* utf8_to_utf16_n(const uint8_t* src, size_t srcLen, char16_t* dst, size_t dstLen) { const uint8_t* const u8end = src + srcLen; const uint8_t* u8cur = src; - const uint16_t* const u16end = dst + dstLen; + const char16_t* const u16end = dst + dstLen; char16_t* u16cur = dst; while (u8cur < u8end && u16cur < u16end) { diff --git a/libutils/tests/BlobCache_test.cpp b/libutils/tests/BlobCache_test.cpp index 7202123eb..dac4e2c26 100644 --- a/libutils/tests/BlobCache_test.cpp +++ b/libutils/tests/BlobCache_test.cpp @@ -44,7 +44,7 @@ protected: }; TEST_F(BlobCacheTest, CacheSingleValueSucceeds) { - char buf[4] = { 0xee, 0xee, 0xee, 0xee }; + unsigned char buf[4] = { 0xee, 0xee, 0xee, 0xee }; mBC->set("abcd", 4, "efgh", 4); ASSERT_EQ(size_t(4), mBC->get("abcd", 4, buf, 4)); ASSERT_EQ('e', buf[0]); @@ -54,7 +54,7 @@ TEST_F(BlobCacheTest, CacheSingleValueSucceeds) { } TEST_F(BlobCacheTest, CacheTwoValuesSucceeds) { - char buf[2] = { 0xee, 0xee }; + unsigned char buf[2] = { 0xee, 0xee }; mBC->set("ab", 2, "cd", 2); mBC->set("ef", 2, "gh", 2); ASSERT_EQ(size_t(2), mBC->get("ab", 2, buf, 2)); @@ -66,7 +66,7 @@ TEST_F(BlobCacheTest, CacheTwoValuesSucceeds) { } TEST_F(BlobCacheTest, GetOnlyWritesInsideBounds) { - char buf[6] = { 0xee, 0xee, 0xee, 0xee, 0xee, 0xee }; + unsigned char buf[6] = { 0xee, 0xee, 0xee, 0xee, 0xee, 0xee }; mBC->set("abcd", 4, "efgh", 4); ASSERT_EQ(size_t(4), mBC->get("abcd", 4, buf+1, 4)); ASSERT_EQ(0xee, buf[0]); @@ -78,7 +78,7 @@ TEST_F(BlobCacheTest, GetOnlyWritesInsideBounds) { } TEST_F(BlobCacheTest, GetOnlyWritesIfBufferIsLargeEnough) { - char buf[3] = { 0xee, 0xee, 0xee }; + unsigned char buf[3] = { 0xee, 0xee, 0xee }; mBC->set("abcd", 4, "efgh", 4); ASSERT_EQ(size_t(4), mBC->get("abcd", 4, buf, 3)); ASSERT_EQ(0xee, buf[0]); @@ -92,7 +92,7 @@ TEST_F(BlobCacheTest, GetDoesntAccessNullBuffer) { } TEST_F(BlobCacheTest, MultipleSetsCacheLatestValue) { - char buf[4] = { 0xee, 0xee, 0xee, 0xee }; + unsigned char buf[4] = { 0xee, 0xee, 0xee, 0xee }; mBC->set("abcd", 4, "efgh", 4); mBC->set("abcd", 4, "ijkl", 4); ASSERT_EQ(size_t(4), mBC->get("abcd", 4, buf, 4)); @@ -103,7 +103,7 @@ TEST_F(BlobCacheTest, MultipleSetsCacheLatestValue) { } TEST_F(BlobCacheTest, SecondSetKeepsFirstValueIfTooLarge) { - char buf[MAX_VALUE_SIZE+1] = { 0xee, 0xee, 0xee, 0xee }; + unsigned char buf[MAX_VALUE_SIZE+1] = { 0xee, 0xee, 0xee, 0xee }; mBC->set("abcd", 4, "efgh", 4); mBC->set("abcd", 4, buf, MAX_VALUE_SIZE+1); ASSERT_EQ(size_t(4), mBC->get("abcd", 4, buf, 4)); @@ -115,7 +115,7 @@ TEST_F(BlobCacheTest, SecondSetKeepsFirstValueIfTooLarge) { TEST_F(BlobCacheTest, DoesntCacheIfKeyIsTooBig) { char key[MAX_KEY_SIZE+1]; - char buf[4] = { 0xee, 0xee, 0xee, 0xee }; + unsigned char buf[4] = { 0xee, 0xee, 0xee, 0xee }; for (int i = 0; i < MAX_KEY_SIZE+1; i++) { key[i] = 'a'; } @@ -165,7 +165,7 @@ TEST_F(BlobCacheTest, DoesntCacheIfKeyValuePairIsTooBig) { TEST_F(BlobCacheTest, CacheMaxKeySizeSucceeds) { char key[MAX_KEY_SIZE]; - char buf[4] = { 0xee, 0xee, 0xee, 0xee }; + unsigned char buf[4] = { 0xee, 0xee, 0xee, 0xee }; for (int i = 0; i < MAX_KEY_SIZE; i++) { key[i] = 'a'; } @@ -214,7 +214,7 @@ TEST_F(BlobCacheTest, CacheMaxKeyValuePairSizeSucceeds) { } TEST_F(BlobCacheTest, CacheMinKeyAndValueSizeSucceeds) { - char buf[1] = { 0xee }; + unsigned char buf[1] = { 0xee }; mBC->set("x", 1, "y", 1); ASSERT_EQ(size_t(1), mBC->get("x", 1, buf, 1)); ASSERT_EQ('y', buf[0]); @@ -282,7 +282,7 @@ protected: }; TEST_F(BlobCacheFlattenTest, FlattenOneValue) { - char buf[4] = { 0xee, 0xee, 0xee, 0xee }; + unsigned char buf[4] = { 0xee, 0xee, 0xee, 0xee }; mBC->set("abcd", 4, "efgh", 4); roundTrip(); ASSERT_EQ(size_t(4), mBC2->get("abcd", 4, buf, 4)); @@ -348,7 +348,7 @@ TEST_F(BlobCacheFlattenTest, FlattenCatchesBufferTooSmall) { } TEST_F(BlobCacheFlattenTest, UnflattenCatchesBadMagic) { - char buf[4] = { 0xee, 0xee, 0xee, 0xee }; + unsigned char buf[4] = { 0xee, 0xee, 0xee, 0xee }; mBC->set("abcd", 4, "efgh", 4); size_t size = mBC->getFlattenedSize(); @@ -365,7 +365,7 @@ TEST_F(BlobCacheFlattenTest, UnflattenCatchesBadMagic) { } TEST_F(BlobCacheFlattenTest, UnflattenCatchesBadBlobCacheVersion) { - char buf[4] = { 0xee, 0xee, 0xee, 0xee }; + unsigned char buf[4] = { 0xee, 0xee, 0xee, 0xee }; mBC->set("abcd", 4, "efgh", 4); size_t size = mBC->getFlattenedSize(); @@ -384,7 +384,7 @@ TEST_F(BlobCacheFlattenTest, UnflattenCatchesBadBlobCacheVersion) { } TEST_F(BlobCacheFlattenTest, UnflattenCatchesBadBlobCacheDeviceVersion) { - char buf[4] = { 0xee, 0xee, 0xee, 0xee }; + unsigned char buf[4] = { 0xee, 0xee, 0xee, 0xee }; mBC->set("abcd", 4, "efgh", 4); size_t size = mBC->getFlattenedSize(); @@ -403,7 +403,7 @@ TEST_F(BlobCacheFlattenTest, UnflattenCatchesBadBlobCacheDeviceVersion) { } TEST_F(BlobCacheFlattenTest, UnflattenCatchesBufferTooSmall) { - char buf[4] = { 0xee, 0xee, 0xee, 0xee }; + unsigned char buf[4] = { 0xee, 0xee, 0xee, 0xee }; mBC->set("abcd", 4, "efgh", 4); size_t size = mBC->getFlattenedSize();