From ef0894b19c550cf8de8a183c38ca30d426c510ea Mon Sep 17 00:00:00 2001 From: Ladislas de Toldi Date: Tue, 18 Jan 2022 23:09:07 +0100 Subject: [PATCH 1/5] :hammer: (cmake): Explicitly show standard include directories in compilation database This should fix clang-tidy and make autocompletion better --- CMakeLists.txt | 3 --- cmake/ToolchainGCCArmBuild.cmake | 7 +++++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 64072c5005..0a0a118f41 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,9 +16,6 @@ endif(CCACHE) # Compile commands database set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "") -if(CMAKE_EXPORT_COMPILE_COMMANDS) - set(CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES}) -endif() # # MARK: - Compilation flags/options diff --git a/cmake/ToolchainGCCArmBuild.cmake b/cmake/ToolchainGCCArmBuild.cmake index e94684fd36..3bc9ef4d1c 100644 --- a/cmake/ToolchainGCCArmBuild.cmake +++ b/cmake/ToolchainGCCArmBuild.cmake @@ -66,3 +66,10 @@ target_link_libraries(mbed-os INTERFACE -T\"${PREPROCESSED_LINKER_SCRIPT}\" ${MBED_LINK_OPTIONS} ) + +# Export cross-compilation include for clang-tidy +if(CMAKE_EXPORT_COMPILE_COMMANDS) + set(CMAKE_ASM_STANDARD_INCLUDE_DIRECTORIES ${CMAKE_ASM_IMPLICIT_INCLUDE_DIRECTORIES}) + set(CMAKE_C_STANDARD_INCLUDE_DIRECTORIES ${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES}) + set(CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES}) +endif() From 2c7e817212cabe40b87c8f3d976b197069f22b0b Mon Sep 17 00:00:00 2001 From: Ladislas de Toldi Date: Sat, 29 Jan 2022 15:33:36 +0100 Subject: [PATCH 2/5] :technologist: (tools): Add run-clang-tidy.rb script This is a work around for: https://github.com/llvm/llvm-project/issues/53468 --- tools/run-clang-tidy.rb | 97 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 tools/run-clang-tidy.rb diff --git a/tools/run-clang-tidy.rb b/tools/run-clang-tidy.rb new file mode 100644 index 0000000000..0494c6878b --- /dev/null +++ b/tools/run-clang-tidy.rb @@ -0,0 +1,97 @@ +#!/usr/bin/env ruby + +# Leka - LekaOS +# Copyright 2022 APF France handicap +# SPDX-License-Identifier: Apache-2.0 + +SIS="#include <...> search starts here:" +SIE="End of search list." + +$DEBUG = false + +def log_debug(message) + if $DEBUG + puts message + end +end + +$tmp_search_dirs = [] + +# Previous trials, for posterity +# search_dirs_cmds = ["arm-none-eabi-g++ -E -Wp,-v -xc++ /dev/null 2>&1", "arm-none-eabi-gcc -E -Wp,-v -xc /dev/null 2>&1"] +search_dirs_cmds = ["arm-none-eabi-g++ -E -Wp,-v -xc++ /dev/null 2>&1"] + +search_dirs_cmds.each do |cmd| + log_debug "Find search dirs for #{cmd}" + raw_search_dirs = `#{cmd}` + search_dirs = raw_search_dirs.match(/#{SIS}(.*?)#{SIE}/m)[1] + + log_debug "Remove starting empty space" + search_dirs = search_dirs.gsub(/^\s*/, '') + + log_debug "Appending -extra-arg" + search_dirs = search_dirs.gsub!(/^/, '-extra-arg=-isystem\&') + + $tmp_search_dirs += search_dirs.split("\n") +end + +# At the moment this is not needed but might in the future, so we keep it here +# +# print_search_dirs_cmds = ["arm-none-eabi-gcc -print-search-dirs", "arm-none-eabi-g++ -print-search-dirs"] +# print_search_dirs_cmds.each do |cmd| +# log_debug "Find search dirs for #{cmd}" +# raw_search_dirs = `#{cmd}` + +# log_debug "Remove prefixes" +# search_dirs = raw_search_dirs.gsub('install: ', '').gsub('programs: =', '').gsub('libraries: =', '') + +# log_debug "Split lines" +# search_dirs = search_dirs.gsub('/:/', "/\n/") + +# log_debug "Appending -extra-arg" +# search_dirs = search_dirs.gsub!(/^/, '-extra-arg=-isystem\&') + +# $tmp_search_dirs += search_dirs.split("\n") +# end + +log_debug "Remove duplicates" +$tmp_search_dirs = $tmp_search_dirs.uniq + +log_debug "Find clang-tidy" +$CLANG_TIDY_EXEC = "" + +if system("which clang-tidy > /dev/null") + $CLANG_TIDY_EXEC = "clang-tidy" +elsif system("which clang-tidy-13 > /dev/null") + $CLANG_TIDY_EXEC = "clang-tidy-13" +elsif system("which /opt/homebrew/opt/llvm/bin/clang-tidy > /dev/null") + $CLANG_TIDY_EXEC = "/opt/homebrew/opt/llvm/bin/clang-tidy" +elsif system("which /usr/local/opt/llvm/bin/clang-tidy > /dev/null") + $CLANG_TIDY_EXEC = "/usr/local/opt/llvm/bin/clang-tidy" +end + +puts "Clang-tidy found at: #{$CLANG_TIDY_EXEC}" + +puts "Standard headers search dirs:" +puts $tmp_search_dirs.map { |d| " #{d}" }.join("\n") +puts "" + +$BUILD_DIR = ARGV.shift +$SEARCH_DIRS = $tmp_search_dirs.join("\s") +$FILES = ARGV.join(" ") +$FILES_ARR = ARGV + +puts "Running clang-tidy on:" +puts $FILES_ARR.map { |f| " #{f}" }.join("\n") + +RUN_CLANG_TIDY_CMD = < Date: Sat, 29 Jan 2022 15:42:36 +0100 Subject: [PATCH 3/5] :hammer: (make): Update clang_tidy_diff with run-clang-tidy.rb script --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 69aa024034..3d5f465ed1 100644 --- a/Makefile +++ b/Makefile @@ -232,11 +232,11 @@ clang_tidy_diff: @echo "๐Ÿƒโ€โ™‚๏ธ Running clang-tidy on modified files ๐Ÿงน" @echo "" @git diff --name-status develop \ - | grep -E -v "_test" | grep -E "^A|^M" | sed "s/^[AM]\t//g" | grep -E "\.h\$$|\.cpp\$$" + | grep -E -v "_test" | grep -E "^A|^M" | sed "s/^[AM]\t//g" | grep -E "\.h\$$|\.cpp\$$" || echo "No files added nor modified!" @echo "" @git diff --name-status develop \ | grep -E -v "_test" | grep -E "^A|^M" | sed "s/^[AM]\t//g" | grep -E "\.h\$$|\.cpp\$$" \ - | xargs /usr/local/opt/llvm/bin/clang-tidy -p=. --quiet + | xargs --no-run-if-empty ruby tools/run-clang-tidy.rb $(CMAKE_TOOLS_BUILD_DIR) clang_tidy_diff_fix: @echo "" From c4444c6d8683090d68ff7521eef40425c8184e25 Mon Sep 17 00:00:00 2001 From: Ladislas de Toldi Date: Sat, 29 Jan 2022 15:43:25 +0100 Subject: [PATCH 4/5] :wastebasket: (make): Remove clang_tidy_diff_fix Never really worked and thus never really used --- Makefile | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/Makefile b/Makefile index 3d5f465ed1..30695a406b 100644 --- a/Makefile +++ b/Makefile @@ -238,17 +238,6 @@ clang_tidy_diff: | grep -E -v "_test" | grep -E "^A|^M" | sed "s/^[AM]\t//g" | grep -E "\.h\$$|\.cpp\$$" \ | xargs --no-run-if-empty ruby tools/run-clang-tidy.rb $(CMAKE_TOOLS_BUILD_DIR) -clang_tidy_diff_fix: - @echo "" - @echo "๐Ÿƒโ€โ™‚๏ธ Running clang-tidy on modified files ๐Ÿงน" - @echo "" - @git diff --name-status develop \ - | grep -E -v "_test" | grep -E "^A|^M" | sed "s/^[AM]\t//g" | grep -E "\.h\$$|\.cpp\$$" - @echo "" - @git diff --name-status develop \ - | grep -E -v "_test" | grep -E "^A|^M" | sed "s/^[AM]\t//g" | grep -E "\.h\$$|\.cpp\$$" \ - | xargs /usr/local/opt/llvm/bin/clang-tidy -p=. --quiet --fix --fix-errors - # # MARK: - Mbed targets # From c227961e819ae95b6c030e13125eac507fc1ec41 Mon Sep 17 00:00:00 2001 From: Ladislas de Toldi Date: Sat, 29 Jan 2022 16:58:47 +0100 Subject: [PATCH 5/5] :bug: (workflows): Use run-clang-tidy.rb to fix clang-tidy CI --- .github/workflows/ci-code_analysis-clang_tidy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci-code_analysis-clang_tidy.yml b/.github/workflows/ci-code_analysis-clang_tidy.yml index 2ddde77868..8fc34bce48 100644 --- a/.github/workflows/ci-code_analysis-clang_tidy.yml +++ b/.github/workflows/ci-code_analysis-clang_tidy.yml @@ -73,4 +73,4 @@ jobs: | grep -E -v "mocks" \ | grep -E -v "stubs" \ | grep -E "\.h$|\.cpp$" \ - | xargs --no-run-if-empty clang-tidy-13 -p=_build_cmake_tools --quiet + | xargs --no-run-if-empty ruby tools/run-clang-tidy.rb _build_cmake_tools