[clang-tools-extra] r268919 - [clang-tidy] new google-default-arguments check
Haojian Wu via cfe-commits
cfe-commits at lists.llvm.org
Mon May 9 06:43:59 PDT 2016
Author: hokein
Date: Mon May 9 08:43:58 2016
New Revision: 268919
URL: http://llvm.org/viewvc/llvm-project?rev=268919&view=rev
Log:
[clang-tidy] new google-default-arguments check
Summary:
To check the google style guide rule here:
https://google.github.io/styleguide/cppguide.html#Default_Arguments
Patch by Clement Courbet!
Reviewers: hokein
Subscribers: cfe-commits
Differential Revision: http://reviews.llvm.org/D19534
Added:
clang-tools-extra/trunk/clang-tidy/google/DefaultArgumentsCheck.cpp
clang-tools-extra/trunk/clang-tidy/google/DefaultArgumentsCheck.h
clang-tools-extra/trunk/docs/clang-tidy/checks/google-default-arguments.rst
clang-tools-extra/trunk/test/clang-tidy/google-default-arguments.cpp
Modified:
clang-tools-extra/trunk/clang-tidy/google/CMakeLists.txt
clang-tools-extra/trunk/clang-tidy/google/GoogleTidyModule.cpp
clang-tools-extra/trunk/docs/ReleaseNotes.rst
clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst
Modified: clang-tools-extra/trunk/clang-tidy/google/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/google/CMakeLists.txt?rev=268919&r1=268918&r2=268919&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/google/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/clang-tidy/google/CMakeLists.txt Mon May 9 08:43:58 2016
@@ -2,6 +2,7 @@ set(LLVM_LINK_COMPONENTS support)
add_clang_library(clangTidyGoogleModule
AvoidCStyleCastsCheck.cpp
+ DefaultArgumentsCheck.cpp
ExplicitConstructorCheck.cpp
ExplicitMakePairCheck.cpp
GlobalNamesInHeadersCheck.cpp
Added: clang-tools-extra/trunk/clang-tidy/google/DefaultArgumentsCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/google/DefaultArgumentsCheck.cpp?rev=268919&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/google/DefaultArgumentsCheck.cpp (added)
+++ clang-tools-extra/trunk/clang-tidy/google/DefaultArgumentsCheck.cpp Mon May 9 08:43:58 2016
@@ -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<CXXMethodDecl>("Decl");
+ diag(MatchedDecl->getLocation(),
+ "default arguments on virtual or override methods are prohibited");
+}
+
+} // namespace google
+} // namespace tidy
+} // namespace clang
Added: clang-tools-extra/trunk/clang-tidy/google/DefaultArgumentsCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/google/DefaultArgumentsCheck.h?rev=268919&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/google/DefaultArgumentsCheck.h (added)
+++ clang-tools-extra/trunk/clang-tidy/google/DefaultArgumentsCheck.h Mon May 9 08:43:58 2016
@@ -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
Modified: clang-tools-extra/trunk/clang-tidy/google/GoogleTidyModule.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/google/GoogleTidyModule.cpp?rev=268919&r1=268918&r2=268919&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/google/GoogleTidyModule.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/google/GoogleTidyModule.cpp Mon May 9 08:43:58 2016
@@ -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 @@ public:
"google-build-namespaces");
CheckFactories.registerCheck<build::UsingNamespaceDirectiveCheck>(
"google-build-using-namespace");
+ CheckFactories.registerCheck<DefaultArgumentsCheck>(
+ "google-default-arguments");
CheckFactories.registerCheck<ExplicitConstructorCheck>(
"google-explicit-constructor");
CheckFactories.registerCheck<runtime::IntegerTypesCheck>(
Modified: clang-tools-extra/trunk/docs/ReleaseNotes.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/ReleaseNotes.rst?rev=268919&r1=268918&r2=268919&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/ReleaseNotes.rst (original)
+++ clang-tools-extra/trunk/docs/ReleaseNotes.rst Mon May 9 08:43:58 2016
@@ -100,6 +100,11 @@ identified. The improvements since the
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
+ <http://clang.llvm.org/extra/clang-tidy/checks/google-default-arguments.html>`_ check
+
+ Flags default arguments in virtual methods.
+
- New `misc-dangling-handle
<http://clang.llvm.org/extra/clang-tidy/checks/misc-dangling-handle.html>`_ check
Added: clang-tools-extra/trunk/docs/clang-tidy/checks/google-default-arguments.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/google-default-arguments.rst?rev=268919&view=auto
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/google-default-arguments.rst (added)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/google-default-arguments.rst Mon May 9 08:43:58 2016
@@ -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
Modified: clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst?rev=268919&r1=268918&r2=268919&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst (original)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst Mon May 9 08:43:58 2016
@@ -33,6 +33,7 @@ Clang-Tidy Checks
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) <google-readability-braces-around-statements>
Added: clang-tools-extra/trunk/test/clang-tidy/google-default-arguments.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/google-default-arguments.cpp?rev=268919&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/google-default-arguments.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/google-default-arguments.cpp Mon May 9 08:43:58 2016
@@ -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);
+};
More information about the cfe-commits
mailing list