CloexecOpenCheck.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. //===--- CloexecOpenCheck.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 "CloexecOpenCheck.h"
  9. #include "clang/AST/ASTContext.h"
  10. #include "clang/ASTMatchers/ASTMatchFinder.h"
  11. using namespace clang::ast_matchers;
  12. namespace clang::tidy::android {
  13. void CloexecOpenCheck::registerMatchers(MatchFinder *Finder) {
  14. auto CharPointerType = hasType(pointerType(pointee(isAnyCharacter())));
  15. registerMatchersImpl(Finder,
  16. functionDecl(isExternC(), returns(isInteger()),
  17. hasAnyName("open", "open64"),
  18. hasParameter(0, CharPointerType),
  19. hasParameter(1, hasType(isInteger()))));
  20. registerMatchersImpl(Finder,
  21. functionDecl(isExternC(), returns(isInteger()),
  22. hasName("openat"),
  23. hasParameter(0, hasType(isInteger())),
  24. hasParameter(1, CharPointerType),
  25. hasParameter(2, hasType(isInteger()))));
  26. }
  27. void CloexecOpenCheck::check(const MatchFinder::MatchResult &Result) {
  28. const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>(FuncDeclBindingStr);
  29. assert(FD->param_size() > 1);
  30. int ArgPos = (FD->param_size() > 2) ? 2 : 1;
  31. insertMacroFlag(Result, /*MacroFlag=*/"O_CLOEXEC", ArgPos);
  32. }
  33. } // namespace clang::tidy::android