acquiring all the ancestors or descendants of a matched node - llvm-clang

I need to travel through all the ancestors or descendants of a matched AST node to later use that info to moodify parts of the input source code.
I tried to look for ways to do that. I looked at the getParents member function of the ASTContext class. I could use that to just go up the AST hierarchy to visit all the ancestor nodes of my currently-matched node. but the problem with that is, when i get the parent node, i no longer have the context for that node to try and get its parent. I could try to rebuild the ASTContext for the new node but that seems to be another big task on its own, if possible.
the lowest NodeKind (lowest in the C hierarchy) I'm looking for is a callExpr and the highest I'm looking for is a functionDecl.
how can I obtain all the ancestors or descendants of a matched AST node after the match returns the control to run in MatchCallback?

It may be possible to keep reaching for a parent declaration recursively until you reach TranslationUnitDecl, however, I would instead suggest actually iterating over the declarations in TranslationUnitDecl and working your way toward the FunctionDecl instead.
You can create a recursive function which finds all TagDecl in a translation unit, searches all methods in that class for the FunctionDecl you specify, and also recursively consumes TagDecls within that TagDecl, until you have nothing left to consume.
This would allow you to more easily keep a complete record of the specific AST nodes you want, and would probably be less confusing to write.
However, if you choose to work your way backward you can try something like this (untested)
FunctionDecl *FD;
DeclContext *PC = FD->getParent();
while (!isa<TranslationUnitDecl>(Decl::castFromDeclContext(PC))) {
//consume
PC = PC->getParent();
}
for descendants (children) you'll just have to cast to a type with children and iterate.

Related

Pascal binary search tree that contains linked lists

I need to search through its contents with a recursive function, so it returns a boolean response depending whether the value I read was found or not. I dunno how to make it work. Here's the type for the tree I defined:
text=string[30];
list=^nodeL;
nodeL=record
title:text;
ISBN:text;
next:list;
end;
tree=^nodeT;
nodeT=record
cod:text;
l:list;
LC:tree;
RC:tree;
end;
This looks like a "please do my assignment for me post", which I won't do. I will try and help you do the assignment yourself.
I don't know exactly what your assignment is, so I'm going to have to make some guesses.
I think your assignment is to write a recursive function that will search a tree and return a boolean response depending on whether a value (input to the function) is found or not.
I don't know how the tree gets its content. You say, you defined the tree type, so I'm guessing that means you are not provided with a tree that already has content. So, at least for testing purposes, you are going to have to write code to add content to the tree (so you can search it).
I don't know exactly what kind of tree you are supposed to create. Usually trees have rules about how the items are arranged in the tree. A common type of tree, is a binary tree, where for each node, the item in the left node (if present) is "less than" the item in the right node (if present). You probably need this when adding items (i.e. content) to the tree.
I think you need to change your definition of the tree node, nodeT (I could be wrong). A tree is a kind of linked list, it does not usually contain linked lists. Usually each tree node contains an item of data (not a list of items).
If I were doing in this assignment (and learning to program in Pascal) I would do the following (in this order):
Make sure I understand linked lists (at least singe-linked list). Write at least one program to add data to a linked list, and search
it (do not use recursion).
Make sure I understand recursion. Read some tutorials on recursion (that do not use linked lists, or trees). For example "First Textbook Examples of Recursion". Write at least one program that uses recursion (do not use linked lists or trees).
Make sure I understand trees. Read some tutorials on trees. For example, "Binary Search Trees"
Do the assignment.
P.S. You might want to change the name of your text type from "text", because, in Pascal, "text" is the name of a predefined type, for text files.

Lowest common ancestor in networkx

How can I get the output of this generator? out.next() or next(out) doesn't work:
out=nx.tree_all_pairs_lowest_common_ancestor(G)
print(out)
<generator object tree_all_pairs_lowest_common_ancestor at 0x000002BE4EF90D48>
nx.tree_all_pairs_lowest_common_ancestor is only aimed at working on certain graph structures, as mentioned in the docs. In the case no root is specified, as in your case, the function will do as follows:
If root is not specified, find the exactly one node with in degree 0 and
use it. Raise an error if none are found, or more than one is. Also check
for any nodes with in degree larger than 1, which would imply G is not a
tree.
So it is likely that your function either has multiple root nodes, or there are none, i.e your graph is not a tree. So you can either search locally using a Breadth-first search, or specify the root node of the subtree to operate on in nx.tree_all_pairs_lowest_common_ancestor.

IBM Websphere scripting ID 'containment paths' explained (I.e. AdminConfig.getid(...) )

