10 #include "../utils/ASTUtils.h" 11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 13 #include "clang/Lex/Lexer.h" 25 std::string buildFixMsgForStringFlag(
const Expr *Arg,
const SourceManager &SM,
26 const LangOptions &LangOpts,
char Mode) {
27 if (Arg->getBeginLoc().isMacroID())
28 return (Lexer::getSourceText(
31 " \"" + Twine(Mode) +
"\"")
34 StringRef SR = cast<StringLiteral>(Arg->IgnoreParenCasts())->getString();
35 return (
"\"" + SR + Twine(Mode) +
"\"").str();
39 const char *CloexecCheck::FuncDeclBindingStr =
"funcDecl";
41 const char *CloexecCheck::FuncBindingStr =
"func";
43 void CloexecCheck::registerMatchersImpl(
44 MatchFinder *Finder, internal::Matcher<FunctionDecl> Function) {
48 callee(functionDecl(isExternC(), Function).bind(FuncDeclBindingStr)))
49 .bind(FuncBindingStr),
53 void CloexecCheck::insertMacroFlag(
const MatchFinder::MatchResult &Result,
54 StringRef MacroFlag,
int ArgPos) {
55 const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>(FuncBindingStr);
56 const auto *FlagArg = MatchedCall->getArg(ArgPos);
57 const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>(FuncDeclBindingStr);
58 SourceManager &SM = *Result.SourceManager;
61 Result.Context->getLangOpts(),
65 SourceLocation EndLoc =
66 Lexer::getLocForEndOfToken(SM.getFileLoc(FlagArg->getEndLoc()), 0, SM,
67 Result.Context->getLangOpts());
69 diag(EndLoc,
"%0 should use %1 where possible")
71 << FixItHint::CreateInsertion(EndLoc, (Twine(
" | ") + MacroFlag).str());
74 void CloexecCheck::replaceFunc(
const MatchFinder::MatchResult &Result,
75 StringRef WarningMsg, StringRef FixMsg) {
76 const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>(FuncBindingStr);
77 diag(MatchedCall->getBeginLoc(), WarningMsg)
78 << FixItHint::CreateReplacement(MatchedCall->getSourceRange(), FixMsg);
81 void CloexecCheck::insertStringFlag(
82 const ast_matchers::MatchFinder::MatchResult &Result,
const char Mode,
84 const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>(FuncBindingStr);
85 const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>(FuncDeclBindingStr);
86 const auto *ModeArg = MatchedCall->getArg(ArgPos);
89 const auto *ModeStr = dyn_cast<StringLiteral>(ModeArg->IgnoreParenCasts());
90 if (!ModeStr || (ModeStr->getString().find(Mode) != StringRef::npos))
93 std::string ReplacementText = buildFixMsgForStringFlag(
94 ModeArg, *Result.SourceManager, Result.Context->getLangOpts(), Mode);
96 diag(ModeArg->getBeginLoc(),
"use %0 mode '%1' to set O_CLOEXEC")
97 << FD << std::string(1, Mode)
98 << FixItHint::CreateReplacement(ModeArg->getSourceRange(),
102 StringRef CloexecCheck::getSpellingArg(
const MatchFinder::MatchResult &Result,
104 const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>(FuncBindingStr);
105 const SourceManager &SM = *Result.SourceManager;
106 return Lexer::getSourceText(
108 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< Range > getTokenRange(const SourceManager &SM, const LangOptions &LangOpts, SourceLocation TokLoc)
Returns the taken range at TokLoc.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//