Skip to content

Commit 56131e3

Browse files
authored
[clang][bytecode] Diagnose incomplete types more consistently (#153368)
To match the diagnostics of the current interpreter.
1 parent 78636be commit 56131e3

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

clang/lib/AST/ByteCode/InterpBuiltin.cpp

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,11 +1783,24 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
17831783
if (DestPtr.isDummy() || SrcPtr.isDummy())
17841784
return false;
17851785

1786+
if (DestPtr.getType()->isIncompleteType()) {
1787+
S.FFDiag(S.Current->getSource(OpPC),
1788+
diag::note_constexpr_memcpy_incomplete_type)
1789+
<< Move << DestPtr.getType();
1790+
return false;
1791+
}
1792+
if (SrcPtr.getType()->isIncompleteType()) {
1793+
S.FFDiag(S.Current->getSource(OpPC),
1794+
diag::note_constexpr_memcpy_incomplete_type)
1795+
<< Move << SrcPtr.getType();
1796+
return false;
1797+
}
1798+
17861799
QualType DestElemType = getElemType(DestPtr);
17871800
if (DestElemType->isIncompleteType()) {
17881801
S.FFDiag(S.Current->getSource(OpPC),
1789-
diag::note_constexpr_ltor_incomplete_type)
1790-
<< DestElemType;
1802+
diag::note_constexpr_memcpy_incomplete_type)
1803+
<< Move << DestElemType;
17911804
return false;
17921805
}
17931806

@@ -1832,16 +1845,6 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
18321845
return false;
18331846
}
18341847

1835-
if (DestElemType->isIncompleteType() ||
1836-
DestPtr.getType()->isIncompleteType()) {
1837-
QualType DiagType =
1838-
DestElemType->isIncompleteType() ? DestElemType : DestPtr.getType();
1839-
S.FFDiag(S.Current->getSource(OpPC),
1840-
diag::note_constexpr_memcpy_incomplete_type)
1841-
<< Move << DiagType;
1842-
return false;
1843-
}
1844-
18451848
if (!DestElemType.isTriviallyCopyableType(ASTCtx)) {
18461849
S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_memcpy_nontrivial)
18471850
<< Move << DestElemType;
@@ -2030,8 +2033,13 @@ static bool interp__builtin_memchr(InterpState &S, CodePtr OpPC,
20302033
return true;
20312034
}
20322035

2033-
if (Ptr.isDummy())
2036+
if (Ptr.isDummy()) {
2037+
if (Ptr.getType()->isIncompleteType())
2038+
S.FFDiag(S.Current->getSource(OpPC),
2039+
diag::note_constexpr_ltor_incomplete_type)
2040+
<< Ptr.getType();
20342041
return false;
2042+
}
20352043

20362044
// Null is only okay if the given size is 0.
20372045
if (Ptr.isZero()) {

clang/test/AST/ByteCode/builtin-functions.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1514,7 +1514,7 @@ namespace Memchr {
15141514
extern struct Incomplete incomplete;
15151515
static_assert(__builtin_memchr(&incomplete, 0, 0u) == nullptr);
15161516
static_assert(__builtin_memchr(&incomplete, 0, 1u) == nullptr); // both-error {{not an integral constant}} \
1517-
// ref-note {{read of incomplete type 'struct Incomplete'}}
1517+
// both-note {{read of incomplete type 'struct Incomplete'}}
15181518

15191519
const unsigned char &u1 = 0xf0;
15201520
auto &&i1 = (const signed char []){-128};
@@ -1697,6 +1697,15 @@ namespace WMemMove {
16971697
// both-note {{source of 'wmemmove' is nullptr}}
16981698
static_assert(__builtin_wmemmove(null, &global, sizeof(wchar_t))); // both-error {{}} \
16991699
// both-note {{destination of 'wmemmove' is nullptr}}
1700+
1701+
// Check that a pointer to an incomplete array is rejected.
1702+
constexpr int test_address_of_incomplete_array_type() { // both-error {{never produces a constant}}
1703+
extern int arr[];
1704+
__builtin_memmove(&arr, &arr, 4 * sizeof(arr[0])); // both-note 2{{cannot constant evaluate 'memmove' between objects of incomplete type 'int[]'}}
1705+
return arr[0] * 1000 + arr[1] * 100 + arr[2] * 10 + arr[3];
1706+
}
1707+
static_assert(test_address_of_incomplete_array_type() == 1234); // both-error {{constant}} \
1708+
// both-note {{in call}}
17001709
}
17011710

17021711
namespace Invalid {

0 commit comments

Comments
 (0)