Skip to content

Commit

Permalink
[clang-tidy][Part2] Add a new module Android and three new checks
Browse files Browse the repository at this point in the history
Summary: -- creat() should be replaced by open(). [android-creat-usage] 

Reviewers: chh, alexfh, aaron.ballman, hokein

Reviewed By: hokein

Subscribers: JDevlieghere, srhines, mgorny, xazax.hun

Tags: #clang-tools-extra

Differential Revision: https://reviews.llvm.org/D33745

llvm-svn: 306708
  • Loading branch information
yan-wang-google committed Jun 29, 2017
1 parent 4f98ab3 commit 0b97414
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 0 deletions.
2 changes: 2 additions & 0 deletions clang-tools-extra/clang-tidy/android/AndroidTidyModule.cpp
Expand Up @@ -10,6 +10,7 @@
#include "../ClangTidy.h"
#include "../ClangTidyModule.h"
#include "../ClangTidyModuleRegistry.h"
#include "CloexecCreatCheck.h"
#include "FileOpenFlagCheck.h"

using namespace clang::ast_matchers;
Expand All @@ -23,6 +24,7 @@ class AndroidModule : public ClangTidyModule {
public:
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
CheckFactories.registerCheck<FileOpenFlagCheck>("android-file-open-flag");
CheckFactories.registerCheck<CloexecCreatCheck>("android-cloexec-creat");
}
};

Expand Down
1 change: 1 addition & 0 deletions clang-tools-extra/clang-tidy/android/CMakeLists.txt
Expand Up @@ -2,6 +2,7 @@ set(LLVM_LINK_COMPONENTS support)

add_clang_library(clangTidyAndroidModule
AndroidTidyModule.cpp
CloexecCreatCheck.cpp
FileOpenFlagCheck.cpp

LINK_LIBS
Expand Down
59 changes: 59 additions & 0 deletions clang-tools-extra/clang-tidy/android/CloexecCreatCheck.cpp
@@ -0,0 +1,59 @@
//===--- CloexecCreatCheck.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 "CloexecCreatCheck.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 {

void CloexecCreatCheck::registerMatchers(MatchFinder *Finder) {
auto CharPointerType = hasType(pointerType(pointee(isAnyCharacter())));
auto MODETType = hasType(namedDecl(hasName("mode_t")));

Finder->addMatcher(
callExpr(callee(functionDecl(isExternC(), returns(isInteger()),
hasName("creat"),
hasParameter(0, CharPointerType),
hasParameter(1, MODETType))
.bind("funcDecl")))
.bind("creatFn"),
this);
}

void CloexecCreatCheck::check(const MatchFinder::MatchResult &Result) {
const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>("creatFn");
const SourceManager &SM = *Result.SourceManager;

const std::string &ReplacementText =
(Twine("open (") +
Lexer::getSourceText(CharSourceRange::getTokenRange(
MatchedCall->getArg(0)->getSourceRange()),
SM, Result.Context->getLangOpts()) +
", O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, " +
Lexer::getSourceText(CharSourceRange::getTokenRange(
MatchedCall->getArg(1)->getSourceRange()),
SM, Result.Context->getLangOpts()) +
")")
.str();

diag(MatchedCall->getLocStart(),
"prefer open() to creat() because open() allows O_CLOEXEC")
<< FixItHint::CreateReplacement(MatchedCall->getSourceRange(),
ReplacementText);
}

} // namespace android
} // namespace tidy
} // namespace clang
35 changes: 35 additions & 0 deletions clang-tools-extra/clang-tidy/android/CloexecCreatCheck.h
@@ -0,0 +1,35 @@
//===--- CloexecCreatCheck.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_CREAT_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ANDROID_CLOEXEC_CREAT_H

#include "../ClangTidy.h"

namespace clang {
namespace tidy {
namespace android {

/// creat() is better to be replaced by open().
/// Find the usage of creat() and redirect user to use open().

/// http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-creat.html
class CloexecCreatCheck : public ClangTidyCheck {
public:
CloexecCreatCheck(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_CREAT_H
5 changes: 5 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Expand Up @@ -57,6 +57,11 @@ The improvements are...
Improvements to clang-tidy
--------------------------

- New `android-cloexec-creat
<http://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-creat.html>`_ check

Detect usage of ``creat()``.

- New `android-file-open-flag
<http://clang.llvm.org/extra/clang-tidy/checks/android-file-open-flag.html>`_ check

Expand Down
16 changes: 16 additions & 0 deletions clang-tools-extra/docs/clang-tidy/checks/android-cloexec-creat.rst
@@ -0,0 +1,16 @@
.. title:: clang-tidy - android-cloexec-creat

android-cloexec-creat
=====================

The usage of ``creat()`` is not recommended, it's better to use ``open()``.

Examples:

.. code-block:: c++

int fd = creat(path, mode);

// becomes

int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, mode);
35 changes: 35 additions & 0 deletions clang-tools-extra/test/clang-tidy/android-cloexec-creat.cpp
@@ -0,0 +1,35 @@
// RUN: %check_clang_tidy %s android-cloexec-creat %t

typedef int mode_t;

extern "C" int creat(const char *path, mode_t, ...);
extern "C" int create(const char *path, mode_t, ...);

void f() {
creat("filename", 0);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: prefer open() to creat() because open() allows O_CLOEXEC [android-cloexec-creat]
// CHECK-FIXES: open ("filename", O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0);
create("filename", 0);
// CHECK-MESSAGES-NOT: warning:
mode_t mode = 0755;
creat("filename", mode);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning:
// CHECK-FIXES: open ("filename", O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, mode);
}

namespace i {
int creat(const char *path, mode_t, ...);
void g() {
creat("filename", 0);
// CHECK-MESSAGES-NOT: warning:
}
} // namespace i

class C {
public:
int creat(const char *path, mode_t, ...);
void h() {
creat("filename", 0);
// CHECK-MESSAGES-NOT: warning:
}
};

0 comments on commit 0b97414

Please sign in to comment.