views:

252

answers:

2

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?

+5  A: 

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
The regex should be changed to also allow for embedded comments like "// ignore all exceptions" - technically this is still an empty exception handler.
mghie
@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
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)
mjustin
A: 

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
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 ;)
mjustin