-
Notifications
You must be signed in to change notification settings - Fork 79
[LValue Generalization] Canonicalize MemberExprs [6/n] #1030
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
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
a5ddda3
Change the Parent property of the Node class from OperatorNode * to N…
c5eccbd
Add ConstantFoldOperator method to constant fold the nodes within an …
0e36620
Add ImplicitCastNode class to mirror the clang AST ImplicitCastExpr c…
7509be2
Add UnaryOperatorNode class to mirror the clang AST UnaryOperator class
c6ea51c
Add MemberNode class that mirrors the clang AST MemberExpr class
f109eaf
Cleanup UnaryOperatorNodes, MemberNodes, and ImplicitCastNodes
a531957
Merge branch 'master' of https://github.com/microsoft/checkedc-clang …
093129e
Minor fixes
2478dc5
Add PreorderAST::AddZero method
996d17f
Formatting fixes
3c9baac
Only create leaf nodes for +e or -e if e is an integer constant expr
82b5e54
Use the AddZero method to normalize the root p of a PreorderAST to p + 0
6dbf715
Use the AddZero method to normalize a->f to (a + 0)->f, *(a).f to (*(…
918ac93
Use AddZero to normalize *e to *(e + 0)
94f156c
Use the AddZero method to normalize e1[e2] to *(e1 + e2 + 0)
e21ee14
Rename OperatorNode to BinaryOperatorNode
3b8f119
Make default the first switch case
d4787e2
Add TODOs for checkedc-clang issue 1032: each Node class should have …
f322fd4
Add an assert to the Node constructor if the Parent is a LeafExprNode
faa2579
Add an assert to AddNode if the Parent node is a LeafExprNode
20b359a
Fix comment
0fa874c
Recompute BParent->Children.end() on each loop iteration
1761fff
Update comments
8435d4c
Add asserts to AddNode if the Root is null
724ff18
Rename AddNode to AttachNode
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,32 +23,46 @@ | |
|
|
||
| namespace clang { | ||
| using Result = Lexicographic::Result; | ||
| class OperatorNode; | ||
| class LeafExprNode; | ||
|
|
||
| class Node { | ||
| public: | ||
| enum class NodeKind { OperatorNode, LeafExprNode }; | ||
| // Nodes with two different kinds are sorted according to the order in | ||
| // which their kinds appear in this enum. | ||
| enum class NodeKind { | ||
| BinaryOperatorNode, | ||
| UnaryOperatorNode, | ||
| MemberNode, | ||
| ImplicitCastNode, | ||
| LeafExprNode | ||
| }; | ||
|
|
||
| NodeKind Kind; | ||
| OperatorNode *Parent; | ||
|
|
||
| Node(NodeKind Kind, OperatorNode *Parent) : | ||
| Kind(Kind), Parent(Parent) {} | ||
| Node *Parent; | ||
|
|
||
| Node(NodeKind Kind, Node *Parent) : | ||
| Kind(Kind), Parent(Parent) { | ||
| if (Parent) | ||
| assert(!isa<LeafExprNode>(Parent) && | ||
| "Parent node cannot be a LeafExprNode"); | ||
| } | ||
| }; | ||
|
|
||
| class OperatorNode : public Node { | ||
| class BinaryOperatorNode : public Node { | ||
| public: | ||
| BinaryOperator::Opcode Opc; | ||
| // Note: An OperatorNode has a list of children because the preorder AST is | ||
| // an n-ary tree. | ||
| // A BinaryOperatorNode representing a commutative and associative binary | ||
| // operation may have more than two children because of coalescing. | ||
| // Ex: a + (b + c) will be represented by one BinaryOperatorNode for + | ||
| // with three children nodes for a, b and c after coalescing. | ||
| llvm::SmallVector<Node *, 2> Children; | ||
|
|
||
| OperatorNode(BinaryOperator::Opcode Opc, OperatorNode *Parent) : | ||
| Node(NodeKind::OperatorNode, Parent), | ||
| BinaryOperatorNode(BinaryOperator::Opcode Opc, Node *Parent) : | ||
| Node(NodeKind::BinaryOperatorNode, Parent), | ||
| Opc(Opc) {} | ||
|
|
||
| static bool classof(const Node *N) { | ||
| return N->Kind == NodeKind::OperatorNode; | ||
| return N->Kind == NodeKind::BinaryOperatorNode; | ||
| } | ||
|
|
||
| // Is the operator commutative and associative? | ||
|
|
@@ -57,11 +71,54 @@ namespace clang { | |
| } | ||
| }; | ||
|
|
||
| class UnaryOperatorNode : public Node { | ||
| public: | ||
| UnaryOperator::Opcode Opc; | ||
| Node *Child; | ||
|
|
||
| UnaryOperatorNode(UnaryOperator::Opcode Opc, Node *Parent) : | ||
| Node(NodeKind::UnaryOperatorNode, Parent), | ||
| Opc(Opc) {} | ||
|
|
||
| static bool classof(const Node *N) { | ||
| return N->Kind == NodeKind::UnaryOperatorNode; | ||
| } | ||
| }; | ||
|
|
||
| class MemberNode : public Node { | ||
| public: | ||
| Node *Base = nullptr; | ||
| ValueDecl *Field = nullptr; | ||
| bool IsArrow; | ||
|
|
||
| MemberNode(ValueDecl *Field, bool IsArrow, Node *Parent) : | ||
| Node(NodeKind::MemberNode, Parent), | ||
| Field(Field), IsArrow(IsArrow) {} | ||
|
|
||
| static bool classof(const Node *N) { | ||
| return N->Kind == NodeKind::MemberNode; | ||
| } | ||
| }; | ||
|
|
||
| class ImplicitCastNode : public Node { | ||
| public: | ||
| CastKind CK; | ||
| Node *Child; | ||
|
|
||
| ImplicitCastNode(CastKind CK, Node *Parent) : | ||
| Node(NodeKind::ImplicitCastNode, Parent), | ||
| CK(CK) {} | ||
|
|
||
| static bool classof(const Node *N) { | ||
| return N->Kind == NodeKind::ImplicitCastNode; | ||
| } | ||
| }; | ||
|
|
||
| class LeafExprNode : public Node { | ||
| public: | ||
| Expr *E; | ||
|
|
||
| LeafExprNode(Expr *E, OperatorNode *Parent) : | ||
| LeafExprNode(Expr *E, Node *Parent) : | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the parent is now of type |
||
| Node(NodeKind::LeafExprNode, Parent), | ||
| E(E) {} | ||
|
|
||
|
|
@@ -84,32 +141,40 @@ namespace clang { | |
| // Create a PreorderAST for the expression E. | ||
| // @param[in] E is the sub expression to be added to a new node. | ||
| // @param[in] Parent is the parent of the new node. | ||
| void Create(Expr *E, OperatorNode *Parent = nullptr); | ||
|
|
||
| // Add a new node to the AST. | ||
| // @param[in] Node is the current node to be added. | ||
| // @param[in] Parent is the parent of the node to be added. | ||
| void AddNode(Node *N, OperatorNode *Parent); | ||
|
|
||
| // Coalesce the OperatorNode O with its parent. This involves moving the | ||
| // children (if any) of node O to its parent and then removing O. | ||
| // @param[in] O is the current node. O should be a OperatorNode. | ||
| void CoalesceNode(OperatorNode *O); | ||
|
|
||
| // Determines if a OperatorNode could be coalesced into its parent. | ||
| // @param[in] O is the current node. O should be a OperatorNode. | ||
| // @return Return true if O can be coalesced into its parent, false | ||
| void Create(Expr *E, Node *Parent = nullptr); | ||
|
|
||
| // Create a BinaryOperatorNode with an addition operator and two children | ||
| // (E and 0), and attach the created BinaryOperatorNode to the Parent node. | ||
| // @param[in] E is the expression that is one of the two children of | ||
| // the created BinaryOperatorNode (the other child is 0). | ||
| // @param[in] Parent is the parent of the created BinaryOperatorNode. | ||
| void AddZero(Expr *E, Node *Parent); | ||
|
|
||
| // Attach a new node to the AST. The node N is attached to the Parent node. | ||
| // @param[in] N is the current node to be attached. | ||
| // @param[in] Parent is the parent of the node to be attached. | ||
| void AttachNode(Node *N, Node *Parent); | ||
|
|
||
| // Coalesce the BinaryOperatorNode B with its parent. This involves moving | ||
| // the children (if any) of node B to its parent and then removing B. | ||
| // @param[in] B is the current node. B should be a BinaryOperatorNode. | ||
| void CoalesceNode(BinaryOperatorNode *B); | ||
|
|
||
| // Determines if a BinaryOperatorNode could be coalesced into its parent. | ||
| // @param[in] B is the current node. B should be a BinaryOperatorNode. | ||
| // @return Return true if B can be coalesced into its parent, false | ||
| // otherwise. | ||
| bool CanCoalesceNode(OperatorNode *O); | ||
| bool CanCoalesceNode(BinaryOperatorNode *B); | ||
|
|
||
| // Recursively coalesce OperatoreNodes having the same commutative and | ||
| // associative operator. | ||
| // Recursively coalesce BinaryOperatorNodes having the same commutative | ||
| // and associative operator. | ||
| // @param[in] N is current node of the AST. Initial value is Root. | ||
| // @param[in] Changed indicates whether a node was coalesced. We need this | ||
| // to control when to stop recursive coalescing. | ||
| void Coalesce(Node *N, bool &Changed); | ||
|
|
||
| // Sort the children expressions in a OperatorNode of the AST. | ||
| // Recursively descend the PreorderAST to sort the children of all | ||
| // BinaryOperatorNodes if the binary operator is commutative. | ||
| // @param[in] N is current node of the AST. Initial value is Root. | ||
| void Sort(Node *N); | ||
|
|
||
|
|
@@ -126,6 +191,12 @@ namespace clang { | |
| // this to control when to stop recursive constant folding. | ||
| void ConstantFold(Node *N, bool &Changed); | ||
|
|
||
| // Constant fold integer expressions within a BinaryOperatorNode. | ||
| // @param[in] N is current node of the AST. | ||
| // @param[in] Changed indicates whether constant folding was done. We need | ||
| // this to control when to stop recursive constant folding. | ||
| void ConstantFoldOperator(BinaryOperatorNode *N, bool &Changed); | ||
|
|
||
| // Get the deref offset from the DerefExpr. The offset represents the | ||
| // possible amount by which the bounds of an ntptr could be widened. | ||
| // @param[in] UpperExpr is the upper bounds expr for the ntptr. | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is this class declaration still needed 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.
It's used for the assert in the
Nodeconstructor ifParentis aLeafExprNode.