-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[clang] [NFC] explicitly check if ParentMap contains key #121736
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
The implementation of ParentMap assumes that the key is absent if it is mapped to nullptr. This breaks when trying to store a tuple as the value type. Remove this assumption by explicit uses of `contains()` and `erase()`.
|
@llvm/pr-subscribers-clang Author: Sameer Sahasrabuddhe (ssahasra) ChangesThe implementation of ParentMap assumes that the key is absent if it is mapped to nullptr. This breaks when trying to store a tuple as the value type. Remove this assumption by explicit uses of Full diff: https://github.com/llvm/llvm-project/pull/121736.diff 1 Files Affected:
diff --git a/clang/lib/AST/ParentMap.cpp b/clang/lib/AST/ParentMap.cpp
index fd749b02b758c9..ada7b19487a782 100644
--- a/clang/lib/AST/ParentMap.cpp
+++ b/clang/lib/AST/ParentMap.cpp
@@ -34,13 +34,13 @@ static void BuildParentMap(MapTy& M, Stmt* S,
case Stmt::PseudoObjectExprClass: {
PseudoObjectExpr *POE = cast<PseudoObjectExpr>(S);
- if (OVMode == OV_Opaque && M[POE->getSyntacticForm()])
+ if (OVMode == OV_Opaque && M.contains(POE->getSyntacticForm()))
break;
// If we are rebuilding the map, clear out any existing state.
- if (M[POE->getSyntacticForm()])
+ if (M.contains(POE->getSyntacticForm()))
for (Stmt *SubStmt : S->children())
- M[SubStmt] = nullptr;
+ M.erase(SubStmt);
M[POE->getSyntacticForm()] = S;
BuildParentMap(M, POE->getSyntacticForm(), OV_Transparent);
@@ -78,7 +78,7 @@ static void BuildParentMap(MapTy& M, Stmt* S,
// The right thing to do is to give the OpaqueValueExpr its syntactic
// parent, then not reassign that when traversing the semantic expressions.
OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(S);
- if (OVMode == OV_Transparent || !M[OVE->getSourceExpr()]) {
+ if (OVMode == OV_Transparent || !M.contains(OVE->getSourceExpr())) {
M[OVE->getSourceExpr()] = S;
BuildParentMap(M, OVE->getSourceExpr(), OV_Transparent);
}
|
jayfoad
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No objections, just a couple of ideas for improvements. I have no idea if ParentMap lookups are on any kind of hot path.
clang/lib/AST/ParentMap.cpp
Outdated
| PseudoObjectExpr *POE = cast<PseudoObjectExpr>(S); | ||
|
|
||
| if (OVMode == OV_Opaque && M[POE->getSyntacticForm()]) | ||
| if (OVMode == OV_Opaque && M.contains(POE->getSyntacticForm())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could put this and the next if inside one big if (M.contains) check to avoid a duplicated map lookup.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed with try_emplace() to avoid the second lookup as well, which inserts when the key is already present.
clang/lib/AST/ParentMap.cpp
Outdated
| if (OVMode == OV_Transparent || !M.contains(OVE->getSourceExpr())) { | ||
| M[OVE->getSourceExpr()] = S; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could use M.find to avoid the duplicated lookup here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed with try_emplace() to avoid the second lookup as well, which inserts when the key is already present.
b3777e5 to
028e464
Compare
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/52/builds/5076 Here is the relevant piece of the build log for the reference |
This fixes a bug that slipped into #121736.
The implementation of ParentMap assumes that the key is absent if it is mapped to nullptr. This breaks when trying to store a tuple as the value type. Remove this assumption by explicit uses of
contains()anderase().