Sfoglia il codice sorgente

fix for maxindent metric, magic numbers metric

avkonst 11 anni fa
parent
commit
efd83643c0

+ 15 - 2
mainline/CHANGELOG.md

@@ -1,5 +1,18 @@
-## 1.2 (July, 2013)
-- 
+## 1.2 (August, 2013)
+- **Feature** suppressions capability for limit tool
+- **Feature** distribution tables and graphs in viewer
+- **Feature** export tool (exproting of data files to csv format)
+- **Feature** unified stdout output format for all tools
+- **New metric plugin** std.code.magic:numbers: Counts number of magic numbers per region.
+- **New metric plugin** std.code.lines: Counts number of lines of code, comments, preprocessor,
+etc.
+- **New metric plugin** std.code.length: Counts number of symbols per region.
+- **New metric plugin** std.code.complexity:maxindend: Measures maximum level of indented blocks per function.
+- **New metric plugin** std.general:size: Measures file size in bytes.
+- **New documentation**: Explains code regions concepts, available metrics, workflow,
+plugin development.
+- **Re-factoring**: better Metrix++ api for plugin development.
+- **Major bug-fixing**: application to real commercial software and use cases.
 
 ## 1.1 (March, 2013)
 - **Extension point for post analysis tools** added. All tools are merged

+ 1 - 2
mainline/ext/std/code/complexity.py

@@ -62,8 +62,7 @@ class Plugin(mpp.api.Plugin, mpp.api.MetricPluginMixin, mpp.api.Child, mpp.api.I
                                 'std.code.java': self.pattern_indent,
                             },
                             marker_type_mask=mpp.api.Marker.T.CODE,
-                            # TODO shall scan all regions?, it is likely actual to functions
-                            region_type_mask=mpp.api.Region.T.ANY)
+                            region_type_mask=mpp.api.Region.T.FUNCTION)
         
         super(Plugin, self).initialize(fields=self.get_fields())
         

+ 27 - 0
mainline/ext/std/code/magic.ini

@@ -0,0 +1,27 @@
+;
+;    Metrix++, Copyright 2009-2013, Metrix++ Project
+;    Link: http://metrixplusplus.sourceforge.net
+;    
+;    This file is a part of Metrix++ Tool.
+;    
+;    Metrix++ is free software: you can redistribute it and/or modify
+;    it under the terms of the GNU General Public License as published by
+;    the Free Software Foundation, version 3 of the License.
+;    
+;    Metrix++ is distributed in the hope that it will be useful,
+;    but WITHOUT ANY WARRANTY; without even the implied warranty of
+;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;    GNU General Public License for more details.
+;    
+;    You should have received a copy of the GNU General Public License
+;    along with Metrix++.  If not, see <http://www.gnu.org/licenses/>.
+;
+
+[Plugin]
+version: 1.0
+package: std.code
+module:  magic
+class:   Plugin
+depends: None
+actions: collect
+enabled: True

+ 68 - 0
mainline/ext/std/code/magic.py

@@ -0,0 +1,68 @@
+#
+#    Metrix++, Copyright 2009-2013, Metrix++ Project
+#    Link: http://metrixplusplus.sourceforge.net
+#    
+#    This file is a part of Metrix++ Tool.
+#    
+#    Metrix++ is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation, version 3 of the License.
+#    
+#    Metrix++ is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+#    GNU General Public License for more details.
+#    
+#    You should have received a copy of the GNU General Public License
+#    along with Metrix++.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+import mpp.api
+import re
+
+class Plugin(mpp.api.Plugin,
+             mpp.api.IConfigurable,
+             mpp.api.Child,
+             mpp.api.MetricPluginMixin):
+    
+    def declare_configuration(self, parser):
+        parser.add_option("--std.code.magic.numbers", "--scmn",
+            action="store_true", default=False,
+            help="Enables collection of magic numbers metric [default: %default]")
+        parser.add_option("--std.code.magic.numbers.simplier", "--scmns",
+            action="store_true", default=False,
+            help="Is set, 0, -1 and 1 numbers are not counted "
+            "in 'std.code.magic.numbers' metric [default: %default]")
+    
+    def configure(self, options):
+        self.is_active_numbers = options.__dict__['std.code.magic.numbers']
+        self.is_active_numbers_simplier = options.__dict__['std.code.magic.numbers.simplier']
+    
+    def initialize(self):
+        pattern_to_search_java = re.compile(
+            r'''(const\s+([_$a-zA-Z][_$a-zA-Z0-9]*\s+)+[=]\s*)?[-+]?[0-9]+''')
+        pattern_to_search_cpp_cs = re.compile(
+            r'''(const\s+([_a-zA-Z][_a-zA-Z0-9]*\s+)+[=]\s*)?[-+]?[0-9]+''')
+        self.declare_metric(self.is_active_numbers,
+                            self.Field('numbers', int,
+                                non_zero=True),
+                            {
+                             'std.code.java': pattern_to_search_java,
+                             'std.code.cpp': pattern_to_search_cpp_cs,
+                             'std.code.cs': pattern_to_search_cpp_cs,
+                            },
+                            marker_type_mask=mpp.api.Marker.T.CODE,
+                            region_type_mask=mpp.api.Region.T.ANY)
+        
+        super(Plugin, self).initialize(fields=self.get_fields(),
+            properties=[self.Property('number.simplier', self.is_active_numbers_simplier)])
+        
+        if self.is_active() == True:
+            self.subscribe_by_parents_interface(mpp.api.ICode)
+
+    def _numbers_count(self, alias, data, region, marker, match, count, counter_data):
+        if (match.group(0).startswith('const') or
+            (self.is_active_numbers_simplier == True and
+             match.group(0) in ['0', '1', '-1', '+1'])):
+            return count
+        return count + 1

