Skip to content
Merged
Show file tree
Hide file tree
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…
Apr 10, 2021
c5eccbd
Add ConstantFoldOperator method to constant fold the nodes within an …
Apr 10, 2021
0e36620
Add ImplicitCastNode class to mirror the clang AST ImplicitCastExpr c…
Apr 10, 2021
7509be2
Add UnaryOperatorNode class to mirror the clang AST UnaryOperator class
Apr 10, 2021
c6ea51c
Add MemberNode class that mirrors the clang AST MemberExpr class
Apr 10, 2021
f109eaf
Cleanup UnaryOperatorNodes, MemberNodes, and ImplicitCastNodes
Apr 10, 2021
a531957
Merge branch 'master' of https://github.com/microsoft/checkedc-clang …
Apr 10, 2021
093129e
Minor fixes
Apr 10, 2021
2478dc5
Add PreorderAST::AddZero method
Apr 14, 2021
996d17f
Formatting fixes
Apr 14, 2021
3c9baac
Only create leaf nodes for +e or -e if e is an integer constant expr
Apr 14, 2021
82b5e54
Use the AddZero method to normalize the root p of a PreorderAST to p + 0
Apr 14, 2021
6dbf715
Use the AddZero method to normalize a->f to (a + 0)->f, *(a).f to (*(…
Apr 14, 2021
918ac93
Use AddZero to normalize *e to *(e + 0)
Apr 14, 2021
94f156c
Use the AddZero method to normalize e1[e2] to *(e1 + e2 + 0)
Apr 14, 2021
e21ee14
Rename OperatorNode to BinaryOperatorNode
Apr 14, 2021
3b8f119
Make default the first switch case
Apr 14, 2021
d4787e2
Add TODOs for checkedc-clang issue 1032: each Node class should have …
Apr 15, 2021
f322fd4
Add an assert to the Node constructor if the Parent is a LeafExprNode
Apr 15, 2021
faa2579
Add an assert to AddNode if the Parent node is a LeafExprNode
Apr 15, 2021
20b359a
Fix comment
Apr 15, 2021
0fa874c
Recompute BParent->Children.end() on each loop iteration
Apr 15, 2021
1761fff
Update comments
Apr 19, 2021
8435d4c
Add asserts to AddNode if the Root is null
Apr 19, 2021
724ff18
Rename AddNode to AttachNode
Apr 19, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 103 additions & 32 deletions clang/include/clang/AST/PreorderAST.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,46 @@

namespace clang {
using Result = Lexicographic::Result;
class OperatorNode;
class LeafExprNode;
Copy link

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?

Copy link
Contributor Author

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 Node constructor if Parent is a 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?
Expand All @@ -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) :
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the parent is now of type Node it means even a LeafExprNode can be a parent. But we should not allow parent to be a LeafExprNode. Maybe an assert and a check should be added.

Node(NodeKind::LeafExprNode, Parent),
E(E) {}

Expand All @@ -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);

Expand All @@ -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.
Expand Down
Loading