r315811 - Re-land r315787, "[Sema] Warn about unused variables if we can constant evaluate the initializer."

Benjamin Kramer via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 19 12:07:31 PDT 2017


Fixed in r316177.

On Thu, Oct 19, 2017 at 5:57 PM, Benjamin Kramer <benny.kra at gmail.com> wrote:
> We should check VD->getInit()->isValueDependent() before we call
> evaluateValue. I wasn't able to come up with a test case that triggers
> the assert though :(
>
> On Thu, Oct 19, 2017 at 5:45 PM, Alexander Kornienko <alexfh at google.com> wrote:
>>
>>
>> On Sat, Oct 14, 2017 at 5:59 PM, Benjamin Kramer via cfe-commits
>> <cfe-commits at lists.llvm.org> wrote:
>>>
>>> Author: d0k
>>> Date: Sat Oct 14 08:59:34 2017
>>> New Revision: 315811
>>>
>>> URL: http://llvm.org/viewvc/llvm-project?rev=315811&view=rev
>>> Log:
>>> Re-land r315787, "[Sema] Warn about unused variables if we can constant
>>> evaluate the initializer."
>>>
>>> The warnings in libc++ tests were fixed in the meantime.
>>>
>>> Modified:
>>>     cfe/trunk/lib/Sema/SemaDecl.cpp
>>>     cfe/trunk/test/SemaCXX/warn-unused-variables.cpp
>>>
>>> Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
>>> URL:
>>> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=315811&r1=315810&r2=315811&view=diff
>>>
>>> ==============================================================================
>>> --- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
>>> +++ cfe/trunk/lib/Sema/SemaDecl.cpp Sat Oct 14 08:59:34 2017
>>> @@ -1723,7 +1723,8 @@ static bool ShouldDiagnoseUnusedDecl(con
>>>              dyn_cast<CXXConstructExpr>(Init);
>>>            if (Construct && !Construct->isElidable()) {
>>>              CXXConstructorDecl *CD = Construct->getConstructor();
>>> -            if (!CD->isTrivial() && !RD->hasAttr<WarnUnusedAttr>())
>>> +            if (!CD->isTrivial() && !RD->hasAttr<WarnUnusedAttr>() &&
>>> +                !VD->evaluateValue())
>>
>>
>> The evaluateValue call above causes an assertion failure on
>> instantiation-dependent values:
>> llvm/tools/clang/lib/AST/Decl.cpp:2196 in clang::APValue
>> *clang::VarDecl::evaluateValue(SmallVectorImpl<clang::PartialDiagnosticAt>
>> &) const: !Init->isValueDependent()
>>
>> I'm not sure why evaluateValue uses an assertion instead of outputting a
>> note and returning nullptr, but the assertion can be avoided on the caller
>> site as well.
>>
>> Working on a reduced test case...
>>
>>>
>>>                return false;
>>>            }
>>>          }
>>>
>>> Modified: cfe/trunk/test/SemaCXX/warn-unused-variables.cpp
>>> URL:
>>> http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-unused-variables.cpp?rev=315811&r1=315810&r2=315811&view=diff
>>>
>>> ==============================================================================
>>> --- cfe/trunk/test/SemaCXX/warn-unused-variables.cpp (original)
>>> +++ cfe/trunk/test/SemaCXX/warn-unused-variables.cpp Sat Oct 14 08:59:34
>>> 2017
>>> @@ -1,4 +1,5 @@
>>>  // RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label
>>> -Wno-c++1y-extensions -verify %s
>>> +// RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label
>>> -Wno-c++1y-extensions -verify -std=c++11 %s
>>>  template<typename T> void f() {
>>>    T t;
>>>    t = 17;
>>> @@ -194,3 +195,35 @@ void test() {
>>>  }
>>>
>>>  }
>>> +
>>> +#if __cplusplus >= 201103L
>>> +namespace with_constexpr {
>>> +template <typename T>
>>> +struct Literal {
>>> +  T i;
>>> +  Literal() = default;
>>> +  constexpr Literal(T i) : i(i) {}
>>> +};
>>> +
>>> +struct NoLiteral {
>>> +  int i;
>>> +  NoLiteral() = default;
>>> +  constexpr NoLiteral(int i) : i(i) {}
>>> +  ~NoLiteral() {}
>>> +};
>>> +
>>> +static Literal<int> gl1;          // expected-warning {{unused variable
>>> 'gl1'}}
>>> +static Literal<int> gl2(1);       // expected-warning {{unused variable
>>> 'gl2'}}
>>> +static const Literal<int> gl3(0); // expected-warning {{unused variable
>>> 'gl3'}}
>>> +
>>> +template <typename T>
>>> +void test(int i) {
>>> +  Literal<int> l1;     // expected-warning {{unused variable 'l1'}}
>>> +  Literal<int> l2(42); // expected-warning {{unused variable 'l2'}}
>>> +  Literal<int> l3(i);  // no-warning
>>> +  Literal<T> l4(0);    // no-warning
>>> +  NoLiteral nl1;       // no-warning
>>> +  NoLiteral nl2(42);   // no-warning
>>> +}
>>> +}
>>> +#endif
>>>
>>>
>>> _______________________________________________
>>> cfe-commits mailing list
>>> cfe-commits at lists.llvm.org
>>> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
>>
>>


More information about the cfe-commits mailing list