libgpr2_24.0.0_eda3c693/tools/src/gprinspect-process.adb

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
------------------------------------------------------------------------------
--                                                                          --
--                           GPR2 PROJECT MANAGER                           --
--                                                                          --
--                     Copyright (C) 2019-2023, AdaCore                     --
--                                                                          --
-- This is  free  software;  you can redistribute it and/or modify it under --
-- terms of the  GNU  General Public License as published by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion.  This software is distributed in the hope  that it will be useful, --
-- but WITHOUT ANY WARRANTY;  without even the implied warranty of MERCHAN- --
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public --
-- License for more details.  You should have received  a copy of the  GNU  --
-- General Public License distributed with GNAT; see file  COPYING. If not, --
-- see <http://www.gnu.org/licenses/>.                                      --
--                                                                          --
------------------------------------------------------------------------------

with Ada.Calendar.Formatting;
with Ada.Command_Line;
with Ada.Containers.Ordered_Maps;
with Ada.Exceptions; use Ada.Exceptions;
with Ada.Text_IO;

with GNATCOLL.JSON;

with GPRtools.Options;

with GPR2.Containers;
with GPR2.Log;
with GPR2.Message;
with GPR2.Path_Name;
with GPR2.Project.Attribute.Set;
with GPR2.Project.Registry.Attribute;
with GPR2.Project.Tree;
with GPR2.Project.Typ.Set;
with GPR2.Project.Variable.Set;
with GPR2.Project.View.Set;
with GPR2.Version;
with GPR2.View_Ids;

procedure GPRinspect.Process
  (Options : in out GPRinspect.GPRinspect_Options)
