Skip to content

Commit 1d1a98e

Browse files
committed
Revert "[clang] use getCommonSugar in an assortment of places"
This reverts commit aff1f63.
1 parent aff1f63 commit 1d1a98e

File tree

23 files changed

+159
-222
lines changed

23 files changed

+159
-222
lines changed

clang-tools-extra/clangd/unittests/tweaks/ExtractVariableTests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ TEST_F(ExtractVariableTest, Test) {
330330
void bar() {
331331
int (*placeholder)(int) = foo('c'); (void)placeholder;
332332
})cpp"},
333-
// Arithmetic on typedef types preserves typedef types
333+
// Arithmetic on typedef types yields plain integer types
334334
{R"cpp(typedef long NSInteger;
335335
void varDecl() {
336336
NSInteger a = 2 * 5;
@@ -339,7 +339,7 @@ TEST_F(ExtractVariableTest, Test) {
339339
R"cpp(typedef long NSInteger;
340340
void varDecl() {
341341
NSInteger a = 2 * 5;
342-
NSInteger placeholder = a * 7; NSInteger b = placeholder + 3;
342+
long placeholder = a * 7; NSInteger b = placeholder + 3;
343343
})cpp"},
344344
};
345345
for (const auto &IO : InputOutputs) {

clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/narrowing-conversions-ignoreconversionfromtypes-option.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ void narrowing_size_method() {
4242
// IGNORED: Warning is disabled with IgnoreConversionFromTypes=global_size_t.
4343

4444
i = j + v.size();
45-
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:7: warning: narrowing conversion from 'global_size_t' (aka 'long long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]
45+
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:7: warning: narrowing conversion from 'long long' to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]
4646
// IGNORED: Warning is disabled with IgnoreConversionFromTypes=global_size_t.
4747
}
4848

@@ -51,7 +51,7 @@ void narrowing_size_method_binary_expr() {
5151
int j;
5252
vector v;
5353
i = j + v.size();
54-
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:7: warning: narrowing conversion from 'global_size_t' (aka 'long long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]
54+
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:7: warning: narrowing conversion from 'long long' to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]
5555
// IGNORED: Warning is disabled with IgnoreConversionFromTypes=global_size_t.
5656
}
5757

@@ -63,7 +63,7 @@ void narrowing_size_method_binary_op() {
6363
// IGNORED: Warning is disabled with IgnoreConversionFromTypes=global_size_t.
6464

6565
i += j + v.size();
66-
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:8: warning: narrowing conversion from 'global_size_t' (aka 'long long') to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]
66+
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:8: warning: narrowing conversion from 'long long' to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]
6767
// IGNORED: Warning is disabled with IgnoreConversionFromTypes=global_size_t.
6868
}
6969

clang/lib/Sema/SemaExpr.cpp

Lines changed: 83 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,7 +1088,7 @@ static bool handleIntegerToComplexFloatConversion(Sema &S, ExprResult &IntExpr,
10881088
if (IntTy->isComplexType() || IntTy->isRealFloatingType()) return true;
10891089
if (SkipCast) return false;
10901090
if (IntTy->isIntegerType()) {
1091-
QualType fpTy = ComplexTy->castAs<ComplexType>()->getElementType();
1091+
QualType fpTy = cast<ComplexType>(ComplexTy)->getElementType();
10921092
IntExpr = S.ImpCastExprToType(IntExpr.get(), fpTy, CK_IntegralToFloating);
10931093
IntExpr = S.ImpCastExprToType(IntExpr.get(), ComplexTy,
10941094
CK_FloatingRealToComplex);
@@ -1100,59 +1100,60 @@ static bool handleIntegerToComplexFloatConversion(Sema &S, ExprResult &IntExpr,
11001100
return false;
11011101
}
11021102

1103-
// This handles complex/complex, complex/float, or float/complex.
1104-
// When both operands are complex, the shorter operand is converted to the
1105-
// type of the longer, and that is the type of the result. This corresponds
1106-
// to what is done when combining two real floating-point operands.
1107-
// The fun begins when size promotion occur across type domains.
1108-
// From H&S 6.3.4: When one operand is complex and the other is a real
1109-
// floating-point type, the less precise type is converted, within it's
1110-
// real or complex domain, to the precision of the other type. For example,
1111-
// when combining a "long double" with a "double _Complex", the
1112-
// "double _Complex" is promoted to "long double _Complex".
1113-
static QualType handleComplexFloatConversion(Sema &S, ExprResult &Shorter,
1114-
QualType ShorterType,
1115-
QualType LongerType,
1116-
bool PromotePrecision) {
1117-
bool LongerIsComplex = isa<ComplexType>(LongerType.getCanonicalType());
1118-
QualType Result =
1119-
LongerIsComplex ? LongerType : S.Context.getComplexType(LongerType);
1120-
1121-
if (PromotePrecision) {
1122-
if (isa<ComplexType>(ShorterType.getCanonicalType())) {
1123-
Shorter =
1124-
S.ImpCastExprToType(Shorter.get(), Result, CK_FloatingComplexCast);
1125-
} else {
1126-
if (LongerIsComplex)
1127-
LongerType = LongerType->castAs<ComplexType>()->getElementType();
1128-
Shorter = S.ImpCastExprToType(Shorter.get(), LongerType, CK_FloatingCast);
1129-
}
1130-
}
1131-
return Result;
1132-
}
1133-
11341103
/// Handle arithmetic conversion with complex types. Helper function of
11351104
/// UsualArithmeticConversions()
1136-
static QualType handleComplexConversion(Sema &S, ExprResult &LHS,
1137-
ExprResult &RHS, QualType LHSType,
1138-
QualType RHSType, bool IsCompAssign) {
1105+
static QualType handleComplexFloatConversion(Sema &S, ExprResult &LHS,
1106+
ExprResult &RHS, QualType LHSType,
1107+
QualType RHSType,
1108+
bool IsCompAssign) {
11391109
// if we have an integer operand, the result is the complex type.
11401110
if (!handleIntegerToComplexFloatConversion(S, RHS, LHS, RHSType, LHSType,
1141-
/*SkipCast=*/false))
1111+
/*skipCast*/false))
11421112
return LHSType;
11431113
if (!handleIntegerToComplexFloatConversion(S, LHS, RHS, LHSType, RHSType,
1144-
/*SkipCast=*/IsCompAssign))
1114+
/*skipCast*/IsCompAssign))
11451115
return RHSType;
11461116

1117+
// This handles complex/complex, complex/float, or float/complex.
1118+
// When both operands are complex, the shorter operand is converted to the
1119+
// type of the longer, and that is the type of the result. This corresponds
1120+
// to what is done when combining two real floating-point operands.
1121+
// The fun begins when size promotion occur across type domains.
1122+
// From H&S 6.3.4: When one operand is complex and the other is a real
1123+
// floating-point type, the less precise type is converted, within it's
1124+
// real or complex domain, to the precision of the other type. For example,
1125+
// when combining a "long double" with a "double _Complex", the
1126+
// "double _Complex" is promoted to "long double _Complex".
1127+
11471128
// Compute the rank of the two types, regardless of whether they are complex.
11481129
int Order = S.Context.getFloatingTypeOrder(LHSType, RHSType);
1149-
if (Order < 0)
1130+
1131+
auto *LHSComplexType = dyn_cast<ComplexType>(LHSType);
1132+
auto *RHSComplexType = dyn_cast<ComplexType>(RHSType);
1133+
QualType LHSElementType =
1134+
LHSComplexType ? LHSComplexType->getElementType() : LHSType;
1135+
QualType RHSElementType =
1136+
RHSComplexType ? RHSComplexType->getElementType() : RHSType;
1137+
1138+
QualType ResultType = S.Context.getComplexType(LHSElementType);
1139+
if (Order < 0) {
11501140
// Promote the precision of the LHS if not an assignment.
1151-
return handleComplexFloatConversion(S, LHS, LHSType, RHSType,
1152-
/*PromotePrecision=*/!IsCompAssign);
1153-
// Promote the precision of the RHS unless it is already the same as the LHS.
1154-
return handleComplexFloatConversion(S, RHS, RHSType, LHSType,
1155-
/*PromotePrecision=*/Order > 0);
1141+
ResultType = S.Context.getComplexType(RHSElementType);
1142+
if (!IsCompAssign) {
1143+
if (LHSComplexType)
1144+
LHS =
1145+
S.ImpCastExprToType(LHS.get(), ResultType, CK_FloatingComplexCast);
1146+
else
1147+
LHS = S.ImpCastExprToType(LHS.get(), RHSElementType, CK_FloatingCast);
1148+
}
1149+
} else if (Order > 0) {
1150+
// Promote the precision of the RHS.
1151+
if (RHSComplexType)
1152+
RHS = S.ImpCastExprToType(RHS.get(), ResultType, CK_FloatingComplexCast);
1153+
else
1154+
RHS = S.ImpCastExprToType(RHS.get(), LHSElementType, CK_FloatingCast);
1155+
}
1156+
return ResultType;
11561157
}
11571158

