Browse Source

Ensure we pass the correct type argument to GatherVisitorBase<>.

pull/45/merge
Daniel Grunwald 12 years ago
parent
commit
1cec2e62f8
  1. 10
      ICSharpCode.NRefactory.CSharp/Ast/AstNode.cs
  2. 11
      ICSharpCode.NRefactory.CSharp/Ast/AstNodeCollection.cs
  3. 2
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/DuplicateExpressionsInConditionsIssue.cs
  4. 2
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/DuplicateIfInIfChainIssue.cs
  5. 2
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/ParameterCanBeDemotedIssue/ParameterCanBeIEnumerableIssue.cs
  6. 2
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/RedundantBaseConstructorIssue.cs
  7. 2
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/UseBlockInsteadColonIssue.cs
  8. 51
      ICSharpCode.NRefactory.Tests/CSharp/Refactoring/RefactoringStructureTests.cs
  9. 1
      ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj
  10. 2
      ICSharpCode.NRefactory/Role.cs

10
ICSharpCode.NRefactory.CSharp/Ast/AstNode.cs

@ -230,6 +230,10 @@ namespace ICSharpCode.NRefactory.CSharp @@ -230,6 +230,10 @@ namespace ICSharpCode.NRefactory.CSharp
}
}
internal uint RoleIndex {
get { return flags & roleIndexMask; }
}
void SetRole(Role role)
{
flags = (flags & ~roleIndexMask) | role.Index;
@ -293,14 +297,14 @@ namespace ICSharpCode.NRefactory.CSharp @@ -293,14 +297,14 @@ namespace ICSharpCode.NRefactory.CSharp
}
/// <summary>
/// Gets all descendants of this node (excluding this node itself).
/// Gets all descendants of this node (excluding this node itself) in pre-order.
/// </summary>
public IEnumerable<AstNode> Descendants {
get { return GetDescendantsImpl(false); }
}
/// <summary>
/// Gets all descendants of this node (including this node itself).
/// Gets all descendants of this node (including this node itself) in pre-order.
/// </summary>
public IEnumerable<AstNode> DescendantsAndSelf {
get { return GetDescendantsImpl(true); }
@ -347,6 +351,8 @@ namespace ICSharpCode.NRefactory.CSharp @@ -347,6 +351,8 @@ namespace ICSharpCode.NRefactory.CSharp
nextStack.Push(null);
AstNode pos = firstChild;
while (pos != null) {
// Remember next before yielding pos.
// This allows removing/replacing nodes while iterating through the list.
if (pos.nextSibling != null)
nextStack.Push(pos.nextSibling);
if (IsInsideRegion(region, pos))

11
ICSharpCode.NRefactory.CSharp/Ast/AstNodeCollection.cs

@ -50,8 +50,9 @@ namespace ICSharpCode.NRefactory.CSharp @@ -50,8 +50,9 @@ namespace ICSharpCode.NRefactory.CSharp
public int Count {
get {
int count = 0;
uint roleIndex = role.Index;
for (AstNode cur = node.FirstChild; cur != null; cur = cur.NextSibling) {
if (cur.Role == role)
if (cur.RoleIndex == roleIndex)
count++;
}
return count;
@ -107,7 +108,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -107,7 +108,7 @@ namespace ICSharpCode.NRefactory.CSharp
public bool Contains(T element)
{
return element != null && element.Parent == node && element.Role == role;
return element != null && element.Parent == node && element.RoleIndex == role.Index;
}
public bool Remove(T element)
@ -163,13 +164,14 @@ namespace ICSharpCode.NRefactory.CSharp @@ -163,13 +164,14 @@ namespace ICSharpCode.NRefactory.CSharp
public IEnumerator<T> GetEnumerator()
{
uint roleIndex = role.Index;
AstNode next;
for (AstNode cur = node.FirstChild; cur != null; cur = next) {
Debug.Assert(cur.Parent == node);
// Remember next before yielding cur.
// This allows removing/replacing nodes while iterating through the list.
next = cur.NextSibling;
if (cur.Role == role)
if (cur.RoleIndex == roleIndex)
yield return (T)cur;
}
}
@ -214,13 +216,14 @@ namespace ICSharpCode.NRefactory.CSharp @@ -214,13 +216,14 @@ namespace ICSharpCode.NRefactory.CSharp
/// </summary>
public void AcceptVisitor(IAstVisitor visitor)
{
uint roleIndex = role.Index;
AstNode next;
for (AstNode cur = node.FirstChild; cur != null; cur = next) {
Debug.Assert(cur.Parent == node);
// Remember next before yielding cur.
// This allows removing/replacing nodes while iterating through the list.
next = cur.NextSibling;
if (cur.Role == role)
if (cur.RoleIndex == roleIndex)
cur.AcceptVisitor(visitor);
}
}

2
ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/DuplicateExpressionsInConditionsIssue.cs

@ -52,7 +52,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -52,7 +52,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
SupportedOperators.Add(BinaryOperatorType.ConditionalOr);
}
class GatherVisitor : GatherVisitorBase<IdenticalConditionalBranchIssue>
class GatherVisitor : GatherVisitorBase<DuplicateExpressionsInConditionsIssue>
{
public GatherVisitor (BaseRefactoringContext ctx)
: base (ctx)

2
ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/DuplicateIfInIfChainIssue.cs

@ -43,7 +43,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -43,7 +43,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
return new GatherVisitor (context).GetIssues ();
}
class GatherVisitor : GatherVisitorBase<IdenticalConditionalBranchIssue>
class GatherVisitor : GatherVisitorBase<DuplicateIfInIfChainIssue>
{
public GatherVisitor (BaseRefactoringContext ctx)
: base (ctx)

2
ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/ParameterCanBeDemotedIssue/ParameterCanBeIEnumerableIssue.cs

@ -59,7 +59,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -59,7 +59,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
}
#endregion
class GatherVisitor : GatherVisitorBase<ParameterCanBeDemotedIssue>
class GatherVisitor : GatherVisitorBase<ParameterCanBeIEnumerableIssue>
{
bool tryResolve;

2
ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/RedundantBaseConstructorIssue.cs

@ -41,7 +41,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -41,7 +41,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
return new GatherVisitor(context).GetIssues();
}
class GatherVisitor : GatherVisitorBase<RedundantAttributeParenthesesIssue>
class GatherVisitor : GatherVisitorBase<RedundantBaseConstructorIssue>
{
public GatherVisitor(BaseRefactoringContext ctx)
: base (ctx)

2
ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/UseBlockInsteadColonIssue.cs

@ -42,7 +42,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -42,7 +42,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
return new GatherVisitor (context).GetIssues ();
}
class GatherVisitor : GatherVisitorBase<IdenticalConditionalBranchIssue>
class GatherVisitor : GatherVisitorBase<UseBlockInsteadColonIssue>
{
private readonly string _commandTitle;

51
ICSharpCode.NRefactory.Tests/CSharp/Refactoring/RefactoringStructureTests.cs

@ -0,0 +1,51 @@ @@ -0,0 +1,51 @@
// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
//
// 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 System;
using System.Reflection;
using NUnit.Framework;
namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
[TestFixture]
public class RefactoringStructureTests
{
[Test]
public void GatherVisitorBaseClass()
{
Assembly NR_CSharp = typeof(ICodeIssueProvider).Assembly;
bool foundGatherVisitor = false;
foreach (var topLevelType in NR_CSharp.GetTypes()) {
foreach (var nestedType in topLevelType.GetNestedTypes(BindingFlags.NonPublic | BindingFlags.Public)) {
if (nestedType.Name != "GatherVisitor")
continue;
foundGatherVisitor = true;
bool foundGatherVisitorBase = false;
for (Type baseType = nestedType.BaseType; baseType != null; baseType = baseType.BaseType) {
if (baseType.Name == "GatherVisitorBase`1") {
foundGatherVisitorBase = true;
Assert.AreEqual(new[] { topLevelType }, baseType.GetGenericArguments(), "Invalid base type of " + nestedType.FullName);
}
}
Assert.IsTrue(foundGatherVisitorBase, nestedType.FullName + " should derive from GatherVisitorBase");
}
}
Assert.IsTrue(foundGatherVisitor, "where are the gather visitors?");
}
}
}

1
ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj

@ -226,6 +226,7 @@ @@ -226,6 +226,7 @@
<Compile Include="CSharp\Parser\GeneralScope\AttributeSectionTests.cs" />
<Compile Include="CSharp\Parser\ParseUtil.cs" />
<Compile Include="CSharp\QueryExpressionExpanderTests.cs" />
<Compile Include="CSharp\Refactoring\RefactoringStructureTests.cs" />
<Compile Include="CSharp\Refactoring\TypeSystemAstBuilderTests.cs" />
<Compile Include="CSharp\Resolver\AnonymousTypeTests.cs" />
<Compile Include="CSharp\Resolver\ArrayCreateTests.cs" />

2
ICSharpCode.NRefactory/Role.cs

@ -43,7 +43,7 @@ namespace ICSharpCode.NRefactory @@ -43,7 +43,7 @@ namespace ICSharpCode.NRefactory
{
this.index = (uint)Interlocked.Increment(ref nextRoleIndex);
if (this.index >= roles.Length)
throw new InvalidOperationException("");
throw new InvalidOperationException("Too many roles");
roles[this.index] = this;
}

Loading…
Cancel
Save