clang-tools 20.0.0git
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
15using namespace clang::ast_matchers;
16
17namespace clang::tidy::android {
18
20 StringRef Name, ClangTidyContext *Context)
21 : ClangTidyCheck(Name, Context),
22 RawRetryList(Options.get("RetryMacros", "TEMP_FAILURE_RETRY")) {
23 StringRef(RawRetryList).split(RetryMacros, ",", -1, false);
24}
25
28 Options.store(Opts, "RetryMacros", RawRetryList);
29}
30
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 binaryOperator(hasOperatorName("="),
48 hasRHS(ignoringParenCasts(
49 binaryOperator(isComparisonOperator()).bind("inner"))))
50 .bind("outer"),
51 this);
52}
53
55 const MatchFinder::MatchResult &Result) {
56 StringRef RetryMacroName;
57 const auto &Node = *Result.Nodes.getNodeAs<BinaryOperator>("outer");
58 if (!Node.getBeginLoc().isMacroID())
59 return;
60
61 const SourceManager &SM = *Result.SourceManager;
62 if (!SM.isMacroArgExpansion(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(LocStart);
69 Token Tok;
70 if (!Lexer::getRawToken(SM.getSpellingLoc(Invocation), Tok, SM, Opts,
71 /*IgnoreWhiteSpace=*/true)) {
72 if (Tok.getKind() == tok::raw_identifier &&
73 llvm::is_contained(RetryMacros, 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>("inner");
85 diag(Inner.getOperatorLoc(), "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
std::pair< Context, Canceler > Inner
llvm::SmallString< 256U > Name
::clang::DynTypedNode Node
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
ComparisonInTempFailureRetryCheck(StringRef Name, ClangTidyContext *Context)
llvm::StringMap< ClangTidyValue > OptionMap