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 arguments on virtual or override methods are prohibited"); +} + +} // 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" @@ -42,6 +43,8 @@ "google-build-namespaces"); CheckFactories.registerCheck( "google-build-using-namespace"); + CheckFactories.registerCheck( + "google-default-arguments"); CheckFactories.registerCheck( "google-explicit-constructor"); CheckFactories.registerCheck( Index: docs/ReleaseNotes.rst =================================================================== --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -100,6 +100,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 virtual 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 arguments 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 @@ -33,6 +33,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) 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 arguments on virtual or override methods are prohibited [google-default-arguments] +}; + +struct B : public A { + void f(int I, int J = 5); + // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: default arguments on virtual or override methods are prohibited +}; + +struct C : public B { + void f(int I, int J = 5) override; + // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: default arguments on virtual or override methods are prohibited +}; + +// 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); +};