brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.2 KiB · c42f069 Raw
91 lines · cpp
1//===----------------------------------------------------------------------===//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-exception6//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 14using namespace clang::ast_matchers;15 16namespace clang::tidy::android {17 18ComparisonInTempFailureRetryCheck::ComparisonInTempFailureRetryCheck(19    StringRef Name, ClangTidyContext *Context)20    : ClangTidyCheck(Name, Context),21      RawRetryList(Options.get("RetryMacros", "TEMP_FAILURE_RETRY")) {22  RawRetryList.split(RetryMacros, ",", -1, false);23}24 25void ComparisonInTempFailureRetryCheck::storeOptions(26    ClangTidyOptions::OptionMap &Opts) {27  Options.store(Opts, "RetryMacros", RawRetryList);28}29 30void 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`, and43  // 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      binaryOperator(hasOperatorName("="),47                     hasRHS(ignoringParenCasts(48                         binaryOperator(isComparisonOperator()).bind("inner"))))49          .bind("outer"),50      this);51}52 53void ComparisonInTempFailureRetryCheck::check(54    const MatchFinder::MatchResult &Result) {55  StringRef RetryMacroName;56  const auto &Node = *Result.Nodes.getNodeAs<BinaryOperator>("outer");57  if (!Node.getBeginLoc().isMacroID())58    return;59 60  const SourceManager &SM = *Result.SourceManager;61  if (!SM.isMacroArgExpansion(Node.getRHS()->IgnoreParenCasts()->getBeginLoc()))62    return;63 64  const LangOptions &Opts = Result.Context->getLangOpts();65  SourceLocation LocStart = Node.getBeginLoc();66  while (LocStart.isMacroID()) {67    const SourceLocation Invocation = SM.getImmediateMacroCallerLoc(LocStart);68    Token Tok;69    if (!Lexer::getRawToken(SM.getSpellingLoc(Invocation), Tok, SM, Opts,70                            /*IgnoreWhiteSpace=*/true)) {71      if (Tok.getKind() == tok::raw_identifier &&72          llvm::is_contained(RetryMacros, 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>("inner");84  diag(Inner.getOperatorLoc(), "top-level comparison in %0") << RetryMacroName;85 86  // FIXME: FixIts would be nice, but potentially nontrivial when nested macros87  // happen, e.g. `TEMP_FAILURE_RETRY(IS_ZERO(foo()))`88}89 90} // namespace clang::tidy::android91