1

In new versions of GExperts, the grep utility now supports more 'expert' expressions.

I have not yet found a way to locate empty try ... except blocks in Delphi sources using regular expressions, how could I do this with the GExperts grep tool?

mjn
  • 36,362
  • 28
  • 176
  • 378

2 Answers2

5

I doubt that GExperts Regex functionality allows you to search beyond line delimiters.

If you don't mind using a component like TPerlRegEx, following code should get you started to roll your own search.

var
  emptyExceptBlock: TPerlRegEx;
  Results: TStringList;

emptyExceptBlock := TPerlRegEx.Create(nil);
emptyExceptBlock.RegEx := except\s+((//.*|/\*.*\*/|\(\*.*\*\))\s+)*end;
emptyExceptBlock.Options := [preExtended];
emptyExceptBlock.Subject := LoadFromFile('YourFile.pas');
Results := TStringList.Create;
if emptyExceptBlock.Match then begin
    repeat
        Results.Add(emptyExceptBlock.MatchedExpression);
    until not emptyExceptBlock.MatchAgain;
end;
Lieven Keersmaekers
  • 57,207
  • 13
  • 112
  • 146
  • The regex should be changed to also allow for embedded comments like "// ignore all exceptions" - technically this is still an empty exception handler. – mghie Jun 09 '09 at 11:46
  • @mghie: I've adjusted the regex to match //, /* */ and (* *). I've left nested comments as an excercise for those who want to go that far. – Lieven Keersmaekers Jun 09 '09 at 12:49
  • A great solution! Yes there are still limitations in the last GExpert release. It would be a very interesting feature if GExpert could perform some simple static code analysis tasks using TPerlRegEx. (even if there are some false positives) – mjn Jun 09 '09 at 13:15
0

There is a tool called Insert Auto Todo (which is not part of GExperts, I think I got it from CodeCentral) that automatically inserts todos into empty begin/end blocks. Maybe that's what you want?

dummzeuch
  • 10,975
  • 4
  • 51
  • 158
  • Following the rule "if you touch it, you own it" - the analysis tool should not modify the source, only report 'violations' and optionally suggest a penalty ;) – mjn Jun 09 '09 at 13:20