xref: /llvm-project/clang-tools-extra/clang-tidy/google/DefaultArgumentsCheck.cpp (revision 7d2ea6c422d3f5712b7253407005e1a465a76946)
111726686SHaojian Wu //===--- DefaultArgumentsCheck.cpp - clang-tidy----------------------------===//
211726686SHaojian Wu //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
611726686SHaojian Wu //
711726686SHaojian Wu //===----------------------------------------------------------------------===//
811726686SHaojian Wu 
911726686SHaojian Wu #include "DefaultArgumentsCheck.h"
1011726686SHaojian Wu #include "clang/AST/ASTContext.h"
1111726686SHaojian Wu #include "clang/ASTMatchers/ASTMatchFinder.h"
1211726686SHaojian Wu 
1311726686SHaojian Wu using namespace clang::ast_matchers;
1411726686SHaojian Wu 
15*7d2ea6c4SCarlos Galvez namespace clang::tidy::google {
1611726686SHaojian Wu 
registerMatchers(MatchFinder * Finder)1711726686SHaojian Wu void DefaultArgumentsCheck::registerMatchers(MatchFinder *Finder) {
1811726686SHaojian Wu   Finder->addMatcher(
1911726686SHaojian Wu       cxxMethodDecl(anyOf(isOverride(), isVirtual()),
2011726686SHaojian Wu                     hasAnyParameter(parmVarDecl(hasInitializer(expr()))))
2111726686SHaojian Wu           .bind("Decl"),
2211726686SHaojian Wu       this);
2311726686SHaojian Wu }
2411726686SHaojian Wu 
check(const MatchFinder::MatchResult & Result)2511726686SHaojian Wu void DefaultArgumentsCheck::check(const MatchFinder::MatchResult &Result) {
2611726686SHaojian Wu   const auto *MatchedDecl = Result.Nodes.getNodeAs<CXXMethodDecl>("Decl");
2711726686SHaojian Wu   diag(MatchedDecl->getLocation(),
2811726686SHaojian Wu        "default arguments on virtual or override methods are prohibited");
2911726686SHaojian Wu }
3011726686SHaojian Wu 
31*7d2ea6c4SCarlos Galvez } // namespace clang::tidy::google
32