mirror of
https://github.com/reactos/syzkaller.git
synced 2025-02-02 10:02:41 +00:00
csource: format source with clang-format
This commit is contained in:
parent
81dc7f8dc8
commit
7a94216729
@ -207,3 +207,34 @@ func Build(src string) (string, error) {
|
||||
}
|
||||
return bin.Name(), nil
|
||||
}
|
||||
|
||||
// Format reformats C source using clang-format.
|
||||
func Format(src []byte) ([]byte, error) {
|
||||
stdout, stderr := new(bytes.Buffer), new(bytes.Buffer)
|
||||
cmd := exec.Command("clang-format", "-assume-filename=/src.c", "-style", style)
|
||||
cmd.Stdin = bytes.NewReader(src)
|
||||
cmd.Stdout = stdout
|
||||
cmd.Stderr = stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
return nil, fmt.Errorf("failed to format source: %v\n%v", err, stderr.String())
|
||||
}
|
||||
return stdout.Bytes(), nil
|
||||
}
|
||||
|
||||
// Something acceptable for kernel developers and email-friendly.
|
||||
var style = `{
|
||||
BasedOnStyle: LLVM,
|
||||
IndentWidth: 2,
|
||||
UseTab: Never,
|
||||
BreakBeforeBraces: Linux,
|
||||
IndentCaseLabels: false,
|
||||
DerivePointerAlignment: false,
|
||||
PointerAlignment: Left,
|
||||
AlignTrailingComments: true,
|
||||
AllowShortBlocksOnASingleLine: false,
|
||||
AllowShortCaseLabelsOnASingleLine: false,
|
||||
AllowShortFunctionsOnASingleLine: false,
|
||||
AllowShortIfStatementsOnASingleLine: false,
|
||||
AllowShortLoopsOnASingleLine: false,
|
||||
ColumnLimit: 72,
|
||||
}`
|
||||
|
@ -39,5 +39,10 @@ func main() {
|
||||
Collide: *flagCollide,
|
||||
}
|
||||
src := csource.Write(p, opts)
|
||||
if formatted, err := csource.Format(src); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%v\n", err)
|
||||
} else {
|
||||
src = formatted
|
||||
}
|
||||
os.Stdout.Write(src)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user