diff options
Diffstat (limited to 'clang-tools-extra/clang-tidy/android')
4 files changed, 115 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-tidy/android/AndroidTidyModule.cpp b/clang-tools-extra/clang-tidy/android/AndroidTidyModule.cpp index b93c4aab6fc..aa630986d40 100644 --- a/clang-tools-extra/clang-tidy/android/AndroidTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/android/AndroidTidyModule.cpp @@ -11,6 +11,7 @@ #include "../ClangTidyModule.h" #include "../ClangTidyModuleRegistry.h" #include "CloexecCreatCheck.h" +#include "CloexecFopenCheck.h" #include "FileOpenFlagCheck.h" using namespace clang::ast_matchers; @@ -25,6 +26,7 @@ public: void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { CheckFactories.registerCheck<FileOpenFlagCheck>("android-file-open-flag"); CheckFactories.registerCheck<CloexecCreatCheck>("android-cloexec-creat"); + CheckFactories.registerCheck<CloexecFopenCheck>("android-cloexec-fopen"); } }; diff --git a/clang-tools-extra/clang-tidy/android/CMakeLists.txt b/clang-tools-extra/clang-tidy/android/CMakeLists.txt index 626720db37f..1c0bea40d5a 100644 --- a/clang-tools-extra/clang-tidy/android/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/android/CMakeLists.txt @@ -3,6 +3,7 @@ set(LLVM_LINK_COMPONENTS support) add_clang_library(clangTidyAndroidModule AndroidTidyModule.cpp CloexecCreatCheck.cpp + CloexecFopenCheck.cpp FileOpenFlagCheck.cpp LINK_LIBS diff --git a/clang-tools-extra/clang-tidy/android/CloexecFopenCheck.cpp b/clang-tools-extra/clang-tidy/android/CloexecFopenCheck.cpp new file mode 100644 index 00000000000..06dc5bd66bd --- /dev/null +++ b/clang-tools-extra/clang-tidy/android/CloexecFopenCheck.cpp @@ -0,0 +1,74 @@ +//===--- CloexecFopenCheck.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 "CloexecFopenCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Type.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +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); +} + +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); +} + +} // namespace android +} // namespace tidy +} // namespace clang diff --git a/clang-tools-extra/clang-tidy/android/CloexecFopenCheck.h b/clang-tools-extra/clang-tidy/android/CloexecFopenCheck.h new file mode 100644 index 00000000000..71f015a9e4e --- /dev/null +++ b/clang-tools-extra/clang-tidy/android/CloexecFopenCheck.h @@ -0,0 +1,38 @@ +//===--- CloexecFopenCheck.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_FOPEN_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_FOPEN_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace android { + +/// fopen() is suggested to include "e" in their mode string; like "re" would be +/// better than "r". +/// +/// This check only works when corresponding argument is StringLiteral. No +/// constant propagation. +/// +/// http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-fopen.html +class CloexecFopenCheck : public ClangTidyCheck { +public: + CloexecFopenCheck(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_FOPEN_H |

