xref: /llvm-project/clang-tools-extra/clang-tidy/cert/VariadicFunctionDefCheck.cpp (revision 7d2ea6c422d3f5712b7253407005e1a465a76946)
1ffe9f00cSFangrui Song //===-- VariadicFunctionDefCheck.cpp - clang-tidy -------------------------===//
246bc3047SAaron Ballman //
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
646bc3047SAaron Ballman //
746bc3047SAaron Ballman //===----------------------------------------------------------------------===//
846bc3047SAaron Ballman 
946bc3047SAaron Ballman #include "VariadicFunctionDefCheck.h"
1046bc3047SAaron Ballman #include "clang/AST/ASTContext.h"
1146bc3047SAaron Ballman #include "clang/ASTMatchers/ASTMatchFinder.h"
1246bc3047SAaron Ballman 
1346bc3047SAaron Ballman using namespace clang::ast_matchers;
1446bc3047SAaron Ballman 
15*7d2ea6c4SCarlos Galvez namespace clang::tidy::cert {
1646bc3047SAaron Ballman 
registerMatchers(MatchFinder * Finder)1746bc3047SAaron Ballman void VariadicFunctionDefCheck::registerMatchers(MatchFinder *Finder) {
18fd3a3b3fSAaron Ballman   // We only care about function *definitions* that are variadic, and do not
19fd3a3b3fSAaron Ballman   // have extern "C" language linkage.
20fd3a3b3fSAaron Ballman   Finder->addMatcher(
21fd3a3b3fSAaron Ballman       functionDecl(isDefinition(), isVariadic(), unless(isExternC()))
22fd3a3b3fSAaron Ballman           .bind("func"),
2346bc3047SAaron Ballman       this);
2446bc3047SAaron Ballman }
2546bc3047SAaron Ballman 
check(const MatchFinder::MatchResult & Result)2646bc3047SAaron Ballman void VariadicFunctionDefCheck::check(const MatchFinder::MatchResult &Result) {
2746bc3047SAaron Ballman   const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>("func");
2846bc3047SAaron Ballman 
2946bc3047SAaron Ballman   diag(FD->getLocation(),
3046bc3047SAaron Ballman        "do not define a C-style variadic function; consider using a function "
3146bc3047SAaron Ballman        "parameter pack or currying instead");
3246bc3047SAaron Ballman }
3346bc3047SAaron Ballman 
34*7d2ea6c4SCarlos Galvez } // namespace clang::tidy::cert
35