diff options
author | Jesse Jones <jesjones@mono-cvs.ximian.com> | 2009-04-30 22:46:36 +0400 |
---|---|---|
committer | Jesse Jones <jesjones@mono-cvs.ximian.com> | 2009-04-30 22:46:36 +0400 |
commit | 17dff5e815f9b35dbeaa00a6b9dd714dd8aa24e9 (patch) | |
tree | 2336a590b14bb75b2a910a8212809c91242654f8 | |
parent | 668d06dcf8f223105298f8d92a3503dfbef9ee50 (diff) |
Added VariableDefinitionRocks which has an IsGeneratedName rock.
Currently it only checks for names which start with "V_" which
may not be enough for csc and boo.
svn path=/trunk/mono-tools/; revision=133203
6 files changed, 203 insertions, 0 deletions
diff --git a/gendarme/framework/ChangeLog b/gendarme/framework/ChangeLog index e8f8cf97..6b710864 100644 --- a/gendarme/framework/ChangeLog +++ b/gendarme/framework/ChangeLog @@ -1,3 +1,7 @@ +2009-04-30 Jesse Jones <jesjones@mindspring.com> + + * Makefile.am: Added VariableDefinitionRocks files. + 2009-01-27 Jonathan Pryor <jpryor@novell.com> * Makefile.am ($(framework_doc_source)): Change the .source file diff --git a/gendarme/framework/Gendarme.Framework.Rocks/ChangeLog b/gendarme/framework/Gendarme.Framework.Rocks/ChangeLog index 6fbff8fc..780042a0 100644 --- a/gendarme/framework/Gendarme.Framework.Rocks/ChangeLog +++ b/gendarme/framework/Gendarme.Framework.Rocks/ChangeLog @@ -1,3 +1,7 @@ +2009-04-30 Jesse Jones <jesjones@mindspring.com> + + * VariableDefinitionRocks.cs: Added IsGeneratedName rock. + 2009-01-28 Jb Evain <jbevain@novell.com> * TypeRocks.cs: Resolve: remove. Implements: handle types diff --git a/gendarme/framework/Gendarme.Framework.Rocks/VariableDefinitionRocks.cs b/gendarme/framework/Gendarme.Framework.Rocks/VariableDefinitionRocks.cs new file mode 100644 index 00000000..142501d9 --- /dev/null +++ b/gendarme/framework/Gendarme.Framework.Rocks/VariableDefinitionRocks.cs @@ -0,0 +1,45 @@ +// +// Gendarme.Framework.Rocks.VariableDefinitionRocks +// +// Authors: +// Jesse Jones <jesjones@mindspring.com> +// +// (C) 2009 Jesse Jones +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +using Mono.Cecil.Cil; +using System; + +namespace Gendarme.Framework.Rocks { + + public static class VariableDefinitionRocks { + + /// <summary> + /// Check if the local variable has a name which was generated by the compiler. + /// Note that this will return true for all variables if debugging information is not present. + /// </summary> + /// <param name="self">The VariableDefinition on which the extension method can be called.</param> + /// <returns>True if the field name was generated by the compiler, False otherwise</returns> + public static bool IsGeneratedName (this VariableDefinition self) + { + return self.Name.StartsWith ("V_"); + } + } +} diff --git a/gendarme/framework/Makefile.am b/gendarme/framework/Makefile.am index 7abb5b89..870501a0 100644 --- a/gendarme/framework/Makefile.am +++ b/gendarme/framework/Makefile.am @@ -48,6 +48,7 @@ framework_sources = \ Gendarme.Framework.Rocks/MethodRocks.cs \ Gendarme.Framework.Rocks/ModuleRocks.cs \ Gendarme.Framework.Rocks/TypeRocks.cs \ + Gendarme.Framework.Rocks/VariableDefinitionRocks.cs \ Gendarme.Framework/ApplicabilityScope.cs \ Gendarme.Framework/AssemblyResolver.cs \ Gendarme.Framework/BasicIgnoreList.cs \ @@ -94,6 +95,7 @@ framework_test_sources = \ Gendarme.Framework.Rocks/InstructionRocksTest.cs \ Gendarme.Framework.Rocks/MethodRocksTest.cs \ Gendarme.Framework.Rocks/TypeRocksTest.cs \ + Gendarme.Framework.Rocks/VariableDefinitionRocksTest.cs \ Gendarme.Framework.Rocks/FieldRocksTest.cs framework_test_build_sources = $(addprefix $(srcdir)/Test/, $(framework_test_sources)) diff --git a/gendarme/framework/Test/Gendarme.Framework.Rocks/ChangeLog b/gendarme/framework/Test/Gendarme.Framework.Rocks/ChangeLog index e9e96f12..14bb603a 100644 --- a/gendarme/framework/Test/Gendarme.Framework.Rocks/ChangeLog +++ b/gendarme/framework/Test/Gendarme.Framework.Rocks/ChangeLog @@ -1,3 +1,8 @@ +2009-04-30 Jesse Jones <jesjones@mindspring.com> + + * VariableDefinitionRocksTest.cs: Added tests for new + IsGeneratedName rock. + 2008-08-28 Sebastien Pouliot <sebastien@ximian.com> * InstructionRocksTest.cs: Add more test to cover all Is* rocks diff --git a/gendarme/framework/Test/Gendarme.Framework.Rocks/VariableDefinitionRocksTest.cs b/gendarme/framework/Test/Gendarme.Framework.Rocks/VariableDefinitionRocksTest.cs new file mode 100644 index 00000000..e69638cc --- /dev/null +++ b/gendarme/framework/Test/Gendarme.Framework.Rocks/VariableDefinitionRocksTest.cs @@ -0,0 +1,143 @@ +// +// Unit tests for InstructionRocks +// +// Authors: +// Sebastien Pouliot <sebastien@ximian.com> +// +// Copyright (C) 2008 Novell, Inc (http://www.novell.com) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +using Gendarme.Framework; +using Gendarme.Framework.Rocks; +using Mono.Cecil; +using Mono.Cecil.Cil; +using NUnit.Framework; +using System; +using System.IO; +using System.Linq; +using System.Text; + +namespace Test.Framework.Rocks { + + [TestFixture] + public class VariableDefinitionRocksTest { + + private TypeDefinition type_def; + + [TestFixtureSetUp] + public void FixtureSetUp () + { + string unit = System.Reflection.Assembly.GetExecutingAssembly ().Location; + AssemblyDefinition assembly = AssemblyFactory.GetAssembly (unit); + assembly.MainModule.LoadDebuggingSymbols (); + + type_def = assembly.MainModule.Types ["Test.Framework.Rocks.VariableDefinitionRocksTest"]; + } + + public string path; + public string host; + public string cachedLocalPath; + public string AbsolutePath; + + private string Unescape (string s) + { + return s; + } + + // This has one compiler generated local with gmcs. + private string Big () + { + if (cachedLocalPath != null) + return cachedLocalPath; + + bool windows = (path.Length > 3 && path [1] == ':' && + (path [2] == '\\' || path [2] == '/')); + + if (cachedLocalPath != null) { + string p = Unescape (path); + bool replace = windows; + if (replace) + cachedLocalPath = p.Replace ('/', '\\'); + else + cachedLocalPath = p; + } else { + if (path.Length > 1 && path [1] == ':') + cachedLocalPath = Unescape (path.Replace (Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar)); + + else if (System.IO.Path.DirectorySeparatorChar == '\\') { + string h = host; + if (path.Length > 0) { + if ((path.Length > 1) || (path [0] != '/')) { + h += path.Replace ('/', '\\'); + } + } + cachedLocalPath = "\\\\" + Unescape (h); + } else + cachedLocalPath = Unescape (path); + } + if (cachedLocalPath.Length == 0) + cachedLocalPath = Path.DirectorySeparatorChar.ToString (); + return cachedLocalPath; + } + + [Test] + public void BigTest () + { + MethodDefinition method = type_def.GetMethod ("Big"); + DoTest (method, "windows", "p", "replace", "h"); + } + + // This has two compiler generated locals with gmcs. + private void ForEach (string [] names) + { + foreach (string name in names) { + Console.WriteLine (name); + } + } + + [Test] + public void ForEachTest () + { + MethodDefinition method = type_def.GetMethod ("ForEach"); + DoTest (method, "name"); + } + + private void DoTest (MethodDefinition method, params string [] userNames) + { + int count = 0; + + foreach (Instruction ins in method.Body.Instructions) { + VariableDefinition v = ins.GetVariable (method); + if (v != null) { + bool userName = userNames.Any (n => n == v.Name); + if (userName) { + Assert.IsFalse (v.IsGeneratedName (), "{0} was reported as a generated name", v.Name); + } else { + ++count; + Assert.IsTrue (v.IsGeneratedName (), "{0} was not reported as a generated name", v.Name); + } + } + } + + if (count == 0) + Assert.Fail ("Didn't find any generated locals for VariableDefinitionRocksTest::{0}", method.Name); + } + } +} |