Skip to content

Commit 817c832

Browse files
authored
[clang] Improve source location in binary type traits diagnostics (#88097)
This patch takes advantage of a recent NFC change that refactored `EvaluateBinaryTypeTrait()` to accept `TypeSourceInfo` instead of `QualType` c7db450. Before: ``` test2.cpp:105:55: error: variable length arrays are not supported in '__is_layout_compatible' 105 | static_assert(!__is_layout_compatible(int[n], int[n])); | ^ test2.cpp:125:76: error: incomplete type 'CStructIncomplete' where a complete type is required 125 | static_assert(__is_layout_compatible(CStructIncomplete, CStructIncomplete)); | ^ ``` After: ``` test2.cpp:105:41: error: variable length arrays are not supported in '__is_layout_compatible' 105 | static_assert(!__is_layout_compatible(int[n], int[n])); | ^ test2.cpp:125:40: error: incomplete type 'CStructIncomplete' where a complete type is required 125 | static_assert(__is_layout_compatible(CStructIncomplete, CStructIncomplete)); | ^ ```
1 parent 6c40d46 commit 817c832

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

clang/lib/Sema/SemaExprCXX.cpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5895,7 +5895,8 @@ static bool EvaluateBinaryTypeTrait(Sema &Self, TypeTrait BTT, const TypeSourceI
58955895
return false;
58965896

58975897
if (Self.RequireCompleteType(
5898-
KeyLoc, RhsT, diag::err_incomplete_type_used_in_type_trait_expr))
5898+
Rhs->getTypeLoc().getBeginLoc(), RhsT,
5899+
diag::err_incomplete_type_used_in_type_trait_expr))
58995900
return false;
59005901

59015902
return BaseInterface->isSuperClassOf(DerivedInterface);
@@ -5918,8 +5919,9 @@ static bool EvaluateBinaryTypeTrait(Sema &Self, TypeTrait BTT, const TypeSourceI
59185919
// If Base and Derived are class types and are different types
59195920
// (ignoring possible cv-qualifiers) then Derived shall be a
59205921
// complete type.
5921-
if (Self.RequireCompleteType(KeyLoc, RhsT,
5922-
diag::err_incomplete_type_used_in_type_trait_expr))
5922+
if (Self.RequireCompleteType(
5923+
Rhs->getTypeLoc().getBeginLoc(), RhsT,
5924+
diag::err_incomplete_type_used_in_type_trait_expr))
59235925
return false;
59245926

59255927
return cast<CXXRecordDecl>(rhsRecord->getDecl())
@@ -5971,7 +5973,8 @@ static bool EvaluateBinaryTypeTrait(Sema &Self, TypeTrait BTT, const TypeSourceI
59715973
return LhsT->isVoidType();
59725974

59735975
// A function definition requires a complete, non-abstract return type.
5974-
if (!Self.isCompleteType(KeyLoc, RhsT) || Self.isAbstractType(KeyLoc, RhsT))
5976+
if (!Self.isCompleteType(Rhs->getTypeLoc().getBeginLoc(), RhsT) ||
5977+
Self.isAbstractType(Rhs->getTypeLoc().getBeginLoc(), RhsT))
59755978
return false;
59765979

59775980
// Compute the result of add_rvalue_reference.
@@ -6021,12 +6024,14 @@ static bool EvaluateBinaryTypeTrait(Sema &Self, TypeTrait BTT, const TypeSourceI
60216024
// For both, T and U shall be complete types, (possibly cv-qualified)
60226025
// void, or arrays of unknown bound.
60236026
if (!LhsT->isVoidType() && !LhsT->isIncompleteArrayType() &&
6024-
Self.RequireCompleteType(KeyLoc, LhsT,
6025-
diag::err_incomplete_type_used_in_type_trait_expr))
6027+
Self.RequireCompleteType(
6028+
Lhs->getTypeLoc().getBeginLoc(), LhsT,
6029+
diag::err_incomplete_type_used_in_type_trait_expr))
60266030
return false;
60276031
if (!RhsT->isVoidType() && !RhsT->isIncompleteArrayType() &&
6028-
Self.RequireCompleteType(KeyLoc, RhsT,
6029-
diag::err_incomplete_type_used_in_type_trait_expr))
6032+
Self.RequireCompleteType(
6033+
Rhs->getTypeLoc().getBeginLoc(), RhsT,
6034+
diag::err_incomplete_type_used_in_type_trait_expr))
60306035
return false;
60316036

60326037
// cv void is never assignable.
@@ -6081,12 +6086,17 @@ static bool EvaluateBinaryTypeTrait(Sema &Self, TypeTrait BTT, const TypeSourceI
60816086
}
60826087
case BTT_IsLayoutCompatible: {
60836088
if (!LhsT->isVoidType() && !LhsT->isIncompleteArrayType())
6084-
Self.RequireCompleteType(KeyLoc, LhsT, diag::err_incomplete_type);
6089+
Self.RequireCompleteType(Lhs->getTypeLoc().getBeginLoc(), LhsT,
6090+
diag::err_incomplete_type);
60856091
if (!RhsT->isVoidType() && !RhsT->isIncompleteArrayType())
6086-
Self.RequireCompleteType(KeyLoc, RhsT, diag::err_incomplete_type);
6092+
Self.RequireCompleteType(Rhs->getTypeLoc().getBeginLoc(), RhsT,
6093+
diag::err_incomplete_type);
60876094

6088-
if (LhsT->isVariableArrayType() || RhsT->isVariableArrayType())
6089-
Self.Diag(KeyLoc, diag::err_vla_unsupported)
6095+
if (LhsT->isVariableArrayType())
6096+
Self.Diag(Lhs->getTypeLoc().getBeginLoc(), diag::err_vla_unsupported)
6097+
<< 1 << tok::kw___is_layout_compatible;
6098+
if (RhsT->isVariableArrayType())
6099+
Self.Diag(Rhs->getTypeLoc().getBeginLoc(), diag::err_vla_unsupported)
60906100
<< 1 << tok::kw___is_layout_compatible;
60916101
return Self.IsLayoutCompatible(LhsT, RhsT);
60926102
}

clang/test/SemaCXX/type-traits.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1759,6 +1759,7 @@ void is_layout_compatible(int n)
17591759
// expected-error@-1 {{variable length arrays are not supported in '__is_layout_compatible'}}
17601760
static_assert(!__is_layout_compatible(int[n], int[n]));
17611761
// expected-error@-1 {{variable length arrays are not supported in '__is_layout_compatible'}}
1762+
// expected-error@-2 {{variable length arrays are not supported in '__is_layout_compatible'}}
17621763
static_assert(__is_layout_compatible(int&, int&));
17631764
static_assert(!__is_layout_compatible(int&, char&));
17641765
static_assert(__is_layout_compatible(void(int), void(int)));

0 commit comments

Comments
 (0)