diff --git a/.clang-tidy b/.clang-tidy index 2f4fa78c090..92dcba5e296 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -72,9 +72,6 @@ Checks: '*, -llvm-header-guard, -llvm-include-order, -llvm-namespace-comment, - -misc-incorrect-roundings, - -misc-macro-parentheses, - -misc-misplaced-widening-cast, -misc-redundant-expression, -misc-unconventional-assign-operator, -misc-unused-parameters, @@ -177,6 +174,7 @@ Checks: '*, -misc-definitions-in-headers, -bugprone-casting-through-void, -readability-redundant-string-init, + -google-readability-avoid-underscore-in-googletest-name, ' WarningsAsErrors: '*' CheckOptions: diff --git a/.github/workflows/clang-tidy.yml b/.github/workflows/clang-tidy.yml index dfd622738cb..57e9028055d 100644 --- a/.github/workflows/clang-tidy.yml +++ b/.github/workflows/clang-tidy.yml @@ -95,7 +95,7 @@ jobs: # Two clang build directories, prepared one after the other so their # configures never fetch submodules at the same time: - # - build/px4_sitl_default-clang-test: configure-only, BUILD_TESTING=ON + # - build/px4_sitl_default-clang-test: configure + generated headers only, BUILD_TESTING=ON # (used by clang-tidy-diff-18.py so test files are in the # compilation database with resolved gtest/fuzztest includes) # - build/px4_sitl_default-clang: full build, BUILD_TESTING=OFF @@ -104,6 +104,20 @@ jobs: - name: Configure clang SITL with tests run: make px4_sitl_default-clang-test + # The test build dir is never built, so generate its headers (uORB + # topics, events, parameters, mavlink, ...). Without them every TU + # stops at a missing include and clang-tidy-diff reports on the + # half-parsed AST, e.g. events::send(...) as a chained comparison. + # fuzztest's json_grammar.h is skipped: generating it builds abseil + # and the antlr4 runtime, and no PX4 code includes it. + - name: Generate headers for clang SITL with tests + working-directory: build/px4_sitl_default-clang-test + shell: bash + run: | + ninja -t targets all \ + | awk -F': ' '$2 == "CUSTOM_COMMAND" && $1 ~ /\.(h|hpp)$/ && $1 !~ /^(\/|test\/fuzztest\/)/ {print $1}' \ + | xargs -r ninja + - name: Build clang SITL run: make -j$(nproc) px4_sitl_default-clang @@ -126,9 +140,13 @@ jobs: --build-dir build/px4_sitl_default-clang-test \ --base-ref origin/${{ github.base_ref }} \ --out pr-review/diff.patch + # The -checks list turns checks off for these comments only; the + # gating run above keeps them. Here they also lint test code, where + # they flag gtest fixtures and binary packet buffers. if [ -s pr-review/diff.patch ]; then clang-tidy-diff-18.py -p1 \ -path build/px4_sitl_default-clang-test \ + -checks=-cppcoreguidelines-virtual-class-destructor,-readability-suspicious-call-argument,-bugprone-not-null-terminated-result \ -export-fixes pr-review/fixes.yml \ -j0 < pr-review/diff.patch || true else diff --git a/Tools/ci/clang-tidy-diff-filter.py b/Tools/ci/clang-tidy-diff-filter.py index 5f516c5d01e..d6764949cb2 100644 --- a/Tools/ci/clang-tidy-diff-filter.py +++ b/Tools/ci/clang-tidy-diff-filter.py @@ -11,9 +11,15 @@ actually analyze against the current compilation database: compiled. Feeding them to clang-tidy-diff produces spurious "header not found" errors (gtest/gtest.h in particular). - - Header files (.h, .hpp, .hxx) always pass through. clang-tidy - analyzes header changes via the TUs that include them; there is - no separate TU for a header to match against the database. + - C++ headers (.hpp, .hxx) pass through. clang-tidy-diff parses each + one on its own, with flags borrowed from a nearby source file. + Those under boards/ or platforms/nuttx/ are dropped: they are not + built for SITL, so the borrowed flags are not theirs. + + - .h headers are dropped. Many are shared with C or are board/NuttX + configuration, and parsed alone as C++ they draw findings that do + not apply. The gating clang-tidy run's header filter only matches + .hpp, so it never lints them either. - All other files (CMakeLists.txt, .yml, .md, etc.) are dropped. @@ -32,7 +38,8 @@ import sys SOURCE_EXTS = {'.c', '.cpp', '.cc', '.cxx', '.m', '.mm'} -HEADER_EXTS = {'.h', '.hpp', '.hxx'} +HEADER_EXTS = {'.hpp', '.hxx'} +EXCLUDED_HEADER_DIRS = ('boards/', 'platforms/nuttx/') def load_db_files(build_dir): @@ -65,7 +72,7 @@ def keep_file(path, db_files): """Decide whether to keep this path in the filtered diff.""" ext = os.path.splitext(path)[1].lower() if ext in HEADER_EXTS: - return True + return not path.startswith(EXCLUDED_HEADER_DIRS) if ext in SOURCE_EXTS: return path in db_files return False diff --git a/Tools/ci/clang-tidy-fixes-to-review.py b/Tools/ci/clang-tidy-fixes-to-review.py index 3567ae4f76f..16a3607c988 100644 --- a/Tools/ci/clang-tidy-fixes-to-review.py +++ b/Tools/ci/clang-tidy-fixes-to-review.py @@ -87,6 +87,10 @@ import _github_helpers from _github_helpers import fail as _fail +# Author of the comments the PR Review Poster posts with GITHUB_TOKEN. +BOT_LOGIN = 'github-actions[bot]' + + # Markers used inside the per-comment body to call out severity. Plain # strings rather than emojis to keep the file emoji-free per project # preferences; the rendered Markdown is unaffected. @@ -98,22 +102,16 @@ SINGLE_COMMENT_MARKERS = { } -# Diagnostics we never surface as PR review comments. `file not found` from -# clang-diagnostic-error fires whenever clang-tidy analyzes a header whose -# including TU is not in the active compile_commands.json (e.g. board- -# specific NuttX headers against the SITL clang build). It is noise here, -# not a signal: the build_all_targets CI matrix compiles those headers for -# real and will fail loudly if an include is actually missing. -DROPPED_DIAGNOSTIC_PATTERNS = ( - ('clang-diagnostic-error', 'file not found'), -) +# Diagnostics we never surface as PR review comments. clang-diagnostic-error +# is a compile error, and here it almost always comes from parsing a header +# on its own or a file whose real flags differ from the SITL clang build's +# (missing includes, NuttX-only types, an #error for another platform). The +# build jobs compile every file for real and fail loudly on a genuine error. +DROPPED_DIAGNOSTICS = ('clang-diagnostic-error',) -def is_dropped_diagnostic(diag_name, diag_message): - for name, needle in DROPPED_DIAGNOSTIC_PATTERNS: - if diag_name == name and needle in (diag_message or ''): - return True - return False +def is_dropped_diagnostic(diag_name): + return diag_name in DROPPED_DIAGNOSTICS # --------------------------------------------------------------------------- @@ -156,6 +154,23 @@ def fetch_pull_request_files(client, repo, pr_number): yield entry +def fetch_posted_comment_keys(client, repo, pr_number): + """Return the (path, line, body) of every review comment the bot + already posted on the PR. + + COMMENT reviews cannot be dismissed, so without this every push posts + the same findings again. GitHub moves `line` to the current head and + sets it to null once the comment is outdated, so a finding that only + moved still matches, and one whose comment went outdated posts again. + """ + path = 'repos/{}/pulls/{}/comments'.format(repo, pr_number) + return { + (c.get('path'), c.get('line'), c.get('body')) + for c in client.paginated(path) + if (c.get('user') or {}).get('login') == BOT_LOGIN + } + + # --------------------------------------------------------------------------- # Diagnostic ordering (adapted from platisd) # --------------------------------------------------------------------------- @@ -304,7 +319,7 @@ def generate_review_comments(clang_tidy_fixes, repository_root, diag_name = diag.get('DiagnosticName', '') diag_message_msg = diag_message.get('Message', '') - if is_dropped_diagnostic(diag_name, diag_message_msg): + if is_dropped_diagnostic(diag_name): dropped += 1 continue level = diag.get('Level', 'Warning') @@ -451,9 +466,8 @@ def generate_review_comments(clang_tidy_fixes, repository_root, 'changed in this PR') if dropped: - print('Dropped {} diagnostic(s) matching the ignored-patterns list ' - '(e.g. clang-diagnostic-error "file not found"); ' - 'build_all_targets covers real missing includes.'.format(dropped)) + print('Dropped {} clang-diagnostic-error diagnostic(s); the build ' + 'jobs report real compile errors.'.format(dropped)) # --------------------------------------------------------------------------- @@ -541,6 +555,12 @@ def main(argv=None): print('Generated {} review comment(s)'.format(len(comments))) + if comments: + posted = fetch_posted_comment_keys(client, args.repo, args.pr_number) + comments = [c for c in comments + if (c['path'], c['line'], c['body']) not in posted] + print('{} not already posted on the PR'.format(len(comments))) + manifest = { 'pr_number': args.pr_number, 'marker': args.marker,