Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:18

0001 //===--- CloexecCheck.h - clang-tidy-----------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 ///
0009 /// \file
0010 /// This file contains the declaration of the CloexecCheck class, which is the
0011 /// base class for all of the close-on-exec checks in Android module.
0012 ///
0013 //===----------------------------------------------------------------------===//
0014 
0015 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_H
0016 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_H
0017 
0018 #include "../ClangTidyCheck.h"
0019 
0020 namespace clang::tidy::android {
0021 
0022 /// The base class for all close-on-exec checks in Android module.
0023 /// To be specific, there are some functions that need the close-on-exec flag to
0024 /// prevent the file descriptor leakage on fork+exec and this class provides
0025 /// utilities to identify and fix these C functions.
0026 class CloexecCheck : public ClangTidyCheck {
0027 public:
0028   CloexecCheck(StringRef Name, ClangTidyContext *Context)
0029       : ClangTidyCheck(Name, Context) {}
0030 
0031 protected:
0032   void
0033   registerMatchersImpl(ast_matchers::MatchFinder *Finder,
0034                        ast_matchers::internal::Matcher<FunctionDecl> Function);
0035 
0036   /// Currently, we have three types of fixes.
0037   ///
0038   /// Type1 is to insert the necessary macro flag in the flag argument. For
0039   /// example, 'O_CLOEXEC' is required in function 'open()', so
0040   /// \code
0041   ///   open(file, O_RDONLY);
0042   /// \endcode
0043   /// should be
0044   /// \code
0045   ///   open(file, O_RDONLY | O_CLOEXE);
0046   /// \endcode
0047   ///
0048   /// \param [out] Result MatchResult from AST matcher.
0049   /// \param MacroFlag The macro name of the flag.
0050   /// \param ArgPos The 0-based position of the flag argument.
0051   void insertMacroFlag(const ast_matchers::MatchFinder::MatchResult &Result,
0052                        StringRef MacroFlag, int ArgPos);
0053 
0054   /// Type2 is to replace the API to another function that has required the
0055   /// ability. For example:
0056   /// \code
0057   ///   creat(path, mode);
0058   /// \endcode
0059   /// should be
0060   /// \code
0061   ///   open(path, O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, mode)
0062   /// \endcode
0063   ///
0064   /// \param [out] Result MatchResult from AST matcher.
0065   /// \param WarningMsg The warning message.
0066   /// \param FixMsg The fix message.
0067   void replaceFunc(const ast_matchers::MatchFinder::MatchResult &Result,
0068                    StringRef WarningMsg, StringRef FixMsg);
0069 
0070   /// Type3 is also to add a flag to the corresponding argument, but this time,
0071   /// the flag is some string and each char represents a mode rather than a
0072   /// macro. For example, 'fopen' needs char 'e' in its mode argument string, so
0073   /// \code
0074   ///   fopen(in_file, "r");
0075   /// \endcode
0076   /// should be
0077   /// \code
0078   ///   fopen(in_file, "re");
0079   /// \endcode
0080   ///
0081   /// \param [out] Result MatchResult from AST matcher.
0082   /// \param Mode The required mode char.
0083   /// \param ArgPos The 0-based position of the flag argument.
0084   void insertStringFlag(const ast_matchers::MatchFinder::MatchResult &Result,
0085                         const char Mode, const int ArgPos);
0086 
0087   /// Helper function to get the spelling of a particular argument.
0088   StringRef getSpellingArg(const ast_matchers::MatchFinder::MatchResult &Result,
0089                            int N) const;
0090 
0091   /// Binding name of the FuncDecl of a function call.
0092   static const char *FuncDeclBindingStr;
0093 
0094   /// Binding name of the function call expression.
0095   static const char *FuncBindingStr;
0096 };
0097 
0098 } // namespace clang::tidy::android
0099 
0100 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_H