Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
herobuxx committed Nov 2, 2024
2 parents eb66d7e + 2804762 commit e151b2b
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,33 +78,44 @@ void IdDependentBackwardBranchCheck::registerMatchers(MatchFinder *Finder) {

IdDependentBackwardBranchCheck::IdDependencyRecord *
IdDependentBackwardBranchCheck::hasIdDepVar(const Expr *Expression) {
if (!Expression)
return nullptr;

if (const auto *Declaration = dyn_cast<DeclRefExpr>(Expression)) {
// It is a DeclRefExpr, so check if it's an ID-dependent variable.
const auto *CheckVariable = dyn_cast<VarDecl>(Declaration->getDecl());
const auto *CheckVariable =
dyn_cast_if_present<VarDecl>(Declaration->getDecl());
if (!CheckVariable)
return nullptr;
auto FoundVariable = IdDepVarsMap.find(CheckVariable);
if (FoundVariable == IdDepVarsMap.end())
return nullptr;
return &(FoundVariable->second);
}
for (const auto *Child : Expression->children())
if (const auto *ChildExpression = dyn_cast<Expr>(Child))
if (const auto *ChildExpression = dyn_cast_if_present<Expr>(Child))
if (IdDependencyRecord *Result = hasIdDepVar(ChildExpression))
return Result;
return nullptr;
}

IdDependentBackwardBranchCheck::IdDependencyRecord *
IdDependentBackwardBranchCheck::hasIdDepField(const Expr *Expression) {
if (!Expression)
return nullptr;

if (const auto *MemberExpression = dyn_cast<MemberExpr>(Expression)) {
const auto *CheckField =
dyn_cast<FieldDecl>(MemberExpression->getMemberDecl());
dyn_cast_if_present<FieldDecl>(MemberExpression->getMemberDecl());
if (!CheckField)
return nullptr;
auto FoundField = IdDepFieldsMap.find(CheckField);
if (FoundField == IdDepFieldsMap.end())
return nullptr;
return &(FoundField->second);
}
for (const auto *Child : Expression->children())
if (const auto *ChildExpression = dyn_cast<Expr>(Child))
if (const auto *ChildExpression = dyn_cast_if_present<Expr>(Child))
if (IdDependencyRecord *Result = hasIdDepField(ChildExpression))
return Result;
return nullptr;
Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ New check aliases
Changes in existing checks
^^^^^^^^^^^^^^^^^^^^^^^^^^

- Improved :doc:`altera-id-dependent-backward-branch
<clang-tidy/checks/altera/id-dependent-backward-branch>` check by fixing
crashes from invalid code.

- Improved :doc:`bugprone-casting-through-void
<clang-tidy/checks/bugprone/casting-through-void>` check to suggest replacing
the offending code with ``reinterpret_cast``, to more clearly express intent.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %check_clang_tidy %s altera-id-dependent-backward-branch %t -- -header-filter=.* "--" -cl-std=CL1.2 -c
// RUN: %check_clang_tidy %s altera-id-dependent-backward-branch %t -- -header-filter=.* "--" -cl-std=CLC++1.0 -c

void error() {
// ==== Conditional Expressions ====
Expand Down Expand Up @@ -80,3 +80,9 @@ void success() {
}
}
}

template<char... STOP>
void gh55408(char const input[], int pos) {
while (((input[pos] != STOP) && ...));
}

13 changes: 6 additions & 7 deletions clang/lib/Sema/CheckExprLifetime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,14 @@ using LocalVisitor = llvm::function_ref<bool(IndirectLocalPath &Path, Local L,
ReferenceKind RK)>;
} // namespace

