diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -10234,6 +10234,11 @@ "%select{non-pointer|function pointer|void pointer}0 argument to " "'__builtin_launder' is not allowed">; +def warn_mismatched_import : Warning< + "import %select{module|name}0 does not match previous declaration">; +def warn_import_on_definition : Warning< + "import %select{module|name}0 cannot be applied to a function with a definition">; + def err_preserve_field_info_not_field : Error< "__builtin_preserve_field_info argument %0 not a field access">; def err_preserve_field_info_not_const: Error< diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -2833,6 +2833,10 @@ const InternalLinkageAttr &AL); CommonAttr *mergeCommonAttr(Decl *D, const ParsedAttr &AL); CommonAttr *mergeCommonAttr(Decl *D, const CommonAttr &AL); + WebAssemblyImportNameAttr *mergeImportNameAttr( + Decl *D, const WebAssemblyImportNameAttr &AL); + WebAssemblyImportModuleAttr *mergeImportModuleAttr( + Decl *D, const WebAssemblyImportModuleAttr &AL); void mergeDeclAttributes(NamedDecl *New, Decl *Old, AvailabilityMergeKind AMK = AMK_Redeclaration); diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -2591,6 +2591,10 @@ NewAttr = S.mergeSpeculativeLoadHardeningAttr(D, *SLHA); else if (const auto *SLHA = dyn_cast(Attr)) NewAttr = S.mergeNoSpeculativeLoadHardeningAttr(D, *SLHA); + else if (const auto *IMA = dyn_cast(Attr)) + NewAttr = S.mergeImportModuleAttr(D, *IMA); + else if (const auto *INA = dyn_cast(Attr)) + NewAttr = S.mergeImportNameAttr(D, *INA); else if (Attr->shouldInheritEvenIfAlreadyPresent() || !DeclHasAttr(D, Attr)) NewAttr = cast(Attr->clone(S.Context)); diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp --- a/clang/lib/Sema/SemaDeclAttr.cpp +++ b/clang/lib/Sema/SemaDeclAttr.cpp @@ -5776,45 +5776,73 @@ D->addAttr(UsedAttr::CreateImplicit(S.Context)); } -static void handleWebAssemblyImportModuleAttr(Sema &S, Decl *D, const ParsedAttr &AL) { - if (!isFunctionOrMethod(D)) { - S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) - << "'import_module'" << ExpectedFunction; - return; +WebAssemblyImportModuleAttr * +Sema::mergeImportModuleAttr(Decl *D, const WebAssemblyImportModuleAttr &AL) { + auto *FD = cast(D); + + if (const auto *ExistingAttr = FD->getAttr()) { + if (ExistingAttr->getImportModule() == AL.getImportModule()) + return nullptr; + Diag(ExistingAttr->getLocation(), diag::warn_mismatched_import) << 0; + Diag(AL.getLoc(), diag::note_previous_attribute); + return nullptr; + } + if (FD->hasBody()) { + Diag(AL.getLoc(), diag::warn_import_on_definition) << 0; + return nullptr; } + return ::new (Context) WebAssemblyImportModuleAttr(Context, AL, + AL.getImportModule()); +} +WebAssemblyImportNameAttr * +Sema::mergeImportNameAttr(Decl *D, const WebAssemblyImportNameAttr &AL) { auto *FD = cast(D); - if (FD->isThisDeclarationADefinition()) { - S.Diag(D->getLocation(), diag::err_alias_is_definition) << FD << 0; - return; + + if (const auto *ExistingAttr = FD->getAttr()) { + if (ExistingAttr->getImportName() == AL.getImportName()) + return nullptr; + Diag(ExistingAttr->getLocation(), diag::warn_mismatched_import) << 1; + Diag(AL.getLoc(), diag::note_previous_attribute); + return nullptr; + } + if (FD->hasBody()) { + Diag(AL.getLoc(), diag::warn_import_on_definition) << 1; + return nullptr; } + return ::new (Context) WebAssemblyImportNameAttr(Context, AL, + AL.getImportName()); +} + +static void +handleWebAssemblyImportModuleAttr(Sema &S, Decl *D, const ParsedAttr &AL) { + auto *FD = cast(D); StringRef Str; SourceLocation ArgLoc; if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc)) return; + if (FD->hasBody()) { + S.Diag(AL.getLoc(), diag::warn_import_on_definition) << 0; + return; + } FD->addAttr(::new (S.Context) WebAssemblyImportModuleAttr(S.Context, AL, Str)); } -static void handleWebAssemblyImportNameAttr(Sema &S, Decl *D, const ParsedAttr &AL) { - if (!isFunctionOrMethod(D)) { - S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) - << "'import_name'" << ExpectedFunction; - return; - } - +static void +handleWebAssemblyImportNameAttr(Sema &S, Decl *D, const ParsedAttr &AL) { auto *FD = cast(D); - if (FD->isThisDeclarationADefinition()) { - S.Diag(D->getLocation(), diag::err_alias_is_definition) << FD << 0; - return; - } StringRef Str; SourceLocation ArgLoc; if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc)) return; + if (FD->hasBody()) { + S.Diag(AL.getLoc(), diag::warn_import_on_definition) << 1; + return; + } FD->addAttr(::new (S.Context) WebAssemblyImportNameAttr(S.Context, AL, Str)); } diff --git a/clang/test/Sema/attr-wasm.c b/clang/test/Sema/attr-wasm.c new file mode 100644 --- /dev/null +++ b/clang/test/Sema/attr-wasm.c @@ -0,0 +1,31 @@ +// RUN: %clang_cc1 -triple wasm32-unknown-unknown -fsyntax-only -verify %s + +void name_a() __attribute__((import_name)); //expected-error {{'import_name' attribute takes one argument}} + +int name_b __attribute__((import_name("foo"))); //expected-error {{'import_name' attribute only applies to functions}} + +void name_c() __attribute__((import_name("foo", "bar"))); //expected-error {{'import_name' attribute takes one argument}} + +void name_d() __attribute__((import_name("foo", "bar", "qux"))); //expected-error {{'import_name' attribute takes one argument}} + +__attribute__((import_name("foo"))) void name_e() {} //FIXME-expected-error {{import name cannot be applied to a function with a definition}} + +void name_z() __attribute__((import_name("foo"))); //expected-note {{previous attribute is here}} + +void name_z() __attribute__((import_name("bar"))); //expected-warning {{import name does not match previous declaration}} + +void module_a() __attribute__((import_module)); //expected-error {{'import_module' attribute takes one argument}} + +int module_b __attribute__((import_module("foo"))); //expected-error {{'import_module' attribute only applies to functions}} + +void module_c() __attribute__((import_module("foo", "bar"))); //expected-error {{'import_module' attribute takes one argument}} + +void module_d() __attribute__((import_module("foo", "bar", "qux"))); //expected-error {{'import_module' attribute takes one argument}} + +void module_e() __attribute__((import_module("foo"))) {} //FIXME-expected-error {{import module cannot be applied to a function with a definition}} + +void module_z() __attribute__((import_module("foo"))); //expected-note {{previous attribute is here}} + +void module_z() __attribute__((import_module("bar"))); //expected-warning {{import module does not match previous declaration}} + +void both() __attribute__((import_name("foo"), import_module("bar")));