16
16
-- of the license. --
17
17
-- ----------------------------------------------------------------------------
18
18
19
+ with Ada.Characters.Handling ;
19
20
with Ada.Containers ; use Ada.Containers;
20
21
with Ada.Containers.Hashed_Maps ;
21
22
with Ada.Directories ; use Ada.Directories;
@@ -125,9 +126,15 @@ package body Instrument.C is
125
126
-- when we will refine what is done with macros.
126
127
127
128
function Builtin_Macros
128
- (Compiler, Output_Dir : String) return Macro_Vector_Cst_Access;
129
- -- Return the list of built-in macros for the given compiler. Output_Dir
130
- -- is used to store a temporary file.
129
+ (Lang, Compiler, Std, Output_Dir : String) return Macro_Vector_Cst_Access;
130
+ -- Return the list of built-in macros for the given compiler, standard and
131
+ -- language. Output_Dir is used to store a temporary file.
132
+ --
133
+ -- Note that we could generate a fully-fledged preprocessor configuration
134
+ -- (the standard macros + the command-line defined macros with an
135
+ -- additional argument there), but it is more convenient to cache the
136
+ -- "light" preprocessor configuration that is determined by the compiler,
137
+ -- language and standard only.
131
138
132
139
procedure Preprocess_Source
133
140
(Filename : String;
@@ -2279,11 +2286,14 @@ package body Instrument.C is
2279
2286
-- ------------------
2280
2287
2281
2288
function Builtin_Macros
2282
- (Compiler, Output_Dir : String) return Macro_Vector_Cst_Access
2289
+ (Lang, Compiler, Std , Output_Dir : String) return Macro_Vector_Cst_Access
2283
2290
is
2291
+ use Ada.Characters.Handling;
2284
2292
use Compiler_Macros_Maps;
2285
2293
2286
- Key : constant Unbounded_String := +Compiler;
2294
+ L : constant String := To_Lower (Lang);
2295
+ Key : constant Unbounded_String :=
2296
+ +Compiler & " -x " & L & " " & Std;
2287
2297
Cur : constant Cursor := Compiler_Macros.Find (Key);
2288
2298
Result : Macro_Vector_Access;
2289
2299
begin
@@ -2305,6 +2315,11 @@ package body Instrument.C is
2305
2315
-- Run the preprocessor on an empty file and write the
2306
2316
-- preprocessed sources to Filename.
2307
2317
2318
+ Args.Append (+" -x" );
2319
+ Args.Append (+L);
2320
+ if Std'Length /= 0 then
2321
+ Args.Append (+Std);
2322
+ end if ;
2308
2323
Args.Append (+" -E" );
2309
2324
Args.Append (+" -dM" );
2310
2325
Args.Append (+" -" );
@@ -2313,7 +2328,7 @@ package body Instrument.C is
2313
2328
(Command => Compiler,
2314
2329
Arguments => Args,
2315
2330
Origin_Command_Name =>
2316
- " getting built-in macros for " & Compiler ,
2331
+ " getting built-in macros for " & (+Key) ,
2317
2332
Output_File => Filename,
2318
2333
In_To_Null => True);
2319
2334
@@ -3791,23 +3806,51 @@ package body Instrument.C is
3791
3806
-- ---------------
3792
3807
3793
3808
procedure Add_Options
3794
- (Args : in out String_Vectors.Vector; Options : Analysis_Options) is
3809
+ (Args : in out String_Vectors.Vector; Options : Analysis_Options)
3810
+ is
3811
+
3812
+ procedure Add_Macro_Switches (Macros : Macro_Vectors.Vector);
3813
+ -- Add the given macro switches to Args
3814
+
3815
+ -- ----------------------
3816
+ -- Add_Macro_Switches --
3817
+ -- ----------------------
3818
+
3819
+ procedure Add_Macro_Switches (Macros : Macro_Vectors.Vector) is
3820
+ begin
3821
+ for M of Macros loop
3822
+ declare
3823
+ Prefix : constant Unbounded_String :=
3824
+ +(if M.Define
3825
+ then " -D"
3826
+ else " -U" );
3827
+ begin
3828
+ Args.Append (Prefix & M.Value);
3829
+ end ;
3830
+ end loop ;
3831
+ end Add_Macro_Switches ;
3832
+
3795
3833
begin
3796
3834
for Dir of Options.PP_Search_Path loop
3797
3835
Args.Append (+" -I" );
3798
3836
Args.Append (Dir);
3799
3837
end loop ;
3800
3838
3801
- for M of Options.PP_Macros loop
3802
- declare
3803
- Prefix : constant Unbounded_String :=
3804
- +(if M.Define
3805
- then " -D"
3806
- else " -U" );
3807
- begin
3808
- Args.Append (Prefix & M.Value);
3809
- end ;
3810
- end loop ;
3839
+ -- Add builtin macros before macros from command line switches, as the
3840
+ -- latter should have precedence over builtins and thus must come last
3841
+ -- in Args.
3842
+
3843
+ Add_Macro_Switches (Options.Builtin_Macros);
3844
+ Add_Macro_Switches (Options.PP_Macros);
3845
+
3846
+ -- The -std switch also indicates the C/C++ version used, and
3847
+ -- influences both the configuration of the preprocessor, and the
3848
+ -- parsing of the file.
3849
+
3850
+ if Length (Options.Std) /= 0 then
3851
+ Args.Append (Options.Std);
3852
+ end if ;
3853
+
3811
3854
end Add_Options ;
3812
3855
3813
3856
-- -----------------------
@@ -3822,10 +3865,6 @@ package body Instrument.C is
3822
3865
is
3823
3866
Switches : GNAT.Strings.String_List_Access;
3824
3867
begin
3825
- Self.PP_Macros :=
3826
- Builtin_Macros
3827
- (Compiler_Driver (Info.Project, Language), +Info.Output_Dir).all ;
3828
-
3829
3868
-- Pass the source directories of the project file as -I options
3830
3869
3831
3870
for Dir of Info.Project.Source_Dirs loop
@@ -3892,7 +3931,7 @@ package body Instrument.C is
3892
3931
Option_Name : Character;
3893
3932
Value : out Unbounded_String) return Boolean
3894
3933
is
3895
- Prefix : constant String := ( ' - ' , Option_Name) ;
3934
+ Prefix : constant String := " - " & Option_Name;
3896
3935
begin
3897
3936
if Arg = Prefix then
3898
3937
@@ -3941,6 +3980,9 @@ package body Instrument.C is
3941
3980
3942
3981
elsif Read_With_Argument (A, ' U' , Value) then
3943
3982
Self.PP_Macros.Append ((Define => False, Value => Value));
3983
+
3984
+ elsif Has_Prefix (A, " -std=" ) then
3985
+ Self.Std := +A;
3944
3986
end if ;
3945
3987
3946
3988
I := I + 1 ;
@@ -4032,6 +4074,16 @@ package body Instrument.C is
4032
4074
for Args of Switches.Args.String_List_Args (Opt) loop
4033
4075
Import_From_Args (Self, Split_Args (Args));
4034
4076
end loop ;
4077
+
4078
+ -- Now, we can generate the preprocessor configuration (i.e. the set
4079
+ -- of predefined macros).
4080
+
4081
+ Self.Builtin_Macros :=
4082
+ Builtin_Macros
4083
+ (Image (Language),
4084
+ Compiler_Driver (Info.Project, Language),
4085
+ +Self.Std,
4086
+ +Info.Output_Dir).all ;
4035
4087
end Import_Options ;
4036
4088
4037
4089
end Instrument.C ;
0 commit comments