static bool isVarOnPath(IndirectLocalPath &Path, VarDecl *VD) {
static bool isVarOnPath(const IndirectLocalPath &Path, VarDecl *VD) {
for (auto E : Path)
if (E.Kind == IndirectLocalPathEntry::VarInit && E.D == VD)
return true;
return false;
}

static bool pathContainsInit(IndirectLocalPath &Path) {
static bool pathContainsInit(const IndirectLocalPath &Path) {
return llvm::any_of(Path, [=](IndirectLocalPathEntry E) {
return E.Kind == IndirectLocalPathEntry::DefaultInit ||
E.Kind == IndirectLocalPathEntry::VarInit;
Expand Down Expand Up @@ -1076,7 +1076,7 @@ static SourceRange nextPathEntryRange(const IndirectLocalPath &Path, unsigned I,
return E->getSourceRange();
}

static bool pathOnlyHandlesGslPointer(IndirectLocalPath &Path) {
static bool pathOnlyHandlesGslPointer(const IndirectLocalPath &Path) {
for (const auto &It : llvm::reverse(Path)) {
switch (It.Kind) {
case IndirectLocalPathEntry::VarInit:
Expand Down Expand Up @@ -1124,24 +1124,23 @@ static void checkExprLifetimeImpl(Sema &SemaRef,

// FIXME: consider moving the TemporaryVisitor and visitLocalsRetained*
// functions to a dedicated class.
auto TemporaryVisitor = [&](IndirectLocalPath &Path, Local L,
auto TemporaryVisitor = [&](const IndirectLocalPath &Path, Local L,
ReferenceKind RK) -> bool {
SourceRange DiagRange = nextPathEntryRange(Path, 0, L);
SourceLocation DiagLoc = DiagRange.getBegin();

auto *MTE = dyn_cast<MaterializeTemporaryExpr>(L);

bool IsGslPtrValueFromGslTempOwner = false;
bool IsLocalGslOwner = false;
if (pathOnlyHandlesGslPointer(Path)) {
if (isa<DeclRefExpr>(L)) {
// We do not want to follow the references when returning a pointer
// originating from a local owner to avoid the following false positive:
// int &p = *localUniquePtr;
// someContainer.add(std::move(localUniquePtr));
// return p;
IsLocalGslOwner = isRecordWithAttr<OwnerAttr>(L->getType());
if (pathContainsInit(Path) || !IsLocalGslOwner)
if (pathContainsInit(Path) ||
!isRecordWithAttr<OwnerAttr>(L->getType()))
return false;
} else {
IsGslPtrValueFromGslTempOwner =
Expand Down
8 changes: 6 additions & 2 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10318,8 +10318,10 @@ SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
AddNodeIDNode(ID, Opcode, VTs, Ops);
void *IP = nullptr;

if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
E->intersectFlagsWith(Flags);
return SDValue(E, 0);
}

N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
createOperands(N, Ops);
Expand Down Expand Up @@ -10524,8 +10526,10 @@ SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
FoldingSetNodeID ID;
AddNodeIDNode(ID, Opcode, VTList, Ops);
void *IP = nullptr;
if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
E->intersectFlagsWith(Flags);
return SDValue(E, 0);
}

N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
createOperands(N, Ops);
Expand Down
23 changes: 23 additions & 0 deletions llvm/test/CodeGen/ARM/dagcombine-drop-flags-freeze.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
; RUN: llc -mtriple=arm-eabi -o - %s | FileCheck %s

; Ensure poison-generating flags are stripped by the time a freeze operand is visited.

define i1 @drop_flags(i32 noundef %numentries, i64 %cond, i64 %arg) {
; CHECK-LABEL: drop_flags:
; CHECK: @ %bb.0: @ %entry
; CHECK-NEXT: ldm sp, {r1, r12}
; CHECK-NEXT: subs r1, r2, r1
; CHECK-NEXT: sbcs r1, r3, r12
; CHECK-NEXT: movlo r0, r2
; CHECK-NEXT: rsbs r1, r0, #0
; CHECK-NEXT: adc r0, r0, r1
; CHECK-NEXT: mov pc, lr
entry:
%cmp4 = icmp samesign ult i64 %cond, %arg
%conv6 = trunc nuw i64 %cond to i32
%spec.select = select i1 %cmp4, i32 %conv6, i32 %numentries
%spec.select.fr = freeze i32 %spec.select
%cmpz = icmp eq i32 %spec.select.fr, 0
ret i1 %cmpz
}

0 comments on commit e151b2b

Please sign in to comment.