-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang] Instantiate attributes on LabelDecls #115924
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
Start propagating attributes on (e.g.) labels inside of templated functions to their instances.
@llvm/pr-subscribers-clang Author: Eric Astor (ericastor) ChangesStart propagating attributes on (e.g.) labels inside of templated functions to their instances. Full diff: https://github.com/llvm/llvm-project/pull/115924.diff 2 Files Affected:
diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index 5a001843e2ba46..bfc5913dbafd0f 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -990,6 +990,7 @@ Decl *
TemplateDeclInstantiator::VisitLabelDecl(LabelDecl *D) {
LabelDecl *Inst = LabelDecl::Create(SemaRef.Context, Owner, D->getLocation(),
D->getIdentifier());
+ SemaRef.InstantiateAttrs(TemplateArgs, D, Inst, LateAttrs, StartingScope);
Owner->addDecl(Inst);
return Inst;
}
@@ -1009,6 +1010,7 @@ TemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
D->getQualifierLoc(),
D->getTargetNameLoc(),
D->getNamespace());
+ SemaRef.InstantiateAttrs(TemplateArgs, D, Inst, LateAttrs, StartingScope);
Owner->addDecl(Inst);
return Inst;
}
@@ -1095,15 +1097,21 @@ Decl *TemplateDeclInstantiator::InstantiateTypedefNameDecl(TypedefNameDecl *D,
Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/false);
- if (Typedef)
+ if (Typedef) {
+ SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef, LateAttrs,
+ StartingScope);
Owner->addDecl(Typedef);
+ }
return Typedef;
}
Decl *TemplateDeclInstantiator::VisitTypeAliasDecl(TypeAliasDecl *D) {
Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/true);
- if (Typedef)
+ if (Typedef) {
+ SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef, LateAttrs,
+ StartingScope);
Owner->addDecl(Typedef);
+ }
return Typedef;
}
@@ -1160,8 +1168,10 @@ Decl *TemplateDeclInstantiator::InstantiateTypeAliasTemplateDecl(
Decl *
TemplateDeclInstantiator::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
Decl *Inst = InstantiateTypeAliasTemplateDecl(D);
- if (Inst)
+ if (Inst) {
+ SemaRef.InstantiateAttrs(TemplateArgs, D, Inst, LateAttrs, StartingScope);
Owner->addDecl(Inst);
+ }
return Inst;
}
diff --git a/clang/test/SemaCXX/attr-mode-tmpl.cpp b/clang/test/SemaCXX/attr-mode-tmpl.cpp
index f665b1ba491237..58266f947051c5 100644
--- a/clang/test/SemaCXX/attr-mode-tmpl.cpp
+++ b/clang/test/SemaCXX/attr-mode-tmpl.cpp
@@ -9,7 +9,7 @@ void CheckEnumerations() {
// Check that non-vector 'mode' attribute is OK with enumeration types.
typedef T __attribute__((mode(QI))) T1;
typedef T T2 __attribute__((mode(HI)));
- typedef T __attribute__((mode(V8SI))) T3; // expected-error{{mode 'V8SI' is not supported for enumeration types}}
+ typedef T __attribute__((mode(V8SI))) T3; // expected-error2{{mode 'V8SI' is not supported for enumeration types}}
// expected-warning@-1{{specifying vector types with the 'mode' attribute is deprecated}}
typedef enum __attribute__((mode(HI))) { A4, B4 } T4;
@@ -62,7 +62,7 @@ struct TemplatedStruct {
// Check typedefs.
typedef T __attribute__((mode(DI))) T1;
- typedef T __attribute__((mode(V8DI))) T2; // expected-error{{mode 'V8DI' is not supported for enumeration types}}
+ typedef T __attribute__((mode(V8DI))) T2; // expected-error2{{mode 'V8DI' is not supported for enumeration types}}
// expected-warning@-1{{deprecated}}
// Check parameters.
|
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.
The change is reasonable, but I need to see tests for each of the types.
In general, we've had the attitude of "we can enable this instantiation once we see examples of it being useful", so we need tests to make sure it is instantiated, AND that it is useful.
@@ -9,7 +9,7 @@ void CheckEnumerations() { | |||
// Check that non-vector 'mode' attribute is OK with enumeration types. | |||
typedef T __attribute__((mode(QI))) T1; | |||
typedef T T2 __attribute__((mode(HI))); | |||
typedef T __attribute__((mode(V8SI))) T3; // expected-error{{mode 'V8SI' is not supported for enumeration types}} | |||
typedef T __attribute__((mode(V8SI))) T3; // expected-error2{{mode 'V8SI' is not supported for enumeration types}} |
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 looks like it should just cause the expected check to go away? What is going on here? If this is causing it to happen 2x, we might wonder if the attribute mode
should have not been generated if it errored the 1st time.
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.
Yeah, unfortunately the way InstantiateAttrs
is implemented seems to cause it to copy attributes even if they errored previously. You can see this used elsewhere in the same file for much the same reason... this is a case where we're just having a pre-existing problem show up in a new scenario.
This makes a lot of sense... but I'm not sure there's currently a way to write a FileCheck test for (e.g.) attributes on labels, which is actually my motivating case here. It'd be a pretty big change to start dumping label attributes in text AST dumps when the LabelDecl isn't even shown. Any suggestions? |
the others make sense for it.
|
We apply this to add a test of the new label attribute instantiation support.
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.
Still need tests for namespacealiasdecl, typedefnamedecl, and typealiastemplatedecl actually being instantiated.
Also, is there a reason why the only way to currently test these are with the plugins? I would expect there to be SOME builtin attribute that could be validated for all of those as well.
Since I'm not fully familiar with all of the attributes that could be applied to those decls, I'm withdrawing those parts of this PR for now. I'll be happy to help with those separately. I'll look into whether I can test it without using plugins, but I first wanted to get any test working with LabelDecl, and (as mentioned) I wasn't sure what built-in attributes would apply. I'll follow up with more tests. |
Alright, I've scoped this change back down to just LabelDecls, and added tests that avoid the use of plugins. I appreciate your review! |
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.
test needs some simplification/help, else this is fine.
Thanks, I was mimicking another file that apparently isn't following best practices. Done! |
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.
Test seems to have disappeared entirely!
Whoops. Apologies, missed that I'd forgotten to track the new file. |
Start propagating attributes on (e.g.) labels inside of templated functions to their instances.