summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clang-tidy/android/CloexecOpenCheck.cpp
diff options
context:
space:
mode:
authorYan Wang <yawanng@google.com>2017-06-29 19:13:29 +0000
committerYan Wang <yawanng@google.com>2017-06-29 19:13:29 +0000
commit600a6133ad65fa5f0ef9d5aad4b353e2bc32cedc (patch)
treef7c02ca3682a4ce77f9db2bf021511db047bc43b /clang-tools-extra/clang-tidy/android/CloexecOpenCheck.cpp
parent99886f09a13127f676e5136e1c639021f35de02b (diff)
downloadbcm5719-llvm-600a6133ad65fa5f0ef9d5aad4b353e2bc32cedc.tar.gz
bcm5719-llvm-600a6133ad65fa5f0ef9d5aad4b353e2bc32cedc.zip
[clang-tidy] Rename android-file-open-flag and fix a bug
Summary: 1. Rename android-file-open-flag to android-cloexec-open. 2. Handle a case when the function is passed as an argument of a function-like macro. Reviewers: chh Reviewed By: chh Subscribers: srhines, mgorny, JDevlieghere, xazax.hun, cfe-commits Tags: #clang-tools-extra Differential Revision: https://reviews.llvm.org/D34633 llvm-svn: 306728
Diffstat (limited to 'clang-tools-extra/clang-tidy/android/CloexecOpenCheck.cpp')
-rw-r--r--clang-tools-extra/clang-tidy/android/CloexecOpenCheck.cpp100
1 files changed, 100 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-tidy/android/CloexecOpenCheck.cpp b/clang-tools-extra/clang-tidy/android/CloexecOpenCheck.cpp
new file mode 100644
index 00000000000..4dfe3951d93
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/android/CloexecOpenCheck.cpp
@@ -0,0 +1,100 @@
+//===--- CloexecOpenCheck.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 "CloexecOpenCheck.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 {
+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())));
+
+ Finder->addMatcher(
+ callExpr(callee(functionDecl(isExternC(), returns(isInteger()),
+ hasAnyName("open", "open64"),
+ hasParameter(0, CharPointerType),
+ hasParameter(1, hasType(isInteger())))
+ .bind("funcDecl")))
+ .bind("openFn"),
+ this);
+ Finder->addMatcher(
+ callExpr(callee(functionDecl(isExternC(), returns(isInteger()),
+ hasName("openat"),
+ hasParameter(0, hasType(isInteger())),
+ hasParameter(1, CharPointerType),
+ hasParameter(2, hasType(isInteger())))
+ .bind("funcDecl")))
+ .bind("openatFn"),
+ this);
+}
+
+void CloexecOpenCheck::check(const MatchFinder::MatchResult &Result) {
+ const Expr *FlagArg = nullptr;
+ if (const auto *OpenFnCall = Result.Nodes.getNodeAs<CallExpr>("openFn"))
+ FlagArg = OpenFnCall->getArg(1);
+ else if (const auto *OpenFnCall =
+ Result.Nodes.getNodeAs<CallExpr>("openatFn"))
+ FlagArg = OpenFnCall->getArg(2);
+ assert(FlagArg);
+
+ const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>("funcDecl");
+
+ // Check the required flag.
+ SourceManager &SM = *Result.SourceManager;
+ if (hasCloseOnExecFlag(FlagArg->IgnoreParenCasts(), SM,
+ Result.Context->getLangOpts()))
+ return;
+
+ SourceLocation EndLoc =
+ Lexer::getLocForEndOfToken(SM.getFileLoc(FlagArg->getLocEnd()), 0, SM,
+ Result.Context->getLangOpts());
+
+ diag(EndLoc, "%0 should use %1 where possible")
+ << FD << O_CLOEXEC
+ << FixItHint::CreateInsertion(EndLoc, (Twine(" | ") + O_CLOEXEC).str());
+}
+
+} // namespace android
+} // namespace tidy
+} // namespace clang
OpenPOWER on IntegriCloud