2

i am developing a tool for finding sub type range overflow problems in Ada source code.for this purpose i am using ASIS for finding assignment statements in Ada source code and finding type of the variables on the right side of the assignment expression.now i want to replace the variables(not of record type) of the assignment expression with 'first, 'last values of the variable type in the assignment statement so that i will get compilation error if any range overflow happens.below is an example what i am trying to convey.

procedure Example is
   subtype A_Type is integer 1 .. 10;
   subtype B_Type is integer -5 .. 5;
   subtype C_Type is integer 1 .. 12;

   A : A_Type;
   B : B_Type;
   C : C_Type;
begin
   C := A + B;
end Example;

I want modify C := A + B; with C := A_Type'Last + B_Type'Last in the source code. C := A_Type'Last + B_Type'Last assignment statement will get warning at compile time or Constraint error during run time.

Is it possible do above modifications with ASIS?

Jacob Sparre Andersen
  • 6,733
  • 17
  • 22

4 Answers4

2

For your purpose, you shouldn't rewrite the source text you are processing. You should rather write a new program, which only contains exactly the required declarations and assignments.

So the output should be something like:

with Type_Declarations;

procedure Test_Driver is
begin
   declare
      C : Type_Declarations.C_Type;
   begin
      C := Type_Declarations."+" (Type_Declarations.A_Type'First, Type_Declarations.B_Type'First);
      C := Type_Declarations."+" (Type_Declarations.A_Type'First, Type_Declarations.B_Type'Last);
      C := Type_Declarations."+" (Type_Declarations.A_Type'Last, Type_Declarations.B_Type'First);
      C := Type_Declarations."+" (Type_Declarations.A_Type'Last, Type_Declarations.B_Type'Last);
   end;
end Test_Driver;
Jacob Sparre Andersen
  • 6,733
  • 17
  • 22
1

ASIS was not designed to make modifications like that. You can, however take a look at libadalang from AdaCore, which supports this (and works on partial sources, so you won't have to precompile your sources)

egilhh
  • 6,464
  • 1
  • 18
  • 19
1

GNAT includes utilities gnat2xml and xml2gnat; gnat2xml generates a representation of the source based on ASIS, and xml2gnat converts it back to Ada. You could maybe modify the XML output of the first and feed it back to the second.

Not that I’m recommending this; the XML schema isn’t documented, and is complicated.

Simon Wright
  • 25,108
  • 2
  • 35
  • 62
  • Does the XML preserve formatting of text and comments? – Ira Baxter Jun 20 '18 at 16:00
  • No: from the [doc](https://docs.adacore.com/gnat_ugn-docs/html/gnat_ugn/gnat_ugn/gnat_utility_programs.html#the-ada-to-xml-converter-gnat2xml), "xml2gnat is a back-translator that translates the XML back into Ada source code. This is primarily for the purpose of testing gnat2xml, rather than for users. The Ada generated by xml2gnat has identical semantics to the original Ada code passed to gnat2xml. It is not textually identical, however — for example, no attempt is made to preserve the original indentation." – Simon Wright Jun 20 '18 at 16:28
  • So this might be ok for temporarily-generated code but not for permanent code changes that one would like to have. Our experience is that if you remove comments and formatting from code, the programmers will instantly reject the result if they have to maintain it. – Ira Baxter Jun 20 '18 at 17:12
  • Afraid I can’t comment, because I can’t get the tool to run! But, anyway, I’d have to agree with you, I’d reject the result too. It depends really on what has to be maintained. – Simon Wright Jun 21 '18 at 19:00
1

If you want a tool that can apply modifications to Ada source code, you might be interested in our DMS Software Reengineering Toolkit with its Ada front end.

DMS parses source code to ASTs, and makes those ASTs available for modification using DMS's Abstract Syntax Tree procedural interface (direct hacking at the tree nodes) and/or DMS's rewrite rules (source-to-source transformations "if you see this replace it by that" written in [Ada] language surface syntax, that directly manipulates the trees. After your changes are made, DMS can prettyprint the source to regenerate valid Ada source code, even preserving comments and formatting in those places that have not been modified.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341