11 #include "../utils/ASTUtils.h"
12 #include "clang/AST/ASTContext.h"
13 #include "clang/ASTMatchers/ASTMatchFinder.h"
14 #include "clang/Lex/Lexer.h"
16 using namespace clang::ast_matchers;
22 static constexpr
const char *
O_CLOEXEC =
"O_CLOEXEC";
24 void CloexecOpenCheck::registerMatchers(MatchFinder *
Finder) {
25 auto CharPointerType = hasType(pointerType(pointee(isAnyCharacter())));
28 callExpr(callee(functionDecl(isExternC(), returns(isInteger()),
29 hasAnyName(
"open",
"open64"),
30 hasParameter(0, CharPointerType),
31 hasParameter(1, hasType(isInteger())))
36 callExpr(callee(functionDecl(isExternC(), returns(isInteger()),
38 hasParameter(0, hasType(isInteger())),
39 hasParameter(1, CharPointerType),
40 hasParameter(2, hasType(isInteger())))
46 void CloexecOpenCheck::check(
const MatchFinder::MatchResult &Result) {
47 const Expr *FlagArg =
nullptr;
48 if (
const auto *OpenFnCall = Result.Nodes.getNodeAs<CallExpr>(
"openFn"))
49 FlagArg = OpenFnCall->getArg(1);
50 else if (
const auto *OpenFnCall =
51 Result.Nodes.getNodeAs<CallExpr>(
"openatFn"))
52 FlagArg = OpenFnCall->getArg(2);
55 const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>(
"funcDecl");
58 SourceManager &
SM = *Result.SourceManager;
60 Result.Context->getLangOpts(),
O_CLOEXEC))
63 SourceLocation EndLoc =
64 Lexer::getLocForEndOfToken(SM.getFileLoc(FlagArg->getLocEnd()), 0, SM,
65 Result.Context->getLangOpts());
67 diag(EndLoc,
"%0 should use %1 where possible")
69 << FixItHint::CreateInsertion(EndLoc, (Twine(
" | ") +
O_CLOEXEC).str());
static constexpr const char * O_CLOEXEC
std::unique_ptr< ast_matchers::MatchFinder > Finder
bool exprHasBitFlagWithSpelling(const Expr *Flags, const SourceManager &SM, const LangOptions &LangOpts, StringRef FlagName)
Checks whether a macro flag is present in the given argument.