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 9be4dbb commit 69f39b5
Show file tree
Hide file tree
Showing 18 changed files with 23 additions and 117 deletions.
7 changes: 7 additions & 0 deletions other/analysis/gen-file.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<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
Expand All @@ -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
Expand Down
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
4 changes: 2 additions & 2 deletions toxav/ring_buffer_test.cc
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include "ring_buffer.h"

#include <gtest/gtest.h>

#include <algorithm>
#include <cassert>
#include <vector>

#include <gtest/gtest.h>

namespace {

template <typename T>
Expand Down
4 changes: 2 additions & 2 deletions toxav/rtp_test.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "rtp.h"

#include "../toxcore/crypto_core.h"

#include <gtest/gtest.h>

#include "../toxcore/crypto_core.h"

namespace {

RTPHeader random_header() {
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,12 +12,13 @@
#include "config.h"
#endif

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

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

#include "ccompat.h"

#ifndef VANILLA_NACL
/* We use libsodium by default. */
#include <sodium.h>
Expand Down
4 changes: 2 additions & 2 deletions toxcore/crypto_core_test.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "crypto_core.h"

#include <gtest/gtest.h>

#include <algorithm>
#include <vector>

#include <gtest/gtest.h>

namespace {

enum {
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
3 changes: 2 additions & 1 deletion toxcore/ping_array_test.cc
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include "ping_array.h"

#include <gtest/gtest.h>

#include <memory>

#include <gtest/gtest.h>
#include "mono_time.h"

namespace {
Expand Down
4 changes: 2 additions & 2 deletions toxcore/util_test.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "util.h"

#include "crypto_core.h"

#include <gtest/gtest.h>

#include "crypto_core.h"

namespace {

TEST(Util, TwoRandomIdsAreNotEqual) {
Expand Down
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 69f39b5

Please sign in to comment.