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