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

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