llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

False positive for -Wunused-variable in function template when calling nontrivial ctor with __func__

Open llvmbot opened this issue 13 years ago • 5 comments

Bugzilla Link 15532
Version trunk
OS All
Reporter LLVM Bugzilla Contributor
CC @dwblaikie

Extended Description

class Parser {
  class Tracer {
   public:
    Tracer(const char*, Parser*);
  };
  template <char delim> void ParseFoo() {
    Tracer trace(__func__, this);
  }
  void ParseBar() {
    Tracer trace(__func__, this);
  }
};

We warn on the 'trace' variable in ParseFoo, but not the one in ParseBar. The Decl for 'trace' in ParseBar looks like:

VarDecl 0x3c83d70 </tmp/in.ii:10:5, col:32> trace 'class Parser::Tracer'
`-CXXConstructExpr 0x3c84268 <col:12, col:32> 'class Parser::Tracer' 'void (const char *, class Parser *)'
  |-ImplicitCastExpr 0x3c84250 <col:18> 'const char *' <ArrayToPointerDecay>
  | `-PredefinedExpr 0x3c83e48 <col:18> 'const char [9]' lvalue __func__
  `-CXXThisExpr 0x3c83e60 <col:28> 'class Parser *' this

So, in ShouldDiagnoseUnusedDecl, we correctly determine that the constructor is non-elidable and nontrivial, and return false.

On the other hand, the 'trace' in ParseFoo looks like this:

VarDecl 0x3c83c30 </tmp/in.ii:7:5, col:32> trace 'class Parser::Tracer'
`-ParenListExpr 0x3c83cf0 <col:17, col:32> 'NULL TYPE'
  |-PredefinedExpr 0x3c83c88 <col:18> '<dependent type>' lvalue __func__
  `-CXXThisExpr 0x3c83ca0 <col:28> 'class Parser *' this

We don't find a CXXConstructExpr, so ShouldDiagnoseUnusedDecl returns true. Oops.

llvmbot avatar Mar 18 '13 00:03 llvmbot

mentioned in issue llvm/llvm-bugzilla-archive#17014

dwblaikie avatar Nov 26 '21 18:11 dwblaikie

A reduced version of llvm/llvm-bugzilla-archive#17014 :

struct foo {
  foo(int x) {}
};
template<typename T>
void func(T t)  {
  foo false_positive_unsused_variable(t);
}

int main(int argc, char **argv) {
  func(3);
}

shows a similar AST for the template, both in the instantiation and the original template:

FunctionTemplateDecl 0x5d4e770 <line:4:1, line:7:1> func
|-TemplateTypeParmDecl 0x5d4e510 <line:4:10, col:19> typename T
|-FunctionDecl 0x5d4e6d0 <line:5:1, line:7:1> func 'void (T)'
| |-ParmVarDecl 0x5d4e5d0 <line:5:11, col:13> t 'T'
| `-CompoundStmt 0x5d4e8d0 <col:17, line:7:1>
|   `-DeclStmt 0x5d4e8b8 <line:6:3, col:41>
|     `-VarDecl 0x5d4e800 <col:3, col:40> false_positive_unsused_variable 'struct foo'
|       `-ParenListExpr 0x5d4e888 <col:38, col:40> 'NULL TYPE'
|         `-DeclRefExpr 0x5d4e7c0 <col:39> 'T' lvalue ParmVar 0x5d4e5d0 't' 'T'
`-FunctionDecl 0x5d4ed90 <line:5:1, line:7:1> func 'void (int)'
  |-TemplateArgument type 'int'
  |-ParmVarDecl 0x5d4ecd0 <line:5:11, col:13> t 'int':'int'
  `-CompoundStmt 0x5d4f268 <col:17, line:7:1>
    `-DeclStmt 0x5d4f250 <line:6:3, col:41>
      `-VarDecl 0x5d4ef80 <col:3, col:40> false_positive_unsused_variable 'struct foo'
        `-CXXConstructExpr 0x5d4f218 <col:7, col:40> 'struct foo' 'void (int)'
          `-ImplicitCastExpr 0x5d4f200 <col:39> 'int':'int' <LValueToRValue>
            `-DeclRefExpr 0x5d4efd8 <col:39> 'int':'int' lvalue ParmVar 0x5d4ecd0 't' 'int':'int'

Though I assume it's the template pattern, not the specialization, that we're looking at for this warning (since we wouldn't want to accidentally find a trivial ctor in one template specialization & consider the variable unused even if it was a non-trivial ctor in another specialization).

dwblaikie avatar Aug 27 '13 22:08 dwblaikie

For things that might be a copy/move ctor (or trivial braced init) but are still dependent, we can't provide this warning if the copy/move ctor is trivial (because some instantiations of the template may use the trivial copy/move ctor where we want to warn, and others may use different (necessarily user defined) ctors that should suppress the warning)

dwblaikie avatar Aug 27 '13 22:08 dwblaikie

*** Bug llvm/llvm-bugzilla-archive#17014 has been marked as a duplicate of this bug. ***

dwblaikie avatar Aug 27 '13 21:08 dwblaikie

Bisected: the reported behavior no longer reproduces starting with https://github.com/llvm/llvm-project/commit/654c0daef75a41eede06b413a5ecdd8ad2640240.

Godbolt: https://godbolt.org/z/8aMEo45Mv

  • Clang 9.0.1: Reports unused variable 'trace' at line 7
  • Clang 10.0.0: Compiles without an unused-variable warning for trace
  • Clang (trunk): Compiles without the trace warning

This is only an observed change in behavior. I have not checked whether this commit addresses the cause of this report or only stops it from reproducing; whether that resolves the issue is up to the maintainers.

Prepared with assistance from Codex.

hongtaihu avatar Nov 08 '26 13:11 hongtaihu