Skip to content

Commit a33eee1

Browse files
committed
Made array index warnings conditional on -Warray-index
1 parent 325447c commit a33eee1

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

cmdline.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ static FlagTable warnflag[] = {
616616
{ "hide-members", WARN_HIDE_MEMBERS },
617617
{ "init-vars", WARN_UNINIT_VARS },
618618
{ "language-extensions", WARN_LANG_EXTENSIONS },
619-
619+
{ "array-index", WARN_ARRAY_INDEX },
620620
{ "all", WARN_ALL },
621621
};
622622

doc/general.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,13 +253,17 @@ function for "warn(!init-vars)" myfunc()
253253

254254
Multiple warnings may be given, separated by commas. To turn a warning off, prefix it with `!` or with `~`. To enable all warnings, use the word `all`.
255255

256-
Thus, a Spin function with `{++opt(!all,hide-members)}` will always be compiled with no warnings except for uninitialzed variables.
256+
Thus, a Spin function with `{++opt(!all,hide-members)}` will always be compiled with no warnings except for uninitialized variables.
257257

258258
### Warning control on the command line
259259

260260
Multiple `-W` options may be given, or combined separated by commas. So for example to compile with all warnings except uninitialized variables, one would give `-Wall,!init-vars`.
261261

262262

263+
### Array index warnings (-Warray-index) (enabled by default)
264+
265+
Warns when a constant array index is out of range. Note that this only applies to explicit constants; the warning will not be issued for computed indices, even when the computation is "obvious".
266+
263267
### Assembler usage warnings (-Wasm-usage) (enabled by default)
264268

265269
Warns about some common issues in assembly code, for example forgetting to put `wc` or `wz` on a `cmp` instruction. This option is only available on the command line, it is generally ignored on a per-function basis (even for functions with inline assembly) because assembly parsing is handled specially.

frontends/common.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,11 @@ extern int gl_warn_flags; /* flags for warnings */
213213
#define WARN_ASM_USAGE 0x000004
214214
#define WARN_UNINIT_VARS 0x000008
215215
#define WARN_C_CONST_STRING 0x000010
216+
#define WARN_ARRAY_INDEX 0x000020
216217
#define WARN_ASM_FIRST_PASS 0x800000
217218
#define WARN_ALL 0xFFFFFF
218219

219-
#define DEFAULT_WARN_FLAGS (WARN_ASM_USAGE | WARN_UNINIT_VARS | WARN_ASM_FIRST_PASS)
220+
#define DEFAULT_WARN_FLAGS (WARN_ASM_USAGE | WARN_UNINIT_VARS | WARN_ASM_FIRST_PASS | WARN_ARRAY_INDEX)
220221

221222
extern int gl_list_options; /* options for listing files */
222223
#define LIST_INCLUDE_CONSTANTS 0x0001

frontends/types.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1971,16 +1971,18 @@ static AST *doCheckTypes(AST *ast)
19711971
if (IsConstExpr(ast->right)) {
19721972
int idx = EvalConstExpr(ast->right);
19731973
int low = 0;
1974+
unsigned warn_flags = curfunc ? curfunc->warn_flags : gl_warn_flags;
19741975
if (!base || IsConstExpr(base)) {
19751976
low = base ? EvalConstExpr(base) : 0;
1976-
if (idx < low) {
1977+
if (idx < low && 0 != (warn_flags & WARN_ARRAY_INDEX)) {
19771978
WARNING(ast, "Array index %d is less than array start (%d)", idx, low);
19781979
}
19791980
}
19801981
AST *aSize = GetArraySize(lefttype);
19811982
if (IsConstExpr(aSize)) {
1982-
int high = EvalConstExpr(aSize) + low - 1;
1983-
if (idx > high) {
1983+
int len = EvalConstExpr(aSize);
1984+
int high = len + low - 1;
1985+
if (idx > high && len > 0 && 0 != (warn_flags & WARN_ARRAY_INDEX)) {
19841986
WARNING(ast, "Array index %d is above end of array (%d)", idx, high);
19851987
}
19861988
}

0 commit comments

Comments
 (0)