home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lifeos2.zip / LIFE-1.02 / TESTS / FILES / TESTSLC.OUT < prev    next >
Text File  |  1996-06-04  |  34KB  |  187 lines

  1. module("superlint") ?
  2.  
  3. public(main) ?
  4.  
  5. load("c_public_terms")?
  6. load("sl_public_terms")?
  7. load("sl_utils")?
  8. load("sl_parser")?
  9.  
  10. open("c_public_terms")?
  11. open("sl_public_terms")?
  12. open("sl_parser")?
  13. open("sl_utils")?
  14. load("rl_overload")?
  15.  
  16. '*rule*'(_A: "variable_length",
  17.          _B,
  18.          _C) :- is_list_member("declaration",
  19.                                _C), (parse_mode :== prefix, _B :== local_declaration), !, '*rule_name*' <<- ref(_A), _D = _B .root_scope, _E = _D .left_brace, _F = _D .right_brace,(_E .file :\== _F .file, !, error_msg("Braces around a block must be in the same file",
  20.                                                                                                                                                                                                                           _F) ; strlen(_B .name .id) =< sqrt(_F .line - _E .line), !, error_msg("Variable name is too short",
  21.                                                                                                                                                                                                                                                                                                 _B .name) ; succeed) ; succeed.
  22.  
  23. '*rule*'(_A: "extern_in_c_file",
  24.          _B,
  25.          _C) :- is_list_member("declaration",
  26.                                _C), (parse_mode :== prefix, _B :== external_declaration), !, '*rule_name*' <<- ref(_A),((_B .type .store_class :== extern, file_extension(_B .name .file) :== "c"), !, error_msg("No declaration in a .c file accessed out of the .c file",
  27.                                                                                                                                                                                                                  _B .name) ; succeed) ; succeed.
  28.  
  29. '*rule*'(_A: "block_alignment",
  30.          _B,
  31.          _C) :- is_list_member("alignment",
  32.                                _C), ((parse_mode :== prefix, is_not_token(_B)),((_B :== if, ! ; _B :== while), ! ; _B :== for)), !, '*rule_name*' <<- ref(_A), _D = _B,(_D :== {while;
  33.                                                                                                                                                                                 for}, !, _E = _B .keyword, _F = _B .body,(_F :== block, !, test_alignment(_E,
  34.                                                                                                                                                                                                                                                           _F) ; succeed) ; _D :== if, !, _G = _B .keyword, _H = _B .then_body, (_H :== block, !, test_alignment(_G,
  35.                                                                                                                                                                                                                                                                                                                                                                 _H) ; succeed), _I = _B .else_body,(_I :\== nothing, !, _J = _B .else_keyword,(_I :== block, !, test_alignment(_J,
  36.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                _I) ; succeed) ; succeed) ; succeed) ; succeed.
  37.  
  38. test_alignment(_A,
  39.                _B) :- _C = _B .left_brace, _D = _B .right_brace, (_C .white_spaces :\== " ", !, error_msg("There must be one space between brace and condition",
  40.                                                                                                           _C) ; succeed),(_D .column :\== _A .column, !, error_msg("Brace must be aligned with first character of instruction",
  41.                                                                                                                                                                    _D) ; succeed).
  42.  
  43. '*rule*'(_A: "nested_typedef",
  44.          _B,
  45.          _C) :- is_list_member("type_definition",
  46.                                _C), (parse_mode :== prefix, _B :== type_definition), !, '*rule_name*' <<- ref(_A), _D = _B .type,((_D :== protected_type, _D .type :== protected_type), !, error_msg("Only one level of nested parentheses allowed around type definitions",
  47.                                                                                                                                                                                                      _B .name) ; succeed) ; succeed.
  48.  
  49. '*rule*'(_A: "contiguous_nested_typedef",
  50.          _B,
  51.          _C) :- is_list_member("type_definition",
  52.                                _C), (parse_mode :== prefix, _B :== type_definition), !, '*rule_name*' <<- ref(_A), _D = _B .type,(((_D :== protected_type, _D .type :== protected_type), _D .left_parenthesis .next :== "("), !, error_msg("Only one level of contiguous nested parentheses allowed around type definitions",
  53.                                                                                                                                                                                                                                            _B .name) ; succeed) ; succeed.
  54.  
  55. '*rule*'(_A: "assignment_style",
  56.          _B,
  57.          _C) :- is_list_member("operator",
  58.                                _C), (parse_mode :== prefix, is_assignment_operator(_B)), !, '*rule_name*' <<- ref(_A),((_B .white_spaces :\== " ", ! ; _B .next .white_spaces :\== " "), !, error_msg("Assignment operators must be preceded and followed by a single space",
  59.                                                                                                                                                                                                       _B) ; succeed) ; succeed.
  60.  
  61. '*rule*'(_A: "adjacent_operators",
  62.          _B,
  63.          _C) :- is_list_member("operator",
  64.                                _C), ((parse_mode :== prefix, is_operator(_B)), is_token(_B)), !, '*rule_name*' <<- ref(_A),((is_operator(_B .previous), _B .white_spaces :== ""), !, error_msg("Two operators cannot be adjacent",
  65.                                                                                                                                                                                                _B) ; succeed) ; succeed.
  66.  
  67. '*rule*'(_A: "uninitialized_variable",
  68.          _B,
  69.          _C) :- is_list_member("semantics",
  70.                                _C), is_expression(_B), !, '*rule_name*' <<- ref(_A), _D = _B .mode, (parse_mode :== prefix, !, _E = _D,(_E :== special, !,(_B .operator :== "?", !, verify_init(_B .condition), verify_init(_B .then), verify_init(_B .else) ; _B :== protected_expression, !, verify_init(_B .expression) ; _B :== function_call, !, verify_init(_B .call), verify_init_args(_B .arguments .first_argument) ; _B :== array_reference, !, verify_init(_B .array), verify_init(_B .index) ; verify_init(_B .expression)) ; _E :== infix, !, _F = base_expression(_B .left_expression), ((_B .operator :== "=", _F :== identifier), !, set_feature(uninit_marked,
  71.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  true,
  72.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  _F) ; verify_init(_B .left_expression)), verify_init(_B .right_expression) ; _E :== {prefix;
  73.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       postfix}, !, verify_init(_B .expression) ; succeed) ; succeed),(((parse_mode :== postfix, _D :== infix), _B .operator :== "="), !, _F = base_expression(_B .left_expression),(_F :== identifier, !, _G = declaration_of(_F .id),(_G :== local_declaration, !, set_initialized_variable(_F .id,
  74.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              current_instruction .scope) ; succeed) ; succeed) ; succeed) ; succeed.
  75.  
  76. verify_init(_A) :- _A :== identifier, !,(has_feature(uninit_marked,
  77.                                                      _A), !, succeed ; _B = first_mention_of(_A .id),(_B :== nothing, !, error_msg("Variable may be uninitialized",
  78.                                                                                                                                    _A) ; succeed)) ; succeed.
  79.  
  80. verify_init_args(_A) :- _A :\== nothing, !, verify_init(_A .expression), verify_init_args(_A .next) ; succeed.
  81.  
  82. set_initialized_variable(_A,
  83.                          _B) :- _C = _B,(_C :== then_body, !, _D = _B .instruction, (has_feature(then_init_variables,
  84.                                                                                                  _D), !, succeed ; set_feature(then_init_variables,
  85.                                                                                                                                variables,
  86.                                                                                                                                _D)), set_feature(_A,
  87.                                                                                                                                                  init,
  88.                                                                                                                                                  _D .then_init_variables) ; _C :== else_body, !, _D = _B .instruction, (has_feature(else_init_variables,
  89.                                                                                                                                                                                                                                     _D), !, succeed ; set_feature(else_init_variables,
  90.                                                                                                                                                                                                                                                                   variables,
  91.                                                                                                                                                                                                                                                                   _D)), set_feature(_A,
  92.                                                                                                                                                                                                                                                                                     init,
  93.                                                                                                                                                                                                                                                                                     _D .else_init_variables) ; _C :== {for;
  94.                                                                                                                                                                                                                                                                                                                        {while;
  95.                                                                                                                                                                                                                                                                                                                         do_while}}, !, (has_feature(init_variables,
  96.                                                                                                                                                                                                                                                                                                                                                     _B), !, succeed ; set_feature(init_variables,
  97.                                                                                                                                                                                                                                                                                                                                                                                   variables,
  98.                                                                                                                                                                                                                                                                                                                                                                                   _B)), set_feature(_A,
  99.                                                                                                                                                                                                                                                                                                                                                                                                     init,
  100.                                                                                                                                                                                                                                                                                                                                                                                                     _B .init_variables) ; _C :== block, !, _E = _B .block_declarations .local_declarations,(has_feature(_A,
  101.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         _E), !, set_feature(init,
  102.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             true,
  103.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             _E ._A) ; set_initialized_variable(_A,
  104.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                _B .scope)) ; _C :== {nothing;
  105.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      function_body}, !, succeed ; set_initialized_variable(_A,
  106.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            _B .scope) ; succeed).
  107.  
  108. '*rule*'(_A: "propagate_init",
  109.          _B,
  110.          _C) :- is_list_member("semantics",
  111.                                _C), ((_B :== if, is_not_token(_B)), parse_mode :== postfix), !, '*rule_name*' <<- ref(_A), _D = intersect_init_variables(_B), set_initialized_variables(_D,
  112.                                                                                                                                                                                         _B .scope) ; succeed.
  113.  
  114. set_initialized_variables(_A,
  115.                           _B) :- _A :== [], !, succeed ; set_initialized_variable(_A .first_element,
  116.                                                                                   _B), set_initialized_variables(_A .tail,
  117.                                                                                                                  _B).
  118.  
  119. intersect_init_variables(_A) -> _B | (has_feature(then_init_variables,
  120.                                                   _A), !,(has_feature(else_init_variables,
  121.                                                                       _A), !, _B = intersection(features_list(_A .then_init_variables),
  122.                                                                                                 features_list(_A .else_init_variables)) ; _B = features_list(_A .then_init_variables)) ; has_feature(else_init_variables,
  123.                                                                                                                                                                                                      _A), !, _B = features_list(_A .else_init_variables) ; _B = []), cond(_B :== @,
  124.                                                                                                                                                                                                                                                                           _B = unknown).
  125.  
  126. intersection(_A,
  127.              _B) -> _C | (_A :== [], !, _C = [] ; _D = _A .first_element, _E = intersection(_A .tail,
  128.                                                                                             _B),(member(_D,
  129.                                                                                                         _B), !, _C = newlist(_D,
  130.                                                                                                                              _E) ; _C = _E)), cond(_C :== @,
  131.                                                                                                                                                    _C = unknown).
  132.  
  133. base_expression(_A) -> _B | (_A :== protected_expression, !, _B = base_expression(_A .expression) ; _B = _A), cond(_B :== @,
  134.                                                                                                                    _B = unknown).
  135.  
  136. is_expression(_A) -> _B | (has_feature(mode,
  137.                                        _A), !, _B = true ; _B = false), cond(_B :== @,
  138.                                                                              _B = unknown).
  139.  
  140. first_mention_of(_A) -> _B | (current_block :== nothing, !, _B = external ; _B = search_mention_of(_A,
  141.                                                                                                    current_block)), cond(_B :== @,
  142.                                                                                                                          _B = unknown).
  143.  
  144. search_mention_of(_A,
  145.                   _B) -> _C | (_D = _B,(_D :== block, !, _E = _B .block_declarations .local_declarations,(has_feature(_A,
  146.                                                                                                                       _E), !,((_E ._A .initialization :\== nothing, ! ; has_feature(init,
  147.                                                                                                                                                                                     _E ._A)), !, _C = init ; _C = nothing) ; _C = search_mention_of(_A,
  148.                                                                                                                                                                                                                                                     _B .scope)) ; _D :== then_body, !, _F = _B .instruction,((has_feature(then_init_variables,
  149.                                                                                                                                                                                                                                                                                                                           _F), has_feature(_A,
  150.                                                                                                                                                                                                                                                                                                                                            _F .then_init_variables)), !, _C = init ; _C = search_mention_of(_A,
  151.                                                                                                                                                                                                                                                                                                                                                                                                             _F .scope)) ; _D :== else_body, !, _F = _B .instruction,((has_feature(else_init_variables,
  152.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   _F), has_feature(_A,
  153.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    _F .else_init_variables)), !, _C = init ; _C = search_mention_of(_A,
  154.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     _F .scope)) ; _D :== {for;
  155.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           {while;
  156.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            do_while}}, !,((has_feature(init_variables,
  157.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        _B), has_feature(_A,
  158.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         _B .init_variables)), !, _C = init ; _C = search_mention_of(_A,
  159.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     _B .scope)) ; _D :== {nothing;
  160.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           function_body}, !, _C = external ; search_mention_of(_A,
  161.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                _B .scope) ; succeed)), cond(_C :== @,
  162.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             _C = unknown).
  163.  
  164. declaration_of(_A) -> _B | (current_block :== nothing, !, _B = nothing ; _B = search_declaration(current_block .block_declarations,
  165.                                                                                                  _A)), cond(_B :== @,_B = unknown).
  166.  
  167. search_declaration(_A,
  168.                    _B) -> _C | ((_A :== block_declarations, !, _D = _A .local_declarations ; _D = nothing),(_D :== nothing, !, _C = nothing ; has_feature(_B,
  169.                                                                                                                                                           _D), !, _C = _D ._B ; _C = search_declaration(_A .parent_declarations,
  170.                                                                                                                                                                                                         _B))), cond(_C :== @,
  171.                                                                                                                                                                                                                     _C = unknown).
  172.  
  173. M : main :- 
  174.                   sl_parse(strip(M)),writeln, writeln("Style checking : "), writeln("----------------"), writeln, scan_tree({(operator, type_definition, alignment, declaration, variable_length)}), writeln, writeln("Uninitialized variables : "), writeln("-------------------------"), writeln, scan_tree(semantics).
  175.  
  176. '*superlint_rules*' <<- ["variable_length",
  177.  "extern_in_c_file",
  178.  "block_alignment",
  179.  "nested_typedef",
  180.  "contiguous_nested_typedef",
  181.  "assignment_style",
  182.  "adjacent_operators",
  183.  "uninitialized_variable",
  184.  "propagate_init",
  185.  "main"] ?
  186.  
  187.