10#include "clang/ASTMatchers/ASTMatchFinder.h"
11#include "clang/ASTMatchers/ASTMatchers.h"
12#include "clang/Basic/DiagnosticIDs.h"
19 MatchFinder *Finder) {
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"),
34 const MatchFinder::MatchResult &Result) {
36 Result.Nodes.getNodeAs<ConditionalOperator>(
"conditional-operator");
37 const auto *NCO = Result.Nodes.getNodeAs<ConditionalOperator>(
38 "nested-conditional-operator");
42 if (CO->getBeginLoc().isMacroID() || NCO->getBeginLoc().isMacroID())
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",
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.