Skip to content

Commit

Permalink
Prevent clang-format in certain places (#468)
Browse files Browse the repository at this point in the history
This is a preparation for adding clang-format.

These comments prevent automatic formatting in some places. With these
exceptions, we will be able to run clang-format on the rest of the code.

This is a preparation for #323.

---------

Signed-off-by: Viktor Söderqvist <viktor.soderqvist@est.tech>
  • Loading branch information
zuiderkwast committed May 8, 2024
1 parent 2278dfd commit 6af51f5
Show file tree
Hide file tree
Showing 24 changed files with 161 additions and 38 deletions.
12 changes: 12 additions & 0 deletions src/.clang-format-ignore
@@ -0,0 +1,12 @@
# Don't format files copied from other sources.
lzf*
crccombine.*
crcspeed.*
mt19937-64.*
pqsort.*
setcpuaffinity.c
setproctitle.c
sha1.*
sha256.*
siphash.c
strl.c
22 changes: 17 additions & 5 deletions src/acl.c
Expand Up @@ -1601,10 +1601,12 @@ static int ACLSelectorCheckKey(aclSelector *selector, const char *key, int keyle
listRewind(selector->patterns,&li);

int key_flags = 0;
/* clang-format off */
if (keyspec_flags & CMD_KEY_ACCESS) key_flags |= ACL_READ_PERMISSION;
if (keyspec_flags & CMD_KEY_INSERT) key_flags |= ACL_WRITE_PERMISSION;
if (keyspec_flags & CMD_KEY_DELETE) key_flags |= ACL_WRITE_PERMISSION;
if (keyspec_flags & CMD_KEY_UPDATE) key_flags |= ACL_WRITE_PERMISSION;
/* clang-format on */

/* Test this key against every pattern. */
while((ln = listNext(&li))) {
Expand Down Expand Up @@ -1632,10 +1634,12 @@ static int ACLSelectorHasUnrestrictedKeyAccess(aclSelector *selector, int flags)
listRewind(selector->patterns,&li);

int access_flags = 0;
/* clang-format off */
if (flags & CMD_KEY_ACCESS) access_flags |= ACL_READ_PERMISSION;
if (flags & CMD_KEY_INSERT) access_flags |= ACL_WRITE_PERMISSION;
if (flags & CMD_KEY_DELETE) access_flags |= ACL_WRITE_PERMISSION;
if (flags & CMD_KEY_UPDATE) access_flags |= ACL_WRITE_PERMISSION;
/* clang-format on */

/* Test this key against every pattern. */
while((ln = listNext(&li))) {
Expand Down Expand Up @@ -2700,13 +2704,15 @@ void addACLLogEntry(client *c, int reason, int context, int argpos, sds username
if (object) {
le->object = object;
} else {
/* clang-format off */
switch(reason) {
case ACL_DENIED_CMD: le->object = sdsdup(c->cmd->fullname); break;
case ACL_DENIED_KEY: le->object = sdsdup(c->argv[argpos]->ptr); break;
case ACL_DENIED_CHANNEL: le->object = sdsdup(c->argv[argpos]->ptr); break;
case ACL_DENIED_AUTH: le->object = sdsdup(c->argv[0]->ptr); break;
default: le->object = sdsempty();
case ACL_DENIED_CMD: le->object = sdsdup(c->cmd->fullname); break;
case ACL_DENIED_KEY: le->object = sdsdup(c->argv[argpos]->ptr); break;
case ACL_DENIED_CHANNEL: le->object = sdsdup(c->argv[argpos]->ptr); break;
case ACL_DENIED_AUTH: le->object = sdsdup(c->argv[0]->ptr); break;
default: le->object = sdsempty();
}
/* clang-format on */
}

/* if we have a real client from the network, use it (could be missing on module timers) */
Expand Down Expand Up @@ -3090,24 +3096,28 @@ void aclCommand(client *c) {

addReplyBulkCString(c,"reason");
char *reasonstr;
/* clang-format off */
switch(le->reason) {
case ACL_DENIED_CMD: reasonstr="command"; break;
case ACL_DENIED_KEY: reasonstr="key"; break;
case ACL_DENIED_CHANNEL: reasonstr="channel"; break;
case ACL_DENIED_AUTH: reasonstr="auth"; break;
default: reasonstr="unknown";
}
/* clang-format on */
addReplyBulkCString(c,reasonstr);

addReplyBulkCString(c,"context");
char *ctxstr;
/* clang-format off */
switch(le->context) {
case ACL_LOG_CTX_TOPLEVEL: ctxstr="toplevel"; break;
case ACL_LOG_CTX_MULTI: ctxstr="multi"; break;
case ACL_LOG_CTX_LUA: ctxstr="lua"; break;
case ACL_LOG_CTX_MODULE: ctxstr="module"; break;
default: ctxstr="unknown";
}
/* clang-format on */
addReplyBulkCString(c,ctxstr);

addReplyBulkCString(c,"object");
Expand Down Expand Up @@ -3156,6 +3166,7 @@ void aclCommand(client *c) {

addReply(c,shared.ok);
} else if (c->argc == 2 && !strcasecmp(sub,"help")) {
/* clang-format off */
const char *help[] = {
"CAT [<category>]",
" List all commands that belong to <category>, or all command categories",
Expand Down Expand Up @@ -3185,6 +3196,7 @@ void aclCommand(client *c) {
" Return the current connection username.",
NULL
};
/* clang-format on */
addReplyHelp(c,help);
} else {
addReplySubcommandSyntaxError(c);
Expand Down
4 changes: 4 additions & 0 deletions src/aof.c
Expand Up @@ -2035,6 +2035,7 @@ int rioWriteStreamPendingEntry(rio *r, robj *key, const char *groupname, size_t
RETRYCOUNT <count> JUSTID FORCE. */
streamID id;
streamDecodeID(rawid,&id);
/* clang-format off */
if (rioWriteBulkCount(r,'*',12) == 0) return 0;
if (rioWriteBulkString(r,"XCLAIM",6) == 0) return 0;
if (rioWriteBulkObject(r,key) == 0) return 0;
Expand All @@ -2048,6 +2049,7 @@ int rioWriteStreamPendingEntry(rio *r, robj *key, const char *groupname, size_t
if (rioWriteBulkLongLong(r,nack->delivery_count) == 0) return 0;
if (rioWriteBulkString(r,"JUSTID",6) == 0) return 0;
if (rioWriteBulkString(r,"FORCE",5) == 0) return 0;
/* clang-format on */
return 1;
}

Expand All @@ -2056,12 +2058,14 @@ int rioWriteStreamPendingEntry(rio *r, robj *key, const char *groupname, size_t
* All this in the context of the specified key and group. */
int rioWriteStreamEmptyConsumer(rio *r, robj *key, const char *groupname, size_t groupname_len, streamConsumer *consumer) {
/* XGROUP CREATECONSUMER <key> <group> <consumer> */
/* clang-format off */
if (rioWriteBulkCount(r,'*',5) == 0) return 0;
if (rioWriteBulkString(r,"XGROUP",6) == 0) return 0;
if (rioWriteBulkString(r,"CREATECONSUMER",14) == 0) return 0;
if (rioWriteBulkObject(r,key) == 0) return 0;
if (rioWriteBulkString(r,groupname,groupname_len) == 0) return 0;
if (rioWriteBulkString(r,consumer->name,sdslen(consumer->name)) == 0) return 0;
/* clang-format on */
return 1;
}

Expand Down
3 changes: 3 additions & 0 deletions src/asciilogo.h
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
* Copyright (c) 2024, Valkey contributors
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -27,6 +28,7 @@
* POSSIBILITY OF SUCH DAMAGE.
*/

/* clang-format off */
const char *ascii_logo =
" .+^+. \n"
" .+#########+. \n"
Expand All @@ -47,3 +49,4 @@ const char *ascii_logo =
" '| |####+########+' \n"
" +#########+' \n"
" '+v+' \n\n";
/* clang-format off */
8 changes: 6 additions & 2 deletions src/eval.c
Expand Up @@ -675,15 +675,16 @@ void evalShaRoCommand(client *c) {

void scriptCommand(client *c) {
if (c->argc == 2 && !strcasecmp(c->argv[1]->ptr,"help")) {
/* clang-format off */
const char *help[] = {
"DEBUG (YES|SYNC|NO)",
" Set the debug mode for subsequent scripts executed.",
"EXISTS <sha1> [<sha1> ...]",
" Return information about the existence of the scripts in the script cache.",
"FLUSH [ASYNC|SYNC]",
" Flush the Lua scripts cache. Very dangerous on replicas.",
" When called without the optional mode argument, the behavior is determined by the",
" lazyfree-lazy-user-flush configuration directive. Valid modes are:",
" When called without the optional mode argument, the behavior is determined",
" by the lazyfree-lazy-user-flush configuration directive. Valid modes are:",
" * ASYNC: Asynchronously flush the scripts cache.",
" * SYNC: Synchronously flush the scripts cache.",
"KILL",
Expand All @@ -692,6 +693,7 @@ void scriptCommand(client *c) {
" Load a script into the scripts cache without executing it.",
NULL
};
/* clang-format on */
addReplyHelp(c, help);
} else if (c->argc >= 2 && !strcasecmp(c->argv[1]->ptr,"flush")) {
int async = 0;
Expand Down Expand Up @@ -1251,6 +1253,7 @@ char *ldbRedisProtocolToHuman_Double(sds *o, char *reply);
* char*) so that we can return a modified pointer, as for SDS semantics. */
char *ldbRedisProtocolToHuman(sds *o, char *reply) {
char *p = reply;
/* clang-format off */
switch(*p) {
case ':': p = ldbRedisProtocolToHuman_Int(o,reply); break;
case '$': p = ldbRedisProtocolToHuman_Bulk(o,reply); break;
Expand All @@ -1263,6 +1266,7 @@ char *ldbRedisProtocolToHuman(sds *o, char *reply) {
case '#': p = ldbRedisProtocolToHuman_Bool(o,reply); break;
case ',': p = ldbRedisProtocolToHuman_Double(o,reply); break;
}
/* clang-format on */
return p;
}

Expand Down
2 changes: 2 additions & 0 deletions src/functions.c
Expand Up @@ -826,6 +826,7 @@ void functionFlushCommand(client *c) {

/* FUNCTION HELP */
void functionHelpCommand(client *c) {
/* clang-format off */
const char *help[] = {
"LOAD [REPLACE] <FUNCTION CODE>",
" Create a new library with the given library name and code.",
Expand Down Expand Up @@ -864,6 +865,7 @@ void functionHelpCommand(client *c) {
" libraries with the new libraries (notice that even on this option there is a chance of failure",
" in case of functions name collision with another library).",
NULL };
/* clang-format on */
addReplyHelp(c, help);
}

Expand Down
2 changes: 2 additions & 0 deletions src/latency.c
Expand Up @@ -696,6 +696,7 @@ void latencyCommand(client *c) {
latencySpecificCommandsFillCDF(c);
}
} else if (!strcasecmp(c->argv[1]->ptr,"help") && c->argc == 2) {
/* clang-format off */
const char *help[] = {
"DOCTOR",
" Return a human readable latency analysis report.",
Expand All @@ -713,6 +714,7 @@ void latencyCommand(client *c) {
" If no commands are specified then all histograms are replied.",
NULL
};
/* clang-format on */
addReplyHelp(c, help);
} else {
addReplySubcommandSyntaxError(c);
Expand Down
4 changes: 4 additions & 0 deletions src/listpack.c
Expand Up @@ -426,6 +426,7 @@ static inline void lpEncodeString(unsigned char *buf, unsigned char *s, uint32_t
* lpCurrentEncodedSizeBytes or ASSERT_INTEGRITY_LEN (possibly since 'p' is
* a return value of another function that validated its return. */
static inline uint32_t lpCurrentEncodedSizeUnsafe(unsigned char *p) {
/* clang-format off */
if (LP_ENCODING_IS_7BIT_UINT(p[0])) return 1;
if (LP_ENCODING_IS_6BIT_STR(p[0])) return 1+LP_ENCODING_6BIT_STR_LEN(p);
if (LP_ENCODING_IS_13BIT_INT(p[0])) return 2;
Expand All @@ -437,13 +438,15 @@ static inline uint32_t lpCurrentEncodedSizeUnsafe(unsigned char *p) {
if (LP_ENCODING_IS_32BIT_STR(p[0])) return 5+LP_ENCODING_32BIT_STR_LEN(p);
if (p[0] == LP_EOF) return 1;
return 0;
/* clang-format on */
}

/* Return bytes needed to encode the length of the listpack element pointed by 'p'.
* This includes just the encoding byte, and the bytes needed to encode the length
* of the element (excluding the element data itself)
* If the element encoding is wrong then 0 is returned. */
static inline uint32_t lpCurrentEncodedSizeBytes(unsigned char *p) {
/* clang-format off */
if (LP_ENCODING_IS_7BIT_UINT(p[0])) return 1;
if (LP_ENCODING_IS_6BIT_STR(p[0])) return 1;
if (LP_ENCODING_IS_13BIT_INT(p[0])) return 1;
Expand All @@ -455,6 +458,7 @@ static inline uint32_t lpCurrentEncodedSizeBytes(unsigned char *p) {
if (LP_ENCODING_IS_32BIT_STR(p[0])) return 5;
if (p[0] == LP_EOF) return 1;
return 0;
/* clang-format on */
}

/* Skip the current entry returning the next. It is invalid to call this
Expand Down
2 changes: 2 additions & 0 deletions src/module.c
Expand Up @@ -1143,6 +1143,7 @@ int64_t commandFlagsFromString(char *s) {
sds *tokens = sdssplitlen(s,strlen(s)," ",1,&count);
for (j = 0; j < count; j++) {
char *t = tokens[j];
/* clang-format off */
if (!strcasecmp(t,"write")) flags |= CMD_WRITE;
else if (!strcasecmp(t,"readonly")) flags |= CMD_READONLY;
else if (!strcasecmp(t,"admin")) flags |= CMD_ADMIN;
Expand All @@ -1164,6 +1165,7 @@ int64_t commandFlagsFromString(char *s) {
else if (!strcasecmp(t,"no-mandatory-keys")) flags |= CMD_NO_MANDATORY_KEYS;
else if (!strcasecmp(t,"allow-busy")) flags |= CMD_ALLOW_BUSY;
else break;
/* clang-format on */
}
sdsfreesplitres(tokens,count);
if (j != count) return -1; /* Some token not processed correctly. */
Expand Down
8 changes: 8 additions & 0 deletions src/networking.c
Expand Up @@ -2813,6 +2813,7 @@ sds catClientInfoString(sds s, client *client) {
else
*p++ = 'S';
}
/* clang-format off */
if (client->flags & CLIENT_MASTER) *p++ = 'M';
if (client->flags & CLIENT_PUBSUB) *p++ = 'P';
if (client->flags & CLIENT_MULTI) *p++ = 'x';
Expand All @@ -2829,6 +2830,7 @@ sds catClientInfoString(sds s, client *client) {
if (client->flags & CLIENT_NO_EVICT) *p++ = 'e';
if (client->flags & CLIENT_NO_TOUCH) *p++ = 'T';
if (p == flags) *p++ = 'N';
/* clang-format on */
*p++ = '\0';

p = events;
Expand All @@ -2848,6 +2850,7 @@ sds catClientInfoString(sds s, client *client) {
used_blocks_of_repl_buf = last->id - cur->id + 1;
}

/* clang-format off */
sds ret = sdscatfmt(s, FMTARGS(
"id=%U", (unsigned long long) client->id,
" addr=%s", getClientPeerId(client),
Expand Down Expand Up @@ -2883,6 +2886,7 @@ sds catClientInfoString(sds s, client *client) {
" tot-net-in=%U", client->net_input_bytes,
" tot-net-out=%U", client->net_output_bytes,
" tot-cmds=%U", client->commands_processed));
/* clang-format on */
return ret;
}

Expand Down Expand Up @@ -3025,6 +3029,7 @@ void clientCommand(client *c) {
listIter li;

if (c->argc == 2 && !strcasecmp(c->argv[1]->ptr,"help")) {
/* clang-format off */
const char *help[] = {
"CACHING (YES|NO)",
" Enable/disable tracking of the keys for next command in OPTIN/OPTOUT modes.",
Expand Down Expand Up @@ -3083,6 +3088,7 @@ void clientCommand(client *c) {
" Will not touch LRU/LFU stats when this mode is on.",
NULL
};
/* clang-format on */
addReplyHelp(c, help);
} else if (!strcasecmp(c->argv[1]->ptr,"id") && c->argc == 2) {
/* CLIENT ID */
Expand Down Expand Up @@ -3242,13 +3248,15 @@ NULL
listRewind(server.clients,&li);
while ((ln = listNext(&li)) != NULL) {
client *client = listNodeValue(ln);
/* clang-format off */
if (addr && strcmp(getClientPeerId(client),addr) != 0) continue;
if (laddr && strcmp(getClientSockname(client),laddr) != 0) continue;
if (type != -1 && getClientType(client) != type) continue;
if (id != 0 && client->id != id) continue;
if (user && client->user != user) continue;
if (c == client && skipme) continue;
if (max_age != 0 && (long long)(commandTimeSnapshot() / 1000 - client->ctime) < max_age) continue;
/* clang-format on */

/* Kill it. */
if (c == client) {
Expand Down
4 changes: 4 additions & 0 deletions src/notify.c
Expand Up @@ -42,6 +42,7 @@ int keyspaceEventsStringToFlags(char *classes) {
int c, flags = 0;

while((c = *p++) != '\0') {
/* clang-format off */
switch(c) {
case 'A': flags |= NOTIFY_ALL; break;
case 'g': flags |= NOTIFY_GENERIC; break;
Expand All @@ -60,6 +61,7 @@ int keyspaceEventsStringToFlags(char *classes) {
case 'n': flags |= NOTIFY_NEW; break;
default: return -1;
}
/* clang-format on */
}
return flags;
}
Expand All @@ -71,6 +73,7 @@ int keyspaceEventsStringToFlags(char *classes) {
sds keyspaceEventsFlagsToString(int flags) {
sds res;

/* clang-format off */
res = sdsempty();
if ((flags & NOTIFY_ALL) == NOTIFY_ALL) {
res = sdscatlen(res,"A",1);
Expand All @@ -91,6 +94,7 @@ sds keyspaceEventsFlagsToString(int flags) {
if (flags & NOTIFY_KEYEVENT) res = sdscatlen(res,"E",1);
if (flags & NOTIFY_KEY_MISS) res = sdscatlen(res,"m",1);
return res;
/* clang-format on */
}

/* The API provided to the rest of the serer core is a simple function:
Expand Down

0 comments on commit 6af51f5

Please sign in to comment.