[llvm] f1d3bc4 - [CMake] Replace list(FIND) by if(IN_LIST) where index isn't used

Aaron Puchert via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 23 14:59:38 PST 2023


Author: Aaron Puchert
Date: 2023-01-23T23:59:31+01:00
New Revision: f1d3bc49634cfefe35de3aa631cef9f808b12896

URL: https://github.com/llvm/llvm-project/commit/f1d3bc49634cfefe35de3aa631cef9f808b12896
DIFF: https://github.com/llvm/llvm-project/commit/f1d3bc49634cfefe35de3aa631cef9f808b12896.diff

LOG: [CMake] Replace list(FIND) by if(IN_LIST) where index isn't used

If we don't use the index otherwise, if(IN_LIST) is more readable and
doesn't clutter the local scope with index variables.

This was pointed out by @beanz in D96670.

Reviewed By: beanz

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

Added: 
    

Modified: 
    llvm/cmake/config-ix.cmake
    llvm/cmake/modules/AddLLVM.cmake
    llvm/cmake/modules/AddSphinxTarget.cmake
    llvm/cmake/modules/ChooseMSVCCRT.cmake
    llvm/cmake/modules/LLVM-Config.cmake
    llvm/cmake/modules/LLVMExternalProjectUtils.cmake
    llvm/cmake/modules/LLVMProcessSources.cmake
    llvm/cmake/modules/TableGen.cmake

Removed: 
    


################################################################################
diff  --git a/llvm/cmake/config-ix.cmake b/llvm/cmake/config-ix.cmake
index f58f3b13a080..f959cd1f8848 100644
--- a/llvm/cmake/config-ix.cmake
+++ b/llvm/cmake/config-ix.cmake
@@ -508,8 +508,7 @@ foreach (NATIVE_KEYWORD host Native)
   endif()
 endforeach()
 
-list(FIND LLVM_TARGETS_TO_BUILD ${LLVM_NATIVE_ARCH} NATIVE_ARCH_IDX)
-if (NATIVE_ARCH_IDX EQUAL -1)
+if (NOT ${LLVM_NATIVE_ARCH} IN_LIST LLVM_TARGETS_TO_BUILD)
   message(STATUS
     "Native target ${LLVM_NATIVE_ARCH} is not selected; lli will not JIT code")
 else ()

diff  --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake
index 38b0f18fcb4f..9eef4eb7e35d 100644
--- a/llvm/cmake/modules/AddLLVM.cmake
+++ b/llvm/cmake/modules/AddLLVM.cmake
@@ -1228,8 +1228,7 @@ function(export_executable_symbols target)
           endif()
           get_target_property(transitive_libs ${lib} INTERFACE_LINK_LIBRARIES)
           foreach(transitive_lib ${transitive_libs})
-            list(FIND link_libs ${transitive_lib} idx)
-            if(TARGET ${transitive_lib} AND idx EQUAL -1)
+            if(TARGET ${transitive_lib} AND NOT ${transitive_lib} IN_LIST link_libs)
               list(APPEND newer_libs ${transitive_lib})
               list(APPEND link_libs ${transitive_lib})
             endif()

diff  --git a/llvm/cmake/modules/AddSphinxTarget.cmake b/llvm/cmake/modules/AddSphinxTarget.cmake
index bbc889c6b085..b90639fbbf07 100644
--- a/llvm/cmake/modules/AddSphinxTarget.cmake
+++ b/llvm/cmake/modules/AddSphinxTarget.cmake
@@ -68,8 +68,7 @@ function (add_sphinx_target builder project)
   # but we should only add this path once
   get_property(_CURRENT_MAKE_CLEAN_FILES
                DIRECTORY PROPERTY ADDITIONAL_MAKE_CLEAN_FILES)
-  list(FIND _CURRENT_MAKE_CLEAN_FILES "${SPHINX_DOC_TREE_DIR}" _INDEX)
-  if (_INDEX EQUAL -1)
+  if (NOT "${SPHINX_DOC_TREE_DIR}" IN_LIST _CURRENT_MAKE_CLEAN_FILES)
     set_property(DIRECTORY APPEND PROPERTY
                  ADDITIONAL_MAKE_CLEAN_FILES
                  "${SPHINX_DOC_TREE_DIR}")

diff  --git a/llvm/cmake/modules/ChooseMSVCCRT.cmake b/llvm/cmake/modules/ChooseMSVCCRT.cmake
index 0e6e1aa55254..54bc379bd0c4 100644
--- a/llvm/cmake/modules/ChooseMSVCCRT.cmake
+++ b/llvm/cmake/modules/ChooseMSVCCRT.cmake
@@ -81,11 +81,10 @@ variables (LLVM_USE_CRT_DEBUG, etc) instead.")
       set(flag_string " ")
     else()
       set(flag_string " /${LLVM_USE_CRT_${build}} ")
-      list(FIND ${MSVC_CRT} ${LLVM_USE_CRT_${build}} idx)
-      if (idx LESS 0)
+      if (NOT ${LLVM_USE_CRT_${build}} IN_LIST ${MSVC_CRT})
         message(FATAL_ERROR
           "Invalid value for LLVM_USE_CRT_${build}: ${LLVM_USE_CRT_${build}}. Valid options are one of: ${${MSVC_CRT}}")
-      endif (idx LESS 0)
+      endif()
       message(STATUS "Using ${build_type} VC++ CRT: ${LLVM_USE_CRT_${build}}")
     endif()
     foreach(lang C CXX)

diff  --git a/llvm/cmake/modules/LLVM-Config.cmake b/llvm/cmake/modules/LLVM-Config.cmake
index 7ef8a8aa808b..a5a7be61dce0 100644
--- a/llvm/cmake/modules/LLVM-Config.cmake
+++ b/llvm/cmake/modules/LLVM-Config.cmake
@@ -131,29 +131,24 @@ function(llvm_expand_pseudo_components out_components)
   endif()
   foreach(c ${link_components})
     # add codegen, asmprinter, asmparser, disassembler
-    list(FIND LLVM_TARGETS_TO_BUILD ${c} idx)
-    if( NOT idx LESS 0 )
-      list(FIND LLVM_AVAILABLE_LIBS LLVM${c}CodeGen lib_idx)
-      if( lib_idx GREATER_EQUAL 0 )
+    if(${c} IN_LIST LLVM_TARGETS_TO_BUILD)
+      if(LLVM${c}CodeGen IN_LIST LLVM_AVAILABLE_LIBS)
         list(APPEND expanded_components "${c}CodeGen")
       else()
-        list(FIND LLVM_AVAILABLE_LIBS LLVM${c} lib_idx)
-        if( lib_idx GREATER_EQUAL 0 )
+        if(LLVM${c} IN_LIST LLVM_AVAILABLE_LIBS)
           list(APPEND expanded_components "${c}")
         else()
           message(FATAL_ERROR "Target ${c} is not in the set of libraries.")
         endif()
       endif()
       foreach(subcomponent IN ITEMS AsmPrinter AsmParser Desc Disassembler Info Utils)
-        list(FIND LLVM_AVAILABLE_LIBS LLVM${c}${subcomponent} lib_idx)
-        if( lib_idx GREATER_EQUAL 0 )
+        if(LLVM${c}${subcomponent} IN_LIST LLVM_AVAILABLE_LIBS)
           list(APPEND expanded_components "${c}${subcomponent}")
         endif()
       endforeach()
     elseif( c STREQUAL "nativecodegen" )
       foreach(subcomponent IN ITEMS CodeGen Desc Info)
-        list(FIND LLVM_AVAILABLE_LIBS LLVM${LLVM_NATIVE_ARCH}${subcomponent} lib_idx)
-        if( lib_idx GREATER_EQUAL 0 )
+        if(LLVM${LLVM_NATIVE_ARCH}${subcomponent} IN_LIST LLVM_AVAILABLE_LIBS)
           list(APPEND expanded_components "${LLVM_NATIVE_ARCH}${subcomponent}")
         endif()
       endforeach()
@@ -167,32 +162,28 @@ function(llvm_expand_pseudo_components out_components)
     elseif( c STREQUAL "AllTargetsAsmParsers" )
       # Link all the asm parsers from all the targets
       foreach(t ${LLVM_TARGETS_TO_BUILD})
-        list(FIND LLVM_AVAILABLE_LIBS LLVM${t}AsmParser lib_idx)
-        if( lib_idx GREATER_EQUAL 0 )
+        if(LLVM${t}AsmParser IN_LIST LLVM_AVAILABLE_LIBS)
           list(APPEND expanded_components "${t}AsmParser")
         endif()
       endforeach(t)
     elseif( c STREQUAL "AllTargetsDescs" )
       # Link all the descs from all the targets
       foreach(t ${LLVM_TARGETS_TO_BUILD})
-        list(FIND LLVM_AVAILABLE_LIBS LLVM${t}Desc lib_idx)
-        if( lib_idx GREATER_EQUAL 0 )
+        if(LLVM${t}Desc IN_LIST LLVM_AVAILABLE_LIBS)
           list(APPEND expanded_components "${t}Desc")
         endif()
       endforeach(t)
     elseif( c STREQUAL "AllTargetsDisassemblers" )
       # Link all the disassemblers from all the targets
       foreach(t ${LLVM_TARGETS_TO_BUILD})
-        list(FIND LLVM_AVAILABLE_LIBS LLVM${t}Disassembler lib_idx)
-        if( lib_idx GREATER_EQUAL 0 )
+        if(LLVM${t}Disassembler IN_LIST LLVM_AVAILABLE_LIBS)
           list(APPEND expanded_components "${t}Disassembler")
         endif()
       endforeach(t)
     elseif( c STREQUAL "AllTargetsInfos" )
       # Link all the infos from all the targets
       foreach(t ${LLVM_TARGETS_TO_BUILD})
-        list(FIND LLVM_AVAILABLE_LIBS LLVM${t}Info lib_idx)
-        if( lib_idx GREATER_EQUAL 0 )
+        if(LLVM${t}Info IN_LIST LLVM_AVAILABLE_LIBS)
           list(APPEND expanded_components "${t}Info")
         endif()
       endforeach(t)
@@ -236,22 +227,17 @@ function(llvm_map_components_to_libnames out_libs)
   endif()
 
   # Expand some keywords:
-  list(FIND LLVM_TARGETS_TO_BUILD "${LLVM_NATIVE_ARCH}" have_native_backend)
-  list(FIND link_components "engine" engine_required)
-  if( NOT engine_required EQUAL -1 )
-    list(FIND LLVM_TARGETS_WITH_JIT "${LLVM_NATIVE_ARCH}" have_jit)
-    if( NOT have_native_backend EQUAL -1 AND NOT have_jit EQUAL -1 )
+  if(engine IN_LIST link_components)
+    if(${LLVM_NATIVE_ARCH} IN_LIST LLVM_TARGETS_TO_BUILD AND
+       ${LLVM_NATIVE_ARCH} IN_LIST LLVM_TARGETS_WITH_JIT)
       list(APPEND link_components "jit")
       list(APPEND link_components "native")
     else()
       list(APPEND link_components "interpreter")
     endif()
   endif()
-  list(FIND link_components "native" native_required)
-  if( NOT native_required EQUAL -1 )
-    if( NOT have_native_backend EQUAL -1 )
-      list(APPEND link_components ${LLVM_NATIVE_ARCH})
-    endif()
+  if(native IN_LIST link_components AND ${LLVM_NATIVE_ARCH} IN_LIST LLVM_TARGETS_TO_BUILD)
+    list(APPEND link_components ${LLVM_NATIVE_ARCH})
   endif()
 
   # Translate symbolic component names to real libraries:
@@ -301,8 +287,7 @@ endfunction()
 # This duplicates the algorithm used by llvm-config, originally
 # in tools/llvm-config/llvm-config.cpp, function ComputeLibsForComponents.
 function(expand_topologically name required_libs visited_libs)
-  list(FIND visited_libs ${name} found)
-  if( found LESS 0 )
+  if(NOT ${name} IN_LIST visited_libs)
     list(APPEND visited_libs ${name})
     set(visited_libs ${visited_libs} PARENT_SCOPE)
 

