Author: alexfh Date: Fri Jul 28 05:46:02 2017 New Revision: 309379 URL: http://llvm.org/viewvc/llvm-project?rev=309379&view=rev Log: [clang-tidy] readability-redundant-declaration: ignore friends and macros
Added: clang-tools-extra/trunk/test/clang-tidy/readability-redundant-declaration-ignore-macros.cpp Modified: clang-tools-extra/trunk/clang-tidy/readability/RedundantDeclarationCheck.cpp clang-tools-extra/trunk/clang-tidy/readability/RedundantDeclarationCheck.h clang-tools-extra/trunk/test/clang-tidy/readability-redundant-declaration.cpp Modified: clang-tools-extra/trunk/clang-tidy/readability/RedundantDeclarationCheck.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/readability/RedundantDeclarationCheck.cpp?rev=309379&r1=309378&r2=309379&view=diff ============================================================================== --- clang-tools-extra/trunk/clang-tidy/readability/RedundantDeclarationCheck.cpp (original) +++ clang-tools-extra/trunk/clang-tidy/readability/RedundantDeclarationCheck.cpp Fri Jul 28 05:46:02 2017 @@ -18,11 +18,16 @@ namespace clang { namespace tidy { namespace readability { +RedundantDeclarationCheck::RedundantDeclarationCheck(StringRef Name, + ClangTidyContext *Context) + : ClangTidyCheck(Name, Context), + IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", true)) {} + void RedundantDeclarationCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher( - namedDecl( - anyOf(varDecl(unless(isDefinition())), - functionDecl(unless(anyOf(isDefinition(), isDefaulted()))))) + namedDecl(anyOf(varDecl(unless(isDefinition())), + functionDecl(unless(anyOf(isDefinition(), isDefaulted(), + hasParent(friendDecl())))))) .bind("Decl"), this); } @@ -36,6 +41,13 @@ void RedundantDeclarationCheck::check(co return; if (Prev->getLocation() == D->getLocation()) return; + if (IgnoreMacros && + (D->getLocation().isMacroID() || Prev->getLocation().isMacroID())) + return; + // Don't complain when the previous declaration is a friend declaration. + for (const auto &Parent : Result.Context->getParents(*Prev)) + if (Parent.get<FriendDecl>()) + return; const SourceManager &SM = *Result.SourceManager; Modified: clang-tools-extra/trunk/clang-tidy/readability/RedundantDeclarationCheck.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/readability/RedundantDeclarationCheck.h?rev=309379&r1=309378&r2=309379&view=diff ============================================================================== --- clang-tools-extra/trunk/clang-tidy/readability/RedundantDeclarationCheck.h (original) +++ clang-tools-extra/trunk/clang-tidy/readability/RedundantDeclarationCheck.h Fri Jul 28 05:46:02 2017 @@ -22,10 +22,12 @@ namespace readability { /// http://clang.llvm.org/extra/clang-tidy/checks/readability-redundant-declaration.html class RedundantDeclarationCheck : public ClangTidyCheck { public: - RedundantDeclarationCheck(StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context) {} + RedundantDeclarationCheck(StringRef Name, ClangTidyContext *Context); void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + +private: + const bool IgnoreMacros; }; } // namespace readability Added: clang-tools-extra/trunk/test/clang-tidy/readability-redundant-declaration-ignore-macros.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/readability-redundant-declaration-ignore-macros.cpp?rev=309379&view=auto ============================================================================== --- clang-tools-extra/trunk/test/clang-tidy/readability-redundant-declaration-ignore-macros.cpp (added) +++ clang-tools-extra/trunk/test/clang-tidy/readability-redundant-declaration-ignore-macros.cpp Fri Jul 28 05:46:02 2017 @@ -0,0 +1,22 @@ +// RUN: %check_clang_tidy %s readability-redundant-declaration %t -- \ +// RUN: -config="{CheckOptions: \ +// RUN: [{key: readability-redundant-declaration.IgnoreMacros, \ +// RUN: value: 1}]}" \ +// RUN: -- -std=c++11 + +extern int Xyz; +extern int Xyz; // Xyz +// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: redundant 'Xyz' declaration [readability-redundant-declaration] +// CHECK-FIXES: {{^}}// Xyz{{$}} + +namespace macros { +#define DECLARE(x) extern int x +#define DEFINE(x) extern int x; int x = 42 +DECLARE(test); +DEFINE(test); +// CHECK-FIXES: {{^}}#define DECLARE(x) extern int x{{$}} +// CHECK-FIXES: {{^}}#define DEFINE(x) extern int x; int x = 42{{$}} +// CHECK-FIXES: {{^}}DECLARE(test);{{$}} +// CHECK-FIXES: {{^}}DEFINE(test);{{$}} + +} // namespace macros Modified: clang-tools-extra/trunk/test/clang-tidy/readability-redundant-declaration.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/readability-redundant-declaration.cpp?rev=309379&r1=309378&r2=309379&view=diff ============================================================================== --- clang-tools-extra/trunk/test/clang-tidy/readability-redundant-declaration.cpp (original) +++ clang-tools-extra/trunk/test/clang-tidy/readability-redundant-declaration.cpp Fri Jul 28 05:46:02 2017 @@ -1,4 +1,8 @@ -// RUN: %check_clang_tidy %s readability-redundant-declaration %t +// RUN: %check_clang_tidy %s readability-redundant-declaration %t -- \ +// RUN: -config="{CheckOptions: \ +// RUN: [{key: readability-redundant-declaration.IgnoreMacros, \ +// RUN: value: 0}]}" \ +// RUN: -- -std=c++11 extern int Xyz; extern int Xyz; // Xyz @@ -42,3 +46,25 @@ struct C2 { template <class T> C2<T>::C2() = default; + +void best_friend(); + +struct Friendly { + friend void best_friend(); + friend void enemy(); +}; + +void enemy(); + +namespace macros { +#define DECLARE(x) extern int x +#define DEFINE(x) extern int x; int x = 42 +DECLARE(test); +DEFINE(test); +// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant 'test' declaration +// CHECK-FIXES: {{^}}#define DECLARE(x) extern int x{{$}} +// CHECK-FIXES: {{^}}#define DEFINE(x) extern int x; int x = 42{{$}} +// CHECK-FIXES: {{^}}DECLARE(test);{{$}} +// CHECK-FIXES: {{^}}DEFINE(test);{{$}} + +} // namespace macros _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits