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