11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/Lex/Lexer.h"
23 const SourceManager &SM,
24 const LangOptions &LangOpts,
26 if (Arg->getBeginLoc().isMacroID())
27 return (Lexer::getSourceText(
28 CharSourceRange::getTokenRange(Arg->getSourceRange()), SM,
30 " \"" + Twine(Mode) +
"\"")
34 cast<StringLiteral>(Arg->IgnoreParenCasts())->getString();
35 return (
"\"" + SR + Twine(Mode) +
"\"").str();
39 MatchFinder *Finder,
const internal::Matcher<FunctionDecl> &Function) {
49 StringRef MacroFlag,
int ArgPos) {
50 const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>(
FuncBindingStr);
51 const auto *FlagArg = MatchedCall->getArg(ArgPos);
53 const SourceManager &SM = *Result.SourceManager;
56 Result.Context->getLangOpts(),
60 const SourceLocation EndLoc =
61 Lexer::getLocForEndOfToken(SM.getFileLoc(FlagArg->getEndLoc()), 0, SM,
62 Result.Context->getLangOpts());
64 diag(EndLoc,
"%0 should use %1 where possible")
66 << FixItHint::CreateInsertion(EndLoc, (Twine(
" | ") + MacroFlag).str());
70 StringRef WarningMsg, StringRef FixMsg) {
71 const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>(
FuncBindingStr);
72 diag(MatchedCall->getBeginLoc(), WarningMsg)
73 << FixItHint::CreateReplacement(MatchedCall->getSourceRange(), FixMsg);
77 const ast_matchers::MatchFinder::MatchResult &Result,
const char Mode,
79 const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>(
FuncBindingStr);
81 const auto *ModeArg = MatchedCall->getArg(ArgPos);
84 const auto *ModeStr = dyn_cast<StringLiteral>(ModeArg->IgnoreParenCasts());
85 if (!ModeStr || ModeStr->getString().contains(Mode))
89 ModeArg, *Result.SourceManager, Result.Context->getLangOpts(), Mode);
91 diag(ModeArg->getBeginLoc(),
"use %0 mode '%1' to set O_CLOEXEC")
92 << FD << std::string(1, Mode)
93 << FixItHint::CreateReplacement(ModeArg->getSourceRange(),
99 const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>(
FuncBindingStr);
100 const SourceManager &SM = *Result.SourceManager;
101 return Lexer::getSourceText(
102 CharSourceRange::getTokenRange(MatchedCall->getArg(N)->getSourceRange()),
103 SM, Result.Context->getLangOpts());
This file contains the declaration of the CloexecCheck class, which is the base class for all of the ...
void replaceFunc(const ast_matchers::MatchFinder::MatchResult &Result, StringRef WarningMsg, StringRef FixMsg)
Type2 is to replace the API to another function that has required the ability.
void insertMacroFlag(const ast_matchers::MatchFinder::MatchResult &Result, StringRef MacroFlag, int ArgPos)
Currently, we have three types of fixes.
StringRef getSpellingArg(const ast_matchers::MatchFinder::MatchResult &Result, int N) const
Helper function to get the spelling of a particular argument.
void insertStringFlag(const ast_matchers::MatchFinder::MatchResult &Result, char Mode, int ArgPos)
Type3 is also to add a flag to the corresponding argument, but this time, the flag is some string and...
static constexpr char FuncBindingStr[]
Binding name of the function call expression.
static constexpr char FuncDeclBindingStr[]
Binding name of the FuncDecl of a function call.
void registerMatchersImpl(ast_matchers::MatchFinder *Finder, const ast_matchers::internal::Matcher< FunctionDecl > &Function)
static std::string buildFixMsgForStringFlag(const Expr *Arg, const SourceManager &SM, const LangOptions &LangOpts, char Mode)
bool exprHasBitFlagWithSpelling(const Expr *Flags, const SourceManager &SM, const LangOptions &LangOpts, StringRef FlagName)
Checks whether a macro flag is present in the given argument.