[PATCH] D128449: [clang] Introduce -Warray-parameter

Aaron Ballman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 23 10:34:58 PDT 2022

aaron.ballman added a comment.

Thanks for working on this!

Comment at: clang/lib/Sema/SemaDecl.cpp:49
 #include "llvm/ADT/Triple.h"
+#include "llvm/Support/raw_ostream.h"
 #include <algorithm>
Is this new include necessary?

Comment at: clang/lib/Sema/SemaDecl.cpp:3251-3254
+  const DecayedType *OldParamDT =
+      dyn_cast<const DecayedType>(OldParam->getType());
+  const DecayedType *NewParamDT =
+      dyn_cast<const DecayedType>(NewParam->getType());

Comment at: clang/test/Misc/warning-wall.c:3
-     CHECK:-Wall
-CHECK-NEXT:  -Wmost
-CHECK-NEXT:    -Wbool-operation
-CHECK-NEXT:    -Wbitwise-instead-of-logical
-CHECK-NEXT:    -Wchar-subscripts
-CHECK-NEXT:    -Wcomment
-CHECK-NEXT:    -Wdelete-non-virtual-dtor
-CHECK-NEXT:      -Wdelete-non-abstract-non-virtual-dtor
-CHECK-NEXT:      -Wdelete-abstract-non-virtual-dtor
-CHECK-NEXT:    -Wformat
-CHECK-NEXT:      -Wformat-extra-args
-CHECK-NEXT:      -Wformat-zero-length
-CHECK-NEXT:      -Wnonnull
-CHECK-NEXT:      -Wformat-security
-CHECK-NEXT:      -Wformat-y2k
-CHECK-NEXT:      -Wformat-invalid-specifier
-CHECK-NEXT:      -Wformat-insufficient-args
-CHECK-NEXT:    -Wfor-loop-analysis
-CHECK-NEXT:    -Wframe-address
-CHECK-NEXT:    -Wimplicit
-CHECK-NEXT:      -Wimplicit-function-declaration
-CHECK-NEXT:      -Wimplicit-int
-CHECK-NEXT:    -Winfinite-recursion
-CHECK-NEXT:    -Wint-in-bool-context
-CHECK-NEXT:    -Wmismatched-tags
-CHECK-NEXT:    -Wmissing-braces
-CHECK-NEXT:    -Wmove
-CHECK-NEXT:      -Wpessimizing-move
-CHECK-NEXT:      -Wredundant-move
-CHECK-NEXT:      -Wreturn-std-move
-CHECK-NEXT:      -Wself-move
-CHECK-NEXT:    -Wmultichar
-CHECK-NEXT:    -Wrange-loop-construct
-CHECK-NEXT:    -Wreorder
-CHECK-NEXT:      -Wreorder-ctor
-CHECK-NEXT:      -Wreorder-init-list
-CHECK-NEXT:    -Wreturn-type
-CHECK-NEXT:      -Wreturn-type-c-linkage
-CHECK-NEXT:    -Wself-assign
-CHECK-NEXT:      -Wself-assign-overloaded
-CHECK-NEXT:      -Wself-assign-field
-CHECK-NEXT:    -Wself-move
-CHECK-NEXT:    -Wsizeof-array-argument
-CHECK-NEXT:    -Wsizeof-array-decay
-CHECK-NEXT:    -Wstring-plus-int
-CHECK-NEXT:    -Wtautological-compare
-CHECK-NEXT:      -Wtautological-constant-compare
-CHECK-NEXT:        -Wtautological-constant-out-of-range-compare
-CHECK-NEXT:      -Wtautological-pointer-compare
-CHECK-NEXT:      -Wtautological-overlap-compare
-CHECK-NEXT:      -Wtautological-bitwise-compare
-CHECK-NEXT:      -Wtautological-undefined-compare
-CHECK-NEXT:      -Wtautological-objc-bool-compare
-CHECK-NEXT:    -Wtrigraphs
-CHECK-NEXT:    -Wuninitialized
-CHECK-NEXT:      -Wsometimes-uninitialized
-CHECK-NEXT:      -Wstatic-self-init
-CHECK-NEXT:      -Wuninitialized-const-reference
-CHECK-NEXT:    -Wunknown-pragmas
-CHECK-NEXT:    -Wunused
-CHECK-NEXT:      -Wunused-argument
-CHECK-NEXT:      -Wunused-function
-CHECK-NEXT:        -Wunneeded-internal-declaration
-CHECK-NEXT:      -Wunused-label
-CHECK-NEXT:      -Wunused-private-field
-CHECK-NEXT:      -Wunused-lambda-capture
-CHECK-NEXT:      -Wunused-local-typedef
-CHECK-NEXT:      -Wunused-value
-CHECK-NEXT:        -Wunused-comparison
-CHECK-NEXT:        -Wunused-result
-CHECK-NEXT:        -Wunevaluated-expression
-CHECK-NEXT:          -Wpotentially-evaluated-expression
-CHECK-NEXT:      -Wunused-variable
-CHECK-NEXT:        -Wunused-const-variable
-CHECK-NEXT:      -Wunused-but-set-variable
-CHECK-NEXT:      -Wunused-property-ivar
-CHECK-NEXT:    -Wvolatile-register-var
-CHECK-NEXT:    -Wobjc-missing-super-calls
-CHECK-NEXT:    -Wobjc-designated-initializers
-CHECK-NEXT:    -Wobjc-flexible-array
-CHECK-NEXT:    -Woverloaded-virtual
-CHECK-NEXT:    -Wprivate-extern
-CHECK-NEXT:    -Wcast-of-sel-type
-CHECK-NEXT:    -Wextern-c-compat
-CHECK-NEXT:    -Wuser-defined-warnings
-CHECK-NEXT:  -Wparentheses
-CHECK-NEXT:    -Wlogical-op-parentheses
-CHECK-NEXT:    -Wlogical-not-parentheses
-CHECK-NEXT:    -Wbitwise-conditional-parentheses
-CHECK-NEXT:    -Wbitwise-op-parentheses
-CHECK-NEXT:    -Wshift-op-parentheses
-CHECK-NEXT:    -Woverloaded-shift-op-parentheses
-CHECK-NEXT:    -Wparentheses-equality
-CHECK-NEXT:    -Wdangling-else
-CHECK-NEXT:  -Wswitch
-CHECK-NEXT:  -Wswitch-bool
-CHECK-NEXT:  -Wmisleading-indentation
+                                                                                    CHECK : -Wall CHECK -
+                                                                          NEXT : -Wmost
Something went sideways here (perhaps saved with the wrong line endings?).