+ 15 - 0
mainline/tests/general/test_basic.py

@@ -310,6 +310,21 @@ class Test(tests.common.TestCase):
         runner = tests.common.ToolRunner('view')
         self.assertExec(runner.run())
 
+    def test_std_code_magic(self):
+
+        runner = tests.common.ToolRunner('collect',
+                                         ['--std.code.magic.numbers'])
+        self.assertExec(runner.run())
+
+        runner = tests.common.ToolRunner('view',
+                                         ['--nest-regions'],
+                                         prefix='nest_per_file',
+                                         dirs_list=['./simple.cpp'])
+        self.assertExec(runner.run())
+
+        runner = tests.common.ToolRunner('view')
+        self.assertExec(runner.run())
+
     def test_debug_tool(self):
 
         runner = tests.common.ToolRunner('collect')

+ 6 - 0
mainline/tests/general/test_basic/test_help_collect_default_stdout.gold.txt

@@ -50,6 +50,12 @@ Options:
   --std.code.lines.total, --sclt
                         Enables collection of lines of comments metric -
                         number of non-empty lines of comments [default: False]
+  --std.code.magic.numbers, --scmn
+                        Enables collection of magic numbers metric [default:
+                        False]
+  --std.code.magic.numbers.simplier, --scmns
+                        Is set, 0, -1 and 1 numbers are not counted in
+                        'std.code.magic.numbers' metric [default: False]
   --std.suppress, --ss  If set (True), suppression markers are collected from
                         comments in code. Suppressions are used by post-
                         processing tools, like limit, to remove false-positive

+ 0 - 0
mainline/tests/general/test_basic/test_std_code_magic_collect_default_stdout.gold.txt


+ 15 - 0
mainline/tests/general/test_basic/test_std_code_magic_view_default_stdout.gold.txt

@@ -0,0 +1,15 @@
+:: info: Overall metrics for 'std.code.magic:numbers' metric
+	Average        : 1.55555555556 (excluding zero metric values)
+	Minimum        : 1
+	Maximum        : 4
+	Total          : 14.0
+	Distribution   : 9 regions in total (including 0 suppressed)
+	  Metric value : Ratio : R-sum : Number of regions
+	             1 : 0.667 : 0.667 : 6	|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
+	             2 : 0.222 : 0.889 : 2	||||||||||||||||||||||
+	             4 : 0.111 : 1.000 : 1	|||||||||||
+
+:: info: Directory content:
+	Directory      : .
+
+

+ 96 - 0
mainline/tests/general/test_basic/test_std_code_magic_view_nest_per_file_stdout.gold.txt

