clang-tools  8.0.0
CloexecCheck.cpp
Go to the documentation of this file.
1 //===--- 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 
16 using namespace clang::ast_matchers;
17 
18 namespace clang {
19 namespace tidy {
20 namespace android {
21 
22 namespace {
23 // 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>.
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,
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 
40 const char *CloexecCheck::FuncDeclBindingStr = "funcDecl";
41 
42 const char *CloexecCheck::FuncBindingStr ="func";
43 
44 void 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 
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;
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->getEndLoc()), 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 
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);
80 }
81 
82 void 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->getBeginLoc(), "use %0 mode '%1' to set O_CLOEXEC")
98  << FD << std::string(1, Mode)
99  << FixItHint::CreateReplacement(ModeArg->getSourceRange(),
100  ReplacementText);
101 }
102 
103 StringRef 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 
112 } // namespace android
113 } // namespace tidy
114 } // namespace clang
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:43
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++ -*-===//