[libc][NFC] Add a few compiler warning flags.

A bunch of cleanup to supress the new warnings is also done.

Reviewed By: abrachet

Differential Revision: https://reviews.llvm.org/D130723

GitOrigin-RevId: 2e4ef9b6efcaacd5556e8c0ddf659550d3a089a0
diff --git a/cmake/modules/LLVMLibCObjectRules.cmake b/cmake/modules/LLVMLibCObjectRules.cmake
index 9e82514..e3919ec 100644
--- a/cmake/modules/LLVMLibCObjectRules.cmake
+++ b/cmake/modules/LLVMLibCObjectRules.cmake
@@ -18,14 +18,25 @@
   endif()
 
   set(compile_options ${LIBC_COMPILE_OPTIONS_DEFAULT} ${ARGN})
-  if(NOT ${LIBC_TARGET_OS} STREQUAL "windows")
-    set(compile_options ${compile_options} -fpie -ffreestanding -fno-builtin)
-  endif()
   if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
+    list(APPEND compile_options "-fpie")
+    list(APPEND compile_options "-ffreestanding")
+    list(APPEND compile_options "-fno-builtin")
     list(APPEND compile_options "-fno-exceptions")
     list(APPEND compile_options "-fno-unwind-tables")
     list(APPEND compile_options "-fno-asynchronous-unwind-tables")
     list(APPEND compile_options "-fno-rtti")
+    list(APPEND compile_options "-Wall")
+    list(APPEND compile_options "-Wextra")
+    list(APPEND compile_options "-Wimplicit-fallthrough")
+    list(APPEND compile_options "-Wwrite-strings")
+    list(APPEND compile_options "-Wextra-semi")
+    list(APPEND compile_options "-Wstrict-prototypes")
+    if(NOT CMAKE_COMPILER_IS_GNUCXX)
+      list(APPEND compile_options "-Wnewline-eof")
+      list(APPEND compile_options "-Wnonportable-system-include-path")
+      list(APPEND compile_options "-Wthread-safety")
+    endif()
     if(ADD_FMA_FLAG)
       list(APPEND compile_options "-mfma")
     endif()
diff --git a/src/__support/CPP/stringstream.h b/src/__support/CPP/stringstream.h
index 2fb4670..fbd0572 100644
--- a/src/__support/CPP/stringstream.h
+++ b/src/__support/CPP/stringstream.h
@@ -63,7 +63,7 @@
   }
 
   template <typename T, enable_if_t<is_floating_point_v<T>, int> = 0>
