brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.7 KiB · a6dcb57 Raw
101 lines · c
1//===----------------------------------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8///9/// \file10/// This file contains the declaration of the CloexecCheck class, which is the11/// base class for all of the close-on-exec checks in Android module.12///13//===----------------------------------------------------------------------===//14 15#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXECCHECK_H16#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXECCHECK_H17 18#include "../ClangTidyCheck.h"19 20namespace clang::tidy::android {21 22/// The base class for all close-on-exec checks in Android module.23/// To be specific, there are some functions that need the close-on-exec flag to24/// prevent the file descriptor leakage on fork+exec and this class provides25/// utilities to identify and fix these C functions.26class CloexecCheck : public ClangTidyCheck {27public:28  CloexecCheck(StringRef Name, ClangTidyContext *Context)29      : ClangTidyCheck(Name, Context) {}30 31protected:32  void registerMatchersImpl(33      ast_matchers::MatchFinder *Finder,34      const ast_matchers::internal::Matcher<FunctionDecl> &Function);35 36  /// Currently, we have three types of fixes.37  ///38  /// Type1 is to insert the necessary macro flag in the flag argument. For39  /// example, 'O_CLOEXEC' is required in function 'open()', so40  /// \code41  ///   open(file, O_RDONLY);42  /// \endcode43  /// should be44  /// \code45  ///   open(file, O_RDONLY | O_CLOEXE);46  /// \endcode47  ///48  /// \param [out] Result MatchResult from AST matcher.49  /// \param MacroFlag The macro name of the flag.50  /// \param ArgPos The 0-based position of the flag argument.51  void insertMacroFlag(const ast_matchers::MatchFinder::MatchResult &Result,52                       StringRef MacroFlag, int ArgPos);53 54  /// Type2 is to replace the API to another function that has required the55  /// ability. For example:56  /// \code57  ///   creat(path, mode);58  /// \endcode59  /// should be60  /// \code61  ///   open(path, O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, mode)62  /// \endcode63  ///64  /// \param [out] Result MatchResult from AST matcher.65  /// \param WarningMsg The warning message.66  /// \param FixMsg The fix message.67  void replaceFunc(const ast_matchers::MatchFinder::MatchResult &Result,68                   StringRef WarningMsg, StringRef FixMsg);69 70  /// Type3 is also to add a flag to the corresponding argument, but this time,71  /// the flag is some string and each char represents a mode rather than a72  /// macro. For example, 'fopen' needs char 'e' in its mode argument string, so73  /// \code74  ///   fopen(in_file, "r");75  /// \endcode76  /// should be77  /// \code78  ///   fopen(in_file, "re");79  /// \endcode80  ///81  /// \param [out] Result MatchResult from AST matcher.82  /// \param Mode The required mode char.83  /// \param ArgPos The 0-based position of the flag argument.84  void insertStringFlag(const ast_matchers::MatchFinder::MatchResult &Result,85                        char Mode, int ArgPos);86 87  /// Helper function to get the spelling of a particular argument.88  StringRef getSpellingArg(const ast_matchers::MatchFinder::MatchResult &Result,89                           int N) const;90 91  /// Binding name of the FuncDecl of a function call.92  static constexpr char FuncDeclBindingStr[] = "funcDecl";93 94  /// Binding name of the function call expression.95  static constexpr char FuncBindingStr[] = "func";96};97 98} // namespace clang::tidy::android99 100#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXECCHECK_H101