10 #include "clang/AST/RecursiveASTVisitor.h" 11 #include "clang/Lex/Lexer.h" 21 namespace readability {
25 StringRef getText(
const MatchFinder::MatchResult &Result, SourceRange
Range) {
27 *Result.SourceManager,
28 Result.Context->getLangOpts());
32 StringRef getText(
const MatchFinder::MatchResult &Result, T &Node) {
33 return getText(Result, Node.getSourceRange());
36 const char ConditionThenStmtId[] =
"if-bool-yields-then";
37 const char ConditionElseStmtId[] =
"if-bool-yields-else";
38 const char TernaryId[] =
"ternary-bool-yields-condition";
39 const char TernaryNegatedId[] =
"ternary-bool-yields-not-condition";
40 const char IfReturnsBoolId[] =
"if-return";
41 const char IfReturnsNotBoolId[] =
"if-not-return";
42 const char ThenLiteralId[] =
"then-literal";
43 const char IfAssignVariableId[] =
"if-assign-lvalue";
44 const char IfAssignLocId[] =
"if-assign-loc";
45 const char IfAssignBoolId[] =
"if-assign";
46 const char IfAssignNotBoolId[] =
"if-assign-not";
47 const char IfAssignVarId[] =
"if-assign-var";
48 const char CompoundReturnId[] =
"compound-return";
49 const char CompoundBoolId[] =
"compound-bool";
50 const char CompoundNotBoolId[] =
"compound-bool-not";
52 const char IfStmtId[] =
"if";
54 const char SimplifyOperatorDiagnostic[] =
55 "redundant boolean literal supplied to boolean operator";
56 const char SimplifyConditionDiagnostic[] =
57 "redundant boolean literal in if statement condition";
58 const char SimplifyConditionalReturnDiagnostic[] =
59 "redundant boolean literal in conditional return statement";
61 const CXXBoolLiteralExpr *getBoolLiteral(
const MatchFinder::MatchResult &Result,
63 const auto *Literal = Result.Nodes.getNodeAs<CXXBoolLiteralExpr>(Id);
64 return (Literal && Literal->getBeginLoc().isMacroID()) ?
nullptr : Literal;
67 internal::Matcher<Stmt> returnsBool(
bool Value, StringRef Id =
"ignored") {
68 auto SimpleReturnsBool =
69 returnStmt(has(cxxBoolLiteral(equals(Value)).bind(Id)))
70 .bind(
"returns-bool");
71 return anyOf(SimpleReturnsBool,
72 compoundStmt(statementCountIs(1), has(SimpleReturnsBool)));
75 bool needsParensAfterUnaryNegation(
const Expr *
E) {
76 E = E->IgnoreImpCasts();
77 if (isa<BinaryOperator>(E) || isa<ConditionalOperator>(E))
80 if (
const auto *Op = dyn_cast<CXXOperatorCallExpr>(E))
81 return Op->getNumArgs() == 2 && Op->getOperator() != OO_Call &&
82 Op->getOperator() != OO_Subscript;
87 std::pair<BinaryOperatorKind, BinaryOperatorKind> Opposites[] = {
88 {BO_LT, BO_GE}, {BO_GT, BO_LE}, {BO_EQ, BO_NE}};
90 StringRef negatedOperator(
const BinaryOperator *BinOp) {
91 const BinaryOperatorKind Opcode = BinOp->getOpcode();
92 for (
auto NegatableOp : Opposites) {
93 if (Opcode == NegatableOp.first)
94 return BinOp->getOpcodeStr(NegatableOp.second);
95 if (Opcode == NegatableOp.second)
96 return BinOp->getOpcodeStr(NegatableOp.first);
101 std::pair<OverloadedOperatorKind, StringRef> OperatorNames[] = {
102 {OO_EqualEqual,
"=="}, {OO_ExclaimEqual,
"!="}, {OO_Less,
"<"},
103 {OO_GreaterEqual,
">="}, {OO_Greater,
">"}, {OO_LessEqual,
"<="}};
105 StringRef getOperatorName(OverloadedOperatorKind OpKind) {
106 for (
auto Name : OperatorNames) {
107 if (
Name.first == OpKind)
114 std::pair<OverloadedOperatorKind, OverloadedOperatorKind> OppositeOverloads[] =
115 {{OO_EqualEqual, OO_ExclaimEqual},
116 {OO_Less, OO_GreaterEqual},
117 {OO_Greater, OO_LessEqual}};
119 StringRef negatedOperator(
const CXXOperatorCallExpr *OpCall) {
120 const OverloadedOperatorKind Opcode = OpCall->getOperator();
121 for (
auto NegatableOp : OppositeOverloads) {
122 if (Opcode == NegatableOp.first)
123 return getOperatorName(NegatableOp.second);
124 if (Opcode == NegatableOp.second)
125 return getOperatorName(NegatableOp.first);
130 std::string asBool(StringRef text,
bool NeedsStaticCast) {
132 return (
"static_cast<bool>(" + text +
")").str();
137 bool needsNullPtrComparison(
const Expr *E) {
138 if (
const auto *ImpCast = dyn_cast<ImplicitCastExpr>(E))
139 return ImpCast->getCastKind() == CK_PointerToBoolean ||
140 ImpCast->getCastKind() == CK_MemberPointerToBoolean;
145 bool needsZeroComparison(
const Expr *E) {
146 if (
const auto *ImpCast = dyn_cast<ImplicitCastExpr>(E))
147 return ImpCast->getCastKind() == CK_IntegralToBoolean;
152 bool needsStaticCast(
const Expr *E) {
153 if (
const auto *ImpCast = dyn_cast<ImplicitCastExpr>(E)) {
154 if (ImpCast->getCastKind() == CK_UserDefinedConversion &&
155 ImpCast->getSubExpr()->getType()->isBooleanType()) {
156 if (
const auto *MemCall =
157 dyn_cast<CXXMemberCallExpr>(ImpCast->getSubExpr())) {
158 if (
const auto *MemDecl =
159 dyn_cast<CXXConversionDecl>(MemCall->getMethodDecl())) {
160 if (MemDecl->isExplicit())
167 E = E->IgnoreImpCasts();
168 return !E->getType()->isBooleanType();
171 std::string compareExpressionToConstant(
const MatchFinder::MatchResult &Result,
172 const Expr *E,
bool Negated,
173 const char *Constant) {
174 E = E->IgnoreImpCasts();
175 const std::string ExprText =
176 (isa<BinaryOperator>(
E) ? (
"(" + getText(Result, *E) +
")")
177 : getText(Result, *E))
179 return ExprText +
" " + (Negated ?
"!=" :
"==") +
" " + Constant;
182 std::string compareExpressionToNullPtr(
const MatchFinder::MatchResult &Result,
183 const Expr *E,
bool Negated) {
184 const char *NullPtr =
185 Result.Context->getLangOpts().CPlusPlus11 ?
"nullptr" :
"NULL";
186 return compareExpressionToConstant(Result, E, Negated, NullPtr);
189 std::string compareExpressionToZero(
const MatchFinder::MatchResult &Result,
190 const Expr *E,
bool Negated) {
191 return compareExpressionToConstant(Result, E, Negated,
"0");
194 std::string replacementExpression(
const MatchFinder::MatchResult &Result,
195 bool Negated,
const Expr *E) {
196 E = E->ignoreParenBaseCasts();
197 if (
const auto *EC = dyn_cast<ExprWithCleanups>(E))
198 E = EC->getSubExpr();
200 const bool NeedsStaticCast = needsStaticCast(E);
202 if (
const auto *UnOp = dyn_cast<UnaryOperator>(E)) {
203 if (UnOp->getOpcode() == UO_LNot) {
204 if (needsNullPtrComparison(UnOp->getSubExpr()))
205 return compareExpressionToNullPtr(Result, UnOp->getSubExpr(),
true);
207 if (needsZeroComparison(UnOp->getSubExpr()))
208 return compareExpressionToZero(Result, UnOp->getSubExpr(),
true);
210 return replacementExpression(Result,
false, UnOp->getSubExpr());
214 if (needsNullPtrComparison(E))
215 return compareExpressionToNullPtr(Result, E,
false);
217 if (needsZeroComparison(E))
218 return compareExpressionToZero(Result, E,
false);
220 StringRef NegatedOperator;
221 const Expr *LHS =
nullptr;
222 const Expr *RHS =
nullptr;
223 if (
const auto *BinOp = dyn_cast<BinaryOperator>(E)) {
224 NegatedOperator = negatedOperator(BinOp);
225 LHS = BinOp->getLHS();
226 RHS = BinOp->getRHS();
227 }
else if (
const auto *OpExpr = dyn_cast<CXXOperatorCallExpr>(E)) {
228 if (OpExpr->getNumArgs() == 2) {
229 NegatedOperator = negatedOperator(OpExpr);
230 LHS = OpExpr->getArg(0);
231 RHS = OpExpr->getArg(1);
234 if (!NegatedOperator.empty() && LHS && RHS)
235 return (asBool((getText(Result, *LHS) +
" " + NegatedOperator +
" " +
236 getText(Result, *RHS))
240 StringRef
Text = getText(Result, *E);
241 if (!NeedsStaticCast && needsParensAfterUnaryNegation(E))
242 return (
"!(" + Text +
")").str();
244 if (needsNullPtrComparison(E))
245 return compareExpressionToNullPtr(Result, E,
false);
247 if (needsZeroComparison(E))
248 return compareExpressionToZero(Result, E,
false);
250 return (
"!" + asBool(Text, NeedsStaticCast));
253 if (
const auto *UnOp = dyn_cast<UnaryOperator>(E)) {
254 if (UnOp->getOpcode() == UO_LNot) {
255 if (needsNullPtrComparison(UnOp->getSubExpr()))
256 return compareExpressionToNullPtr(Result, UnOp->getSubExpr(),
false);
258 if (needsZeroComparison(UnOp->getSubExpr()))
259 return compareExpressionToZero(Result, UnOp->getSubExpr(),
false);
263 if (needsNullPtrComparison(E))
264 return compareExpressionToNullPtr(Result, E,
true);
266 if (needsZeroComparison(E))
267 return compareExpressionToZero(Result, E,
true);
269 return asBool(getText(Result, *E), NeedsStaticCast);
272 const CXXBoolLiteralExpr *stmtReturnsBool(
const ReturnStmt *Ret,
bool Negated) {
273 if (
const auto *Bool = dyn_cast<CXXBoolLiteralExpr>(Ret->getRetValue())) {
274 if (Bool->getValue() == !Negated)
281 const CXXBoolLiteralExpr *stmtReturnsBool(
const IfStmt *IfRet,
bool Negated) {
282 if (IfRet->getElse() !=
nullptr)
285 if (
const auto *Ret = dyn_cast<ReturnStmt>(IfRet->getThen()))
286 return stmtReturnsBool(Ret, Negated);
288 if (
const auto *Compound = dyn_cast<CompoundStmt>(IfRet->getThen())) {
289 if (Compound->size() == 1) {
290 if (
const auto *CompoundRet = dyn_cast<ReturnStmt>(Compound->body_back()))
291 return stmtReturnsBool(CompoundRet, Negated);
298 bool containsDiscardedTokens(
const MatchFinder::MatchResult &Result,
299 CharSourceRange CharRange) {
300 std::string ReplacementText =
301 Lexer::getSourceText(CharRange, *Result.SourceManager,
302 Result.Context->getLangOpts())
304 Lexer Lex(CharRange.getBegin(), Result.Context->getLangOpts(),
305 ReplacementText.data(), ReplacementText.data(),
306 ReplacementText.data() + ReplacementText.size());
307 Lex.SetCommentRetentionState(
true);
310 while (!Lex.LexFromRawLexer(Tok)) {
311 if (Tok.is(tok::TokenKind::comment) || Tok.is(tok::TokenKind::hash))
323 const MatchFinder::MatchResult &Result)
324 : Check(Check), Result(Result) {}
327 Check->reportBinOp(Result, Op);
333 const MatchFinder::MatchResult &Result;
337 SimplifyBooleanExprCheck::SimplifyBooleanExprCheck(StringRef
Name,
340 ChainedConditionalReturn(Options.get(
"ChainedConditionalReturn", 0U)),
341 ChainedConditionalAssignment(
342 Options.get(
"ChainedConditionalAssignment", 0U)) {}
347 E = E->IgnoreParenImpCasts();
348 if (isa<CXXBoolLiteralExpr>(E))
350 if (
const auto *BinOp = dyn_cast<BinaryOperator>(E))
353 if (
const auto *UnaryOp = dyn_cast<UnaryOperator>(E))
358 void SimplifyBooleanExprCheck::reportBinOp(
359 const MatchFinder::MatchResult &Result,
const BinaryOperator *Op) {
360 const auto *LHS = Op->getLHS()->IgnoreParenImpCasts();
361 const auto *RHS = Op->getRHS()->IgnoreParenImpCasts();
363 const CXXBoolLiteralExpr *Bool;
364 const Expr *Other =
nullptr;
365 if ((Bool = dyn_cast<CXXBoolLiteralExpr>(LHS)))
367 else if ((Bool = dyn_cast<CXXBoolLiteralExpr>(RHS)))
372 if (Bool->getBeginLoc().isMacroID())
379 bool BoolValue = Bool->getValue();
381 auto replaceWithExpression = [
this, &Result, LHS, RHS, Bool](
382 const Expr *ReplaceWith,
bool Negated) {
383 std::string Replacement =
384 replacementExpression(Result, Negated, ReplaceWith);
385 SourceRange
Range(LHS->getBeginLoc(), RHS->getEndLoc());
386 issueDiag(Result, Bool->getBeginLoc(), SimplifyOperatorDiagnostic,
Range,
390 switch (Op->getOpcode()) {
394 replaceWithExpression(Other,
false);
397 replaceWithExpression(Bool,
false);
403 replaceWithExpression(Bool,
false);
406 replaceWithExpression(Other,
false);
411 replaceWithExpression(Other, !BoolValue);
415 replaceWithExpression(Other, BoolValue);
422 void SimplifyBooleanExprCheck::matchBoolCondition(MatchFinder *Finder,
424 StringRef BooleanId) {
426 ifStmt(isExpansionInMainFile(),
427 hasCondition(cxxBoolLiteral(equals(Value)).bind(BooleanId)))
432 void SimplifyBooleanExprCheck::matchTernaryResult(MatchFinder *Finder,
434 StringRef TernaryId) {
436 conditionalOperator(isExpansionInMainFile(),
437 hasTrueExpression(cxxBoolLiteral(equals(Value))),
438 hasFalseExpression(cxxBoolLiteral(equals(!Value))))
443 void SimplifyBooleanExprCheck::matchIfReturnsBool(MatchFinder *Finder,
444 bool Value, StringRef Id) {
445 if (ChainedConditionalReturn)
446 Finder->addMatcher(ifStmt(isExpansionInMainFile(),
447 hasThen(returnsBool(Value, ThenLiteralId)),
448 hasElse(returnsBool(!Value)))
452 Finder->addMatcher(ifStmt(isExpansionInMainFile(),
453 unless(hasParent(ifStmt())),
454 hasThen(returnsBool(Value, ThenLiteralId)),
455 hasElse(returnsBool(!Value)))
460 void SimplifyBooleanExprCheck::matchIfAssignsBool(MatchFinder *Finder,
461 bool Value, StringRef Id) {
462 auto VarAssign = declRefExpr(hasDeclaration(decl().bind(IfAssignVarId)));
463 auto VarRef = declRefExpr(hasDeclaration(equalsBoundNode(IfAssignVarId)));
464 auto MemAssign = memberExpr(hasDeclaration(decl().bind(IfAssignVarId)));
465 auto MemRef = memberExpr(hasDeclaration(equalsBoundNode(IfAssignVarId)));
467 binaryOperator(hasOperatorName(
"="), hasLHS(anyOf(VarAssign, MemAssign)),
468 hasLHS(expr().bind(IfAssignVariableId)),
469 hasRHS(cxxBoolLiteral(equals(Value)).bind(IfAssignLocId)));
470 auto Then = anyOf(SimpleThen, compoundStmt(statementCountIs(1),
471 hasAnySubstatement(SimpleThen)));
473 binaryOperator(hasOperatorName(
"="), hasLHS(anyOf(VarRef, MemRef)),
474 hasRHS(cxxBoolLiteral(equals(!Value))));
475 auto Else = anyOf(SimpleElse, compoundStmt(statementCountIs(1),
476 hasAnySubstatement(SimpleElse)));
477 if (ChainedConditionalAssignment)
478 Finder->addMatcher(ifStmt(hasThen(Then), hasElse(Else)).bind(Id),
this);
481 ifStmt(unless(hasParent(ifStmt())), hasThen(Then), hasElse(Else))
486 void SimplifyBooleanExprCheck::matchCompoundIfReturnsBool(MatchFinder *Finder,
492 ifStmt(hasThen(returnsBool(Value)), unless(hasElse(stmt())))),
493 hasAnySubstatement(returnStmt(has(ignoringParenImpCasts(
494 cxxBoolLiteral(equals(!Value)))))
495 .bind(CompoundReturnId)))
501 Options.
store(Opts,
"ChainedConditionalReturn", ChainedConditionalReturn);
503 ChainedConditionalAssignment);
507 Finder->addMatcher(translationUnitDecl().bind(
"top"),
this);
509 matchBoolCondition(Finder,
true, ConditionThenStmtId);
510 matchBoolCondition(Finder,
false, ConditionElseStmtId);
512 matchTernaryResult(Finder,
true, TernaryId);
513 matchTernaryResult(Finder,
false, TernaryNegatedId);
515 matchIfReturnsBool(Finder,
true, IfReturnsBoolId);
516 matchIfReturnsBool(Finder,
false, IfReturnsNotBoolId);
518 matchIfAssignsBool(Finder,
true, IfAssignBoolId);
519 matchIfAssignsBool(Finder,
false, IfAssignNotBoolId);
521 matchCompoundIfReturnsBool(Finder,
true, CompoundBoolId);
522 matchCompoundIfReturnsBool(Finder,
false, CompoundNotBoolId);
526 if (Result.Nodes.getNodeAs<TranslationUnitDecl>(
"top"))
527 Visitor(
this, Result).TraverseAST(*Result.Context);
528 else if (
const CXXBoolLiteralExpr *TrueConditionRemoved =
529 getBoolLiteral(Result, ConditionThenStmtId))
530 replaceWithThenStatement(Result, TrueConditionRemoved);
531 else if (
const CXXBoolLiteralExpr *FalseConditionRemoved =
532 getBoolLiteral(Result, ConditionElseStmtId))
533 replaceWithElseStatement(Result, FalseConditionRemoved);
534 else if (
const auto *Ternary =
535 Result.Nodes.getNodeAs<ConditionalOperator>(TernaryId))
536 replaceWithCondition(Result, Ternary);
537 else if (
const auto *TernaryNegated =
538 Result.Nodes.getNodeAs<ConditionalOperator>(TernaryNegatedId))
539 replaceWithCondition(Result, TernaryNegated,
true);
540 else if (
const auto *If = Result.Nodes.getNodeAs<IfStmt>(IfReturnsBoolId))
541 replaceWithReturnCondition(Result, If);
542 else if (
const auto *IfNot =
543 Result.Nodes.getNodeAs<IfStmt>(IfReturnsNotBoolId))
544 replaceWithReturnCondition(Result, IfNot,
true);
545 else if (
const auto *IfAssign =
546 Result.Nodes.getNodeAs<IfStmt>(IfAssignBoolId))
547 replaceWithAssignment(Result, IfAssign);
548 else if (
const auto *IfAssignNot =
549 Result.Nodes.getNodeAs<IfStmt>(IfAssignNotBoolId))
550 replaceWithAssignment(Result, IfAssignNot,
true);
551 else if (
const auto *Compound =
552 Result.Nodes.getNodeAs<CompoundStmt>(CompoundBoolId))
553 replaceCompoundReturnWithCondition(Result, Compound);
554 else if (
const auto *Compound =
555 Result.Nodes.getNodeAs<CompoundStmt>(CompoundNotBoolId))
556 replaceCompoundReturnWithCondition(Result, Compound,
true);
559 void SimplifyBooleanExprCheck::issueDiag(
560 const ast_matchers::MatchFinder::MatchResult &Result, SourceLocation
Loc,
561 StringRef
Description, SourceRange ReplacementRange,
562 StringRef Replacement) {
563 CharSourceRange CharRange =
567 DiagnosticBuilder Diag =
diag(Loc, Description);
568 if (!containsDiscardedTokens(Result, CharRange))
569 Diag << FixItHint::CreateReplacement(CharRange, Replacement);
572 void SimplifyBooleanExprCheck::replaceWithThenStatement(
573 const MatchFinder::MatchResult &Result,
574 const CXXBoolLiteralExpr *TrueConditionRemoved) {
575 const auto *IfStatement = Result.Nodes.getNodeAs<IfStmt>(IfStmtId);
576 issueDiag(Result, TrueConditionRemoved->getBeginLoc(),
577 SimplifyConditionDiagnostic, IfStatement->getSourceRange(),
578 getText(Result, *IfStatement->getThen()));
581 void SimplifyBooleanExprCheck::replaceWithElseStatement(
582 const MatchFinder::MatchResult &Result,
583 const CXXBoolLiteralExpr *FalseConditionRemoved) {
584 const auto *IfStatement = Result.Nodes.getNodeAs<IfStmt>(IfStmtId);
585 const Stmt *ElseStatement = IfStatement->getElse();
586 issueDiag(Result, FalseConditionRemoved->getBeginLoc(),
587 SimplifyConditionDiagnostic, IfStatement->getSourceRange(),
588 ElseStatement ? getText(Result, *ElseStatement) :
"");
591 void SimplifyBooleanExprCheck::replaceWithCondition(
592 const MatchFinder::MatchResult &Result,
const ConditionalOperator *Ternary,
594 std::string Replacement =
595 replacementExpression(Result, Negated, Ternary->getCond());
596 issueDiag(Result, Ternary->getTrueExpr()->getBeginLoc(),
597 "redundant boolean literal in ternary expression result",
598 Ternary->getSourceRange(), Replacement);
601 void SimplifyBooleanExprCheck::replaceWithReturnCondition(
602 const MatchFinder::MatchResult &Result,
const IfStmt *If,
bool Negated) {
603 StringRef Terminator = isa<CompoundStmt>(If->getElse()) ?
";" :
"";
604 std::string
Condition = replacementExpression(Result, Negated, If->getCond());
605 std::string Replacement = (
"return " + Condition + Terminator).str();
606 SourceLocation Start =
607 Result.Nodes.getNodeAs<CXXBoolLiteralExpr>(ThenLiteralId)->getBeginLoc();
608 issueDiag(Result, Start, SimplifyConditionalReturnDiagnostic,
609 If->getSourceRange(), Replacement);
612 void SimplifyBooleanExprCheck::replaceCompoundReturnWithCondition(
613 const MatchFinder::MatchResult &Result,
const CompoundStmt *Compound,
615 const auto *Ret = Result.Nodes.getNodeAs<ReturnStmt>(CompoundReturnId);
623 assert(Compound->size() >= 2);
624 const IfStmt *BeforeIf =
nullptr;
625 CompoundStmt::const_body_iterator Current = Compound->body_begin();
626 CompoundStmt::const_body_iterator After = Compound->body_begin();
627 for (++After; After != Compound->body_end() && *Current != Ret;
628 ++Current, ++After) {
629 if (
const auto *If = dyn_cast<IfStmt>(*Current)) {
630 if (
const CXXBoolLiteralExpr *Lit = stmtReturnsBool(If, Negated)) {
632 if (!ChainedConditionalReturn && BeforeIf)
636 std::string Replacement =
637 "return " + replacementExpression(Result, Negated, Condition);
639 Result, Lit->getBeginLoc(), SimplifyConditionalReturnDiagnostic,
640 SourceRange(If->getBeginLoc(), Ret->getEndLoc()), Replacement);
652 void SimplifyBooleanExprCheck::replaceWithAssignment(
653 const MatchFinder::MatchResult &Result,
const IfStmt *IfAssign,
655 SourceRange Range = IfAssign->getSourceRange();
656 StringRef VariableName =
657 getText(Result, *Result.Nodes.getNodeAs<Expr>(IfAssignVariableId));
658 StringRef Terminator = isa<CompoundStmt>(IfAssign->getElse()) ?
";" :
"";
660 replacementExpression(Result, Negated, IfAssign->getCond());
661 std::string Replacement =
662 (VariableName +
" = " + Condition + Terminator).str();
664 Result.Nodes.getNodeAs<CXXBoolLiteralExpr>(IfAssignLocId)->getBeginLoc();
665 issueDiag(Result, Location,
666 "redundant boolean literal in conditional assignment", Range,
SourceLocation Loc
'#' location in the include directive
bool containsBoolLiteral(const Expr *E)
Visitor(SimplifyBooleanExprCheck *Check, const MatchFinder::MatchResult &Result)
void storeOptions(ClangTidyOptions::OptionMap &Options) override
Should store all options supported by this check with their current values or default values for opti...
bool VisitBinaryOperator(BinaryOperator *Op)
Looks for boolean expressions involving boolean constants and simplifies them to use the appropriate ...
Base class for all clang-tidy checks.
const LangOptions & getLangOpts() const
Returns the language options from the context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
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.
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
llvm::Optional< Range > getTokenRange(const SourceManager &SM, const LangOptions &LangOpts, SourceLocation TokLoc)
Returns the taken range at TokLoc.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
std::string Condition
Condition used after the preprocessor directive.
CharSourceRange Range
SourceRange for the file name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.