1//===--- DefaultOperatorNewCheck.cpp - clang-tidy --------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "DefaultOperatorNewAlignmentCheck.h"
10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/Basic/TargetInfo.h"
13
14using namespace clang::ast_matchers;
15
16namespace clang::tidy::cert {
17
18void DefaultOperatorNewAlignmentCheck::registerMatchers(MatchFinder *Finder) {
19 Finder->addMatcher(
20 NodeMatch: cxxNewExpr(unless(hasAnyPlacementArg(InnerMatcher: anything()))).bind(ID: "new"), Action: this);
21}
22
23void DefaultOperatorNewAlignmentCheck::check(
24 const MatchFinder::MatchResult &Result) {
25 // Get the found 'new' expression.
26 const auto *NewExpr = Result.Nodes.getNodeAs<CXXNewExpr>(ID: "new");
27
28 QualType T = NewExpr->getAllocatedType();
29 // Dependent types do not have fixed alignment.
30 if (T->isDependentType())
31 return;
32 const TagDecl *D = T->getAsTagDecl();
33 // Alignment can not be obtained for undefined type.
34 if (!D || !D->getDefinition() || !D->isCompleteDefinition())
35 return;
36
37 ASTContext &Context = D->getASTContext();
38
39 // Check if no alignment was specified for the type.
40 if (!Context.isAlignmentRequired(T))
41 return;
42
43 // The user-specified alignment (in bits).
44 unsigned SpecifiedAlignment = D->getMaxAlignment();
45 // Double-check if no alignment was specified.
46 if (!SpecifiedAlignment)
47 return;
48 // The alignment used by default 'operator new' (in bits).
49 unsigned DefaultNewAlignment = Context.getTargetInfo().getNewAlign();
50
51 bool OverAligned = SpecifiedAlignment > DefaultNewAlignment;
52 bool HasDefaultOperatorNew =
53 !NewExpr->getOperatorNew() || NewExpr->getOperatorNew()->isImplicit();
54
55 unsigned CharWidth = Context.getTargetInfo().getCharWidth();
56 if (HasDefaultOperatorNew && OverAligned)
57 diag(Loc: NewExpr->getBeginLoc(),
58 Description: "allocation function returns a pointer with alignment %0 but the "
59 "over-aligned type being allocated requires alignment %1")
60 << (DefaultNewAlignment / CharWidth)
61 << (SpecifiedAlignment / CharWidth);
62}
63
64} // namespace clang::tidy::cert
65

source code of clang-tools-extra/clang-tidy/cert/DefaultOperatorNewAlignmentCheck.cpp