inclyc created this revision. inclyc added a reviewer: aaron.ballman. Herald added a project: All. inclyc added reviewers: rsmith, clang-language-wg. inclyc published this revision for review. Herald added a project: clang. Herald added a subscriber: cfe-commits.
Fixes: https://github.com/llvm/llvm-project/issues/57098 Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D132952 Files: clang/include/clang/Sema/Sema.h clang/lib/Sema/SemaType.cpp clang/test/Sema/warn-vla.c Index: clang/test/Sema/warn-vla.c =================================================================== --- clang/test/Sema/warn-vla.c +++ clang/test/Sema/warn-vla.c @@ -5,8 +5,18 @@ int v[n]; // expected-warning {{variable length array}} } -void test2(int n, int v[n]) { // expected-warning {{variable length array}} +void test2(int n, int v[n]) { // c99 no-warning +#if __STDC_VERSION__ < 199901L +// expected-warning@-2{{variable length arrays are a C99 feature}} +#endif } -void test3(int n, int v[n]); // expected-warning {{variable length array}} +void test3(int n, int v[n]); // c99 no-warning +#if __STDC_VERSION__ < 199901L +// expected-warning@-2{{variable length arrays are a C99 feature}} +#endif +void test4(int n, int v[][*]); // c99 no-warning +#if __STDC_VERSION__ < 199901L +// expected-warning@-2{{variable length arrays are a C99 feature}} +#endif Index: clang/lib/Sema/SemaType.cpp =================================================================== --- clang/lib/Sema/SemaType.cpp +++ clang/lib/Sema/SemaType.cpp @@ -2367,9 +2367,11 @@ unsigned VLADiag; bool VLAIsError; bool IsVLA = false; + bool SuppressNotICEVLA = false; - VLADiagnoser(unsigned VLADiag, bool VLAIsError) - : VLADiag(VLADiag), VLAIsError(VLAIsError) {} + VLADiagnoser(unsigned VLADiag, bool VLAIsError, bool SuppressNotICEVLA) + : VLADiag(VLADiag), VLAIsError(VLAIsError), + SuppressNotICEVLA(SuppressNotICEVLA) {} Sema::SemaDiagnosticBuilder diagnoseNotICEType(Sema &S, SourceLocation Loc, QualType T) override { @@ -2379,14 +2381,18 @@ Sema::SemaDiagnosticBuilder diagnoseNotICE(Sema &S, SourceLocation Loc) override { IsVLA = !VLAIsError; - return S.Diag(Loc, VLADiag); + if (!SuppressNotICEVLA) + return S.Diag(Loc, VLADiag); + return Sema::SemaDiagnosticBuilder(S); } Sema::SemaDiagnosticBuilder diagnoseFold(Sema &S, SourceLocation Loc) override { return S.Diag(Loc, diag::ext_vla_folded_to_constant); } - } Diagnoser(VLADiag, VLAIsError); + } Diagnoser(VLADiag, VLAIsError, + S.getCurScope()->isFunctionPrototypeScope() && + VLADiag == diag::warn_vla_used); ExprResult R = S.VerifyIntegerConstantExpression(ArraySize, &SizeVal, Diagnoser); @@ -2528,7 +2534,9 @@ llvm::APSInt ConstVal(Context.getTypeSize(Context.getSizeType())); if (!ArraySize) { if (ASM == ArrayType::Star) { - Diag(Loc, VLADiag); + if (!(getCurScope()->isFunctionPrototypeScope() && + VLADiag == diag::warn_vla_used)) + Diag(Loc, VLADiag); if (VLAIsError) return QualType(); Index: clang/include/clang/Sema/Sema.h =================================================================== --- clang/include/clang/Sema/Sema.h +++ clang/include/clang/Sema/Sema.h @@ -1756,6 +1756,8 @@ K_Deferred }; + // Special builder emitting no diagnostics + SemaDiagnosticBuilder(Sema &S) : S(S) {} SemaDiagnosticBuilder(Kind K, SourceLocation Loc, unsigned DiagID, FunctionDecl *Fn, Sema &S); SemaDiagnosticBuilder(SemaDiagnosticBuilder &&D);
Index: clang/test/Sema/warn-vla.c =================================================================== --- clang/test/Sema/warn-vla.c +++ clang/test/Sema/warn-vla.c @@ -5,8 +5,18 @@ int v[n]; // expected-warning {{variable length array}} } -void test2(int n, int v[n]) { // expected-warning {{variable length array}} +void test2(int n, int v[n]) { // c99 no-warning +#if __STDC_VERSION__ < 199901L +// expected-warning@-2{{variable length arrays are a C99 feature}} +#endif } -void test3(int n, int v[n]); // expected-warning {{variable length array}} +void test3(int n, int v[n]); // c99 no-warning +#if __STDC_VERSION__ < 199901L +// expected-warning@-2{{variable length arrays are a C99 feature}} +#endif +void test4(int n, int v[][*]); // c99 no-warning +#if __STDC_VERSION__ < 199901L +// expected-warning@-2{{variable length arrays are a C99 feature}} +#endif Index: clang/lib/Sema/SemaType.cpp =================================================================== --- clang/lib/Sema/SemaType.cpp +++ clang/lib/Sema/SemaType.cpp @@ -2367,9 +2367,11 @@ unsigned VLADiag; bool VLAIsError; bool IsVLA = false; + bool SuppressNotICEVLA = false; - VLADiagnoser(unsigned VLADiag, bool VLAIsError) - : VLADiag(VLADiag), VLAIsError(VLAIsError) {} + VLADiagnoser(unsigned VLADiag, bool VLAIsError, bool SuppressNotICEVLA) + : VLADiag(VLADiag), VLAIsError(VLAIsError), + SuppressNotICEVLA(SuppressNotICEVLA) {} Sema::SemaDiagnosticBuilder diagnoseNotICEType(Sema &S, SourceLocation Loc, QualType T) override { @@ -2379,14 +2381,18 @@ Sema::SemaDiagnosticBuilder diagnoseNotICE(Sema &S, SourceLocation Loc) override { IsVLA = !VLAIsError; - return S.Diag(Loc, VLADiag); + if (!SuppressNotICEVLA) + return S.Diag(Loc, VLADiag); + return Sema::SemaDiagnosticBuilder(S); } Sema::SemaDiagnosticBuilder diagnoseFold(Sema &S, SourceLocation Loc) override { return S.Diag(Loc, diag::ext_vla_folded_to_constant); } - } Diagnoser(VLADiag, VLAIsError); + } Diagnoser(VLADiag, VLAIsError, + S.getCurScope()->isFunctionPrototypeScope() && + VLADiag == diag::warn_vla_used); ExprResult R = S.VerifyIntegerConstantExpression(ArraySize, &SizeVal, Diagnoser); @@ -2528,7 +2534,9 @@ llvm::APSInt ConstVal(Context.getTypeSize(Context.getSizeType())); if (!ArraySize) { if (ASM == ArrayType::Star) { - Diag(Loc, VLADiag); + if (!(getCurScope()->isFunctionPrototypeScope() && + VLADiag == diag::warn_vla_used)) + Diag(Loc, VLADiag); if (VLAIsError) return QualType(); Index: clang/include/clang/Sema/Sema.h =================================================================== --- clang/include/clang/Sema/Sema.h +++ clang/include/clang/Sema/Sema.h @@ -1756,6 +1756,8 @@ K_Deferred }; + // Special builder emitting no diagnostics + SemaDiagnosticBuilder(Sema &S) : S(S) {} SemaDiagnosticBuilder(Kind K, SourceLocation Loc, unsigned DiagID, FunctionDecl *Fn, Sema &S); SemaDiagnosticBuilder(SemaDiagnosticBuilder &&D);
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits