| 1 | //===--- ComparisonInTempFailureRetryCheck.cpp - clang-tidy----------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "ComparisonInTempFailureRetryCheck.h" |
| 10 | #include "clang/AST/ASTContext.h" |
| 11 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 12 | #include "clang/Lex/Lexer.h" |
| 13 | |
| 14 | using namespace clang::ast_matchers; |
| 15 | |
| 16 | namespace clang::tidy::android { |
| 17 | |
| 18 | ComparisonInTempFailureRetryCheck::ComparisonInTempFailureRetryCheck( |
| 19 | StringRef Name, ClangTidyContext *Context) |
| 20 | : ClangTidyCheck(Name, Context), |
| 21 | RawRetryList(Options.get(LocalName: "RetryMacros" , Default: "TEMP_FAILURE_RETRY" )) { |
| 22 | StringRef(RawRetryList).split(A&: RetryMacros, Separator: "," , MaxSplit: -1, KeepEmpty: false); |
| 23 | } |
| 24 | |
| 25 | void ComparisonInTempFailureRetryCheck::storeOptions( |
| 26 | ClangTidyOptions::OptionMap &Opts) { |
| 27 | Options.store(Options&: Opts, LocalName: "RetryMacros" , Value: RawRetryList); |
| 28 | } |
| 29 | |
| 30 | void ComparisonInTempFailureRetryCheck::registerMatchers(MatchFinder *Finder) { |
| 31 | // Both glibc's and Bionic's TEMP_FAILURE_RETRY macros structurally look like: |
| 32 | // |
| 33 | // #define TEMP_FAILURE_RETRY(x) ({ \ |
| 34 | // typeof(x) y; \ |
| 35 | // do y = (x); \ |
| 36 | // while (y == -1 && errno == EINTR); \ |
| 37 | // y; \ |
| 38 | // }) |
| 39 | // |
| 40 | // (glibc uses `long int` instead of `typeof(x)` for the type of y). |
| 41 | // |
| 42 | // It's unclear how to walk up the AST from inside the expansion of `x`, and |
| 43 | // we need to not complain about things like TEMP_FAILURE_RETRY(foo(x == 1)), |
| 44 | // so we just match the assignment of `y = (x)` and inspect `x` from there. |
| 45 | Finder->addMatcher( |
| 46 | NodeMatch: binaryOperator(hasOperatorName(Name: "=" ), |
| 47 | hasRHS(InnerMatcher: ignoringParenCasts( |
| 48 | InnerMatcher: binaryOperator(isComparisonOperator()).bind(ID: "inner" )))) |
| 49 | .bind(ID: "outer" ), |
| 50 | Action: this); |
| 51 | } |
| 52 | |
| 53 | void ComparisonInTempFailureRetryCheck::check( |
| 54 | const MatchFinder::MatchResult &Result) { |
| 55 | StringRef RetryMacroName; |
| 56 | const auto &Node = *Result.Nodes.getNodeAs<BinaryOperator>(ID: "outer" ); |
| 57 | if (!Node.getBeginLoc().isMacroID()) |
| 58 | return; |
| 59 | |
| 60 | const SourceManager &SM = *Result.SourceManager; |
| 61 | if (!SM.isMacroArgExpansion(Loc: Node.getRHS()->IgnoreParenCasts()->getBeginLoc())) |
| 62 | return; |
| 63 | |
| 64 | const LangOptions &Opts = Result.Context->getLangOpts(); |
| 65 | SourceLocation LocStart = Node.getBeginLoc(); |
| 66 | while (LocStart.isMacroID()) { |
| 67 | SourceLocation Invocation = SM.getImmediateMacroCallerLoc(Loc: LocStart); |
| 68 | Token Tok; |
| 69 | if (!Lexer::getRawToken(Loc: SM.getSpellingLoc(Loc: Invocation), Result&: Tok, SM, LangOpts: Opts, |
| 70 | /*IgnoreWhiteSpace=*/true)) { |
| 71 | if (Tok.getKind() == tok::raw_identifier && |
| 72 | llvm::is_contained(Range&: RetryMacros, Element: Tok.getRawIdentifier())) { |
| 73 | RetryMacroName = Tok.getRawIdentifier(); |
| 74 | break; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | LocStart = Invocation; |
| 79 | } |
| 80 | if (RetryMacroName.empty()) |
| 81 | return; |
| 82 | |
| 83 | const auto &Inner = *Result.Nodes.getNodeAs<BinaryOperator>(ID: "inner" ); |
| 84 | diag(Loc: Inner.getOperatorLoc(), Description: "top-level comparison in %0" ) << RetryMacroName; |
| 85 | |
| 86 | // FIXME: FixIts would be nice, but potentially nontrivial when nested macros |
| 87 | // happen, e.g. `TEMP_FAILURE_RETRY(IS_ZERO(foo()))` |
| 88 | } |
| 89 | |
| 90 | } // namespace clang::tidy::android |
| 91 | |