Skip to content

Conversation

tamasvajk
Copy link
Contributor

No description provided.

@github-actions github-actions bot added the C# label Oct 20, 2020
@tamasvajk tamasvajk force-pushed the feature/attributes branch 3 times, most recently from 93c0351 to d211e4f Compare October 20, 2020 08:33
@tamasvajk tamasvajk changed the title Refactor attribute extraction C#: Refactor attribute extraction Oct 20, 2020
public class Class1
{
[CustomAttribute(42 + 0, new int[] { 1, 2, 3 }, typeof(Class1), Enum1.B, new int[] { 1, 2, 3 }, Prop2 = new object[] { 1, typeof(int) })]
[return: Custom(42 + 0, new int[] { 1, 2, 3 }, null, Enum1.A, new int[] { 1, 2, 3 }, Prop2 = new object[] { 1, typeof(int) })]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: we're not extracting this attribute.

using System;

[assembly: Assembly1.CustomAttribute(1, new object[] { 1, 2, null }, typeof(Assembly1.CustomAttribute), (Assembly1.Enum1)12, null, Prop2 = new object[] { 1, typeof(int) })]
[module: Assembly1.CustomAttribute(2, new object[] { 1, 2, null }, typeof(Assembly1.CustomAttribute), (Assembly1.Enum1)12, null, Prop2 = new object[] { 1, typeof(int) })]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We're extracting module: as if it was assembly:

@tamasvajk tamasvajk force-pushed the feature/attributes branch 3 times, most recently from 1a46cb9 to a10da96 Compare October 20, 2020 12:54
@tamasvajk
Copy link
Contributor Author

Comment on lines 60 to 81
var outputAssembly = Assembly.CreateOutputAssembly(cx);
var attributeDatas = cx.Compilation.Assembly.GetAttributes().ToList();
attributeDatas.AddRange(cx.Compilation.Assembly.Modules.SelectMany(m => m.GetAttributes()));
foreach (var attribute in node.Attributes)
{
var ae = new Attribute(cx, attribute, outputAssembly);
cx.BindComments(ae, attribute.GetLocation());
var attributeData = attributeDatas.Single(ad => ad.ApplicationSyntaxReference?.GetSyntax() == attribute);
if (attributeData is object)
{
var ae = Attribute.Create(cx, attributeData, outputAssembly);
cx.BindComments(ae, attribute.GetLocation());
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is quite ugly, but I couldn't find a better way to extract assembly attributes.

@tamasvajk
Copy link
Contributor Author

https://jenkins.internal.semmle.com/job/Changes/job/CSharp-Differences/549/

  • There's no significant change in analysis times, maybe it got a bit slower.
  • No new real issues were found. There are changes in the obsolete-method-called check, but that might just be wobbliness. (There are true and false positives too.)
  • DB size increased by 0-3.5% depending on project.

Comment on lines +264 to +304
not isNotNeeded(element.getParent+()) and
// LambdaExpr is both a Callable and a ControlFlowElement,
// print it with the more specific CallableNode
not element instanceof Callable
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is unrelated to this PR, but it was consistently failing locally and in the CI job too, so I included the fix here.

@tamasvajk
Copy link
Contributor Author

@hvitved one significant change by this PR is that we can have expressions outside of source files now. What do you think is this going to cause trouble somewhere?

@tamasvajk tamasvajk marked this pull request as ready for review October 21, 2020 12:31
@tamasvajk tamasvajk requested a review from a team as a code owner October 21, 2020 12:31
@tamasvajk tamasvajk dismissed a stale review via b61b6a2 October 29, 2020 15:07
@tamasvajk
Copy link
Contributor Author

tamasvajk commented Oct 29, 2020

I've rebased this PR to fix merge conflicts.

Also starting a new c# differences job: https://jenkins.internal.semmle.com/job/Changes/job/CSharp-Differences/574/

tamasvajk and others added 9 commits November 11, 2020 09:46
- Only cache on `AttributeData` and not the parent entity.
- Move `CreateGeneratedExpressionFromArgument` to `Expression.cs`.
- Restructure the various `CreateGenerated` methods so child entities are
  created inside them (and therefore no need to expose child index logic).
- Add locations to generated expressions.
- Avoid linear lookup in `CompilationUnit.cs`.
- Consolidate tests.
@tamasvajk
Copy link
Contributor Author

@hvitved I rebased this PR, and pushed an extra commit to fix the failing tests, can you please double check if these were really expected elements?

I'm triggering a C# differences job in the meantime.

@tamasvajk
Copy link
Contributor Author

Differences job

@hvitved
Copy link
Contributor

hvitved commented Nov 11, 2020

@hvitved I rebased this PR, and pushed an extra commit to fix the failing tests, can you please double check if these were really expected elements?

Turns out that it is because the compiler inserts Nullable(1) attributes in tuple patterns, so we should be good.

Copy link
Contributor

@hvitved hvitved left a comment

Choose a reason for hiding this comment

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

LGTM. Let's wait for the Differences job before merging.

@tamasvajk tamasvajk merged commit b5ef3bd into github:main Nov 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants