[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
Tue Oct 8 01:50:47 PDT 2024


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

>From e02b44cf94c71c15235bb4727211587283778189 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 5c4e0562152c5b..6d34ed212e2761 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -7957,7 +7957,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 e07acefdff7778819b687421dae67a15545b492c 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 5897fb35691464eaf66cd2ec047e17407aa8edea 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 6d34ed212e2761..4eaa3913f4344a 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -7957,8 +7957,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 61b9d68bbcbea956cc8f24b34cd8c7e067152027 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 6c0245ce660251..6f13cdea75b917 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -395,6 +395,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 c5db62860b38c52c4fe3c0887ca4ab1c0a04f472 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 6f13cdea75b917..3a36321f7f9014 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -396,7 +396,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