Skip to content

About

Stereocode classifies methods, free functions, and types (e.g., classes) of code in multiple languages by their stereotypes, such as get, command, or entity, in srcML archives.

Topics

Resources

Stars

2 stars

Watchers

11 watching

Forks

Latest commit

Β 

History

245 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🧬 Stereocode

Tests C++17 Release License

Stereocode classifies methods, free functions, and types (e.g., classes) of C, C++, C#, Java, Python, JavaScript, and TypeScript code by their stereotypes.

A stereotype is a short label for the role of a code element (e.g., a method or a class), such as get for a method that returns a field, command for a method that changes the state of the object, or entity for a class that keeps its data together with the methods that work on the data.

Tip

The wiki defines every method, class, and free-function stereotype with its rules and examples.

Downstream Tools gives every member of the report of -n and every function of the library.

Developer Notes lists the developers, the history, and the citation of Stereocode.

πŸ“¦ Requirements

  • srcML 1.2 with its development package (srcml.org)
  • libxml2 with its development package (libxml2-dev)
  • CMake 3.20 or newer and a C++17 compiler (Clang, GCC, or MSVC)

πŸ”§ Installation

git clone https://github.com/srcML/stereocode.git  # download the source code
cd stereocode                                      # go into the source folder
cmake -B build -DCMAKE_BUILD_TYPE=Release          # configure a release build
cmake --build build                                # compile build/stereocode
cmake --install build                              # copy stereocode and its library to the system (e.g., /usr/local)

🐳 Docker

The image ghcr.io/srcml/stereocode on GitHub holds srcML and Stereocode for Intel and ARM machines, ready to run without the requirements or the build. GitHub Actions rebuilds the image with each release of Stereocode and every week with the newest srcML release and Ubuntu packages. The tags latest and 2.0.0 always name the newest build.

docker pull ghcr.io/srcml/stereocode                                  # get or update the image
alias run='docker run --rm -v "$PWD:/data" ghcr.io/srcml/stereocode'  # run commands in the image

The commands of the usage example then run in the image, on the files of the current folder.

run srcml --register-ext h=C++ my_project -o my_project.xml  # turn the code into a srcML archive
run stereocode my_project.xml -f -r                          # add stereotypes and write CSV reports

πŸš€ Usage

Stereocode reads a srcML archive and annotates it with stereotype information as st:stereotype attributes.

For example, these two commands classify a C++ project in the folder my_project.

srcml --register-ext h=C++ my_project -o my_project.xml  # turn the code into a srcML archive
build/stereocode my_project.xml -f -r                    # add stereotypes and write CSV reports

In the output archive my_project.stereotypes.xml, code elements (e.g., classes and functions) carry their stereotypes, as in this example.

<class st:stereotype="entity"> ... </class>
<function st:stereotype="get"> ... </function>

Note

srcML parses .h files as C by default. A C++ class in such a header then gets no class markup. The option --register-ext h=C++ of srcml parses these headers as C++.

Pipelines and queries

Stereocode also runs as one step in a pipeline of srcML tools. A run without an input archive accepts the archive on standard input and writes the annotated archive to standard output. With an input file, the option -o - sends the annotated archive to standard output as well.

srcml --register-ext h=C++ my_project | build/stereocode -f > annotated.xml  # convert and annotate in one pipeline

A later step of the pipeline can query the stereotypes with XPath. The stereotypes are attributes in the namespace http://www.srcML.org/srcML/stereotype, which a query binds to a prefix such as st. An element with several stereotypes lists them in one attribute, separated by spaces.

srcml my_project | build/stereocode \
  | srcml --xmlns:st=http://www.srcML.org/srcML/stereotype \
          --xpath "//src:function[contains(concat(' ', @st:stereotype, ' '), ' get ')]/src:name"  # list the names of the get methods

Options

