-
Notifications
You must be signed in to change notification settings - Fork 511
[CI] upgrade to clang-tidy 20 #3762
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
dbarker
wants to merge
3
commits into
open-telemetry:main
Choose a base branch
from
dbarker:upgrade_to_clang_tidy_20
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| # Copyright The OpenTelemetry Authors | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import argparse | ||
| import re | ||
| import sys | ||
| from collections import defaultdict | ||
| from enum import Enum | ||
| from pathlib import Path | ||
| from typing import Dict, List, NamedTuple, Optional, Set | ||
|
|
||
| # --- Configuration --- | ||
| REPO_NAME = "opentelemetry-cpp" | ||
| MAX_ROWS = 1000 | ||
| WARNING_RE = re.compile( | ||
| r"^(?P<file>.+):(?P<line>\d+):(?P<col>\d+): warning: (?P<msg>.+) " | ||
| r"\[(?P<check>.+)\]$" | ||
| ) | ||
| ANSI_RE = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])") | ||
|
|
||
|
|
||
| class OutputKeys(str, Enum): | ||
| TOTAL_WARNINGS = "TOTAL_WARNINGS" | ||
| REPORT_PATH = "REPORT_PATH" | ||
|
|
||
|
|
||
| class ClangTidyWarning(NamedTuple): | ||
| file: str | ||
| line: int | ||
| col: int | ||
| msg: str | ||
| check: str | ||
|
|
||
|
|
||
| def clean_path(path: str) -> str: | ||
| """Strip path prefix to make it relative to the repo or CWD.""" | ||
| if f"{REPO_NAME}/" in path: | ||
| return path.split(f"{REPO_NAME}/", 1)[1] | ||
| try: | ||
| return str(Path(path).relative_to(Path.cwd())) | ||
| except ValueError: | ||
| return path | ||
|
|
||
|
|
||
| def parse_log(log_path: Path) -> Set[ClangTidyWarning]: | ||
| if not log_path.exists(): | ||
| sys.exit(f"[ERROR] Log not found: {log_path}") | ||
| unique = set() | ||
| with log_path.open("r", encoding="utf-8", errors="replace") as f: | ||
| for line in f: | ||
| line = ANSI_RE.sub("", line.strip()) | ||
| if "warning:" not in line: | ||
| continue | ||
| match = WARNING_RE.match(line) | ||
| if match: | ||
| unique.add( | ||
| ClangTidyWarning( | ||
| clean_path(match.group("file")), | ||
| int(match.group("line")), | ||
| int(match.group("col")), | ||
| match.group("msg"), | ||
| match.group("check"), | ||
| ) | ||
| ) | ||
| return unique | ||
|
|
||
|
|
||
| def generate_report( | ||
| warnings: Set[ClangTidyWarning], | ||
| output_path: Path, | ||
| job_name: Optional[str] = None, | ||
| ): | ||
| by_check: Dict[str, List[ClangTidyWarning]] = defaultdict(list) | ||
| by_file: Dict[str, List[ClangTidyWarning]] = defaultdict(list) | ||
|
|
||
| for w in warnings: | ||
| by_check[w.check].append(w) | ||
| by_file[w.file].append(w) | ||
|
|
||
| with output_path.open("w", encoding="utf-8") as md: | ||
| title = "# " | ||
| if job_name: | ||
| title += f"{job_name}" | ||
|
|
||
| md.write( | ||
| f"{title} `clang-tidy` job \t[**{len(warnings)} warnings**]\n\n" | ||
| ) | ||
| md.write(f"<details><summary><b>{'Warnings breakdown'}</b><i> - Click to expand</i></summary>\n\n") | ||
|
|
||
| def write_section( | ||
| title, data, item_sort_key, header, row_fmt, group_key, reverse | ||
| ): | ||
| md.write(f"## {title}\n") | ||
| sorted_groups = sorted( | ||
| data.items(), key=group_key, reverse=reverse | ||
| ) | ||
| for key, items in sorted_groups: | ||
| md.write( | ||
| f"<details><summary><b>{key} ({len(items)})</b></summary>" | ||
| f"\n\n{header}\n" | ||
| ) | ||
| for i, w in enumerate(sorted(items, key=item_sort_key)): | ||
| if i >= MAX_ROWS: | ||
| remaining = len(items) - i | ||
| md.write( | ||
| f"| ... | ... | *{remaining} more omitted...* |\n" | ||
| ) | ||
| break | ||
| md.write(row_fmt(w) + "\n") | ||
| md.write("\n</details>\n\n") | ||
|
|
||
| # Warnings by File: Sorted Alphabetically | ||
| write_section( | ||
| "Warnings by File", | ||
| by_file, | ||
| item_sort_key=lambda w: w.line, | ||
| header="| Line | Check | Message |\n|---|---|---|", | ||
| row_fmt=lambda w: f"| {w.line} | `{w.check}` | {w.msg} |", | ||
| group_key=lambda x: x[0], | ||
| reverse=False, | ||
| ) | ||
|
|
||
| # Warnings by clang-tidy check: Sort by Warning count | ||
| write_section( | ||
| "Warnings by `clang-tidy` Check", | ||
| by_check, | ||
| item_sort_key=lambda w: (w.file, w.line), | ||
| header="| File | Line | Message |\n|---|---|---|", | ||
| row_fmt=lambda w: f"| `{w.file}` | {w.line} | {w.msg} |", | ||
| group_key=lambda x: len(x[1]), | ||
| reverse=True, | ||
| ) | ||
|
|
||
| md.write(f"</details>\n") | ||
| md.write("\n----\n") | ||
|
|
||
|
|
||
| def main(): | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument( | ||
| "-l", | ||
| "--build_log", | ||
| type=Path, | ||
| required=True, | ||
| help="Clang-tidy log file", | ||
| ) | ||
| parser.add_argument( | ||
| "-o", | ||
| "--output", | ||
| type=Path, | ||
| default="clang_tidy_report.md", | ||
| help="Output report path", | ||
| ) | ||
| parser.add_argument( | ||
| "-j", | ||
| "--job_name", | ||
| type=str, | ||
| help="Job name to include in the report title", | ||
| ) | ||
| args = parser.parse_args() | ||
|
|
||
| warnings = parse_log(args.build_log) | ||
| generate_report(warnings, args.output, args.job_name) | ||
|
|
||
| sys.stdout.write(f"{OutputKeys.TOTAL_WARNINGS.value}={len(warnings)}\n") | ||
| if args.output.exists(): | ||
| sys.stdout.write(f"{OutputKeys.REPORT_PATH.value}={args.output.resolve()}\n") | ||
| else: | ||
| sys.exit(f"[ERROR] Failed to write report: {args.output.resolve()}") | ||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -341,17 +341,24 @@ elif [[ "$1" == "cmake.legacy.test" ]]; then | |
| elif [[ "$1" == "cmake.clang_tidy.test" ]]; then | ||
| cd "${BUILD_DIR}" | ||
| rm -rf * | ||
| export BUILD_ROOT="${BUILD_DIR}" | ||
| clang-tidy --version | ||
| LOG_FILE="${BUILD_DIR}/opentelemetry-cpp-clang-tidy.log" | ||
| cmake -S ${SRC_DIR} \ | ||
| -B ${BUILD_DIR} \ | ||
| -C ${SRC_DIR}/test_common/cmake/all-options-abiv2-preview.cmake \ | ||
| "${CMAKE_OPTIONS[@]}" \ | ||
| -DWITH_OPENTRACING=OFF \ | ||
| -DCMAKE_CXX_FLAGS="-Wno-deprecated-declarations" \ | ||
| -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \ | ||
| -DCMAKE_CXX_CLANG_TIDY="clang-tidy;--quiet;-p;${BUILD_DIR}" | ||
| make -j $(nproc) | ||
| -DCMAKE_CXX_CLANG_TIDY="clang-tidy;--header-filter=.*/opentelemetry-cpp/.*;--exclude-header-filter=.*(internal/absl|third_party|third-party)/.*;--quiet" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way to put this logic (header-filter) in the top level file .clang-tidy instead ? It is desirable to have all the clang-tidy configuration in the same place, independent of the workflow & makefiles. |
||
| make -j $(nproc) 2>&1 | tee "$LOG_FILE" | ||
| make test | ||
| SCRIPT_OUTPUT=$(python3 ${SRC_DIR}/ci/create_clang_tidy_report.py \ | ||
| --build_log "$LOG_FILE" \ | ||
| --output clang_tidy_report.md) | ||
| export $SCRIPT_OUTPUT | ||
| echo "total warnings = $TOTAL_WARNINGS" | ||
| echo "clang-tidy report generated at $REPORT_PATH" | ||
| exit 0 | ||
| elif [[ "$1" == "cmake.legacy.exporter.otprotocol.test" ]]; then | ||
| cd "${BUILD_DIR}" | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way to put this logic (header-filter) in the top level file .clang-tidy instead ?