format like AFL style (dotfiles)

This commit is contained in:
Andrea Fioraldi
2019-08-30 12:03:11 +02:00
parent ca6ac09dcc
commit 2eeb07d164
2 changed files with 29 additions and 5 deletions

View File

@ -3,16 +3,16 @@ Language: Cpp
# BasedOnStyle: Google # BasedOnStyle: Google
AccessModifierOffset: -1 AccessModifierOffset: -1
AlignAfterOpenBracket: Align AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: true AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: true AlignConsecutiveDeclarations: true
AlignEscapedNewlines: Left AlignEscapedNewlines: Left
AlignOperands: true AlignOperands: true
AlignTrailingComments: true AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false AllowShortBlocksOnASingleLine: true
AllowShortCaseLabelsOnASingleLine: false AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: false AllowShortFunctionsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: false AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: false AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None AlwaysBreakAfterReturnType: None

View File

@ -3,6 +3,9 @@
import subprocess import subprocess
import sys import sys
import os import os
import re
# string_re = re.compile('(\\"(\\\\.|[^"\\\\])*\\")') # future use
with open(".clang-format") as f: with open(".clang-format") as f:
fmt = f.read() fmt = f.read()
@ -36,14 +39,17 @@ for line in fmt.split("\n"):
if line[0].strip() == "ColumnLimit": if line[0].strip() == "ColumnLimit":
COLUMN_LIMIT = int(line[1].strip()) COLUMN_LIMIT = int(line[1].strip())
def custom_format(filename): def custom_format(filename):
p = subprocess.Popen([CLANG_FORMAT_BIN, filename], stdout=subprocess.PIPE) p = subprocess.Popen([CLANG_FORMAT_BIN, filename], stdout=subprocess.PIPE)
src, _ = p.communicate() src, _ = p.communicate()
src = str(src, "utf-8") src = str(src, "utf-8")
macro_indent = 0 macro_indent = 0
in_define = False
last_line = None
out = "" out = ""
for line in src.split("\n"): for line in src.split("\n"):
if line.startswith("#"): if line.startswith("#"):
i = macro_indent i = macro_indent
@ -54,6 +60,8 @@ def custom_format(filename):
i -= 1 i -= 1
elif line.startswith("#if") and not (line.startswith("#ifndef") and (line.endswith("_H") or line.endswith("H_"))): elif line.startswith("#if") and not (line.startswith("#ifndef") and (line.endswith("_H") or line.endswith("H_"))):
macro_indent += 1 macro_indent += 1
elif line.startswith("#define"):
in_define = True
r = "#" + (i * " ") + line[1:] r = "#" + (i * " ") + line[1:]
if i != 0 and line.endswith("\\"): if i != 0 and line.endswith("\\"):
r = r[:-1] r = r[:-1]
@ -67,7 +75,23 @@ def custom_format(filename):
cmt_start = line.rfind("/*") cmt_start = line.rfind("/*")
line = line[:cmt_start] + " " * (COLUMN_LIMIT-2 - len(line)) + line[cmt_start:] 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" out += line + "\n"
last_line = line
return (out) return (out)