Skip to content
50 changes: 50 additions & 0 deletions .github/c-h-after-uncrustify.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/bash
# -----------------------------------------------------------------------------
# Post-process C/H files after uncrustify formatting
# -----------------------------------------------------------------------------
# Fixes: Function return type pointer style
# FROM: struct foo *
# function_name(...)
# TO: struct foo*
# function_name(...)
#
# Usage:
# .github/c-after-uncrustify.sh <file.c> [file2.h ...]
# .github/c-after-uncrustify.sh core/zero/*.h
# -----------------------------------------------------------------------------

if [ $# -eq 0 ]; then
echo "Usage: $0 <file.c> [file2.h ...]"
echo "Post-processes C/H files after uncrustify formatting."
exit 1
fi

for file in "$@"; do
if [ ! -f "$file" ]; then
echo "Warning: File not found: $file"
continue
fi

echo "Processing: $file"

# Fix function return type pointer style:
# When a line ends with " *" and the next line is a function name,
# change " *" to "*" (remove the space before the asterisk)
#
# Pattern: "word *\n" followed by a line starting with a function name
# This handles:
# struct gkyl_dg_array_mask *
# gkyl_dg_array_mask_acquire(...)
# Becomes:
# struct gkyl_dg_array_mask*
# gkyl_dg_array_mask_acquire(...)

perl -i -0pe '
# Match: (type) space asterisk newline (function_name)
# Replace with: (type) asterisk newline (function_name)
s/(\w) \*\n(\w+\s*\()/$1*\n$2/g;
' "$file"

done

echo "Done."
71 changes: 71 additions & 0 deletions .github/cuda-after-uncrustify.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/bin/bash
# -----------------------------------------------------------------------------
# Post-process CUDA files after uncrustify formatting
# -----------------------------------------------------------------------------
# Fixes issues caused by uncrustify not understanding CUDA syntax:
# 1. Removes spaces in/around kernel launch operators: << < -> <<<, >> > -> >>>
# 2. Fixes indentation of arguments after kernel launch calls
#
# Usage:
# .github/cuda-after-uncrustify.sh <file.cu> [file2.cu ...]
# .github/cuda-after-uncrustify.sh core/zero/*.cu
# -----------------------------------------------------------------------------

if [ $# -eq 0 ]; then
echo "Usage: $0 <file.cu> [file2.cu ...]"
echo "Post-processes CUDA files after uncrustify formatting."
exit 1
fi

for file in "$@"; do
if [ ! -f "$file" ]; then
echo "Warning: File not found: $file"
continue
fi

echo "Processing: $file"

# Fix 1: Remove spaces in and around <<< and >>> operators
# Handles: << < -> <<<, >> > -> >>>, and removes surrounding spaces
sed -i 's/<< </<<</g; s/>> >/>>>/g' "$file"
sed -i 's/ <<<\s*/<<</' "$file"
sed -i 's/\s*>>> />>>/' "$file"

# Fix 2: Fix indentation of continuation lines after kernel launches
# Find lines with <<<...>>>( and fix indentation of following lines
# until we hit the closing );
perl -i -pe '
BEGIN { $in_kernel_call = 0; $base_indent = ""; }

# Detect kernel launch line ending with (
if (/^(\s*)(\S+)\s*<<<.*>>>\s*\(\s*$/) {
$in_kernel_call = 1;
$base_indent = $1 . " "; # 2 spaces from function start
next;
}

# If we are in a kernel call, fix indentation
if ($in_kernel_call) {
# Check if this line ends the call
if (/\);\s*$/) {
s/^(\s*)/$base_indent/;
$in_kernel_call = 0;
} else {
# Fix indentation of continuation lines
s/^(\s*)/$base_indent/;
}
}
' "$file"

# Fix 3: Function return type pointer style:
# When a line ends with " *" and the next line is a function name,
# change " *" to "*" (remove the space before the asterisk)
perl -i -0pe '
# Match: (type) space asterisk newline (function_name)
# Replace with: (type) asterisk newline (function_name)
s/(\w) \*\n(\w+\s*\()/$1*\n$2/g;
' "$file"

done

echo "Done."
90 changes: 90 additions & 0 deletions .github/uncrustify-wrapper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/bin/bash
# -----------------------------------------------------------------------------
# Uncrustify wrapper for VS Code
# -----------------------------------------------------------------------------
# This script wraps uncrustify to add post-processing for CUDA files.
# Set this as the "Uncrustify › Executable Path" in VS Code settings.
#
# It passes all arguments to uncrustify, then runs cuda-after-uncrustify.sh
# if the file being formatted is a .cu file.
#
# Works with:
# - Format Document
# - Format Selection (uncrustify handles this via --frag flag)
#
# How to set up in VS Code:
#
# Navigate to .vscode settings in this repository and open .vscode/settings.json.
# Then, set the "uncrustify.executablePath" setting to point to this script
# using an ABSOLUTE path, e.g.
#
# "uncrustify.configPath.linux": "/path/to/gkeyll/.github/uncrustify.cfg",
# "uncrustify.executablePath.linux": "/path/to/gkeyll/.github/uncrustify-wrapper.sh",
#
# To enable formatting of .h and .cu files, add these lines to .vscode/settings.json:
# "files.associations": {
# "*.h": "cpp",
# "*.cu": "cpp",
# -----------------------------------------------------------------------------

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CUDA_POST_SCRIPT="$SCRIPT_DIR/cuda-after-uncrustify.sh"
C_H_POST_SCRIPT="$SCRIPT_DIR/c-h-after-uncrustify.sh"

# Find the actual uncrustify binary; fall back to known locations if PATH is stale
# (VS Code may have a cached PATH from before uncrustify was installed).
UNCRUSTIFY_BIN=$(which uncrustify 2>/dev/null)
if [ -z "$UNCRUSTIFY_BIN" ]; then
for candidate in /usr/bin/uncrustify /usr/local/bin/uncrustify; do
if [ -x "$candidate" ]; then
UNCRUSTIFY_BIN="$candidate"
break
fi
done
fi
if [ -z "$UNCRUSTIFY_BIN" ]; then
echo "Error: uncrustify not found in PATH or known locations" >&2
exit 1
fi

# Run uncrustify with all passed arguments
"$UNCRUSTIFY_BIN" "$@"
UNCRUSTIFY_EXIT=$?

# If uncrustify failed, exit with its error code
if [ $UNCRUSTIFY_EXIT -ne 0 ]; then
exit $UNCRUSTIFY_EXIT
fi

# Check if any argument is a .cu, .c, or .h file and if --replace or -o was used
# (meaning the file was modified in place or output was written)
CUDA_FILE=""
C_H_FILE=""
HAS_REPLACE=0

for arg in "$@"; do
case "$arg" in
--replace|--no-backup)
HAS_REPLACE=1
;;
*.cu)
CUDA_FILE="$arg"
;;
*.c|*.h)
C_H_FILE="$arg"
;;
esac
done

# Postprocessing scripts
# .c and .h files
if [ -n "$C_H_FILE" ] && [ $HAS_REPLACE -eq 1 ] && [ -x "$C_H_POST_SCRIPT" ]; then
"$C_H_POST_SCRIPT" "$C_H_FILE" >/dev/null 2>&1
fi

# CUDA files
if [ -n "$CUDA_FILE" ] && [ $HAS_REPLACE -eq 1 ] && [ -x "$CUDA_POST_SCRIPT" ]; then
"$CUDA_POST_SCRIPT" "$CUDA_FILE" >/dev/null 2>&1
fi

exit 0
Loading
Loading