Author: Alexsander Borges Damaceno
Date: 2026-09-17T19:18:25+08:00
New Revision: a70e9fa1ccf273eef2d7d33eb8f5b6424b3ff7fe

URL: 
https://github.com/llvm/llvm-project/commit/a70e9fa1ccf273eef2d7d33eb8f5b6424b3ff7fe
DIFF: 
https://github.com/llvm/llvm-project/commit/a70e9fa1ccf273eef2d7d33eb8f5b6424b3ff7fe.diff

LOG: [Clang] Improve diagnostics to show the correct position for 
`[[maybe_unused]]` before a using-alias (#223910)

For [[maybe_unused]] written before the 'using' keyword of an
alias-declaration:

`[[maybe_unused]] using T = int;`

Clang currently reports only:

`error: an attribute list cannot appear here
`

with no indication of where the attribute should actually go

`Parser::DiagnoseProhibitedAttributes` already provides exactly this
behavior, emitting a more helpful fix-it that suggests moving the
attribute to the correct location. ParseUsingDeclaration already uses it
without pass the FixItLoc location this way the fix-it sugestion is not
show for using decl.

This patch passes the right location when the attribute is
`[[maybe_unused]]`, making the fix-it appear for the user.

Fixes https://github.com/llvm/llvm-project/issues/155787

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.md
    clang/lib/Parse/ParseDeclCXX.cpp
    clang/test/Parser/cxx0x-attributes.cpp
    clang/test/Parser/cxx0x-keyword-attributes.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 18d2895531ea6..89df8e80459f0 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -513,6 +513,9 @@ features cannot lower the translation-unit ABI level;
 - Improve the input size mismatch diagnostic when calling 
`__builtin_shufflevector` with valid
   vector element types but 
diff erent sizes. (GH221791)
 
+- Suggests the correct location for an attribute written before the `using`
+  keyword of an alias-declaration. (#GH155787)
+
 ### Improvements to Clang's time-trace
 
 ### Improvements to Coverage Mapping

diff  --git a/clang/lib/Parse/ParseDeclCXX.cpp 
b/clang/lib/Parse/ParseDeclCXX.cpp
index dbc1b85acd143..1eeb0d21b68d7 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -740,7 +740,7 @@ Parser::DeclGroupPtrTy Parser::ParseUsingDeclaration(
       return nullptr;
     }
 
-    ProhibitAttributes(PrefixAttrs);
+    ProhibitAttributes(PrefixAttrs, Tok.getLocation());
 
     Decl *DeclFromDeclSpec = nullptr;
     Scope *CurScope = getCurScope();

diff  --git a/clang/test/Parser/cxx0x-attributes.cpp 
b/clang/test/Parser/cxx0x-attributes.cpp
index a587f40aadd98..1e529db2965fa 100644
--- a/clang/test/Parser/cxx0x-attributes.cpp
+++ b/clang/test/Parser/cxx0x-attributes.cpp
@@ -181,7 +181,7 @@ void bad_attributes_in_do_while() {
                        // expected-error@-1 {{expected ';' after do/while}}
 } // expected-error 2{{expected ')'}} expected-error {{expected expression}}
 
-[[]] using T = int; // expected-error {{an attribute list cannot appear here}}
+[[]] using T = int; // expected-error {{misplaced attributes; expected 
attributes here}}
 using T [[]] = int; // ok
 template<typename T> using U [[]] = T;
 using ns::i [[]];
@@ -200,6 +200,10 @@ using T [[noreturn]] = int; // expected-error {{'noreturn' 
attribute only applie
 using V = int; // expected-note {{previous}}
 using V [[gnu::vector_size(16)]] = int; // expected-error {{redefinition with 
diff erent types}}
 
+void using_alias_in_block() {
+  [[maybe_unused]] using BlockAlias = int; // expected-error {{misplaced 
attributes; expected attributes here}}
+}
+
 auto trailing() -> [[]] const int; // expected-error {{an attribute list 
cannot appear here}}
 auto trailing() -> const [[]] int; // expected-error {{an attribute list 
cannot appear here}}
 auto trailing() -> const int [[]];

diff  --git a/clang/test/Parser/cxx0x-keyword-attributes.cpp 
b/clang/test/Parser/cxx0x-keyword-attributes.cpp
index e044336eb1792..72e2a90195ee1 100644
--- a/clang/test/Parser/cxx0x-keyword-attributes.cpp
+++ b/clang/test/Parser/cxx0x-keyword-attributes.cpp
@@ -139,7 +139,7 @@ using ATTR_USE alignas(4) ATTR_USE foobar = int; // 
expected-error {{'ATTR_NAME'
                                                                   
expected-error {{'alignas' attribute only applies to}} \
                                                                   
expected-error 2 {{'ATTR_NAME' only applies to function types}}
 
-ATTR_USE using T = int; // expected-error {{'ATTR_NAME' cannot appear here}}
+ATTR_USE using T = int; // expected-error {{misplaced 'ATTR_NAME'; expected 
'ATTR_NAME' here}}
 using T ATTR_USE = int; // expected-error {{'ATTR_NAME' only applies to 
function types}}
 template<typename T> using U ATTR_USE = T; // expected-error {{'ATTR_NAME' 
only applies to function types}}
 using ns::i ATTR_USE; // expected-warning {{ISO C++}} \


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to