-  StringStream &operator<<(T val) {
+  StringStream &operator<<(T) {
     // If this specialization gets activated, then the static_assert will
     // trigger a compile error about missing floating point number support.
     static_assert(!is_floating_point_v<T>,
diff --git a/src/__support/FPUtil/PolyEval.h b/src/__support/FPUtil/PolyEval.h
index 4a4ab0d..b6faa0f 100644
--- a/src/__support/FPUtil/PolyEval.h
+++ b/src/__support/FPUtil/PolyEval.h
@@ -21,7 +21,7 @@
 namespace __llvm_libc {
 namespace fputil {
 
-template <typename T> static inline T polyeval(T x, T a0) { return a0; }
+template <typename T> static inline T polyeval(T, T a0) { return a0; }
 
 template <typename T, typename... Ts>
 static inline T polyeval(T x, T a0, Ts... a) {
diff --git a/src/__support/str_to_float.h b/src/__support/str_to_float.h
index 3b043ec..a385a7f 100644
--- a/src/__support/str_to_float.h
+++ b/src/__support/str_to_float.h
@@ -67,9 +67,7 @@
   return static_cast<uint64_t>(num >> 64);
 }
 
-template <class T> inline void set_implicit_bit(fputil::FPBits<T> &result) {
-  return;
-}
+template <class T> inline void set_implicit_bit(fputil::FPBits<T> &) { return; }
 
 #if defined(SPECIAL_X86_LONG_DOUBLE)
 template <>
diff --git a/src/pthread/pthread_attr_destroy.cpp b/src/pthread/pthread_attr_destroy.cpp
index 592d002..f51b056 100644
--- a/src/pthread/pthread_attr_destroy.cpp
+++ b/src/pthread/pthread_attr_destroy.cpp
@@ -14,7 +14,7 @@
 
 namespace __llvm_libc {
 
-LLVM_LIBC_FUNCTION(int, pthread_attr_destroy, (pthread_attr_t * attr)) {
+LLVM_LIBC_FUNCTION(int, pthread_attr_destroy, (pthread_attr_t *)) {
   // There is nothing to cleanup.
   return 0;
 }
diff --git a/src/pthread/pthread_create.cpp b/src/pthread/pthread_create.cpp
index ec1e0e3..7064125 100644
--- a/src/pthread/pthread_create.cpp
+++ b/src/pthread/pthread_create.cpp
@@ -20,10 +20,10 @@
               "Mismatch between pthread_t and internal Thread.");
 
 LLVM_LIBC_FUNCTION(int, pthread_create,
-                   (pthread_t *__restrict th,
-                    const pthread_attr_t *__restrict attr,
+                   (pthread_t *__restrict th, const pthread_attr_t *__restrict,
                     __pthread_start_t func, void *arg)) {
   auto *thread = reinterpret_cast<__llvm_libc::Thread *>(th);
+  // TODO: Use the attributes parameter to set up thread properties.
   int result = thread->run(func, arg, nullptr, 0);
   if (result != 0 && result != EPERM)
     return EAGAIN;
diff --git a/src/pthread/pthread_mutexattr_destroy.cpp b/src/pthread/pthread_mutexattr_destroy.cpp
index ed338b4..cedfbe3 100644
--- a/src/pthread/pthread_mutexattr_destroy.cpp
+++ b/src/pthread/pthread_mutexattr_destroy.cpp
@@ -15,8 +15,7 @@
 
 namespace __llvm_libc {
 
-LLVM_LIBC_FUNCTION(int, pthread_mutexattr_destroy,
-                   (pthread_mutexattr_t * attr)) {
+LLVM_LIBC_FUNCTION(int, pthread_mutexattr_destroy, (pthread_mutexattr_t *)) {
   return 0;
 }
 
diff --git a/src/stdio/fopencookie.cpp b/src/stdio/fopencookie.cpp
index 64177eb..6facc96 100644
--- a/src/stdio/fopencookie.cpp
+++ b/src/stdio/fopencookie.cpp
@@ -56,7 +56,7 @@
   return cookie_file->ops.close(cookie_file->cookie);
 }
 
-int flush_func(File *f) { return 0; }
+int flush_func(File *) { return 0; }
 
 } // anonymous namespace
 
diff --git a/src/string/memory_utils/elements.h b/src/string/memory_utils/elements.h
index 68718cd..a5aa30d 100644
--- a/src/string/memory_utils/elements.h
+++ b/src/string/memory_utils/elements.h
@@ -145,7 +145,7 @@
 };
 
 template <typename Element> struct Repeated<Element, 0> {
-  static void move(char *dst, const char *src) {}
+  static void move(char *, const char *) {}
 };
 
 // Chain the operation of several types.
@@ -188,11 +188,11 @@
 
 template <> struct Chained<> {
   static constexpr size_t SIZE = 0;
-  static void copy(char *__restrict dst, const char *__restrict src) {}
-  static void move(char *dst, const char *src) {}
-  static bool equals(const char *lhs, const char *rhs) { return true; }
-  static int three_way_compare(const char *lhs, const char *rhs) { return 0; }
-  static void splat_set(char *dst, const unsigned char value) {}
+  static void copy(char *__restrict, const char *__restrict) {}
+  static void move(char *, const char *) {}
+  static bool equals(const char *, const char *) { return true; }
+  static int three_way_compare(const char *, const char *) { return 0; }
+  static void splat_set(char *, const unsigned char) {}
 };
 
 // Overlap ElementA and ElementB so they span Size bytes.
@@ -431,14 +431,14 @@
 
 template <> struct ArgSelector<Arg::_1> {
   template <typename T1, typename T2>
-  static T1 *__restrict &Select(T1 *__restrict &p1ref, T2 *__restrict &p2ref) {
+  static T1 *__restrict &Select(T1 *__restrict &p1ref, T2 *__restrict &) {
     return p1ref;
   }
 };
 
 template <> struct ArgSelector<Arg::_2> {
   template <typename T1, typename T2>
-  static T2 *__restrict &Select(T1 *__restrict &p1ref, T2 *__restrict &p2ref) {
+  static T2 *__restrict &Select(T1 *__restrict &, T2 *__restrict &p2ref) {
     return p2ref;
   }
 };
diff --git a/src/threads/mtx_destroy.cpp b/src/threads/mtx_destroy.cpp
index 773db20..6285b2c 100644
--- a/src/threads/mtx_destroy.cpp
+++ b/src/threads/mtx_destroy.cpp
@@ -13,6 +13,6 @@
 
 namespace __llvm_libc {
 
-LLVM_LIBC_FUNCTION(void, mtx_destroy, (mtx_t * mutex)) {}
+LLVM_LIBC_FUNCTION(void, mtx_destroy, (mtx_t *)) {}
 
 } // namespace __llvm_libc
diff --git a/test/src/time/asctime_test.cpp b/test/src/time/asctime_test.cpp
index 0b0c159..93591de 100644
--- a/test/src/time/asctime_test.cpp
+++ b/test/src/time/asctime_test.cpp
@@ -28,60 +28,58 @@
 // Weekdays are in the range 0 to 6. Test passing invalid value in wday.
 TEST(LlvmLibcAsctime, InvalidWday) {
   struct tm tm_data;
-  char *result;
 
   // Test with wday = -1.
-  result = call_asctime(&tm_data,
-                        1970, // year
-                        1,    // month
-                        1,    // day
-                        0,    // hr
-                        0,    // min
-                        0,    // sec
-                        -1,   // wday
-                        0);   // yday
+  call_asctime(&tm_data,
+               1970, // year
+               1,    // month
+               1,    // day
+               0,    // hr
+               0,    // min
+               0,    // sec
+               -1,   // wday
+               0);   // yday
   ASSERT_EQ(EINVAL, llvmlibc_errno);
 
   // Test with wday = 7.
-  result = call_asctime(&tm_data,
-                        1970, // year
-                        1,    // month
-                        1,    // day
-                        0,    // hr
-                        0,    // min
-                        0,    // sec
-                        7,    // wday
-                        0);   // yday
+  call_asctime(&tm_data,
+               1970, // year
+               1,    // month
+               1,    // day
+               0,    // hr
+               0,    // min
+               0,    // sec
+               7,    // wday
+               0);   // yday
   ASSERT_EQ(EINVAL, llvmlibc_errno);
 }
 
 // Months are from January to December. Test passing invalid value in month.
 TEST(LlvmLibcAsctime, InvalidMonth) {
   struct tm tm_data;
-  char *result;
 
   // Test with month = 0.
-  result = call_asctime(&tm_data,
-                        1970, // year
-                        0,    // month
-                        1,    // day
-                        0,    // hr
-                        0,    // min
-                        0,    // sec
-                        4,    // wday
-                        0);   // yday
+  call_asctime(&tm_data,
+               1970, // year
+               0,    // month
+               1,    // day
+               0,    // hr
+               0,    // min
+               0,    // sec
+               4,    // wday
+               0);   // yday
   ASSERT_EQ(EINVAL, llvmlibc_errno);
 
   // Test with month = 13.
-  result = call_asctime(&tm_data,
-                        1970, // year
-                        13,   // month
-                        1,    // day
-                        0,    // hr
-                        0,    // min
-                        0,    // sec
-                        4,    // wday
-                        0);   // yday
+  call_asctime(&tm_data,
+               1970, // year
+               13,   // month
+               1,    // day
+               0,    // hr
+               0,    // min
+               0,    // sec
+               4,    // wday
+               0);   // yday
   ASSERT_EQ(EINVAL, llvmlibc_errno);
 }