NonCopyableObjects.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //===--- NonCopyableObjects.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 "NonCopyableObjects.h"
  9. #include "clang/AST/ASTContext.h"
  10. #include "clang/ASTMatchers/ASTMatchFinder.h"
  11. #include <algorithm>
  12. using namespace clang::ast_matchers;
  13. namespace clang::tidy::misc {
  14. void NonCopyableObjectsCheck::registerMatchers(MatchFinder *Finder) {
  15. // There are two ways to get into trouble with objects like FILE *:
  16. // dereferencing the pointer type to be a non-pointer type, and declaring
  17. // the type as a non-pointer type in the first place. While the declaration
  18. // itself could technically be well-formed in the case where the type is not
  19. // an opaque type, it's highly suspicious behavior.
  20. //
  21. // POSIX types are a bit different in that it's reasonable to declare a
  22. // non-pointer variable or data member of the type, but it is not reasonable
  23. // to dereference a pointer to the type, or declare a parameter of non-pointer
  24. // type.
  25. // FIXME: it would be good to make a list that is also user-configurable so
  26. // that users can add their own elements to the list. However, it may require
  27. // some extra thought since POSIX types and FILE types are usable in different
  28. // ways.
  29. auto BadFILEType = hasType(
  30. namedDecl(hasAnyName("::FILE", "FILE", "std::FILE")).bind("type_decl"));
  31. auto BadPOSIXType =
  32. hasType(namedDecl(hasAnyName("::pthread_cond_t", "::pthread_mutex_t",
  33. "pthread_cond_t", "pthread_mutex_t"))
  34. .bind("type_decl"));
  35. auto BadEitherType = anyOf(BadFILEType, BadPOSIXType);
  36. Finder->addMatcher(
  37. namedDecl(anyOf(varDecl(BadFILEType), fieldDecl(BadFILEType)))
  38. .bind("decl"),
  39. this);
  40. Finder->addMatcher(parmVarDecl(BadPOSIXType).bind("decl"), this);
  41. Finder->addMatcher(
  42. expr(unaryOperator(hasOperatorName("*"), BadEitherType)).bind("expr"),
  43. this);
  44. }
  45. void NonCopyableObjectsCheck::check(const MatchFinder::MatchResult &Result) {
  46. const auto *D = Result.Nodes.getNodeAs<NamedDecl>("decl");
  47. const auto *BD = Result.Nodes.getNodeAs<NamedDecl>("type_decl");
  48. const auto *E = Result.Nodes.getNodeAs<Expr>("expr");
  49. if (D && BD)
  50. diag(D->getLocation(), "%0 declared as type '%1', which is unsafe to copy"
  51. "; did you mean '%1 *'?")
  52. << D << BD->getName();
  53. else if (E)
  54. diag(E->getExprLoc(),
  55. "expression has opaque data structure type %0; type should only be "
  56. "used as a pointer and not dereferenced")
  57. << BD;
  58. }
  59. } // namespace clang::tidy::misc