Index: clang-tidy/google/CMakeLists.txt =================================================================== --- clang-tidy/google/CMakeLists.txt +++ clang-tidy/google/CMakeLists.txt @@ -2,6 +2,7 @@ add_clang_library(clangTidyGoogleModule AvoidCStyleCastsCheck.cpp + DefaultArgumentsCheck.cpp ExplicitConstructorCheck.cpp ExplicitMakePairCheck.cpp GlobalNamesInHeadersCheck.cpp Index: clang-tidy/google/DefaultArgumentsCheck.h =================================================================== --- /dev/null +++ clang-tidy/google/DefaultArgumentsCheck.h @@ -0,0 +1,34 @@ +//===--- DefaultArgumentsCheck.h - clang-tidy--------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_DEFAULT_ARGUMENTS_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_DEFAULT_ARGUMENTS_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace google { + +/// Checks that default parameters are not given for virtual methods. +/// +/// See https://google.github.io/styleguide/cppguide.html#Default_Arguments +class DefaultArgumentsCheck : public ClangTidyCheck { +public: + DefaultArgumentsCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace google +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_DEFAULT_ARGUMENTS_H Index: clang-tidy/google/DefaultArgumentsCheck.cpp =================================================================== --- /dev/null +++ clang-tidy/google/DefaultArgumentsCheck.cpp @@ -0,0 +1,36 @@ +//===--- DefaultArgumentsCheck.cpp - clang-tidy----------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "DefaultArgumentsCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace google { + +void DefaultArgumentsCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + cxxMethodDecl(anyOf(isOverride(), isVirtual()), + hasAnyParameter(parmVarDecl(hasInitializer(expr())))) + .bind("Decl"), + this); +} + +void DefaultArgumentsCheck::check(const MatchFinder::MatchResult &Result) { + const auto *MatchedDecl = Result.Nodes.getNodeAs("Decl"); + diag(MatchedDecl->getLocation(), + "default argument given for virtual or override method."); +} + +} // namespace google +} // namespace tidy +} // namespace clang Index: clang-tidy/google/GoogleTidyModule.cpp =================================================================== --- clang-tidy/google/GoogleTidyModule.cpp +++ clang-tidy/google/GoogleTidyModule.cpp @@ -15,6 +15,7 @@ #include "../readability/NamespaceCommentCheck.h" #include "../readability/RedundantSmartptrGetCheck.h" #include "AvoidCStyleCastsCheck.h" +#include "DefaultArgumentsCheck.h" #include "ExplicitConstructorCheck.h" #include "ExplicitMakePairCheck.h" #include "GlobalNamesInHeadersCheck.h" @@ -36,6 +37,8 @@ class GoogleModule : public ClangTidyModule { public: void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { + CheckFactories.registerCheck( + "google-default-arguments"); CheckFactories.registerCheck( "google-build-explicit-make-pair"); CheckFactories.registerCheck( Index: docs/ReleaseNotes.rst =================================================================== --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -86,6 +86,11 @@ Flags user-defined constructor definitions that do not initialize all builtin and pointer fields which leaves their memory in an undefined state. +- New `google-default-arguments + `_ check + + Flags default arguments in vitual methods. + - New `misc-dangling-handle `_ check Index: docs/clang-tidy/checks/google-default-arguments.rst =================================================================== --- /dev/null +++ docs/clang-tidy/checks/google-default-arguments.rst @@ -0,0 +1,8 @@ +.. title:: clang-tidy - google-default-arguments + +google-default-arguments +======================== + +Checks that default parameters are not given for virtual methods. + +See https://google.github.io/styleguide/cppguide.html#Default_Arguments Index: docs/clang-tidy/checks/list.rst =================================================================== --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -30,6 +30,7 @@ google-build-explicit-make-pair google-build-namespaces google-build-using-namespace + google-default-arguments google-explicit-constructor google-global-names-in-headers google-readability-braces-around-statements (redirects to readability-braces-around-statements) @@ -75,7 +76,7 @@ misc-string-literal-with-embedded-nul misc-suspicious-missing-comma misc-suspicious-semicolon - misc-suspicious-string-compare + misc-suspicious-string-compare misc-swapped-arguments misc-throw-by-value-catch-by-reference misc-undelegated-constructor Index: test/clang-tidy/google-default-arguments.cpp =================================================================== --- /dev/null +++ test/clang-tidy/google-default-arguments.cpp @@ -0,0 +1,29 @@ +// RUN: %check_clang_tidy %s google-default-arguments %t + +struct A { + virtual void f(int I, int J = 3); + // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: default argument given for virtual or override method +}; + +struct B : public A { + void f(int I, int J = 5); + // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: default argument given for virtual or override method +}; + +struct C : public B { + void f(int I, int J = 5) override; + // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: default argument given for virtual or override method +}; + +// Negatives. +struct D : public B { + void f(int I, int J) override; +}; + +struct X { + void f(int I, int J = 3); +}; + +struct Y : public X { + void f(int I, int J = 5); +};