Skip to content

Commit 915bc57

Browse files
committed
C instrumentation: fix computation of builtin macros
The selected language standard (-std=c89, -std=c++20, ...) has an influence over the builtin macros. This commit adds the necessary infrastructure to detect the language standard to use. Change-Id: I99480dddb2cf4468255bc29551136738f1f58647 TN: V816-018
1 parent a94c723 commit 915bc57

File tree

2 files changed

+82
-23
lines changed

2 files changed

+82
-23
lines changed

tools/gnatcov/instrument-c.adb

+74-22
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
-- of the license. --
1717
------------------------------------------------------------------------------
1818

19+
with Ada.Characters.Handling;
1920
with Ada.Containers; use Ada.Containers;
2021
with Ada.Containers.Hashed_Maps;
2122
with Ada.Directories; use Ada.Directories;
@@ -125,9 +126,15 @@ package body Instrument.C is
125126
-- when we will refine what is done with macros.
126127

127128
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.
131138

132139
procedure Preprocess_Source
133140
(Filename : String;
@@ -2279,11 +2286,14 @@ package body Instrument.C is
22792286
--------------------
22802287

22812288
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
22832290
is
2291+
use Ada.Characters.Handling;
22842292
use Compiler_Macros_Maps;
22852293

2286-
Key : constant Unbounded_String := +Compiler;
2294+
L : constant String := To_Lower (Lang);
2295+
Key : constant Unbounded_String :=
2296+
+Compiler & " -x " & L & " " & Std;
22872297
Cur : constant Cursor := Compiler_Macros.Find (Key);
22882298
Result : Macro_Vector_Access;
22892299
begin
@@ -2305,6 +2315,11 @@ package body Instrument.C is
23052315
-- Run the preprocessor on an empty file and write the
23062316
-- preprocessed sources to Filename.
23072317

2318+
Args.Append (+"-x");
2319+
Args.Append (+L);
2320+
if Std'Length /= 0 then
2321+
Args.Append (+Std);
2322+
end if;
23082323
Args.Append (+"-E");
23092324
Args.Append (+"-dM");
23102325
Args.Append (+"-");
@@ -2313,7 +2328,7 @@ package body Instrument.C is
23132328
(Command => Compiler,
23142329
Arguments => Args,
23152330
Origin_Command_Name =>
2316-
"getting built-in macros for " & Compiler,
2331+
"getting built-in macros for " & (+Key),
23172332
Output_File => Filename,
23182333
In_To_Null => True);
23192334

@@ -3791,23 +3806,51 @@ package body Instrument.C is
37913806
-----------------
37923807

37933808
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+
37953833
begin
37963834
for Dir of Options.PP_Search_Path loop
37973835
Args.Append (+"-I");
37983836
Args.Append (Dir);
37993837
end loop;
38003838

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+
38113854
end Add_Options;
38123855

38133856
-------------------------
@@ -3822,10 +3865,6 @@ package body Instrument.C is
38223865
is
38233866
Switches : GNAT.Strings.String_List_Access;
38243867
begin
3825-
Self.PP_Macros :=
3826-
Builtin_Macros
3827-
(Compiler_Driver (Info.Project, Language), +Info.Output_Dir).all;
3828-
38293868
-- Pass the source directories of the project file as -I options
38303869

38313870
for Dir of Info.Project.Source_Dirs loop
@@ -3892,7 +3931,7 @@ package body Instrument.C is
38923931
Option_Name : Character;
38933932
Value : out Unbounded_String) return Boolean
38943933
is
3895-
Prefix : constant String := ('-', Option_Name);
3934+
Prefix : constant String := "-" & Option_Name;
38963935
begin
38973936
if Arg = Prefix then
38983937

@@ -3941,6 +3980,9 @@ package body Instrument.C is
39413980

39423981
elsif Read_With_Argument (A, 'U', Value) then
39433982
Self.PP_Macros.Append ((Define => False, Value => Value));
3983+
3984+
elsif Has_Prefix (A, "-std=") then
3985+
Self.Std := +A;
39443986
end if;
39453987

39463988
I := I + 1;
@@ -4032,6 +4074,16 @@ package body Instrument.C is
40324074
for Args of Switches.Args.String_List_Args (Opt) loop
40334075
Import_From_Args (Self, Split_Args (Args));
40344076
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;
40354087
end Import_Options;
40364088

40374089
end Instrument.C;

tools/gnatcov/instrument-c.ads

+8-1
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,21 @@ package Instrument.C is
237237
PP_Search_Path : String_Vectors.Vector;
238238
-- List of directories to search when looking for an included file
239239

240+
Builtin_Macros : Macro_Vectors.Vector;
241+
-- List of predefined macros for the project compiler driver
242+
240243
PP_Macros : Macro_Vectors.Vector;
241244
-- List of macros for the preprocessor
245+
246+
Std : Unbounded_String;
247+
-- -std=X argument to pass to the preprocessor and the parser, or the
248+
-- empty string.
242249
end record;
243250
-- Options to analyze (preprocess and/or parse) a compilation unit
244251

245252
procedure Add_Options
246253
(Args : in out String_Vectors.Vector; Options : Analysis_Options);
247-
-- Append to Args the command line options correspondig to Options
254+
-- Append to Args the command line options corresponding to Options
248255

249256
procedure Import_From_Project
250257
(Self : out Analysis_Options;

0 commit comments

Comments
 (0)