Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace pragmas in clang-format's stdout to handle cases when the user did not specify -i #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions p-clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,21 @@ function main()
fi
done

# Create and trap a temporary file
tmpfile=$(mktemp --tmpdir="${TMPDIR:-/tmp}" p-clang-format.XXXXXXXXXX)
trap 'rm -f -- "$tmpfile"' INT TERM HUP EXIT
# Append the temporary file to file_pathes so pragmas will be replaced in it
# (this handles the case when the user did not specify -i to edit files in-place)
file_pathes+=("$tmpfile")

# Replace "#pragma" -> "//#pragma" in the files
replace_all "$pragma_prefix" "pragma_key_words[@]" "$comment_pattern" "file_pathes[@]"
format "$@"
# Run clang-format and redirect its output to the $tmpfile
format "$@" > "$tmpfile"
# Replace "//#pragma" -> "#pragma" in the files
re_replace_all "$pragma_prefix" "pragma_key_words[@]" "$re_comment_pattern" "file_pathes[@]"
# Show the clang-format's stdout after replacement
cat "$tmpfile"
}

main "$@"