Skip to content

Instantly share code, notes, and snippets.

@mlcollard
Last active October 1, 2018 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlcollard/e21ac208e8d764704d3e4903204752e8 to your computer and use it in GitHub Desktop.
Save mlcollard/e21ac208e8d764704d3e4903204752e8 to your computer and use it in GitHub Desktop.
Static Analysis with srcML and XSLT
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.srcML.org/srcML/src"
xmlns:src="http://www.srcML.org/srcML/src"
version="1.0">
<!--
calls_direct.xsl
Direct calls from every function in CSV format
<function_name>,<call1>,<call2>,...<calln>
-->
<xsl:output method="text" encoding="UTF-8"/>
<!-- Output list of direct calls for all functions -->
<!-- Note: .//src:function equivalent to descendant::src:function -->
<xsl:template match="/"><xsl:apply-templates select="descendant::src:function"/></xsl:template>
<!-- Output the name of the function and all direct calls -->
<xsl:template match="src:function">
<!-- Note: .//src:call equivalent to descendant::src:call -->
<xsl:value-of select="src:name"/><xsl:apply-templates select="descendant::src:call"/><xsl:text>
</xsl:text>
</xsl:template>
<!-- Comma followed by name of the call -->
<xsl:template match="src:call">,<xsl:value-of select="src:name"/></xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.srcML.org/srcML/src"
xmlns:src="http://www.srcML.org/srcML/src"
xmlns:exsl="http://exslt.org/common"
xmlns:set="http://exslt.org/sets"
xmlns:static="http://mlcollard.net/static"
extension-element-prefixes="set exsl"
exclude-result-prefixes="src"
version="1.0">
<!--
calls_key.xsl
Direct and indirect calls from every function in CSV format, using xsl:key
<function_name>,<call1>,<call2>,...<calln>
-->
<xsl:output method="text" encoding="UTF-8"/>
<!-- Map from function name to direct calls in that function -->
<xsl:key name="calls" match="src:call/src:name" use="ancestor::src:function[1]/src:name"/>
<!-- Output list of direct and indirect calls for all functions -->
<xsl:template match="/"><xsl:apply-templates select="descendant::src:function"/></xsl:template>
<!-- Output the name of the function and all the direct and indirect calls -->
<xsl:template match="src:function">
<!-- all calls, direct and indirect, with possible duplicates -->
<xsl:variable name="callset">
<xsl:apply-templates select="src:name" mode="call"/>
</xsl:variable>
<!-- output distinct calls in CSV -->
<xsl:for-each select="set:distinct(exsl:node-set($callset)/static:call)">
<xsl:value-of select="."/><xsl:text>,</xsl:text>
</xsl:for-each>
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:template match="src:name" mode="call">
<xsl:param name="sofar" select="src:name"/>
<!-- current call name wrapped in a static:call element -->
<static:call><xsl:copy-of select="."/></static:call>
<!-- direct calls made by the function with the current name that have not been visited so far -->
<xsl:variable name="calls" select="key('calls', current())[not(.=$sofar)]"/>
<!-- direct, and indirect, calls -->
<xsl:apply-templates select="$calls" mode="call">
<xsl:with-param name="sofar" select="$calls"/>
</xsl:apply-templates>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.srcML.org/srcML/src"
xmlns:src="http://www.srcML.org/srcML/src"
version="1.0">
<!--
text.xsl
Output all text in xml (i.e., not tags)
Starting point for static analysis using srcML
-->
<xsl:output method="text" encoding="UTF-8"/>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment