From 69f39b5e3b7b6c8b29b0ae9e500b5ff5235afdb9 Mon Sep 17 00:00:00 2001 From: iphydf Date: Sat, 2 May 2020 00:34:13 +0100 Subject: [PATCH] Add a check that we don't have any unused functions. This check puts all of our code in a C++ anonymous namespace, which is effectively making all functions `static`. This allows the compiler to determine that a function is unused, so we can delete it. --- other/analysis/gen-file.sh | 7 ++++ other/cpufeatures.c | 2 +- testing/DHT_test.c | 1 + toxav/ring_buffer_test.cc | 4 +-- toxav/rtp_test.cc | 4 +-- toxcore/LAN_discovery.api.h | 2 +- toxcore/LAN_discovery.h | 2 -- toxcore/Messenger.c | 32 ------------------- toxcore/Messenger.h | 9 ------ toxcore/crypto_core.c | 3 +- toxcore/crypto_core_test.cc | 4 +-- toxcore/group.c | 30 ----------------- toxcore/group.h | 13 -------- toxcore/list.c | 10 ------ toxcore/list.h | 8 ----- toxcore/ping_array_test.cc | 3 +- toxcore/util_test.cc | 4 +-- .../sse/pwhash_scryptsalsa208sha256_sse.c | 2 +- 18 files changed, 23 insertions(+), 117 deletions(-) diff --git a/other/analysis/gen-file.sh b/other/analysis/gen-file.sh index 019bd8f937c..e98aa467eda 100644 --- a/other/analysis/gen-file.sh +++ b/other/analysis/gen-file.sh @@ -53,6 +53,12 @@ FIND_QUERY="$FIND_QUERY -and -not -name av_test.c" FIND_QUERY="$FIND_QUERY -and -not -name dht_test.c" FIND_QUERY="$FIND_QUERY -and -not -name version_test.c" +(for i in $(eval $FIND_QUERY); do + grep -o '#include <[^>]*>' $i \ + | grep -E -v '|> amalgamation.cc + +echo 'namespace {' >> amalgamation.cc for i in $(eval $FIND_QUERY); do if ! grep -q '^int main(' $i; then put $i @@ -67,6 +73,7 @@ done echo "static void call(int m(), int argc, char **argv) { m(); }" >> amalgamation.cc echo "static void call(int m(int, char **), int argc, char **argv) { m(argc, argv); }" >> amalgamation.cc +echo '} // namespace' >> amalgamation.cc echo "int main(int argc, char **argv) {" >> amalgamation.cc for i in $(eval $FIND_QUERY); do diff --git a/other/cpufeatures.c b/other/cpufeatures.c index 3da06e58e66..38c97059963 100644 --- a/other/cpufeatures.c +++ b/other/cpufeatures.c @@ -3,4 +3,4 @@ #include ANDROID_CPU_FEATURES #endif -extern int unused_declaration; +typedef int unused_declaration; diff --git a/testing/DHT_test.c b/testing/DHT_test.c index 8ba61ec2aeb..5f28eb99c69 100644 --- a/testing/DHT_test.c +++ b/testing/DHT_test.c @@ -17,6 +17,7 @@ #define _XOPEN_SOURCE 600 #endif +#include #include #include #include diff --git a/toxav/ring_buffer_test.cc b/toxav/ring_buffer_test.cc index 672ee2cee0e..667f90e3a04 100644 --- a/toxav/ring_buffer_test.cc +++ b/toxav/ring_buffer_test.cc @@ -1,11 +1,11 @@ #include "ring_buffer.h" +#include + #include #include #include -#include - namespace { template diff --git a/toxav/rtp_test.cc b/toxav/rtp_test.cc index b08541426a0..bd94cc9c0e9 100644 --- a/toxav/rtp_test.cc +++ b/toxav/rtp_test.cc @@ -1,9 +1,9 @@ #include "rtp.h" -#include "../toxcore/crypto_core.h" - #include +#include "../toxcore/crypto_core.h" + namespace { RTPHeader random_header() { diff --git a/toxcore/LAN_discovery.api.h b/toxcore/LAN_discovery.api.h index 5c25b67b98a..ad3d60ed7a7 100644 --- a/toxcore/LAN_discovery.api.h +++ b/toxcore/LAN_discovery.api.h @@ -21,7 +21,7 @@ namespace lan_discovery { /** * Interval in seconds between LAN discovery packet sending. */ -const INTERVAL = 10; +#define LAN_DISCOVERY_INTERVAL 10 /** * Send a LAN discovery pcaket to the broadcast address with port port. diff --git a/toxcore/LAN_discovery.h b/toxcore/LAN_discovery.h index 7e049b9961c..6b55e3193bf 100644 --- a/toxcore/LAN_discovery.h +++ b/toxcore/LAN_discovery.h @@ -26,8 +26,6 @@ typedef struct IP IP; */ #define LAN_DISCOVERY_INTERVAL 10 -uint32_t lan_discovery_interval(void); - /** * Send a LAN discovery pcaket to the broadcast address with port port. */ diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 6b691ad8455..ce8808c4418 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -1450,38 +1450,6 @@ int file_data(const Messenger *m, int32_t friendnumber, uint32_t filenumber, uin return -6; } -/* Give the number of bytes left to be sent/received. - * - * send_receive is 0 if we want the sending files, 1 if we want the receiving. - * - * return number of bytes remaining to be sent/received on success - * return 0 on failure - */ -uint64_t file_dataremaining(const Messenger *m, int32_t friendnumber, uint8_t filenumber, uint8_t send_receive) -{ - if (!friend_is_valid(m, friendnumber)) { - return 0; - } - - const struct File_Transfers *const sending = &m->friendlist[friendnumber].file_sending[filenumber]; - - if (send_receive == 0) { - if (sending->status == FILESTATUS_NONE) { - return 0; - } - - return sending->size - sending->transferred; - } - - const struct File_Transfers *const receiving = &m->friendlist[friendnumber].file_receiving[filenumber]; - - if (receiving->status == FILESTATUS_NONE) { - return 0; - } - - return receiving->size - receiving->transferred; -} - /** * Iterate over all file transfers and request chunks (from the client) for each * of them. diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 74c19e09c8f..31526b631d0 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -651,15 +651,6 @@ int file_seek(const Messenger *m, int32_t friendnumber, uint32_t filenumber, uin int file_data(const Messenger *m, int32_t friendnumber, uint32_t filenumber, uint64_t position, const uint8_t *data, uint16_t length); -/* Give the number of bytes left to be sent/received. - * - * send_receive is 0 if we want the sending files, 1 if we want the receiving. - * - * return number of bytes remaining to be sent/received on success - * return 0 on failure - */ -uint64_t file_dataremaining(const Messenger *m, int32_t friendnumber, uint8_t filenumber, uint8_t send_receive); - /** A/V related */ /* Set the callback for msi packets. diff --git a/toxcore/crypto_core.c b/toxcore/crypto_core.c index 5538b1b8eed..2fa4750ff4a 100644 --- a/toxcore/crypto_core.c +++ b/toxcore/crypto_core.c @@ -12,12 +12,13 @@ #include "config.h" #endif -#include "ccompat.h" #include "crypto_core.h" #include #include +#include "ccompat.h" + #ifndef VANILLA_NACL /* We use libsodium by default. */ #include diff --git a/toxcore/crypto_core_test.cc b/toxcore/crypto_core_test.cc index 66569506801..9f60fb837d7 100644 --- a/toxcore/crypto_core_test.cc +++ b/toxcore/crypto_core_test.cc @@ -1,10 +1,10 @@ #include "crypto_core.h" +#include + #include #include -#include - namespace { enum { diff --git a/toxcore/group.c b/toxcore/group.c index 3e04f181c88..a946e7b8e77 100644 --- a/toxcore/group.c +++ b/toxcore/group.c @@ -1301,36 +1301,6 @@ int group_set_max_frozen(const Group_Chats *g_c, uint32_t groupnumber, uint32_t return 0; } -/* List all the (frozen, if frozen is true) peers in the group chat. - * - * Copies the names of the peers to the `name[length][MAX_NAME_LENGTH]` array. - * - * Copies the lengths of the names to `lengths[length]` - * - * returns the number of peers on success. - * - * return -1 on failure. - */ -int group_names(const Group_Chats *g_c, uint32_t groupnumber, uint8_t names[][MAX_NAME_LENGTH], uint16_t lengths[], - uint16_t length, bool frozen) -{ - const Group_c *g = get_group_c(g_c, groupnumber); - - if (!g) { - return -1; - } - - const uint32_t num = frozen ? g->numfrozen : g->numpeers; - - unsigned int i; - - for (i = 0; i < num && i < length; ++i) { - lengths[i] = group_peername(g_c, groupnumber, i, names[i], frozen); - } - - return i; -} - /* Return the number of (frozen, if frozen is true) peers in the group chat on * success. * return -1 if groupnumber is invalid. diff --git a/toxcore/group.h b/toxcore/group.h index 1e054de3c66..eb8db55f104 100644 --- a/toxcore/group.h +++ b/toxcore/group.h @@ -355,19 +355,6 @@ int group_number_peers(const Group_Chats *g_c, uint32_t groupnumber, bool frozen */ int group_peernumber_is_ours(const Group_Chats *g_c, uint32_t groupnumber, uint32_t peernumber); -/* List all the (frozen, if frozen is true) peers in the group chat. - * - * Copies the names of the peers to the `name[length][MAX_NAME_LENGTH]` array. - * - * Copies the lengths of the names to `lengths[length]` - * - * returns the number of peers on success. - * - * return -1 on failure. - */ -int group_names(const Group_Chats *g_c, uint32_t groupnumber, uint8_t names[][MAX_NAME_LENGTH], uint16_t lengths[], - uint16_t length, bool frozen); - /* Set handlers for custom lossy packets. */ void group_lossy_packet_registerhandler(Group_Chats *g_c, uint8_t byte, lossy_packet_cb *function); diff --git a/toxcore/list.c b/toxcore/list.c index cb3c71cc2a5..fca8cea898c 100644 --- a/toxcore/list.c +++ b/toxcore/list.c @@ -248,13 +248,3 @@ int bs_list_remove(BS_List *list, const uint8_t *data, int id) return 1; } - -int bs_list_trim(BS_List *list) -{ - if (!resize(list, list->n)) { - return 0; - } - - list->capacity = list->n; - return 1; -} diff --git a/toxcore/list.h b/toxcore/list.h index 9d65b53fc6d..afe4240b76e 100644 --- a/toxcore/list.h +++ b/toxcore/list.h @@ -57,12 +57,4 @@ int bs_list_add(BS_List *list, const uint8_t *data, int id); */ int bs_list_remove(BS_List *list, const uint8_t *data, int id); -/* Removes the memory overhead - * - * return value: - * 1 : success - * 0 : failure - */ -int bs_list_trim(BS_List *list); - #endif diff --git a/toxcore/ping_array_test.cc b/toxcore/ping_array_test.cc index fca5009a734..09836efa211 100644 --- a/toxcore/ping_array_test.cc +++ b/toxcore/ping_array_test.cc @@ -1,8 +1,9 @@ #include "ping_array.h" +#include + #include -#include #include "mono_time.h" namespace { diff --git a/toxcore/util_test.cc b/toxcore/util_test.cc index ff57f3fb755..744c7745a3a 100644 --- a/toxcore/util_test.cc +++ b/toxcore/util_test.cc @@ -1,9 +1,9 @@ #include "util.h" -#include "crypto_core.h" - #include +#include "crypto_core.h" + namespace { TEST(Util, TwoRandomIdsAreNotEqual) { diff --git a/toxencryptsave/crypto_pwhash_scryptsalsa208sha256/sse/pwhash_scryptsalsa208sha256_sse.c b/toxencryptsave/crypto_pwhash_scryptsalsa208sha256/sse/pwhash_scryptsalsa208sha256_sse.c index 7f015238612..38a536a61a0 100644 --- a/toxencryptsave/crypto_pwhash_scryptsalsa208sha256/sse/pwhash_scryptsalsa208sha256_sse.c +++ b/toxencryptsave/crypto_pwhash_scryptsalsa208sha256/sse/pwhash_scryptsalsa208sha256_sse.c @@ -398,4 +398,4 @@ escrypt_kdf_sse(escrypt_local_t * local, #endif /* ISO C requires a translation unit to contain at least one declaration */ -extern int non_empty_tu_decl; +typedef int non_empty_tu_decl;