From cd259fe1180e39fe311abe48f32675f9feb72cb2 Mon Sep 17 00:00:00 2001 From: Andrea Fioraldi Date: Tue, 27 Aug 2019 17:26:04 +0200 Subject: add custom format wrapping clang-format --- .custom-format.py | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100755 .custom-format.py (limited to '.custom-format.py') diff --git a/.custom-format.py b/.custom-format.py new file mode 100755 index 00000000..b4a2c48a --- /dev/null +++ b/.custom-format.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python3 + +import subprocess +import sys + +with open(".clang-format") as f: + fmt = f.read() + +COLUMN_LIMIT = 80 +for line in fmt.split("\n"): + line = line.split(":") + if line[0].strip() == "ColumnLimit": + COLUMN_LIMIT = int(line[1].strip()) + +def custom_format(filename): + p = subprocess.Popen(['clang-format-7', filename], stdout=subprocess.PIPE) + src, _ = p.communicate() + src = str(src, "utf-8") + + macro_indent = 0 + + out = "" + for line in src.split("\n"): + if line.startswith("#"): + i = macro_indent + if line.startswith("#end") and macro_indent > 0: + macro_indent -= 1 + i -= 1 + elif line.startswith("#el") and macro_indent > 0: + i -= 1 + elif line.startswith("#if") and not (line.startswith("#ifndef") and line.endswith("_H")): + macro_indent += 1 + r = "#" + (i * " ") + line[1:] + if i != 0 and line.endswith("\\"): + r = r[:-1] + while r[-1].isspace() and len(r) != (len(line)-1): + r = r[:-1] + r += "\\" + if len(r) <= COLUMN_LIMIT: + line = r + + elif "/*" in line and not line.strip().startswith("/*") and line.endswith("*/") and len(line) < (COLUMN_LIMIT-2): + cmt_start = line.rfind("/*") + line = line[:cmt_start] + " " * (COLUMN_LIMIT-2 - len(line)) + line[cmt_start:] + + out += line + "\n" + + return (out) + +args = sys.argv[1:] +if len(args) == 0: + print ("Usage: ./format.py [-i] ") + print () + print (" The -i option, if specified, let the script to modify in-place") + print (" the source files. By default the results are written to stdout.") + print() + exit(1) + +in_place = False +if args[0] == "-i": + in_place = True + args = args[1:] + +for filename in args: + code = custom_format(filename) + if in_place: + with open(filename, "w") as f: + f.write(code) + else: + print(code) + -- cgit 1.4.1 From aca63d4986540ca6c51cc90321f54509aee2ce45 Mon Sep 17 00:00:00 2001 From: Andrea Fioraldi Date: Tue, 27 Aug 2019 19:35:44 +0200 Subject: custom format now search for the best clang-format version --- .clang-format | 2 +- .custom-format.py | 28 ++++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) (limited to '.custom-format.py') diff --git a/.clang-format b/.clang-format index e506392d..f691d684 100644 --- a/.clang-format +++ b/.clang-format @@ -124,7 +124,7 @@ RawStringFormats: CanonicalDelimiter: '' BasedOnStyle: google ReflowComments: true -SortIncludes: true +SortIncludes: false SortUsingDeclarations: true SpaceAfterCStyleCast: false SpaceAfterTemplateKeyword: true diff --git a/.custom-format.py b/.custom-format.py index b4a2c48a..a73d92ab 100755 --- a/.custom-format.py +++ b/.custom-format.py @@ -2,10 +2,34 @@ import subprocess import sys +import os with open(".clang-format") as f: fmt = f.read() +CLANG_FORMAT_BIN = os.getenv("CLANG_FORMAT_BIN") +if CLANG_FORMAT_BIN is None: + p = subprocess.Popen(["clang-format", "--version"], stdout=subprocess.PIPE) + o, _ = p.communicate() + o = str(o, "utf-8") + o = o[len("clang-format version "):].strip() + o = o[:o.find(".")] + o = int(o) + if o < 7: + if subprocess.call(['which', 'clang-format-7'], stdout=subprocess.PIPE) == 0: + CLANG_FORMAT_BIN = 'clang-format-7' + elif subprocess.call(['which', 'clang-format-8'], stdout=subprocess.PIPE) == 0: + CLANG_FORMAT_BIN = 'clang-format-8' + elif subprocess.call(['which', 'clang-format-9'], stdout=subprocess.PIPE) == 0: + CLANG_FORMAT_BIN = 'clang-format-9' + elif subprocess.call(['which', 'clang-format-10'], stdout=subprocess.PIPE) == 0: + CLANG_FORMAT_BIN = 'clang-format-10' + else: + print ("clang-format 7 or above is needed. Aborted.") + exit(1) + else: + CLANG_FORMAT_BIN = 'clang-format' + COLUMN_LIMIT = 80 for line in fmt.split("\n"): line = line.split(":") @@ -13,7 +37,7 @@ for line in fmt.split("\n"): COLUMN_LIMIT = int(line[1].strip()) def custom_format(filename): - p = subprocess.Popen(['clang-format-7', filename], stdout=subprocess.PIPE) + p = subprocess.Popen([CLANG_FORMAT_BIN, filename], stdout=subprocess.PIPE) src, _ = p.communicate() src = str(src, "utf-8") @@ -28,7 +52,7 @@ def custom_format(filename): i -= 1 elif line.startswith("#el") and macro_indent > 0: i -= 1 - elif line.startswith("#if") and not (line.startswith("#ifndef") and line.endswith("_H")): + elif line.startswith("#if") and not (line.startswith("#ifndef") and (line.endswith("_H") or line.endswith("H_"))): macro_indent += 1 r = "#" + (i * " ") + line[1:] if i != 0 and line.endswith("\\"): -- cgit 1.4.1 From 2eeb07d164cb7874a64a48bd9c1bf4112636ac43 Mon Sep 17 00:00:00 2001 From: Andrea Fioraldi Date: Fri, 30 Aug 2019 12:03:11 +0200 Subject: format like AFL style (dotfiles) --- .clang-format | 8 ++++---- .custom-format.py | 26 +++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 5 deletions(-) (limited to '.custom-format.py') diff --git a/.clang-format b/.clang-format index f691d684..ef4cb190 100644 --- a/.clang-format +++ b/.clang-format @@ -3,16 +3,16 @@ Language: Cpp # BasedOnStyle: Google AccessModifierOffset: -1 AlignAfterOpenBracket: Align -AlignConsecutiveAssignments: true +AlignConsecutiveAssignments: false AlignConsecutiveDeclarations: true AlignEscapedNewlines: Left AlignOperands: true AlignTrailingComments: true AllowAllParametersOfDeclarationOnNextLine: true -AllowShortBlocksOnASingleLine: false -AllowShortCaseLabelsOnASingleLine: false +AllowShortBlocksOnASingleLine: true +AllowShortCaseLabelsOnASingleLine: true AllowShortFunctionsOnASingleLine: false -AllowShortIfStatementsOnASingleLine: false +AllowShortIfStatementsOnASingleLine: true AllowShortLoopsOnASingleLine: false AlwaysBreakAfterDefinitionReturnType: None AlwaysBreakAfterReturnType: None diff --git a/.custom-format.py b/.custom-format.py index a73d92ab..32b8f7c9 100755 --- a/.custom-format.py +++ b/.custom-format.py @@ -3,6 +3,9 @@ import subprocess import sys import os +import re + +# string_re = re.compile('(\\"(\\\\.|[^"\\\\])*\\")') # future use with open(".clang-format") as f: fmt = f.read() @@ -36,14 +39,17 @@ for line in fmt.split("\n"): if line[0].strip() == "ColumnLimit": COLUMN_LIMIT = int(line[1].strip()) + def custom_format(filename): p = subprocess.Popen([CLANG_FORMAT_BIN, filename], stdout=subprocess.PIPE) src, _ = p.communicate() src = str(src, "utf-8") macro_indent = 0 - + in_define = False + last_line = None out = "" + for line in src.split("\n"): if line.startswith("#"): i = macro_indent @@ -54,6 +60,8 @@ def custom_format(filename): i -= 1 elif line.startswith("#if") and not (line.startswith("#ifndef") and (line.endswith("_H") or line.endswith("H_"))): macro_indent += 1 + elif line.startswith("#define"): + in_define = True r = "#" + (i * " ") + line[1:] if i != 0 and line.endswith("\\"): r = r[:-1] @@ -67,7 +75,23 @@ def custom_format(filename): cmt_start = line.rfind("/*") line = line[:cmt_start] + " " * (COLUMN_LIMIT-2 - len(line)) + line[cmt_start:] + define_padding = 0 + if last_line is not None and in_define and last_line.endswith("\\"): + last_line = last_line[:-1] + define_padding = max(0, len(last_line[last_line.rfind("\n")+1:])) + + if last_line is not None and last_line.strip().endswith("{") and line.strip() != "": + line = (" " * define_padding + "\\" if in_define else "") + "\n" + line + elif last_line is not None and last_line.strip().startswith("}") and line.strip() != "": + line = (" " * define_padding + "\\" if in_define else "") + "\n" + line + elif line.strip().startswith("}") and last_line is not None and last_line.strip() != "": + line = (" " * define_padding + "\\" if in_define else "") + "\n" + line + + if not line.endswith("\\"): + in_define = False + out += line + "\n" + last_line = line return (out) -- cgit 1.4.1