Skip to content

Commit

Permalink
Add a check that we don't have any unused functions.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
iphydf committed May 2, 2020
1 parent 2570ddc commit f8ab32a
Show file tree
Hide file tree
Showing 14 changed files with 28 additions and 123 deletions.
35 changes: 21 additions & 14 deletions other/analysis/gen-file.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ put() {
if [ "$SKIP_LINES" = "" ]; then
echo "#line 1 \"$1\"" >> amalgamation.cc
fi
cat $1 >> amalgamation.cc
cat "$1" >> amalgamation.cc
}

putmain() {
echo "namespace $(echo $1 | sed -e 's/[^a-zA-Z0-9_]/_/g') {" >> amalgamation.cc
echo "namespace $(echo "$1" | sed -e 's/[^a-zA-Z0-9_]/_/g') {" >> amalgamation.cc
if [ "$SKIP_LINES" = "" ]; then
echo "#line 1 \"$1\"" >> amalgamation.cc
fi
sed -e 's/^int main(/static &/' $1 >> amalgamation.cc
echo "} // namespace $(echo $1 | sed -e 's/[^a-zA-Z0-9_]/_/g')" >> amalgamation.cc
sed -e 's/^int main(/static &/' "$1" >> amalgamation.cc
echo "} // namespace $(echo "$1" | sed -e 's/[^a-zA-Z0-9_]/_/g')" >> amalgamation.cc
}

callmain() {
echo " call($(echo $1 | sed -e 's/[^a-zA-Z0-9_]/_/g')::main, argc, argv);" >> amalgamation.cc
echo " call($(echo "$1" | sed -e 's/[^a-zA-Z0-9_]/_/g')::main, argc, argv);" >> amalgamation.cc
}

:> amalgamation.cc
Expand All @@ -53,25 +53,32 @@ 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
if ! grep -q '^int main(' $i; then
put $i
(for i in $(eval "$FIND_QUERY"); do
grep -o '#include <[^>]*>' "$i" \
| grep -E -v '<win|<ws|<iphlp|<libc|<mach/|<crypto_|<randombytes|<u.h>|<sys/filio|<linux'
done) | sort -u >> amalgamation.cc

echo 'namespace {' >> amalgamation.cc
for i in $(eval "$FIND_QUERY"); do
if ! grep -q '^int main(' "$i"; then
put "$i"
fi
done

for i in $(eval $FIND_QUERY); do
if grep -q '^int main(' $i; then
putmain $i
for i in $(eval "$FIND_QUERY"); do
if grep -q '^int main(' "$i"; then
putmain "$i"
fi
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
if grep -q '^int main(' $i; then
callmain $i
for i in $(eval "$FIND_QUERY"); do
if grep -q '^int main(' "$i"; then
callmain "$i"
fi
done
echo " return 0;" >> amalgamation.cc
Expand Down
2 changes: 1 addition & 1 deletion other/bootstrap_daemon/docker/tox-bootstrapd.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
e7b6d31485b5e1561659be08f3e3ef003cef186042d7e0fe2ef48cf341932b5a /usr/local/bin/tox-bootstrapd
89df21d6a19a1b6652db5b7e6e9f65ad5f8be8abdca9f58645548b9e0c9383e3 /usr/local/bin/tox-bootstrapd
2 changes: 1 addition & 1 deletion other/cpufeatures.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
#include ANDROID_CPU_FEATURES
#endif

extern int unused_declaration;
typedef int unused_declaration;
1 change: 1 addition & 0 deletions testing/DHT_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define _XOPEN_SOURCE 600
#endif

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
Expand Down
2 changes: 1 addition & 1 deletion toxcore/LAN_discovery.api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 0 additions & 2 deletions toxcore/LAN_discovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
32 changes: 0 additions & 32 deletions toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 0 additions & 9 deletions toxcore/Messenger.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion toxcore/crypto_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
#include "config.h"
#endif

#include "crypto_core.h"

#include <stdlib.h>
#include <string.h>

#include "ccompat.h"
#include "crypto_core.h"

#ifndef VANILLA_NACL
/* We use libsodium by default. */
Expand Down
30 changes: 0 additions & 30 deletions toxcore/group.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 0 additions & 13 deletions toxcore/group.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
10 changes: 0 additions & 10 deletions toxcore/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
8 changes: 0 additions & 8 deletions toxcore/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit f8ab32a

Please sign in to comment.