blob: 8473f1ab81c8dcabf4c793be44623836e0bd292e [file] [log] [blame]
Yan Wangb21739f2017-08-10 22:09:22 +00001//===--- CloexecCheck.cpp - clang-tidy-------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "CloexecCheck.h"
11#include "../utils/ASTUtils.h"
12#include "clang/AST/ASTContext.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
14#include "clang/Lex/Lexer.h"
15
16using namespace clang::ast_matchers;
17
18namespace clang {
19namespace tidy {
20namespace android {
21
22namespace {
Yan Wangb21739f2017-08-10 22:09:22 +000023// Helper function to form the correct string mode for Type3.
24// Build the replace text. If it's string constant, add <Mode> directly in the
25// end of the string. Else, add <Mode>.
26std::string buildFixMsgForStringFlag(const Expr *Arg, const SourceManager &SM,
27 const LangOptions &LangOpts, char Mode) {
28 if (Arg->getLocStart().isMacroID())
29 return (Lexer::getSourceText(
30 CharSourceRange::getTokenRange(Arg->getSourceRange()), SM,
31 LangOpts) +
32 " \"" + Twine(Mode) + "\"")
33 .str();
34
35 StringRef SR = cast<StringLiteral>(Arg->IgnoreParenCasts())->getString();
36 return ("\"" + SR + Twine(Mode) + "\"").str();
37}
38} // namespace
39
Chih-Hung Hsieha54d3792017-08-16 19:13:35 +000040const char *CloexecCheck::FuncDeclBindingStr = "funcDecl";
Chih-Hung Hsiehfec506d2017-08-16 16:59:26 +000041
Chih-Hung Hsieha54d3792017-08-16 19:13:35 +000042const char *CloexecCheck::FuncBindingStr ="func";
Chih-Hung Hsiehfec506d2017-08-16 16:59:26 +000043
Yan Wangb21739f2017-08-10 22:09:22 +000044void CloexecCheck::registerMatchersImpl(
45 MatchFinder *Finder, internal::Matcher<FunctionDecl> Function) {
46 // We assume all the checked APIs are C functions.
47 Finder->addMatcher(
48 callExpr(
49 callee(functionDecl(isExternC(), Function).bind(FuncDeclBindingStr)))
50 .bind(FuncBindingStr),
51 this);
52}
53
54void 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;
60
61 if (utils::exprHasBitFlagWithSpelling(FlagArg->IgnoreParenCasts(), SM,
62 Result.Context->getLangOpts(),
63 MacroFlag))
64 return;
65
66 SourceLocation EndLoc =
67 Lexer::getLocForEndOfToken(SM.getFileLoc(FlagArg->getLocEnd()), 0, SM,
68 Result.Context->getLangOpts());
69
70 diag(EndLoc, "%0 should use %1 where possible")
71 << FD << MacroFlag
72 << FixItHint::CreateInsertion(EndLoc, (Twine(" | ") + MacroFlag).str());
73}
74
75void CloexecCheck::replaceFunc(const MatchFinder::MatchResult &Result,
76 StringRef WarningMsg, StringRef FixMsg) {
77 const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>(FuncBindingStr);
78 diag(MatchedCall->getLocStart(), WarningMsg)
79 << FixItHint::CreateReplacement(MatchedCall->getSourceRange(), FixMsg);
80}
81
82void CloexecCheck::insertStringFlag(
83 const ast_matchers::MatchFinder::MatchResult &Result, const char Mode,
84 const int ArgPos) {
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);
88
89 // Check if the <Mode> may be in the mode string.
90 const auto *ModeStr = dyn_cast<StringLiteral>(ModeArg->IgnoreParenCasts());
91 if (!ModeStr || (ModeStr->getString().find(Mode) != StringRef::npos))
92 return;
93
94 const std::string &ReplacementText = buildFixMsgForStringFlag(
95 ModeArg, *Result.SourceManager, Result.Context->getLangOpts(), Mode);
96
97 diag(ModeArg->getLocStart(), "use %0 mode '%1' to set O_CLOEXEC")
98 << FD << std::string(1, Mode)
99 << FixItHint::CreateReplacement(ModeArg->getSourceRange(),
100 ReplacementText);
101}
102
Chih-Hung Hsieh56650e72017-08-14 17:04:16 +0000103StringRef CloexecCheck::getSpellingArg(const MatchFinder::MatchResult &Result,
104 int N) const {
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());
110}
111
Yan Wangb21739f2017-08-10 22:09:22 +0000112} // namespace android
113} // namespace tidy
114} // namespace clang