Skip to content

Commit 981dcc5

Browse files
committed
Merge branch 'topic/489' into 'master'
GNATCheckWorker: make JSON instances object distinct from diagnostics Closes #521 See merge request eng/libadalang/langkit-query-language!525
2 parents 9d99f91 + 9197705 commit 981dcc5

File tree

41 files changed

+641
-156
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+641
-156
lines changed

lkql_checker/doc/gnatcheck_rm/using_gnatcheck.rst

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,9 @@ described in the following paragraph).
462462

463463
By default, GNATcheck will look for a ``rules.lkql`` file besides the specified
464464
project file if any. If one is found and no other rule configuration has been
465-
provided (either through the LKQL --rule-file option, or by the now deprecated
466-
legacy -rules options), GNATcheck will load the rule configuration file as if
467-
it was provided by the --rule-file option.
465+
provided (either through the LKQL ``--rule-file`` option, or by the now
466+
deprecated legacy ``-rules`` options), GNATcheck will load the rule
467+
configuration file as if it was provided by the ``--rule-file`` option.
468468

469469
.. note::
470470

@@ -485,7 +485,7 @@ string, or the list of strings type, as shown in the simple example below:
485485
Forbidden_Attributes: {Forbidden: ["GNAT"], Allowed: ["First", "Last"]}
486486
}
487487

488-
Using the "@" object notation is strongly advised to make your configuration
488+
Using the ``@`` object notation is strongly advised to make your configuration
489489
file way more understandable:
490490

491491
Please read the :ref:`Predefined_Rules` documentation to view examples on how
@@ -524,7 +524,7 @@ to provide parameters to rules through LKQL rule files.
524524
}
525525

526526
Moreover, each instance must be identifiable through a unique name, thus the
527-
following configuration is invalid and will lead to a GNATCheck error:
527+
following configuration is invalid and will lead to a GNATcheck error:
528528

529529
::
530530

@@ -535,6 +535,10 @@ to provide parameters to rules through LKQL rule files.
535535
]
536536
}
537537

538+
However, if two instances have the same name **and** the same parameters,
539+
GNATcheck will just ignore the duplicate instance and emit a warning instead
540+
of an error so that the analysis can nevertheless be executed.
541+
538542
Additionally to the ``rules`` top-level symbol, an LKQL rule file may export
539543
``ada_rules`` and ``spark_rules`` symbols to enable associated rules,
540544
respectively, only on Ada code or only on SPARK code. Those symbols must also
@@ -624,8 +628,10 @@ defined in ``specific_rules.lkql``.
624628
are going to be searched in.
625629

626630
You can enable the same rule in multiple files, but the constraint about the
627-
instance name uniqueness remains valid, meaning that such configuration is
628-
invalid:
631+
instance name uniqueness remains valid: when two instances have the same name,
632+
GNATcheck will emit an error if the instances have different parameters but
633+
ignore duplicate instances that are configured identically. That means such a
634+
configuration is invalid:
629635

630636
.. code-block:: lkql
631637
@@ -649,6 +655,21 @@ invalid:
649655
})
650656
# error: This rule configuration defines two instances with the same name: "Forbid_Attr"
651657
658+
Note that GNATcheck will also detect instances that run the same check (i.e.,
659+
instances that have different names but are configured with the same
660+
parameters). In such cases, GNATcheck will emit a warning so that duplicate
661+
checks can be easily detected when combining rules object.
662+
663+
Same restrictions apply when combining LKQL rules files with rules specified
664+
with the command line interface (using the ``--rule``
665+
:ref:`switch<General_gnatcheck_Switches>`), or through the ``Check``
666+
:ref:`GPR package<Check_GPR_Package>`.
667+
668+
.. attention::
669+
670+
Combining :ref:`LKQL rule file<LKQL_options_file>` with the deprecated
671+
:ref:`GNATcheck Rule Options<gnatcheck_Rule_Options>` is undefined behavior.
672+
652673

653674
.. _gnatcheck_Rule_Options:
654675

lkql_checker/src/gnatcheck-compiler.adb

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ with Gnatcheck.Rules.Rule_Table; use Gnatcheck.Rules.Rule_Table;
2525
with Gnatcheck.Source_Table; use Gnatcheck.Source_Table;
2626
with Gnatcheck.String_Utilities; use Gnatcheck.String_Utilities;
2727

28-
with GNATCOLL.VFS; use GNATCOLL.VFS;
28+
with GNATCOLL.JSON; use GNATCOLL.JSON;
29+
with GNATCOLL.VFS; use GNATCOLL.VFS;
2930

3031
with Langkit_Support.Slocs; use Langkit_Support.Slocs;
3132

@@ -339,6 +340,10 @@ package body Gnatcheck.Compiler is
339340
-- Analyze one line containing a builder output. Insert the relevant
340341
-- messages into gnatcheck diagnoses table.
341342

343+
procedure Process_Worker_Message
344+
(Message : String; Printer : access procedure (S, L : String));
345+
-- Helper to process messages received from the Worker
346+
342347
------------------
343348
-- Analyze_Line --
344349
------------------
@@ -529,6 +534,24 @@ package body Gnatcheck.Compiler is
529534
else Get_Rule_Id (Message_Kind)));
530535
end Analyze_Line;
531536

537+
----------------------------
538+
-- Process_Worker_Message --
539+
----------------------------
540+
541+
procedure Process_Worker_Message
542+
(Message : String; Printer : access procedure (S, L : String))
543+
is
544+
Decoded_Message : constant Read_Result := Read (Message);
545+
begin
546+
if Decoded_Message.Success then
547+
Printer
548+
(Decoded_Message.Value.Get ("message"),
549+
Decoded_Message.Value.Get ("location"));
550+
else
551+
Printer (Message, "");
552+
end if;
553+
end Process_Worker_Message;
554+
532555
-- Start of processing for Analyze_Output
533556

