[PATCH] D36759: [clang-tidy] Use CloexecCheck as base class of CloexecFopenCheck.

Chih-Hung Hsieh via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 15 11:06:07 PDT 2017


chh created this revision.

Simplify registerMatchers and check functions in CloexecFopenCheck.


https://reviews.llvm.org/D36759

Files:
  clang-tidy/android/CloexecFopenCheck.cpp
  clang-tidy/android/CloexecFopenCheck.h


Index: clang-tidy/android/CloexecFopenCheck.h
===================================================================
--- clang-tidy/android/CloexecFopenCheck.h
+++ clang-tidy/android/CloexecFopenCheck.h
@@ -10,7 +10,7 @@
 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_FOPEN_H
 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_FOPEN_H
 
-#include "../ClangTidy.h"
+#include "CloexecCheck.h"
 
 namespace clang {
 namespace tidy {
@@ -23,10 +23,10 @@
 /// constant propagation.
 ///
 /// http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-fopen.html
-class CloexecFopenCheck : public ClangTidyCheck {
+class CloexecFopenCheck : public CloexecCheck {
 public:
   CloexecFopenCheck(StringRef Name, ClangTidyContext *Context)
-      : ClangTidyCheck(Name, Context) {}
+      : CloexecCheck(Name, Context) {}
   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
 };
Index: clang-tidy/android/CloexecFopenCheck.cpp
===================================================================
--- clang-tidy/android/CloexecFopenCheck.cpp
+++ clang-tidy/android/CloexecFopenCheck.cpp
@@ -18,55 +18,17 @@
 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, 'e', 1);
 }
 
 } // namespace android


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D36759.111209.patch
Type: text/x-patch
Size: 3660 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170815/1e3e5ec2/attachment.bin>


More information about the cfe-commits mailing list