Skip to content

Commit

Permalink
[clang-tidy] improve messages when auto-fix does not work (llvm#96917)
Browse files Browse the repository at this point in the history
Fixes: llvm#93157
  • Loading branch information
HerrCai0907 authored Jun 28, 2024
1 parent 7ae4b8e commit e697943
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "SimplifyBooleanExprCheck.h"
#include "clang/AST/Expr.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Basic/DiagnosticIDs.h"
#include "clang/Lex/Lexer.h"
#include "llvm/Support/SaveAndRestore.h"

Expand Down Expand Up @@ -702,7 +703,8 @@ bool SimplifyBooleanExprCheck::canBeBypassed(const Stmt *S) const {
return IgnoreMacros && S->getBeginLoc().isMacroID();
}

void SimplifyBooleanExprCheck::issueDiag(const ASTContext &Context,
/// @brief return true when replacement created.
bool SimplifyBooleanExprCheck::issueDiag(const ASTContext &Context,
SourceLocation Loc,
StringRef Description,
SourceRange ReplacementRange,
Expand All @@ -712,8 +714,10 @@ void SimplifyBooleanExprCheck::issueDiag(const ASTContext &Context,
Context.getSourceManager(), getLangOpts());

DiagnosticBuilder Diag = diag(Loc, Description);
if (!containsDiscardedTokens(Context, CharRange))
const bool HasReplacement = !containsDiscardedTokens(Context, CharRange);
if (HasReplacement)
Diag << FixItHint::CreateReplacement(CharRange, Replacement);
return HasReplacement;
}

void SimplifyBooleanExprCheck::replaceWithThenStatement(
Expand Down Expand Up @@ -751,18 +755,42 @@ void SimplifyBooleanExprCheck::replaceWithReturnCondition(
replacementExpression(Context, Negated, If->getCond());
std::string Replacement = ("return " + Condition + Terminator).str();
SourceLocation Start = BoolLiteral->getBeginLoc();
issueDiag(Context, Start, SimplifyConditionalReturnDiagnostic,
If->getSourceRange(), Replacement);

const bool HasReplacement =
issueDiag(Context, Start, SimplifyConditionalReturnDiagnostic,
If->getSourceRange(), Replacement);

if (!HasReplacement) {
const SourceRange ConditionRange = If->getCond()->getSourceRange();
if (ConditionRange.isValid())
diag(ConditionRange.getBegin(), "conditions that can be simplified",
DiagnosticIDs::Note)
<< ConditionRange;
}
}

void SimplifyBooleanExprCheck::replaceCompoundReturnWithCondition(
const ASTContext &Context, const ReturnStmt *Ret, bool Negated,
const IfStmt *If, const Expr *ThenReturn) {
const std::string Replacement =
"return " + replacementExpression(Context, Negated, If->getCond());
issueDiag(Context, ThenReturn->getBeginLoc(),
SimplifyConditionalReturnDiagnostic,
SourceRange(If->getBeginLoc(), Ret->getEndLoc()), Replacement);

const bool HasReplacement = issueDiag(
Context, ThenReturn->getBeginLoc(), SimplifyConditionalReturnDiagnostic,
SourceRange(If->getBeginLoc(), Ret->getEndLoc()), Replacement);

if (!HasReplacement) {
const SourceRange ConditionRange = If->getCond()->getSourceRange();
if (ConditionRange.isValid())
diag(ConditionRange.getBegin(), "conditions that can be simplified",
DiagnosticIDs::Note)
<< ConditionRange;
const SourceRange ReturnRange = Ret->getSourceRange();
if (ReturnRange.isValid())
diag(ReturnRange.getBegin(), "return statement that can be simplified",
DiagnosticIDs::Note)
<< ReturnRange;
}
}

void SimplifyBooleanExprCheck::replaceWithAssignment(const ASTContext &Context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class SimplifyBooleanExprCheck : public ClangTidyCheck {
const BinaryOperator *Inner, bool TryOfferFix,
const Stmt *Parent, const ParenExpr *Parens);

void issueDiag(const ASTContext &Context, SourceLocation Loc,
bool issueDiag(const ASTContext &Context, SourceLocation Loc,
StringRef Description, SourceRange ReplacementRange,
StringRef Replacement);

Expand Down
3 changes: 2 additions & 1 deletion clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,8 @@ Changes in existing checks

- Improved :doc:`readability-simplify-boolean-expr
<clang-tidy/checks/readability/simplify-boolean-expr>` check to avoid to emit
warning for macro when IgnoreMacro option is enabled.
warning for macro when IgnoreMacro option is enabled and improve messages
when auto-fix does not work.

- Improved :doc:`readability-static-definition-in-anonymous-namespace
<clang-tidy/checks/readability/static-definition-in-anonymous-namespace>`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,23 @@ bool conditional_return_statements(int i) {
// CHECK-FIXES: {{^}} return i == 0;{{$}}
// CHECK-FIXES-NEXT: {{^}$}}

bool conditional_return_statements_no_fix_1(int i) {
if (i == 0) return true;
// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: {{.*}} in conditional return statement
// CHECK-MESSAGES: :[[@LINE-2]]:7: note: conditions that can be simplified
// comment
return false;
// CHECK-MESSAGES: :[[@LINE-1]]:3: note: return statement that can be simplified
}

bool conditional_return_statements_no_fix_2(int i) {
if (i == 0) return true;
// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: {{.*}} in conditional return statement
// CHECK-MESSAGES: :[[@LINE-2]]:7: note: conditions that can be simplified
// comment
else return false;
}

bool conditional_return_statements_then_expr(int i, int j) {
if (i == j) return (i == 0); else return false;
}
Expand Down

0 comments on commit e697943

Please sign in to comment.