Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
c8d684d2
R
roslyn
项目概览
lwm1986
/
roslyn
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
roslyn
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
c8d684d2
编写于
10月 26, 2015
作者:
J
John Hamby
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Moved system language analyzers to separate files.
上级
d70a522f
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
147 addition
and
137 deletion
+147
-137
src/Compilers/CSharp/Test/Semantic/Diagnostics/OperationAnalyzerTests.cs
...Sharp/Test/Semantic/Diagnostics/OperationAnalyzerTests.cs
+7
-7
src/Compilers/Core/CodeAnalysisTest/CodeAnalysisTest.csproj
src/Compilers/Core/CodeAnalysisTest/CodeAnalysisTest.csproj
+4
-3
src/Compilers/Core/CodeAnalysisTest/Diagnostics/EmptyArrayAnalyzer.cs
...s/Core/CodeAnalysisTest/Diagnostics/EmptyArrayAnalyzer.cs
+5
-6
src/Compilers/Core/CodeAnalysisTest/Diagnostics/FieldCouldBeReadOnlyAnalyzer.cs
...eAnalysisTest/Diagnostics/FieldCouldBeReadOnlyAnalyzer.cs
+0
-114
src/Compilers/Core/CodeAnalysisTest/Diagnostics/LocalCouldBeConstAnalyzer.cs
...CodeAnalysisTest/Diagnostics/LocalCouldBeConstAnalyzer.cs
+124
-0
src/Compilers/VisualBasic/Test/Semantic/Diagnostics/OperationAnalyzerTests.vb
...Basic/Test/Semantic/Diagnostics/OperationAnalyzerTests.vb
+7
-7
未找到文件。
src/Compilers/CSharp/Test/Semantic/Diagnostics/OperationAnalyzerTests.cs
浏览文件 @
c8d684d2
...
...
@@ -34,13 +34,13 @@ void M1()
}"
;
CreateCompilationWithMscorlib45
(
source
)
.
VerifyDiagnostics
()
.
VerifyAnalyzerDiagnostics
(
new
DiagnosticAnalyzer
[]
{
new
EmptyArray
Operation
Analyzer
()
},
null
,
null
,
false
,
Diagnostic
(
EmptyArray
Operation
Analyzer
.
UseArrayEmptyDescriptor
.
Id
,
"new int[0]"
).
WithLocation
(
6
,
22
),
Diagnostic
(
EmptyArray
Operation
Analyzer
.
UseArrayEmptyDescriptor
.
Id
,
"{ }"
).
WithLocation
(
7
,
23
),
Diagnostic
(
EmptyArray
Operation
Analyzer
.
UseArrayEmptyDescriptor
.
Id
,
"new C[] { }"
).
WithLocation
(
8
,
20
),
Diagnostic
(
EmptyArray
Operation
Analyzer
.
UseArrayEmptyDescriptor
.
Id
,
"new int[0][]"
).
WithLocation
(
12
,
24
),
Diagnostic
(
EmptyArray
Operation
Analyzer
.
UseArrayEmptyDescriptor
.
Id
,
"new int[0][][][]"
).
WithLocation
(
13
,
28
),
Diagnostic
(
EmptyArray
Operation
Analyzer
.
UseArrayEmptyDescriptor
.
Id
,
"new int[0][,]"
).
WithLocation
(
15
,
26
)
.
VerifyAnalyzerDiagnostics
(
new
DiagnosticAnalyzer
[]
{
new
EmptyArrayAnalyzer
()
},
null
,
null
,
false
,
Diagnostic
(
EmptyArrayAnalyzer
.
UseArrayEmptyDescriptor
.
Id
,
"new int[0]"
).
WithLocation
(
6
,
22
),
Diagnostic
(
EmptyArrayAnalyzer
.
UseArrayEmptyDescriptor
.
Id
,
"{ }"
).
WithLocation
(
7
,
23
),
Diagnostic
(
EmptyArrayAnalyzer
.
UseArrayEmptyDescriptor
.
Id
,
"new C[] { }"
).
WithLocation
(
8
,
20
),
Diagnostic
(
EmptyArrayAnalyzer
.
UseArrayEmptyDescriptor
.
Id
,
"new int[0][]"
).
WithLocation
(
12
,
24
),
Diagnostic
(
EmptyArrayAnalyzer
.
UseArrayEmptyDescriptor
.
Id
,
"new int[0][][][]"
).
WithLocation
(
13
,
28
),
Diagnostic
(
EmptyArrayAnalyzer
.
UseArrayEmptyDescriptor
.
Id
,
"new int[0][,]"
).
WithLocation
(
15
,
26
)
);
}
...
...
src/Compilers/Core/CodeAnalysisTest/CodeAnalysisTest.csproj
浏览文件 @
c8d684d2
...
...
@@ -29,9 +29,10 @@
<Compile
Include=
"Collections\ByteSequenceComparerTests.cs"
/>
<Compile
Include=
"Diagnostics\BoxingOperationAnalyzer.cs"
/>
<Compile
Include=
"Diagnostics\DiagnosticLocalizationTests.cs"
/>
<Compile
Include=
"Diagnostics\EmptyArrayOperationAnalyzer.cs"
/>
<Compile
Include=
"Diagnostics\EmptyArrayAnalyzer.cs"
/>
<Compile
Include=
"Diagnostics\LocalCouldBeConstAnalyzer.cs"
/>
<Compile
Include=
"Diagnostics\OperationTestAnalyzer.cs"
/>
<Compile
Include=
"Diagnostics\
SystemLanguage
Analyzer.cs"
/>
<Compile
Include=
"Diagnostics\
FieldCouldBeReadOnly
Analyzer.cs"
/>
<Compile
Include=
"Emit\EmitOptionsTests.cs"
/>
<Compile
Include=
"Emit\CustomDebugInfoTests.cs"
/>
<Compile
Include=
"InternalUtilities\StreamExtensionsTests.cs"
/>
...
...
@@ -184,4 +185,4 @@
<Import
Project=
"..\..\..\..\build\Targets\VSL.Imports.targets"
/>
<Import
Project=
"..\..\..\..\build\Targets\Roslyn.Toolsets.Xunit.targets"
/>
</ImportGroup>
</Project>
</Project>
\ No newline at end of file
src/Compilers/Core/CodeAnalysisTest/Diagnostics/EmptyArray
Operation
Analyzer.cs
→
src/Compilers/Core/CodeAnalysisTest/Diagnostics/EmptyArrayAnalyzer.cs
浏览文件 @
c8d684d2
...
...
@@ -5,13 +5,12 @@
using
Microsoft.CodeAnalysis.Diagnostics
;
using
Microsoft.CodeAnalysis.Semantics
;
namespace
Microsoft.CodeAnalysis.UnitTests.Diagnostics
namespace
Microsoft.CodeAnalysis.UnitTests.Diagnostics
.SystemLanguage
{
/// <summary>
Base type for an a
nalyzer that looks for empty array allocations and recommends their replacement.</summary>
public
class
EmptyArray
Operation
Analyzer
:
DiagnosticAnalyzer
/// <summary>
A
nalyzer that looks for empty array allocations and recommends their replacement.</summary>
public
class
EmptyArrayAnalyzer
:
DiagnosticAnalyzer
{
/// <summary>Diagnostic category "Performance".</summary>
private
const
string
PerformanceCategory
=
"Performance"
;
private
const
string
SystemCategory
=
"System"
;
/// <summary>The name of the array type.</summary>
internal
const
string
ArrayTypeName
=
"System.Array"
;
// using instead of GetSpecialType to make more testable
...
...
@@ -27,7 +26,7 @@ public class EmptyArrayOperationAnalyzer : DiagnosticAnalyzer
"EmptyArrayRule"
,
localizableTitle
,
localizableMessage
,
Performance
Category
,
System
Category
,
DiagnosticSeverity
.
Warning
,
isEnabledByDefault
:
true
);
...
...
src/Compilers/Core/CodeAnalysisTest/Diagnostics/
SystemLanguage
Analyzer.cs
→
src/Compilers/Core/CodeAnalysisTest/Diagnostics/
FieldCouldBeReadOnly
Analyzer.cs
浏览文件 @
c8d684d2
...
...
@@ -132,118 +132,4 @@ void Report(CompilationAnalysisContext context, IFieldSymbol field, DiagnosticDe
context
.
ReportDiagnostic
(
Diagnostic
.
Create
(
descriptor
,
field
.
Locations
.
FirstOrDefault
()));
}
}
/// <summary>Analyzer used to identify local variables that could be declared Const.</summary>
public
class
LocalCouldBeConstAnalyzer
:
DiagnosticAnalyzer
{
private
const
string
SystemCategory
=
"System"
;
public
static
readonly
DiagnosticDescriptor
LocalCouldBeConstDescriptor
=
new
DiagnosticDescriptor
(
"LocalCouldBeReadOnly"
,
"Local Could Be Const"
,
"Local variable is never modified and so could be const."
,
SystemCategory
,
DiagnosticSeverity
.
Warning
,
isEnabledByDefault
:
true
);
/// <summary>Gets the set of supported diagnostic descriptors from this analyzer.</summary>
public
sealed
override
ImmutableArray
<
DiagnosticDescriptor
>
SupportedDiagnostics
{
get
{
return
ImmutableArray
.
Create
(
LocalCouldBeConstDescriptor
);
}
}
public
sealed
override
void
Initialize
(
AnalysisContext
context
)
{
context
.
RegisterOperationBlockStartAction
(
(
operationBlockContext
)
=>
{
IMethodSymbol
containingMethod
=
operationBlockContext
.
OwningSymbol
as
IMethodSymbol
;
if
(
containingMethod
!=
null
)
{
HashSet
<
ILocalSymbol
>
mightBecomeConstLocals
=
new
HashSet
<
ILocalSymbol
>();
HashSet
<
ILocalSymbol
>
assignedToLocals
=
new
HashSet
<
ILocalSymbol
>();
operationBlockContext
.
RegisterOperationAction
(
(
operationContext
)
=>
{
IAssignmentExpression
assignment
=
(
IAssignmentExpression
)
operationContext
.
Operation
;
AssignTo
(
assignment
.
Target
,
assignedToLocals
,
mightBecomeConstLocals
);
},
OperationKind
.
AssignmentExpression
,
OperationKind
.
CompoundAssignmentExpression
,
OperationKind
.
IncrementExpression
);
operationBlockContext
.
RegisterOperationAction
(
(
operationContext
)
=>
{
IInvocationExpression
invocation
=
(
IInvocationExpression
)
operationContext
.
Operation
;
foreach
(
IArgument
argument
in
invocation
.
ArgumentsInParameterOrder
)
{
if
(
argument
.
Parameter
.
RefKind
==
RefKind
.
Out
||
argument
.
Parameter
.
RefKind
==
RefKind
.
Ref
)
{
AssignTo
(
argument
.
Value
,
assignedToLocals
,
mightBecomeConstLocals
);
}
}
},
OperationKind
.
InvocationExpression
);
operationBlockContext
.
RegisterOperationAction
(
(
operationContext
)
=>
{
IVariableDeclarationStatement
declaration
=
(
IVariableDeclarationStatement
)
operationContext
.
Operation
;
foreach
(
IVariable
variable
in
declaration
.
Variables
)
{
ILocalSymbol
local
=
variable
.
Variable
;
if
(!
local
.
IsConst
)
{
var
localType
=
local
.
Type
;
if
((!
localType
.
IsReferenceType
||
localType
.
SpecialType
==
SpecialType
.
System_String
)
&&
localType
.
SpecialType
!=
SpecialType
.
None
)
{
if
(
variable
.
InitialValue
!=
null
&&
variable
.
InitialValue
.
ConstantValue
!=
null
)
{
mightBecomeConstLocals
.
Add
(
local
);
}
}
}
}
},
OperationKind
.
VariableDeclarationStatement
);
operationBlockContext
.
RegisterOperationBlockEndAction
(
(
operationBlockEndContext
)
=>
{
foreach
(
ILocalSymbol
couldBeConstLocal
in
mightBecomeConstLocals
)
{
Report
(
operationBlockEndContext
,
couldBeConstLocal
,
LocalCouldBeConstDescriptor
);
}
});
}
});
}
static
void
AssignTo
(
IExpression
target
,
HashSet
<
ILocalSymbol
>
assignedToLocals
,
HashSet
<
ILocalSymbol
>
mightBecomeConstLocals
)
{
if
(
target
.
Kind
==
OperationKind
.
LocalReferenceExpression
)
{
ILocalSymbol
targetLocal
=
((
ILocalReferenceExpression
)
target
).
Local
;
assignedToLocals
.
Add
(
targetLocal
);
mightBecomeConstLocals
.
Remove
(
targetLocal
);
}
else
if
(
target
.
Kind
==
OperationKind
.
FieldReferenceExpression
)
{
IFieldReferenceExpression
fieldReference
=
(
IFieldReferenceExpression
)
target
;
if
(
fieldReference
.
Instance
!=
null
&&
fieldReference
.
Instance
.
ResultType
.
IsValueType
)
{
AssignTo
(
fieldReference
.
Instance
,
assignedToLocals
,
mightBecomeConstLocals
);
}
}
}
void
Report
(
OperationBlockAnalysisContext
context
,
ILocalSymbol
local
,
DiagnosticDescriptor
descriptor
)
{
context
.
ReportDiagnostic
(
Diagnostic
.
Create
(
descriptor
,
local
.
Locations
.
FirstOrDefault
()));
}
}
}
\ No newline at end of file
src/Compilers/Core/CodeAnalysisTest/Diagnostics/LocalCouldBeConstAnalyzer.cs
0 → 100644
浏览文件 @
c8d684d2
// Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using
System.Collections.Generic
;
using
System.Collections.Immutable
;
using
System.Linq
;
using
Microsoft.CodeAnalysis.Diagnostics
;
using
Microsoft.CodeAnalysis.Semantics
;
namespace
Microsoft.CodeAnalysis.UnitTests.Diagnostics.SystemLanguage
{
/// <summary>Analyzer used to identify local variables that could be declared Const.</summary>
public
class
LocalCouldBeConstAnalyzer
:
DiagnosticAnalyzer
{
private
const
string
SystemCategory
=
"System"
;
public
static
readonly
DiagnosticDescriptor
LocalCouldBeConstDescriptor
=
new
DiagnosticDescriptor
(
"LocalCouldBeReadOnly"
,
"Local Could Be Const"
,
"Local variable is never modified and so could be const."
,
SystemCategory
,
DiagnosticSeverity
.
Warning
,
isEnabledByDefault
:
true
);
/// <summary>Gets the set of supported diagnostic descriptors from this analyzer.</summary>
public
sealed
override
ImmutableArray
<
DiagnosticDescriptor
>
SupportedDiagnostics
{
get
{
return
ImmutableArray
.
Create
(
LocalCouldBeConstDescriptor
);
}
}
public
sealed
override
void
Initialize
(
AnalysisContext
context
)
{
context
.
RegisterOperationBlockStartAction
(
(
operationBlockContext
)
=>
{
IMethodSymbol
containingMethod
=
operationBlockContext
.
OwningSymbol
as
IMethodSymbol
;
if
(
containingMethod
!=
null
)
{
HashSet
<
ILocalSymbol
>
mightBecomeConstLocals
=
new
HashSet
<
ILocalSymbol
>();
HashSet
<
ILocalSymbol
>
assignedToLocals
=
new
HashSet
<
ILocalSymbol
>();
operationBlockContext
.
RegisterOperationAction
(
(
operationContext
)
=>
{
IAssignmentExpression
assignment
=
(
IAssignmentExpression
)
operationContext
.
Operation
;
AssignTo
(
assignment
.
Target
,
assignedToLocals
,
mightBecomeConstLocals
);
},
OperationKind
.
AssignmentExpression
,
OperationKind
.
CompoundAssignmentExpression
,
OperationKind
.
IncrementExpression
);
operationBlockContext
.
RegisterOperationAction
(
(
operationContext
)
=>
{
IInvocationExpression
invocation
=
(
IInvocationExpression
)
operationContext
.
Operation
;
foreach
(
IArgument
argument
in
invocation
.
ArgumentsInParameterOrder
)
{
if
(
argument
.
Parameter
.
RefKind
==
RefKind
.
Out
||
argument
.
Parameter
.
RefKind
==
RefKind
.
Ref
)
{
AssignTo
(
argument
.
Value
,
assignedToLocals
,
mightBecomeConstLocals
);
}
}
},
OperationKind
.
InvocationExpression
);
operationBlockContext
.
RegisterOperationAction
(
(
operationContext
)
=>
{
IVariableDeclarationStatement
declaration
=
(
IVariableDeclarationStatement
)
operationContext
.
Operation
;
foreach
(
IVariable
variable
in
declaration
.
Variables
)
{
ILocalSymbol
local
=
variable
.
Variable
;
if
(!
local
.
IsConst
&&
!
assignedToLocals
.
Contains
(
local
))
{
var
localType
=
local
.
Type
;
if
((!
localType
.
IsReferenceType
||
localType
.
SpecialType
==
SpecialType
.
System_String
)
&&
localType
.
SpecialType
!=
SpecialType
.
None
)
{
if
(
variable
.
InitialValue
!=
null
&&
variable
.
InitialValue
.
ConstantValue
!=
null
)
{
mightBecomeConstLocals
.
Add
(
local
);
}
}
}
}
},
OperationKind
.
VariableDeclarationStatement
);
operationBlockContext
.
RegisterOperationBlockEndAction
(
(
operationBlockEndContext
)
=>
{
foreach
(
ILocalSymbol
couldBeConstLocal
in
mightBecomeConstLocals
)
{
Report
(
operationBlockEndContext
,
couldBeConstLocal
,
LocalCouldBeConstDescriptor
);
}
});
}
});
}
static
void
AssignTo
(
IExpression
target
,
HashSet
<
ILocalSymbol
>
assignedToLocals
,
HashSet
<
ILocalSymbol
>
mightBecomeConstLocals
)
{
if
(
target
.
Kind
==
OperationKind
.
LocalReferenceExpression
)
{
ILocalSymbol
targetLocal
=
((
ILocalReferenceExpression
)
target
).
Local
;
assignedToLocals
.
Add
(
targetLocal
);
mightBecomeConstLocals
.
Remove
(
targetLocal
);
}
else
if
(
target
.
Kind
==
OperationKind
.
FieldReferenceExpression
)
{
IFieldReferenceExpression
fieldReference
=
(
IFieldReferenceExpression
)
target
;
if
(
fieldReference
.
Instance
!=
null
&&
fieldReference
.
Instance
.
ResultType
.
IsValueType
)
{
AssignTo
(
fieldReference
.
Instance
,
assignedToLocals
,
mightBecomeConstLocals
);
}
}
}
void
Report
(
OperationBlockAnalysisContext
context
,
ILocalSymbol
local
,
DiagnosticDescriptor
descriptor
)
{
context
.
ReportDiagnostic
(
Diagnostic
.
Create
(
descriptor
,
local
.
Locations
.
FirstOrDefault
()));
}
}
}
\ No newline at end of file
src/Compilers/VisualBasic/Test/Semantic/Diagnostics/OperationAnalyzerTests.vb
浏览文件 @
c8d684d2
...
...
@@ -39,13 +39,13 @@ End Class
Dim
comp
=
CompilationUtils
.
CreateCompilationWithMscorlibAndVBRuntime
(
source
)
comp
.
VerifyDiagnostics
()
comp
.
VerifyAnalyzerDiagnostics
({
New
EmptyArray
Operation
Analyzer
},
Nothing
,
Nothing
,
False
,
Diagnostic
(
EmptyArray
Operation
Analyzer
.
UseArrayEmptyDescriptor
.
Id
,
"New Integer(-1) { }"
).
WithLocation
(
3
,
33
),
Diagnostic
(
EmptyArray
Operation
Analyzer
.
UseArrayEmptyDescriptor
.
Id
,
"{ }"
).
WithLocation
(
4
,
30
),
Diagnostic
(
EmptyArray
Operation
Analyzer
.
UseArrayEmptyDescriptor
.
Id
,
"New C(-1) { }"
).
WithLocation
(
5
,
27
),
Diagnostic
(
EmptyArray
Operation
Analyzer
.
UseArrayEmptyDescriptor
.
Id
,
"New Integer(-1)() { }"
).
WithLocation
(
9
,
35
),
Diagnostic
(
EmptyArray
Operation
Analyzer
.
UseArrayEmptyDescriptor
.
Id
,
"New Integer( -1)()()() { }"
).
WithLocation
(
10
,
39
),
Diagnostic
(
EmptyArray
Operation
Analyzer
.
UseArrayEmptyDescriptor
.
Id
,
"New Integer(-1)(,) { }"
).
WithLocation
(
12
,
37
))
comp
.
VerifyAnalyzerDiagnostics
({
New
EmptyArrayAnalyzer
},
Nothing
,
Nothing
,
False
,
Diagnostic
(
EmptyArrayAnalyzer
.
UseArrayEmptyDescriptor
.
Id
,
"New Integer(-1) { }"
).
WithLocation
(
3
,
33
),
Diagnostic
(
EmptyArrayAnalyzer
.
UseArrayEmptyDescriptor
.
Id
,
"{ }"
).
WithLocation
(
4
,
30
),
Diagnostic
(
EmptyArrayAnalyzer
.
UseArrayEmptyDescriptor
.
Id
,
"New C(-1) { }"
).
WithLocation
(
5
,
27
),
Diagnostic
(
EmptyArrayAnalyzer
.
UseArrayEmptyDescriptor
.
Id
,
"New Integer(-1)() { }"
).
WithLocation
(
9
,
35
),
Diagnostic
(
EmptyArrayAnalyzer
.
UseArrayEmptyDescriptor
.
Id
,
"New Integer( -1)()()() { }"
).
WithLocation
(
10
,
39
),
Diagnostic
(
EmptyArrayAnalyzer
.
UseArrayEmptyDescriptor
.
Id
,
"New Integer(-1)(,) { }"
).
WithLocation
(
12
,
37
))
End
Sub
<
Fact
>
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录