Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper for source generators to filter to nodes with attribute matching an attribute name #61293

Merged
merged 157 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
157 commits
Select commit Hold shift + click to select a range
af00fad
Add helper for source generators to filter to nodes with attribute ma…
CyrusNajmabadi May 12, 2022
400f3b6
Simplify
CyrusNajmabadi May 12, 2022
c396037
Doc
CyrusNajmabadi May 12, 2022
efa3873
Add docs
CyrusNajmabadi May 13, 2022
54fc5d0
Add tracking names
CyrusNajmabadi May 13, 2022
6fe27f3
Simplify
CyrusNajmabadi May 13, 2022
e5149f5
Operate on attribute lists
CyrusNajmabadi May 13, 2022
e007656
Validate argument
CyrusNajmabadi May 13, 2022
51ec0a0
Add tests
CyrusNajmabadi May 13, 2022
ab8cb20
Add tests
CyrusNajmabadi May 13, 2022
31741bd
Add tests
CyrusNajmabadi May 13, 2022
f03429c
Add tests
CyrusNajmabadi May 13, 2022
886b754
Add tests
CyrusNajmabadi May 13, 2022
27e76f5
Temp
CyrusNajmabadi May 13, 2022
2da2eb1
Add tests
CyrusNajmabadi May 13, 2022
72b0373
Crash in driver
CyrusNajmabadi May 13, 2022
9524986
rename
CyrusNajmabadi May 13, 2022
b1bec44
Add semantic check
CyrusNajmabadi May 13, 2022
64c2545
Update tracking name
CyrusNajmabadi May 13, 2022
83cc7f3
Merge remote-tracking branch 'upstream/update-runstep-table' into att…
CyrusNajmabadi May 13, 2022
da3b9f5
Add test
CyrusNajmabadi May 13, 2022
c6dfaa1
REname tests
CyrusNajmabadi May 13, 2022
cca9aa7
add tests
CyrusNajmabadi May 13, 2022
9020e0c
add tests
CyrusNajmabadi May 13, 2022
00ebabf
REname tests
CyrusNajmabadi May 13, 2022
cd9fa46
Add tests
CyrusNajmabadi May 13, 2022
9881c0c
Cleanup
CyrusNajmabadi May 13, 2022
099d600
Update tests
CyrusNajmabadi May 13, 2022
af2d9e0
Update src/Compilers/CSharp/Portable/SourceGeneration/IncrementalGene…
CyrusNajmabadi May 16, 2022
a36f557
Update src/Compilers/CSharp/Portable/SourceGeneration/IncrementalGene…
CyrusNajmabadi May 16, 2022
55c2079
Fix docs
CyrusNajmabadi May 16, 2022
28567c3
Add grouping semantics
CyrusNajmabadi May 16, 2022
ffceb65
Merge branch 'attributeLookup' of https://github.com/CyrusNajmabadi/r…
CyrusNajmabadi May 16, 2022
4803e2e
Add comment
CyrusNajmabadi May 16, 2022
df5f1f9
Make lambdas static where possible
CyrusNajmabadi May 16, 2022
f570e3d
Clarify docs
CyrusNajmabadi May 16, 2022
7640c50
Merge remote-tracking branch 'upstream/main' into attributeLookup
CyrusNajmabadi May 16, 2022
afc4144
rename test
CyrusNajmabadi May 16, 2022
d595ec4
Add tests
CyrusNajmabadi May 16, 2022
539e519
Add tests
CyrusNajmabadi May 17, 2022
e2d1a4b
Add test
CyrusNajmabadi May 17, 2022
eb67b4f
add test
CyrusNajmabadi May 17, 2022
00fa78e
Add support for nested types
CyrusNajmabadi May 17, 2022
c5e1c1e
Add tests
CyrusNajmabadi May 17, 2022
9a37965
Add tests
CyrusNajmabadi May 17, 2022
c936714
Merge remote-tracking branch 'upstream/main' into attributeLookup
CyrusNajmabadi May 19, 2022
3b995cc
Move code down
CyrusNajmabadi May 19, 2022
b32f00c
Share more syntax
CyrusNajmabadi May 19, 2022
aa07150
Merge remote-tracking branch 'upstream/main' into attributeLookup
CyrusNajmabadi May 19, 2022
568cf4a
Update
CyrusNajmabadi May 19, 2022
6bffef9
Delete
CyrusNajmabadi May 19, 2022
abbfcfb
Flesh out VB support, including global options
CyrusNajmabadi May 19, 2022
5daed61
Initial port of vb tests
CyrusNajmabadi May 19, 2022
b61eb4f
another pass over tests
CyrusNajmabadi May 19, 2022
832329e
Building
CyrusNajmabadi May 20, 2022
d1d80bc
Get passing
CyrusNajmabadi May 20, 2022
e561ce0
Get passing
CyrusNajmabadi May 20, 2022
5c2f3fd
Get passing
CyrusNajmabadi May 20, 2022
4a75439
Get passing
CyrusNajmabadi May 20, 2022
89e8a80
Get passing
CyrusNajmabadi May 20, 2022
5ae1b48
Get passing
CyrusNajmabadi May 20, 2022
f16b69f
Get passing
CyrusNajmabadi May 20, 2022
a14f48c
Add tests
CyrusNajmabadi May 20, 2022
1527bd6
Add tests
CyrusNajmabadi May 20, 2022
374d965
Remove
CyrusNajmabadi May 20, 2022
d46ac30
Move extension
CyrusNajmabadi May 23, 2022
7d05e27
Use comparer
CyrusNajmabadi May 23, 2022
8e6aad3
Add new compilationoptions provider for better caching
CyrusNajmabadi May 23, 2022
ba4faf1
Make public
CyrusNajmabadi May 23, 2022
f44993c
Add benchmark
CyrusNajmabadi May 23, 2022
5d656c5
Cleanup
CyrusNajmabadi May 23, 2022
fc14b27
Merge branch 'delayModel' into attributeLookup
CyrusNajmabadi May 23, 2022
95a5842
In progress
CyrusNajmabadi May 23, 2022
b06b09d
Tweak API
CyrusNajmabadi May 23, 2022
73449d2
Make instance methods
CyrusNajmabadi May 23, 2022
ebbd988
lint
CyrusNajmabadi May 23, 2022
0515b15
rename
CyrusNajmabadi May 23, 2022
06ee6fb
Add to public api
CyrusNajmabadi May 24, 2022
9df89ff
make nested function
CyrusNajmabadi May 24, 2022
c976c14
inline
CyrusNajmabadi May 24, 2022
e0d89ce
Merge remote-tracking branch 'upstream/main' into attributeLookup
CyrusNajmabadi May 24, 2022
567f3dc
Merge remote-tracking branch 'upstream/main' into attributeLookup
CyrusNajmabadi May 24, 2022
578fe4c
Make internal
CyrusNajmabadi May 24, 2022
de24258
Naming consistency
CyrusNajmabadi May 24, 2022
a7ab72e
Make internal
CyrusNajmabadi May 24, 2022
e7fa0a5
Update src/Compilers/Core/Portable/SourceGeneration/GeneratorContexts.cs
CyrusNajmabadi May 24, 2022
2345d17
Update src/Compilers/Core/Portable/SourceGeneration/GeneratorContexts.cs
CyrusNajmabadi May 24, 2022
34905e1
Simplify
CyrusNajmabadi May 24, 2022
c0ca036
Merge branch 'attributeLookup' of https://github.com/CyrusNajmabadi/r…
CyrusNajmabadi May 24, 2022
6584893
Merge remote-tracking branch 'upstream/main' into attributeLookup
CyrusNajmabadi May 25, 2022
8d67dfd
Update src/Compilers/CSharp/Portable/SourceGeneration/CSharpSyntaxHel…
CyrusNajmabadi May 25, 2022
f9380f5
Merge branch 'attributeLookup' of https://github.com/CyrusNajmabadi/r…
CyrusNajmabadi May 26, 2022
71fe40b
Merge remote-tracking branch 'upstream/main' into attributeLookup
CyrusNajmabadi May 27, 2022
1c212ac
API review feedback
CyrusNajmabadi May 27, 2022
9ea52f2
Add tests for assembly attributes
CyrusNajmabadi May 27, 2022
4939920
Add vb support
CyrusNajmabadi May 27, 2022
6e64f40
Simplify name
CyrusNajmabadi May 27, 2022
e0524f9
Update
CyrusNajmabadi May 27, 2022
f1e94e2
ad ddocs
CyrusNajmabadi May 27, 2022
b7391e9
newline
CyrusNajmabadi May 27, 2022
5a0577a
SImplify
CyrusNajmabadi May 27, 2022
ff6c5ab
Add test for local function
CyrusNajmabadi May 27, 2022
3585e13
Add test
CyrusNajmabadi May 27, 2022
7d2b9d3
Add test
CyrusNajmabadi May 27, 2022
6971f38
Add support for lambdas
CyrusNajmabadi May 27, 2022
dfe5cbf
Add support for return: attributes
CyrusNajmabadi May 27, 2022
fc73d8b
Add support for fields
CyrusNajmabadi May 27, 2022
95aa4f2
Add vb support
CyrusNajmabadi May 27, 2022
2615059
Add test
CyrusNajmabadi May 27, 2022
50d5113
Simplify
CyrusNajmabadi May 27, 2022
a85ea10
Merge remote-tracking branch 'upstream/main' into attributeLookup
CyrusNajmabadi May 31, 2022
a85e986
Update comment
CyrusNajmabadi May 31, 2022
0a8e287
Merge branch 'attributeLookup' of https://github.com/CyrusNajmabadi/r…
CyrusNajmabadi Jun 1, 2022
c89574f
Merge remote-tracking branch 'upstream/main' into attributeLookup
CyrusNajmabadi Jun 1, 2022
9a4a3b1
Update src/Compilers/Core/Portable/SourceGeneration/GlobalAliases.cs
CyrusNajmabadi Jun 2, 2022
9868e88
Update src/Compilers/Core/Portable/SourceGeneration/GlobalAliases.cs
CyrusNajmabadi Jun 2, 2022
37f9bcc
Merge remote-tracking branch 'upstream/main' into attributeLookup
CyrusNajmabadi Jun 2, 2022
1b33937
Simplify
CyrusNajmabadi Jun 2, 2022
dc807ce
Simplify
CyrusNajmabadi Jun 2, 2022
b7722d5
Include target symbol in result
CyrusNajmabadi Jun 3, 2022
0714d39
remove console writelines :(
CyrusNajmabadi Jun 3, 2022
6b15f4c
Fix api
CyrusNajmabadi Jun 3, 2022
d3714a5
Make API public now that it has been approved
CyrusNajmabadi Jun 3, 2022
a01886c
Add negative tests
CyrusNajmabadi Jun 3, 2022
2261ec0
Merge remote-tracking branch 'upstream/main' into attributeLookup
CyrusNajmabadi Jun 3, 2022
99a9d5d
Use theory in tests
CyrusNajmabadi Jun 3, 2022
8645c6c
Use single instead of first
CyrusNajmabadi Jun 3, 2022
2584c0a
Add more test cases
CyrusNajmabadi Jun 3, 2022
869f4df
Add more test cases
CyrusNajmabadi Jun 3, 2022
ff2f71c
Add more test cases
CyrusNajmabadi Jun 3, 2022
a43c562
Add tests
CyrusNajmabadi Jun 3, 2022
056b978
Update src/Compilers/Core/Portable/SourceGeneration/IncrementalGenera…
CyrusNajmabadi Jun 6, 2022
58509e7
Merge remote-tracking branch 'upstream/main' into attributeLookup
CyrusNajmabadi Jun 7, 2022
5562727
Add tests
CyrusNajmabadi Jun 7, 2022
353997d
Add tests
CyrusNajmabadi Jun 7, 2022
306e9c2
Add tests
CyrusNajmabadi Jun 7, 2022
6f9db7c
Fix tests
CyrusNajmabadi Jun 7, 2022
b9c93e0
Make tests more precise
CyrusNajmabadi Jun 7, 2022
4dae540
Merge tests
CyrusNajmabadi Jun 7, 2022
7dc6149
Indentation
CyrusNajmabadi Jun 7, 2022
e1ad10c
indentation
CyrusNajmabadi Jun 7, 2022
275075a
indentation
CyrusNajmabadi Jun 7, 2022
ae9c7d6
Fix modules and add tests
CyrusNajmabadi Jun 7, 2022
80d2459
Add test for type parameters
CyrusNajmabadi Jun 7, 2022
f83bc73
use theories
CyrusNajmabadi Jun 7, 2022
ce58977
Add theories
CyrusNajmabadi Jun 7, 2022
ec332ba
Add theories
CyrusNajmabadi Jun 7, 2022
7412540
Add theories
CyrusNajmabadi Jun 7, 2022
c591e66
Add theories
CyrusNajmabadi Jun 7, 2022
3101731
Theories
CyrusNajmabadi Jun 7, 2022
c6ba1bc
Merge remote-tracking branch 'upstream/main' into attributeLookup
CyrusNajmabadi Jun 8, 2022
0cc8e7a
Use reference equality for compilation options comparisons
CyrusNajmabadi Jun 9, 2022
5f57e08
Merge remote-tracking branch 'upstream/main' into attributeLookup
CyrusNajmabadi Jun 14, 2022
6e27446
Move api
CyrusNajmabadi Jun 14, 2022
e566f05
Fix tests
CyrusNajmabadi Jun 14, 2022
5ec4786
Fix tests
CyrusNajmabadi Jun 14, 2022
303d0a1
lint
CyrusNajmabadi Jun 14, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

using System.Collections.Generic;
using System.Collections.Immutable;
using System.ComponentModel;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis.Diagnostics;
using System.Linq;
using System.ComponentModel;
using Microsoft.CodeAnalysis.SourceGeneration;

namespace Microsoft.CodeAnalysis.CSharp
{
Expand Down Expand Up @@ -74,5 +75,7 @@ internal override SyntaxTree ParseGeneratedSourceText(GeneratedSourceText input,
internal override CommonMessageProvider MessageProvider => CSharp.MessageProvider.Instance;

internal override string SourceExtension => ".cs";

internal override ISyntaxHelper SyntaxHelper => CSharpSyntaxHelper.Instance;
}
}
101 changes: 101 additions & 0 deletions src/Compilers/CSharp/Portable/SourceGeneration/CSharpSyntaxHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.CodeAnalysis.SourceGeneration;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Roslyn.Utilities;
using System.Diagnostics.CodeAnalysis;

namespace Microsoft.CodeAnalysis.CSharp
{
internal sealed class CSharpSyntaxHelper : AbstractSyntaxHelper
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similar to ISyntaxFacts in IDE. This allows us to have algorithms in the compiler layer that abstract over VB/C#.

{
public static readonly ISyntaxHelper Instance = new CSharpSyntaxHelper();

private CSharpSyntaxHelper()
{
}

public override bool IsCaseSensitive
=> true;

public override bool IsValidIdentifier(string name)
=> SyntaxFacts.IsValidIdentifier(name);

public override bool IsAnyNamespaceBlock(SyntaxNode node)
=> node is BaseNamespaceDeclarationSyntax;

public override bool IsAttribute(SyntaxNode node)
=> node is AttributeSyntax;

public override SyntaxNode GetNameOfAttribute(SyntaxNode node)
=> ((AttributeSyntax)node).Name;

public override bool IsAttributeList(SyntaxNode node)
=> node is AttributeListSyntax;

public override void AddAttributeTargets(SyntaxNode node, ArrayBuilder<SyntaxNode> targets)
{
var attributeList = (AttributeListSyntax)node;
var container = attributeList.Parent;
RoslynDebug.AssertNotNull(container);

// For fields/events, the attribute applies to all the variables declared.
if (container is FieldDeclarationSyntax field)
targets.AddRange(field.Declaration.Variables);
else if (container is EventFieldDeclarationSyntax ev)
targets.AddRange(ev.Declaration.Variables);
else
targets.Add(container);
}

public override SeparatedSyntaxList<SyntaxNode> GetAttributesOfAttributeList(SyntaxNode node)
=> ((AttributeListSyntax)node).Attributes;

public override bool IsLambdaExpression(SyntaxNode node)
=> node is LambdaExpressionSyntax;

public override SyntaxToken GetUnqualifiedIdentifierOfName(SyntaxNode node)
=> ((NameSyntax)node).GetUnqualifiedName().Identifier;

public override void AddAliases(SyntaxNode node, ArrayBuilder<(string aliasName, string symbolName)> aliases, bool global)
{
if (node is CompilationUnitSyntax compilationUnit)
{
AddAliases(compilationUnit.Usings, aliases, global);
}
else if (node is BaseNamespaceDeclarationSyntax namespaceDeclaration)
{
AddAliases(namespaceDeclaration.Usings, aliases, global);
}
else
{
throw ExceptionUtilities.UnexpectedValue(node.Kind());
}
}

private static void AddAliases(SyntaxList<UsingDirectiveSyntax> usings, ArrayBuilder<(string aliasName, string symbolName)> aliases, bool global)
{
foreach (var usingDirective in usings)
{
if (usingDirective.Alias is null)
continue;

if (global != usingDirective.GlobalKeyword.Kind() is SyntaxKind.GlobalKeyword)
continue;

var aliasName = usingDirective.Alias.Name.Identifier.ValueText;
var symbolName = usingDirective.Name.GetUnqualifiedName().Identifier.ValueText;
aliases.Add((aliasName, symbolName));
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: tehse helpers are very specific to the new API being added. Instead of calling this ISyntaxHelper, we could make this an interface specific to the attribute functionality being added. This is all internal, so i don't care much.


public override void AddAliases(CompilationOptions compilation, ArrayBuilder<(string aliasName, string symbolName)> aliases)
{
// C# doesn't have global aliases at the compilation level.
return;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,15 @@ private void VisitNamedTypeWithoutNullability(INamedTypeSymbol symbol)
if (IncludeNamedType(symbol.ContainingType))
{
symbol.ContainingType.Accept(this.NotFirstVisitor);
AddPunctuation(SyntaxKind.DotToken);

if (format.CompilerInternalOptions.HasFlag(SymbolDisplayCompilerInternalOptions.UsePlusForNestedTypes))
{
AddPunctuation(SyntaxKind.PlusToken);
}
else
{
AddPunctuation(SyntaxKind.DotToken);
}
}
}

Expand Down
Loading