@@ -0,0 +1,96 @@
+./simple.cpp:0: info: Metrics per '__global__' region
+	Region name    : __global__
+	Region type    : global
+	Offsets        : 0-697
+	Line numbers   : 1-71
+	Modified       : None
+
+.   ./simple.cpp:4: info: Metrics per 'hmm' region
+    	Region name    : hmm
+    	Region type    : namespace
+    	Offsets        : 2-696
+    	Line numbers   : 3-70
+    	Modified       : None
+
+.   .   ./simple.cpp:9: info: Metrics per 'A' region
+        	Region name    : A
+        	Region type    : class
+        	Offsets        : 94-692
+        	Line numbers   : 9-68
+        	Modified       : None
+        	std.code.magic:numbers: 1
+
+.   .   .   ./simple.cpp:12: info: Metrics per 'A' region
+            	Region name    : A
+            	Region type    : function
+            	Offsets        : 106-252
+            	Line numbers   : 12-23
+            	Modified       : None
+            	std.code.magic:numbers: 4
+
+.   .   .   ./simple.cpp:26: info: Metrics per 'func' region
+            	Region name    : func
+            	Region type    : function
+            	Offsets        : 256-405
+            	Line numbers   : 26-40
+            	Modified       : None
+            	std.code.magic:numbers: 1
+
+.   .   .   .   ./simple.cpp:28: info: Metrics per 'embeded' region
+                	Region name    : embeded
+                	Region type    : class
+                	Offsets        : 285-391
+                	Line numbers   : 28-38
+                	Modified       : None
+
+.   .   .   .   .   ./simple.cpp:30: info: Metrics per 'embeded' region
+                    	Region name    : embeded
+                    	Region type    : function
+                    	Offsets        : 306-387
+                    	Line numbers   : 30-37
+                    	Modified       : None
+                    	std.code.magic:numbers: 1
+
+.   .   .   ./simple.cpp:42: info: Metrics per 'func_to_be_removed_in_new_sources' region
+            	Region name    : func_to_be_removed_in_new_sources
+            	Region type    : function
+            	Offsets        : 408-596
+            	Line numbers   : 42-56
+            	Modified       : None
+            	std.code.magic:numbers: 1
+
+.   .   .   .   ./simple.cpp:44: info: Metrics per 'embeded' region
+                	Region name    : embeded
+                	Region type    : class
+                	Offsets        : 466-577
+                	Line numbers   : 44-54
+                	Modified       : None
+
+.   .   .   .   .   ./simple.cpp:46: info: Metrics per 'embeded' region
+                    	Region name    : embeded
+                    	Region type    : function
+                    	Offsets        : 487-573
+                    	Line numbers   : 46-53
+                    	Modified       : None
+                    	std.code.magic:numbers: 1
+
+.   .   .   ./simple.cpp:58: info: Metrics per 'never' region
+            	Region name    : never
+            	Region type    : function
+            	Offsets        : 599-669
+            	Line numbers   : 58-65
+            	Modified       : None
+            	std.code.magic:numbers: 2
+
+./simple.cpp:: info: Overall metrics for 'std.code.magic:numbers' metric
+	Average        : 1.57142857143 (excluding zero metric values)
+	Minimum        : 1
+	Maximum        : 4
+	Total          : 11.0
+	Distribution   : 7 regions in total (including 0 suppressed)
+	  Metric value : Ratio : R-sum : Number of regions
+	             1 : 0.714 : 0.714 : 5	|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
+	             2 : 0.143 : 0.857 : 1	||||||||||||||
+	             4 : 0.143 : 1.000 : 1	||||||||||||||
+
+

+ 7 - 8
mainline/tests/general/test_basic/test_std_complexity_maxindent_view_default_stdout.gold.txt

@@ -1,14 +1,13 @@
 :: info: Overall metrics for 'std.code.complexity:maxindent' metric
-	Average        : 1.3125
-	Minimum        : 0
+	Average        : 1.875
+	Minimum        : 1
 	Maximum        : 3
-	Total          : 21.0
-	Distribution   : 16 regions in total (including 0 suppressed)
+	Total          : 15.0
+	Distribution   : 8 regions in total (including 0 suppressed)
 	  Metric value : Ratio : R-sum : Number of regions
-	             0 : 0.125 : 0.125 :  2	|||||||||||||
-	             1 : 0.500 : 0.625 :  8	||||||||||||||||||||||||||||||||||||||||||||||||||
-	             2 : 0.312 : 0.938 :  5	|||||||||||||||||||||||||||||||
-	             3 : 0.062 : 1.000 :  1	||||||
+	             1 : 0.250 : 0.250 : 2	|||||||||||||||||||||||||
+	             2 : 0.625 : 0.875 : 5	|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
+	             3 : 0.125 : 1.000 : 1	|||||||||||||
 
 :: info: Directory content:
 	Directory      : .

+ 7 - 13
mainline/tests/general/test_basic/test_std_complexity_maxindent_view_nest_per_file_stdout.gold.txt

@@ -4,7 +4,6 @@
 	Offsets        : 0-697
 	Line numbers   : 1-71
 	Modified       : None
