-
Notifications
You must be signed in to change notification settings - Fork 1.8k
C#: Refactor attribute extraction #4516
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
Conversation
93c0351
to
d211e4f
Compare
d211e4f
to
38b95d4
Compare
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) })] |
There was a problem hiding this comment.
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) })] |
There was a problem hiding this comment.
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:
1a46cb9
to
a10da96
Compare
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()); | ||
} |
There was a problem hiding this comment.
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.
|
bd2d6aa
to
462cb48
Compare
not isNotNeeded(element.getParent+()) and | ||
// LambdaExpr is both a Callable and a ControlFlowElement, | ||
// print it with the more specific CallableNode | ||
not element instanceof Callable |
There was a problem hiding this comment.
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.
@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? |
0676e06
to
8c4d5a1
Compare
8c4d5a1
to
b61b6a2
Compare
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/ |
csharp/ql/test/library-tests/controlflow/graph/ExitElement.expected
Outdated
Show resolved
Hide resolved
cc37447
to
4d5aa22
Compare
…rated expressions when missing
…when location is from source
- 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.
a14beca
to
453c97a
Compare
@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. |
Turns out that it is because the compiler inserts |
There was a problem hiding this 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.
No description provided.