[cfe-commits] [PATCH] Disable -Wtautological-compare in template instantiations.

David Blaikie dblaikie at gmail.com
Mon Nov 19 09:53:11 PST 2012


It would be nicer if we could build into the constant evaluator some
kind of result that indicated that the expression is dependent (if we
skipped over a SubstNonTypeTemplateParameter while evaluating the
expression (not sure if there's a way to detect the other, perhaps
more common cases of foo<T>::constant, though)) - this would give us
better fidelity (we could still warn on non-dependent cases) & that
machinery will be useful for other cases (like CFG construction)

Earlier this year I implemented part of this sort of thing, except for
'sizeof' rather than for templates, in an effort to improve
-Wunreachable-code - this change has not yet been committed (it came
with some CFG changes that Ted had feedback on & I haven't had a
chance to follow up on), but could be used as a rough guide.

Alternatively: could we possibly just not run this warning/analysis on
template specializations and only run it on template patterns? Then it
would be trivial: any expression that could be constant evaluated
wasn't dependent anyway. (this is what I proposed (& still propose)
doing for -Wunreachable-code in templates: simply always do it on the
template pattern & never on the template specialization)

On Mon, Nov 19, 2012 at 9:42 AM, Richard Trieu <rtrieu at google.com> wrote:
> http://llvm-reviews.chandlerc.com/D128
>
> Files:
>   test/SemaCXX/compare.cpp
>   lib/Sema/SemaChecking.cpp
>
> Index: test/SemaCXX/compare.cpp
> ===================================================================
> --- test/SemaCXX/compare.cpp
> +++ test/SemaCXX/compare.cpp
> @@ -348,3 +348,16 @@
>    (void)((E)x == 1);
>    (void)((E)x == -1);
>  }
> +
> +template <typename T>
> +void template9 (T t, unsigned u) {
> +  (void)(t >= 0);
> +  (void)(u >= 0);  // expected-warning{{true}}
> +
> +  (void)(t != 0x100000000);
> +  (void)(u != 0x100000000);  // expected-warning{{true}}
> +}
> +
> +void test9() {
> +  template9<unsigned>(1,1);
> +}
> Index: lib/Sema/SemaChecking.cpp
> ===================================================================
> --- lib/Sema/SemaChecking.cpp
> +++ lib/Sema/SemaChecking.cpp
> @@ -4309,6 +4309,9 @@
>    if (E->isValueDependent())
>      return;
>
> +  if (!S.ActiveTemplateInstantiations.empty())
> +    return;
> +
>    if (op == BO_LT && IsZero(S, E->getRHS())) {
>      S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison)
>        << "< 0" << "false" << HasEnumType(E->getLHS())
> @@ -4336,6 +4339,9 @@
>    if (Value == 0)
>      return;
>
> +  if (!S.ActiveTemplateInstantiations.empty())
> +    return;
> +
>    BinaryOperatorKind op = E->getOpcode();
>    QualType OtherT = Other->getType();
>    QualType ConstantT = Constant->getType();
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>



More information about the cfe-commits mailing list