clang-tools 22.0.0git
CloexecCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "CloexecCheck.h"
10#include "../utils/ASTUtils.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/Lex/Lexer.h"
14
15using namespace clang::ast_matchers;
16
17namespace clang::tidy::android {
18
19// Helper function to form the correct string mode for Type3.
20// Build the replace text. If it's string constant, add <Mode> directly in the
21// end of the string. Else, add <Mode>.
22static std::string buildFixMsgForStringFlag(const Expr *Arg,
23 const SourceManager &SM,
24 const LangOptions &LangOpts,
25 char Mode) {
26 if (Arg->getBeginLoc().isMacroID())
27 return (Lexer::getSourceText(
28 CharSourceRange::getTokenRange(Arg->getSourceRange()), SM,
29 LangOpts) +
30 " \"" + Twine(Mode) + "\"")
31 .str();
32
33 StringRef SR = cast<StringLiteral>(Arg->IgnoreParenCasts())->getString();
34 return ("\"" + SR + Twine(Mode) + "\"").str();
35}
36
38 MatchFinder *Finder, internal::Matcher<FunctionDecl> Function) {
39 // We assume all the checked APIs are C functions.
40 Finder->addMatcher(
41 callExpr(
42 callee(functionDecl(isExternC(), Function).bind(FuncDeclBindingStr)))
43 .bind(FuncBindingStr),
44 this);
45}
46
47void CloexecCheck::insertMacroFlag(const MatchFinder::MatchResult &Result,
48 StringRef MacroFlag, int ArgPos) {
49 const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>(FuncBindingStr);
50 const auto *FlagArg = MatchedCall->getArg(ArgPos);
51 const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>(FuncDeclBindingStr);
52 SourceManager &SM = *Result.SourceManager;
53
54 if (utils::exprHasBitFlagWithSpelling(FlagArg->IgnoreParenCasts(), SM,
55 Result.Context->getLangOpts(),
56 MacroFlag))
57 return;
58
59 SourceLocation EndLoc =
60 Lexer::getLocForEndOfToken(SM.getFileLoc(FlagArg->getEndLoc()), 0, SM,
61 Result.Context->getLangOpts());
62
63 diag(EndLoc, "%0 should use %1 where possible")
64 << FD << MacroFlag
65 << FixItHint::CreateInsertion(EndLoc, (Twine(" | ") + MacroFlag).str());
66}
67
68void CloexecCheck::replaceFunc(const MatchFinder::MatchResult &Result,
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);
73}
74
76 const ast_matchers::MatchFinder::MatchResult &Result, const char Mode,
77 const int ArgPos) {
78 const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>(FuncBindingStr);
79 const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>(FuncDeclBindingStr);
80 const auto *ModeArg = MatchedCall->getArg(ArgPos);
81
82 // Check if the <Mode> may be in the mode string.
83 const auto *ModeStr = dyn_cast<StringLiteral>(ModeArg->IgnoreParenCasts());
84 if (!ModeStr || ModeStr->getString().contains(Mode))
85 return;
86
87 std::string ReplacementText = buildFixMsgForStringFlag(
88 ModeArg, *Result.SourceManager, Result.Context->getLangOpts(), Mode);
89
90 diag(ModeArg->getBeginLoc(), "use %0 mode '%1' to set O_CLOEXEC")
91 << FD << std::string(1, Mode)
92 << FixItHint::CreateReplacement(ModeArg->getSourceRange(),
93 ReplacementText);
94}
95
96StringRef CloexecCheck::getSpellingArg(const MatchFinder::MatchResult &Result,
97 int N) const {
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());
103}
104
105} // namespace clang::tidy::android
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.
Definition ASTUtils.cpp:38