X Tutup
Skip to content

Commit d94a24c

Browse files
committed
Add macro for checking if some flags are set
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.
1 parent 00bfe67 commit d94a24c

File tree

12 files changed

+30
-13
lines changed

12 files changed

+30
-13
lines changed

coccinelle/flags-set.cocci

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@@
2+
expression x, y;
3+
@@
4+
- ((x) & (y)) == (y)
5+
+ FLAGS_SET(x, y)
6+
@@
7+
expression x, y;
8+
@@
9+
- (x & (y)) == (y)
10+
+ FLAGS_SET(x, y)
11+
@@
12+
expression x, y;
13+
@@
14+
- ((x) & y) == y
15+
+ FLAGS_SET(x, y)

src/basic/capability-util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static inline void cap_free_charpp(char **p) {
4141
static inline bool cap_test_all(uint64_t caps) {
4242
uint64_t m;
4343
m = (UINT64_C(1) << (cap_last_cap() + 1)) - 1;
44-
return (caps & m) == m;
44+
return FLAGS_SET(caps, m);
4545
}
4646

4747
bool ambient_capabilities_supported(void);

src/basic/copy.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ static int fd_is_nonblock_pipe(int fd) {
8181
if (flags < 0)
8282
return -errno;
8383

84-
return (flags & O_NONBLOCK) == O_NONBLOCK ? FD_IS_NONBLOCKING_PIPE : FD_IS_BLOCKING_PIPE;
84+
return FLAGS_SET(flags, O_NONBLOCK) ? FD_IS_NONBLOCKING_PIPE : FD_IS_BLOCKING_PIPE;
8585
}
8686

8787
int copy_bytes_full(

src/basic/fs-util.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,10 +602,10 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
602602
assert(path);
603603

604604
/* Either the file may be missing, or we return an fd to the final object, but both make no sense */
605-
if ((flags & (CHASE_NONEXISTENT|CHASE_OPEN)) == (CHASE_NONEXISTENT|CHASE_OPEN))
605+
if (FLAGS_SET(flags, CHASE_NONEXISTENT | CHASE_OPEN))
606606
return -EINVAL;
607607

608-
if ((flags & (CHASE_STEP|CHASE_OPEN)) == (CHASE_STEP|CHASE_OPEN))
608+
if (FLAGS_SET(flags, CHASE_STEP | CHASE_OPEN))
609609
return -EINVAL;
610610

611611
if (isempty(path))

src/basic/macro.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,8 @@ static inline unsigned long ALIGN_POWER2(unsigned long u) {
357357

358358
#define SET_FLAG(v, flag, b) \
359359
(v) = (b) ? ((v) | (flag)) : ((v) & ~(flag))
360+
#define FLAGS_SET(v, flags) \
361+
(((v) & (flags)) == (flags))
360362

361363
#define CASE_F(X) case X:
362364
#define CASE_F_1(CASE, X) CASE_F(X)

src/basic/process-util.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1359,7 +1359,7 @@ int safe_fork_full(
13591359
}
13601360
}
13611361

1362-
if ((flags & (FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE)) == (FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE)) {
1362+
if (FLAGS_SET(flags, FORK_NEW_MOUNTNS | FORK_MOUNTNS_SLAVE)) {
13631363

13641364
/* Optionally, make sure we never propagate mounts to the host. */
13651365

src/basic/rm-rf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ int rm_rf(const char *path, RemoveFlags flags) {
178178
return -EPERM;
179179
}
180180

181-
if ((flags & (REMOVE_SUBVOLUME|REMOVE_ROOT|REMOVE_PHYSICAL)) == (REMOVE_SUBVOLUME|REMOVE_ROOT|REMOVE_PHYSICAL)) {
181+
if (FLAGS_SET(flags, REMOVE_SUBVOLUME | REMOVE_ROOT | REMOVE_PHYSICAL)) {
182182
/* Try to remove as subvolume first */
183183
r = btrfs_subvol_remove(path, BTRFS_REMOVE_RECURSIVE|BTRFS_REMOVE_QUOTA);
184184
if (r >= 0)

src/core/cgroup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1303,7 +1303,7 @@ const char *unit_get_realized_cgroup_path(Unit *u, CGroupMask mask) {
13031303

13041304
if (u->cgroup_path &&
13051305
u->cgroup_realized &&
1306-
(u->cgroup_realized_mask & mask) == mask)
1306+
FLAGS_SET(u->cgroup_realized_mask, mask))
13071307
return u->cgroup_path;
13081308

13091309
u = UNIT_DEREF(u->slice);

src/core/unit.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,7 +1053,7 @@ static void print_unit_dependency_mask(FILE *f, const char *kind, UnitDependency
10531053
if (mask == 0)
10541054
break;
10551055

1056-
if ((mask & table[i].mask) == table[i].mask) {
1056+
if (FLAGS_SET(mask, table[i].mask)) {
10571057
if (*space)
10581058
fputc(' ', f);
10591059
else
@@ -2695,8 +2695,8 @@ static int unit_add_dependency_hashmap(
26952695
if (info.data) {
26962696
/* Entry already exists. Add in our mask. */
26972697

2698-
if ((info.origin_mask & origin_mask) == info.origin_mask &&
2699-
(info.destination_mask & destination_mask) == info.destination_mask)
2698+
if (FLAGS_SET(origin_mask, info.origin_mask) &&
2699+
FLAGS_SET(destination_mask, info.destination_mask))
27002700
return 0; /* NOP */
27012701

27022702
info.origin_mask |= origin_mask;

src/import/curl-util.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static int curl_glue_on_io(sd_event_source *s, int fd, uint32_t revents, void *u
3737

3838
translated_fd = PTR_TO_FD(hashmap_get(g->translate_fds, FD_TO_PTR(fd)));
3939

40-
if ((revents & (EPOLLIN|EPOLLOUT)) == (EPOLLIN|EPOLLOUT))
40+
if (FLAGS_SET(revents, EPOLLIN | EPOLLOUT))
4141
action = CURL_POLL_INOUT;
4242
else if (revents & EPOLLIN)
4343
action = CURL_POLL_IN;

0 commit comments

Comments
 (0)
X Tutup