From 21652163d44768c6ff29ed820c2ca1a78b7156d8 Mon Sep 17 00:00:00 2001 From: Rongxin Liu Date: Tue, 12 Sep 2023 18:40:05 -0400 Subject: [PATCH 1/3] switch astyle to clang-format --- README.md | 2 +- setup.py | 2 +- style50/languages.py | 26 ++++++++++---------------- 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 08b3175..d584b6f 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ This is style50, a tool with which code can be checked against the CS50 style gu pip install style50 ``` -In order to style check C, C++, or Java code, a recent version (`>=3.0.1`) of `astyle` must be installed. `astyle` may be downloaded [here](https://sourceforge.net/projects/astyle/files/astyle/astyle%203.0.1/). +In order to style check C, C++, or Java code, a recent version (`>=14.0.0`) of `clang-format` must be installed. `clang-format` may be downloaded [here](https://clang.llvm.org/docs/ClangFormat.html). ### Windows diff --git a/setup.py b/setup.py index 63e94b6..28330a5 100644 --- a/setup.py +++ b/setup.py @@ -26,6 +26,6 @@ "console_scripts": ["style50=style50.__main__:main"], }, url="https://github.com/cs50/style50", - version="2.7.7", + version="2.8.0", include_package_data=True, ) diff --git a/style50/languages.py b/style50/languages.py index 178bed9..7507459 100644 --- a/style50/languages.py +++ b/style50/languages.py @@ -13,14 +13,9 @@ class C(StyleCheck): extensions = ["c", "h", "cpp", "hpp"] magic_names = [] # Only recognize C files by their extension - astyle = [ - "astyle", "--ascii", "--add-braces", "--break-one-line-headers", - "--align-pointer=name", "--pad-comma", "--unpad-paren", - "--pad-header", "--pad-oper", "--max-code-length=132", - "--convert-tabs", "--indent=spaces=4", - "--indent-continuation=1", "--indent-switches", - "--lineend=linux", "--min-conditional-indent=1", - "--options=none", "--style=allman" + styleConfig = '{"UseTab":true,"IndentWidth":4,"BreakBeforeBraces":"Allman","AllowShortIfStatementsOnASingleLine":false,"IndentCaseLabels":false,"ColumnLimit":0}' + clangFormat = [ + "clang-format", f"-style={styleConfig}" ] # Match (1) /**/ comments, and (2) // comments. @@ -30,15 +25,15 @@ class C(StyleCheck): match_literals = re.compile(r'"(?:\\.|[^"\\])*"', re.DOTALL) def __init__(self, code): - version_text = self.run(["astyle", "--version"]) + version_text = self.run(["clang-format", "--version"]) try: - # Match astyle version via regex. - version = re.match("Artistic Style Version (\d.+)", version_text).groups()[0] + # Match clang-format version via regex. + version = re.match("clang-format version (\d.+)", version_text).groups()[0] except IndexError: - raise Error("could not determine astyle version") + raise Error("could not determine clang-format version") if tuple(map(int, version.split("."))) < (3, 0, 1): - raise Error("style50 requires astyle version 3.0.1 or greater, " + raise Error("style50 requires clang-format version 16.0.0 or greater, " "but version {} was found".format(version)) # Call parent init. @@ -50,7 +45,7 @@ def count_comments(self, code): return sum(1 for _ in self.match_comments.finditer(stripped)) def style(self, code): - return self.run(self.astyle, input=code) + return self.run(self.clangFormat, input=code) class Python(StyleCheck): @@ -98,7 +93,7 @@ class Js(C): ((? Date: Tue, 12 Sep 2023 18:53:23 -0400 Subject: [PATCH 2/3] dropped version check for clang-format --- style50/languages.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/style50/languages.py b/style50/languages.py index 7507459..06f4b2b 100644 --- a/style50/languages.py +++ b/style50/languages.py @@ -25,16 +25,6 @@ class C(StyleCheck): match_literals = re.compile(r'"(?:\\.|[^"\\])*"', re.DOTALL) def __init__(self, code): - version_text = self.run(["clang-format", "--version"]) - try: - # Match clang-format version via regex. - version = re.match("clang-format version (\d.+)", version_text).groups()[0] - except IndexError: - raise Error("could not determine clang-format version") - - if tuple(map(int, version.split("."))) < (3, 0, 1): - raise Error("style50 requires clang-format version 16.0.0 or greater, " - "but version {} was found".format(version)) # Call parent init. StyleCheck.__init__(self, code) From dde890b3bc12af9d75043d3a357b1d34f7affbb8 Mon Sep 17 00:00:00 2001 From: Rongxin Liu Date: Tue, 12 Sep 2023 19:16:04 -0400 Subject: [PATCH 3/3] updated clang-format style config --- style50/languages.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/style50/languages.py b/style50/languages.py index 06f4b2b..468fd17 100644 --- a/style50/languages.py +++ b/style50/languages.py @@ -13,7 +13,7 @@ class C(StyleCheck): extensions = ["c", "h", "cpp", "hpp"] magic_names = [] # Only recognize C files by their extension - styleConfig = '{"UseTab":true,"IndentWidth":4,"BreakBeforeBraces":"Allman","AllowShortIfStatementsOnASingleLine":false,"IndentCaseLabels":false,"ColumnLimit":0}' + styleConfig = '{ AllowShortFunctionsOnASingleLine: Empty, BraceWrapping: { AfterCaseLabel: true, AfterControlStatement: true, AfterFunction: true, AfterStruct: true, BeforeElse: true, BeforeWhile: true }, BreakBeforeBraces: Custom, ColumnLimit: 132, IndentCaseLabels: true, IndentWidth: 4, SpaceAfterCStyleCast: true, TabWidth: 4 }' clangFormat = [ "clang-format", f"-style={styleConfig}" ]