Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[clang-tidy][Part2] Add a new module Android and three new checks
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
1 parent
4f98ab3
commit 0b97414
Showing
7 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
clang-tools-extra/clang-tidy/android/CloexecCreatCheck.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
clang-tools-extra/docs/clang-tidy/checks/android-cloexec-creat.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
35
clang-tools-extra/test/clang-tidy/android-cloexec-creat.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: | ||
} | ||
}; |