is

   use Ada;
   use GNATCOLL;
   use GNATCOLL.JSON;
   use GPR2;
   use GPR2.View_Ids;
   use type GPRtools.Display_Kind;
   use type Project.Registry.Attribute.Value_Kind;

   package PRA renames Project.Registry.Attribute;

   --  Variables for tool's options
   Project_Tree : aliased Project.Tree.Object;

   procedure Inspect_Project_JSON_Output
     (Tree : Project.Tree.Object;
      Compact : Boolean);
   --  Inspect project and possibly recursively all imports

   procedure Inspect_Project_Textual_Output (Tree : Project.Tree.Object);
   --  Inspect project and possibly recursively all imports

   procedure Load_Project
     (Tree    : in out Project.Tree.Object;
      Options : in out GPRinspect.GPRinspect_Options);
   --  Load project to inspect

   function View_Id (View : Project.View.Object) return String;
   --  Get the View's View_Id image

   function No_View_Restriction
     (Views : Restricted_Scope;
      VName : Name_Type) return Boolean;
   --  Return if the view must be processed and displayed or not.

   ---------------------------------
   -- Inspect_Project_JSON_Output --
   ---------------------------------

   procedure Inspect_Project_JSON_Output
     (Tree    : Project.Tree.Object;
      Compact : Boolean)
   is
      J_Res   : constant JSON_Value := Create_Object;
      --  The JSON response

      O_Array : GPR2.Containers.Value_Set;
      --  Object search-paths, global array with all project's object
      --  directories and the possible runtime object directory.

      S_Array : GPR2.Containers.Value_Set;
      --  Sources search-paths, global array with all project's sources
      --  directories.

      Handled      : Project.View.Set.Object;

      function Info_Object return JSON_Value;
      --  Information node (date, toolset version, ...)

      function Tree_Object return JSON_Value;
      --  Information node (date, toolset version, ...)

      procedure Parse_Project
        (Prjs : in out JSON_Array;
         View    : Project.View.Object;
         Parent  : Project.View.Object);
      --  Project information (name, kind, ...)

      function To_JSON_Array (A : GPR2.Containers.Value_Set) return JSON_Array;

      function Image (Kind : Project_Kind) return String;
      --  Returns the Kind only

      -----------
      -- Image --
      -----------

      function Image (Kind : Project_Kind) return String is
         K : constant String := GPR2.Image (Kind);
      begin
         if K (K'Last - 7 .. K'Last) = " project" then
            return K (K'First .. K'Last - 8);
         else
            return K;
         end if;
      end Image;

      ----------
      -- Info --
      ----------

      function Info_Object return JSON_Value is
         Inf : constant JSON_Value := Create_Object;
      begin
         Set_Field (Inf, "generated-on",
                    Calendar.Formatting.Image (Calendar.Clock));
         Set_Field (Inf, "version", GPR2.Version.Long_Value);

         return Inf;
      end Info_Object;

      -------------------
      -- Parse_Project --
      -------------------

      procedure Parse_Project
        (Prjs   : in out JSON_Array;
         View   : Project.View.Object;
         Parent : Project.View.Object)
      is
         function Attributes
           (Atts : Project.Attribute.Set.Object) return JSON_Array;
         --  Return the set of attribute as a JSON_Array

         function Variables
           (Vars : Project.Variable.Set.Object) return JSON_Array;
         --  Return the set of attribute as a JSON_Array

         function Types
           (Typs : Project.Typ.Set.Object) return JSON_Array;
         --  Return the set of types as a JSON_Array

         Typs : Project.Typ.Set.Object := View.Types;
         --  The types used in the project, the variables's types will be added
         --  into this set.

         ----------------
         -- Attributes --
         ----------------

         function Attributes
           (Atts : Project.Attribute.Set.Object) return JSON_Array
         is
            A_Array : JSON_Array;
         begin
            for A of Atts loop
               if not A.Is_From_Config
                 or else Options.Display_Config_Attributes
               then
                  declare
                     Att : constant JSON_Value := Create_Object;
                  begin
                     Set_Field (Att, "name", Image (A.Name.Id.Attr));

                     if A.Has_Index then
                        Set_Field (Att, "index", A.Index.Value);
                     end if;

                     case A.Kind is
                        when Project.Registry.Attribute.Single =>
                           Set_Field (Att, "kind", "single");
                           Set_Field (Att, "value", A.Value.Text);
                        when Project.Registry.Attribute.List =>
                           Set_Field (Att, "kind", "list");

                           declare
                              Values : JSON_Array;
                           begin
                              for V of A.Values loop
                                 Append (Values, Create (V.Text));
                              end loop;

                              Set_Field (Att, "values", Values);
                           end;
                     end case;

                     Append (A_Array, Att);
                  end;
               end if;
            end loop;

            return A_Array;
         end Attributes;

         -----------
         -- Types --
         -----------

         function Types
           (Typs : Project.Typ.Set.Object) return JSON_Array
         is
            T_Array : JSON_Array;
         begin
            for T of Typs loop
               declare
                  Typ : constant JSON_Value := Create_Object;
               begin
                  Set_Field (Typ, "name", String (T.Name.Text));

                  declare
                     Values : JSON_Array;
                  begin
                     for V of T.Values loop
                        Append (Values, Create (V.Text));
                     end loop;

                     Set_Field (Typ, "values", Values);
                  end;

                  Append (T_Array, Typ);
               end;
            end loop;

            return T_Array;
         end Types;

         ---------------
         -- Variables --
         ---------------

         function Variables
           (Vars : Project.Variable.Set.Object) return JSON_Array
         is
            V_Array : JSON_Array;
         begin
            for V of Vars loop
               declare
                  Var : constant JSON_Value := Create_Object;
               begin
                  Set_Field (Var, "name", String (V.Name.Text));

                  if V.Has_Type then
                     Set_Field (Var, "type", String (V.Typ.Name.Text));

                     Typs.Include (V.Typ.Name.Text, V.Typ);
                  end if;

                  case V.Kind is
                     when Project.Registry.Attribute.Single =>
                        Set_Field (Var, "kind", "single");
                        Set_Field (Var, "value", V.Value.Text);
                     when Project.Registry.Attribute.List =>
                        Set_Field (Var, "kind", "list");

                        declare
                           Values : JSON_Array;
                        begin
                           for T of V.Values loop
                              Append (Values, Create (T.Text));
                           end loop;

                           Set_Field (Var, "values", Values);
                        end;
                  end case;

                  Append (V_Array, Var);
               end;
            end loop;

            return V_Array;
         end Variables;

         Prj     : constant JSON_Value := Create_Object;
         F_Prj   : constant JSON_Value := Create_Object;
         C_Array : JSON_Array;
         P_Array : JSON_Array;
         A_Array : JSON_Array;

      begin
         if Handled.Contains (View) then
            return;
         end if;

         Handled.Include (View);

         --  Global project information

         Set_Field (Prj, "id", View_Id (View));
         Set_Field (Prj, "name", String (View.Name));
         Set_Field (Prj, "kind", Image (View.Kind));
         Set_Field (Prj, "qualifier", Image (View.Qualifier));
         Set_Field (Prj, "simple-name", String (View.Path_Name.Simple_Name));
         Set_Field (Prj, "file-name", View.Path_Name.Value);
         Set_Field (Prj, "directory", View.Path_Name.Dir_Name);

         if View.Kind in With_Object_Dir_Kind then
            Set_Field
              (Prj, "object-directory", View.Object_Directory.Dir_Name);

            O_Array.Include (View.Object_Directory.Dir_Name);

            if View.Kind in With_Source_Dirs_Kind then
               declare
                  Src_Array : GPR2.Containers.Value_Set;
               begin
                  for S of View.Source_Directories loop
                     S_Array.Include (S.Value);
                     Src_Array.Include (S.Value);
                  end loop;

                  Set_Field
                    (Prj, "source-directories", To_JSON_Array (Src_Array));
               end;
            end if;
         end if;

         if View.Is_Library then
            Set_Field (Prj, "library-file-name",
                       String (View.Library_Filename.Value));
            Set_Field (Prj, "library-name", String (View.Library_Name));
            Set_Field (Prj, "library-directory",
                       View.Library_Directory.Dir_Name);
            Set_Field (Prj, "library-ali-directory",
                       View.Library_Ali_Directory.Dir_Name);
         end if;

         Set_Field (F_Prj, "project", Prj);

         --  Extending

         if View.Is_Extended then
            declare
               E : constant JSON_Value := Create_Object;
            begin
               Set_Field (E, "extending-all", Create (View.Is_Extending_All));
               Set_Field (E, "project-id", Create (View_Id (View.Extending)));

               Set_Field (F_Prj, "extending", E);
            end;
         end if;

         --  Extended

         if View.Is_Extending then
            declare
               E : JSON_Array;
            begin
               for V of View.Extended loop
                  Append (E, Create (View_Id (V)));
               end loop;

               Set_Field (F_Prj, "extended", E);
            end;
         end if;

         --  Imported by

         if Parent.Is_Defined then
            Append (P_Array, Create (View_Id (Parent)));

            Set_Field (F_Prj, "imported-by", P_Array);
         end if;

         --  Imported

         if View.Has_Imports then
            for I of View.Imports loop
               Append (C_Array, Create (View_Id (I)));

               if Options.All_Projects then
                  Parse_Project (Prjs, I, View);
               end if;
            end loop;

            Set_Field (F_Prj, "imports", C_Array);
         end if;

         --  Aggregated

         if View.Qualifier in Aggregate_Kind then
            for A of View.Aggregated (Recursive => False) loop
               Append (A_Array, Create (View_Id (A)));

               if Options.All_Projects then
                  Parse_Project (Prjs, A, View);
               end if;
            end loop;

            Set_Field (F_Prj, "aggregated", A_Array);
         end if;

         --  Package

         if Options.Display_Packages or else Options.Display_Everything then
            declare
               P_Array : JSON_Array := Empty_Array;
            begin
               for P of View.Packages (False, False) loop
                  declare
                     Pck  : constant JSON_Value := Create_Object;
                  begin
                     Set_Field (Pck, "name", Image (P));

                     if Options.Display_Attributes
                       or else Options.Display_Everything
                     then
                        declare
                           Atts : constant Project.Attribute.Set.Object :=
                             View.Attributes (P);
                        begin
                           if not Atts.Is_Empty then
                              Set_Field (Pck, "attributes", Attributes (Atts));
                           end if;
                        end;
                     end if;

                     Append (P_Array, Pck);
                  end;
               end loop;

               if not Is_Empty (P_Array) then
                  Set_Field (F_Prj, "packages", P_Array);
               end if;
            end;
         end if;

         --  Attributes

         if Options.Display_Attributes or else Options.Display_Everything then
            declare
               Atts : constant Project.Attribute.Set.Object := View.Attributes;
            begin
               if not Atts.Is_Empty then
                  Set_Field (F_Prj, "attributes", Attributes (Atts));
               end if;
            end;
         end if;

         --  Variables

         if Options.Display_Variables or else Options.Display_Everything then
            declare
               Vars : constant Project.Variable.Set.Object := View.Variables;
            begin
               if not Vars.Is_Empty then
                  Set_Field (F_Prj, "variables", Variables (Vars));
               end if;
            end;
         end if;

         --  Types

         if Options.Display_Variables or else Options.Display_Everything then
            declare
               Typs : constant Project.Typ.Set.Object := View.Types;
            begin
               if not Typs.Is_Empty then
                  Set_Field (F_Prj, "types", Types (Typs));
               end if;
            end;
         end if;

         Append (Prjs, F_Prj);
      end Parse_Project;

      -------------------
      -- To_JSON_Array --
      -------------------

      function To_JSON_Array
        (A : GPR2.Containers.Value_Set) return JSON_Array
      is
         Res : JSON_Array;
      begin
         for S of A loop
            Append (Res, Create (S));
         end loop;

         return Res;
      end To_JSON_Array;

      -----------------
      -- Tree_Object --
      -----------------

      function Tree_Object return JSON_Value is
         T       : constant JSON_Value := Create_Object;
         R       : constant JSON_Value := Create_Object;
         Stat    : constant JSON_Value := Create_Object;
         P_Array : JSON_Array;
         M_Array : JSON_Array;
      begin
         --  Messages

         if Tree.Has_Messages then
            for C in Tree.Log_Messages.Iterate
              (Information => False,
               Warning     => False,
               Error       => False,
               Lint        => True,
               Read        => False,
               Unread      => True)
            loop
               declare
                  M : constant Message.Object := GPR2.Log.Element (C);
               begin
                  Append (M_Array, Create (M.Format));
               end;
            end loop;

            Set_Field (T, "messages", M_Array);
         end if;

         --  Some stats about the tree

         Set_Field (Stat, "project-count", Create (Integer (Handled.Length)));
         Set_Field (T, "stats", Stat);

         --  Project search paths

         for P of Tree.Project_Search_Paths loop
            Append (P_Array, Create (P.Dir_Name));
         end loop;

         Set_Field (T, "project-search-paths", P_Array);

         if Tree.Has_Runtime_Project then
            O_Array.Include (Tree.Runtime_Project.Object_Directory.Dir_Name);

            for S of Tree.Runtime_Project.Source_Directories loop
               S_Array.Include (S.Dir_Name);
            end loop;
         end if;

         --  Object search paths

         if not O_Array.Is_Empty then
            Set_Field (T, "object-search-paths", To_JSON_Array (O_Array));
         end if;

         --  Source search path

         if not S_Array.Is_Empty then
            Set_Field (T, "source-search-paths", To_JSON_Array (S_Array));
         end if;

         --  The root project data

         Set_Field (R, "name", String (Tree.Root_Project.Name));
         Set_Field (R, "id", View_Id (Tree.Root_Project));

         Set_Field (T, "root-project", R);

         return T;
      end Tree_Object;

   begin
      declare
         P_Array : JSON_Array;
      begin
         Parse_Project (Prjs   => P_Array,
                        View   => Tree.Root_Project,
                        Parent => Project.View.Undefined);
         Set_Field (J_Res, "info", Info_Object);
         Set_Field (J_Res, "tree", Tree_Object);
         Set_Field (J_Res, "projects", P_Array);
      end;

      Text_IO.Put_Line (JSON.Write (J_Res, Compact => Compact));
   end Inspect_Project_JSON_Output;

   ------------------------------------
   -- Inspect_Project_Textual_Output --
   ------------------------------------

   procedure Inspect_Project_Textual_Output (Tree : Project.Tree.Object) is

      procedure Indent (Level : Natural; Item : String);
      procedure Print_Infos;
      procedure Print_Projects;
      procedure Print_Tree;

      function Image
        (V : GPR2.Project.View.Object) return String
      is (String (V.Name) & " (" & View_Id (V) & ")");

      ------------
      -- Indent --
      ------------

      procedure Indent (Level : Natural; Item : String) is
         Prefix : constant String :=
                    (case Level is
                        when 0      => "",
                        when 1      => "* ",
                        when others => (1 .. (Level - 1) * 3 => ' ') & " - ");

      begin
         Text_IO.Put_Line (Prefix & Item);
      end Indent;

      -----------------
      -- Print_Infos --
      -----------------

      procedure Print_Infos is
      begin
         Indent (0, "+--------------------------------------+");
         Indent (0, "|         General Information          |");
         Indent (0, "+--------------------------------------+");
         Indent (1, "Generated on : " &
                   Calendar.Formatting.Image (Calendar.Clock));
         Indent (1, "Version      : " &
                   GPR2.Version.Long_Value);
      end Print_Infos;

      --------------------
      -- Print_Projects --
      --------------------

      procedure Print_Projects is
         First_Attr : Boolean := True;

         procedure Print_Attributes
           (View : GPR2.Project.View.Object;
            Pack : GPR2.Package_Id);

         ----------------------
         -- Print_Attributes --
         ----------------------

         procedure Print_Attributes
           (View : GPR2.Project.View.Object;
            Pack : GPR2.Package_Id) is
         begin
            for Attr of View.Attributes
              (Pack, With_Config => Options.Display_Config_Attributes)
            loop
               if First_Attr then
                  --  Actually has attributes to display, print the
                  --  category
                  Indent (2, "Attributes        : ");
                  First_Attr := False;
               end if;

               Indent (3, Image (Attr.Name.Id) &
                         " [ " & Attr.Kind'Img & " ]");

               if Attr.Has_Index then
                  Indent (4, "Index value : """ &
                            Attr.Index.Value & '"');
               end if;

               if Attr.Kind = PRA.Single  then
                  Indent (4, "Value : """ & Attr.Value.Text & '"');

               elsif Attr.Kind = PRA.List then
                  Indent (4, "Values : ");

                  for V of Attr.Values loop
                     Indent (5, '"' & V.Text & '"');
                  end loop;
               end if;
            end loop;
         end Print_Attributes;

      begin

         Indent (0, "+--------------------------------------+");
         Indent (0, "|         Projects Information         |");
         Indent (0, "+--------------------------------------+");

         for V in Tree.Iterate loop
            declare
               View : constant Project.View.Object := Project.Tree.Element (V);

            begin
               if (Options.All_Projects
                   and then No_View_Restriction
                     (Views => Options.Restricted_Views,
                      VName => View.Name))
                 or else (View_Id (View) = View_Id (Tree.Root_Project))
               then
                  Indent (1, Image (View) & " [ " & Image (View.Kind) & " ]");
                  Indent (2, "Project file      : " & View.Path_Name.Value);
                  Indent (2, "Project directory : " & View.Path_Name.Dir_Name);

                  if View.Kind in With_Object_Dir_Kind then
                     Indent (2, "Object directory  : " &
                               View.Object_Directory.Dir_Name);
                  end if;

                  if View.Kind in With_Source_Dirs_Kind then
                     Indent (2, "Source directory  :");

                     for S of View.Source_Directories loop
                        Indent (3, S.Value);
                     end loop;
                  end if;

                  if View.Is_Library then
                     Indent (2, "Library name      : " &
                               String (View.Library_Name));
                     Indent (2, "Library file      : " &
                               String (View.Library_Filename.Value));
                     Indent (2, "Library directory : " &
                               View.Library_Directory.Dir_Name);
                     Indent (2, "Library ALI dir.  : " &
                               View.Library_Ali_Directory.Dir_Name);
                  end if;

                  if View.Is_Extended then
                     Indent (2, "Extended by       : " &
                               Image (View.Extending));
                  end if;

                  if View.Is_Extending then
                     Indent (2, "Extends           : ");

                     for Extended_V of View.Extended loop
                        Indent (3, Image (Extended_V));
                     end loop;
                  end if;

                  declare
                     First_Import : Boolean := True;
                  begin
                     for I_V in Tree.Iterate loop
                        declare
                           I_View : constant Project.View.Object :=
                                      Project.Tree.Element (I_V);

                        begin
                           if I_View.Id /= View.Id
                             and then I_View.Has_Imports
                           then
                              for I of I_View.Imports loop
                                 if I.Id = View.Id then
                                    if First_Import then
                                       Indent (2, "Imported-by        : ");
                                       First_Import := False;
                                    end if;

                                    Indent (3, Image (I_View));
                                 end if;
                              end loop;
                           end if;
                        end;
                     end loop;
                  end;

                  if View.Has_Imports then
                     Indent (2, "Imports           : ");

                     for I of View.Imports loop
                        Indent (3, Image (I));
                     end loop;
                  end if;

                  if View.Qualifier in Aggregate_Kind then
                     Indent (2, "Aggregated        : ");

                     for A of View.Aggregated (Recursive => False) loop
                        Indent (3, Image (A));
                     end loop;
                  end if;

                  if Options.Display_Attributes
                    or else Options.Display_Everything
                  then
                     First_Attr := True;

                     Print_Attributes (View, Project_Level_Scope);

                     if Options.Display_Packages
                       or else Options.Display_Everything
                     then
                        for P of View.Packages
                          (With_Defaults => False,
                           With_Config   => Options.Display_Config_Attributes)
                        loop
                           Print_Attributes (View, P);
                        end loop;
                     end if;
                  end if;

                  if Options.Display_Variables
                    or else Options.Display_Everything
                  then
                     if not View.Variables.Is_Empty then
                        Indent (2, "Variables      : ");

                        for Var of View.Variables loop
                           Indent (3, String (Var.Name.Text) &
                                     " [ " & Var.Kind'Img & " ]");

                           if Var.Has_Type then
                              Indent (4, "Variable type : """ &
                                        String (Var.Typ.Name.Text) & '"');
                           end if;

                           if Var.Kind = PRA.Single then
                              Indent (4, "Value : """ & Var.Value.Text & '"');

                           elsif Var.Kind = PRA.List then
                              Indent (4, "Values : ");

                              for V of Var.Values loop
                                 Indent (5, '"' & V.Text & '"');
                              end loop;
                           end if;
                        end loop;
                     end if;
                  end if;

                  if Options.Display_Variables
                    or else Options.Display_Everything
                  then
                     if not View.Types.Is_Empty then
                        Indent (2, "Types          : ");

                        for T of View.Types loop
                           Indent (3, String (T.Name.Text));
                           Indent (4, "Values : ");

                           for V of T.Values loop
                              Indent (5, '"' & V.Text & '"');
                           end loop;
                        end loop;
                     end if;
                  end if;

                  Text_IO.New_Line;
               end if;
            end;
         end loop;
      end Print_Projects;

      ----------------
      -- Print_Tree --
      ----------------

      procedure Print_Tree is
      begin
         Indent (0, "+--------------------------------------+");
         Indent (0, "|       Project Tree Information       |");
         Indent (0, "+--------------------------------------+");

         if Tree.Has_Messages then
            declare
               First_Message : Boolean := False;
            begin
               for C in Project_Tree.Log_Messages.Iterate
                 (Information => False,
                  Warning     => False,
                  Error       => False,
                  Lint        => True,
                  Read        => False,
                  Unread      => True)
               loop
                  if not First_Message then
                     Indent (1, "Messages :");
                     First_Message := True;
                  end if;

                  declare
                     M : constant Message.Object := GPR2.Log.Element (C);
                  begin
                     Indent (2, M.Format);
                  end;
               end loop;
            end;
         end if;

         declare
            Project_Count : Integer := 0;
         begin
            for V in Tree.Iterate loop
               declare
                  View : constant Project.View.Object :=
                           Project.Tree.Element (V);
               begin
                  if (Options.All_Projects
                      and then No_View_Restriction
                        (Views => Options.Restricted_Views,
                         VName => View.Name))
                    or else (View_Id (View) = View_Id (Tree.Root_Project))
                  then
                     Project_Count := Project_Count + 1;
                  end if;
               end;
            end loop;

            Indent (1, "Project count        : " & Project_Count'Img);
         end;

         declare
            First_PPath : Boolean := False;
         begin
            for P of Tree.Project_Search_Paths loop
               if not First_PPath then
                  Indent (1, "Project search paths :");
                  First_PPath := True;
               end if;

               Indent (2, P.Dir_Name);
            end loop;
         end;

         declare
            First_SPath : Boolean := False;
         begin
            if Tree.Has_Runtime_Project then
               Indent (1, "Object search paths  :");

               for V in Tree.Iterate loop
                  declare
                     View : constant Project.View.Object :=
                              Project.Tree.Element (V);

                  begin
                     if (Options.All_Projects
                         and then No_View_Restriction
                           (Views => Options.Restricted_Views,
                            VName => View.Name))
                       or else (View_Id (View) = View_Id (Tree.Root_Project))
                     then
                        if View.Kind in With_Object_Dir_Kind then
                           Indent (2, View.Object_Directory.Dir_Name);
                        end if;
                     end if;
                  end;
               end loop;

               Indent (2, Tree.Runtime_Project.Object_Directory.Dir_Name);

               for V in Tree.Iterate loop
                  declare
                     View : constant Project.View.Object :=
                              Project.Tree.Element (V);
                  begin
                     if (Options.All_Projects
                         and then No_View_Restriction
                           (Views => Options.Restricted_Views,
                            VName => View.Name))
                       or else (View_Id (View) = View_Id (Tree.Root_Project))
                     then
                        if View.Kind in With_Source_Dirs_Kind then
                           for S of View.Source_Directories loop
                              if not First_SPath then
                                 Indent (1, "Source search paths  :");
                                 First_SPath := True;
                              end if;

                              Indent (2, S.Value);
                           end loop;
                        end if;
                     end if;
                  end;
               end loop;

               for S of Tree.Runtime_Project.Source_Directories loop
                  if not First_SPath then
                     Indent (1, "Source search paths  :");
                     First_SPath := True;
                  end if;

                  Indent (2, S.Dir_Name);
               end loop;
            end if;
         end;

         Indent (1, "Root project :");
         Indent (2, Image (Tree.Root_Project));
      end Print_Tree;

      pragma Unreferenced (Tree);

   begin
      Print_Infos;
      Text_IO.New_Line;
      Print_Tree;
      Text_IO.New_Line;
      Print_Projects;
   end Inspect_Project_Textual_Output;

   ------------------
   -- Load_Project --
   ------------------

   procedure Load_Project
     (Tree    : in out GPR2.Project.Tree.Object;
      Options : in out GPRinspect.GPRinspect_Options)
   is
      package Imported_By_Map is new
        Ada.Containers.Ordered_Maps
          (Project.View.Object,
           Project.View.Set.Object,
           Project.View."<",
           Project.View.Set."=");

      Imported_By  : Imported_By_Map.Map;
   begin
      Options.Tree := Project_Tree.Reference;

      if not
        GPRtools.Options.Load_Project
          (Opt                => Options,
           Absent_Dir_Error   => Project.Tree.No_Error,
           Handle_Information => Options.Verbose)
      then
         Command_Line.Set_Exit_Status (Command_Line.Failure);
         return;
      end if;

      --  Build list of imported-by projects

      for C in Tree.Iterate loop
         declare
            V : constant Project.View.Object := Project.Tree.Element (C);
         begin
            for I of V.Imports loop
               declare
                  S : constant Imported_By_Map.Cursor := Imported_By.Find (I);
               begin
                  if Imported_By_Map.Has_Element (S) then
                     Imported_By.Reference (S).Insert (V);
                  else
                     declare
                        N : Project.View.Set.Object;
                     begin
                        N.Include (V);
                        Imported_By.Insert (I, N);
                     end;
                  end if;
               end;
            end loop;
         end;
      end loop;
   end Load_Project;

   ----------------------
   -- View_Restriction --
   ----------------------

   function No_View_Restriction
     (Views : Restricted_Scope;
      VName : Name_Type) return Boolean is
   begin
      return (not Views.Restrict or else (Views.Views.Contains (VName)));
   end No_View_Restriction;

   -------------
   -- View_Id --
   -------------

   function View_Id (View : Project.View.Object) return String is
   begin
      return String (GPR2.View_Ids.Image (View.Id));
   end View_Id;

begin
   Load_Project (Tree    => Project_Tree,
                 Options => Options);

   case Options.Kind_Of_Display is
      when GPRtools.K_JSON | GPRtools.K_JSON_Compact =>
         Inspect_Project_JSON_Output
           (Tree    => Project_Tree,
            Compact => (Options.Kind_Of_Display = GPRtools.K_JSON_Compact));

      when GPRtools.K_Textual_IO =>
         Inspect_Project_Textual_Output (Tree => Project_Tree);
   end case;

exception
   when E : others =>
      Text_IO.Put_Line ("error: " & Exception_Information (E));
      if Options.Tree /= null
      then
         if Options.Tree.Has_Messages then
            for M of Options.Tree.Log_Messages.all loop
               Text_IO.Put_Line (M.Format);
            end loop;
         end if;
      end if;
end GPRinspect.Process;