Imported Upstream version 6.10.0.49

Former-commit-id: 1d6753294b2993e1fbf92de9366bb9544db4189b
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2020-01-16 16:38:04 +00:00
parent d94e79959b
commit 468663ddbb
48518 changed files with 2789335 additions and 61176 deletions

View File

@@ -0,0 +1,67 @@
//===--- AndroidTidyModule.cpp - clang-tidy--------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "../ClangTidy.h"
#include "../ClangTidyModule.h"
#include "../ClangTidyModuleRegistry.h"
#include "CloexecAccept4Check.h"
#include "CloexecAcceptCheck.h"
#include "CloexecCreatCheck.h"
#include "CloexecEpollCreate1Check.h"
#include "CloexecEpollCreateCheck.h"
#include "CloexecDupCheck.h"
#include "CloexecFopenCheck.h"
#include "CloexecInotifyInit1Check.h"
#include "CloexecInotifyInitCheck.h"
#include "CloexecMemfdCreateCheck.h"
#include "CloexecOpenCheck.h"
#include "CloexecSocketCheck.h"
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
namespace android {
/// This module is for Android specific checks.
class AndroidModule : public ClangTidyModule {
public:
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
CheckFactories.registerCheck<CloexecAccept4Check>("android-cloexec-accept4");
CheckFactories.registerCheck<CloexecAcceptCheck>("android-cloexec-accept");
CheckFactories.registerCheck<CloexecCreatCheck>("android-cloexec-creat");
CheckFactories.registerCheck<CloexecEpollCreate1Check>(
"android-cloexec-epoll-create1");
CheckFactories.registerCheck<CloexecEpollCreateCheck>(
"android-cloexec-epoll-create");
CheckFactories.registerCheck<CloexecDupCheck>("android-cloexec-dup");
CheckFactories.registerCheck<CloexecFopenCheck>("android-cloexec-fopen");
CheckFactories.registerCheck<CloexecInotifyInitCheck>(
"android-cloexec-inotify-init");
CheckFactories.registerCheck<CloexecInotifyInit1Check>(
"android-cloexec-inotify-init1");
CheckFactories.registerCheck<CloexecMemfdCreateCheck>(
"android-cloexec-memfd-create");
CheckFactories.registerCheck<CloexecOpenCheck>("android-cloexec-open");
CheckFactories.registerCheck<CloexecSocketCheck>("android-cloexec-socket");
}
};
// Register the AndroidTidyModule using this statically initialized variable.
static ClangTidyModuleRegistry::Add<AndroidModule>
X("android-module", "Adds Android platform checks.");
} // namespace android
// This anchor is used to force the linker to link in the generated object file
// and thus register the AndroidModule.
volatile int AndroidModuleAnchorSource = 0;
} // namespace tidy
} // namespace clang

View File

@@ -0,0 +1,26 @@
set(LLVM_LINK_COMPONENTS support)
add_clang_library(clangTidyAndroidModule
AndroidTidyModule.cpp
CloexecAccept4Check.cpp
CloexecAcceptCheck.cpp
CloexecCheck.cpp
CloexecCreatCheck.cpp
CloexecEpollCreate1Check.cpp
CloexecEpollCreateCheck.cpp
CloexecDupCheck.cpp
CloexecFopenCheck.cpp
CloexecInotifyInit1Check.cpp
CloexecInotifyInitCheck.cpp
CloexecMemfdCreateCheck.cpp
CloexecOpenCheck.cpp
CloexecSocketCheck.cpp
LINK_LIBS
clangAST
clangASTMatchers
clangBasic
clangLex
clangTidy
clangTidyUtils
)

View File

@@ -0,0 +1,40 @@
//===--- CloexecAccept4Check.cpp - clang-tidy------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "CloexecAccept4Check.h"
#include "../utils/ASTUtils.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
namespace android {
void CloexecAccept4Check::registerMatchers(MatchFinder *Finder) {
auto SockAddrPointerType =
hasType(pointsTo(recordDecl(isStruct(), hasName("sockaddr"))));
auto SockLenPointerType = hasType(pointsTo(namedDecl(hasName("socklen_t"))));
registerMatchersImpl(Finder,
functionDecl(returns(isInteger()), hasName("accept4"),
hasParameter(0, hasType(isInteger())),
hasParameter(1, SockAddrPointerType),
hasParameter(2, SockLenPointerType),
hasParameter(3, hasType(isInteger()))));
}
void CloexecAccept4Check::check(const MatchFinder::MatchResult &Result) {
insertMacroFlag(Result, /*MarcoFlag=*/"SOCK_CLOEXEC", /*ArgPos=*/3);
}
} // namespace android
} // namespace tidy
} // namespace clang

View File

@@ -0,0 +1,35 @@
//===--- CloexecAccept4Check.h - clang-tidy----------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_ACCEPT4_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_ACCEPT4_H
#include "CloexecCheck.h"
namespace clang {
namespace tidy {
namespace android {
/// Finds code that uses accept4() without using the SOCK_CLOEXEC flag.
///
/// For the user-facing documentation see:
/// http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-accept4.html
class CloexecAccept4Check : public CloexecCheck {
public:
CloexecAccept4Check(StringRef Name, ClangTidyContext *Context)
: CloexecCheck(Name, Context) {}
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
};
} // namespace android
} // namespace tidy
} // namespace clang
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_ACCEPT4_H

View File

@@ -0,0 +1,47 @@
//===--- CloexecAcceptCheck.cpp - clang-tidy-------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "CloexecAcceptCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
namespace android {
void CloexecAcceptCheck::registerMatchers(MatchFinder *Finder) {
auto SockAddrPointerType =
hasType(pointsTo(recordDecl(isStruct(), hasName("sockaddr"))));
auto SockLenPointerType = hasType(pointsTo(namedDecl(hasName("socklen_t"))));
registerMatchersImpl(Finder,
functionDecl(returns(isInteger()), hasName("accept"),
hasParameter(0, hasType(isInteger())),
hasParameter(1, SockAddrPointerType),
hasParameter(2, SockLenPointerType)));
}
void CloexecAcceptCheck::check(const MatchFinder::MatchResult &Result) {
const std::string &ReplacementText =
(Twine("accept4(") + getSpellingArg(Result, 0) + ", " +
getSpellingArg(Result, 1) + ", " + getSpellingArg(Result, 2) +
", SOCK_CLOEXEC)")
.str();
replaceFunc(
Result,
"prefer accept4() to accept() because accept4() allows SOCK_CLOEXEC",
ReplacementText);
}
} // namespace android
} // namespace tidy
} // namespace clang

View File

@@ -0,0 +1,35 @@
//===--- CloexecAcceptCheck.h - clang-tidy-----------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_ACCEPT_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_ACCEPT_H
#include "CloexecCheck.h"
namespace clang {
namespace tidy {
namespace android {
/// accept() is better to be replaced by accept4().
///
/// For the user-facing documentation see:
/// http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-accept.html
class CloexecAcceptCheck : public CloexecCheck {
public:
CloexecAcceptCheck(StringRef Name, ClangTidyContext *Context)
: CloexecCheck(Name, Context) {}
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
};
} // namespace android
} // namespace tidy
} // namespace clang
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_ACCEPT_H

View File

