summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clang-tidy/android/CloexecFopenCheck.cpp
diff options
context:
space:
mode:
authorChih-Hung Hsieh <chh@google.com>2017-08-16 16:59:26 +0000
committerChih-Hung Hsieh <chh@google.com>2017-08-16 16:59:26 +0000
commitfec506daaa4f2c4d0d3b449fbf3901bfd6270b70 (patch)
tree7ed03aa8a0b641dd5618c23f822fba163bada38d /clang-tools-extra/clang-tidy/android/CloexecFopenCheck.cpp
parentbf9751760a3d60f96ce9c0ad2cb75929008b079b (diff)
downloadbcm5719-llvm-fec506daaa4f2c4d0d3b449fbf3901bfd6270b70.tar.gz
bcm5719-llvm-fec506daaa4f2c4d0d3b449fbf3901bfd6270b70.zip
[clang-tidy] Use CloexecCheck as base class.
Summary: Simplify registerMatchers and check functions in CloexecCreatCheck, CloexecSocketCheck, CloexecFopenCheck, and CloexecOpenCheck. Differential Revision: https://reviews.llvm.org/D36761 llvm-svn: 311020
Diffstat (limited to 'clang-tools-extra/clang-tidy/android/CloexecFopenCheck.cpp')
-rw-r--r--clang-tools-extra/clang-tidy/android/CloexecFopenCheck.cpp50
1 files changed, 6 insertions, 44 deletions
diff --git a/clang-tools-extra/clang-tidy/android/CloexecFopenCheck.cpp b/clang-tools-extra/clang-tidy/android/CloexecFopenCheck.cpp
index 06dc5bd66bd..33058d6e549 100644
--- a/clang-tools-extra/clang-tidy/android/CloexecFopenCheck.cpp
+++ b/clang-tools-extra/clang-tidy/android/CloexecFopenCheck.cpp
@@ -18,55 +18,17 @@ namespace clang {
namespace tidy {
namespace android {
-namespace {
-static const char MODE = 'e';
-
-// Build the replace text. If it's string constant, add 'e' directly in the end
-// of the string. Else, add "e".
-std::string BuildReplaceText(const Expr *Arg, const SourceManager &SM,
- const LangOptions &LangOpts) {
- 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
-
void CloexecFopenCheck::registerMatchers(MatchFinder *Finder) {
auto CharPointerType = hasType(pointerType(pointee(isAnyCharacter())));
-
- Finder->addMatcher(
- callExpr(callee(functionDecl(isExternC(), returns(asString("FILE *")),
- hasName("fopen"),
- hasParameter(0, CharPointerType),
- hasParameter(1, CharPointerType))
- .bind("funcDecl")))
- .bind("fopenFn"),
- this);
+ registerMatchersImpl(Finder,
+ functionDecl(isExternC(), returns(asString("FILE *")),
+ hasName("fopen"),
+ hasParameter(0, CharPointerType),
+ hasParameter(1, CharPointerType)));
}
void CloexecFopenCheck::check(const MatchFinder::MatchResult &Result) {
- const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>("fopenFn");
- const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>("funcDecl");
- const Expr *ModeArg = MatchedCall->getArg(1);
-
- // Check if the 'e' 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 = BuildReplaceText(
- ModeArg, *Result.SourceManager, Result.Context->getLangOpts());
-
- diag(ModeArg->getLocStart(), "use %0 mode 'e' to set O_CLOEXEC")
- << FD
- << FixItHint::CreateReplacement(ModeArg->getSourceRange(),
- ReplacementText);
+ insertStringFlag(Result, /*Mode=*/'e', /*ArgPos=*/1);
}
} // namespace android
OpenPOWER on IntegriCloud