-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang][Interp] Fix variables referring to their own address #70587
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This was a combination of issues: 1) We can subtract pointers that don't point into arrays. 2) In C, everything is possible.
@llvm/pr-subscribers-clang Author: Timm Baeder (tbaederr) ChangesThis was a combination of issues:
Full diff: https://github.com/llvm/llvm-project/pull/70587.diff 3 Files Affected:
diff --git a/clang/lib/AST/Interp/Interp.h b/clang/lib/AST/Interp/Interp.h
index 7dd415d6e460536..c5ee720d5a29ddd 100644
--- a/clang/lib/AST/Interp/Interp.h
+++ b/clang/lib/AST/Interp/Interp.h
@@ -1453,7 +1453,7 @@ bool OffsetHelper(InterpState &S, CodePtr OpPC, const T &Offset,
DiagInvalidOffset();
}
- if (Invalid && !Ptr.isDummy())
+ if (Invalid && !Ptr.isDummy() && S.getLangOpts().CPlusPlus)
return false;
// Offset is valid - compute it on unsigned.
@@ -1531,7 +1531,7 @@ inline bool SubPtr(InterpState &S, CodePtr OpPC) {
const Pointer &LHS = S.Stk.pop<Pointer>();
const Pointer &RHS = S.Stk.pop<Pointer>();
- if (!Pointer::hasSameArray(LHS, RHS)) {
+ if (!Pointer::hasSameBase(LHS, RHS) && S.getLangOpts().CPlusPlus) {
// TODO: Diagnose.
return false;
}
diff --git a/clang/test/AST/Interp/c.c b/clang/test/AST/Interp/c.c
index 6bfcded0a78646c..2bc3d906bcc5ef9 100644
--- a/clang/test/AST/Interp/c.c
+++ b/clang/test/AST/Interp/c.c
@@ -4,6 +4,7 @@
// RUN: %clang_cc1 -pedantic -verify=pedantic-ref -std=c11 %s
typedef __INTPTR_TYPE__ intptr_t;
+typedef __PTRDIFF_TYPE__ ptrdiff_t;
_Static_assert(1, "");
_Static_assert(0 != 1, "");
@@ -81,3 +82,6 @@ const intptr_t L = (intptr_t)(&(yy->y)); // expected-error {{not a compile-time
// pedantic-expected-error {{not a compile-time constant}} \
// ref-error {{not a compile-time constant}} \
// pedantic-ref-error {{not a compile-time constant}}
+const ptrdiff_t m = &m + 137 - &m;
+_Static_assert(m == 137, ""); // pedantic-ref-warning {{GNU extension}} \
+ // pedantic-expected-warning {{GNU extension}}
diff --git a/clang/test/AST/Interp/literals.cpp b/clang/test/AST/Interp/literals.cpp
index ba24955d14503be..92b467da3e8847b 100644
--- a/clang/test/AST/Interp/literals.cpp
+++ b/clang/test/AST/Interp/literals.cpp
@@ -7,6 +7,7 @@
#define INT_MAX __INT_MAX__
typedef __INTPTR_TYPE__ intptr_t;
+typedef __PTRDIFF_TYPE__ ptrdiff_t;
static_assert(true, "");
@@ -198,6 +199,20 @@ namespace PointerComparison {
// ref-note {{comparison between '&s.b' and 'nullptr' has unspecified value}}
constexpr bool v7 = qv <= (void*)&s.b; // ok
+
+ constexpr ptrdiff_t m = &m - &m;
+ static_assert(m == 0, "");
+
+ constexpr ptrdiff_t m2 = (&m2 + 1) - (&m2 + 1);
+ static_assert(m2 == 0, "");
+
+ constexpr long m3 = (&m3 + 1) - (&m3);
+ static_assert(m3 == 1, "");
+
+ constexpr long m4 = &m4 + 2 - &m4; // ref-error {{must be initialized by a constant expression}} \
+ // ref-note {{cannot refer to element 2 of non-array object}} \
+ // expected-error {{must be initialized by a constant expression}} \
+ // expected-note {{cannot refer to element 2 of non-array object}}
}
namespace SizeOf {
|
Ping |
1 similar comment
Ping |
AaronBallman
approved these changes
Nov 14, 2023
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, lol, yes, everything is possible in C. :-D
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
clang:frontend
Language frontend issues, e.g. anything involving "Sema"
clang
Clang issues not falling into any other category
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This was a combination of issues: