From a3445a05d442ed01d62d6e2a80bf5c4253fa88dc Mon Sep 17 00:00:00 2001 From: Pascal Obry Date: Thu, 17 Jan 2008 16:06:56 +0100 Subject: [PATCH] Add new option (-d) to check for single line comment ending. --- regtests/makefile | 2 + regtests/out15.out | 4 ++ src/checks.ads => regtests/pck.ads | 14 ++++--- src/checks.ads | 3 ++ src/languages.adb | 27 +++++++++++++ src/languages.ads | 8 ++++ src/style_checker.adb | 79 ++++++++++++++++++++++++++++++++------ 7 files changed, 121 insertions(+), 16 deletions(-) create mode 100644 regtests/out15.out copy src/checks.ads => regtests/pck.ads (91%) diff --git a/regtests/makefile b/regtests/makefile index e719aee..d4f64f7 100644 --- a/regtests/makefile +++ b/regtests/makefile @@ -21,6 +21,7 @@ run_tests: -cf'Copyright \(C\) 2\d\d\d-2\d\d\d, (Pascal Obry|Qwerty)' \ copyright.ads > out13.res 2>&1 -../style_checker -H script.sh > out14.res 2>&1 + -../style_checker -d -cY pck.ads > out15.res 2>&1 check_results: echo "### regressions follows" @@ -39,6 +40,7 @@ check_results: -diff -wc out12.out out12.res || echo $$? >> out_status.res -diff -wc out13.out out13.res || echo $$? >> out_status.res -diff -wc out14.out out14.res || echo $$? >> out_status.res + -diff -wc out15.out out15.res || echo $$? >> out_status.res test ! -e out_status.res clean: diff --git a/regtests/out15.out b/regtests/out15.out new file mode 100644 index 0000000..ff09fda --- /dev/null +++ b/regtests/out15.out @@ -0,0 +1,4 @@ +pck.ads:43: single line comment should not terminate with dot +pck.ads:67: no trailing spaces allowed +pck.ads:67: single line comment should not terminate with dot +pck.ads:77: single line comment should not terminate with dot diff --git a/src/checks.ads b/regtests/pck.ads similarity index 91% copy from src/checks.ads copy to regtests/pck.ads index 62daf3c..d176574 100644 --- a/src/checks.ads +++ b/regtests/pck.ads @@ -22,7 +22,7 @@ with Ada.Strings.Unbounded; with GNAT.OS_Lib; -package Checks is +package Pck is use Ada.Strings.Unbounded; @@ -40,7 +40,7 @@ package Checks is type Data is record Line_Ending : Line_Ending_Style := UNIX; - -- The line ending style accepted + -- The line ending style accepted. Line_Length_Max : Positive := 79; -- The maximum line length @@ -64,15 +64,19 @@ package Checks is -- Copyright line must match the given regexp pattern Check_Syntax : Boolean := True; - -- Syntax must be checked + -- Syntax must be checked. Space_Comment : Natural := 2; - -- Number of spaces after a comment tag + -- Number of spaces after a comment tag. + -- Test me. Checker_Params : GNAT.OS_Lib.Argument_List (1 .. Max_Parameters); -- Style checker parameters + Comment_Dot_EOL : Boolean := True; + -- Single line comment can terminate with a dot. + Index : Natural := 0; end record; -end Checks; +end Pck; diff --git a/src/checks.ads b/src/checks.ads index 62daf3c..36fb0f1 100644 --- a/src/checks.ads +++ b/src/checks.ads @@ -72,6 +72,9 @@ package Checks is Checker_Params : GNAT.OS_Lib.Argument_List (1 .. Max_Parameters); -- Style checker parameters + Comment_Dot_EOL : Boolean := True; + -- Single line comment can terminate with a dot + Index : Natural := 0; end record; diff --git a/src/languages.adb b/src/languages.adb index eee75d2..a9e23ad 100644 --- a/src/languages.adb +++ b/src/languages.adb @@ -93,6 +93,15 @@ package body Languages is return Get_From_Name ("unknown"); end Get; + ------------------------- + -- Get_Comment_Dot_EOL -- + ------------------------- + + function Get_Comment_Dot_EOL (L : in Lang) return Boolean is + begin + return L.C.Comment_Dot_EOL; + end Get_Comment_Dot_EOL; + --------------------------- -- Get_Copyright_Pattern -- --------------------------- @@ -169,6 +178,24 @@ package body Languages is Lang_Set (Index).Name := To_Unbounded_String (Name); end Register; + ------------------------- + -- Set_Comment_Dot_EOL -- + ------------------------- + + procedure Set_Comment_Dot_EOL + (L : in Lang_Access; + Mode : in Boolean) is + begin + if L = null then + for K in 1 .. Index loop + Set_Comment_Dot_EOL (Lang_Set (K), Mode); + end loop; + + else + L.C.Comment_Dot_EOL := Mode; + end if; + end Set_Comment_Dot_EOL; + --------------------------- -- Set_Copyright_Pattern -- --------------------------- diff --git a/src/languages.ads b/src/languages.ads index 134e7cb..8135c15 100644 --- a/src/languages.ads +++ b/src/languages.ads @@ -137,6 +137,14 @@ package Languages is function Get_Space_Comment (L : in Lang) return Natural; + -- Comments ending + + procedure Set_Comment_Dot_EOL + (L : in Lang_Access; + Mode : in Boolean); + + function Get_Comment_Dot_EOL (L : in Lang) return Boolean; + -- Style checker parameter procedure Add_Style_Checker_Parameter diff --git a/src/style_checker.adb b/src/style_checker.adb index 51a0792..b7472fb 100644 --- a/src/style_checker.adb +++ b/src/style_checker.adb @@ -83,13 +83,15 @@ procedure Style_Checker is Real_Filename : Unbounded_String; type File_Checker is record - File : File_Reader.File_Type; - Lang : Languages.Lang_Access; - Count_Blank : Natural := 0; - Copyright_Found : Boolean := False; - Copyright_Year : Boolean := False; - Header_Size : Natural := 0; - In_Header : Boolean := True; + File : File_Reader.File_Type; + Lang : Languages.Lang_Access; + Count_Blank : Natural := 0; + Copyright_Found : Boolean := False; + Copyright_Year : Boolean := False; + Header_Size : Natural := 0; + In_Header : Boolean := True; + Consecutive_Comment : Natural := 0; + Last_Comment_Dot_EOL : Boolean := False; end record; procedure Check (Filename : in String); @@ -101,9 +103,12 @@ procedure Style_Checker is Line_Ending : in Checks.Line_Ending_Style); -- Pass all checks that are line related + subtype Line_Offset is Integer range -1 .. 0; + procedure Report_Error (File : in File_Reader.File_Type; - Message : in String); + Message : in String; + Offset : in Line_Offset := 0); -- Report an error to standard error procedure Report_Error @@ -218,6 +223,47 @@ procedure Style_Checker is procedure Check_Space_Comment; + procedure Check_Comment_Dot_EOL; + + --------------------------- + -- Check_Comment_Dot_EOL -- + --------------------------- + + procedure Check_Comment_Dot_EOL is + begin + if not Checker.Lang.Get_Comment_Dot_EOL + and then Checker.Lang.Comment /= "" + then + if Fixed.Index (Line, String'(Checker.Lang.Comment)) /= 0 then + -- This is a comment + Checker.Consecutive_Comment := Checker.Consecutive_Comment + 1; + + if Line + (Fixed.Index_Non_Blank (Line, Going => Backward)) = '.' + then + Checker.Last_Comment_Dot_EOL := True; + else + Checker.Last_Comment_Dot_EOL := False; + end if; + + else + -- No more in a comment line + + if Checker.Consecutive_Comment = 1 + and then Checker.Last_Comment_Dot_EOL + then + Report_Error + (Checker.File, + "single line comment should not terminate with dot", + Offset => -1); + end if; + + Checker.Consecutive_Comment := 0; + Checker.Last_Comment_Dot_EOL := False; + end if; + end if; + end Check_Comment_Dot_EOL; + --------------------- -- Check_Copyright -- --------------------- @@ -394,6 +440,7 @@ procedure Style_Checker is Check_Header; Check_Copyright; Check_Space_Comment; + Check_Comment_Dot_EOL; end Check_Line; -------------------- @@ -416,9 +463,11 @@ procedure Style_Checker is procedure Report_Error (File : in File_Reader.File_Type; - Message : in String) + Message : in String; + Offset : in Line_Offset := 0) is - Line : constant String := Natural'Image (File_Reader.Line (File)); + Line : constant String := + Natural'Image (File_Reader.Line (File) + Offset); begin Error_Count := Error_Count + 1; if Error_Count <= Max_Error then @@ -499,6 +548,8 @@ procedure Style_Checker is P (" -cf : if present a copyright line should match the" & "given pattern"); P (" -cF : disable copyright pattern check"); + P (" -d : check single comment line dot ending"); + P (" -D : disable check for single comment line dot ending"); P (" -e DOS|UNIX : line ending style (UNIX default)"); P (" -E : disable line ending check"); P (" -h N : start with an header of N line (default N 20)"); @@ -531,7 +582,7 @@ begin loop case GNAT.Command_Line.Getopt ("abs lang: ign: e: E l? h? H " - & "L b B s S t T v c? C cp cy cP cY cf: cF sp: m: n:") + & "L b B s S t T v c? C cp cy cP cY cf: cF d D sp: m: n:") is when ASCII.NUL => exit; @@ -543,6 +594,12 @@ begin raise Checks.Syntax_Error; end if; + when 'd' => + Languages.Set_Comment_Dot_EOL (Lang, False); + + when 'D' => + Languages.Set_Comment_Dot_EOL (Lang, True); + when 'e' => Languages.Set_Line_Ending (Lang, Checks.Line_Ending_Style'Value -- 2.11.4.GIT