10#include "clang/ASTMatchers/ASTMatchFinder.h"
18 auto IsPointerType = refersToType(qualType(isAnyPointer()));
19 Finder->addMatcher(callExpr(hasDeclaration(functionDecl(allOf(
20 hasName(
"::std::bit_cast"),
21 hasTemplateArgument(0, IsPointerType),
22 hasTemplateArgument(1, IsPointerType)))))
27 auto IsDoublePointerType =
28 hasType(qualType(pointsTo(qualType(isAnyPointer()))));
29 Finder->addMatcher(callExpr(hasArgument(0, IsDoublePointerType),
30 hasArgument(1, IsDoublePointerType),
31 hasDeclaration(functionDecl(hasName(
"::memcpy"))))
37 if (
const auto *Call = Result.Nodes.getNodeAs<CallExpr>(
"bit_cast"))
38 diag(Call->getBeginLoc(),
39 "do not use 'std::bit_cast' to cast between pointers")
40 << Call->getSourceRange();
41 else if (
const auto *Call = Result.Nodes.getNodeAs<CallExpr>(
"memcpy"))
42 diag(Call->getBeginLoc(),
"do not use 'memcpy' to cast between pointers")
43 << Call->getSourceRange();
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
const LangOptions & getLangOpts() const
Returns the language options from the context.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.