diff options
| author | Haojian Wu <hokein@google.com> | 2016-05-09 13:43:58 +0000 |
|---|---|---|
| committer | Haojian Wu <hokein@google.com> | 2016-05-09 13:43:58 +0000 |
| commit | 11726686820358e49dba86b0ee07002bd54965b6 (patch) | |
| tree | 636353529305d03d0b492e080e3204d207883282 /clang-tools-extra/clang-tidy/google | |
| parent | e473dc937f59a9fefe450725ca4774a5245826a2 (diff) | |
| download | bcm5719-llvm-11726686820358e49dba86b0ee07002bd54965b6.tar.gz bcm5719-llvm-11726686820358e49dba86b0ee07002bd54965b6.zip | |
[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
llvm-svn: 268919
Diffstat (limited to 'clang-tools-extra/clang-tidy/google')
4 files changed, 74 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-tidy/google/CMakeLists.txt b/clang-tools-extra/clang-tidy/google/CMakeLists.txt index 42274dcb717..efe3b3cde9c 100644 --- a/clang-tools-extra/clang-tidy/google/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/google/CMakeLists.txt @@ -2,6 +2,7 @@ set(LLVM_LINK_COMPONENTS support) add_clang_library(clangTidyGoogleModule AvoidCStyleCastsCheck.cpp + DefaultArgumentsCheck.cpp ExplicitConstructorCheck.cpp ExplicitMakePairCheck.cpp GlobalNamesInHeadersCheck.cpp diff --git a/clang-tools-extra/clang-tidy/google/DefaultArgumentsCheck.cpp b/clang-tools-extra/clang-tidy/google/DefaultArgumentsCheck.cpp new file mode 100644 index 00000000000..ccbd870a6ec --- /dev/null +++ b/clang-tools-extra/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<CXXMethodDecl>("Decl"); + diag(MatchedDecl->getLocation(), + "default arguments on virtual or override methods are prohibited"); +} + +} // namespace google +} // namespace tidy +} // namespace clang diff --git a/clang-tools-extra/clang-tidy/google/DefaultArgumentsCheck.h b/clang-tools-extra/clang-tidy/google/DefaultArgumentsCheck.h new file mode 100644 index 00000000000..1457a093db6 --- /dev/null +++ b/clang-tools-extra/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 diff --git a/clang-tools-extra/clang-tidy/google/GoogleTidyModule.cpp b/clang-tools-extra/clang-tidy/google/GoogleTidyModule.cpp index 1281e15ff7b..49f879ccd9d 100644 --- a/clang-tools-extra/clang-tidy/google/GoogleTidyModule.cpp +++ b/clang-tools-extra/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 @@ 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>( |

