From a65a3bffd31f57243929b0f50abbd90bd626faee Mon Sep 17 00:00:00 2001 From: Ishaan Gandhi Date: Sun, 3 Jul 2022 14:42:31 -0600 Subject: [PATCH] [clang-tidy] Don't treat invalid branches as identical The clang-tidy check bugprone-branch-clone has a false positive if some symbols are undefined. This patch silences the warning when the two sides of a branch are invalid. Fixes #56057 Differential Revision: https://reviews.llvm.org/D128402 --- .../clang-tidy/bugprone/BranchCloneCheck.cpp | 11 +++++++++++ clang-tools-extra/docs/ReleaseNotes.rst | 4 ++++ .../checkers/bugprone/branch-clone-unknown-expr.cpp | 9 +++++++++ 3 files changed, 24 insertions(+) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/bugprone/branch-clone-unknown-expr.cpp diff --git a/clang-tools-extra/clang-tidy/bugprone/BranchCloneCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/BranchCloneCheck.cpp index f5f418b13956..5bc35aa6918d 100644 --- a/clang-tools-extra/clang-tidy/bugprone/BranchCloneCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/BranchCloneCheck.cpp @@ -19,6 +19,17 @@ using namespace clang::ast_matchers; /// Returns true when the statements are Type I clones of each other. static bool areStatementsIdentical(const Stmt *LHS, const Stmt *RHS, const ASTContext &Context) { + if (isa(LHS) && isa(RHS)) { + // If we have errors in expressions, we will be unable + // to accurately profile and compute hashes for each + // of the left and right statements. + const auto *LHSExpr = llvm::cast(LHS); + const auto *RHSExpr = llvm::cast(RHS); + if (LHSExpr->containsErrors() && RHSExpr->containsErrors()) { + return false; + } + } + llvm::FoldingSetNodeID DataLHS, DataRHS; LHS->Profile(DataLHS, Context, false); RHS->Profile(DataRHS, Context, false); diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index f148884e90c5..e6d9a57dccea 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -158,6 +158,10 @@ Changes in existing checks - Fixed nonsensical suggestion of :doc:`altera-struct-pack-align ` check for empty structs. +- Fixed a false positive in :doc:`bugprone-branch-clone + ` when the branches + involve unknown expressions. + - Fixed some false positives in :doc:`bugprone-infinite-loop ` involving dependent expressions. diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/branch-clone-unknown-expr.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/branch-clone-unknown-expr.cpp new file mode 100644 index 000000000000..d2641523750c --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/branch-clone-unknown-expr.cpp @@ -0,0 +1,9 @@ +// RUN: %check_clang_tidy -expect-clang-tidy-error %s bugprone-branch-clone %t + +int test_unknown_expression() { + if (unknown_expression_1) { // CHECK-MESSAGES: :[[@LINE]]:7: error: use of undeclared identifier 'unknown_expression_1' [clang-diagnostic-error] + function1(unknown_expression_2); // CHECK-MESSAGES: :[[@LINE]]:15: error: use of undeclared identifier 'unknown_expression_2' [clang-diagnostic-error] + } else { + function2(unknown_expression_3); // CHECK-MESSAGES: :[[@LINE]]:15: error: use of undeclared identifier 'unknown_expression_3' [clang-diagnostic-error] + } +}