Skip to content
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

[clang][Diagnostics] Add bitfield source range to zero width diags #68312

Merged
merged 1 commit into from
Oct 6, 2023

Conversation

tbaederr
Copy link
Contributor

@tbaederr tbaederr commented Oct 5, 2023

Before:

array.cpp:157:8: error: named bit-field 'a' has zero width
  157 |   char a : 12 - 12;
      |        ^
1 error generated.

After:

array.cpp:157:8: error: named bit-field 'a' has zero width
  157 |   char a : 12 - 12;
      |        ^   ~~~~~~~
1 error generated.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Oct 5, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Oct 5, 2023

@llvm/pr-subscribers-clang

Changes

Before:

array.cpp:157:8: error: named bit-field 'a' has zero width
  157 |   char a : 12 - 12;
      |        ^
1 error generated.

After:

array.cpp:157:8: error: named bit-field 'a' has zero width
  157 |   char a : 12 - 12;
      |        ^   ~~~~~~~
1 error generated.

Full diff: https://github.com/llvm/llvm-project/pull/68312.diff

2 Files Affected:

  • (modified) clang/lib/Sema/SemaDecl.cpp (+2-1)
  • (added) clang/test/Misc/misc-source-ranges.cpp (+7)
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 28d9a0ac654b81f..8fff33c49f2e5c7 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -18161,7 +18161,8 @@ ExprResult Sema::VerifyBitField(SourceLocation FieldLoc,
 
   // Zero-width bitfield is ok for anonymous field.
   if (Value == 0 && FieldName)
-    return Diag(FieldLoc, diag::err_bitfield_has_zero_width) << FieldName;
+    return Diag(FieldLoc, diag::err_bitfield_has_zero_width)
+           << FieldName << BitWidth->getSourceRange();
 
   if (Value.isSigned() && Value.isNegative()) {
     if (FieldName)
diff --git a/clang/test/Misc/misc-source-ranges.cpp b/clang/test/Misc/misc-source-ranges.cpp
new file mode 100644
index 000000000000000..7a9d9d057dac407
--- /dev/null
+++ b/clang/test/Misc/misc-source-ranges.cpp
@@ -0,0 +1,7 @@
+// RUN: not %clang_cc1 -fsyntax-only -fdiagnostics-print-source-range-info %s 2>&1 | FileCheck %s
+
+struct S {
+  char a : 12 - 12;
+};
+// CHECK: misc-source-ranges.cpp:[[@LINE-2]]:8:{[[@LINE-2]]:12-[[@LINE-2]]:19}
+

@tbaederr tbaederr merged commit aad7e0a into llvm:main Oct 6, 2023
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants