[PATCH] D30299: [tablegen] Optionally format tablegen targets with clang-format

Daniel Sanders via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 23 06:20:43 PST 2017


dsanders created this revision.
Herald added subscribers: mgorny, aemerson.

While it's useful to make a reasonable effort to format tablegen output in a
sensible way, it's also easy to get overly bogged down in the formatting
concerns.

This patch adds a way for tablegen-erated intermediate files to opt-in to being
filtered through clang-format when it's available. Files that opt-in to this
will fall back on the output that comes directly from tablegen.

To opt-in, invoke tablegen() using the FORMAT option like so:

  tablegen(LLVM AArch64GenRegisterBank.inc FORMAT OPTIONS -gen-register-bank)


https://reviews.llvm.org/D30299

Files:
  CMakeLists.txt
  cmake/modules/TableGen.cmake
  lib/Target/AArch64/CMakeLists.txt


Index: lib/Target/AArch64/CMakeLists.txt
===================================================================
--- lib/Target/AArch64/CMakeLists.txt
+++ lib/Target/AArch64/CMakeLists.txt
@@ -14,8 +14,8 @@
 tablegen(LLVM AArch64GenDisassemblerTables.inc -gen-disassembler)
 tablegen(LLVM AArch64GenSystemOperands.inc -gen-searchable-tables)
 if(LLVM_BUILD_GLOBAL_ISEL)
-  tablegen(LLVM AArch64GenRegisterBank.inc -gen-register-bank)
-  tablegen(LLVM AArch64GenGlobalISel.inc -gen-global-isel)
+  tablegen(LLVM AArch64GenRegisterBank.inc FORMAT OPTIONS -gen-register-bank)
+  tablegen(LLVM AArch64GenGlobalISel.inc FORMAT OPTIONS -gen-global-isel)
 endif()
 
 add_public_tablegen_target(AArch64CommonTableGen)
Index: cmake/modules/TableGen.cmake
===================================================================
--- cmake/modules/TableGen.cmake
+++ cmake/modules/TableGen.cmake
@@ -9,6 +9,9 @@
 endif()
 
 function(tablegen project ofn)
+  cmake_parse_arguments(tablegen "FORMAT" "" "OPTIONS" ${ARGN})
+  set(tablegen_OPTIONS ${tablegen_OPTIONS} ${tablegen_UNPARSED_ARGUMENTS})
+
   # Validate calling context.
   if(NOT ${project}_TABLEGEN_EXE)
     message(FATAL_ERROR "${project}_TABLEGEN_EXE not set")
@@ -24,18 +27,30 @@
       ${CMAKE_CURRENT_SOURCE_DIR}/${LLVM_TARGET_DEFINITIONS})
   endif()
   if (LLVM_ENABLE_DAGISEL_COV)
-    list(FIND ARGN "-gen-dag-isel" idx)
+    list(FIND tablegen_OPTIONS "-gen-dag-isel" idx)
     if( NOT idx EQUAL -1 )
       list(APPEND LLVM_TABLEGEN_FLAGS "-instrument-coverage")
     endif()
   endif()
 
+  set(tablegen_format_command)
+  if(tablegen_FORMAT)
+    if(CLANG_FORMAT_EXECUTABLE)
+      set(tablegen_format_command COMMAND ${CLANG_FORMAT_EXECUTABLE} -i ${CMAKE_CURRENT_BINARY_DIR}/${ofn}.tmp)
+    else()
+      message(STATUS "Not formatting tablegen output ${ofn} due to lack of clang-format")
+    endif()
+  else()
+  endif()
+
   add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${ofn}.tmp
     # Generate tablegen output in a temporary file.
-    COMMAND ${${project}_TABLEGEN_EXE} ${ARGN} -I ${CMAKE_CURRENT_SOURCE_DIR}
+    COMMAND ${${project}_TABLEGEN_EXE} ${tablegen_OPTIONS} -I ${CMAKE_CURRENT_SOURCE_DIR}
     ${LLVM_TABLEGEN_FLAGS} 
     ${LLVM_TARGET_DEFINITIONS_ABSOLUTE}
     -o ${CMAKE_CURRENT_BINARY_DIR}/${ofn}.tmp
+    # Add a formatting command if we want one.
+    ${tablegen_format_command}
     # The file in LLVM_TARGET_DEFINITIONS may be not in the current
     # directory and local_tds may not contain it, so we must
     # explicitly list it here:
Index: CMakeLists.txt
===================================================================
--- CMakeLists.txt
+++ CMakeLists.txt
@@ -157,6 +157,10 @@
 
 option(LLVM_DEPENDENCY_DEBUGGING "Dependency debugging mode to verify correctly expressed library dependencies (Darwin only)" OFF)
 
+# Some tablegen-erators like to format their output using clang-format when
+# it's available.
+find_program(CLANG_FORMAT_EXECUTABLE clang-format)
+
 # Some features of the LLVM build may be disallowed when dependency debugging is
 # enabled. In particular you cannot use ccache because we want to force compile
 # operations to always happen.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D30299.89505.patch
Type: text/x-patch
Size: 3162 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170223/51721824/attachment.bin>


More information about the llvm-commits mailing list