Skip to content

Commit

Permalink
switch astyle to clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
rongxin-liu committed Sep 12, 2023
1 parent c1d5e3d commit 2165216
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
26 changes: 10 additions & 16 deletions style50/languages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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):
Expand Down Expand Up @@ -98,7 +93,7 @@ class Js(C):
((?<![\*\/])\/(?![\/\*]).*?(?<![\\])\/) # JS regexes, trying hard not to be tripped up by comments
""", re.VERBOSE)

# C.__init__ checks for astyle but we don't need this for Js
# C.__init__ checks for clang-format but we don't need this for Js
__init__ = StyleCheck.__init__

# TODO: Determine which options, if any should be passed here
Expand All @@ -115,4 +110,3 @@ def style(self, code):
class Java(C):
extensions = ["java"]
magic_names = ["Java source"]
astyle = C.astyle + ["--mode=java", "--style=java"]

0 comments on commit 2165216

Please sign in to comment.