1//===--- AvoidNestedConditionalOperatorCheck.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 "AvoidNestedConditionalOperatorCheck.h"
10#include "clang/ASTMatchers/ASTMatchFinder.h"
11#include "clang/ASTMatchers/ASTMatchers.h"
12#include "clang/Basic/DiagnosticIDs.h"
13
14using namespace clang::ast_matchers;
15
16namespace clang::tidy::readability {
17
18void AvoidNestedConditionalOperatorCheck::registerMatchers(
19 MatchFinder *Finder) {
20 Finder->addMatcher(
21 NodeMatch: conditionalOperator(
22 anyOf(
23 hasCondition(InnerMatcher: ignoringParenCasts(
24 InnerMatcher: conditionalOperator().bind(ID: "nested-conditional-operator"))),
25 hasTrueExpression(InnerMatcher: ignoringParenCasts(
26 InnerMatcher: conditionalOperator().bind(ID: "nested-conditional-operator"))),
27 hasFalseExpression(InnerMatcher: ignoringParenCasts(
28 InnerMatcher: conditionalOperator().bind(ID: "nested-conditional-operator")))))
29 .bind(ID: "conditional-operator"),
30 Action: this);
31}
32
33void AvoidNestedConditionalOperatorCheck::check(
34 const MatchFinder::MatchResult &Result) {
35 const auto *CO =
36 Result.Nodes.getNodeAs<ConditionalOperator>(ID: "conditional-operator");
37 const auto *NCO = Result.Nodes.getNodeAs<ConditionalOperator>(
38 ID: "nested-conditional-operator");
39 assert(CO);
40 assert(NCO);
41
42 if (CO->getBeginLoc().isMacroID() || NCO->getBeginLoc().isMacroID())
43 return;
44
45 diag(Loc: NCO->getBeginLoc(),
46 Description: "conditional operator is used as sub-expression of parent conditional "
47 "operator, refrain from using nested conditional operators");
48 diag(Loc: CO->getBeginLoc(), Description: "parent conditional operator here",
49 Level: DiagnosticIDs::Note);
50}
51
52} // namespace clang::tidy::readability
53

source code of clang-tools-extra/clang-tidy/readability/AvoidNestedConditionalOperatorCheck.cpp