AdaCore/Ada-SPARK-Crate-Of-The-Year

[2022][rejuvenation] Analysis and manipulation of Ada code based on concrete patterns

Closed this issue · 0 comments

Rejuvenation

Analysis and manipulation of Ada code based on concrete patterns

badge

Please click
thumbs up
to
vote for Rejuvenation-Ada as The 2022 Ada Crate Of The Year

Problem

Many organizations have invested huge amounts in Ada code, possibly starting from the 1980s. Yet, change is the only constant: hardware becomes obsolete; AdaCore releases new Ada versions, such as Ada 2022; libraries get extended with new functionality; people join and leave the organization; and new features are added. These changes often cause Ada code to become legacy code that is hard to understand and maintain.
Powerful tools for analysis and manipulation, such as Libadalang and ASIS, help to understand and maintain code. Unfortunately, many developers experience a steep learning curve. Can these useful, powerful tools be made more intuitive?

Solution

The Ada programming language has a concrete syntax and every implementation of the Ada programming language uses an abstract syntax. The current tools for analysis and manipulation, such as Libadalang and ASIS, not only use an abstract syntax but also expose that abstract syntax. The rejuvenation crate is a tool for analysis and manipulation with an interface based on Ada's concrete syntax. Consequently, developers do not have to learn an abstract syntax since they are already familiar with Ada's concrete syntax.

Simple example

With the rejuvenation crate, you can analyze, insensitive for layout and the presence of comments, for occurrences of the code pattern

ready; set; go;

and you would find the following occurrences (when they appear in your Ada project)

ready; set; go;
ready; 
set; 
go;
ready;   -- first signal to prepare action

set;     -- second signal to acquire resources

go;      -- take action

Furthermore, you can manipulate all found occurrences by replacing them with the code pattern

start;

Placeholders

The rejuvenation crate adds one extension to Ada's concrete syntax: Placeholders. Ada's concrete syntax can be easily extended by using characters, such as $, that are illegal in the Ada programming language (except inside character and string literals). The rejuvenation crate introduces two types of placeholders, those starting with $S_ and those starting with $M_. Any alphanumeric string is allowed to follow after those prefixes.

  • $S_ placeholders allow one to match a single node in the abstract syntax, be that a single expression, a single statement, a single argument, or anything else. As long as Ada parses it to a single node, the $S_ placeholders can match it.
  • $M_ placeholders allow one to match a list of nodes in the abstract syntax, i.e., zero or more nodes.

Real-world example

Now you are able to manipulate your Ada code to use quantified expressions by just applying two patterns:
A pattern to find the code that is equivalent to returning a for all expression

for $S_Element of $S_Elements loop 
    if $S_Condition then return false; end if; 
end loop;
return true;

and a pattern to replace that code to use the for all expression

return (for all $S_Element of $S_Elements => not ($S_Condition));

where the value of the placeholders in the replacement is equal to the value in the found code.

Let's illustrate using an instance of this find pattern that was found in the semantic versioning project:

could be simplified into

 return (for all R of VS => Satisfies (V, R));

where also the find and replace of not (not ($S_Expr)) by $S_Expr was used.

This manipulation is beneficial not only for migrating code written before quantified expressions became available in Ada 2012
but also for developers that might not be aware of this particular feature of Ada.

Next steps

The rejuvenation crate contains a workshop to learn even more, many examples, and the Find and Find and Replace tools for easy experimentation.

The rejuvenation crate is part of the Renaissance Ada project that contains a variety of tools for analysis and manipulation of Ada code. The Rewriters crate, in particular, contains many examples of how to use the rejuvenation crate.

Architecture

The rejuvenation crate exploits the power of the abstract syntax as provided by Libadalang, making the analysis and manipulation insensitive for layout and the presence of comments. Yet, its interface does not expose Libadalang's abstract syntax. Instead, its interface is almost identical to the concrete syntax of the Ada programming language.

Using the rejuvenation crate one can:

  • find and replace based on concrete patterns using Rejuvenation.Finder and Rejuvenation.Find_And_Replacer;
  • programmatically analyze and manipulate using Rejuvenation.Text_Rewrites to manipulate text and Rejuvenation.Match_Patterns to inspect the piece of code found;
  • select the part, e.g. text, file, directory, or project, for analysis and manipulation using Rejuvenation.Simple_Factory and Rejuvenation.Factory; and
  • pretty print the manipulated code only, using Rejuvenation.Pretty_Print.

About the project

ESI together with Thermo Fisher and Philips developed in public-private research projects the Renaissance methods to handle legacy software. For more info, see e.g. this Bits & Chips article describing how "ESI helps Thermo Fisher and Philips grease their software machines" and ESI's research on model-based software transformation.

The Renaissance methods are of course supported by tools. These tools were originally developed to target the C and C++ programming languages that are used by Thermo Fisher and Philips. The development of tools to target the Ada programming language started in a public-private research project together with ITEC, an independent subsidiary of Nexperia. These tools are available at Renaissance-Ada.

Nexperia described during the AdaCore Tech Days 2021 how they benefit from the Renaissance tools. The rejuvenation crate is an essential part of most of these tools.

Pierre van de Laar and Arjan Mooij presented the rejuvenation crate as part of "Renaissance-Ada: Tools for Analysis and Transformation of Ada Code" at 26th Ada-Europe International Conference on Reliable Software Technologies (AEiC 2022) on 15 June 2022 in Ghent, Belgium.

Pierre van de Laar was interviewed on the rejuvenation crate at Bits and Chips for the Technology Pioneers podcast.

Authors

Pierre van de Laar and Arjan Mooij developed the Ada version of the rejuvenation crate inspired by the C/C++ version. The C/C++ version was developed by Dennis Dams, Arjan Mooij, Jeroen Ketema, Pierre van de Laar, Rosilde Corvino,
and many more. Fabien Chouteau inspired and contributed to making the rejuvenation library an Alire crate.

Benefits for the Ada/SPARK community

The rejuvenation crate is a powerful tool for analysis and manipulation of Ada code, enabling the community to
understand and maintain Ada software that has evolved over many years. Furthermore, the rejuvation crate usage of Ada's concrete syntax ensures that developers can focus on their analysis and manipulation goals without the need to map them on the abstract syntax used within the analysis and manipulation tool. Using the rejuvenation crate, the community can more easily follow compiler changes, educate non-Ada programmers on unique Ada features, and better embrace change.

Using the rejuvenation crate is as simple as using regular expressions in Ada. Although the rejuvenation crate has well-defined interfaces and good documentation, we are open to suggestions for improvements. The rejuvenation crate is available in a public repository at github where issues and suggestions for improvements can be submitted. The rejuvenation crate uses git as the version control system. The rejuvenation crate is built using the alr build command. For pretty-printing modified code, gnatpp is used. gnatpp is part of the libadalang_tools. Unfortunately, the user still has to install gnatpp manually.

Open source

The rejuvenation crate has a BSD3 software license. It uses the open tools libadalang and gnatpp. It is platforms independent. Unfortunately, the MacOS build fails since our dependency libadalang depends on gnatcoll that depends on gmp which is not standardly available on MacOS

Software quality

The rejuvenation crate has a large number of test cases that are automatically run using Github Actions for each commit. The rejuvenation crate practices eat your own dog food and the analysis and manipulation capabilities are used to ensure that its own code is of good quality. Furthermore, we regularly check the rejuvenation crate status to determine whether improvements are needed. Although the rejuvenation crate has well-defined interfaces and good documentation, we are open to suggestions for improvements.

Buzz effect

Using the rejuvenation crate, we have analyzed and manipulated a number of open source Ada projects to improve their code quality. Furthermore, to create the "wow effect" we have submitted a proposal to make a language for automatic analysis and manipulation of Ada code part of the Ada ecosystem.
Finally, we posted on social media, see e.g. https://www.linkedin.com/posts/pierrevandelaar_announcing-the-2022-adaspark-crate-of-the-activity-7015962728859115520-HK02?utm_source=share&utm_medium=member_desktop