I have been digging through IBM knowledge center and have returned frustrated at the lack of definition to some of their scripting interfaces.
IBM keeps talking about a containment path used in their id definitions and I am only assuming that it corresponds to an xml element within file in the websphere config folder hierachy, but that's only from observation. I haven't found this declared in any documentation as yet.
Also, to find an ID, there is a syntax that needs to be used that will retrieve it, yet I cannot find a reference to the possible values of 'types' used in the AdminConfig.getid(...) function call.
So to summarize I have a couple questions:
What is the correct definition of the id "heirachy" both for fetching an id and for the id itself?
e.g. to get an Id of the server I would say something like: AdminConfig.getid('/Cell:MYCOMPUTERNode01Cell/Node:MYCOMPUTERNode01/Server:server1'), which would give me an id something like: server1(cells/MYCOMPUTERNode01Cell/nodes/MYCOMPUTERNode01/servers/server1|server.xml#server_1873491723429)
What are the possible /type values in retrieving the id from the websphere server?
e.g. in the above example, /Cell, /Node and /Server are examples of type values used in queries.
UPDATE: I have discovered this document that outlines the locations of various configuration files in the configuration directory.
UPDATE: I am starting to think that the configuration files represent complex objects with attributes and nested attributes. not every object with an 'id' is query-able through the AdminConfig.getid(<containment_path>). Object attributes (and this is not attributes in the strict xml sense as 'attributes' could be nested nodes with a simple structure within the parent node) may be queried using AdminConfig.showAttribute(<element_id>, <attribute_name>). This function will either return the string value of the inline attribute of the element itself or a string list representation of the ids of the nested attribute node(s).
UPDATE: I have discovered the AdminConfig.types() function that will display a list of all manipulatible object types in the configuration files and the AdminConfig.parent() function that displays what nodes are considered parents of the specified node.
Note: The AdminConfig.parent() function does not reveal the parents in order of their hierachy, rather it seems to just present a list of parents. e.g. AdminConfig.parent('JDBCProvider') gives us this exact list: 'Cell\nDeployment\nNode\nServer\nServerCluster' and even though Server comes before ServerCluster , running AdminConfig.parent('Server') reveals it's parents as: 'Node\nServerCluster'. Although some elements can have no parents - e.g. Cell - Some elements produce an error when running the parent function - e.g. Deployment.
Due to the apparent lack of a reciprocal AdminConfig.children() function, It appears as though obtaining a full hierachical tree of parents/children lies in calling this function on each of the elements returned from the AdminConfig.parent(<>) call and combining the results. That being said, a trial and error approach is mostly fruitful due to the sometimes obvious ordering.

XQuery - calculating number of elements

I'm trying to declare a user-defined function in XQuery, that would be passed an element and would return the total number of elements in its tree (meaning itself plus its subtree).
Is this even possible to do in XQuery with a recursive function or will I need another approach?
Yes, this is possible. As this has a smell of homework, I'm not giving a full answer, but the idea on how to do it.
For both cases, you'll have to consider which children to consider while counting. Reading your question, it looks like you're looking for elements only and can safely ignore attributes, comments, text nodes and processing instructions.
Using a Recursive Function
Define a function, which sums up the size of each individual subtree (which you determine by a recursive function call). Something like (this is not XQuery code!):
function subtree_size {
sum(
for each element
return subtree_size(current element)
)
}
Passing all Elements to the count Function
XQuery has a count function, which returns the number of elements passed. There is a very short and rather easy to find XPath expression to return all descendant nodes (including the node itself). Have a look at the axis steps available in XPath.

What is the best way to uniquely identify a DOM node?

What is a way to uniquely identify all DOM nodes in an HTML document. To illustrate what I mean, here is a (fictional) example:
Script X randomly selects a DOM node from document.html.
Script X needs to tell script Y which DOM node it has chosen.
How does script X uniquely identify the DOM node it has chosen so that script Y knows exactly which node it is in document.html?
I'm really interested in how to uniquely identify the DOM node so that the script Y can identify it and manipulate it. Preferably, it should work with text nodes as well. I was thinking of XPath maybe, but I'm not sure how to generate a unique XPath to any given node.
You should be able to determine a unique XPath by working backwards from the node to the root node, and tracking the node you're on, and which sibling it is, such that you get something like:
/a[1]/b[2]/c[101]/text()
so that's the 101st C node under the second B node, etc. As such, that's a unique path and can be copied around with reference to the original document
You might want to take a look at XPathGen https://github.com/amouat/XPathGen
It will create a unique XPath of the form /node()[1]/node()[1] for a given DOM node. However, there are some issues with XPath, namely non-coalesced text nodes and "prolog" nodes, which cannot be uniquely identified purely with XPath. For example if you have the following document in DOM:
<a>b</a>
And add a text node to become:
<a>bc</a>
The XPath to nodes b and c will be the same, but you will still have separate DOM nodes (unless you call normalize on the document). If you need to handle this situation you will need to store offsets and lengths for text nodes.
Well, an XPath expression that results in a single node should be unique. What do you mean by "how to generate a unique XPath to any given node"?
Ordinal child positions along XPath axes. Nodes are strongly ordered, and so saying:
child 1 of child 3 of child 4 of child 5.
should do it.