clang-tools 6.0.0

ProTypeVarargCheck.cpp
Go to the documentation of this file.
1 //===--- ProTypeVarargCheck.cpp - clang-tidy-------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "ProTypeVarargCheck.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13
14 using namespace clang::ast_matchers;
15
16 namespace clang {
17 namespace tidy {
18 namespace cppcoreguidelines {
19
20 const internal::VariadicDynCastAllOfMatcher<Stmt, VAArgExpr> vAArgExpr;
21
22 void ProTypeVarargCheck::registerMatchers(MatchFinder *Finder) {
23 if (!getLangOpts().CPlusPlus)
24 return;
25
26 Finder->addMatcher(vAArgExpr().bind("va_use"), this);
27
28 Finder->addMatcher(
29 callExpr(callee(functionDecl(isVariadic()))).bind("callvararg"), this);
30 }
31
32 static bool hasSingleVariadicArgumentWithValue(const CallExpr *C, uint64_t I) {
33 const auto *FDecl = dyn_cast<FunctionDecl>(C->getCalleeDecl());
34 if (!FDecl)
35 return false;
36
37 auto N = FDecl->getNumParams(); // Number of parameters without '...'
38 if (C->getNumArgs() != N + 1)
39 return false; // more/less than one argument passed to '...'
40
41 const auto *IntLit =
42 dyn_cast<IntegerLiteral>(C->getArg(N)->IgnoreParenImpCasts());
43 if (!IntLit)
44 return false;
45
46 if (IntLit->getValue() != I)
47 return false;
48
49 return true;
50 }
51
52 void ProTypeVarargCheck::check(const MatchFinder::MatchResult &Result) {
53 if (const auto *Matched = Result.Nodes.getNodeAs<CallExpr>("callvararg")) {
54 if (hasSingleVariadicArgumentWithValue(Matched, 0))
55 return;
56 diag(Matched->getExprLoc(), "do not call c-style vararg functions");
57 }
58
59 if (const auto *Matched = Result.Nodes.getNodeAs<Expr>("va_use")) {
60 diag(Matched->getExprLoc(),
61 "do not use va_start/va_arg to define c-style vararg functions; "
62 "use variadic templates instead");
63 }
64
65 if (const auto *Matched = Result.Nodes.getNodeAs<VarDecl>("va_list")) {
66 auto SR = Matched->getSourceRange();
67 if (SR.isInvalid())
68 return; // some implicitly generated builtins take va_list
69 diag(SR.getBegin(), "do not declare variables of type va_list; "
70 "use variadic templates instead");
71 }
72 }
73
74 } // namespace cppcoreguidelines
75 } // namespace tidy
76 } // namespace clang
clang::ast_matchers
Definition: ClangTidyDiagnosticConsumer.h:27
clang::tidy::cppcoreguidelines::vAArgExpr
const internal::VariadicDynCastAllOfMatcher< Stmt, VAArgExpr > vAArgExpr
Definition: ProTypeVarargCheck.cpp:20
ProTypeVarargCheck.h
clang
Definition: AndroidTidyModule.cpp:28
clang::tidy::cppcoreguidelines::hasSingleVariadicArgumentWithValue
static bool hasSingleVariadicArgumentWithValue(const CallExpr *C, uint64_t I)
Definition: ProTypeVarargCheck.cpp:32