Mercurial > public > mercurial-scm > hg-stable
diff contrib/chg/util.c @ 41361:763b45bc4483
cleanup: use clang-tidy to add missing {} around one-line statements
I find this easier to read. Cleanup performed like this:
hg files 'set:(**.c or **.cc or **.h) and not "listfile:contrib/clang-format-ignorelist"' | while read f ; do
clang-tidy -fix -checks=readability-braces-around-statements $f -- $(python-config --cflags) -Imercurial/cext -Imercurial
done
make format-c
I had to revert chg/chg.c as it's got a construct that seems to confuse
clang-tidy, so I'll work on that file later if this change is acceptable. I
only tackle files that are under clang-format's authority because otherwise
I'd have to do a bunch of manual formatting. A few files didn't get edited
because clang-tidy couldn't find some headers. Again, I'll figure that out
later assuming this change is accepted.
No check-code rule added for now because writing the regex sounds hard. In a
perfect world I guess we could write a test that uses clang-tidy on these
files, but I think clang-tidy is pretty rarely installed. :/
Differential Revision: https://phab.mercurial-scm.org/D5675
author | Augie Fackler <augie@google.com> |
---|---|
date | Thu, 24 Jan 2019 10:21:59 -0500 |
parents | 9724f54923ec |
children |
line wrap: on
line diff
--- a/contrib/chg/util.c Thu Jan 24 11:35:40 2019 -0500 +++ b/contrib/chg/util.c Thu Jan 24 10:21:59 2019 -0500 @@ -25,8 +25,9 @@ static inline void fsetcolor(FILE *fp, const char *code) { - if (!colorenabled) + if (!colorenabled) { return; + } fprintf(fp, "\033[%sm", code); } @@ -35,8 +36,9 @@ fsetcolor(stderr, "1;31"); fputs("chg: abort: ", stderr); vfprintf(stderr, fmt, args); - if (no != 0) + if (no != 0) { fprintf(stderr, " (errno = %d, %s)", no, strerror(no)); + } fsetcolor(stderr, ""); fputc('\n', stderr); exit(255); @@ -82,8 +84,9 @@ void debugmsg(const char *fmt, ...) { - if (!debugmsgenabled) + if (!debugmsgenabled) { return; + } va_list args; va_start(args, fmt); @@ -98,32 +101,37 @@ void fchdirx(int dirfd) { int r = fchdir(dirfd); - if (r == -1) + if (r == -1) { abortmsgerrno("failed to fchdir"); + } } void fsetcloexec(int fd) { int flags = fcntl(fd, F_GETFD); - if (flags < 0) + if (flags < 0) { abortmsgerrno("cannot get flags of fd %d", fd); - if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) + } + if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) { abortmsgerrno("cannot set flags of fd %d", fd); + } } void *mallocx(size_t size) { void *result = malloc(size); - if (!result) + if (!result) { abortmsg("failed to malloc"); + } return result; } void *reallocx(void *ptr, size_t size) { void *result = realloc(ptr, size); - if (!result) + if (!result) { abortmsg("failed to realloc"); + } return result; } @@ -144,30 +152,37 @@ memset(&newsa, 0, sizeof(newsa)); newsa.sa_handler = SIG_IGN; newsa.sa_flags = 0; - if (sigemptyset(&newsa.sa_mask) < 0) + if (sigemptyset(&newsa.sa_mask) < 0) { goto done; - if (sigaction(SIGINT, &newsa, &oldsaint) < 0) + } + if (sigaction(SIGINT, &newsa, &oldsaint) < 0) { goto done; + } doneflags |= F_SIGINT; - if (sigaction(SIGQUIT, &newsa, &oldsaquit) < 0) + if (sigaction(SIGQUIT, &newsa, &oldsaquit) < 0) { goto done; + } doneflags |= F_SIGQUIT; - if (sigaddset(&newsa.sa_mask, SIGCHLD) < 0) + if (sigaddset(&newsa.sa_mask, SIGCHLD) < 0) { goto done; - if (sigprocmask(SIG_BLOCK, &newsa.sa_mask, &oldmask) < 0) + } + if (sigprocmask(SIG_BLOCK, &newsa.sa_mask, &oldmask) < 0) { goto done; + } doneflags |= F_SIGMASK; pid_t pid = fork(); - if (pid < 0) + if (pid < 0) { goto done; + } if (pid == 0) { sigaction(SIGINT, &oldsaint, NULL); sigaction(SIGQUIT, &oldsaquit, NULL); sigprocmask(SIG_SETMASK, &oldmask, NULL); - if (cwd && chdir(cwd) < 0) + if (cwd && chdir(cwd) < 0) { _exit(127); + } const char *argv[] = {"sh", "-c", cmd, NULL}; if (envp) { execve("/bin/sh", (char **)argv, (char **)envp); @@ -176,25 +191,32 @@ } _exit(127); } else { - if (waitpid(pid, &status, 0) < 0) + if (waitpid(pid, &status, 0) < 0) { goto done; + } doneflags |= F_WAITPID; } done: - if (doneflags & F_SIGINT) + if (doneflags & F_SIGINT) { sigaction(SIGINT, &oldsaint, NULL); - if (doneflags & F_SIGQUIT) + } + if (doneflags & F_SIGQUIT) { sigaction(SIGQUIT, &oldsaquit, NULL); - if (doneflags & F_SIGMASK) + } + if (doneflags & F_SIGMASK) { sigprocmask(SIG_SETMASK, &oldmask, NULL); + } /* no way to report other errors, use 127 (= shell termination) */ - if (!(doneflags & F_WAITPID)) + if (!(doneflags & F_WAITPID)) { return 127; - if (WIFEXITED(status)) + } + if (WIFEXITED(status)) { return WEXITSTATUS(status); - if (WIFSIGNALED(status)) + } + if (WIFSIGNALED(status)) { return -WTERMSIG(status); + } return 127; }