Skip to content

Commit

Permalink
Pin clang-format version to 17 and mitigate ValueErrors (#1181)
Browse files Browse the repository at this point in the history
* Pin clang-format version to 17 and mitigate ValueErrors, when testing cmds with non numeric suffices.

* Update extrafiles/tooling/CppClangFormat.py

Co-authored-by: Eugenio Angriman <angriman.eugenio@gmail.com>

---------

Co-authored-by: Eugenio Angriman <angriman.eugenio@gmail.com>
  • Loading branch information
fabratu and angriman committed Feb 18, 2024
1 parent ed6965f commit c351d55
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
12 changes: 7 additions & 5 deletions extrafiles/tooling/CppClangFormat.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,18 @@ def isSupported(cmd):
# Read major revision number from "clang-format version XX.XX.XX ... "
version = str(subprocess.check_output([cmd, "--version"],
universal_newlines=True, env=getEnv())).strip().split()[-1].split('.')[0]
return int(version) >= nkt.MIN_CLANG_FORMAT_VERSION

try:
found_version = int(version)
return found_version == nkt.CLANG_FORMAT_VERSION
except ValueError as val_error:
return False

def findClangFormat():
"""Tries to find clang-format-XXX variants within the path"""
if "NETWORKIT_OVERRIDE_CLANG_FORMAT" in os.environ:
return os.environ["NETWORKIT_OVERRIDE_CLANG_FORMAT"]
cmd = "clang-format"
allowed = [cmd + "-" + str(x) for x in range(nkt.MAX_CLANG_FORMAT_VERSION, nkt.MIN_CLANG_FORMAT_VERSION - 1, -1)] + [cmd]
allowed = [cmd] + [cmd + "-" + str(nkt.CLANG_FORMAT_VERSION)]
for candidate in allowed:
if isSupported(candidate):
if nkt.isVerbose():
Expand All @@ -51,12 +54,11 @@ def findClangFormat():

raise FileNotFoundError("clang-format binary not found. We searched for:\n " + "\n ".join(allowed))

def runClangFormat(inputFilename, outputFilename, clangFormatBinary = 'clang-format-16'):
def runClangFormat(inputFilename, outputFilename, clangFormatBinary = f"clang-format-{nkt.CLANG_FORMAT_VERSION}"):
"""Execute clang-format onto inputFilename and stores the result in outputFilename"""
with open(outputFilename, "w") as outfile:
subprocess.call([clangFormatBinary, '-style=file', inputFilename], stdout=outfile)


nkt.setup()

# we need to set pwd in order for clang-format to find the .clang-format file!
Expand Down
3 changes: 1 addition & 2 deletions extrafiles/tooling/nktooling/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
READONLY = True
VERBOSE = False
SHOW_DIFFS = True
MIN_CLANG_FORMAT_VERSION = 8
MAX_CLANG_FORMAT_VERSION = 16
CLANG_FORMAT_VERSION = 17

def setup(args = None):
"""
Expand Down

0 comments on commit c351d55

Please sign in to comment.