10#include "../utils/Matchers.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/ASTMatchers/ASTMatchers.h"
14#include "clang/Basic/TargetInfo.h"
15#include "clang/Lex/PPCallbacks.h"
16#include "clang/Lex/Preprocessor.h"
17#include "clang/Lex/Token.h"
23const internal::VariadicDynCastAllOfMatcher<Stmt, VAArgExpr>
VAArgExpr;
27 "__builtin_isgreater",
28 "__builtin_isgreaterequal",
30 "__builtin_islessequal",
31 "__builtin_islessgreater",
32 "__builtin_isunordered",
33 "__builtin_fpclassify",
36 "__builtin_isinf_sign",
40 "__builtin_constant_p",
41 "__builtin_classify_type",
43 "__builtin_assume_aligned",
47 "__builtin_shufflevector",
49 "__builtin_convertvector",
50 "__builtin_call_with_static_chain",
51 "__builtin_annotation",
52 "__builtin_add_overflow",
53 "__builtin_sub_overflow",
54 "__builtin_mul_overflow",
55 "__builtin_preserve_access_index",
56 "__builtin_nontemporal_store",
57 "__builtin_nontemporal_load",
58 "__builtin_ms_va_start",
63 "do not use va_arg to define c-style vararg functions; "
64 "use variadic templates instead";
68 ASTContext &Context = Finder->getASTContext();
69 QualType Desugar =
Node.getDesugaredType(Context);
70 QualType NodeTy =
Node.getUnqualifiedType();
72 auto CheckVaList = [](QualType NodeTy, QualType
Expected,
73 const ASTContext &Context) {
76 QualType Desugar = NodeTy;
80 Desugar = Ty.getSingleStepDesugaredType(Context);
83 }
while (Desugar != Ty);
90 using BuiltinVaListKind = TargetInfo::BuiltinVaListKind;
91 BuiltinVaListKind VaListKind = Context.getTargetInfo().getBuiltinVaListKind();
92 if (VaListKind == BuiltinVaListKind::CharPtrBuiltinVaList ||
93 VaListKind == BuiltinVaListKind::VoidPtrBuiltinVaList) {
94 if (CheckVaList(NodeTy, Context.getBuiltinVaListType(), Context))
97 Context.getBuiltinVaListType().getDesugaredType(Context)) {
103 if (Desugar == Context.getBuiltinMSVaListType().getDesugaredType(Context) &&
104 CheckVaList(NodeTy, Context.getBuiltinMSVaListType(), Context)) {
112 ast_matchers::internal::Matcher<QualType>, InnerType) {
113 return InnerType.matches(
Node.getOriginalType(), Finder,
Builder);
116class VaArgPPCallbacks :
public PPCallbacks {
120 void MacroExpands(
const Token &MacroNameTok,
const MacroDefinition &MD,
121 SourceRange
Range,
const MacroArgs *
Args)
override {
122 if (MacroNameTok.getIdentifierInfo()->getName() ==
"va_arg") {
133 Finder->addMatcher(
VAArgExpr().bind(
"va_use"),
this);
136 callExpr(callee(functionDecl(isVariadic(),
138 unless(hasAncestor(expr(matchers::hasUnevaluatedContext()))),
139 unless(hasAncestor(typeLoc())))
144 varDecl(unless(parmVarDecl()),
146 anyOf(isVAList(), decayedType(hasOriginalType(isVAList()))))))
153 Preprocessor *ModuleExpanderPP) {
154 PP->addPPCallbacks(std::make_unique<VaArgPPCallbacks>(
this));
158 const auto *FDecl = dyn_cast<FunctionDecl>(
C->getCalleeDecl());
162 auto N = FDecl->getNumParams();
163 if (
C->getNumArgs() != N + 1)
167 dyn_cast<IntegerLiteral>(
C->getArg(N)->IgnoreParenImpCasts());
171 if (IntLit->getValue() != I)
178 if (
const auto *Matched = Result.Nodes.getNodeAs<CallExpr>(
"callvararg")) {
181 diag(Matched->getExprLoc(),
"do not call c-style vararg functions");
184 if (
const auto *Matched = Result.Nodes.getNodeAs<Expr>(
"va_use")) {
188 if (
const auto *Matched = Result.Nodes.getNodeAs<VarDecl>(
"va_list")) {
189 auto SR = Matched->getSourceRange();
192 diag(SR.getBegin(),
"do not declare variables of type va_list; "
193 "use variadic templates instead");
CodeCompletionBuilder Builder
CharSourceRange Range
SourceRange for the file name.
std::vector< const char * > Expected
::clang::DynTypedNode Node
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
This check flags all calls to c-style variadic functions and all use of va_arg.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
AST_MATCHER_P(UserDefinedLiteral, hasLiteral, clang::ast_matchers::internal::Matcher< Expr >, InnerMatcher)
const internal::VariadicDynCastAllOfMatcher< Stmt, VAArgExpr > VAArgExpr
static constexpr StringRef VaArgWarningMessage
AST_MATCHER(CXXRecordDecl, hasPublicVirtualOrProtectedNonVirtualDestructor)
static bool hasSingleVariadicArgumentWithValue(const CallExpr *C, uint64_t I)
static constexpr StringRef AllowedVariadics[]