Skip to content

Commit 9115f18

Browse files
[clang][JumpDiagnostics] bring VerifyIndirectOrAsmJumps to C++17
Update the code to more modern C++ style. Reviewed By: void, MaskRay Differential Revision: https://reviews.llvm.org/D155336
1 parent 5b012bf commit 9115f18

File tree

1 file changed

+15
-29
lines changed

1 file changed

+15
-29
lines changed

clang/lib/Sema/JumpDiagnostics.cpp

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -721,48 +721,40 @@ void JumpScopeChecker::VerifyIndirectOrAsmJumps(bool IsAsmGoto) {
721721
// If there aren't any address-of-label expressions in this function,
722722
// complain about the first indirect goto.
723723
if (JumpTargets.empty()) {
724-
assert(!IsAsmGoto &&"only indirect goto can get here");
724+
assert(!IsAsmGoto && "only indirect goto can get here");
725725
S.Diag(GotoJumps[0]->getBeginLoc(),
726726
diag::err_indirect_goto_without_addrlabel);
727727
return;
728728
}
729729
// Collect a single representative of every scope containing an
730730
// indirect or asm goto. For most code bases, this substantially cuts
731731
// down on the number of jump sites we'll have to consider later.
732-
typedef std::pair<unsigned, Stmt*> JumpScope;
732+
using JumpScope = std::pair<unsigned, Stmt *>;
733733
SmallVector<JumpScope, 32> JumpScopes;
734734
{
735735
llvm::DenseMap<unsigned, Stmt*> JumpScopesMap;
736-
for (SmallVectorImpl<Stmt *>::iterator I = GotoJumps.begin(),
737-
E = GotoJumps.end();
738-
I != E; ++I) {
739-
Stmt *IG = *I;
736+
for (Stmt *IG : GotoJumps) {
740737
if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(IG)))
741738
continue;
742739
unsigned IGScope = LabelAndGotoScopes[IG];
743-
Stmt *&Entry = JumpScopesMap[IGScope];
744-
if (!Entry) Entry = IG;
740+
if (!JumpScopesMap.contains(IGScope))
741+
JumpScopesMap[IGScope] = IG;
745742
}
746743
JumpScopes.reserve(JumpScopesMap.size());
747-
for (llvm::DenseMap<unsigned, Stmt *>::iterator I = JumpScopesMap.begin(),
748-
E = JumpScopesMap.end();
749-
I != E; ++I)
750-
JumpScopes.push_back(*I);
744+
for (auto &Pair : JumpScopesMap)
745+
JumpScopes.emplace_back(Pair);
751746
}
752747

753748
// Collect a single representative of every scope containing a
754749
// label whose address was taken somewhere in the function.
755750
// For most code bases, there will be only one such scope.
756751
llvm::DenseMap<unsigned, LabelDecl*> TargetScopes;
757-
for (SmallVectorImpl<LabelDecl *>::iterator I = JumpTargets.begin(),
758-
E = JumpTargets.end();
759-
I != E; ++I) {
760-
LabelDecl *TheLabel = *I;
752+
for (LabelDecl *TheLabel : JumpTargets) {
761753
if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(TheLabel->getStmt())))
762754
continue;
763755
unsigned LabelScope = LabelAndGotoScopes[TheLabel->getStmt()];
764-
LabelDecl *&Target = TargetScopes[LabelScope];
765-
if (!Target) Target = TheLabel;
756+
if (!TargetScopes.contains(LabelScope))
757+
TargetScopes[LabelScope] = TheLabel;
766758
}
767759

768760
// For each target scope, make sure it's trivially reachable from
@@ -774,11 +766,7 @@ void JumpScopeChecker::VerifyIndirectOrAsmJumps(bool IsAsmGoto) {
774766
// entered, then verify that every jump scope can be trivially
775767
// exitted to reach a scope in S.
776768
llvm::BitVector Reachable(Scopes.size(), false);
777-
for (llvm::DenseMap<unsigned,LabelDecl*>::iterator
778-
TI = TargetScopes.begin(), TE = TargetScopes.end(); TI != TE; ++TI) {
779-
unsigned TargetScope = TI->first;
780-
LabelDecl *TargetLabel = TI->second;
781-
769+
for (auto [TargetScope, TargetLabel] : TargetScopes) {
782770
Reachable.reset();
783771

784772
// Mark all the enclosing scopes from which you can safely jump
@@ -799,10 +787,8 @@ void JumpScopeChecker::VerifyIndirectOrAsmJumps(bool IsAsmGoto) {
799787

800788
// Walk through all the jump sites, checking that they can trivially
801789
// reach this label scope.
802-
for (SmallVectorImpl<JumpScope>::iterator
803-
I = JumpScopes.begin(), E = JumpScopes.end(); I != E; ++I) {
804-
unsigned Scope = I->first;
805-
790+
for (auto [JumpScope, JumpStmt] : JumpScopes) {
791+
unsigned Scope = JumpScope;
806792
// Walk out the "scope chain" for this scope, looking for a scope
807793
// we've marked reachable. For well-formed code this amortizes
808794
// to O(JumpScopes.size() / Scopes.size()): we only iterate
@@ -813,7 +799,7 @@ void JumpScopeChecker::VerifyIndirectOrAsmJumps(bool IsAsmGoto) {
813799
if (Reachable.test(Scope)) {
814800
// If we find something reachable, mark all the scopes we just
815801
// walked through as reachable.
816-
for (unsigned S = I->first; S != Scope; S = Scopes[S].ParentScope)
802+
for (unsigned S = JumpScope; S != Scope; S = Scopes[S].ParentScope)
817803
Reachable.set(S);
818804
IsReachable = true;
819805
break;
@@ -832,7 +818,7 @@ void JumpScopeChecker::VerifyIndirectOrAsmJumps(bool IsAsmGoto) {
832818
// Only diagnose if we didn't find something.
833819
if (IsReachable) continue;
834820

835-
DiagnoseIndirectOrAsmJump(I->second, I->first, TargetLabel, TargetScope);
821+
DiagnoseIndirectOrAsmJump(JumpStmt, JumpScope, TargetLabel, TargetScope);
836822
}
837823
}
838824
}

0 commit comments

Comments
 (0)