Skip to content

Commit b4b1481

Browse files
committed
Fix all GCC/Clang warnings, consistent variable naming, more cleaning
1 parent 5e3e8ba commit b4b1481

8 files changed

Lines changed: 202 additions & 196 deletions

File tree

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ define forWindows
3232
luaflags += -DLUA_USE_WINDOWS
3333
endef
3434

35-
cflags = $(luaflags) -DPDLUA_VERSION="$(pdlua_version)"
35+
# stbi and nanosvg have functions we don't use
36+
suppress-wunused=1
37+
38+
cflags = $(luaflags) -DPDLUA_VERSION="$(pdlua_version)" -Iluas/luajit/src
3639
ifdef PD_MULTICHANNEL
3740
cflags += -DPD_MULTICHANNEL=$(PD_MULTICHANNEL)
3841
endif

luas/lua.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,8 @@
192192
// Fix unistd namespace clash
193193
#define getmode lua55_getmode
194194

195+
// Lua's source will trigger a gcc warning because it hits the maximum inlining limit
196+
// This is normal and expected, so silence it
197+
#pragma GCC diagnostic ignored "-Winline"
198+
195199
#include "lua/onelua.c"

luas/luajit.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/** @file lua_55.c
2-
* @brief lua_55 -- A wrapper around pdlua.c to allow building against the shipped LuaJIT 2.1 + compat53 submodule
1+
/** @file luajit.c
2+
* @brief luajit.c -- A wrapper around pdlua.c to allow building against the shipped LuaJIT 2.1 + compat53 submodule
33
* @author Timothy Schoen
44
* @date 2026
55
*

pdlua.c

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ static t_symbol* global_gensym(const char* s)
216216
// So we pass a data directory to the setup function instead and store it here.
217217
// ag: Renamed to pdlua_datadir since we also need this in vanilla when
218218
// setting up Lua's package.path.
219-
char pdlua_datadir[MAXPDSTRING];
219+
char pdlua_datadir[PATH_MAX+1];
220220
#if PLUGDATA
221221
// Hook to inform plugdata which class names are lua objects
222222
void(*plugdata_register_class)(const char*);
@@ -675,7 +675,7 @@ static char *src_info(lua_State *L, char *msg)
675675
// cf. "The Debug Interface" in the Lua reference manual
676676
if (*src == '@') src = basename(src+1);
677677
if (strcmp(ar.what, "Lua") == 0 && strcmp(src, "pd.lua") != 0) {
678-
snprintf(msg, MAXPDSTRING-1, "%s: %d", src, ar.linedefined);
678+
snprintf(msg, MAXPDSTRING, "%s: %d", src, ar.linedefined);
679679
return msg;
680680
}
681681
}
@@ -835,7 +835,7 @@ static t_pdlua *pdlua_new
835835
if (object->has_gui) {
836836
t_canvas *parent_canvas = glist_getcanvas(object->canvas);
837837
char buf[MAXPDSTRING];
838-
snprintf(buf, MAXPDSTRING-1, ".x%lx", (unsigned long)parent_canvas);
838+
snprintf(buf, MAXPDSTRING, ".x%lx", (unsigned long)parent_canvas);
839839
object->gfx.proxycanvas = pdlua_proxycanvas_new(object, gensym(buf));
840840
if (!object->gfx.proxycanvas) {
841841
pd_error(NULL, "pdlua: failed to create canvas proxy");
@@ -1155,7 +1155,7 @@ static void pdlua_menu_open(t_pdlua *o)
11551155
{
11561156
const char *name;
11571157
const char *path;
1158-
char pathname[FILENAME_MAX];
1158+
char pathname[FILENAME_MAX+1];
11591159
t_class *class;
11601160

11611161
/* 20240903 ag: This is surpringly complicated, because there are various
@@ -1192,11 +1192,11 @@ static void pdlua_menu_open(t_pdlua *o)
11921192
path = class->c_externdir->s_name;
11931193
if (sys_isabsolutepath(name)) {
11941194
// pdluax returns an absolute path for its script, just use that.
1195-
snprintf(pathname, FILENAME_MAX-1, "%s", name);
1195+
snprintf(pathname, FILENAME_MAX+1, "%s", name);
11961196
} else if (sys_isabsolutepath(path)) {
11971197
// If the externdir is an absolute path, just use it, no questions
11981198
// asked. This should cover most cases.
1199-
snprintf(pathname, FILENAME_MAX-1, "%s/%s", path, name);
1199+
snprintf(pathname, FILENAME_MAX+1, "%s/%s", path, name);
12001200
} else {
12011201
// Normally, the externdir of an object should be absolute, but if
12021202
// it isn't, it should be relative to the cwd we recorded at
@@ -1208,7 +1208,7 @@ static void pdlua_menu_open(t_pdlua *o)
12081208
snprintf(s, PATH_MAX, "%s/%s", pdlua_cwd, name);
12091209
// canonicalize
12101210
if (realpath(s, real_path)) s = real_path;
1211-
snprintf(pathname, FILENAME_MAX-1, "%s", s);
1211+
snprintf(pathname, FILENAME_MAX+1, "%s", s);
12121212
}
12131213
//post("path = %s, name = %s, pathname = %s", path, name, pathname);
12141214
lua_pop(__L(), 2); /* pop name, global "pd"*/
@@ -1536,7 +1536,7 @@ static int pdlua_class_new(lua_State *L)
15361536
return 0;
15371537
}
15381538

