From c8642fdba80b595c98e0ea3cfa55207a54668749 Mon Sep 17 00:00:00 2001 From: Pascal Obry Date: Wed, 27 Nov 2013 16:23:35 +0100 Subject: [PATCH] Minimal check for then inside a string literal. Avoid false positive for wrong then layout. --- regtests/out21.out | 1 + regtests/thenlayout.adb | 11 +++++++++++ src/style_checker.adb | 17 +++++++++++++++-- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/regtests/out21.out b/regtests/out21.out index c7fbc17..4741f2e 100644 --- a/regtests/out21.out +++ b/regtests/out21.out @@ -1 +1,2 @@ thenlayout.adb:15: 'then' incorrect layout +thenlayout.adb:32: 'then' incorrect layout diff --git a/regtests/thenlayout.adb b/regtests/thenlayout.adb index f2bee88..7a652af 100644 --- a/regtests/thenlayout.adb +++ b/regtests/thenlayout.adb @@ -21,4 +21,15 @@ begin elsif Gh = 9 then null; end if; + + if Gh = 12 + and then Gh = "then" + then + null; + end if; + + if Gh = 12 + and then Gh = "then" then + null; + end if; end ThenLayout; diff --git a/src/style_checker.adb b/src/style_checker.adb index 6ac9391..f708557 100644 --- a/src/style_checker.adb +++ b/src/style_checker.adb @@ -586,6 +586,7 @@ procedure Style_Checker is end Is_Word; I : constant Natural := First_Non_Blank; + K : Natural; L : Natural := Line'Length; If_Pos, Then_Pos : Natural; begin @@ -597,8 +598,20 @@ procedure Style_Checker is end if; If_Pos := Fixed.Index (Line (I .. L), "if"); - Then_Pos := - Fixed.Index (Line (I .. L), "then", Going => Strings.Backward); + + K := L; + + loop + Then_Pos := + Fixed.Index + (Line (I .. K), "then", Going => Strings.Backward); + exit when Then_Pos = 0 + or else Fixed.Count + (Line (Then_Pos .. K), String'(1 => '"')) mod 2 = 0; + -- We exit if then is not found or if found that it is not in a + -- string. + K := Then_Pos; + end loop; if If_Pos /= 0 and then not Is_Word (If_Pos, If_Pos + 1) then -- This is not an if keyword -- 2.11.4.GIT