-	std.code.complexity:maxindent: 0
 
 .   ./simple.cpp:4: info: Metrics per 'hmm' region
     	Region name    : hmm
@@ -12,7 +11,6 @@
     	Offsets        : 2-696
     	Line numbers   : 3-70
     	Modified       : None
-    	std.code.complexity:maxindent: 1
 
 .   .   ./simple.cpp:9: info: Metrics per 'A' region
         	Region name    : A
@@ -20,7 +18,6 @@
         	Offsets        : 94-692
         	Line numbers   : 9-68
         	Modified       : None
-        	std.code.complexity:maxindent: 1
 
 .   .   .   ./simple.cpp:12: info: Metrics per 'A' region
             	Region name    : A
@@ -44,7 +41,6 @@
                 	Offsets        : 285-391
                 	Line numbers   : 28-38
                 	Modified       : None
-                	std.code.complexity:maxindent: 1
 
 .   .   .   .   .   ./simple.cpp:30: info: Metrics per 'embeded' region
                     	Region name    : embeded
@@ -68,7 +64,6 @@
                 	Offsets        : 466-577
                 	Line numbers   : 44-54
                 	Modified       : None
-                	std.code.complexity:maxindent: 1
 
 .   .   .   .   .   ./simple.cpp:46: info: Metrics per 'embeded' region
                     	Region name    : embeded
@@ -87,15 +82,14 @@
             	std.code.complexity:maxindent: 2
 
 ./simple.cpp:: info: Overall metrics for 'std.code.complexity:maxindent' metric
-	Average        : 1.36363636364
-	Minimum        : 0
+	Average        : 1.83333333333
+	Minimum        : 1
 	Maximum        : 3
-	Total          : 15.0
-	Distribution   : 11 regions in total (including 0 suppressed)
+	Total          : 11.0
+	Distribution   : 6 regions in total (including 0 suppressed)
 	  Metric value : Ratio : R-sum : Number of regions
-	             0 : 0.091 : 0.091 :  1	|||||||||
-	             1 : 0.545 : 0.636 :  6	|||||||||||||||||||||||||||||||||||||||||||||||||||||||
-	             2 : 0.273 : 0.909 :  3	|||||||||||||||||||||||||||
-	             3 : 0.091 : 1.000 :  1	|||||||||
+	             1 : 0.333 : 0.333 : 2	|||||||||||||||||||||||||||||||||
+	             2 : 0.500 : 0.833 : 3	||||||||||||||||||||||||||||||||||||||||||||||||||
+	             3 : 0.167 : 1.000 : 1	|||||||||||||||||
 
 

+ 2 - 0
mainline/tests/general/test_basic/test_workflow_info_default_stdout.gold.txt

@@ -8,6 +8,8 @@ test_workflow.db:: info: Created using plugins and settings:
 	std.code.java:version: 1.1
 	std.code.java:files: *.java
 	std.code.lines:version: 1.1
+	std.code.magic:version: 1.0
+	std.code.magic:number.simplier: False
 	std.suppress:version: 1.0
 
 test_workflow.db:: info: Collected metrics:

+ 2 - 0
mainline/tests/general/test_basic/test_workflow_info_second_stdout.gold.txt

@@ -8,6 +8,8 @@ test_workflow.db:: info: Created using plugins and settings:
 	std.code.java:version: 1.1
 	std.code.java:files: *.java
 	std.code.lines:version: 1.1
+	std.code.magic:version: 1.0
+	std.code.magic:number.simplier: False
 	std.suppress:version: 1.0
 
 test_workflow.db:: info: Collected metrics:

+ 2 - 0
mainline/tests/system/test_api_tutorial.py

@@ -34,6 +34,8 @@ class Test(tests.common.TestCase):
     def tearDown(self):
         if self.METRIXPLUSPLUS_PATH != None:
             os.environ['METRIXPLUSPLUS_PATH'] = self.METRIXPLUSPLUS_PATH
+        else:
+            del os.environ['METRIXPLUSPLUS_PATH']
         tests.common.TestCase.tearDown(self)
 
     def test_metric_plugin_api(self):

+ 2 - 0
mainline/tests/system/test_boost_parts/test_workflow_info_default_stdout.gold.txt

@@ -8,6 +8,8 @@ test_workflow.db:: info: Created using plugins and settings:
 	std.code.java:version: 1.1
 	std.code.java:files: *.java
 	std.code.lines:version: 1.1
+	std.code.magic:version: 1.0
+	std.code.magic:number.simplier: False
 
 test_workflow.db:: info: Collected metrics:
 	std.code.complexity:cyclomatic: