Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Analyze C code in a variety of forms
ResourceFunction["CodeStructure"][File[filename]] gives the syntax tree of the C file filename. | |
ResourceFunction["CodeStructure"]["string"] gives the syntax tree of the C program given by string. | |
ResourceFunction["CodeStructure"]["string", form] generates a representation of the C program given by string in the format specified by form. | |
ResourceFunction["CodeStructure"][File[filename],form] generates a representation of the C file filename in the format specified by form. |
"SyntaxTree" | syntax tree of the code as a graph |
"SyntaxAnnotation" | annotation of how the syntax tree matches up with a C program |
"SourceAnnotation" | annotation of how the syntax tree matches up with the source code of a C program exactly |
"TokenAnnotation" | annotation of the tokens of the C program |
"CallGraph" | basic call graph of the C program |
"FileCallGraph" | basic call graph of the C program where the files are the nodes and the function calls are the edges |
"CommandLineArguments" | arguments to give to the compiler |
"IncludeTypeInformation" | whether to include type information in the syntax tree |
"BinaryLocation" | location of the binary expected from the build process within the directory |
"ClangBinariesDirectory" | location of the directory containing the clang and clang++ binaries (required if they are not on PATH) |
"DropExternalFunctions" | whether to drop functions that seem to have been defined outside the project to be analyzed |
"NoPostProcessing" | whether to return a raw call graph with no post-processing |
"Recursive" | whether to recursively search for C source files when analyzing a directory that does not contain a CMakeLists.txt or Makefile |
"IncludeFilePrefixes" | whether to include in the label of a function the file it was defined in |
"ShowFunctionCount" | whether to show how many times a file calls a function |
Create two C source files:
In[1]:= |
Get the call graph of the two C source files and use the directory HeadersFolder as an included directory:
In[2]:= |
Out[2]= |
Drop the file prefixes by setting the option "IncludeFilePrefixes" to False:
In[3]:= |
Out[3]= |
Get the call graph of a short C program:
In[4]:= |
Out[4]= |
Use the form "SyntaxAnnotation" to display how the syntax tree of a C program lines up with its source code:
In[5]:= |
Out[5]= |
Use the form "CallGraph" to show the call graph of a directory containing a complete C project using its CMakeLists.txt:
In[6]:= |
Out[6]= |
Use the form "FileCallGraph" to show the call graph of a C project where the files are the nodes and the function calls are the edges:
In[7]:= |
Out[7]= |
Use Wolfram Language graph functions to further refine the file call graph:
In[8]:= |
Out[8]= |
Get the syntax tree of a short C program:
In[9]:= |
Out[9]= |
Display the syntax tree of a C program using the form "SyntaxTree":
In[10]:= |
Out[10]= |
Display tags for the types of tokens in a C program using the form "TokenAnnotation":
In[11]:= |
Out[11]= |
Use the form "SourceAnnotation" to display how the syntax tree of a C program lines up with a more exact version of its source code:
In[12]:= |
Out[12]= |
Call graph generation will fail if the clang and clang++ binaries are not on PATH, as is typical on Linux, where gcc is the default compiler:
In[13]:= |
Out[13]= |
Check the value of CodeAnalysis`$BuildError to find the cause of the error:
In[14]:= |
Out[14]= |
Manually specify the location of the clang and clang++ binaries using the "ClangBinariesDirectory" option:
In[15]:= |
Out[15]= |
Call graph generation will fail if filename is a directory that contains a CMakeLists.txt that requires certain cache variables to be defined:
In[16]:= |
Out[16]= |
Check the value of CodeAnalysis`$BuildError to find the cause of the error:
In[17]:= |
Out[17]= |
To resolve the issue, manually define any required CMake cache variables in the codeanalysis-build subdirectory:
$ cd /Users/wolfram/wstpserver/codenalysis-build
$ cmake -DMATHLINK_INCLUDE_DIRECTORY=/Users/wolfram/mathlink/build/include.
Code analysis can fail or produce a bad result if required command-line arguments are missing:
In[18]:= |
Out[18]= |
Check the value of CodeAnalysis`$BuildError to find the cause of the error:
In[19]:= |
Out[19]= |
Pass the definition of the COMMAND_LINE_NUMBER macro to the compiler using the "CommandLineArguments" option:
In[20]:= |
Out[20]= |
This work is licensed under a Creative Commons Attribution 4.0 International License