11581159
/// Handle arithmetic conversion from integer to float. Helper function
@@ -1538,16 +1539,18 @@ QualType Sema::UsualArithmeticConversions(ExprResult &LHS, ExprResult &RHS,
15381539

15391540
// For conversion purposes, we ignore any qualifiers.
15401541
// For example, "const float" and "float" are equivalent.
1541-
QualType LHSType = LHS.get()->getType().getUnqualifiedType();
1542-
QualType RHSType = RHS.get()->getType().getUnqualifiedType();
1542+
QualType LHSType =
1543+
Context.getCanonicalType(LHS.get()->getType()).getUnqualifiedType();
1544+
QualType RHSType =
1545+
Context.getCanonicalType(RHS.get()->getType()).getUnqualifiedType();
15431546

15441547
// For conversion purposes, we ignore any atomic qualifier on the LHS.
15451548
if (const AtomicType *AtomicLHS = LHSType->getAs<AtomicType>())
15461549
LHSType = AtomicLHS->getValueType();
15471550

15481551
// If both types are identical, no conversion is needed.
1549-
if (Context.hasSameType(LHSType, RHSType))
1550-
return Context.getCommonSugaredType(LHSType, RHSType);
1552+
if (LHSType == RHSType)
1553+
return LHSType;
15511554

15521555
// If either side is a non-arithmetic type (e.g. a pointer), we are done.
15531556
// The caller can deal with this (e.g. pointer + int).
@@ -1565,8 +1568,8 @@ QualType Sema::UsualArithmeticConversions(ExprResult &LHS, ExprResult &RHS,
15651568
LHS = ImpCastExprToType(LHS.get(), LHSType, CK_IntegralCast);
15661569

15671570
// If both types are identical, no conversion is needed.
1568-
if (Context.hasSameType(LHSType, RHSType))
1569-
return Context.getCommonSugaredType(LHSType, RHSType);
1571+
if (LHSType == RHSType)
1572+
return LHSType;
15701573

15711574
// At this point, we have two different arithmetic types.
15721575

@@ -1577,8 +1580,8 @@ QualType Sema::UsualArithmeticConversions(ExprResult &LHS, ExprResult &RHS,
15771580

15781581
// Handle complex types first (C99 6.3.1.8p1).
15791582
if (LHSType->isComplexType() || RHSType->isComplexType())
1580-
return handleComplexConversion(*this, LHS, RHS, LHSType, RHSType,
1581-
ACK == ACK_CompAssign);
1583+
return handleComplexFloatConversion(*this, LHS, RHS, LHSType, RHSType,
1584+
ACK == ACK_CompAssign);
15821585

15831586
// Now handle "real" floating types (i.e. float, double, long double).
15841587
if (LHSType->isRealFloatingType() || RHSType->isRealFloatingType())
@@ -8155,6 +8158,23 @@ static bool checkCondition(Sema &S, Expr *Cond, SourceLocation QuestionLoc) {
81558158
return true;
81568159
}
81578160

8161+
/// Handle when one or both operands are void type.
8162+
static QualType checkConditionalVoidType(Sema &S, ExprResult &LHS,
8163+
ExprResult &RHS) {
8164+
Expr *LHSExpr = LHS.get();
8165+
Expr *RHSExpr = RHS.get();
8166+
8167+
if (!LHSExpr->getType()->isVoidType())
8168+
S.Diag(RHSExpr->getBeginLoc(), diag::ext_typecheck_cond_one_void)
8169+
<< RHSExpr->getSourceRange();
8170+
if (!RHSExpr->getType()->isVoidType())
8171+
S.Diag(LHSExpr->getBeginLoc(), diag::ext_typecheck_cond_one_void)
8172+
<< LHSExpr->getSourceRange();
8173+
LHS = S.ImpCastExprToType(LHS.get(), S.Context.VoidTy, CK_ToVoid);
8174+
RHS = S.ImpCastExprToType(RHS.get(), S.Context.VoidTy, CK_ToVoid);
8175+
return S.Context.VoidTy;
8176+
}
8177+
81588178
/// Return false if the NullExpr can be promoted to PointerTy,
81598179
/// true otherwise.
81608180
static bool checkConditionalNullPointer(Sema &S, ExprResult &NullExpr,
@@ -8178,7 +8198,7 @@ static QualType checkConditionalPointerCompatibility(Sema &S, ExprResult &LHS,
81788198

81798199
if (S.Context.hasSameType(LHSTy, RHSTy)) {
81808200
// Two identical pointers types are always compatible.
8181-
return S.Context.getCommonSugaredType(LHSTy, RHSTy);
8201+
return LHSTy;
81828202
}
81838203

81848204
QualType lhptee, rhptee;
@@ -8680,7 +8700,7 @@ QualType Sema::CheckConditionalOperands(ExprResult &Cond, ExprResult &LHS,
86808700

86818701
// And if they're both bfloat (which isn't arithmetic), that's fine too.
86828702
if (LHSTy->isBFloat16Type() && RHSTy->isBFloat16Type()) {
8683-
return Context.getCommonSugaredType(LHSTy, RHSTy);
8703+
return LHSTy;
86848704
}
86858705

86868706
// If both operands are the same structure or union type, the result is that
@@ -8690,29 +8710,14 @@ QualType Sema::CheckConditionalOperands(ExprResult &Cond, ExprResult &LHS,
86908710
if (LHSRT->getDecl() == RHSRT->getDecl())
86918711
// "If both the operands have structure or union type, the result has
86928712
// that type." This implies that CV qualifiers are dropped.
8693-
return Context.getCommonSugaredType(LHSTy.getUnqualifiedType(),
8694-
RHSTy.getUnqualifiedType());
8713+
return LHSTy.getUnqualifiedType();
86958714
// FIXME: Type of conditional expression must be complete in C mode.
86968715
}
86978716

86988717
// C99 6.5.15p5: "If both operands have void type, the result has void type."
86998718
// The following || allows only one side to be void (a GCC-ism).
87008719
if (LHSTy->isVoidType() || RHSTy->isVoidType()) {
8701-
QualType ResTy;
8702-
if (LHSTy->isVoidType() && RHSTy->isVoidType()) {
8703-
ResTy = Context.getCommonSugaredType(LHSTy, RHSTy);
8704-
} else if (RHSTy->isVoidType()) {
8705-
ResTy = RHSTy;
8706-
Diag(RHS.get()->getBeginLoc(), diag::ext_typecheck_cond_one_void)
8707-
<< RHS.get()->getSourceRange();
8708-
} else {
8709-
ResTy = LHSTy;
8710-
Diag(LHS.get()->getBeginLoc(), diag::ext_typecheck_cond_one_void)
8711-
<< LHS.get()->getSourceRange();
8712-
}
8713-
LHS = ImpCastExprToType(LHS.get(), ResTy, CK_ToVoid);
8714-
RHS = ImpCastExprToType(RHS.get(), ResTy, CK_ToVoid);
8715-
return ResTy;
8720+
return checkConditionalVoidType(*this, LHS, RHS);
87168721
}
87178722

87188723
// C99 6.5.15p6 - "if one operand is a null pointer constant, the result has
@@ -8751,7 +8756,7 @@ QualType Sema::CheckConditionalOperands(ExprResult &Cond, ExprResult &LHS,
87518756
// Allow ?: operations in which both operands have the same
87528757
// built-in sizeless type.
87538758
if (LHSTy->isSizelessBuiltinType() && Context.hasSameType(LHSTy, RHSTy))
8754-
return Context.getCommonSugaredType(LHSTy, RHSTy);
8759+
return LHSTy;
87558760

87568761
// Emit a better diagnostic if one of the expressions is a null pointer
87578762
// constant and the other is not a pointer type. In this case, the user most
@@ -10422,7 +10427,7 @@ QualType Sema::CheckVectorOperands(ExprResult &LHS, ExprResult &RHS,
1042210427

1042310428
// If the vector types are identical, return.
1042410429
if (Context.hasSameType(LHSType, RHSType))
10425-
return Context.getCommonSugaredType(LHSType, RHSType);
10430+
return LHSType;
1042610431

1042710432
// If we have compatible AltiVec and GCC vector types, use the AltiVec type.
1042810433
if (LHSVecType && RHSVecType &&
@@ -13140,7 +13145,7 @@ QualType Sema::CheckMatrixElementwiseOperands(ExprResult &LHS, ExprResult &RHS,
1314013145
assert((LHSMatType || RHSMatType) && "At least one operand must be a matrix");
1314113146

1314213147
if (Context.hasSameType(LHSType, RHSType))
13143-
return Context.getCommonSugaredType(LHSType, RHSType);
13148+
return LHSType;
1314413149

1314513150
// Type conversion may change LHS/RHS. Keep copies to the original results, in
1314613151
// case we have to return InvalidOperands.
@@ -13184,19 +13189,13 @@ QualType Sema::CheckMatrixMultiplyOperands(ExprResult &LHS, ExprResult &RHS,
1318413189
if (LHSMatType->getNumColumns() != RHSMatType->getNumRows())
1318513190
return InvalidOperands(Loc, LHS, RHS);
1318613191

13187-
if (Context.hasSameType(LHSMatType, RHSMatType))
13188-
return Context.getCommonSugaredType(
13189-
LHS.get()->getType().getUnqualifiedType(),
13190-
RHS.get()->getType().getUnqualifiedType());
13191-
13192-
QualType LHSELTy = LHSMatType->getElementType(),
13193-
RHSELTy = RHSMatType->getElementType();
13194-
if (!Context.hasSameType(LHSELTy, RHSELTy))
13192+
if (!Context.hasSameType(LHSMatType->getElementType(),
13193+
RHSMatType->getElementType()))
1319513194
return InvalidOperands(Loc, LHS, RHS);
1319613195

13197-
return Context.getConstantMatrixType(
13198-
Context.getCommonSugaredType(LHSELTy, RHSELTy),
13199-
LHSMatType->getNumRows(), RHSMatType->getNumColumns());
13196+
return Context.getConstantMatrixType(LHSMatType->getElementType(),
13197+
LHSMatType->getNumRows(),
13198+
RHSMatType->getNumColumns());
1320013199
}
1320113200
return CheckMatrixElementwiseOperands(LHS, RHS, Loc, IsCompAssign);
1320213201
}

0 commit comments

Comments
 (0)