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) +
"\"")
33 StringRef SR = cast<StringLiteral>(Arg->IgnoreParenCasts())->getString();
34 return (
"\"" + SR + Twine(Mode) +
"\"").str();
38 MatchFinder *Finder, internal::Matcher<FunctionDecl> Function) {
48 StringRef MacroFlag,
int ArgPos) {
49 const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>(
FuncBindingStr);
50 const auto *FlagArg = MatchedCall->getArg(ArgPos);
52 SourceManager &SM = *Result.SourceManager;
55 Result.Context->getLangOpts(),
59 SourceLocation EndLoc =
60 Lexer::getLocForEndOfToken(SM.getFileLoc(FlagArg->getEndLoc()), 0, SM,
61 Result.Context->getLangOpts());
63 diag(EndLoc,
"%0 should use %1 where possible")
65 << FixItHint::CreateInsertion(EndLoc, (Twine(
" | ") + MacroFlag).str());
69 StringRef WarningMsg, StringRef FixMsg) {
70 const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>(
FuncBindingStr);
71 diag(MatchedCall->getBeginLoc(), WarningMsg)
72 << FixItHint::CreateReplacement(MatchedCall->getSourceRange(), FixMsg);
76 const ast_matchers::MatchFinder::MatchResult &Result,
const char Mode,
78 const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>(
FuncBindingStr);
80 const auto *ModeArg = MatchedCall->getArg(ArgPos);
83 const auto *ModeStr = dyn_cast<StringLiteral>(ModeArg->IgnoreParenCasts());
84 if (!ModeStr || ModeStr->getString().contains(Mode))
88 ModeArg, *Result.SourceManager, Result.Context->getLangOpts(), Mode);
90 diag(ModeArg->getBeginLoc(),
"use %0 mode '%1' to set O_CLOEXEC")
91 << FD << std::string(1, Mode)
92 << FixItHint::CreateReplacement(ModeArg->getSourceRange(),
98 const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>(
FuncBindingStr);
99 const SourceManager &SM = *Result.SourceManager;
100 return Lexer::getSourceText(
101 CharSourceRange::getTokenRange(MatchedCall->getArg(N)->getSourceRange()),
102 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 registerMatchersImpl(ast_matchers::MatchFinder *Finder, ast_matchers::internal::Matcher< FunctionDecl > Function)
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.
static constexpr char FuncBindingStr[]
Binding name of the function call expression.
void insertStringFlag(const ast_matchers::MatchFinder::MatchResult &Result, const char Mode, const 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 FuncDeclBindingStr[]
Binding name of the FuncDecl of a function call.
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.