11 #include "../utils/ASTUtils.h" 12 #include "clang/AST/ASTContext.h" 13 #include "clang/ASTMatchers/ASTMatchFinder.h" 14 #include "clang/Lex/Lexer.h" 26 std::string buildFixMsgForStringFlag(
const Expr *Arg,
const SourceManager &SM,
27 const LangOptions &LangOpts,
char Mode) {
28 if (Arg->getBeginLoc().isMacroID())
29 return (Lexer::getSourceText(
30 CharSourceRange::getTokenRange(Arg->getSourceRange()), SM,
32 " \"" + Twine(Mode) +
"\"")
35 StringRef SR = cast<StringLiteral>(Arg->IgnoreParenCasts())->getString();
36 return (
"\"" + SR + Twine(Mode) +
"\"").str();
40 const char *CloexecCheck::FuncDeclBindingStr =
"funcDecl";
42 const char *CloexecCheck::FuncBindingStr =
"func";
44 void CloexecCheck::registerMatchersImpl(
45 MatchFinder *Finder, internal::Matcher<FunctionDecl> Function) {
49 callee(functionDecl(isExternC(), Function).bind(FuncDeclBindingStr)))
50 .bind(FuncBindingStr),
54 void CloexecCheck::insertMacroFlag(
const MatchFinder::MatchResult &
Result,
55 StringRef MacroFlag,
int ArgPos) {
56 const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>(FuncBindingStr);
57 const auto *FlagArg = MatchedCall->getArg(ArgPos);
58 const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>(FuncDeclBindingStr);
59 SourceManager &SM = *Result.SourceManager;
62 Result.Context->getLangOpts(),
66 SourceLocation EndLoc =
67 Lexer::getLocForEndOfToken(SM.getFileLoc(FlagArg->getEndLoc()), 0, SM,
68 Result.Context->getLangOpts());
70 diag(EndLoc,
"%0 should use %1 where possible")
72 << FixItHint::CreateInsertion(EndLoc, (Twine(
" | ") + MacroFlag).str());
75 void CloexecCheck::replaceFunc(
const MatchFinder::MatchResult &
Result,
76 StringRef WarningMsg, StringRef FixMsg) {
77 const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>(FuncBindingStr);
78 diag(MatchedCall->getBeginLoc(), WarningMsg)
79 << FixItHint::CreateReplacement(MatchedCall->getSourceRange(), FixMsg);
82 void CloexecCheck::insertStringFlag(
83 const ast_matchers::MatchFinder::MatchResult &
Result,
const char Mode,
85 const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>(FuncBindingStr);
86 const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>(FuncDeclBindingStr);
87 const auto *ModeArg = MatchedCall->getArg(ArgPos);
90 const auto *ModeStr = dyn_cast<StringLiteral>(ModeArg->IgnoreParenCasts());
91 if (!ModeStr || (ModeStr->getString().find(Mode) != StringRef::npos))
94 const std::string &ReplacementText = buildFixMsgForStringFlag(
95 ModeArg, *Result.SourceManager, Result.Context->getLangOpts(), Mode);
97 diag(ModeArg->getBeginLoc(),
"use %0 mode '%1' to set O_CLOEXEC")
98 << FD << std::string(1, Mode)
99 << FixItHint::CreateReplacement(ModeArg->getSourceRange(),
103 StringRef CloexecCheck::getSpellingArg(
const MatchFinder::MatchResult &
Result,
105 const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>(FuncBindingStr);
106 const SourceManager &SM = *Result.SourceManager;
107 return Lexer::getSourceText(
108 CharSourceRange::getTokenRange(MatchedCall->getArg(N)->getSourceRange()),
109 SM, Result.Context->getLangOpts());
bool exprHasBitFlagWithSpelling(const Expr *Flags, const SourceManager &SM, const LangOptions &LangOpts, StringRef FlagName)
Checks whether a macro flag is present in the given argument.
This file contains the declaration of the CloexecCheck class, which is the base class for all of the ...
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//