@@ -0,0 +1,114 @@
//===--- CloexecCheck.cpp - clang-tidy-------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "CloexecCheck.h"
#include "../utils/ASTUtils.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Lex/Lexer.h"
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
namespace android {
namespace {
// Helper function to form the correct string mode for Type3.
// Build the replace text. If it's string constant, add <Mode> directly in the
// end of the string. Else, add <Mode>.
std::string buildFixMsgForStringFlag(const Expr *Arg, const SourceManager &SM,
const LangOptions &LangOpts, char Mode) {
if (Arg->getLocStart().isMacroID())
return (Lexer::getSourceText(
CharSourceRange::getTokenRange(Arg->getSourceRange()), SM,
LangOpts) +
" \"" + Twine(Mode) + "\"")
.str();
StringRef SR = cast<StringLiteral>(Arg->IgnoreParenCasts())->getString();
return ("\"" + SR + Twine(Mode) + "\"").str();
}
} // namespace
const char *CloexecCheck::FuncDeclBindingStr = "funcDecl";
const char *CloexecCheck::FuncBindingStr ="func";
void CloexecCheck::registerMatchersImpl(
MatchFinder *Finder, internal::Matcher<FunctionDecl> Function) {
// We assume all the checked APIs are C functions.
Finder->addMatcher(
callExpr(
callee(functionDecl(isExternC(), Function).bind(FuncDeclBindingStr)))
.bind(FuncBindingStr),
this);
}
void CloexecCheck::insertMacroFlag(const MatchFinder::MatchResult &Result,
StringRef MacroFlag, int ArgPos) {
const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>(FuncBindingStr);
const auto *FlagArg = MatchedCall->getArg(ArgPos);
const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>(FuncDeclBindingStr);
SourceManager &SM = *Result.SourceManager;
if (utils::exprHasBitFlagWithSpelling(FlagArg->IgnoreParenCasts(), SM,
Result.Context->getLangOpts(),
MacroFlag))
return;
SourceLocation EndLoc =
Lexer::getLocForEndOfToken(SM.getFileLoc(FlagArg->getLocEnd()), 0, SM,
Result.Context->getLangOpts());
diag(EndLoc, "%0 should use %1 where possible")
<< FD << MacroFlag
<< FixItHint::CreateInsertion(EndLoc, (Twine(" | ") + MacroFlag).str());
}
void CloexecCheck::replaceFunc(const MatchFinder::MatchResult &Result,
StringRef WarningMsg, StringRef FixMsg) {
const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>(FuncBindingStr);
diag(MatchedCall->getLocStart(), WarningMsg)
<< FixItHint::CreateReplacement(MatchedCall->getSourceRange(), FixMsg);
}
void CloexecCheck::insertStringFlag(
const ast_matchers::MatchFinder::MatchResult &Result, const char Mode,
const int ArgPos) {
const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>(FuncBindingStr);
const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>(FuncDeclBindingStr);
const auto *ModeArg = MatchedCall->getArg(ArgPos);
// Check if the <Mode> may be in the mode string.
const auto *ModeStr = dyn_cast<StringLiteral>(ModeArg->IgnoreParenCasts());
if (!ModeStr || (ModeStr->getString().find(Mode) != StringRef::npos))
return;
const std::string &ReplacementText = buildFixMsgForStringFlag(
ModeArg, *Result.SourceManager, Result.Context->getLangOpts(), Mode);
diag(ModeArg->getLocStart(), "use %0 mode '%1' to set O_CLOEXEC")
<< FD << std::string(1, Mode)
<< FixItHint::CreateReplacement(ModeArg->getSourceRange(),
ReplacementText);
}
StringRef CloexecCheck::getSpellingArg(const MatchFinder::MatchResult &Result,
int N) const {
const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>(FuncBindingStr);
const SourceManager &SM = *Result.SourceManager;
return Lexer::getSourceText(
CharSourceRange::getTokenRange(MatchedCall->getArg(N)->getSourceRange()),
SM, Result.Context->getLangOpts());
}
} // namespace android
} // namespace tidy
} // namespace clang

View File

