clang-tools 19.0.0git
AvoidNestedConditionalOperatorCheck.cpp
Go to the documentation of this file.
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
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
17
19 MatchFinder *Finder) {
20 Finder->addMatcher(
21 conditionalOperator(
22 anyOf(
23 hasCondition(ignoringParenCasts(
24 conditionalOperator().bind("nested-conditional-operator"))),
25 hasTrueExpression(ignoringParenCasts(
26 conditionalOperator().bind("nested-conditional-operator"))),
27 hasFalseExpression(ignoringParenCasts(
28 conditionalOperator().bind("nested-conditional-operator")))))
29 .bind("conditional-operator"),
30 this);
31}
32
34 const MatchFinder::MatchResult &Result) {
35 const auto *CO =
36 Result.Nodes.getNodeAs<ConditionalOperator>("conditional-operator");
37 const auto *NCO = Result.Nodes.getNodeAs<ConditionalOperator>(
38 "nested-conditional-operator");
39 assert(CO);
40 assert(NCO);
41
42 if (CO->getBeginLoc().isMacroID() || NCO->getBeginLoc().isMacroID())
43 return;
44
45 diag(NCO->getBeginLoc(),
46 "conditional operator is used as sub-expression of parent conditional "
47 "operator, refrain from using nested conditional operators");
48 diag(CO->getBeginLoc(), "parent conditional operator here",
49 DiagnosticIDs::Note);
50}
51
52} // namespace clang::tidy::readability
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.