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