]> git.donarmstrong.com Git - lilypond.git/blobdiff - buildscripts/fixcc.py
* flower/file-path.cc:
[lilypond.git] / buildscripts / fixcc.py
index 4989850d820169fbe74a42c5a2b949841d19552f..19d97b1955d1e684a3f753af462234059201f582 100644 (file)
@@ -5,9 +5,11 @@
 # TODO
 #  * maintainable rules: regexp's using whitespace (?x) and match names
 #    <identifier>)
-#  * trailing * vs function definition
+#  * trailing `*' vs. function definition
+#  * do not break/change indentation of fixcc-clean files
 #  * check lexer, parser
 #  * rewrite in elisp, add to cc-mode
+#  * using regexes is broken by design
 #  * ?
 #  * profit
 
@@ -20,17 +22,28 @@ import sys
 import time
 
 COMMENT = 'COMMENT'
+STRING = 'STRING'
+GLOBAL_CXX = 'GC++'
 CXX = 'C++'
 verbose_p = 0
 indent_p = 0
 
 rules = {
+       GLOBAL_CXX:
+       [
+       # delete gratuitous block
+       ('''\n(    |\t)\s*{\n\s*(.*?)(?![{}]|\b(do|for|else|if|switch|while)\b);\n\s*}''',
+        '\n\\2;'),
+       ],
        CXX:
        [
        # space before parenthesis open
        ('([^\( \]])[ \t]*\(', '\\1 ('),
        # space after comma
-       (',[ \t]*', ', '),
+       ("\([^'],\)[ \t]*", '\1 '),
+       # delete gratuitous block
+       ('''\n(    |\t)\s*{\n\s*(.*?)(?![{}]|\b(do|for|else|if|switch|while)\b);\n\s*}''',
+        '\n\\2;'),
        # delete inline tabs
        ('(\w)\t+', '\\1 '),
        # delete inline double spaces
@@ -47,7 +60,7 @@ rules = {
        #('\)[ \t]*([^\w])', ')\\1'),
        # delete space around operator
        # ('([\w\(\)\]])([ \t]*)(::|\.)([ \t]*)([\w\(\)])', '\\1\\3\\5'),
-       ('([\w\(\)\]])([ \t]*)(\.)([ \t]*)([\w\(\)])', '\\1\\3\\5'),
+       ('([\w\(\)\]])([ \t]*)(\.|->)([ \t]*)([\w\(\)])', '\\1\\3\\5'),
        # delete space after operator
        ('(::)([ \t]*)([\w\(\)])', '\\1\\3'),
        # delete superflous space around operator
@@ -57,24 +70,27 @@ rules = {
        # space around operator2
        ('([\w\)\]]) *(&&|\|\||<=|>=|!=|\|=|==|\+=|-=|\*=|/=|\?|<|>|=|/|:|&|\||\*) ([^\w\s])', '\\1 \\2 \\3'),
        # space around operator3
-       ('([^\w\s]) (&&|\|\||<=|>=|!=|\|=|==|\+=|-=|\*=|/=|\?|<|>|=|/|:|&|\||\*) *([\w\(])', '\\1 \\2 \\3'),
+       ('([^\w\s]) (&&|\|\||<=|>=|!=|\|=|==|\+=|-=|\*=|/=|\?|<|[^-]>|=|/|:|&|\||\*) *([\w\(])', '\\1 \\2 \\3'),
+       # space around operator4
+       ('([\w\(\)\]]) (\*|/|\+|-) *([-:])', '\\1 \\2 \\3'),
        # space around +/-; exponent
        ('([\w\)\]])(\+|-)([_A-Za-z\(])', '\\1 \\2 \\3'),
        ('([_\dA-Za-df-z\)\]])(\+|-)([\w\(])', '\\1 \\2 \\3'),
        # trailing operator
        (' (::|&&|\|\||<=|>=|!=|\|=|==|\+=|-=|\*=|/=|\?|<|>|\+|-|=|/|:|&XXX|\||\*XXX)[ \t]*\n([ \t]*)',  '\n\\2\\1 '),
-       #breaks function definitions
-       #to#(' (::|&&|\|\||<=|>=|!=|\|=|==|\+=|-=|\*=|/=|<|>|\+|-|=|/|&|\||\*)[ \t]*\n([ \t]*)',         '\n\\2\\1 '),
        # pointer
-       ('(bool|char|const|delete|int|stream|unsigned|void|(struct \w+)|([A-Z]\w*)|[,]|&&|\|\|)[ \t]*(\*|&)[ \t]*', '\\1 \\4'),
+       ##('(bool|char|const|delete|int|stream|unsigned|void|size_t|struct \w+|[A-Z]\w*|,|;|&&|<|[^-]>|\|\||-|\+)[ \t]*(\*|&)[ \t]*', '\\1 \\2'),
+       ('(bool|char|const|delete|int|stream|unsigned|void|size_t|struct \w+|[A-Z]\w*|,|;|:|=|\?\)|&&|<|[^-]>|\|\||-|\+)[ \t]*(\*|&)[ \t]*', '\\1 \\2'),
        #to#('(bool|char|const|delete|int|stream|unsigned|void|([A-Z]\w*)|[,])[ \n\t]*(\*|&)[ \t]*', '\\1 \\3'),
        # pointer with template
-       ('(( *((bool|char|delete|int|stream|unsigned|void|(class[ \t]+\w*)|([A-Z]\w*)|[,])[ \*&],*)+)>) *(\*|&) *', '\\1 \\7'),
+       ('(( *((bool|char|const|delete|int|stream|unsigned|void|size_t|class[ \t]+\w*|[A-Z]\w*|\w+::\w+|[,])[ \*&],*)+)>) *(\*|&) *', '\\1 \\5'),
        #to#('(( *((bool|char|delete|int|stream|unsigned|void|(class[ \t]+\w*)|([A-Z]\w*)|[,])[ \*&],*)+)>)[ \t\n]*(\*|&) *', '\\1 \\7'),
        # unary pointer, minus, not
        ('(return|=) (\*|&|-|!) ([\w\(])', '\\1 \\2\\3'),
        # space after `operator'
        ('(\Woperator) *([^\w\s])', '\\1 \\2'),
+       # dangling brace close
+       ('\n[ \t]*(\n[ \t]*})', '\\1'),
        # dangling newline
        ('\n[ \t]*\n[ \t]*\n', '\n\n'),
        # dangling parenthesis open
@@ -91,9 +107,9 @@ rules = {
        # brace open backslash
        ('(\w[^\n]*){[ \t]*\\\\\n', '\\1\\\n{\\\n'),
        # brace close
-       ('}[ \t]*([^\n]*\w[^\n\\\]*)\n', '}\n\\1\n'),
+       ("}[ \t]*([^'\n]*\w[^\n\\\]*)\n", '}\n\\1\n'),
        # brace close backslash
-       ('}[ \t]*([^\n]*\w[^\n\\\]*)', '\n}\n\\1'),
+       ("}[ \t]*([^'\n]*\w[^\n\\\]*)", '\n}\n\\1'),
        # delete space after `operator'
        #('(\Woperator) (\W)', '\\1\\2'),
        # delete space after case, label
@@ -112,12 +128,15 @@ rules = {
        ('(typedef struct\s+([\w]*\s){([^}]|{[^}]*})*})\s*\n\s*(\w[\w\d]*;)', '\\1 \\4'),
        # delete spaces around template brackets
        #('(dynamic_cast|template|([A-Z]\w*))[ \t]*<[ \t]*(( *(bool|char|int|unsigned|void|(class[ \t]+\w*)|([A-Z]\w*)),?)+)[ \t]?(| [\*&])[ \t]*>', '\\1<\\3\\8>'),
-       ('(dynamic_cast|template|([A-Z]\w*))[ \t]*<[ \t]*(( *(bool|char|int|unsigned|void|(class[ \t]+\w*)|([A-Z]\w*))[,\*&]*)+)[ \t]?(| [\*&])[ \t]*>', '\\1<\\3\\8>'),
+       ('(dynamic_cast|template|typedef|\w+::\w+|[A-Z]\w*)[ \t]*<[ \t]*(( *(bool|char|const|int|unsigned|void|size_t|class[ \t]+\w*|[A-Z]\w*)( *[\*&]?,|[\*&])*)+)[ \t]?(| [\*&])[ \t]*>', '\\1<\\2\\6>'),
+       ('(\w+::\w+|[A-Z]\w*) < ((\w+::\w+|[A-Z]\w*)<[A-Z]\w*>) >', '\\1<\\2 >'),
        ('((if|while)\s+\(([^\)]|\([^\)]*\))*\))\s*;', '\\1\n;'),
        ('(for\s+\(([^;]*;[^;]*;([^\)]|\([^\)]*\))*)\))\s*;', '\\1\n;'),
-       # do .. while
-       ('(\Wdo\s*{([^}]|{[^}]*})*}\s*while\s*)(\(([^\)]|\([^\)]*\))*\))\s*;', '\\1\\3;\n'),
+       # do {..} while
+       ('(}\s*while\s*)(\(([^\)]|\([^\)]*\))*\))\s*;', '\\1\\2;'),
+
        ## Fix code that gets broken by rules above.
+       ##('->\s+\*', '->*'),
        # delete space before #define x()
        ('#[ \t]*define (\w*)[ \t]*\(', '#define \\1('),
        # add space in #define x ()
@@ -155,41 +174,38 @@ rules = {
 no_match = 'a\ba'
 snippet_res = {
        CXX: {
-               'include':
-                 no_match,
-
-               'multiline_comment':
-                 r'''(?sx)
-                   (?P<match>
-                   (?P<code>
-                   [ \t]*/\*.*?\*/))''',
-
-               'singleline_comment':
-                 r'''(?mx)
-                   ^.*
-                   (?P<match>
-                     (?P<code>
-                     [ \t]*//([ \t][^\n]*|)\n))''',
-
-               'string':
-                 r'''(?x)
-                   (?P<match>
-                   (?P<code>
-                   "([^"]|(([^\\]|(\\\\))\\"))*"))''',
-
-               'char':
-                 r'''(?x)
-                   (?P<match>
-                   (?P<code>
-                   '([^']+|\')))''',
-
-               'include':
-                 r'''(?x)
-                   (?P<match>
-                   (?P<code>
-                   "#[ \t]*include[ \t]*<[^>]*>''',
-       },
-       }
+       'multiline_comment':
+       r'''(?sx)
+       (?P<match>
+       (?P<code>
+       [ \t]*/\*.*?\*/))''',
+       
+       'singleline_comment':
+       r'''(?mx)
+       ^.*
+       (?P<match>
+       (?P<code>
+       [ \t]*//([ \t][^\n]*|)\n))''',
+
+       'string':
+       r'''(?x)
+       (?P<match>
+       (?P<code>
+       "([^\"\n](\")*)*"))''',
+       
+       'char':
+       r'''(?x)
+       (?P<match>
+       (?P<code>
+       '([^']+|\')))''',
+          
+          'include':
+          r'''(?x)
+          (?P<match>
+          (?P<code>
+          "#[ \t]*include[ \t]*<[^>]*>''',
+          },
+         }
 
 class Chunk:
        def replacement_text (self):
@@ -198,12 +214,6 @@ class Chunk:
        def filter_text (self):
                return self.replacement_text ()
 
-       def ly_is_outdated (self):
-               return 0
-
-       def png_is_outdated (self):
-               return 0
-
 class Substring (Chunk):
        def __init__ (self, source, start, end):
                self.source = source
@@ -217,11 +227,12 @@ class Substring (Chunk):
                for i in rules[CXX]:
                        if verbose_p:
                                sys.stderr.write ('.')
-                               #sys.stderr.write ('\n*********\n')
+                               #sys.stderr.write ('\n\n***********\n')
                                #sys.stderr.write (i[0])
-                               #sys.stderr.write ('\n=========\n')
+                               #sys.stderr.write ('\n***********\n')
+                               #sys.stderr.write ('\n=========>>\n')
                                #sys.stderr.write (s)
-                               #sys.stderr.write ('\n*********\n')
+                               #sys.stderr.write ('\n<<=========\n')
                        s = re.sub (i[0], i[1], s)
                if verbose_p:
                        sys.stderr.write ('done\n')
@@ -265,8 +276,7 @@ class Multiline_comment (Snippet):
 
 snippet_type_to_class = {
        'multiline_comment': Multiline_comment,
-#      'lilypond_block': Lilypond_snippet,
-#      'lilypond': Lilypond_snippet,
+#      'string': Multiline_comment,
 #      'include': Include_snippet,
 }
 
@@ -345,13 +355,17 @@ def find_toplevel_snippets (s, types):
 def nitpick_file (outdir, file):
        s = open (file).read ()
 
+       for i in rules[GLOBAL_CXX]:
+               s = re.sub (i[0], i[1], s)
+
        # FIXME: Containing blocks must be first, see
        #        find_toplevel_snippets.
+       #        We leave simple strings be part of the code
        snippet_types = (
                'multiline_comment',
                'singleline_comment',
                'string',
-               'char',
+#              'char',
                )
 
        chunks = find_toplevel_snippets (s, snippet_types)
@@ -490,55 +504,78 @@ def main ():
                nitpick_file (outdir, i)
 
 
+## TODO: make this compilable and check with g++
 TEST = '''
+#include <libio.h>
+#include <map>
+class
+ostream ;
+
+class Foo {
+public: static char* foo ();
+std::map<char*,int>* bar (char, char) { return 0; }
+};
+typedef struct
+{
+  Foo **bar;
+} String;
+
 ostream &
 operator << (ostream & os, String d);
 
 typedef struct _t_ligature
 {
   char *succ, *lig;
-  struct _t_ligature *next;
   struct _t_ligature * next;
 }  AFM_Ligature;
+  
+typedef std::map < AFM_Ligature const *, int > Bar;
 
+  /**
+  (c) 1997--2006 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+  */
+  
+/*      ||
+ *      vv
+ * !OK  OK
+ */
+/*     ||
+       vv
+  !OK  OK
+ */
 char *
-Bar:: foe ()
+Foo:: foo ()
 {
-  char* a= ++ 3  ;
-  a [x] = foe (*i, &bar) *
+int
+i
+;
+  char* a= &++ i ;
+  a [*++ a] = (char*) foe (*i, &bar) *
   2;
   int operator double ();
-  int x =foe(1 ,3);
-  Interval_t<T> &operator*= (T r);
+  std::map<char*,int> y =*bar(-*a ,*b);
+  Interval_t<T> & operator*= (T r);
+  Foo<T>*c;
   int compare (Pqueue_ent < K, T > const& e1, Pqueue_ent < K,T> *e2);
   delete *p;
   if (abs (f)*2 > abs (d) *FUDGE)
     ;
   while (0);
-  for (; i < x (); foo > bar);
-  for (; i < x > y;
-  foo > bar)
+  for (; i<x foo(); foo>bar);
+  for (; *p && > y;
+       foo > bar)
 ;
   do {
-  ..
+  ;;;
   }
   while (foe);
 
   squiggle. extent;
-
   1 && * unsmob_moment (lf);
-  
-  line_spanner_ = make_spanner ("DynamicLineSpanner", rq ? rq->self_scm
+  line_spanner_ = make_spanner ("DynamicLineSpanner", rq ? rq->*self_scm
 (): SCM_EOL);
-
   case foo: k;
 
-  typedef struct
-  {
-    ...
-  } cookie_io_functions_t;
-
-
   if (0) {a=b;} else {
    c=d;
   }
@@ -548,6 +585,32 @@ Bar:: foe ()
     ...
   };
 
+  int compare (Array < Pitch> *, Array < Pitch> *);
+  original_ = (Grob *) & s;
+  Drul_array< Link_array<Grob> > o;
+}
+
+  header_.char_info_pos = (6 + header_length) * 4;
+  return ly_bool2scm (*ma < * mb);
+
+  1 *::sign(2);
+
+  (shift) *-d;
+
+  a = 0 ? *x : *y;
+
+a = "foo() 2,2,4";
+{
+  if (!span_)
+    {
+      span_ = make_spanner ("StaffSymbol", SCM_EOL);
+    }
+}
+{
+  if (!span_)
+    {
+      span_ = make_spanner (StaffSymbol, SCM_EOL);
+    }
 }
 '''