@@ -2434,7 +2434,7 @@ bool IsSameFloatAfterCast(const APValue &value,
2434
2434
IsSameFloatAfterCast (value.getComplexFloatImag (), Src, Tgt));
2435
2435
}
2436
2436
2437
- void AnalyzeImplicitConversions (Sema &S, Expr *E);
2437
+ void AnalyzeImplicitConversions (Sema &S, Expr *E, SourceLocation CC );
2438
2438
2439
2439
static bool IsZero (Sema &S, Expr *E) {
2440
2440
// Suppress cases where we are comparing against an enum constant.
@@ -2487,8 +2487,8 @@ void CheckTrivialUnsignedComparison(Sema &S, BinaryOperator *E) {
2487
2487
// / Analyze the operands of the given comparison. Implements the
2488
2488
// / fallback case from AnalyzeComparison.
2489
2489
void AnalyzeImpConvsInComparison (Sema &S, BinaryOperator *E) {
2490
- AnalyzeImplicitConversions (S, E->getLHS ());
2491
- AnalyzeImplicitConversions (S, E->getRHS ());
2490
+ AnalyzeImplicitConversions (S, E->getLHS (), E-> getOperatorLoc () );
2491
+ AnalyzeImplicitConversions (S, E->getRHS (), E-> getOperatorLoc () );
2492
2492
}
2493
2493
2494
2494
// / \brief Implements -Wsign-compare.
@@ -2533,8 +2533,8 @@ void AnalyzeComparison(Sema &S, BinaryOperator *E) {
2533
2533
2534
2534
// Go ahead and analyze implicit conversions in the operands. Note
2535
2535
// that we skip the implicit conversions on both sides.
2536
- AnalyzeImplicitConversions (S, lex);
2537
- AnalyzeImplicitConversions (S, rex);
2536
+ AnalyzeImplicitConversions (S, lex, E-> getOperatorLoc () );
2537
+ AnalyzeImplicitConversions (S, rex, E-> getOperatorLoc () );
2538
2538
2539
2539
// If the signed range is non-negative, -Wsign-compare won't fire,
2540
2540
// but we should still check for comparisons which are always true
@@ -2564,27 +2564,36 @@ void AnalyzeComparison(Sema &S, BinaryOperator *E) {
2564
2564
}
2565
2565
2566
2566
// / Diagnose an implicit cast; purely a helper for CheckImplicitConversion.
2567
- void DiagnoseImpCast (Sema &S, Expr *E, QualType T, unsigned diag) {
2568
- S.Diag (E->getExprLoc (), diag) << E->getType () << T << E->getSourceRange ();
2567
+ void DiagnoseImpCast (Sema &S, Expr *E, QualType T, SourceLocation CContext,
2568
+ unsigned diag) {
2569
+ S.Diag (E->getExprLoc (), diag)
2570
+ << E->getType () << T << E->getSourceRange () << SourceRange (CContext);
2569
2571
}
2570
2572
2571
2573
void CheckImplicitConversion (Sema &S, Expr *E, QualType T,
2572
- bool *ICContext = 0 ) {
2574
+ SourceLocation CC, bool *ICContext = 0 ) {
2573
2575
if (E->isTypeDependent () || E->isValueDependent ()) return ;
2574
2576
2575
2577
const Type *Source = S.Context .getCanonicalType (E->getType ()).getTypePtr ();
2576
2578
const Type *Target = S.Context .getCanonicalType (T).getTypePtr ();
2577
2579
if (Source == Target) return ;
2578
2580
if (Target->isDependentType ()) return ;
2579
2581
2582
+ // If the conversion context location is invalid or instantiated
2583
+ // from a system macro, don't complain.
2584
+ if (CC.isInvalid () ||
2585
+ (CC.isMacroID () && S.Context .getSourceManager ().isInSystemHeader (
2586
+ S.Context .getSourceManager ().getSpellingLoc (CC))))
2587
+ return ;
2588
+
2580
2589
// Never diagnose implicit casts to bool.
2581
2590
if (Target->isSpecificBuiltinType (BuiltinType::Bool))
2582
2591
return ;
2583
2592
2584
2593
// Strip vector types.
2585
2594
if (isa<VectorType>(Source)) {
2586
2595
if (!isa<VectorType>(Target))
2587
- return DiagnoseImpCast (S, E, T, diag::warn_impcast_vector_scalar);
2596
+ return DiagnoseImpCast (S, E, T, CC, diag::warn_impcast_vector_scalar);
2588
2597
2589
2598
Source = cast<VectorType>(Source)->getElementType ().getTypePtr ();
2590
2599
Target = cast<VectorType>(Target)->getElementType ().getTypePtr ();
@@ -2593,7 +2602,7 @@ void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
2593
2602
// Strip complex types.
2594
2603
if (isa<ComplexType>(Source)) {
2595
2604
if (!isa<ComplexType>(Target))
2596
- return DiagnoseImpCast (S, E, T, diag::warn_impcast_complex_scalar);
2605
+ return DiagnoseImpCast (S, E, T, CC, diag::warn_impcast_complex_scalar);
2597
2606
2598
2607
Source = cast<ComplexType>(Source)->getElementType ().getTypePtr ();
2599
2608
Target = cast<ComplexType>(Target)->getElementType ().getTypePtr ();
@@ -2621,15 +2630,15 @@ void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
2621
2630
return ;
2622
2631
}
2623
2632
2624
- DiagnoseImpCast (S, E, T, diag::warn_impcast_float_precision);
2633
+ DiagnoseImpCast (S, E, T, CC, diag::warn_impcast_float_precision);
2625
2634
}
2626
2635
return ;
2627
2636
}
2628
2637
2629
2638
// If the target is integral, always warn.
2630
2639
if ((TargetBT && TargetBT->isInteger ()))
2631
2640
// TODO: don't warn for integer values?
2632
- DiagnoseImpCast (S, E, T, diag::warn_impcast_float_integer);
2641
+ DiagnoseImpCast (S, E, T, CC, diag::warn_impcast_float_integer);
2633
2642
2634
2643
return ;
2635
2644
}
@@ -2644,8 +2653,8 @@ void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
2644
2653
// People want to build with -Wshorten-64-to-32 and not -Wconversion
2645
2654
// and by god we'll let them.
2646
2655
if (SourceRange.Width == 64 && TargetRange.Width == 32 )
2647
- return DiagnoseImpCast (S, E, T, diag::warn_impcast_integer_64_32);
2648
- return DiagnoseImpCast (S, E, T, diag::warn_impcast_integer_precision);
2656
+ return DiagnoseImpCast (S, E, T, CC, diag::warn_impcast_integer_64_32);
2657
+ return DiagnoseImpCast (S, E, T, CC, diag::warn_impcast_integer_precision);
2649
2658
}
2650
2659
2651
2660
if ((TargetRange.NonNegative && !SourceRange.NonNegative ) ||
@@ -2663,7 +2672,7 @@ void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
2663
2672
*ICContext = true ;
2664
2673
}
2665
2674
2666
- return DiagnoseImpCast (S, E, T, DiagID);
2675
+ return DiagnoseImpCast (S, E, T, CC, DiagID);
2667
2676
}
2668
2677
2669
2678
return ;
@@ -2672,24 +2681,26 @@ void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
2672
2681
void CheckConditionalOperator (Sema &S, ConditionalOperator *E, QualType T);
2673
2682
2674
2683
void CheckConditionalOperand (Sema &S, Expr *E, QualType T,
2675
- bool &ICContext) {
2684
+ SourceLocation CC, bool &ICContext) {
2676
2685
E = E->IgnoreParenImpCasts ();
2677
2686
2678
2687
if (isa<ConditionalOperator>(E))
2679
2688
return CheckConditionalOperator (S, cast<ConditionalOperator>(E), T);
2680
2689
2681
- AnalyzeImplicitConversions (S, E);
2690
+ AnalyzeImplicitConversions (S, E, CC );
2682
2691
if (E->getType () != T)
2683
- return CheckImplicitConversion (S, E, T, &ICContext);
2692
+ return CheckImplicitConversion (S, E, T, CC, &ICContext);
2684
2693
return ;
2685
2694
}
2686
2695
2687
2696
void CheckConditionalOperator (Sema &S, ConditionalOperator *E, QualType T) {
2688
- AnalyzeImplicitConversions (S, E->getCond ());
2697
+ SourceLocation CC = E->getQuestionLoc ();
2698
+
2699
+ AnalyzeImplicitConversions (S, E->getCond (), CC);
2689
2700
2690
2701
bool Suspicious = false ;
2691
- CheckConditionalOperand (S, E->getTrueExpr (), T, Suspicious);
2692
- CheckConditionalOperand (S, E->getFalseExpr (), T, Suspicious);
2702
+ CheckConditionalOperand (S, E->getTrueExpr (), T, CC, Suspicious);
2703
+ CheckConditionalOperand (S, E->getFalseExpr (), T, CC, Suspicious);
2693
2704
2694
2705
// If -Wconversion would have warned about either of the candidates
2695
2706
// for a signedness conversion to the context type...
@@ -2708,10 +2719,10 @@ void CheckConditionalOperator(Sema &S, ConditionalOperator *E, QualType T) {
2708
2719
if (E->getType () != T) {
2709
2720
Suspicious = false ;
2710
2721
CheckImplicitConversion (S, E->getTrueExpr ()->IgnoreParenImpCasts (),
2711
- E->getType (), &Suspicious);
2722
+ E->getType (), CC, &Suspicious);
2712
2723
if (!Suspicious)
2713
2724
CheckImplicitConversion (S, E->getFalseExpr ()->IgnoreParenImpCasts (),
2714
- E->getType (), &Suspicious);
2725
+ E->getType (), CC, &Suspicious);
2715
2726
if (!Suspicious)
2716
2727
return ;
2717
2728
}
@@ -2727,7 +2738,7 @@ void CheckConditionalOperator(Sema &S, ConditionalOperator *E, QualType T) {
2727
2738
// / AnalyzeImplicitConversions - Find and report any interesting
2728
2739
// / implicit conversions in the given expression. There are a couple
2729
2740
// / of competing diagnostics here, -Wconversion and -Wsign-compare.
2730
- void AnalyzeImplicitConversions (Sema &S, Expr *OrigE) {
2741
+ void AnalyzeImplicitConversions (Sema &S, Expr *OrigE, SourceLocation CC ) {
2731
2742
QualType T = OrigE->getType ();
2732
2743
Expr *E = OrigE->IgnoreParenImpCasts ();
2733
2744
@@ -2743,14 +2754,14 @@ void AnalyzeImplicitConversions(Sema &S, Expr *OrigE) {
2743
2754
// The non-canonical typecheck is just an optimization;
2744
2755
// CheckImplicitConversion will filter out dead implicit conversions.
2745
2756
if (E->getType () != T)
2746
- CheckImplicitConversion (S, E, T);
2757
+ CheckImplicitConversion (S, E, T, CC );
2747
2758
2748
2759
// Now continue drilling into this expression.
2749
2760
2750
2761
// Skip past explicit casts.
2751
2762
if (isa<ExplicitCastExpr>(E)) {
2752
2763
E = cast<ExplicitCastExpr>(E)->getSubExpr ()->IgnoreParenImpCasts ();
2753
- return AnalyzeImplicitConversions (S, E);
2764
+ return AnalyzeImplicitConversions (S, E, CC );
2754
2765
}
2755
2766
2756
2767
// Do a somewhat different check with comparison operators.
@@ -2767,17 +2778,22 @@ void AnalyzeImplicitConversions(Sema &S, Expr *OrigE) {
2767
2778
if (isa<SizeOfAlignOfExpr>(E)) return ;
2768
2779
2769
2780
// Now just recurse over the expression's children.
2781
+ CC = E->getExprLoc ();
2770
2782
for (Stmt::child_iterator I = E->child_begin (), IE = E->child_end ();
2771
2783
I != IE; ++I)
2772
- AnalyzeImplicitConversions (S, cast<Expr>(*I));
2784
+ AnalyzeImplicitConversions (S, cast<Expr>(*I), CC );
2773
2785
}
2774
2786
2775
2787
} // end anonymous namespace
2776
2788
2777
2789
// / Diagnoses "dangerous" implicit conversions within the given
2778
2790
// / expression (which is a full expression). Implements -Wconversion
2779
2791
// / and -Wsign-compare.
2780
- void Sema::CheckImplicitConversions (Expr *E) {
2792
+ // /
2793
+ // / \param CC the "context" location of the implicit conversion, i.e.
2794
+ // / the most location of the syntactic entity requiring the implicit
2795
+ // / conversion
2796
+ void Sema::CheckImplicitConversions (Expr *E, SourceLocation CC) {
2781
2797
// Don't diagnose in unevaluated contexts.
2782
2798
if (ExprEvalContexts.back ().Context == Sema::Unevaluated)
2783
2799
return ;
@@ -2786,7 +2802,8 @@ void Sema::CheckImplicitConversions(Expr *E) {
2786
2802
if (E->isTypeDependent () || E->isValueDependent ())
2787
2803
return ;
2788
2804
2789
- AnalyzeImplicitConversions (*this , E);
2805
+ // This is not the right CC for (e.g.) a variable initialization.
2806
+ AnalyzeImplicitConversions (*this , E, CC);
2790
2807
}
2791
2808
2792
2809
// / CheckParmsForFunctionDef - Check that the parameters of the given
0 commit comments