[PATCH] D157855: [clang][ExprConstant] Improve error message of compound assignment against uninitialized object
Takuya Shimizu via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Aug 15 02:22:36 PDT 2023
hazohelet updated this revision to Diff 550216.
hazohelet marked an inline comment as done.
hazohelet added a comment.
Address comments from Timm
- Moved test to C++14,20,23 test file from C++20-only one
- NFC stylistic changes
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D157855/new/
https://reviews.llvm.org/D157855
Files:
clang/docs/ReleaseNotes.rst
clang/lib/AST/ExprConstant.cpp
clang/test/SemaCXX/constant-expression-cxx14.cpp
Index: clang/test/SemaCXX/constant-expression-cxx14.cpp
===================================================================
--- clang/test/SemaCXX/constant-expression-cxx14.cpp
+++ clang/test/SemaCXX/constant-expression-cxx14.cpp
@@ -1275,3 +1275,33 @@
(dbt2.wp = nullptr, 0)
};
}
+
+namespace UninitCompoundAssign {
+constexpr int scalar(int a) {
+ int sum; // cxx14-warning {{uninitialized variable in a constexpr function is a C++20 extension}}
+ sum += a; // expected-note {{read of uninitialized object}};
+ return 0;
+}
+static_assert(scalar(3), ""); // expected-error {{constant expression}} \
+ // expected-note {{in call to 'scalar(3)'}}
+
+constexpr int array(int a) {
+ int arr[3]; // cxx14-warning {{uninitialized variable in a constexpr function is a C++20 extension}}
+ arr[1] += a; // expected-note {{read of uninitialized object}};
+ return 0;
+}
+static_assert(array(3), ""); // expected-error {{constant expression}} \
+ // expected-note {{in call to 'array(3)'}}
+
+struct Foo {
+ int val; // cxx14-note{{member not initialized by constructor}}
+ constexpr Foo() {} // cxx14-warning {{constexpr constructor that does not initialize all members is a C++20 extension}}
+};
+constexpr int field(int a) {
+ Foo f;
+ f.val += a; // expected-note {{read of uninitialized object}};
+ return 0;
+}
+static_assert(field(3), ""); // expected-error {{constant expression}} \
+ // expected-note {{in call to 'field(3)'}}
+}
Index: clang/lib/AST/ExprConstant.cpp
===================================================================
--- clang/lib/AST/ExprConstant.cpp
+++ clang/lib/AST/ExprConstant.cpp
@@ -4442,6 +4442,10 @@
return foundPointer(Subobj, SubobjType);
case APValue::Vector:
return foundVector(Subobj, SubobjType);
+ case APValue::Indeterminate:
+ Info.FFDiag(E, diag::note_constexpr_access_uninit)
+ << /*read of=*/0 << /*uninitialized object=*/1;
+ return false;
default:
// FIXME: can this happen?
Info.FFDiag(E);
Index: clang/docs/ReleaseNotes.rst
===================================================================
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -132,6 +132,9 @@
- Clang now warns on unused variables declared and initialized in condition
expressions.
(`#61681: <https://github.com/llvm/llvm-project/issues/61681>`_)
+- Clang constexpr evaluator now diagnoses compound assignment operators against
+ uninitialized variables as a read of uninitialized object.
+ (`#51536 <https://github.com/llvm/llvm-project/issues/51536>_`)
Bug Fixes in This Version
-------------------------
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D157855.550216.patch
Type: text/x-patch
Size: 2714 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230815/9e6de66f/attachment.bin>
More information about the cfe-commits
mailing list