Skip to content

Commit

Permalink
Merge pull request #110 from cs50/clang-format
Browse files Browse the repository at this point in the history
Replace astyle with clang-format
  • Loading branch information
rongxin-liu authored Sep 12, 2023
2 parents c1d5e3d + dde890b commit 8c2682f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 23 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: 5 additions & 21 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 = '{ 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}"
]

# Match (1) /**/ comments, and (2) // comments.
Expand All @@ -30,16 +25,6 @@ class C(StyleCheck):
match_literals = re.compile(r'"(?:\\.|[^"\\])*"', re.DOTALL)

def __init__(self, code):
version_text = self.run(["astyle", "--version"])
try:
# Match astyle version via regex.
version = re.match("Artistic Style Version (\d.+)", version_text).groups()[0]
except IndexError:
raise Error("could not determine astyle version")

if tuple(map(int, version.split("."))) < (3, 0, 1):
raise Error("style50 requires astyle version 3.0.1 or greater, "
"but version {} was found".format(version))

# Call parent init.
StyleCheck.__init__(self, code)
Expand All @@ -50,7 +35,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 +83,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 +100,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 8c2682f

Please sign in to comment.