llvm-project
llvm-project copied to clipboard
clang: Incorrect warning using -Warray-parameter
First: Thank you so much for implementing these warnings! This is highly appreciated!
Here is a minor nit: Clang warns for parameters declared using [ * ]
when followed by
another declaration using [3]
or [n]
or similar, but this is not correct. [ * ]
is used to declare
an array of unspecified size. As such it is consistent with a later declaration that
specifies a bound. Calling this a mismatch is misleading.
Also a definition can never use [ * ]
, so it is then not possible to
use [ * ]
in a declaration without getting this warning at the place of a definition.
The intended use of [ * ]
looks like this:
extern void foo(int n, int a[*]); // unspecified
void foo(int n, int a[n]) { ... } // should not warn!
A use case is where the bound depends on information which may not be available at the point of the declaration, so one wants to leave it unspecified at this point:
// header 1
extern void process_geometry(int n, float (*element)[ n ][ * ][ * ]);
// header 2
enum { DIMENSIONS = 2 }; // change to 3 for 3D simulation
// impl 1
void process_geometry(int n, float (*element)[n][DIMENSION][DIMENSIONS]) { ... }
// use
#include "header1"
#include "header2"
...
float (*elements)[n][DIMENSIONS][DIMENSIONS] = malloc(sizeof *elemnts)
process_geometry(n, elements);
Of course, this is rather obscure, but there is also no good reason to break this. (GCC also gets this wrong).
The corresponding GCC report is in PR #98536. As explained there, the warning is intentional.
I do not think there exists any evidence that this feature is abused in a dangerous way.