clang-tools  11.0.0
ComparisonInTempFailureRetryCheck.cpp
Go to the documentation of this file.
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"
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 AST_MATCHER(BinaryOperator, isRHSATempFailureRetryArg) {
23  if (!Node.getBeginLoc().isMacroID())
24  return false;
25 
26  const SourceManager &SM = Finder->getASTContext().getSourceManager();
27  if (!SM.isMacroArgExpansion(Node.getRHS()->IgnoreParenCasts()->getBeginLoc()))
28  return false;
29 
30  const LangOptions &Opts = Finder->getASTContext().getLangOpts();
31  SourceLocation LocStart = Node.getBeginLoc();
32  while (LocStart.isMacroID()) {
33  SourceLocation Invocation = SM.getImmediateMacroCallerLoc(LocStart);
34  Token Tok;
35  if (!Lexer::getRawToken(SM.getSpellingLoc(Invocation), Tok, SM, Opts,
36  /*IgnoreWhiteSpace=*/true)) {
37  if (Tok.getKind() == tok::raw_identifier &&
38  Tok.getRawIdentifier() == "TEMP_FAILURE_RETRY")
39  return true;
40  }
41 
42  LocStart = Invocation;
43  }
44  return false;
45 }
46 } // namespace
47 
48 void ComparisonInTempFailureRetryCheck::registerMatchers(MatchFinder *Finder) {
49  // Both glibc's and Bionic's TEMP_FAILURE_RETRY macros structurally look like:
50  //
51  // #define TEMP_FAILURE_RETRY(x) ({ \
52  // typeof(x) y; \
53  // do y = (x); \
54  // while (y == -1 && errno == EINTR); \
55  // y; \
56  // })
57  //
58  // (glibc uses `long int` instead of `typeof(x)` for the type of y).
59  //
60  // It's unclear how to walk up the AST from inside the expansion of `x`, and
61  // we need to not complain about things like TEMP_FAILURE_RETRY(foo(x == 1)),
62  // so we just match the assignment of `y = (x)` and inspect `x` from there.
63  Finder->addMatcher(
64  binaryOperator(hasOperatorName("="),
65  hasRHS(ignoringParenCasts(
66  binaryOperator(isComparisonOperator()).bind("binop"))),
67  isRHSATempFailureRetryArg()),
68  this);
69 }
70 
71 void ComparisonInTempFailureRetryCheck::check(
72  const MatchFinder::MatchResult &Result) {
73  const auto &BinOp = *Result.Nodes.getNodeAs<BinaryOperator>("binop");
74  diag(BinOp.getOperatorLoc(), "top-level comparison in TEMP_FAILURE_RETRY");
75 
76  // FIXME: FixIts would be nice, but potentially nontrivial when nested macros
77  // happen, e.g. `TEMP_FAILURE_RETRY(IS_ZERO(foo()))`
78 }
79 
80 } // namespace android
81 } // namespace tidy
82 } // namespace clang
ComparisonInTempFailureRetryCheck.h
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang::ast_matchers::AST_MATCHER
AST_MATCHER(Expr, isMacroID)
Definition: PreferIsaOrDynCastInConditionalsCheck.cpp:19
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27