summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clang-tidy/android/CloexecFopenCheck.cpp
blob: 06dc5bd66bd714001f00a588ce1c33ca7f804279 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//===--- CloexecFopenCheck.cpp - clang-tidy--------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.  //
//===----------------------------------------------------------------------===//

#include "CloexecFopenCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Type.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Lex/Lexer.h"

using namespace clang::ast_matchers;

namespace clang {
namespace tidy {
namespace android {

namespace {
static const char MODE = 'e';

// Build the replace text. If it's string constant, add 'e' directly in the end
// of the string. Else, add "e".
std::string BuildReplaceText(const Expr *Arg, const SourceManager &SM,
                             const LangOptions &LangOpts) {
  if (Arg->getLocStart().isMacroID())
    return (Lexer::getSourceText(
                CharSourceRange::getTokenRange(Arg->getSourceRange()), SM,
                LangOpts) +
            " \"" + Twine(MODE) + "\"")
        .str();

  StringRef SR = cast<StringLiteral>(Arg->IgnoreParenCasts())->getString();
  return ("\"" + SR + Twine(MODE) + "\"").str();
}
} // namespace

void CloexecFopenCheck::registerMatchers(MatchFinder *Finder) {
  auto CharPointerType = hasType(pointerType(pointee(isAnyCharacter())));

  Finder->addMatcher(
      callExpr(callee(functionDecl(isExternC(), returns(asString("FILE *")),
                                   hasName("fopen"),
                                   hasParameter(0, CharPointerType),
                                   hasParameter(1, CharPointerType))
                          .bind("funcDecl")))
          .bind("fopenFn"),
      this);
}

void CloexecFopenCheck::check(const MatchFinder::MatchResult &Result) {
  const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>("fopenFn");
  const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>("funcDecl");
  const Expr *ModeArg = MatchedCall->getArg(1);

  // Check if the 'e' may be in the mode string.
  const auto *ModeStr = dyn_cast<StringLiteral>(ModeArg->IgnoreParenCasts());
  if (!ModeStr || (ModeStr->getString().find(MODE) != StringRef::npos))
    return;

  const std::string &ReplacementText = BuildReplaceText(
      ModeArg, *Result.SourceManager, Result.Context->getLangOpts());

  diag(ModeArg->getLocStart(), "use %0 mode 'e' to set O_CLOEXEC")
      << FD
      << FixItHint::CreateReplacement(ModeArg->getSourceRange(),
                                      ReplacementText);
}

} // namespace android
} // namespace tidy
} // namespace clang
OpenPOWER on IntegriCloud