[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / clang-tools-extra / clang-tidy / cert / DefaultOperatorNewAlignmentCheck.cpp
blob2a0d0ada42b2872463756ed5814de551823e1ade
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 //===----------------------------------------------------------------------===//
9 #include "DefaultOperatorNewAlignmentCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/Basic/TargetInfo.h"
14 using namespace clang::ast_matchers;
16 namespace clang::tidy::cert {
18 void DefaultOperatorNewAlignmentCheck::registerMatchers(MatchFinder *Finder) {
19 Finder->addMatcher(
20 cxxNewExpr(unless(hasAnyPlacementArg(anything()))).bind("new"), this);
23 void DefaultOperatorNewAlignmentCheck::check(
24 const MatchFinder::MatchResult &Result) {
25 // Get the found 'new' expression.
26 const auto *NewExpr = Result.Nodes.getNodeAs<CXXNewExpr>("new");
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;
37 ASTContext &Context = D->getASTContext();
39 // Check if no alignment was specified for the type.
40 if (!Context.isAlignmentRequired(T))
41 return;
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();
51 bool OverAligned = SpecifiedAlignment > DefaultNewAlignment;
52 bool HasDefaultOperatorNew =
53 !NewExpr->getOperatorNew() || NewExpr->getOperatorNew()->isImplicit();
55 unsigned CharWidth = Context.getTargetInfo().getCharWidth();
56 if (HasDefaultOperatorNew && OverAligned)
57 diag(NewExpr->getBeginLoc(),
58 "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);
64 } // namespace clang::tidy::cert