clang-tools  16.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 
14 using namespace clang::ast_matchers;
15 
16 namespace clang {
17 namespace tidy {
18 namespace cert {
19 
20 void DefaultOperatorNewAlignmentCheck::registerMatchers(MatchFinder *Finder) {
21  Finder->addMatcher(
22  cxxNewExpr(unless(hasAnyPlacementArg(anything()))).bind("new"), this);
23 }
24 
26  const MatchFinder::MatchResult &Result) {
27  // Get the found 'new' expression.
28  const auto *NewExpr = Result.Nodes.getNodeAs<CXXNewExpr>("new");
29 
30  QualType T = NewExpr->getAllocatedType();
31  // Dependent types do not have fixed alignment.
32  if (T->isDependentType())
33  return;
34  const TagDecl *D = T->getAsTagDecl();
35  // Alignment can not be obtained for undefined type.
36  if (!D || !D->getDefinition() || !D->isCompleteDefinition())
37  return;
38 
39  ASTContext &Context = D->getASTContext();
40 
41  // Check if no alignment was specified for the type.
42  if (!Context.isAlignmentRequired(T))
43  return;
44 
45  // The user-specified alignment (in bits).
46  unsigned SpecifiedAlignment = D->getMaxAlignment();
47  // Double-check if no alignment was specified.
48  if (!SpecifiedAlignment)
49  return;
50  // The alignment used by default 'operator new' (in bits).
51  unsigned DefaultNewAlignment = Context.getTargetInfo().getNewAlign();
52 
53  bool OverAligned = SpecifiedAlignment > DefaultNewAlignment;
54  bool HasDefaultOperatorNew =
55  !NewExpr->getOperatorNew() || NewExpr->getOperatorNew()->isImplicit();
56 
57  unsigned CharWidth = Context.getTargetInfo().getCharWidth();
58  if (HasDefaultOperatorNew && OverAligned)
59  diag(NewExpr->getBeginLoc(),
60  "allocation function returns a pointer with alignment %0 but the "
61  "over-aligned type being allocated requires alignment %1")
62  << (DefaultNewAlignment / CharWidth)
63  << (SpecifiedAlignment / CharWidth);
64 }
65 
66 } // namespace cert
67 } // namespace tidy
68 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:14
ns1::ns2::D
@ D
Definition: CategoricalFeature.h:3
DefaultOperatorNewAlignmentCheck.h
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::check
bool check(llvm::StringRef File, const ThreadsafeFS &TFS, const ClangdLSPServer::Options &Opts)
Definition: Check.cpp:418