Index: clang-tidy/android/AndroidTidyModule.cpp =================================================================== --- clang-tidy/android/AndroidTidyModule.cpp +++ clang-tidy/android/AndroidTidyModule.cpp @@ -12,6 +12,7 @@ #include "../ClangTidyModuleRegistry.h" #include "CreatUsageCheck.h" #include "FileOpenFlagCheck.h" +#include "FopenModeCheck.h" using namespace clang::ast_matchers; @@ -25,6 +26,7 @@ void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { CheckFactories.registerCheck("android-file-open-flag"); CheckFactories.registerCheck("android-creat-usage"); + CheckFactories.registerCheck("android-fopen-mode"); } }; Index: clang-tidy/android/CMakeLists.txt =================================================================== --- clang-tidy/android/CMakeLists.txt +++ clang-tidy/android/CMakeLists.txt @@ -4,6 +4,7 @@ AndroidTidyModule.cpp CreatUsageCheck.cpp FileOpenFlagCheck.cpp + FopenModeCheck.cpp LINK_LIBS clangAST Index: clang-tidy/android/FopenModeCheck.h =================================================================== --- /dev/null +++ clang-tidy/android/FopenModeCheck.h @@ -0,0 +1,38 @@ +//===--- FopenModeCheck.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_FOPEN_MODE_STRING_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_FOPEN_MODE_STRING_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-fopen-mode.html +class FopenModeCheck : public ClangTidyCheck { +public: + FopenModeCheck(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_FOPEN_MODE_STRING_H Index: clang-tidy/android/FopenModeCheck.cpp =================================================================== --- /dev/null +++ clang-tidy/android/FopenModeCheck.cpp @@ -0,0 +1,83 @@ +//===--- FopenModeCheck.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 "FopenModeCheck.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) + .str() + + " \"" + MODE + "\""; + + StringRef SR = cast(Arg->IgnoreParenCasts())->getString(); + return "\"" + SR.str() + MODE + "\""; +} +} // namespace + +void FopenModeCheck::registerMatchers(MatchFinder *Finder) { + auto CharPointerType = hasType(pointerType(pointee(isAnyCharacter()))); + + Finder->addMatcher( + callExpr(callee(functionDecl(isExternC(), hasName("fopen"), + hasParameter(0, CharPointerType), + hasParameter(1, CharPointerType), + returns(asString("FILE *"))) + .bind("funcDecl"))) + .bind("fopenFn"), + this); +} + +void FopenModeCheck::check(const MatchFinder::MatchResult &Result) { + const auto *MatchedCall = Result.Nodes.getNodeAs("fopenFn"); + const auto *FD = Result.Nodes.getNodeAs("funcDecl"); + const Expr *ModeArg = MatchedCall->getArg(1); + + // Check if the 'e' may be in the mode string. + bool MayHaveE = false; + if (const auto *ModeStr = + dyn_cast(ModeArg->IgnoreParenCasts())) { + if (ModeStr->getString().find(MODE) != StringRef::npos) + MayHaveE = true; + } else + MayHaveE = true; + + if (MayHaveE) { + return; + } + + const std::string &ReplacementText = BuildReplaceText( + ModeArg, *Result.SourceManager, Result.Context->getLangOpts()); + + SourceRange ModeRange(ModeArg->getLocStart(), ModeArg->getLocEnd()); + + diag(ModeRange.getBegin(), "use %0 mode 'e' to set O_CLOEXEC") + << FD << FixItHint::CreateReplacement(ModeRange, ReplacementText); +} + +} // namespace android +} // namespace tidy +} // namespace clang Index: docs/ReleaseNotes.rst =================================================================== --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -68,6 +68,11 @@ Checks if the required file flag ``O_CLOEXEC`` exists in ``open()``, ``open64()`` and ``openat()``. +- New `android-fopen-mode + `_ check + + Checks if the required mode ``e`` exists in the mode argument of ``fopen()``. + - New `cert-dcl21-cpp `_ check Index: docs/clang-tidy/checks/android-fopen-mode.rst =================================================================== --- /dev/null +++ docs/clang-tidy/checks/android-fopen-mode.rst @@ -0,0 +1,18 @@ +.. title:: clang-tidy - android-fopen-mode + +android-fopen-mode +================== + +``fopen()`` should include ``e`` in their mode string; so ``re`` would be +valid. This is equivalent to having set ``FD_CLOEXEC on`` that descriptor. + +Examples: + +.. code-block:: c++ + + fopen("fn", "r"); + + // becomes + + fopen("fn", "re"); + Index: test/clang-tidy/android-fopen-mode.cpp =================================================================== --- /dev/null +++ test/clang-tidy/android-fopen-mode.cpp @@ -0,0 +1,51 @@ +// RUN: %check_clang_tidy %s android-fopen-mode %t + +#define FILE_OPEN_RO "r" + +typedef int FILE; + +extern "C" FILE *fopen(const char *filename, const char *mode, ...); +extern "C" FILE *open(const char *filename, const char *mode, ...); + +void f() { + fopen("filename", "r"); + // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: use 'fopen' mode 'e' to set O_CLOEXEC [android-fopen-mode] + // CHECK-FIXES: fopen("filename", "re"); + + fopen("filename", FILE_OPEN_RO); + // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: use 'fopen' mode 'e' + // CHECK-FIXES: fopen("filename", FILE_OPEN_RO "e"); + + fopen("filename", "er"); + // CHECK-MESSAGES-NOT: warning: + fopen("filename", "re"); + // CHECK-MESSAGES-NOT: warning: + fopen("filename", "e"); + // CHECK-MESSAGES-NOT: warning: + open("filename", "e"); + // CHECK-MESSAGES-NOT: warning: + + char *str = "r"; + fopen("filename", str); + // CHECK-MESSAGES-NOT: warning: + char arr[2] = "r"; + fopen("filename", arr); + // CHECK-MESSAGES-NOT: warning: +} + +namespace i { +int *fopen(const char *filename, const char *mode, ...); +void g() { + fopen("filename", "e"); + // CHECK-MESSAGES-NOT: warning: +} +} // namespace i + +class C { +public: + int *fopen(const char *filename, const char *mode, ...); + void h() { + fopen("filename", "e"); + // CHECK-MESSAGES-NOT: warning: + } +};