1539-
snprintf(name_gfx, MAXPDSTRING-1, "%s:gfx", name);
1539+
snprintf(name_gfx, MAXPDSTRING, "%s:gfx", name);
15401540
PDLUA_DEBUG3("pdlua_class_new: L is %p, name is %s stack top is %d", L, name, lua_gettop(L));
15411541
#pragma GCC diagnostic push
15421542
#pragma GCC diagnostic ignored "-Wcast-function-type"
@@ -2616,15 +2616,16 @@ static void pdlua_packagepath(lua_State *L, const char *path)
26162616
lua_pushstring(L, "path");
26172617
lua_gettable(L, -2);
26182618
const char *packagepath = lua_tostring(L, -1);
2619-
char *buf = malloc(2*strlen(path)+20+strlen(packagepath));
2619+
int bufsize = 2*strlen(path)+20+strlen(packagepath);
2620+
char *buf = malloc(bufsize);
26202621
if (!buf) {
26212622
lua_pop(L, 2);
26222623
return;
26232624
}
26242625
#ifdef _WIN32
2625-
sprintf(buf, "%s\\?.lua;%s\\?\\init.lua;%s", path, path, packagepath);
2626+
snprintf(buf, bufsize, "%s\\?.lua;%s\\?\\init.lua;%s", path, path, packagepath);
26262627
#else
2627-
sprintf(buf, "%s/?.lua;%s/?/init.lua;%s", path, path, packagepath);
2628+
snprintf(buf, bufsize, "%s/?.lua;%s/?/init.lua;%s", path, path, packagepath);
26282629
#endif
26292630
lua_pop(L, 1);
26302631
lua_pushstring(L, "path");
@@ -2639,9 +2640,9 @@ static void pdlua_packagepath(lua_State *L, const char *path)
26392640
return;
26402641
}
26412642
#ifdef _WIN32
2642-
sprintf(buf, "%s\\?.dll;%s", path, packagepath);
2643+
snprintf(buf, bufsize, "%s\\?.dll;%s", path, packagepath);
26432644
#else
2644-
sprintf(buf, "%s/?.so;%s", path, packagepath);
2645+
snprintf(buf, bufsize, "%s/?.so;%s", path, packagepath);
26452646
#endif
26462647
lua_pop(L, 1);
26472648
lua_pushstring(L, "cpath");
@@ -3051,7 +3052,7 @@ static int pdlua_loader_fromfd
30513052
reader.fd = fd;
30523053
// we want to have the filename with extension as the name of the chunk
30533054
char filename[MAXPDSTRING];
3054-
snprintf(filename, MAXPDSTRING-1, "%s.pd_lua", name);
3055+
snprintf(filename, MAXPDSTRING, "%s.pd_lua", name);
30553056
#if LUA_VERSION_NUM < 502
30563057
if (lua_load(__L(), pdlua_reader, &reader, filename) || lua_pcall(__L(), 0, 0, 0))
30573058
#else // 5.2 style
@@ -3185,7 +3186,7 @@ static int pdlua_loader_pathwise
31853186

