summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clang-tidy/android
diff options
context:
space:
mode:
Diffstat (limited to 'clang-tools-extra/clang-tidy/android')
-rw-r--r--clang-tools-extra/clang-tidy/android/AndroidTidyModule.cpp2
-rw-r--r--clang-tools-extra/clang-tidy/android/CMakeLists.txt1
-rw-r--r--clang-tools-extra/clang-tidy/android/CloexecOpenCheck.cpp32
-rw-r--r--clang-tools-extra/clang-tidy/android/CloexecSocketCheck.cpp57
-rw-r--r--clang-tools-extra/clang-tidy/android/CloexecSocketCheck.h35
5 files changed, 98 insertions, 29 deletions
diff --git a/clang-tools-extra/clang-tidy/android/AndroidTidyModule.cpp b/clang-tools-extra/clang-tidy/android/AndroidTidyModule.cpp
index 701dd53dbb1..119f3c0db8b 100644
--- a/clang-tools-extra/clang-tidy/android/AndroidTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/android/AndroidTidyModule.cpp
@@ -13,6 +13,7 @@
#include "CloexecCreatCheck.h"
#include "CloexecFopenCheck.h"
#include "CloexecOpenCheck.h"
+#include "CloexecSocketCheck.h"
using namespace clang::ast_matchers;
@@ -27,6 +28,7 @@ public:
CheckFactories.registerCheck<CloexecCreatCheck>("android-cloexec-creat");
CheckFactories.registerCheck<CloexecFopenCheck>("android-cloexec-fopen");
CheckFactories.registerCheck<CloexecOpenCheck>("android-cloexec-open");
+ CheckFactories.registerCheck<CloexecSocketCheck>("android-cloexec-socket");
}
};
diff --git a/clang-tools-extra/clang-tidy/android/CMakeLists.txt b/clang-tools-extra/clang-tidy/android/CMakeLists.txt
index 83caa6e0428..a68c2d2c98a 100644
--- a/clang-tools-extra/clang-tidy/android/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/android/CMakeLists.txt
@@ -5,6 +5,7 @@ add_clang_library(clangTidyAndroidModule
CloexecCreatCheck.cpp
CloexecFopenCheck.cpp
CloexecOpenCheck.cpp
+ CloexecSocketCheck.cpp
LINK_LIBS
clangAST
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 =
diff --git a/clang-tools-extra/clang-tidy/android/CloexecSocketCheck.cpp b/clang-tools-extra/clang-tidy/android/CloexecSocketCheck.cpp
new file mode 100644
index 00000000000..66a77b87594
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/android/CloexecSocketCheck.cpp
@@ -0,0 +1,57 @@
+//===--- CloexecSocketCheck.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 "CloexecSocketCheck.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 {
+
+static constexpr const char *SOCK_CLOEXEC = "SOCK_CLOEXEC";
+
+void CloexecSocketCheck::registerMatchers(MatchFinder *Finder) {
+ Finder->addMatcher(
+ callExpr(callee(functionDecl(isExternC(), returns(isInteger()),
+ hasName("socket"),
+ hasParameter(0, hasType(isInteger())),
+ hasParameter(1, hasType(isInteger())),
+ hasParameter(2, hasType(isInteger())))
+ .bind("funcDecl")))
+ .bind("socketFn"),
+ this);
+}
+
+void CloexecSocketCheck::check(const MatchFinder::MatchResult &Result) {
+ const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>("socketFn");
+ const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>("funcDecl");
+ const Expr *FlagArg = MatchedCall->getArg(1);
+ SourceManager &SM = *Result.SourceManager;
+
+ if (utils::exprHasBitFlagWithSpelling(FlagArg->IgnoreParenCasts(), SM,
+ Result.Context->getLangOpts(), SOCK_CLOEXEC))
+ return;
+
+ SourceLocation EndLoc =
+ Lexer::getLocForEndOfToken(SM.getFileLoc(FlagArg->getLocEnd()), 0, SM,
+ Result.Context->getLangOpts());
+
+ diag(EndLoc, "%0 should use %1 where possible")
+ << FD << SOCK_CLOEXEC
+ << FixItHint::CreateInsertion(EndLoc,
+ (Twine(" | ") + SOCK_CLOEXEC).str());
+}
+
+} // namespace android
+} // namespace tidy
+} // namespace clang
diff --git a/clang-tools-extra/clang-tidy/android/CloexecSocketCheck.h b/clang-tools-extra/clang-tidy/android/CloexecSocketCheck.h
new file mode 100644
index 00000000000..9334a64ecf0
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/android/CloexecSocketCheck.h
@@ -0,0 +1,35 @@
+//===--- CloexecSocketCheck.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_SOCKET_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_SOCKET_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+namespace android {
+
+/// Finds code that uses socket() without using the SOCK_CLOEXEC flag.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-socket.html
+class CloexecSocketCheck : public ClangTidyCheck {
+public:
+ CloexecSocketCheck(StringRef Name, ClangTidyContext *Context)
+ : ClangTidyCheck(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_SOCKET_H
OpenPOWER on IntegriCloud