Skip to content

Commit 46e5ab5

Browse files
committed
Merge branch 'topic/deprecate_rules' into 'master'
Emit a depreciation message when using "-rules" section in a TTY Closes #275 See merge request eng/libadalang/langkit-query-language!315
2 parents f72fba3 + 4b0ee86 commit 46e5ab5

File tree

11 files changed

+120
-22
lines changed

11 files changed

+120
-22
lines changed

lkql_checker/src/gnatcheck-options.ads

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,9 @@ package Gnatcheck.Options is
193193
-- Whether the help message about the new instance system has already been
194194
-- emitted. This message should be removed in 26.0.
195195

196-
Mixed_Style_Warning_Emitted : Boolean := False;
197-
-- Whether the message about usage of and new and legacy rule options for
198-
-- the same GNATcheck run.
196+
Rules_Depreciation_Emitted : Boolean := False;
197+
-- Whether the message about the ``-rules`` section depreciation has been
198+
-- emitted in the TTY.
199199

200200
--------------------------------------
201201
-- Controlling the gnatcheck report --

lkql_checker/src/gnatcheck-output.adb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ with GNAT.Traceback.Symbolic;
1616
with Gnatcheck.Options; use Gnatcheck.Options;
1717
with Gnatcheck.String_Utilities; use Gnatcheck.String_Utilities;
1818

19+
with Interfaces.C_Streams; use Interfaces.C_Streams;
20+
1921
package body Gnatcheck.Output is
2022

2123
Report_File_Name : String_Access;
@@ -121,6 +123,17 @@ package body Gnatcheck.Output is
121123
end if;
122124
end Error_No_Tool_Name;
123125

126+
------------------
127+
-- Error_In_Tty --
128+
------------------
129+
130+
procedure Error_In_Tty (Message : String) is
131+
begin
132+
if isatty (fileno (stderr)) /= 0 then
133+
Put_Line (Standard_Error, Executable & ": " & Message);
134+
end if;
135+
end Error_In_Tty;
136+
124137
-----------------------
125138
-- Get_Indent_String --
126139
-----------------------

lkql_checker/src/gnatcheck-output.ads

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ package Gnatcheck.Output is
4343
procedure Error_No_Tool_Name (Message : String);
4444
-- Sends into Stderr the error message with no tool name prefix
4545

46+
procedure Error_In_Tty (Message : String);
47+
-- Same as ``Error`` but send the message only if Stderr is a TTY. Also,
48+
-- ``Message`` is not added to the stderr log file.
49+
4650
procedure SLOC_Error
4751
(Message : String;
4852
SLOC : String);

lkql_checker/src/gnatcheck-projects.adb

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,16 +1109,14 @@ package body Gnatcheck.Projects is
11091109
Add_Rule_Option (Full_Switch (Parser => Parser));
11101110
Individual_Rules_Set := True;
11111111
end case;
1112-
if not Mixed_Style_Warning_Emitted then
1113-
if Arg.Rules.Get'Length > 0
1114-
or else Arg.Rule_File.Get /= Null_Unbounded_String
1115-
then
1116-
Warning
1117-
("The '-rules' section usage is discouraged from now, " &
1118-
"you should only use the '--rules' and '--rule-file' " &
1119-
"command-line options");
1120-
end if;
1121-
Mixed_Style_Warning_Emitted := True;
1112+
if not Rules_Depreciation_Emitted then
1113+
Error_In_Tty
1114+
("The '-rules' section is now deprecated. You should only " &
1115+
"use the '--rules' and '--rule-file' command-line options.");
1116+
Error_In_Tty
1117+
("You can use the '--emit-lkql-rule-file' flag to " &
1118+
"automatically translate your rule configuration to the " &
1119+
"new LKQL format.");
11221120
end if;
11231121
end loop;
11241122
end Process_Sections;

testsuite/drivers/base_driver.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import errno
12
import glob
23
import os
34
import os.path as P
5+
import pty
46
import re
7+
import select
8+
import subprocess
59
import sys
610
from typing import TextIO
711

@@ -271,6 +275,60 @@ def run(*prefix):
271275
else:
272276
raise TestAbortWithError(f"invalid perf mode: {mode}")
273277