534557
begin
@@ -600,12 +623,25 @@ package body Gnatcheck.Compiler is
600623
end if;
601624
end;
602625
end if;
626+
elsif Line_Len >= 16 and then Line (1 .. 13) = "WORKER_INFO: " then
627+
Process_Worker_Message (Line (14 .. Line_Len), Info'Access);
603628
elsif Line_Len >= 16 and then Line (1 .. 16) = "WORKER_WARNING: " then
604-
Warning (Line (17 .. Line_Len));
629+
Process_Worker_Message (Line (17 .. Line_Len), Warning'Access);
605630
elsif Line_Len >= 14 and then Line (1 .. 14) = "WORKER_ERROR: " then
606-
Error (Line (15 .. Line_Len));
631+
Process_Worker_Message (Line (15 .. Line_Len), Error'Access);
607632
Detected_Internal_Error := @ + 1;
608633
Errors := True;
634+
elsif Line_Len >= 23
635+
and then Line (1 .. 23) = "WORKER_JSON_INSTANCES: "
636+
then
637+
-- Ignore the JSON instances data. This is analyzed in
638+
-- Process_LKQL_Rule_File.
639+
640+
-- The current line can be longer than Line_Len, skip the rest of
641+
-- the line if that's the case.
642+
if Line_Len = Line'Length then
643+
Skip_Line (File);
644+
end if;
609645
else
610646
Analyze_Line (Line (1 .. Line_Len));
611647
end if;
@@ -1849,6 +1885,11 @@ package body Gnatcheck.Compiler is
18491885
Num_Args := @ + 1;
18501886
Args (Num_Args) := new String'(LKQL_RF_Name);
18511887

1888+
if Verbose_Mode then
1889+
Num_Args := @ + 1;
1890+
Args (Num_Args) := new String'("--verbose");
1891+
end if;
1892+
18521893
-- Output the called command if in debug mode
18531894
if Arg.Debug_Mode.Get then
18541895
Put (Base_Name (Worker.all));

lkql_checker/src/gnatcheck-output.adb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,13 @@ package body Gnatcheck.Output is
228228
-- Info --
229229
----------
230230

231-
procedure Info (Message : String) is
231+
procedure Info (Message : String; Location : String := "") is
232232
begin
233233
Emit_Message
234234
(Message,
235235
Tag => Info,
236-
Tool_Name => True,
236+
Tool_Name => Location = "",
237+
Location => Location,
237238
New_Line => True,
238239
Log_Message => True);
239240
end Info;

lkql_checker/src/gnatcheck-output.ads

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ package Gnatcheck.Output is
4949
procedure Warning (Message : String; Location : String := "");
5050
-- Sends ``Message`` into stderr, prefixed by "tool_name: warning: ".
5151

52-
procedure Info (Message : String);
52+
procedure Info (Message : String; Location : String := "");
5353
-- Sends ``Message`` into stderr, prefixed by "tool_name: info: ".
5454

5555
procedure Info_In_Tty (Message : String);

lkql_checker/src/gnatcheck-rules-rule_table.adb

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,7 @@ package body Gnatcheck.Rules.Rule_Table is
922922
Parser_Pid : Process_Id;
923923
Waited_Pid : Process_Id;
924924
Success : Boolean;
925+
Analyze_Error : Boolean;
925926
Config_JSON : Read_Result;
926927

927928
procedure Rule_Object_Mapper
@@ -952,12 +953,35 @@ package body Gnatcheck.Rules.Rule_Table is
952953
Error ("can not call the LKQL rule file parser");
953954
Rule_Option_Problem_Detected := True;
954955
else
955-
Config_JSON := Read (Read_File (JSON_Config_File_Name).all);
956+
declare
957+
Worker_Output_File : File_Type;
958+
begin
959+
Open (Worker_Output_File, In_File, JSON_Config_File_Name);
960+
961+
while not End_Of_File (Worker_Output_File) loop
962+
declare
963+
Line : constant String := Get_Line (Worker_Output_File);
964+
begin
965+
if Line (1 .. 23) = "WORKER_JSON_INSTANCES: " then
966+
Config_JSON := Read (Line (24 .. Line'Last));
967+
exit;
968+
end if;
969+
end;
970+
end loop;
971+
972+
Close (Worker_Output_File);
973+
end;
974+
975+
-- Process diagnostics from worker's output
976+
Analyze_Output (JSON_Config_File_Name, Analyze_Error);
956977

957978
-- If the JSON parsing failed, it means that LKQL rule file
958-
-- processing failed and diagnostics are in the output file.
959-
if not Config_JSON.Success then
960-
Analyze_Output (JSON_Config_File_Name, Success);
979+
-- processing failed and diagnostics are in the output file and
980+
-- they have been already processed.
981+
if Analyze_Error
982+
or else not Config_JSON.Success
983+
or else Config_JSON.Value = JSON_Null
984+
then
961985
Rule_Option_Problem_Detected := True;
962986

963987
-- Else, populate the global rule table with the rule config

lkql_jit/cli/src/main/java/com/adacore/lkql_jit/cli/BaseLKQLChecker.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ protected Map<String, RuleInstance> getRuleInstances() {
173173
ruleName,
174174
Optional.empty(),
175175
RuleInstance.SourceMode.GENERAL,
176-
instanceArgs.get(instanceId)
176+
instanceArgs.get(instanceId),
177+
null
177178
)
178179
);
179180
}

0 commit comments

Comments
 (0)