@@ -0,0 +1,105 @@
//===--- CloexecCheck.h - clang-tidy-----------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file contains the declaration of the CloexecCheck class, which is the
/// base class for all of the close-on-exec checks in Android module.
///
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_H
#include "../ClangTidy.h"
namespace clang {
namespace tidy {
namespace android {
/// \brief The base class for all close-on-exec checks in Android module.
/// To be specific, there are some functions that need the close-on-exec flag to
/// prevent the file descriptor leakage on fork+exec and this class provides
/// utilities to identify and fix these C functions.
class CloexecCheck : public ClangTidyCheck {
public:
CloexecCheck(StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context) {}
protected:
void
registerMatchersImpl(ast_matchers::MatchFinder *Finder,
ast_matchers::internal::Matcher<FunctionDecl> Function);
/// Currently, we have three types of fixes.
///
/// Type1 is to insert the necessary macro flag in the flag argument. For
/// example, 'O_CLOEXEC' is required in function 'open()', so
/// \code
/// open(file, O_RDONLY);
/// \endcode
/// should be
/// \code
/// open(file, O_RDONLY | O_CLOEXE);
/// \endcode
///
/// \param [out] Result MatchResult from AST matcher.
/// \param MacroFlag The macro name of the flag.
/// \param ArgPos The 0-based position of the flag argument.
void insertMacroFlag(const ast_matchers::MatchFinder::MatchResult &Result,
StringRef MacroFlag, int ArgPos);
/// Type2 is to replace the API to another function that has required the
/// ability. For example:
/// \code
/// creat(path, mode);
/// \endcode
/// should be
/// \code
/// open(path, O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, mode)
/// \endcode
///
/// \param [out] Result MatchResult from AST matcher.
/// \param WarningMsg The warning message.
/// \param FixMsg The fix message.
void replaceFunc(const ast_matchers::MatchFinder::MatchResult &Result,
StringRef WarningMsg, StringRef FixMsg);
/// Type3 is also to add a flag to the corresponding argument, but this time,
/// the flag is some string and each char represents a mode rather than a
/// macro. For example, 'fopen' needs char 'e' in its mode argument string, so
/// \code
/// fopen(in_file, "r");
/// \endcode
/// should be
/// \code
/// fopen(in_file, "re");
/// \endcode
///
/// \param [out] Result MatchResult from AST matcher.
/// \param Mode The required mode char.
/// \param ArgPos The 0-based position of the flag argument.
void insertStringFlag(const ast_matchers::MatchFinder::MatchResult &Result,
const char Mode, const int ArgPos);
/// Helper function to get the spelling of a particular argument.
StringRef getSpellingArg(const ast_matchers::MatchFinder::MatchResult &Result,
int N) const;
/// Binding name of the FuncDecl of a function call.
static const char *FuncDeclBindingStr;
/// Binding name of the function call expression.
static const char *FuncBindingStr;
};
} // namespace android
} // namespace tidy
} // namespace clang
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_H

View File

@@ -0,0 +1,43 @@
//===--- CloexecCreatCheck.cpp - clang-tidy--------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "CloexecCreatCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
namespace android {
void CloexecCreatCheck::registerMatchers(MatchFinder *Finder) {
auto CharPointerType = hasType(pointerType(pointee(isAnyCharacter())));
auto MODETType = hasType(namedDecl(hasName("mode_t")));
registerMatchersImpl(Finder,
functionDecl(isExternC(), returns(isInteger()),
hasName("creat"),
hasParameter(0, CharPointerType),
hasParameter(1, MODETType)));
}
void CloexecCreatCheck::check(const MatchFinder::MatchResult &Result) {
const std::string &ReplacementText =
(Twine("open (") + getSpellingArg(Result, 0) +
", O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, " +
getSpellingArg(Result, 1) + ")")
.str();
replaceFunc(Result,
"prefer open() to creat() because open() allows O_CLOEXEC",
ReplacementText);
}
} // namespace android
} // namespace tidy
} // namespace clang

View File

@@ -0,0 +1,35 @@
//===--- CloexecCreatCheck.h - clang-tidy------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_CREAT_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_CREAT_H
#include "CloexecCheck.h"
namespace clang {
namespace tidy {
namespace android {
/// creat() is better to be replaced by open().
/// Find the usage of creat() and redirect user to use open().
/// http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-creat.html
class CloexecCreatCheck : public CloexecCheck {
public:
CloexecCreatCheck(StringRef Name, ClangTidyContext *Context)
: CloexecCheck(Name, Context) {}
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
};
} // namespace android
} // namespace tidy
} // namespace clang
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_CREAT_H

