This is an archive of the discontinued LLVM Phabricator instance.

readability-redundant-declaration: fix false positive for static member declaration
ClosedPublic

Authored by danielmarjamaki on Nov 23 2016, 7:35 AM.

Details

Summary

Fix a false positive in the readability-redundant-declaration.

Example code:

struct S {
  static int I;
}:
int S::I;  // <- don't warn

For information, I also ran clang-format on the files and got some extra "unintentional" style updates.

Diff Detail

Repository
rL LLVM

Event Timeline

danielmarjamaki retitled this revision from to readability-redundant-declaration: fix false positive for static member declaration.
danielmarjamaki updated this object.
danielmarjamaki added a reviewer: alexfh.
danielmarjamaki set the repository for this revision to rL LLVM.
danielmarjamaki added a reviewer: hokein.
danielmarjamaki added a subscriber: vmiklos.
hokein edited edge metadata.Nov 23 2016, 9:07 AM

Thanks for the fix.

clang-tidy/readability/RedundantDeclarationCheck.cpp
48 ↗(On Diff #79078)

Any reason to use lexical DC here? I think we should use Prev->getDeclContext() to get the semantic DC.

danielmarjamaki marked an inline comment as done.Nov 25 2016, 5:39 AM
danielmarjamaki added inline comments.
clang-tidy/readability/RedundantDeclarationCheck.cpp
48 ↗(On Diff #79078)

Thanks! No there was no reason, it was a mistake.

danielmarjamaki edited edge metadata.
danielmarjamaki marked an inline comment as done.

Fix review comments

hokein added inline comments.Nov 25 2016, 5:53 AM
clang-tidy/readability/RedundantDeclarationCheck.cpp
22 ↗(On Diff #79297)

Some thoughts about the ast matcher:

Since you only want to match function/variable declarations (not definitions) in this check, we could add unless(isDefintion()) in the matcher which can make the check more efficient.

And for varDecl, I think you should also ignore static variables?

49 ↗(On Diff #79297)

Why do you use getRedeclContext? Doesn't DC->isRecord() work?

clang-tidy/readability/RedundantDeclarationCheck.cpp
22 ↗(On Diff #79297)

Sounds reasonable. Probably I did it wrongly.. I get this now:

.../ASTMatchers.h:3919:44: error: ‘const class clang::NamedDecl’ has no member named ‘isThisDeclarationADefinition’
   return Node.isThisDeclarationADefinition();

I will try to make it work.

Maybe I misunderstood.. I want to warn about redundant static variable declarations in module scope.

static int X;
static int X;
49 ↗(On Diff #79297)

It is just because I don't know all the possible shortcuts. I will try DC->isRecord() but I assume it works too.

hokein added inline comments.Nov 25 2016, 6:55 AM
clang-tidy/readability/RedundantDeclarationCheck.cpp
22 ↗(On Diff #79297)
.../ASTMatchers.h:3919:44: error: ‘const class clang::NamedDecl’ has no member named ‘isThisDeclarationADefinition’
   return Node.isThisDeclarationADefinition();

I see you error. isDefinition() can't apply to nameDecl directly. So you need replicate the machter like namedDecl(anyOf(varDecl(unless(isDefinition())), functionDecl(unless(isDefinition()))) (But you can still simplify it by making unless(isDefinition()) a variable).

Maybe I misunderstood.. I want to warn about redundant static variable declarations in module scope.

hmm... I believe there is a redefinition error in your given case, if you declare "static int X;" twice.

clang-tidy/readability/RedundantDeclarationCheck.cpp
22 ↗(On Diff #79297)

hmm... I believe there is a redefinition error in your given case, if you declare "static int X;" twice.

well.. gcc/clang does not complain about it. hmm.. I don't see a clang-tidy warning about redundant declaration neither.

hokein added inline comments.Nov 25 2016, 7:24 AM
clang-tidy/readability/RedundantDeclarationCheck.cpp
22 ↗(On Diff #79297)

Here is my results:

$  cat test.cpp
static int i;
static int i;
$ clang test.cpp
test.cpp:2:12: error: redefinition of 'i'
static int i;
           ^
test.cpp:1:12: note: previous definition is here
static int i;
           ^
1 error generated.
clang-tidy/readability/RedundantDeclarationCheck.cpp
22 ↗(On Diff #79297)

Yes. Well there is C also.

$  cat test.cpp
static int i;
static int i;
$ clang -fsyntax-only test.cpp
test.cpp:2:12: error: redefinition of 'i'
static int i;
           ^
test.cpp:1:12: note: previous definition is here
static int i;
           ^
1 error generated.
$ cp test.cpp test.c
$ clang -fsyntax-only test.c
$
danielmarjamaki removed rL LLVM as the repository for this revision.

Fixed review comments

alexfh requested changes to this revision.Jan 24 2017, 10:08 AM
alexfh added inline comments.
test/clang-tidy/readability-redundant-declaration.cpp
22 ↗(On Diff #79297)

This CHECK line will match any empty line, which is not what you want. Add a comment after the declaration and match it anchored to the start of line. Same elsewhere.

This revision now requires changes to proceed.Jan 24 2017, 10:08 AM
danielmarjamaki edited edge metadata.

Fix review comment about CHECK-FIXES regexp pattern.

hokein accepted this revision.Feb 23 2017, 11:54 AM

Sorry for the delay. LGTM, thanks.

Nit: Please mark the reviewers' comments Done in Phabricator once you address them, it will make review stuff easier.

This revision was automatically updated to reflect the committed changes.
jhasse added a subscriber: jhasse.Jul 26 2017, 5:37 AM