logify.pl 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env perl
  2. # logify.pl
  3. ############
  4. # Converts an Objective-C header file (or anything containing a @interface and method definitions)
  5. #+into a Logos input file which causes all function calls to be logged.
  6. #
  7. # Accepts input on stdin or via filename specified on the commandline.
  8. # Lines are only processed if we were in an @interface, so you can run this on a file containing
  9. # an @implementation, as well.
  10. use strict;
  11. use FindBin;
  12. use lib "$FindBin::Bin/lib";
  13. use Logos::Method;
  14. use Logos::Util;
  15. $Logos::Util::errorhandler = sub {
  16. die "$ARGV:$.: error: missing closing parenthesis$/"
  17. };
  18. my $interface = 0;
  19. while(my $line = <>) {
  20. if($line =~ m/^[+-]\s*\((.*?)\).*?(?=;)/ && $interface == 1) {
  21. print logLineForDeclaration($&);
  22. } elsif($line =~ m/^\s*\@property\s*\((.*?)\)\s*(.*?)\b([\$a-zA-Z_][\$_a-zA-Z0-9]*)(?=;)/ && $interface == 1) {
  23. my @attributes = smartSplit(qr/\s*,\s*/, $1);
  24. my $propertyName = $3;
  25. my $type = $2;
  26. my $readonly = scalar(grep(/readonly/, @attributes));
  27. my %methods = ("setter" => "set".ucfirst($propertyName).":", "getter" => $propertyName);
  28. foreach my $attribute (@attributes) {
  29. next if($attribute !~ /=/);
  30. my @x = smartSplit(qr/\s*=\s*/, $attribute);
  31. $methods{$x[0]} = $x[1];
  32. }
  33. if($readonly == 0) {
  34. print logLineForDeclaration("- (void)".$methods{"setter"}."($type)$propertyName");
  35. }
  36. print logLineForDeclaration("- ($type)".$methods{"getter"});
  37. } elsif($line =~ m/^\@interface\s+(.*?)\s*[:(]/ && $interface == 0) {
  38. print "%hook $1\n";
  39. $interface = 1;
  40. } elsif($line =~ m/^\@end/ && $interface == 1) {
  41. print "%end\n";
  42. $interface = 0;
  43. }
  44. }
  45. sub logLineForDeclaration {
  46. my $declaration = shift;
  47. $declaration =~ m/^[+-]\s*\((.*?)\).*?/;
  48. my $rtype = $1;
  49. my $innards = "%log; ";
  50. if($rtype ne "void") {
  51. $innards .= "$rtype r = %orig; ";
  52. $innards .= "HBLogDebug(@\" = ".Logos::Method::formatCharForArgType($rtype)."\", ".Logos::Method::printArgForArgType($rtype, "r")."); " if defined Logos::Method::printArgForArgType($rtype, "r");
  53. $innards .= "return r; ";
  54. } else {
  55. $innards .= "%orig; ";
  56. }
  57. return "$declaration { $innards}\n";
  58. }