Created
December 27, 2018 09:54
-
-
Save mingodad/dd0524b17b16fc68b1ec249853439e35 to your computer and use it in GitHub Desktop.
Add compiler warning/error for duplicate variable declaration in LuaJIT 2.0.5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- lj_parse.c | |
+++ lj_parse.c | |
@@ -25,6 +25,8 @@ | |
#include "lj_parse.h" | |
#include "lj_vm.h" | |
#include "lj_vmevent.h" | |
+ | |
+#include <stdio.h> | |
/* -- Parser structures and definitions ----------------------------------- */ | |
@@ -162,6 +164,12 @@ | |
LJ_STATIC_ASSERT((int)BC_MODVV-(int)BC_ADDVV == (int)OPR_MOD-(int)OPR_ADD); | |
/* -- Error handling ------------------------------------------------------ */ | |
+ | |
+static void parser_warning (LexState *ls, const char *msg) { | |
+ const char *str = strdata(ls->chunkname); | |
+ fprintf(stderr, "%s:%d: warning %s\n", (str[0] == '@') ? str+1 : str , ls->linenumber, msg); | |
+ fflush(stderr); | |
+} | |
LJ_NORET LJ_NOINLINE static void err_syntax(LexState *ls, ErrMsg em) | |
{ | |
@@ -1032,7 +1040,27 @@ | |
{ | |
FuncState *fs = ls->fs; | |
MSize vtop = ls->vtop; | |
- checklimit(fs, fs->nactvar+n, LJ_MAX_LOCVAR, "local variables"); | |
+ BCReg i, nactvar_n = fs->nactvar+n; | |
+ | |
+ if(name && name > ((GCstr*)0xffff)) { /*some names are made from casting ints */ | |
+ /* allow '_' and '(for...' duplicates */ | |
+ const char *str_name = strdata(name); | |
+ if(!(str_name[0] == '(' || (name->len == 1 && str_name[0] == '_'))) { | |
+ for (i=0; i < nactvar_n; ++i) { | |
+ if (name == strref(var_get(ls, fs, i).name)) { | |
+ if(fs->bl && i < fs->bl->nactvar) { | |
+ parser_warning(ls, lj_str_pushf(ls->L, | |
+ "Name [%s] already declared will be shadowed", str_name)); | |
+ } | |
+ else { | |
+ lj_lex_error(ls, 0, LJ_ERR_XNAMEDUP, str_name); | |
+ } | |
+ } | |
+ } | |
+ } | |
+ } | |
+ | |
+ checklimit(fs, nactvar_n, LJ_MAX_LOCVAR, "local variables"); | |
if (LJ_UNLIKELY(vtop >= ls->sizevstack)) { | |
if (ls->sizevstack >= LJ_MAX_VSTACK) | |
lj_lex_error(ls, 0, LJ_ERR_XLIMC, LJ_MAX_VSTACK); | |
--- lj_errmsg.h | |
+++ lj_errmsg.h | |
@@ -148,6 +148,7 @@ | |
ERRDEF(XLUNDEF, "undefined label " LUA_QS) | |
ERRDEF(XLDUP, "duplicate label " LUA_QS) | |
ERRDEF(XGSCOPE, "<goto %s> jumps into the scope of local " LUA_QS) | |
+ERRDEF(XNAMEDUP, "duplicate name " LUA_QS) | |
/* Bytecode reader errors. */ | |
ERRDEF(BCFMT, "cannot load incompatible bytecode") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment