1 //===--- CloexecOpenCheck.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 "CloexecOpenCheck.h" 11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 13 #include "clang/Lex/Lexer.h" 14 15 using namespace clang::ast_matchers; 16 17 namespace clang { 18 namespace tidy { 19 namespace android { 20 21 namespace { 22 static constexpr const char *O_CLOEXEC = "O_CLOEXEC"; 23 24 bool hasCloseOnExecFlag(const Expr *Flags, const SourceManager &SM, 25 const LangOptions &LangOpts) { 26 // If the Flag is an integer constant, check it. 27 if (isa<IntegerLiteral>(Flags)) { 28 if (!SM.isMacroBodyExpansion(Flags->getLocStart()) && 29 !SM.isMacroArgExpansion(Flags->getLocStart())) 30 return false; 31 32 // Get the Marco name. 33 auto MacroName = Lexer::getSourceText( 34 CharSourceRange::getTokenRange(Flags->getSourceRange()), SM, LangOpts); 35 36 return MacroName == O_CLOEXEC; 37 } 38 // If it's a binary OR operation. 39 if (const auto *BO = dyn_cast<BinaryOperator>(Flags)) 40 if (BO->getOpcode() == clang::BinaryOperatorKind::BO_Or) 41 return hasCloseOnExecFlag(BO->getLHS()->IgnoreParenCasts(), SM, 42 LangOpts) || 43 hasCloseOnExecFlag(BO->getRHS()->IgnoreParenCasts(), SM, LangOpts); 44 45 // Otherwise, assume it has the flag. 46 return true; 47 } 48 } // namespace 49 50 void CloexecOpenCheck::registerMatchers(MatchFinder *Finder) { 51 auto CharPointerType = hasType(pointerType(pointee(isAnyCharacter()))); 52 53 Finder->addMatcher( 54 callExpr(callee(functionDecl(isExternC(), returns(isInteger()), 55 hasAnyName("open", "open64"), 56 hasParameter(0, CharPointerType), 57 hasParameter(1, hasType(isInteger()))) 58 .bind("funcDecl"))) 59 .bind("openFn"), 60 this); 61 Finder->addMatcher( 62 callExpr(callee(functionDecl(isExternC(), returns(isInteger()), 63 hasName("openat"), 64 hasParameter(0, hasType(isInteger())), 65 hasParameter(1, CharPointerType), 66 hasParameter(2, hasType(isInteger()))) 67 .bind("funcDecl"))) 68 .bind("openatFn"), 69 this); 70 } 71 72 void CloexecOpenCheck::check(const MatchFinder::MatchResult &Result) { 73 const Expr *FlagArg = nullptr; 74 if (const auto *OpenFnCall = Result.Nodes.getNodeAs<CallExpr>("openFn")) 75 FlagArg = OpenFnCall->getArg(1); 76 else if (const auto *OpenFnCall = 77 Result.Nodes.getNodeAs<CallExpr>("openatFn")) 78 FlagArg = OpenFnCall->getArg(2); 79 assert(FlagArg); 80 81 const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>("funcDecl"); 82 83 // Check the required flag. 84 SourceManager &SM = *Result.SourceManager; 85 if (hasCloseOnExecFlag(FlagArg->IgnoreParenCasts(), SM, 86 Result.Context->getLangOpts())) 87 return; 88 89 SourceLocation EndLoc = 90 Lexer::getLocForEndOfToken(SM.getFileLoc(FlagArg->getLocEnd()), 0, SM, 91 Result.Context->getLangOpts()); 92 93 diag(EndLoc, "%0 should use %1 where possible") 94 << FD << O_CLOEXEC 95 << FixItHint::CreateInsertion(EndLoc, (Twine(" | ") + O_CLOEXEC).str()); 96 } 97 98 } // namespace android 99 } // namespace tidy 100 } // namespace clang 101