278+
def run_in_tty(self, args: list[str], **kwargs) -> tuple[str, int]:
279+
"""
280+
Run a process in a pseudo-TTY using the ``pty`` module. Returns a
281+
tuple containing the process output and its status code.
282+
"""
283+
# Ensure the current system is not windows, according to the
284+
# documentation, the ``pty`` module is not working fine on it (see
285+
# https://docs.python.org/fr/3/library/pty.html).
286+
if self.env.build.os.name == "windows":
287+
raise TestAbortWithError(
288+
"Cannot run a pseudo-TTY on Windows systems"
289+
)
290+
291+
# Ensure the process is run in the testsuite working dir
292+
if not kwargs.get("cwd"):
293+
kwargs["cwd"] = self.working_dir()
294+
295+
# Open a subprocess with using a pseudo TTY as output
296+
m, s = pty.openpty()
297+
p = subprocess.Popen(
298+
args=args,
299+
stdout=s,
300+
stderr=s,
301+
close_fds=True,
302+
**kwargs
303+
)
304+
os.close(s)
305+
306+
# Read result of the process execution and get its return code
307+
fully_read = False
308+
output = b""
309+
status_code = 0
310+
try:
311+
while not fully_read:
312+
ready, _, _ = select.select([m], [], [], 0.05)
313+
for fd in ready:
314+
try:
315+
data = os.read(fd, 512)
316+
except OSError as e:
317+
if e.errno != errno.EIO:
318+
raise e
319+
fully_read = True
320+
else:
321+
if not data:
322+
fully_read = True
323+
output += data
324+
finally:
325+
os.close(m)
326+
if p.poll() is None:
327+
p.kill()
328+
status_code = p.wait()
329+
330+
return (output.decode(), status_code)
331+
274332
def parse_flagged_lines(self, output: str) -> Flags:
275333
"""
276334
Parse the output of a call and return an object containing files

testsuite/drivers/gnatcheck_driver.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import os
22
import os.path as P
33
import re
4-
import sys
54
import xml.etree.ElementTree as ET
65

76
from e3.testsuite.driver.diff import (
@@ -130,6 +129,8 @@ class GnatcheckDriver(BaseDriver):
130129
- ``worker``: Provide a custom worker for the GNATcheck run.
131130
- ``gnatkp_autoconfig`` (bool): Whether to automatically configure the
132131
target and runtime when running in "gnatkp" mode. Default is True.
132+
- ``in_tty`` (bool): Whether to run GNATcheck in a pseudo TTY using the
133+
``pty`` Python module.
133134
134135
- ``jobs`` (int): The number of jobs to forward to the GNATcheck command.
135136
- ``project`` (str): GPR build file to use (if any).
@@ -404,10 +405,17 @@ def run_one_test(test_data: dict[str, any]) -> None:
404405
if label:
405406
self.output += label + "\n" + ("=" * len(label)) + "\n\n"
406407

407-
p = self.shell(args, env=gnatcheck_env, catch_error=False, analyze_output=False)
408-
409-
# Get the GNATcheck execution output
410-
exec_output = p.out
408+
# Execute GNATcheck and get its output
409+
exec_output = ""
410+
status_code = 0
411+
if test_data.get("in_tty"):
412+
exec_output, status_code = self.run_in_tty(args, env=gnatcheck_env)
413+
else:
414+
p = self.shell(args, env=gnatcheck_env, catch_error=False, analyze_output=False)
415+
exec_output = p.out
416+
status_code = p.status
417+
418+
# Then read GNATcheck report file if there is one
411419
report_file_content = ""
412420
parse_output_for_flags = True
413421
if output_format in ['full', 'short', 'xml']:
@@ -451,8 +459,8 @@ def run_one_test(test_data: dict[str, any]) -> None:
451459
self.output += ("testsuite_driver: Cannot found the rule "
452460
f"list file '{test_data['rule_list_file']}'")
453461

454-
if (not brief and p.status not in [0, 1]) or (brief and p.status != 0):
455-
self.output += ">>>program returned status code {}\n".format(p.status)
462+
if (not brief and status_code not in [0, 1]) or (brief and status_code != 0):
463+
self.output += ">>>program returned status code {}\n".format(status_code)
456464

457465
# List the content of directories if needed
458466
if test_data.get('list_dirs'):

testsuite/tests/gnatcheck/lkql_rules_config/valid_rule_config/test.out

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
gnatcheck: The '-rules' section usage is discouraged from now, you should only use the '--rules' and '--rule-file' command-line options
21
ada_code.adb:2:09: Int does not end with type suffix _T [first_convention|identifier_suffixes]
32
ada_code.adb:5:09: Int_A does not end with access suffix _PTR [first_convention|identifier_suffixes]
43
ada_code.adb:6:09: Int_PTR does not end with access suffix _A [other_convention|identifier_suffixes]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
procedure Main is
2+
begin
3+
goto lbl; -- FLAG
4+
<<lbl>>
5+
end Main;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
gnatcheck: The '-rules' section is now deprecated. You should only use the '--rules' and '--rule-file' command-line options.
2+
gnatcheck: You can use the '--emit-lkql-rule-file' flag to automatically translate your rule configuration to the new LKQL format.
3+
main.adb:3:04: goto statement
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
driver: gnatcheck
2+
format: brief
3+
in_tty: True
4+
rules:
5+
- +RGoto_Statements
6+
input_sources:
7+
- main.adb
8+
control:
9+
- [SKIP,
10+
"os == 'windows'",
11+
"Disable this test because 'pty' is not supported on Windows"]

0 commit comments

Comments
 (0)