clang-tools  8.0.0
CloexecCheck.h
Go to the documentation of this file.
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,
55  StringRef MacroFlag, int ArgPos);
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);
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;
93 
94  /// Binding name of the FuncDecl of a function call.
95  static const char *FuncDeclBindingStr;
96 
97  /// Binding name of the function call expression.
98  static const char *FuncBindingStr;
99 };
100 
101 } // namespace android
102 } // namespace tidy
103 } // namespace clang
104 
105 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_H
void insertStringFlag(const ast_matchers::MatchFinder::MatchResult &Result, const char Mode, const int ArgPos)
Type3 is also to add a flag to the corresponding argument, but this time, the flag is some string and...
void insertMacroFlag(const ast_matchers::MatchFinder::MatchResult &Result, StringRef MacroFlag, int ArgPos)
Currently, we have three types of fixes.
static const char * FuncBindingStr
Binding name of the function call expression.
Definition: CloexecCheck.h:98
Base class for all clang-tidy checks.
Definition: ClangTidy.h:127
void registerMatchersImpl(ast_matchers::MatchFinder *Finder, ast_matchers::internal::Matcher< FunctionDecl > Function)
static constexpr llvm::StringLiteral Name
void replaceFunc(const ast_matchers::MatchFinder::MatchResult &Result, StringRef WarningMsg, StringRef FixMsg)
Type2 is to replace the API to another function that has required the ability.
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
CloexecCheck(StringRef Name, ClangTidyContext *Context)
Definition: CloexecCheck.h:31
The base class for all close-on-exec checks in Android module.
Definition: CloexecCheck.h:29
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
StringRef getSpellingArg(const ast_matchers::MatchFinder::MatchResult &Result, int N) const
Helper function to get the spelling of a particular argument.
static const char * FuncDeclBindingStr
Binding name of the FuncDecl of a function call.
Definition: CloexecCheck.h:95