diff  --git a/llvm/cmake/modules/LLVMExternalProjectUtils.cmake b/llvm/cmake/modules/LLVMExternalProjectUtils.cmake
index ff6cb11f7d1b..05bfd70fe54c 100644
--- a/llvm/cmake/modules/LLVMExternalProjectUtils.cmake
+++ b/llvm/cmake/modules/LLVMExternalProjectUtils.cmake
@@ -130,8 +130,7 @@ function(llvm_ExternalProject_Add name source_dir)
     set(always_clean clean)
   endif()
 
-  list(FIND TOOLCHAIN_TOOLS clang FOUND_CLANG)
-  if(FOUND_CLANG GREATER -1)
+  if(clang IN_LIST TOOLCHAIN_TOOLS)
     set(CLANG_IN_TOOLCHAIN On)
   endif()
 

diff  --git a/llvm/cmake/modules/LLVMProcessSources.cmake b/llvm/cmake/modules/LLVMProcessSources.cmake
index ba8dca313c86..e021943d4b2e 100644
--- a/llvm/cmake/modules/LLVMProcessSources.cmake
+++ b/llvm/cmake/modules/LLVMProcessSources.cmake
@@ -102,10 +102,8 @@ function(llvm_check_source_file_list)
     # Don't reject hidden files. Some editors create backups in the
     # same directory as the file.
     if (NOT "${fn}" MATCHES "^\\.")
-      list(FIND LLVM_OPTIONAL_SOURCES ${entry} idx)
-      if( idx LESS 0 )
-        list(FIND listed ${gp} idx)
-        if( idx LESS 0 )
+      if(NOT ${entry} IN_LIST LLVM_OPTIONAL_SOURCES)
+        if(NOT ${gp} IN_LIST listed)
           if(ARG_SOURCE_DIR)
               set(fn_relative "${ARG_SOURCE_DIR}/${fn}")
           else()

diff  --git a/llvm/cmake/modules/TableGen.cmake b/llvm/cmake/modules/TableGen.cmake
index f7fe197ad9e6..70c2edb8c833 100644
--- a/llvm/cmake/modules/TableGen.cmake
+++ b/llvm/cmake/modules/TableGen.cmake
@@ -42,24 +42,15 @@ function(tablegen project ofn)
     set(LLVM_TARGET_DEFINITIONS_ABSOLUTE
       ${CMAKE_CURRENT_SOURCE_DIR}/${LLVM_TARGET_DEFINITIONS})
   endif()
-  if (LLVM_ENABLE_DAGISEL_COV)
-    list(FIND ARGN "-gen-dag-isel" idx)
-    if( NOT idx EQUAL -1 )
-      list(APPEND LLVM_TABLEGEN_FLAGS "-instrument-coverage")
-    endif()
+  if (LLVM_ENABLE_DAGISEL_COV AND "-gen-dag-isel" IN_LIST ARGN)
+    list(APPEND LLVM_TABLEGEN_FLAGS "-instrument-coverage")
   endif()
-  if (LLVM_ENABLE_GISEL_COV)
-    list(FIND ARGN "-gen-global-isel" idx)
-    if( NOT idx EQUAL -1 )
-      list(APPEND LLVM_TABLEGEN_FLAGS "-instrument-gisel-coverage")
-      list(APPEND LLVM_TABLEGEN_FLAGS "-gisel-coverage-file=${LLVM_GISEL_COV_PREFIX}all")
-    endif()
+  if (LLVM_ENABLE_GISEL_COV AND "-gen-global-isel" IN_LIST ARGN)
+    list(APPEND LLVM_TABLEGEN_FLAGS "-instrument-gisel-coverage")
+    list(APPEND LLVM_TABLEGEN_FLAGS "-gisel-coverage-file=${LLVM_GISEL_COV_PREFIX}all")
   endif()
-  if (LLVM_OMIT_DAGISEL_COMMENTS)
-    list(FIND ARGN "-gen-dag-isel" idx)
-    if (NOT idx EQUAL -1)
-      list(APPEND LLVM_TABLEGEN_FLAGS "-omit-comments")
-    endif()
+  if (LLVM_OMIT_DAGISEL_COMMENTS AND "-gen-dag-isel" IN_LIST ARGN)
+    list(APPEND LLVM_TABLEGEN_FLAGS "-omit-comments")
   endif()
 
   # MSVC can't support long string literals ("long" > 65534 bytes)[1], so if there's


        


More information about the llvm-commits mailing list