Skip to content

Commit f1ab880

Browse files
committed
C/C++ instr: consider other compiler switches for preprocessing
We must launch the preprocessing command with all of the switches that can influence the preprocessing of the file. We used to consider macro definition / undefinition and the C/C++ standard set by the -std switch but there are more switches than that (the list is unfortunately probably not exhaustive): -fno-rtti (do not include runtime type information) and all of the warnings switches which can influence the preprocessing through the __has_warning macro.
1 parent 1e381f6 commit f1ab880

File tree

5 files changed

+74
-31
lines changed

5 files changed

+74
-31
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <typeinfo>
2+
3+
int
4+
main (int argc, char **argv)
5+
{
6+
return 0;
7+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
Check that the instrumenter considers the -fno-rtti switch for preprocessing.
3+
"""
4+
5+
from SCOV.minicheck import build_run_and_coverage, check_xcov_reports
6+
from SCOV.instr import xcov_instrument
7+
from SUITE.context import thistest
8+
from SUITE.cutils import Wdir
9+
from SUITE.tutils import gprbuild, gprfor
10+
from SUITE.gprutils import GPRswitches
11+
12+
13+
tmp = Wdir("tmp_")
14+
15+
build_run_and_coverage(
16+
gprsw=GPRswitches(root_project=gprfor(srcdirs=[".."], mains=["main.cpp"])),
17+
covlevel="stmt",
18+
mains=["main"],
19+
extra_instr_args=["--c++-opts=-fno-rtti"],
20+
extra_gprbuild_cargs=["-fno-rtti"],
21+
extra_coverage_args=["-axcov", "--output-dir=xcov"],
22+
trace_mode="src",
23+
)
24+
25+
check_xcov_reports(
26+
"*.xcov",
27+
{
28+
"main.cpp.xcov": {"+": {6}},
29+
},
30+
"xcov",
31+
)
32+
33+
thistest.result()

tools/gnatcov/instrument-c.adb

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,11 @@ package body Instrument.C is
8080
type Macro_Set_Cst_Access is access constant Macro_Set;
8181

8282
function Builtin_Macros
83-
(Lang, Compiler, Std, Output_Dir : String) return Macro_Set_Cst_Access;
84-
-- Return the list of built-in macros for the given compiler, standard and
85-
-- language. Output_Dir is used to store a temporary file.
86-
--
87-
-- Note that we could generate a fully-fledged preprocessor configuration
88-
-- (the standard macros + the command-line defined macros with an
89-
-- additional argument there), but it is more convenient to cache the
90-
-- "light" preprocessor configuration that is determined by the compiler,
91-
-- language and standard only.
83+
(Lang, Compiler, Output_Dir : String;
84+
Compiler_Switches : String_Vectors.Vector) return Macro_Set_Cst_Access;
85+
-- Return the list of built-in macros for the given compiler, language and
86+
-- according to the compiler switches. Output_Dir is used to store a
87+
-- temporary file.
9288

9389
procedure Preprocess_Source
9490
(Filename : String;
@@ -2524,16 +2520,15 @@ package body Instrument.C is
25242520
--------------------
25252521

25262522
function Builtin_Macros
2527-
(Lang, Compiler, Std, Output_Dir : String) return Macro_Set_Cst_Access
2523+
(Lang, Compiler, Output_Dir : String;
2524+
Compiler_Switches : String_Vectors.Vector) return Macro_Set_Cst_Access
25282525
is
25292526
use Ada.Characters.Handling;
25302527

25312528
PID : constant Unsigned_64 :=
25322529
Unsigned_64 (Pid_To_Integer (Current_Process_Id));
25332530

25342531
L : constant String := To_Lower (Lang);
2535-
Key : constant Unbounded_String :=
2536-
+Compiler & " -x " & L & " " & Std;
25372532
Result : constant Macro_Set_Access := new Macro_Set;
25382533

25392534
Args : String_Vectors.Vector;
@@ -2549,18 +2544,15 @@ package body Instrument.C is
25492544

25502545
Args.Append (+"-x");
25512546
Args.Append (+L);
2552-
if Std'Length /= 0 then
2553-
Args.Append (+Std);
2554-
end if;
2547+
Args.Append (Compiler_Switches);
25552548
Args.Append (+"-E");
25562549
Args.Append (+"-dM");
25572550
Args.Append (+"-");
25582551

25592552
Run_Command
25602553
(Command => Compiler,
25612554
Arguments => Args,
2562-
Origin_Command_Name =>
2563-
"getting built-in macros for " & (+Key),
2555+
Origin_Command_Name => "gnatcov instrument",
25642556
Output_File => Filename,
25652557
In_To_Null => True);
25662558

@@ -4532,8 +4524,8 @@ package body Instrument.C is
45324524
Builtin_Macros
45334525
(Image (C_Family_Language (Instrumenter.Language)),
45344526
+Prj.Compiler_Driver (Instrumenter.Language),
4535-
+Self.Std,
4536-
+Prj.Output_Dir).all;
4527+
+Prj.Output_Dir,
4528+
Self.Compiler_Switches).all;
45374529
end Import_Options;
45384530

45394531
---------------------------

tools/gnatcov/instrument-common.adb

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -473,13 +473,13 @@ package body Instrument.Common is
473473
Add_Macro_Switches (Options.PP_Macros);
474474
end if;
475475

476-
-- The -std switch also indicates the C/C++ version used, and
477-
-- influences both the configuration of the preprocessor, and the
478-
-- parsing of the file.
476+
-- Add other compiler switches as they may also influence both the
477+
-- configuration of the preprocessor, and the parsing of the file. A
478+
-- non-exhaustive list includes undefining macros through -U switches,
479+
-- using -std to change the C++ standard in use, -fno-rtti to prevent
480+
-- inclusion of runtime type information etc.
479481

480-
if Length (Options.Std) /= 0 then
481-
Args.Append (Options.Std);
482-
end if;
482+
Args.Append (Options.Compiler_Switches);
483483

484484
end Add_Options;
485485

@@ -638,11 +638,21 @@ package body Instrument.Common is
638638

639639
elsif Read_With_Argument (A, 'U', Value) then
640640
Self.PP_Macros.Include ((Define => False, Name => Value));
641+
-- Account for all the switches that can influence the file
642+
-- preprocessing.
641643

642-
elsif Has_Prefix (A, "-std=") then
643-
Self.Std := +A;
644-
end if;
644+
elsif Has_Prefix (A, "-std")
645+
or else Has_Prefix (A, "-fno-rtti")
646+
or else Has_Prefix (A, "-fno-exceptions")
645647

648+
-- All the warning switches can influence the preprocessing
649+
-- through the use of the __has_warning macro, e.g.
650+
-- #if __has_warning("-Wimplicit-fallthrough")
651+
652+
or else Has_Prefix (A, "-W")
653+
then
654+
Self.Compiler_Switches.Append (+A);
655+
end if;
646656
I := I + 1;
647657
end;
648658
end loop;

tools/gnatcov/instrument-common.ads

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -552,9 +552,10 @@ package Instrument.Common is
552552
PP_Macros : Macro_Set;
553553
-- Set of macros for the preprocessor
554554

555-
Std : Unbounded_String;
556-
-- -std=X argument to pass to the preprocessor and the parser, or the
557-
-- empty string.
555+
Compiler_Switches : String_Vectors.Vector;
556+
-- List of compiler switches that can influence the file preprocessing.
557+
-- The list should be amended alongside our discoveries. It is
558+
-- currently: -std, -fno-exceptions, -fno-rtti, -W* switches.
558559
end record;
559560
-- Options to analyze (preprocess and/or parse) a compilation unit
560561

0 commit comments

Comments
 (0)