Comment at: clang/test/Sema/array-parameter.c:2
+// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -fsyntax-only -Warray-parameter -verify %s
+void f0(int a[]);
I'd like to see some additional test cases:
void func(int *p);
void func(int a[2]); // Diagnose this one, I presume?
void func(int a[]); // But also diagnose this one as well, yes?
void func(int a[2]) {} // Do we then diagnose this as well, or is this matching a previous declaration and thus fine?

void other(int n, int m, int a[n]);
void other(int n, int m, int a[m]); // Hopefully we diagnose this set!

void another(int n, int array[n]);
void another(int n, int array[*]); // I *think* this should be warned, but I'm still a bit on the fence about it

Also, if this is expected to work in C++ as well, we should probably have cases like:
template <int N>
void func(int i[10]);

template <int N>
void func(int i[N]); // Should we diagnose this before instantiation or wait until we see the instantiation and only diagnose if differs?

static constexpr int Extent = 10;
void other(int i[10]);
void other(int i[Extent]); // Should not be diagnosed

Comment at: clang/test/Sema/array-parameter.c:12-19
+void f3(int a[const 2]); // expected-note {{previously declared as int[const 2] here}}
+void f3(int a[2]);       // expected-warning {{argument 'a' of type 'int[2]' with mismatched bound}}
+void f4(int a[static 2]); // expected-note {{previously declared as int[static 2] here}}
+void f4(int a[2]);        // expected-warning {{argument 'a' of type 'int[2]' with mismatched bound}}
+void f5(int a[restrict 2]); // expected-note {{previously declared as int[__restrict 2] here}}
I don't think we should diagnose any of these -- the array bounds do match. GCC doesn't diagnose them either.

Comment at: clang/test/Sema/array-parameter.c:21-22
+void f6(int a[*]); // expected-note {{previously declared as int[*] here}}
+void f6(int a[]);  // expected-warning {{argument 'a' of type 'int[]' with mismatched bound}}
I don't think we should diagnose this case either -- the `[*]` case is denoting a parameter of variable-length array type with no size information, and `[]` is the same except not a variable-length array type. So it's hard to argue that the bounds are not matched properly.



More information about the cfe-commits mailing list