View File

@@ -0,0 +1,38 @@
//===--- CloexecDupCheck.cpp - clang-tidy----------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "CloexecDupCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
namespace android {
void CloexecDupCheck::registerMatchers(MatchFinder *Finder) {
registerMatchersImpl(Finder,
functionDecl(returns(isInteger()), hasName("dup"),
hasParameter(0, hasType(isInteger()))));
}
void CloexecDupCheck::check(const MatchFinder::MatchResult &Result) {
const std::string &ReplacementText =
(Twine("fcntl(") + getSpellingArg(Result, 0) + ", F_DUPFD_CLOEXEC)")
.str();
replaceFunc(Result,
"prefer fcntl() to dup() because fcntl() allows F_DUPFD_CLOEXEC",
ReplacementText);
}
} // namespace android
} // namespace tidy
} // namespace clang

View File

@@ -0,0 +1,36 @@
//===--- CloexecDupCheck.h - clang-tidy-------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_DUP_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_DUP_H
#include "CloexecCheck.h"
namespace clang {
namespace tidy {
namespace android {
/// dup() is better to be replaced by fcntl(), which has close-on-exec flag.
/// Find the usage of dup() and redirect user to use fcntl().
///
/// For the user-facing documentation see:
/// http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-dup.html
class CloexecDupCheck : public CloexecCheck {
public:
CloexecDupCheck(StringRef Name, ClangTidyContext *Context)
: CloexecCheck(Name, Context) {}
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
};
} // namespace android
} // namespace tidy
} // namespace clang
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_DUP_H

View File

@@ -0,0 +1,33 @@
//===--- CloexecEpollCreate1Check.cpp - clang-tidy-------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "CloexecEpollCreate1Check.h"
#include "../utils/ASTUtils.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
namespace android {
void CloexecEpollCreate1Check::registerMatchers(MatchFinder *Finder) {
registerMatchersImpl(
Finder, functionDecl(returns(isInteger()), hasName("epoll_create1"),
hasParameter(0, hasType(isInteger()))));
}
void CloexecEpollCreate1Check::check(const MatchFinder::MatchResult &Result) {
insertMacroFlag(Result, /*MarcoFlag=*/"EPOLL_CLOEXEC", /*ArgPos=*/0);
}
} // namespace android
} // namespace tidy
} // namespace clang

View File

@@ -0,0 +1,35 @@
//===--- CloexecEpollCreate1Check.h - clang-tidy-----------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_EPOLL_CREATE1_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_EPOLL_CREATE1_H
#include "CloexecCheck.h"
namespace clang {
namespace tidy {
namespace android {
/// Finds code that uses epoll_create1() without using the EPOLL_CLOEXEC flag.
///
/// For the user-facing documentation see:
/// http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-epoll-create1.html
class CloexecEpollCreate1Check : public CloexecCheck {
public:
CloexecEpollCreate1Check(StringRef Name, ClangTidyContext *Context)
: CloexecCheck(Name, Context) {}
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
};
} // namespace android
} // namespace tidy
} // namespace clang
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_EPOLL_CREATE1_H

View File

@@ -0,0 +1,36 @@
//===--- CloexecEpollCreateCheck.cpp - clang-tidy--------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "CloexecEpollCreateCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
namespace android {
void CloexecEpollCreateCheck::registerMatchers(MatchFinder *Finder) {
registerMatchersImpl(
Finder, functionDecl(returns(isInteger()), hasName("epoll_create"),
hasParameter(0, hasType(isInteger()))));
}
void CloexecEpollCreateCheck::check(const MatchFinder::MatchResult &Result) {
replaceFunc(Result,
"prefer epoll_create() to epoll_create1() "
"because epoll_create1() allows "
"EPOLL_CLOEXEC",
"epoll_create1(EPOLL_CLOEXEC)");
}
} // namespace android
} // namespace tidy
} // namespace clang

