diff options
author | Yan Wang <yawanng@google.com> | 2017-07-12 17:43:36 +0000 |
---|---|---|
committer | Yan Wang <yawanng@google.com> | 2017-07-12 17:43:36 +0000 |
commit | b38045d02ebd412f32955769feaa6ba62f3ffd4d (patch) | |
tree | ea63c6589177cb53314d1a44aee21094cd60d889 /clang-tools-extra/clang-tidy/android/CloexecOpenCheck.cpp | |
parent | 4fc696635ded88843f235e55b29c3eefc7c4810a (diff) | |
download | bcm5719-llvm-b38045d02ebd412f32955769feaa6ba62f3ffd4d.tar.gz bcm5719-llvm-b38045d02ebd412f32955769feaa6ba62f3ffd4d.zip |
[clang-tidy] Add a new Android check "android-cloexec-socket"
Summary: socket() is better to include SOCK_CLOEXEC in its type argument to avoid the file descriptor leakage.
Reviewers: chh, Eugene.Zelenko, alexfh, hokein, aaron.ballman
Reviewed By: chh, alexfh
Subscribers: srhines, mgorny, JDevlieghere, xazax.hun, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D34913
llvm-svn: 307818
Diffstat (limited to 'clang-tools-extra/clang-tidy/android/CloexecOpenCheck.cpp')
-rw-r--r-- | clang-tools-extra/clang-tidy/android/CloexecOpenCheck.cpp | 32 |
1 files changed, 3 insertions, 29 deletions
diff --git a/clang-tools-extra/clang-tidy/android/CloexecOpenCheck.cpp b/clang-tools-extra/clang-tidy/android/CloexecOpenCheck.cpp index 4dfe3951d93..6d944aef69c 100644 --- a/clang-tools-extra/clang-tidy/android/CloexecOpenCheck.cpp +++ b/clang-tools-extra/clang-tidy/android/CloexecOpenCheck.cpp @@ -8,6 +8,7 @@ //===----------------------------------------------------------------------===// #include "CloexecOpenCheck.h" +#include "../utils/ASTUtils.h" #include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/Lex/Lexer.h" @@ -18,35 +19,8 @@ namespace clang { namespace tidy { namespace android { -namespace { static constexpr const char *O_CLOEXEC = "O_CLOEXEC"; -bool hasCloseOnExecFlag(const Expr *Flags, const SourceManager &SM, - const LangOptions &LangOpts) { - // If the Flag is an integer constant, check it. - if (isa<IntegerLiteral>(Flags)) { - if (!SM.isMacroBodyExpansion(Flags->getLocStart()) && - !SM.isMacroArgExpansion(Flags->getLocStart())) - return false; - - // Get the Marco name. - auto MacroName = Lexer::getSourceText( - CharSourceRange::getTokenRange(Flags->getSourceRange()), SM, LangOpts); - - return MacroName == O_CLOEXEC; - } - // If it's a binary OR operation. - if (const auto *BO = dyn_cast<BinaryOperator>(Flags)) - if (BO->getOpcode() == clang::BinaryOperatorKind::BO_Or) - return hasCloseOnExecFlag(BO->getLHS()->IgnoreParenCasts(), SM, - LangOpts) || - hasCloseOnExecFlag(BO->getRHS()->IgnoreParenCasts(), SM, LangOpts); - - // Otherwise, assume it has the flag. - return true; -} -} // namespace - void CloexecOpenCheck::registerMatchers(MatchFinder *Finder) { auto CharPointerType = hasType(pointerType(pointee(isAnyCharacter()))); @@ -82,8 +56,8 @@ void CloexecOpenCheck::check(const MatchFinder::MatchResult &Result) { // Check the required flag. SourceManager &SM = *Result.SourceManager; - if (hasCloseOnExecFlag(FlagArg->IgnoreParenCasts(), SM, - Result.Context->getLangOpts())) + if (utils::exprHasBitFlagWithSpelling(FlagArg->IgnoreParenCasts(), SM, + Result.Context->getLangOpts(), O_CLOEXEC)) return; SourceLocation EndLoc = |