mirror of
https://github.com/esphome/esphome.git
synced 2025-10-04 19:03:47 +01:00
Refactor clang-tidy script to use actual compiler flags and includes (#2133)
Co-authored-by: Otto winter <otto@otto-winter.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import multiprocessing
|
||||
import os
|
||||
import queue
|
||||
@@ -15,27 +16,57 @@ import click
|
||||
import pexpect
|
||||
|
||||
sys.path.append(os.path.dirname(__file__))
|
||||
from helpers import basepath, shlex_quote, get_output, build_compile_commands, \
|
||||
build_all_include, temp_header_file, git_ls_files, filter_changed
|
||||
from helpers import shlex_quote, get_output, \
|
||||
build_all_include, temp_header_file, git_ls_files, filter_changed, load_idedata
|
||||
|
||||
|
||||
def run_tidy(args, tmpdir, queue, lock, failed_files):
|
||||
def clang_options(idedata):
|
||||
cmd = [
|
||||
# target 32-bit arch (this prevents size mismatch errors on a 64-bit host)
|
||||
'-m32',
|
||||
# disable built-in include directories from the host
|
||||
'-nostdinc',
|
||||
'-nostdinc++',
|
||||
# allow to condition code on the presence of clang-tidy
|
||||
'-DCLANG_TIDY'
|
||||
]
|
||||
|
||||
# copy compiler flags, except those clang doesn't understand.
|
||||
cmd.extend(flag for flag in idedata['cxx_flags'].split(' ')
|
||||
if flag not in ('-free', '-fipa-pta', '-mlongcalls', '-mtext-section-literals'))
|
||||
|
||||
# defines
|
||||
cmd.extend(f'-D{define}' for define in idedata['defines'])
|
||||
|
||||
# add include directories, using -isystem for dependencies to suppress their errors
|
||||
for directory in idedata['includes']['toolchain']:
|
||||
cmd.extend(['-isystem', directory])
|
||||
for directory in sorted(set(idedata['includes']['build'])):
|
||||
dependency = "framework-arduino" in directory or "/libdeps/" in directory
|
||||
cmd.extend(['-isystem' if dependency else '-I', directory])
|
||||
|
||||
return cmd
|
||||
|
||||
|
||||
def run_tidy(args, options, tmpdir, queue, lock, failed_files):
|
||||
while True:
|
||||
path = queue.get()
|
||||
invocation = ['clang-tidy-11', '-header-filter=^{}/.*'.format(re.escape(basepath))]
|
||||
invocation = ['clang-tidy-11']
|
||||
|
||||
if tmpdir is not None:
|
||||
invocation.append('-export-fixes')
|
||||
invocation.append('--export-fixes')
|
||||
# Get a temporary file. We immediately close the handle so clang-tidy can
|
||||
# overwrite it.
|
||||
(handle, name) = tempfile.mkstemp(suffix='.yaml', dir=tmpdir)
|
||||
os.close(handle)
|
||||
invocation.append(name)
|
||||
invocation.append('-p=.')
|
||||
|
||||
if args.quiet:
|
||||
invocation.append('-quiet')
|
||||
for arg in ['-Wfor-loop-analysis', '-Wshadow-field', '-Wshadow-field-in-constructor']:
|
||||
invocation.append('-extra-arg={}'.format(arg))
|
||||
|
||||
invocation.append(os.path.abspath(path))
|
||||
invocation.append('--')
|
||||
invocation.extend(options)
|
||||
invocation_s = ' '.join(shlex_quote(x) for x in invocation)
|
||||
|
||||
# Use pexpect for a pseudy-TTY with colored output
|
||||
@@ -95,8 +126,8 @@ def main():
|
||||
""")
|
||||
return 1
|
||||
|
||||
build_all_include()
|
||||
build_compile_commands()
|
||||
idedata = load_idedata("esp8266-tidy")
|
||||
options = clang_options(idedata)
|
||||
|
||||
files = []
|
||||
for path in git_ls_files(['*.cpp']):
|
||||
@@ -116,6 +147,7 @@ def main():
|
||||
files = split_list(files, args.split_num)[args.split_at - 1]
|
||||
|
||||
if args.all_headers and args.split_at in (None, 1):
|
||||
build_all_include()
|
||||
files.insert(0, temp_header_file)
|
||||
|
||||
tmpdir = None
|
||||
@@ -128,7 +160,7 @@ def main():
|
||||
lock = threading.Lock()
|
||||
for _ in range(args.jobs):
|
||||
t = threading.Thread(target=run_tidy,
|
||||
args=(args, tmpdir, task_queue, lock, failed_files))
|
||||
args=(args, options, tmpdir, task_queue, lock, failed_files))
|
||||
t.daemon = True
|
||||
t.start()
|
||||
|
||||
|
Reference in New Issue
Block a user