CloexecCheck.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //===--- CloexecCheck.h - clang-tidy-----------------------------*- C++ -*-===//
  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. /// \file
  10. /// This file contains the declaration of the CloexecCheck class, which is the
  11. /// base class for all of the close-on-exec checks in Android module.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_H
  15. #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_H
  16. #include "../ClangTidyCheck.h"
  17. namespace clang::tidy::android {
  18. /// The base class for all close-on-exec checks in Android module.
  19. /// To be specific, there are some functions that need the close-on-exec flag to
  20. /// prevent the file descriptor leakage on fork+exec and this class provides
  21. /// utilities to identify and fix these C functions.
  22. class CloexecCheck : public ClangTidyCheck {
  23. public:
  24. CloexecCheck(StringRef Name, ClangTidyContext *Context)
  25. : ClangTidyCheck(Name, Context) {}
  26. protected:
  27. void
  28. registerMatchersImpl(ast_matchers::MatchFinder *Finder,
  29. ast_matchers::internal::Matcher<FunctionDecl> Function);
  30. /// Currently, we have three types of fixes.
  31. ///
  32. /// Type1 is to insert the necessary macro flag in the flag argument. For
  33. /// example, 'O_CLOEXEC' is required in function 'open()', so
  34. /// \code
  35. /// open(file, O_RDONLY);
  36. /// \endcode
  37. /// should be
  38. /// \code
  39. /// open(file, O_RDONLY | O_CLOEXE);
  40. /// \endcode
  41. ///
  42. /// \param [out] Result MatchResult from AST matcher.
  43. /// \param MacroFlag The macro name of the flag.
  44. /// \param ArgPos The 0-based position of the flag argument.
  45. void insertMacroFlag(const ast_matchers::MatchFinder::MatchResult &Result,
  46. StringRef MacroFlag, int ArgPos);
  47. /// Type2 is to replace the API to another function that has required the
  48. /// ability. For example:
  49. /// \code
  50. /// creat(path, mode);
  51. /// \endcode
  52. /// should be
  53. /// \code
  54. /// open(path, O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, mode)
  55. /// \endcode
  56. ///
  57. /// \param [out] Result MatchResult from AST matcher.
  58. /// \param WarningMsg The warning message.
  59. /// \param FixMsg The fix message.
  60. void replaceFunc(const ast_matchers::MatchFinder::MatchResult &Result,
  61. StringRef WarningMsg, StringRef FixMsg);
  62. /// Type3 is also to add a flag to the corresponding argument, but this time,
  63. /// the flag is some string and each char represents a mode rather than a
  64. /// macro. For example, 'fopen' needs char 'e' in its mode argument string, so
  65. /// \code
  66. /// fopen(in_file, "r");
  67. /// \endcode
  68. /// should be
  69. /// \code
  70. /// fopen(in_file, "re");
  71. /// \endcode
  72. ///
  73. /// \param [out] Result MatchResult from AST matcher.
  74. /// \param Mode The required mode char.
  75. /// \param ArgPos The 0-based position of the flag argument.
  76. void insertStringFlag(const ast_matchers::MatchFinder::MatchResult &Result,
  77. const char Mode, const int ArgPos);
  78. /// Helper function to get the spelling of a particular argument.
  79. StringRef getSpellingArg(const ast_matchers::MatchFinder::MatchResult &Result,
  80. int N) const;
  81. /// Binding name of the FuncDecl of a function call.
  82. static const char *FuncDeclBindingStr;
  83. /// Binding name of the function call expression.
  84. static const char *FuncBindingStr;
  85. };
  86. } // namespace clang::tidy::android
  87. #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_H