View File

@@ -0,0 +1,35 @@
//===--- CloexecEpollCreateCheck.h - clang-tidy------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_EPOLL_CREATE_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_EPOLL_CREATE_H
#include "CloexecCheck.h"
namespace clang {
namespace tidy {
namespace android {
/// epoll_create() is better to be replaced by epoll_create1().
///
/// For the user-facing documentation see:
/// http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-epoll-create.html
class CloexecEpollCreateCheck : public CloexecCheck {
public:
CloexecEpollCreateCheck(StringRef Name, ClangTidyContext *Context)
: CloexecCheck(Name, Context) {}
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
};
} // namespace android
} // namespace tidy
} // namespace clang
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_EPOLL_CREATE_H

View File

@@ -0,0 +1,36 @@
//===--- CloexecFopenCheck.cpp - clang-tidy--------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details. //
//===----------------------------------------------------------------------===//
#include "CloexecFopenCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Type.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Lex/Lexer.h"
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
namespace android {
void CloexecFopenCheck::registerMatchers(MatchFinder *Finder) {
auto CharPointerType = hasType(pointerType(pointee(isAnyCharacter())));
registerMatchersImpl(Finder,
functionDecl(isExternC(), returns(asString("FILE *")),
hasName("fopen"),
hasParameter(0, CharPointerType),
hasParameter(1, CharPointerType)));
}
void CloexecFopenCheck::check(const MatchFinder::MatchResult &Result) {
insertStringFlag(Result, /*Mode=*/'e', /*ArgPos=*/1);
}
} // namespace android
} // namespace tidy
} // namespace clang

View File

@@ -0,0 +1,38 @@
//===--- CloexecFopenCheck.h - clang-tidy------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source //
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_FOPEN_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_FOPEN_H
#include "CloexecCheck.h"
namespace clang {
namespace tidy {
namespace android {
/// fopen() is suggested to include "e" in their mode string; like "re" would be
/// better than "r".
///
/// This check only works when corresponding argument is StringLiteral. No
/// constant propagation.
///
/// http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-fopen.html
class CloexecFopenCheck : public CloexecCheck {
public:
CloexecFopenCheck(StringRef Name, ClangTidyContext *Context)
: CloexecCheck(Name, Context) {}
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
};
} // namespace android
} // namespace tidy
} // namespace clang
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_FOPEN_H

View File

@@ -0,0 +1,33 @@
//===--- CloexecInotifyInit1Check.cpp - clang-tidy-------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "CloexecInotifyInit1Check.h"
#include "../utils/ASTUtils.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
namespace android {
void CloexecInotifyInit1Check::registerMatchers(MatchFinder *Finder) {
registerMatchersImpl(
Finder, functionDecl(returns(isInteger()), hasName("inotify_init1"),
hasParameter(0, hasType(isInteger()))));
}
void CloexecInotifyInit1Check::check(const MatchFinder::MatchResult &Result) {
insertMacroFlag(Result, /*MarcoFlag=*/"IN_CLOEXEC", /*ArgPos=*/0);
}
} // namespace android
} // namespace tidy
} // namespace clang

View File

@@ -0,0 +1,35 @@
//===--- CloexecInotifyInit1Check.h - clang-tidy-----------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_INOTIFY_INIT1_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_INOTIFY_INIT1_H
#include "CloexecCheck.h"
namespace clang {
namespace tidy {
namespace android {
/// Finds code that uses inotify_init1() without using the IN_CLOEXEC flag.
///
/// For the user-facing documentation see:
/// http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-inotify-init1.html
class CloexecInotifyInit1Check : public CloexecCheck {
public:
CloexecInotifyInit1Check(StringRef Name, ClangTidyContext *Context)
: CloexecCheck(Name, Context) {}
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
};
} // namespace android
} // namespace tidy
} // namespace clang
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_INOTIFY_INIT1_H

Some files were not shown because too many files have changed in this diff Show More