Option Description
-o, --output-file Output archive or - for standard output (default <input>.stereotypes.xml or, for standard input, standard output)
-f, --free-function Stereotypes free functions and static methods
-t, --struct Stereotypes structs (C, C++, and C#)
-i, --interface Stereotypes interfaces (C#, Java, and TypeScript)
-u, --union Stereotypes unions (C and C++)
-e, --enum Stereotypes enums (Java)
-r, --csv-report Writes a CSV report of the stereotypes
-x, --txt-report Writes a text report of the stereotypes
-b, --verbose Writes a detailed CSV report (e.g., the calls and the data members of each function) and prints the settings in use
-n, --json-report Writes a JSON report of the stereotypes and the facts of each type and function to a file or, with -, to standard output
-m, --comment Inserts a // @stereotype <labels> comment (# @stereotype <labels> in Python) before each stereotyped element
-d, --no-srcml-output, --srcmlOutput Writes no output archive, only the reports of -r, -x, -b, or -n
-j, --threads Number of threads (default all available up to 8, at most 256)
-l, --large-class Method count above which a type can be a large-class (default mean plus standard deviation over all types)
-p, --primitive-file File of extra types that count as primitive (e.g., a typedef of int)
-c, --call-file File of extra calls to ignore (e.g., a logging function)
-s, --specifier-file File of extra specifiers to strip from types (e.g., an export macro)
-q, --query-file File of extra methods whose calls leave the object unchanged (e.g., size)
-v, --version Prints the version
-h, --help Prints the options

Tip

The detailed report of -b lists, for each function, its calls, the data members, parameters, and globals that it uses and writes, and the objects that it creates. The scripts of Stereocode 1.0 read its first 22 columns unchanged, since these columns keep their names and their order. Its line numbers need an archive that srcml --position writes.

⚑ Speed. Stereocode annotates the 231 MB srcML archive of PowerShell 7.5.4 in about 5 s on its default of eight threads.

JSON report

The option -n writes the stereotypes and the facts of the detailed report as one JavaScript Object Notation (JSON) document. Each type lists its methods with their facts in the types of JSON, such as an array for the data members that a method writes and a number for its line. A fact that the archive lacks is null, such as a line number in an archive that srcml wrote without --position.

srcml --position my_project | build/stereocode -f -d -n - > facts.json  # write only the JSON report

This example shows some members of the report of one class. The member unit gives the position of a unit in the input archive, with one for the first unit. Within that unit, the member xpath selects the element.

{
  "stereocode_version": "2.0.0",
  "schema_version": 1,
  "types": [
    {
      "name": "A",
      "kind": "class",
      "stereotypes": ["data-provider", "data-class", "small-class"],
      "elements": [{"unit": 1, "xpath": "/*/*[1]"}],
      "data_members": ["x"],
      "methods": [
        {
          "name": "getX",
          "signature": "getX()",
          "stereotypes": ["get"],
          "element": {"unit": 1, "xpath": "/*/*[1]/*[2]/*[2]/*[1]"},
          "data_members_used": ["x"],
          "returns": ["data-member"]
        }
      ]
    }
  ],
  "free_functions": []
}

Library

The library libstereocode offers the analysis to other tools through the C functions of stereocode.h, in the style of libsrcml. The install command of the Installation section copies the library and the header to the system with a CMake package. The CMake option -DBUILD_SHARED_LIBS=ON builds a shared library in place of the static library.

project(my_tool LANGUAGES C CXX)                                  # enable C++ also for a C tool, since the static library needs it to link
find_package(Stereocode REQUIRED)                                 # find the installed library
target_link_libraries(my_tool PRIVATE Stereocode::LibStereocode)  # link my_tool with the library

A tool analyzes an archive once and then obtains the result as the output archive, as a report, or as the record of each type and function. A record gives the facts of the JSON report through four functions, for a text, a number, the size of a list, and an item of a list. In this example, the first method of a class without methods is NULL, as is every record or text that does not exist.

#include <stereocode.h>

stereocode_analysis* analysis = stereocode_analyze_filename("my_project.xml", NULL);  /* classify the archive with the default options */
if (!analysis) fprintf(stderr, "%s\n", stereocode_error_string());                    /* print the reason of a failure */

stereocode_analysis_write_archive_filename(analysis, "my_project.stereotypes.xml");   /* write the output archive */
stereocode_analysis_write_report_FILE(analysis, STEREOCODE_REPORT_JSON, stdout);      /* print the JSON report */

const stereocode_type*     type   = stereocode_analysis_get_type(analysis, 0);        /* get the record of the first class */
const stereocode_function* method = stereocode_type_get_method(type, 0);              /* get the record of its first method */
const char* name  = stereocode_function_get_text(method, STEREOCODE_FUNCTION_NAME);                 /* e.g., getX */
const char* label = stereocode_function_get_list_item(method, STEREOCODE_FUNCTION_STEREOTYPES, 0);  /* e.g., get */
const char* xpath = stereocode_function_get_text(method, STEREOCODE_FUNCTION_XPATH);                /* locate its element in its unit */

stereocode_analysis_free(analysis);                                                   /* free the analysis with its records */

Beyond the file names of this example, the library works on the archives of libsrcml directly. The function stereocode_analyze_archive analyzes an archive that a tool opened for reading. After the analysis, stereocode_analysis_write_archive adds the units with their stereotypes to an archive that the tool opened for writing.

List files

The options -p, -c, -s, and -q each read a text file that adds entries to a built-in list, one entry per line. A line [C++], [C#], [Java], [Python], or [JavaScript] limits the entries after it to that language, while the entries before the first such line hold for every language. The line [JavaScript] also covers TypeScript, which srcML marks as JavaScript. A line that starts with # is a comment.

List Example entries Description
Primitive types (-p) qint64, qreal Qt typedefs of numbers
Ignored calls (-c) spdlog::info, Log.d Logging calls of spdlog and Android
Type specifiers (-s) SCI_METHOD A Scintilla macro that srcML reads as part of a return type
Query methods (-q) contains, isBlank Methods whose calls leave the object unchanged

For example, this file queries.txt adds two C++ methods and one Java method for -q.

# Methods whose calls leave the object unchanged
[C++]
contains
starts_with
[Java]
isBlank
build/stereocode my_project.xml -q queries.txt   # use the extra query methods

πŸ§ͺ Tests

Each test name starts with the folder of its files under test/.

  • Unit tests (unit) check the stereotype rules, the helper functions, and the functions of the library in one doctest program.
  • Integration tests (integration) run stereocode on small archives and check the exit code, the messages, and the output files. One test builds a C program with the installed library.
  • Stereotype cases (stereotypes) are small C, C++, C#, Java, Python, JavaScript, and TypeScript files whose expected stereotypes are written by hand in @stereotype comments.
  • System tests (systems) compare every label of Notepad++ (C++), Lua (C), PowerShell (C#), RxJava (Java), Rich (Python), Knex (JavaScript), and TypeORM (TypeScript) with a table recorded from earlier runs.

The stereotype cases, the system tests, and the integration tests of the option -m convert source files to srcML with the srcml program. Without this program, the configuration skips these tests and names them in a status message. The system tests run one at a time, also under ctest -j 8, since each one runs on up to eight threads.

Commands

Run these commands inside the build folder (cd build).

Command Description
ctest -j 8 Every test
ctest -LE systems Every test but the seven systems
ctest -L systems Only the seven systems
ctest -L stereotypes Only the stereotype cases and the helper checks
ctest -R stereotypes/java The stereotype cases of one language
ctest -L unit Only the unit tests
ctest -L integration Only the integration tests
ctest -R <name> --output-on-failure One test, with its output on a failure
ctest -N Lists every test name

πŸ“œ License

Stereocode is free software under the GNU General Public License version 3, as LICENSE states.

About

Stereocode classifies methods, free functions, and types (e.g., classes) of code in multiple languages by their stereotypes, such as get, command, or entity, in srcML archives.

Topics

Resources

Stars

2 stars

Watchers

11 watching

Forks

Releases

Packages

Contributors

Languages