]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Add macro for checking if some flags are set
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 20 Apr 2018 13:36:20 +0000 (15:36 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 4 Jun 2018 09:50:44 +0000 (11:50 +0200)
This way we don't need to repeat the argument twice.
I didn't replace all instances. I think it's better to leave out:
- asserts
- comparisons like x & y == x, which are mathematically equivalent, but
  here we aren't checking if flags are set, but if the argument fits in the
  flags.

12 files changed:
coccinelle/flags-set.cocci [new file with mode: 0644]
src/basic/capability-util.h
src/basic/copy.c
src/basic/fs-util.c
src/basic/macro.h
src/basic/process-util.c
src/basic/rm-rf.c
src/core/cgroup.c
src/core/unit.c
src/import/curl-util.c
src/shared/condition.c
src/udev/udev-builtin-input_id.c

diff --git a/coccinelle/flags-set.cocci b/coccinelle/flags-set.cocci
new file mode 100644 (file)
index 0000000..1a70717
--- /dev/null
@@ -0,0 +1,15 @@
+@@
+expression x, y;
+@@
+- ((x) & (y)) == (y)
++ FLAGS_SET(x, y)
+@@
+expression x, y;
+@@
+- (x & (y)) == (y)
++ FLAGS_SET(x, y)
+@@
+expression x, y;
+@@
+- ((x) & y) == y
++ FLAGS_SET(x, y)
index f90efe2550daa21aece3abbff9004bac32c5bf25..7e51791681df2c99f161411da62fdc9c1f695d16 100644 (file)
@@ -41,7 +41,7 @@ static inline void cap_free_charpp(char **p) {
 static inline bool cap_test_all(uint64_t caps) {
         uint64_t m;
         m = (UINT64_C(1) << (cap_last_cap() + 1)) - 1;
-        return (caps & m) == m;
+        return FLAGS_SET(caps, m);
 }
 
 bool ambient_capabilities_supported(void);
index 5b6cae39d056dfcbf570053c1594210f43b5f5f4..650de612b88c3b6dd27148ba2a849dd6e4fa88c7 100644 (file)
@@ -81,7 +81,7 @@ static int fd_is_nonblock_pipe(int fd) {
         if (flags < 0)
                 return -errno;
 
-        return (flags & O_NONBLOCK) == O_NONBLOCK ? FD_IS_NONBLOCKING_PIPE : FD_IS_BLOCKING_PIPE;
+        return FLAGS_SET(flags, O_NONBLOCK) ? FD_IS_NONBLOCKING_PIPE : FD_IS_BLOCKING_PIPE;
 }
 
 int copy_bytes_full(
index a0d7a43d274c35b570f038acb020943788db8fc2..6146c6678207f8b5a43a4f1b74d1eacd8c061d2b 100644 (file)
@@ -602,10 +602,10 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
         assert(path);
 
         /* Either the file may be missing, or we return an fd to the final object, but both make no sense */
-        if ((flags & (CHASE_NONEXISTENT|CHASE_OPEN)) == (CHASE_NONEXISTENT|CHASE_OPEN))
+        if (FLAGS_SET(flags, CHASE_NONEXISTENT | CHASE_OPEN))
                 return -EINVAL;
 
-        if ((flags & (CHASE_STEP|CHASE_OPEN)) == (CHASE_STEP|CHASE_OPEN))
+        if (FLAGS_SET(flags, CHASE_STEP | CHASE_OPEN))
                 return -EINVAL;
 
         if (isempty(path))
index cd3ae8d3ab734cba17790739dc0e73f783e0c379..05f5e9fa68bc2cb80dac2133336ae6effa934805 100644 (file)
@@ -357,6 +357,8 @@ static inline unsigned long ALIGN_POWER2(unsigned long u) {
 
 #define SET_FLAG(v, flag, b) \
         (v) = (b) ? ((v) | (flag)) : ((v) & ~(flag))
+#define FLAGS_SET(v, flags) \
+        (((v) & (flags)) == (flags))
 
 #define CASE_F(X) case X:
 #define CASE_F_1(CASE, X) CASE_F(X)
index e3be7b97933bb31f884ed6b1939c2651554c85b3..61ab181e6f7e3e349b97e064410640df91552da5 100644 (file)
@@ -1359,7 +1359,7 @@ int safe_fork_full(
                 }
         }
 
-        if ((flags & (FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE)) == (FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE)) {
+        if (FLAGS_SET(flags, FORK_NEW_MOUNTNS | FORK_MOUNTNS_SLAVE)) {
 
                 /* Optionally, make sure we never propagate mounts to the host. */
 
index 91474ad95cf89ca6176139e5aadbb89dcbeb5d76..20f094b9a186c9c0478b905b13b2d281c543262d 100644 (file)
@@ -178,7 +178,7 @@ int rm_rf(const char *path, RemoveFlags flags) {
                 return -EPERM;
         }
 
-        if ((flags & (REMOVE_SUBVOLUME|REMOVE_ROOT|REMOVE_PHYSICAL)) == (REMOVE_SUBVOLUME|REMOVE_ROOT|REMOVE_PHYSICAL)) {
+        if (FLAGS_SET(flags, REMOVE_SUBVOLUME | REMOVE_ROOT | REMOVE_PHYSICAL)) {
                 /* Try to remove as subvolume first */
                 r = btrfs_subvol_remove(path, BTRFS_REMOVE_RECURSIVE|BTRFS_REMOVE_QUOTA);
                 if (r >= 0)
index b3f8600eb4c365d64049724b25e7a1f78241b8ce..3927d6df85024c274413df8b8da3f39eb5075b7a 100644 (file)
@@ -1303,7 +1303,7 @@ const char *unit_get_realized_cgroup_path(Unit *u, CGroupMask mask) {
 
                 if (u->cgroup_path &&
                     u->cgroup_realized &&
-                    (u->cgroup_realized_mask & mask) == mask)
+                    FLAGS_SET(u->cgroup_realized_mask, mask))
                         return u->cgroup_path;
 
                 u = UNIT_DEREF(u->slice);
index fd0d8da3aaa6a9aa8a0ccf5fac1e6252c47236a9..0c49f6115e06e52734c106accfefbb332e3abcbe 100644 (file)
@@ -1053,7 +1053,7 @@ static void print_unit_dependency_mask(FILE *f, const char *kind, UnitDependency
                 if (mask == 0)
                         break;
 
-                if ((mask & table[i].mask) == table[i].mask) {
+                if (FLAGS_SET(mask, table[i].mask)) {
                         if (*space)
                                 fputc(' ', f);
                         else
@@ -2695,8 +2695,8 @@ static int unit_add_dependency_hashmap(
         if (info.data) {
                 /* Entry already exists. Add in our mask. */
 
-                if ((info.origin_mask & origin_mask) == info.origin_mask &&
-                    (info.destination_mask & destination_mask) == info.destination_mask)
+                if (FLAGS_SET(origin_mask, info.origin_mask) &&
+                    FLAGS_SET(destination_mask, info.destination_mask))
                         return 0; /* NOP */
 
                 info.origin_mask |= origin_mask;
index 2fba6e29dd663b064f169ef10d813a0ca3dedaa8..c19a01a266b984b5c7aaa4fce85fb30510ef9af9 100644 (file)
@@ -37,7 +37,7 @@ static int curl_glue_on_io(sd_event_source *s, int fd, uint32_t revents, void *u
 
         translated_fd = PTR_TO_FD(hashmap_get(g->translate_fds, FD_TO_PTR(fd)));
 
-        if ((revents & (EPOLLIN|EPOLLOUT)) == (EPOLLIN|EPOLLOUT))
+        if (FLAGS_SET(revents, EPOLLIN | EPOLLOUT))
                 action = CURL_POLL_INOUT;
         else if (revents & EPOLLIN)
                 action = CURL_POLL_IN;
index bf6476c3393a13338234487bda3e5e0126318669..a41a7826644547b5bd0f39bbe45a8a2638644dc6 100644 (file)
@@ -251,7 +251,7 @@ static int condition_test_control_group_controller(Condition *c) {
                 return 1;
         }
 
-        return (system_mask & wanted_mask) == wanted_mask;
+        return FLAGS_SET(system_mask, wanted_mask);
 }
 
 static int condition_test_group(Condition *c) {
index 4d5b1233f17fcd312af5b01d5e0f9beef38848e4..f80a6a595c49001430458f86d48f6b4d6f6989ab 100644 (file)
@@ -307,7 +307,7 @@ static bool test_key(struct udev_device *dev,
         /* the first 32 bits are ESC, numbers, and Q to D; if we have all of
          * those, consider it a full keyboard; do not test KEY_RESERVED, though */
         mask = 0xFFFFFFFE;
-        if ((bitmask_key[0] & mask) == mask) {
+        if (FLAGS_SET(bitmask_key[0], mask)) {
                 udev_builtin_add_property(dev, test, "ID_INPUT_KEYBOARD", "1");
                 ret = true;
         }