Jakub Staron | f7df726 | 2016-05-11 11:33:16 +0000 | [diff] [blame] | 1 | //===--- UseBoolLiteralsCheck.cpp - clang-tidy-----------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "UseBoolLiteralsCheck.h" |
| 11 | #include "clang/AST/ASTContext.h" |
| 12 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 13 | #include "clang/Lex/Lexer.h" |
| 14 | |
| 15 | using namespace clang::ast_matchers; |
| 16 | |
| 17 | namespace clang { |
| 18 | namespace tidy { |
| 19 | namespace modernize { |
| 20 | |
| 21 | void UseBoolLiteralsCheck::registerMatchers(MatchFinder *Finder) { |
| 22 | if (!getLangOpts().CPlusPlus) |
| 23 | return; |
| 24 | |
| 25 | Finder->addMatcher( |
| 26 | implicitCastExpr( |
Piotr Padlewski | e93a73f | 2016-05-31 15:26:56 +0000 | [diff] [blame] | 27 | has(ignoringParenImpCasts(integerLiteral().bind("literal"))), |
Jakub Staron | f7df726 | 2016-05-11 11:33:16 +0000 | [diff] [blame] | 28 | hasImplicitDestinationType(qualType(booleanType())), |
| 29 | unless(isInTemplateInstantiation()), |
| 30 | anyOf(hasParent(explicitCastExpr().bind("cast")), anything())), |
| 31 | this); |
Kirill Bobyrev | 75de896 | 2016-08-08 17:11:56 +0000 | [diff] [blame] | 32 | |
| 33 | Finder->addMatcher( |
| 34 | conditionalOperator( |
| 35 | hasParent(implicitCastExpr( |
| 36 | hasImplicitDestinationType(qualType(booleanType())), |
| 37 | unless(isInTemplateInstantiation()))), |
| 38 | eachOf(hasTrueExpression( |
| 39 | ignoringParenImpCasts(integerLiteral().bind("literal"))), |
| 40 | hasFalseExpression( |
| 41 | ignoringParenImpCasts(integerLiteral().bind("literal"))))), |
| 42 | this); |
Jakub Staron | f7df726 | 2016-05-11 11:33:16 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void UseBoolLiteralsCheck::check(const MatchFinder::MatchResult &Result) { |
| 46 | const auto *Literal = Result.Nodes.getNodeAs<IntegerLiteral>("literal"); |
| 47 | const auto *Cast = Result.Nodes.getNodeAs<Expr>("cast"); |
| 48 | bool LiteralBooleanValue = Literal->getValue().getBoolValue(); |
| 49 | |
| 50 | if (Literal->isInstantiationDependent()) |
| 51 | return; |
| 52 | |
| 53 | const Expr *Expression = Cast ? Cast : Literal; |
| 54 | |
| 55 | auto Diag = |
| 56 | diag(Expression->getExprLoc(), |
| 57 | "converting integer literal to bool, use bool literal instead"); |
| 58 | |
| 59 | if (!Expression->getLocStart().isMacroID()) |
| 60 | Diag << FixItHint::CreateReplacement( |
| 61 | Expression->getSourceRange(), LiteralBooleanValue ? "true" : "false"); |
| 62 | } |
| 63 | |
| 64 | } // namespace modernize |
| 65 | } // namespace tidy |
| 66 | } // namespace clang |