Yan Wang | b21739f | 2017-08-10 22:09:22 +0000 | [diff] [blame] | 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 | |
| 24 | const char *const FuncDeclBindingStr = "funcDecl"; |
| 25 | const char *const FuncBindingStr = "func"; |
| 26 | |
| 27 | // Helper function to form the correct string mode for Type3. |
| 28 | // Build the replace text. If it's string constant, add <Mode> directly in the |
| 29 | // end of the string. Else, add <Mode>. |
| 30 | std::string buildFixMsgForStringFlag(const Expr *Arg, const SourceManager &SM, |
| 31 | const LangOptions &LangOpts, char Mode) { |
| 32 | if (Arg->getLocStart().isMacroID()) |
| 33 | return (Lexer::getSourceText( |
| 34 | CharSourceRange::getTokenRange(Arg->getSourceRange()), SM, |
| 35 | LangOpts) + |
| 36 | " \"" + Twine(Mode) + "\"") |
| 37 | .str(); |
| 38 | |
| 39 | StringRef SR = cast<StringLiteral>(Arg->IgnoreParenCasts())->getString(); |
| 40 | return ("\"" + SR + Twine(Mode) + "\"").str(); |
| 41 | } |
| 42 | } // namespace |
| 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->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 | |
| 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->getLocStart(), 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->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 Hsieh | 56650e7 | 2017-08-14 17:04:16 +0000 | [diff] [blame^] | 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 | |
Yan Wang | b21739f | 2017-08-10 22:09:22 +0000 | [diff] [blame] | 112 | } // namespace android |
| 113 | } // namespace tidy |
| 114 | } // namespace clang |