clang-tools 19.0.0git
DefaultOperatorNewAlignmentCheck.cpp
Go to the documentation of this file.
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
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
19 Finder->addMatcher(
20 cxxNewExpr(unless(hasAnyPlacementArg(anything()))).bind("new"), this);
21}
22
24 const MatchFinder::MatchResult &Result) {
25 // Get the found 'new' expression.
26 const auto *NewExpr = Result.Nodes.getNodeAs<CXXNewExpr>("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(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);
62}
63
64} // namespace clang::tidy::cert
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.