Search code examples
abstract-syntax-treeadagnatsubtypeasis

Ada Source code modifications using ASIS(Ada Semantics interface specifications)


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?


Solution

  • 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;