[clang] [clang][Interp] Don't diagnose indexing arrays with index 0 (PR #102454)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 8 05:23:03 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
Even if the array is of unknown bounds, index 0 is fine.
---
Full diff: https://github.com/llvm/llvm-project/pull/102454.diff
2 Files Affected:
- (modified) clang/lib/AST/Interp/Interp.h (+2-2)
- (modified) clang/test/AST/Interp/literals.cpp (+7-5)
``````````diff
diff --git a/clang/lib/AST/Interp/Interp.h b/clang/lib/AST/Interp/Interp.h
index 423508b3adb99..832fc028ad669 100644
--- a/clang/lib/AST/Interp/Interp.h
+++ b/clang/lib/AST/Interp/Interp.h
@@ -2421,7 +2421,7 @@ inline bool ArrayElemPtr(InterpState &S, CodePtr OpPC) {
const T &Offset = S.Stk.pop<T>();
const Pointer &Ptr = S.Stk.peek<Pointer>();
- if (!Ptr.isZero()) {
+ if (!Ptr.isZero() && !Offset.isZero()) {
if (!CheckArray(S, OpPC, Ptr))
return false;
}
@@ -2437,7 +2437,7 @@ inline bool ArrayElemPtrPop(InterpState &S, CodePtr OpPC) {
const T &Offset = S.Stk.pop<T>();
const Pointer &Ptr = S.Stk.pop<Pointer>();
- if (!Ptr.isZero()) {
+ if (!Ptr.isZero() && !Offset.isZero()) {
if (!CheckArray(S, OpPC, Ptr))
return false;
}
diff --git a/clang/test/AST/Interp/literals.cpp b/clang/test/AST/Interp/literals.cpp
index 815fb67b9bbfc..a46f6ed747ec2 100644
--- a/clang/test/AST/Interp/literals.cpp
+++ b/clang/test/AST/Interp/literals.cpp
@@ -1196,13 +1196,15 @@ namespace incdecbool {
}
#if __cplusplus >= 201402L
-/// NOTE: The diagnostics of the two interpreters are a little
-/// different here, but they both make sense.
constexpr int externvar1() { // both-error {{never produces a constant expression}}
- extern char arr[]; // ref-note {{declared here}}
- return arr[0]; // ref-note {{read of non-constexpr variable 'arr'}} \
- // expected-note {{indexing of array without known bound}}
+ extern char arr[]; // both-note {{declared here}}
+ return arr[0]; // both-note {{read of non-constexpr variable 'arr'}}
}
+namespace externarr {
+ extern int arr[];
+ constexpr int *externarrindex = &arr[0]; /// No diagnostic.
+}
+
namespace StmtExprs {
constexpr int foo() {
``````````
</details>
https://github.com/llvm/llvm-project/pull/102454
More information about the cfe-commits
mailing list