Skip to content

Commit

Permalink
🔀 Merge branch 'ladislas/feature/fix-clang-tidy' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
ladislas committed Jan 29, 2022
2 parents ffeeff5 + c227961 commit a95edef
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-code_analysis-clang_tidy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 0 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 2 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -232,22 +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

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
| xargs --no-run-if-empty ruby tools/run-clang-tidy.rb $(CMAKE_TOOLS_BUILD_DIR)

#
# MARK: - Mbed targets
Expand Down
7 changes: 7 additions & 0 deletions cmake/ToolchainGCCArmBuild.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
97 changes: 97 additions & 0 deletions tools/run-clang-tidy.rb
Original file line number Diff line number Diff line change
@@ -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 = <<EOL
#{$CLANG_TIDY_EXEC} -p=#{$BUILD_DIR} #{$SEARCH_DIRS} --quiet #{$FILES}
EOL

puts ""
puts RUN_CLANG_TIDY_CMD
puts ""

if !system(RUN_CLANG_TIDY_CMD)
exit 1
end

0 comments on commit a95edef

Please sign in to comment.