Skip to content

Conversation

@tsepez
Copy link
Contributor

@tsepez tsepez commented Jul 7, 2025

Updates UnsafeBufferUsageReporter::handleUnsafeOperation() to report the entire expression when reporting an unsafe buffers operation instead of just one operand. This is more meaningful when trying to find / replace / suppress unsafe buffer warnings in source code. In particular, it changes e.g.

 return i[s]; // This header is checked and makes an error. ^ 

into

 return i[s]; ^~~~ 

as reported in #146579

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jul 7, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 7, 2025

@llvm/pr-subscribers-clang

Author: Thomas Sepez (tsepez)

Changes

Updates UnsafeBufferUsageReporter::handleUnsafeOperation() to report the entire expression when reporting an unsafe buffers operation instead of just one operand. This is more meaningful when trying to find / replace / suppress unsafe buffer warnings in source code. In particular, it changes e.g.

 return i[s]; // This header is checked and makes an error. ^ 

into

 return i[s]; ^~~~ 

as reported in #146579


Full diff: https://github.com/llvm/llvm-project/pull/147363.diff

1 Files Affected:

  • (modified) clang/lib/Sema/AnalysisBasedWarnings.cpp (+6-11)
diff --git a/clang/lib/Sema/AnalysisBasedWarnings.cpp b/clang/lib/Sema/AnalysisBasedWarnings.cpp index 7420ba2d461c6..4129a9537ba40 100644 --- a/clang/lib/Sema/AnalysisBasedWarnings.cpp +++ b/clang/lib/Sema/AnalysisBasedWarnings.cpp @@ -2275,28 +2275,23 @@ class UnsafeBufferUsageReporter : public UnsafeBufferUsageHandler { unsigned MsgParam = 0; NamedDecl *D = nullptr; if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(Operation)) { - Loc = ASE->getBase()->getExprLoc(); - Range = ASE->getBase()->getSourceRange(); + Loc = ASE->getExprLoc(); + Range = ASE->getSourceRange(); MsgParam = 2; } else if (const auto *BO = dyn_cast<BinaryOperator>(Operation)) { BinaryOperator::Opcode Op = BO->getOpcode(); if (Op == BO_Add || Op == BO_AddAssign || Op == BO_Sub || Op == BO_SubAssign) { - if (BO->getRHS()->getType()->isIntegerType()) { - Loc = BO->getLHS()->getExprLoc(); - Range = BO->getLHS()->getSourceRange(); - } else { - Loc = BO->getRHS()->getExprLoc(); - Range = BO->getRHS()->getSourceRange(); - } + Loc = BO->getExprLoc(); + Range = BO->getSourceRange(); MsgParam = 1; } } else if (const auto *UO = dyn_cast<UnaryOperator>(Operation)) { UnaryOperator::Opcode Op = UO->getOpcode(); if (Op == UO_PreInc || Op == UO_PreDec || Op == UO_PostInc || Op == UO_PostDec) { - Loc = UO->getSubExpr()->getExprLoc(); - Range = UO->getSubExpr()->getSourceRange(); + Loc = UO->getExprLoc(); + Range = UO->getSourceRange(); MsgParam = 1; } } else { 
@cor3ntin
Copy link
Contributor

cor3ntin commented Jul 8, 2025

This change needs a release note.
Please add an entry to clang/docs/ReleaseNotes.rst in the section the most adapted to the change, and referencing any Github issue this change fixes. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

3 participants