31863187
static int init_pdlua_environment(lua_State* L, const char* datadir)
31873188
{
3188-
char pd_lua_path[MAXPDSTRING];
3189+
char pd_lua_path[PATH_MAX+8];
31893190
t_pdlua_readerdata reader;
31903191
int fd, result;
31913192

@@ -3199,7 +3200,7 @@ static int init_pdlua_environment(lua_State* L, const char* datadir)
31993200
preload_compat53(L);
32003201
#endif
32013202

3202-
snprintf(pd_lua_path, MAXPDSTRING-1, "%s/pd.lua", datadir);
3203+
snprintf(pd_lua_path, PATH_MAX+8, "%s/pd.lua", datadir);
32033204
PDLUA_DEBUG("pd_lua_path %s", pd_lua_path);
32043205

32053206
fd = open(pd_lua_path, O_RDONLY);
@@ -3306,8 +3307,8 @@ void pdlua_setup(void)
33063307
char pdluaver[MAXPDSTRING];
33073308
char compiled[MAXPDSTRING];
33083309

3309-
snprintf(pdluaver, MAXPDSTRING-1, "pdlua %s (GPL) 2008 Claude Heiland-Allen, 2014 Martin Peach et al.", pdlua_version);
3310-
snprintf(compiled, MAXPDSTRING-1, "pdlua: compiled for pd-%d.%d on %s",
3310+
snprintf(pdluaver, MAXPDSTRING, "pdlua %s (GPL) 2008 Claude Heiland-Allen, 2014 Martin Peach et al.", pdlua_version);
3311+
snprintf(compiled, MAXPDSTRING, "pdlua: compiled for pd-%d.%d on %s",
33113312
PD_MAJOR_VERSION, PD_MINOR_VERSION, BUILD_DATE);
33123313
// post version and other information
33133314
post(pdluaver);
@@ -3321,9 +3322,9 @@ void pdlua_setup(void)
33213322
lvm = (*luaversion)/100;
33223323
lvl = (*luaversion) - (100*lvm);
33233324
#ifdef LUA_USE_JIT
3324-
snprintf(luaversionStr, MAXPDSTRING-1, "Using luajit with lua version %d.%d", lvm, lvl);
3325+
snprintf(luaversionStr, MAXPDSTRING, "Using luajit with lua version %d.%d", lvm, lvl);
33253326
#else
3326-
snprintf(luaversionStr, MAXPDSTRING-1, "Using lua version %d.%d", lvm, lvl);
3327+
snprintf(luaversionStr, MAXPDSTRING, "Using lua version %d.%d", lvm, lvl);
33273328
#endif
33283329

33293330
#if PLUGDATA
@@ -3382,15 +3383,15 @@ void pdlua_setup(void)
33823383
// In plugdata we're linked statically and thus c_externdir is empty.
33833384
// Instead, we get our data directory from plugdata and expect to find the
33843385
// external dir in <datadir>/pdlua.
3385-
snprintf(pdlua_datadir, MAXPDSTRING-1, "%s/pdlua", datadir);
3386+
snprintf(pdlua_datadir, PATH_MAX+1, "%s/pdlua", datadir);
33863387
#else
33873388
const char *s = pdlua_proxyinlet_class->c_externdir->s_name;
33883389
if (!sys_isabsolutepath(s)) {
33893390
// try to turn this into an absolute path
33903391
char real_path[PATH_MAX+1];
33913392
if (realpath(s, real_path)) s = real_path;
33923393
}
3393-
snprintf(pdlua_datadir, MAXPDSTRING-1, "%s", s);
3394+
snprintf(pdlua_datadir, PATH_MAX+1, "%s", s);
33943395
#endif
33953396
if (!getcwd(pdlua_cwd, MAXPDSTRING))
33963397
// if we can't get the cwd, this is the best that we can do

pdlua_gfx.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,12 @@
2323

2424
#if !defined(PLUGDATA) && !defined(PURR_DATA)
2525

26-
#pragma GCC diagnostic push
27-
#pragma GCC diagnostic ignored "-Wunused-function"
2826
#define NANOSVG_IMPLEMENTATION
2927
#include "svg/nanosvg.h"
3028
#define NANOSVGRAST_IMPLEMENTATION
3129
#include "svg/nanosvgrast.h"
32-
#define STB_IMAGE_STATIC
3330
#define STBI_NO_THREAD_LOCALS
31+
#define STB_IMAGE_STATIC
3432
#define STB_IMAGE_IMPLEMENTATION
3533
#include "svg/stb_image.h"
3634
#define STB_IMAGE_WRITE_STATIC
@@ -39,7 +37,6 @@
3937
#define STB_IMAGE_RESIZE_STATIC
4038
#define STB_IMAGE_RESIZE_IMPLEMENTATION
4139
#include "svg/stb_image_resize2.h"
42-
#pragma GCC diagnostic pop
4340
#endif
4441

4542
#ifdef PURR_DATA

0 commit comments

Comments
 (0)