[clang] [clang][Sema] Bad register variable type error should point to the type (PR #110239)

David Spickett via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 7 06:16:02 PDT 2024


https://github.com/DavidSpickett updated https://github.com/llvm/llvm-project/pull/110239

>From 84946607fc58927b1b44da2b70566abca041bbfb Mon Sep 17 00:00:00 2001
From: David Spickett <david.spickett at linaro.org>
Date: Fri, 27 Sep 2024 10:19:19 +0000
Subject: [PATCH 1/5] [clang][Sema] Bad register variable type error should
 point to the type

...not the register keyword. Fixes #109776.

Until now the error was only tested in clang/test/Sema/asm.c,
where you can't check for the "^" character. So I've added a new
caret test file as I see has been done for other error types.
---
 clang/lib/Sema/SemaDecl.cpp                   |  3 ++-
 .../Sema/caret-diags-register-variable.cpp    | 20 +++++++++++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/Sema/caret-diags-register-variable.cpp

diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 1bf0e800a36228..048ac9c22eef50 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -7949,7 +7949,8 @@ NamedDecl *Sema::ActOnVariableDeclarator(
       }
 
       if (!R->isIntegralType(Context) && !R->isPointerType()) {
-        Diag(D.getBeginLoc(), diag::err_asm_bad_register_type);
+        Diag(TInfo->getTypeLoc().getBeginLoc(),
+             diag::err_asm_bad_register_type);
         NewVD->setInvalidDecl(true);
       }
     }
diff --git a/clang/test/Sema/caret-diags-register-variable.cpp b/clang/test/Sema/caret-diags-register-variable.cpp
new file mode 100644
index 00000000000000..09893341717c8e
--- /dev/null
+++ b/clang/test/Sema/caret-diags-register-variable.cpp
@@ -0,0 +1,20 @@
+// RUN: not %clang_cc1 -triple i386-pc-linux-gnu -std=c++11 -fsyntax-only -fno-diagnostics-show-line-numbers -fcaret-diagnostics-max-lines=5 %s 2>&1 | FileCheck %s -strict-whitespace
+
+struct foo {
+  int a;
+};
+
+//CHECK: {{.*}}: error: bad type for named register variable
+//CHECK-NEXT: {{^}}register struct foo bar asm("esp");
+//CHECK-NEXT: {{^}}         ^{{$}}
+register struct foo bar asm("esp"); // expected-error {{bad type for named register variable}}
+
+//CHECK: {{.*}}: error: register 'edi' unsuitable for global register variables on this target
+//CHECK-NEXT: {{^}}register int r0 asm ("edi");
+//CHECK-NEXT: {{^}}                     ^{{$}}
+register int r0 asm ("edi");
+
+//CHECK: {{.*}}: error: size of register 'esp' does not match variable size
+//CHECK-NEXT: {{^}}register long long r1 asm ("esp");
+//CHECK-NEXT: {{^}}                           ^{{$}}
+register long long r1 asm ("esp");

>From d1a84041e89d9e33ab5e974ba43e744c1d0fb736 Mon Sep 17 00:00:00 2001
From: David Spickett <david.spickett at linaro.org>
Date: Mon, 7 Oct 2024 13:06:38 +0000
Subject: [PATCH 2/5] Remove marker leftover from copy paste

---
 clang/test/Sema/caret-diags-register-variable.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/test/Sema/caret-diags-register-variable.cpp b/clang/test/Sema/caret-diags-register-variable.cpp
index 09893341717c8e..d374cd865bcc82 100644
--- a/clang/test/Sema/caret-diags-register-variable.cpp
+++ b/clang/test/Sema/caret-diags-register-variable.cpp
@@ -7,7 +7,7 @@ struct foo {
 //CHECK: {{.*}}: error: bad type for named register variable
 //CHECK-NEXT: {{^}}register struct foo bar asm("esp");
 //CHECK-NEXT: {{^}}         ^{{$}}
-register struct foo bar asm("esp"); // expected-error {{bad type for named register variable}}
+register struct foo bar asm("esp");
 
 //CHECK: {{.*}}: error: register 'edi' unsuitable for global register variables on this target
 //CHECK-NEXT: {{^}}register int r0 asm ("edi");

>From 0bd88738ee2dfb106e14f71c4e6fc6e612648fbd Mon Sep 17 00:00:00 2001
From: David Spickett <david.spickett at linaro.org>
Date: Mon, 7 Oct 2024 13:09:35 +0000
Subject: [PATCH 3/5] Add the source range marker

---
 clang/lib/Sema/SemaDecl.cpp                       | 4 ++--
 clang/test/Sema/caret-diags-register-variable.cpp | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 048ac9c22eef50..7f0e6257508428 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -7949,8 +7949,8 @@ NamedDecl *Sema::ActOnVariableDeclarator(
       }
 
       if (!R->isIntegralType(Context) && !R->isPointerType()) {
-        Diag(TInfo->getTypeLoc().getBeginLoc(),
-             diag::err_asm_bad_register_type);
+        Diag(TInfo->getTypeLoc().getBeginLoc(), diag::err_asm_bad_register_type)
+            << TInfo->getTypeLoc().getSourceRange();
         NewVD->setInvalidDecl(true);
       }
     }
diff --git a/clang/test/Sema/caret-diags-register-variable.cpp b/clang/test/Sema/caret-diags-register-variable.cpp
index d374cd865bcc82..24f5061d4b4d2c 100644
--- a/clang/test/Sema/caret-diags-register-variable.cpp
+++ b/clang/test/Sema/caret-diags-register-variable.cpp
@@ -6,7 +6,7 @@ struct foo {
 
 //CHECK: {{.*}}: error: bad type for named register variable
 //CHECK-NEXT: {{^}}register struct foo bar asm("esp");
-//CHECK-NEXT: {{^}}         ^{{$}}
+//CHECK-NEXT: {{^}}         ^~~~~~~~~~{{$}}
 register struct foo bar asm("esp");
 
 //CHECK: {{.*}}: error: register 'edi' unsuitable for global register variables on this target

>From 1e84d54d3a70db97f174771e35be35e74927957a Mon Sep 17 00:00:00 2001
From: David Spickett <david.spickett at linaro.org>
Date: Mon, 7 Oct 2024 13:14:17 +0000
Subject: [PATCH 4/5] Add release note

---
 clang/docs/ReleaseNotes.rst | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 1fbcac807d0b30..d9bfa7c9aee93f 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -371,6 +371,8 @@ Bug Fixes in This Version
 - Fixed a crash when trying to transform a dependent address space type. Fixes #GH101685.
 - Fixed a crash when diagnosing format strings and encountering an empty
   delimited escape sequence (e.g., ``"\o{}"``). #GH102218
+- The warning emitted for an unsupported register variable type now points to
+  the type instead of the ``register`` keyword (#GH109776).
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

>From dfa0500e35510110feef164552e91c7221a60045 Mon Sep 17 00:00:00 2001
From: David Spickett <david.spickett at linaro.org>
Date: Mon, 7 Oct 2024 13:15:44 +0000
Subject: [PATCH 5/5] Be clearer

---
 clang/docs/ReleaseNotes.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index d9bfa7c9aee93f..678a462d0bac3e 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -372,7 +372,7 @@ Bug Fixes in This Version
 - Fixed a crash when diagnosing format strings and encountering an empty
   delimited escape sequence (e.g., ``"\o{}"``). #GH102218
 - The warning emitted for an unsupported register variable type now points to
-  the type instead of the ``register`` keyword (#GH109776).
+  the unsupported type instead of the ``register`` keyword (#GH109776).
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^



More information about the cfe-commits mailing list