[ASan] Support for cmake build of ASan unittests in 32-bit LLVM build. Currently, to run ASan unit tests both for 32- and 64 bits one has to maintain two distinct LLVM builds. In a bright future, we'd like to use a single build for this

git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@160666 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/asan/tests/CMakeLists.txt b/lib/asan/tests/CMakeLists.txt
index b85b0d0..8a9da45 100644
--- a/lib/asan/tests/CMakeLists.txt
+++ b/lib/asan/tests/CMakeLists.txt
@@ -19,6 +19,12 @@
   -Wno-format
   -fvisibility=hidden
 )
+# Support 64-bit and 32-bit builds.
+if(LLVM_BUILD_32_BITS)
+  list(APPEND ASAN_UNITTEST_COMMON_CFLAGS -m32)
+else()
+  list(APPEND ASAN_UNITTEST_COMMON_CFLAGS -m64)
+endif()
 
 set(ASAN_GTEST_INCLUDE_CFLAGS
   -I${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include
@@ -44,34 +50,37 @@
 add_custom_target(AsanTests)
 set_target_properties(AsanTests PROPERTIES FOLDER "ASan tests")
 function(add_asan_test testname)
-	add_unittest(AsanTests ${testname} ${ARGN})
-	if(CMAKE_SIZEOF_VOID_P EQUAL 4)
-		target_link_libraries(${testname} clang_rt.asan-i386)
-	else()
-		target_link_libraries(${testname} clang_rt.asan-x86_64)
-	endif()
-	if (APPLE)
-		# Darwin-specific linker flags.
-		set_property(TARGET ${testname} APPEND PROPERTY
-		  LINK_FLAGS "-framework CoreFoundation")
-	elseif (UNIX)
-		# Linux-specific linker flags.
-		set_property(TARGET ${testname} APPEND PROPERTY
-		  LINK_FLAGS "-lpthread -ldl -export-dynamic")
-	endif()
-	get_property(compile_flags TARGET ${testname} PROPERTY COMPILE_FLAGS)
-	foreach(arg ${ASAN_UNITTEST_COMMON_CFLAGS})
-	  set(add_compile_flags "${add_compile_flags} ${arg}")
-	endforeach(arg ${ASAN_UNITTEST_COMMON_CFLAGS})
-	set_property(TARGET ${testname} PROPERTY COMPILE_FLAGS
-	  "${compile_flags} ${add_compile_flags}")
+  add_unittest(AsanTests ${testname} ${ARGN})
+  if(LLVM_BUILD_32_BITS)
+    target_link_libraries(${testname} clang_rt.asan-i386)
+  else()
+    target_link_libraries(${testname} clang_rt.asan-x86_64)
+  endif()
+  if (APPLE)
+    # Darwin-specific linker flags.
+    set_property(TARGET ${testname} APPEND PROPERTY
+                 LINK_FLAGS "-framework Foundation")
+  elseif (UNIX)
+    # Linux-specific linker flags.
+    set_property(TARGET ${testname} APPEND PROPERTY
+                 LINK_FLAGS "-lpthread -ldl -export-dynamic")
+  endif()
+  set(add_compile_flags "")
+  get_property(compile_flags TARGET ${testname} PROPERTY COMPILE_FLAGS)
+  foreach(arg ${ASAN_UNITTEST_COMMON_CFLAGS})
+    set(add_compile_flags "${add_compile_flags} ${arg}")
+  endforeach(arg ${ASAN_UNITTEST_COMMON_CFLAGS})
+  set_property(TARGET ${testname} PROPERTY COMPILE_FLAGS
+               "${compile_flags} ${add_compile_flags}")
 endfunction()
 
-set(ASAN_TEST_FILES
+set(ASAN_NOINST_TEST_SOURCES
   asan_noinst_test.cc
   asan_break_optimization.cc
 )
 
+set(ASAN_INST_TEST_OBJECTS)
+
 # We only support building instrumented tests when we're not cross compiling
 # and targeting a unix-like system where we can predict viable compilation and
 # linking strategies.
@@ -80,43 +89,30 @@
   # This function is a custom routine to manage manually compiling source files
   # for unit tests with the just-built Clang binary, using the ASan
   # instrumentation, and linking them into a test executable.
-  function(add_asan_compile_cxx_command source)
+  function(add_asan_compile_command source extra_cflags)
+    set(output_obj "${source}.asan.o")
     add_custom_command(
-      OUTPUT "${source}.asan.o"
+      OUTPUT ${output_obj}
       COMMAND clang
               ${ASAN_UNITTEST_INSTRUMENTED_CFLAGS}
-              -c -o "${source}.asan.o"
+              ${extra_cflags}
+              -c -o "${output_obj}"
               ${CMAKE_CURRENT_SOURCE_DIR}/${source}
       MAIN_DEPENDENCY ${source}
       DEPENDS clang ${ARGN}
       )
   endfunction()
 
-  function(add_asan_compile_objc_command source)
-    add_custom_command(
-      OUTPUT "${source}.asan.o"
-      COMMAND clang
-              ${ASAN_UNITTEST_INSTRUMENTED_CFLAGS}
-              -ObjC -c -o "${source}.asan.o"
-              ${CMAKE_CURRENT_SOURCE_DIR}/${source}
-      MAIN_DEPENDENCY ${source}
-      DEPENDS clang ${ARGN}
-      )
-  endfunction()
-
-  add_asan_compile_cxx_command(asan_globals_test.cc)
-  add_asan_compile_cxx_command(asan_test.cc)
-  list(APPEND ASAN_TEST_FILES
-    asan_globals_test.cc.asan.o
-    asan_test.cc.asan.o
-  )
+  add_asan_compile_command(asan_globals_test.cc "")
+  add_asan_compile_command(asan_test.cc "")
+  list(APPEND ASAN_INST_TEST_OBJECTS asan_globals_test.cc.asan.o
+                                     asan_test.cc.asan.o)
   if (APPLE)
-    add_asan_compile_objc_command(asan_mac_test.mm)
-    list(APPEND ASAN_TEST_FILES asan_mac_test.mm.asan.o)
+    add_asan_compile_command(asan_mac_test.mm "-ObjC")
+    list(APPEND ASAN_INST_TEST_OBJECTS asan_mac_test.mm.asan.o)
   endif()
 
 endif()
 
-add_asan_test(AsanTest
-  ${ASAN_TEST_FILES}
-)
+add_asan_test(AsanTest ${ASAN_NOINST_TEST_SOURCES}
+                       ${ASAN_INST_TEST_OBJECTS})
diff --git a/lib/asan/tests/asan_break_optimization.cc b/lib/asan/tests/asan_break_optimization.cc
index 51fb17b..022a9f8 100644
--- a/lib/asan/tests/asan_break_optimization.cc
+++ b/lib/asan/tests/asan_break_optimization.cc
@@ -15,4 +15,5 @@
 // Have this function in a separate file to avoid inlining.
 // (Yes, we know about cross-file inlining, but let's assume we don't use it).
 extern "C" void break_optimization(void *x) {
+  (void)x;
 }
diff --git a/lib/asan/tests/asan_noinst_test.cc b/lib/asan/tests/asan_noinst_test.cc
index e93806e..8c3d126 100644
--- a/lib/asan/tests/asan_noinst_test.cc
+++ b/lib/asan/tests/asan_noinst_test.cc
@@ -221,6 +221,7 @@
       std::max((size_t)2, (size_t)my_rand(&seed) % (2 * kNumPcs));
     size_t n_frames =
       __asan::AsanStackTrace::CompressStack(&stack0, compressed, compress_size);
+    Ident(n_frames);
     assert(n_frames <= stack0.size);
     __asan::AsanStackTrace::UncompressStack(&stack1, compressed, compress_size);
     assert(stack1.size == n_frames);
@@ -275,6 +276,7 @@
 }
 
 void *ThreadedQuarantineTestWorker(void *unused) {
+  (void)unused;
   u32 seed = my_rand(&global_seed);
   __asan::AsanStackTrace stack;
   stack.trace[0] = 0x890;
@@ -302,6 +304,7 @@
 }
 
 void *ThreadedOneSizeMallocStress(void *unused) {
+  (void)unused;
   __asan::AsanStackTrace stack;
   stack.trace[0] = 0x890;
   stack.size = 1;
@@ -478,9 +481,10 @@
 
 static const size_t kManyThreadsMallocSizes[] = {5, 1UL<<10, 1UL<<20, 357};
 static const size_t kManyThreadsIterations = 250;
-static const size_t kManyThreadsNumThreads = 200;
+static const size_t kManyThreadsNumThreads = (__WORDSIZE == 32) ? 40 : 200;
 
 void *ManyThreadsWithStatsWorker(void *arg) {
+  (void)arg;
   for (size_t iter = 0; iter < kManyThreadsIterations; iter++) {
     for (size_t size_index = 0; size_index < 4; size_index++) {
       free(Ident(malloc(kManyThreadsMallocSizes[size_index])));