10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
19 const ASTContext &Ctx) {
20 Expr::EvalResult Result;
21 if (SizeExpr->EvaluateAsRValue(Result, Ctx))
23 CharUnits::fromQuantity(Result.Val.getInt().getExtValue()));
29 callExpr(callee(namedDecl(
30 anyOf(hasName(
"::memcmp"), hasName(
"::std::memcmp")))),
31 unless(isInstantiationDependent()))
37 const MatchFinder::MatchResult &Result) {
38 const ASTContext &Ctx = *Result.Context;
39 const auto *
CE = Result.Nodes.getNodeAs<CallExpr>(
"call");
41 const Expr *SizeExpr =
CE->getArg(2);
42 assert(SizeExpr !=
nullptr &&
"Third argument of memcmp is mandatory.");
45 for (
unsigned int ArgIndex = 0; ArgIndex < 2; ++ArgIndex) {
46 const Expr *ArgExpr =
CE->getArg(ArgIndex);
47 QualType ArgType = ArgExpr->IgnoreImplicit()->getType();
48 const Type *PointeeType = ArgType->getPointeeOrArrayElementType();
49 assert(PointeeType !=
nullptr &&
"PointeeType should always be available.");
50 QualType PointeeQualifiedType(PointeeType, 0);
52 if (PointeeType->isRecordType()) {
53 if (
const RecordDecl *RD =
54 PointeeType->getAsRecordDecl()->getDefinition()) {
55 if (
const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) {
56 if (!CXXDecl->isStandardLayout()) {
58 "comparing object representation of non-standard-layout type "
59 "%0; consider using a comparison operator instead")
60 << PointeeQualifiedType;
67 if (!PointeeType->isIncompleteType()) {
68 uint64_t PointeeSize = Ctx.getTypeSize(PointeeType);
69 if (ComparedBits && *ComparedBits >= PointeeSize &&
70 !Ctx.hasUniqueObjectRepresentations(PointeeQualifiedType)) {
72 "comparing object representation of type %0 which does not have a "
73 "unique object representation; consider comparing %select{the "
74 "values|the members of the object}1 manually")
75 << PointeeQualifiedType << (PointeeType->isRecordType() ? 1 : 0);
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
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.
static std::optional< uint64_t > tryEvaluateSizeExpr(const Expr *SizeExpr, const ASTContext &Ctx)