[clang] [llvm] Jay1 (PR #138469)
via cfe-commits
cfe-commits at lists.llvm.org
Sun May 4 13:18:33 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: None (PatJay1703)
<details>
<summary>Changes</summary>
---
Patch is 45.90 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/138469.diff
10 Files Affected:
- (modified) .clang-format (+245-1)
- (added) clang-format-diff.py (+183)
- (added) clang-tidy-diff.py (+424)
- (modified) clang/lib/AST/ASTStructuralEquivalence.cpp (+14-15)
- (added) config.yaml (+14)
- (added) remote_clang_format.py (+52)
- (added) remote_clang_tidy.py (+52)
- (added) remote_class_check.py (+73)
- (added) remote_header_check.py (+69)
- (added) remote_naming_conventions.py (+145)
``````````diff
diff --git a/.clang-format b/.clang-format
index 9b3aa8b7213b2..b52a604773bc2 100644
--- a/.clang-format
+++ b/.clang-format
@@ -1 +1,245 @@
-BasedOnStyle: LLVM
+---
+Language: Cpp
+# BasedOnStyle: LLVM
+AccessModifierOffset: -2
+AlignAfterOpenBracket: Align
+AlignArrayOfStructures: None
+AlignConsecutiveAssignments:
+ Enabled: false
+ AcrossEmptyLines: false
+ AcrossComments: false
+ AlignCompound: false
+ AlignFunctionPointers: false
+ PadOperators: true
+AlignConsecutiveBitFields:
+ Enabled: false
+ AcrossEmptyLines: false
+ AcrossComments: false
+ AlignCompound: false
+ AlignFunctionPointers: false
+ PadOperators: false
+AlignConsecutiveDeclarations:
+ Enabled: false
+ AcrossEmptyLines: false
+ AcrossComments: false
+ AlignCompound: false
+ AlignFunctionPointers: false
+ PadOperators: false
+AlignConsecutiveMacros:
+ Enabled: false
+ AcrossEmptyLines: false
+ AcrossComments: false
+ AlignCompound: false
+ AlignFunctionPointers: false
+ PadOperators: false
+AlignConsecutiveShortCaseStatements:
+ Enabled: false
+ AcrossEmptyLines: false
+ AcrossComments: false
+ AlignCaseColons: false
+AlignEscapedNewlines: Right
+AlignOperands: Align
+AlignTrailingComments:
+ Kind: Always
+ OverEmptyLines: 0
+AllowAllArgumentsOnNextLine: true
+AllowAllParametersOfDeclarationOnNextLine: true
+AllowBreakBeforeNoexceptSpecifier: Never
+AllowShortBlocksOnASingleLine: Never
+AllowShortCaseLabelsOnASingleLine: false
+AllowShortCompoundRequirementOnASingleLine: true
+AllowShortEnumsOnASingleLine: true
+AllowShortFunctionsOnASingleLine: All
+AllowShortLambdasOnASingleLine: All
+AllowShortLoopsOnASingleLine: false
+AlwaysBreakAfterDefinitionReturnType: None
+AlwaysBreakAfterReturnType: None
+AlwaysBreakBeforeMultilineStrings: false
+AlwaysBreakTemplateDeclarations: MultiLine
+AttributeMacros:
+ - __capability
+BinPackArguments: true
+BinPackParameters: true
+BitFieldColonSpacing: Both
+BraceWrapping:
+ AfterCaseLabel: false
+ AfterClass: false
+ AfterControlStatement: Never
+ AfterEnum: false
+ AfterExternBlock: false
+ AfterFunction: false
+ AfterNamespace: false
+ AfterObjCDeclaration: false
+ AfterStruct: false
+ AfterUnion: false
+ BeforeCatch: false
+ BeforeElse: false
+ BeforeLambdaBody: false
+ BeforeWhile: false
+ IndentBraces: false
+ SplitEmptyFunction: true
+ SplitEmptyRecord: true
+ SplitEmptyNamespace: true
+BreakAdjacentStringLiterals: true
+BreakAfterAttributes: Leave
+BreakAfterJavaFieldAnnotations: false
+BreakArrays: true
+BreakBeforeBinaryOperators: None
+BreakBeforeConceptDeclarations: Always
+BreakBeforeBraces: Attach
+BreakBeforeInlineASMColon: OnlyMultiline
+BreakBeforeTernaryOperators: true
+BreakConstructorInitializers: BeforeColon
+BreakInheritanceList: BeforeColon
+BreakStringLiterals: true
+ColumnLimit: 80
+CommentPragmas: '^ IWYU pragma:'
+CompactNamespaces: false
+ConstructorInitializerIndentWidth: 4
+ContinuationIndentWidth: 4
+Cpp11BracedListStyle: true
+DerivePointerAlignment: false
+DisableFormat: false
+EmptyLineAfterAccessModifier: Never
+EmptyLineBeforeAccessModifier: LogicalBlock
+ExperimentalAutoDetectBinPacking: false
+FixNamespaceComments: true
+ForEachMacros:
+ - foreach
+ - Q_FOREACH
+ - BOOST_FOREACH
+IfMacros:
+ - KJ_IF_MAYBE
+IncludeBlocks: Preserve
+IncludeCategories:
+ - Regex: '^"(llvm|llvm-c|clang|clang-c)/'
+ Priority: 2
+ SortPriority: 0
+ CaseSensitive: false
+ - Regex: '^(<|"(gtest|gmock|isl|json)/)'
+ Priority: 3
+ SortPriority: 0
+ CaseSensitive: false
+ - Regex: '.*'
+ Priority: 1
+ SortPriority: 0
+ CaseSensitive: false
+IncludeIsMainRegex: '(Test)?$'
+IncludeIsMainSourceRegex: ''
+IndentAccessModifiers: false
+IndentCaseBlocks: false
+IndentCaseLabels: false
+IndentExternBlock: AfterExternBlock
+IndentGotoLabels: true
+IndentPPDirectives: None
+IndentRequiresClause: true
+IndentWidth: 2
+IndentWrappedFunctionNames: false
+InsertBraces: false
+InsertNewlineAtEOF: false
+InsertTrailingCommas: None
+IntegerLiteralSeparator:
+ Binary: 0
+ BinaryMinDigits: 0
+ Decimal: 0
+ DecimalMinDigits: 0
+ Hex: 0
+ HexMinDigits: 0
+JavaScriptQuotes: Leave
+JavaScriptWrapImports: true
+KeepEmptyLinesAtTheStartOfBlocks: true
+KeepEmptyLinesAtEOF: false
+LambdaBodyIndentation: Signature
+LineEnding: DeriveLF
+MacroBlockBegin: ''
+MacroBlockEnd: ''
+MaxEmptyLinesToKeep: 1
+NamespaceIndentation: None
+ObjCBinPackProtocolList: Auto
+ObjCBlockIndentWidth: 2
+ObjCBreakBeforeNestedBlockParam: true
+ObjCSpaceAfterProperty: false
+ObjCSpaceBeforeProtocolList: true
+PackConstructorInitializers: BinPack
+PenaltyBreakAssignment: 2
+PenaltyBreakBeforeFirstCallParameter: 19
+PenaltyBreakComment: 300
+PenaltyBreakFirstLessLess: 120
+PenaltyBreakOpenParenthesis: 0
+PenaltyBreakScopeResolution: 500
+PenaltyBreakString: 1000
+PenaltyBreakTemplateDeclaration: 10
+PenaltyExcessCharacter: 1000000
+PenaltyIndentedWhitespace: 0
+PenaltyReturnTypeOnItsOwnLine: 60
+PointerAlignment: Right
+PPIndentWidth: -1
+QualifierAlignment: Leave
+ReferenceAlignment: Pointer
+ReflowComments: true
+RemoveBracesLLVM: false
+RemoveParentheses: Leave
+RemoveSemicolon: false
+RequiresClausePosition: OwnLine
+RequiresExpressionIndentation: OuterScope
+SeparateDefinitionBlocks: Leave
+ShortNamespaceLines: 1
+SkipMacroDefinitionBody: false
+SortIncludes: CaseSensitive
+SortJavaStaticImport: Before
+SortUsingDeclarations: LexicographicNumeric
+SpaceAfterCStyleCast: false
+SpaceAfterLogicalNot: false
+SpaceAfterTemplateKeyword: true
+SpaceAroundPointerQualifiers: Default
+SpaceBeforeAssignmentOperators: true
+SpaceBeforeCaseColon: false
+SpaceBeforeCpp11BracedList: false
+SpaceBeforeCtorInitializerColon: true
+SpaceBeforeInheritanceColon: true
+SpaceBeforeJsonColon: false
+SpaceBeforeParens: ControlStatements
+SpaceBeforeParensOptions:
+ AfterControlStatements: true
+ AfterForeachMacros: true
+ AfterFunctionDefinitionName: false
+ AfterFunctionDeclarationName: false
+ AfterIfMacros: true
+ AfterOverloadedOperator: false
+ AfterPlacementOperator: true
+ AfterRequiresInClause: false
+ AfterRequiresInExpression: false
+ BeforeNonEmptyParentheses: false
+SpaceBeforeRangeBasedForLoopColon: true
+SpaceBeforeSquareBrackets: false
+SpaceInEmptyBlock: false
+SpacesBeforeTrailingComments: 1
+SpacesInAngles: Never
+SpacesInContainerLiterals: true
+SpacesInLineCommentPrefix:
+ Minimum: 1
+ Maximum: -1
+SpacesInParens: Never
+SpacesInParensOptions:
+ InCStyleCasts: false
+ InConditionalStatements: false
+ InEmptyParentheses: false
+ Other: false
+SpacesInSquareBrackets: false
+Standard: Latest
+StatementAttributeLikeMacros:
+ - Q_EMIT
+StatementMacros:
+ - Q_UNUSED
+ - QT_REQUIRE_VERSION
+TabWidth: 8
+UseTab: Never
+VerilogBreakBetweenInstancePorts: true
+WhitespaceSensitiveMacros:
+ - BOOST_PP_STRINGIZE
+ - CF_SWIFT_NAME
+ - NS_SWIFT_NAME
+ - PP_STRINGIZE
+ - STRINGIZE
+...
+
diff --git a/clang-format-diff.py b/clang-format-diff.py
new file mode 100644
index 0000000000000..c4aa7951e85de
--- /dev/null
+++ b/clang-format-diff.py
@@ -0,0 +1,183 @@
+
+#!/usr/bin/env python3
+#
+# ===- clang-format-diff.py - ClangFormat Diff Reformatter ----*- python -*--===#
+#
+# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+#
+# ===------------------------------------------------------------------------===#
+"""
+This script reads input from a unified diff and reformats all the changed
+lines. This is useful to reformat all the lines touched by a specific patch.
+Example usage for git/svn users:
+ git diff -U0 --no-color --relative HEAD^ | {clang_format_diff} -p1 -i
+ svn diff --diff-cmd=diff -x-U0 | {clang_format_diff} -i
+It should be noted that the filename contained in the diff is used unmodified
+to determine the source file to update. Users calling this script directly
+should be careful to ensure that the path in the diff is correct relative to the
+current working directory.
+"""
+from __future__ import absolute_import, division, print_function
+import argparse
+import difflib
+import re
+import subprocess
+import sys
+if sys.version_info.major >= 3:
+ from io import StringIO
+else:
+ from io import BytesIO as StringIO
+
+def main():
+ parser = argparse.ArgumentParser(
+ description=__doc__.format(clang_format_diff="%(prog)s"),
+ formatter_class=argparse.RawDescriptionHelpFormatter,
+ )
+ parser.add_argument(
+ "-i",
+ action="store_true",
+ default=False,
+ help="apply edits to files instead of displaying a diff",
+ )
+ parser.add_argument(
+ "-p",
+ metavar="NUM",
+ default=0,
+ help="strip the smallest prefix containing P slashes",
+ )
+ parser.add_argument(
+ "-regex",
+ metavar="PATTERN",
+ default=None,
+ help="custom pattern selecting file paths to reformat "
+ "(case sensitive, overrides -iregex)",
+ )
+ parser.add_argument(
+ "-iregex",
+ metavar="PATTERN",
+ default=r".*\.(?:cpp|cc|c\+\+|cxx|cppm|ccm|cxxm|c\+\+m|c|cl|h|hh|hpp"
+ r"|hxx|m|mm|inc|js|ts|proto|protodevel|java|cs|json|ipynb|s?vh?)",
+ help="custom pattern selecting file paths to reformat "
+ "(case insensitive, overridden by -regex)",
+ )
+ parser.add_argument(
+ "-sort-includes",
+ action="store_true",
+ default=False,
+ help="let clang-format sort include blocks",
+ )
+ parser.add_argument(
+ "-v",
+ "--verbose",
+ action="store_true",
+ help="be more verbose, ineffective without -i",
+ )
+ parser.add_argument(
+ "-style",
+ help="formatting style to apply (LLVM, GNU, Google, Chromium, "
+ "Microsoft, Mozilla, WebKit)",
+ )
+ parser.add_argument(
+ "-fallback-style",
+ help="The name of the predefined style used as a"
+ "fallback in case clang-format is invoked with"
+ "-style=file, but can not find the .clang-format"
+ "file to use.",
+ )
+ parser.add_argument(
+ "-binary",
+ default="clang-format",
+ help="location of binary to use for clang-format",
+ )
+ args = parser.parse_args()
+ # Extract changed lines for each file.
+ filename = None
+ lines_by_file = {}
+ for line in sys.stdin:
+ match = re.search(r"^\+\+\+\ (.*?/){%s}(\S*)" % args.p, line)
+ if match:
+ filename = match.group(2)
+ if filename is None:
+ continue
+ if args.regex is not None:
+ if not re.match("^%s$" % args.regex, filename):
+ continue
+ else:
+ if not re.match("^%s$" % args.iregex, filename, re.IGNORECASE):
+ continue
+ match = re.search(r"^@@.*\+(\d+)(?:,(\d+))?", line)
+ if match:
+ start_line = int(match.group(1))
+ line_count = 1
+ if match.group(2):
+ line_count = int(match.group(2))
+ # The input is something like
+ #
+ # @@ -1, +0,0 @@
+ #
+ # which means no lines were added.
+ if line_count == 0:
+ continue
+ # Also format lines range if line_count is 0 in case of deleting
+ # surrounding statements.
+ end_line = start_line
+ if line_count != 0:
+ end_line += line_count - 1
+ lines_by_file.setdefault(filename, []).extend(
+ ["--lines", str(start_line) + ":" + str(end_line)]
+ )
+ # Reformat files containing changes in place.
+ has_diff = False
+ for filename, lines in lines_by_file.items():
+ if args.i and args.verbose:
+ print("Formatting {}".format(filename))
+ command = [args.binary, filename]
+ if args.i:
+ command.append("-i")
+ if args.sort_includes:
+ command.append("--sort-includes")
+ command.extend(lines)
+ if args.style:
+ command.extend(["--style", args.style])
+ if args.fallback_style:
+ command.extend(["--fallback-style", args.fallback_style])
+ try:
+ p = subprocess.Popen(
+ command,
+ stdout=subprocess.PIPE,
+ stderr=None,
+ stdin=subprocess.PIPE,
+ universal_newlines=True,
+ )
+ except OSError as e:
+ # Give the user more context when clang-format isn't
+ # found/isn't executable, etc.
+ raise RuntimeError(
+ 'Failed to run "%s" - %s"' % (" ".join(command), e.strerror)
+ )
+ stdout, _stderr = p.communicate()
+ if p.returncode != 0:
+ return p.returncode
+ if not args.i:
+ with open(filename) as f:
+ code = f.readlines()
+ formatted_code = StringIO(stdout).readlines()
+ diff = difflib.unified_diff(
+ code,
+ formatted_code,
+ filename,
+ filename,
+ "(before formatting)",
+ "(after formatting)",
+ )
+ diff_string = "".join(diff)
+ if len(diff_string) > 0:
+ has_diff = True
+ sys.stdout.write(diff_string)
+ if has_diff:
+ return 1
+
+if __name__ == "__main__":
+ sys.exit(main())
diff --git a/clang-tidy-diff.py b/clang-tidy-diff.py
new file mode 100644
index 0000000000000..6f157c64613d8
--- /dev/null
+++ b/clang-tidy-diff.py
@@ -0,0 +1,424 @@
+
+#!/usr/bin/env python3
+#
+# ===- clang-tidy-diff.py - ClangTidy Diff Checker -----------*- python -*--===#
+#
+# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+#
+# ===-----------------------------------------------------------------------===#
+
+r"""
+ClangTidy Diff Checker
+======================
+
+This script reads input from a unified diff, runs clang-tidy on all changed
+files and outputs clang-tidy warnings in changed lines only. This is useful to
+detect clang-tidy regressions in the lines touched by a specific patch.
+Example usage for git/svn users:
+
+ git diff -U0 HEAD^ | clang-tidy-diff.py -p1
+ svn diff --diff-cmd=diff -x-U0 | \
+ clang-tidy-diff.py -fix -checks=-*,modernize-use-override
+
+"""
+
+import argparse
+import glob
+import json
+import multiprocessing
+import os
+import re
+import shutil
+import subprocess
+import sys
+import tempfile
+import threading
+import traceback
+from pathlib import Path
+
+try:
+ import yaml
+except ImportError:
+ yaml = None
+
+is_py2 = sys.version[0] == "2"
+
+if is_py2:
+ import Queue as queue
+else:
+ import queue as queue
+
+
+def run_tidy(task_queue, lock, timeout, failed_files):
+ watchdog = None
+ while True:
+ command = task_queue.get()
+ try:
+ proc = subprocess.Popen(
+ command, stdout=subprocess.PIPE, stderr=subprocess.PIPE
+ )
+
+ if timeout is not None:
+ watchdog = threading.Timer(timeout, proc.kill)
+ watchdog.start()
+
+ stdout, stderr = proc.communicate()
+ if proc.returncode != 0:
+ if proc.returncode < 0:
+ msg = "Terminated by signal %d : %s\n" % (
+ -proc.returncode,
+ " ".join(command),
+ )
+ stderr += msg.encode("utf-8")
+ failed_files.append(command)
+
+ with lock:
+ sys.stdout.write(stdout.decode("utf-8") + "\n")
+ sys.stdout.flush()
+ if stderr:
+ sys.stderr.write(stderr.decode("utf-8") + "\n")
+ sys.stderr.flush()
+ except Exception as e:
+ with lock:
+ sys.stderr.write("Failed: " + str(e) + ": ".join(command) + "\n")
+ finally:
+ with lock:
+ if not (timeout is None or watchdog is None):
+ if not watchdog.is_alive():
+ sys.stderr.write(
+ "Terminated by timeout: " + " ".join(command) + "\n"
+ )
+ watchdog.cancel()
+ task_queue.task_done()
+
+
+def start_workers(max_tasks, tidy_caller, arguments):
+ for _ in range(max_tasks):
+ t = threading.Thread(target=tidy_caller, args=arguments)
+ t.daemon = True
+ t.start()
+
+
+def merge_replacement_files(tmpdir, mergefile):
+ """Merge all replacement files in a directory into a single file"""
+ # The fixes suggested by clang-tidy >= 4.0.0 are given under
+ # the top level key 'Diagnostics' in the output yaml files
+ mergekey = "Diagnostics"
+ merged = []
+ for replacefile in glob.iglob(os.path.join(tmpdir, "*.yaml")):
+ content = yaml.safe_load(open(replacefile, "r"))
+ if not content:
+ continue # Skip empty files.
+ merged.extend(content.get(mergekey, []))
+
+ if merged:
+ # MainSourceFile: The key is required by the definition inside
+ # include/clang/Tooling/ReplacementsYaml.h, but the value
+ # is actually never used inside clang-apply-replacements,
+ # so we set it to '' here.
+ output = {"MainSourceFile": "", mergekey: merged}
+ with open(mergefile, "w") as out:
+ yaml.safe_dump(output, out)
+ else:
+ # Empty the file:
+ open(mergefile, "w").close()
+
+
+def get_compiling_files(args):
+ """Read a compile_commands.json database and return a set of file paths"""
+ current_dir = Path.cwd()
+ compile_commands_json = (
+ (current_dir / args.build_path) if args.build_path else current_dir
+ )
+ compile_commands_json = compile_commands_json / "compile_commands.json"
+ files = set()
+ with open(compile_commands_json) as db_file:
+ db_json = json.load(db_file)
+ for entry in db_json:
+ if "file" not in entry:
+ continue
+ files.add(Path(entry["file"]))
+ return files
+
+
+def main():
+ parser = argparse.ArgumentParser(
+ description="Run clang-tidy against changed files, and "
+ "output diagnostics only for modified "
+ "lines."
+ )
+ parser.add_argument(
+ "-clang-tidy-binary",
+ metavar="PATH",
+ default="clang-tidy",
+ help="path to clang-tidy binary",
+ )
+ parser.add_argument(
+ "-p",
+ metavar="NUM",
+ default=0,
+ help="strip the smallest prefix containing P slashes",
+ )
+ parser.add_argument(
+ "-regex",
+ metavar="PATTERN",
+ default=None,
+ help="custom pattern selecting file paths to check "
+ "(case sensitive, overrides -iregex)",
+ )
+ parser.add_argument(
+ "-iregex",
+ metavar="PATTERN",
+ default=r".*\.(cpp|cc|c\+\+|cxx|c|cl|h|hpp|m|mm|inc)",
+ help="custom pattern selecting file paths to check "
+ "(case insensitive, overridden by -regex)",
+ )
+ parser.add_argument(
+ "-j",
+ type=int,
+ default=1,
+ help="number of tidy instances to be run in parallel.",
+ )
+ parser.add_argument(
+ "-timeout", type=int, default=None, help="timeout per each file in seconds."
+ )
+ parser.add_argument(
+ "-fix", action="store_true", default=False, help="apply suggested fixes"
+ )
+ parser.add_argument(
+ "-checks",
+ help="checks filter, when not specified, use clang-tidy " "default",
+ default="",
+ )
+ parser.add_argument(
+ "-config-file",
+ dest="config_file",
+ help="Specify the path of .clang-tidy or custom config file",
+ default="",
+ )
+ parser.add_a...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/138469
More information about the cfe-commits
mailing list