DefaultOperatorNewAlignmentCheck.cpp 2.3 KB

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