blob: 9c1c1a9691f8561b19586b3a879a0166d084f7d6 [file] [log] [blame]
Yan Wangb21739f2017-08-10 22:09:22 +00001//===--- 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
21namespace clang {
22namespace tidy {
23namespace 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.
29class CloexecCheck : public ClangTidyCheck {
30public:
31 CloexecCheck(StringRef Name, ClangTidyContext *Context)
32 : ClangTidyCheck(Name, Context) {}
33
34protected:
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 Pilgrim7c87ebf2017-08-12 18:50:53 +000055 StringRef MacroFlag, int ArgPos);
Yan Wangb21739f2017-08-10 22:09:22 +000056
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);
89};
90
91} // namespace android
92} // namespace tidy
93} // namespace clang
94
95#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_H