public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 1/2] [gdb/macro] Ignore in-file macro definition with 0 line complaint for clang
@ 2024-05-17 10:47 Tom de Vries
  2024-05-17 10:47 ` [PATCH 2/2] [gdb/macro] Ignore malformed macro definition " Tom de Vries
  0 siblings, 1 reply; 2+ messages in thread
From: Tom de Vries @ 2024-05-17 10:47 UTC (permalink / raw)
  To: gdb-patches

When showing complaints for the exec of test-case
gdb.dwarf2/clang-cli-macro.exp, we get:
...
$ gdb -q -batch -iex "set complaints 5" clang-cli-macro -ex "p main"
During symbol reading: \
  debug info gives in-file macro definition with zero line 0: ONE 1
$1 = {int ()} 0x4004b7 <main>
...

The readelf output for the .debug_macro section looks like:
...
Contents of the .debug_macro section:

  Offset:                      0
  Version:                     5
  Offset size:                 4
  Offset into .debug_line:     0xe3

 DW_MACRO_start_file - lineno: 0 filenum: 1
 DW_MACRO_define - lineno : 1 macro : TWO 2
 DW_MACRO_end_file
 DW_MACRO_define - lineno : 0 macro : ONE 1
...

The complaint is that the DW_MACRO_define for ONE both:
- has lineno 0, so it's predefined or specified on the command line, and
- occurs after the first DW_MACRO_start_file.

In commit e7e7469e7a3 ("gdb: Fix issue with Clang CLI macros") we've added a
workaround to accept this style of .debug_macro section, and
gdb.dwarf2/clang-cli-macro.exp is the test-case for the workaround.

Given that we've added the workaround, it doesn't make sense to complain.

The warning is produced using the following condition in
dwarf_decode_macro_bytes:
...
	    if ((line == 0 && !at_commandline)
		|| (line != 0 && at_commandline))
...
using the variable at_commandline which is initially 1, and set to 0 when
encountering the first DW_MACRO_start_file.

The value of this variable doesn't make sense in the context of a clang-style
.debug_macro section.

We could try to fix this by changing the value of the variable to fit with the
clang-style .debug_macro section, but I don't think it's worth the effort.

Simply fix this by for a clang producer opting out of all complaints in the
function that use the at_commandline variable in the condition.

Tested on x86_64-linux.
---
 gdb/dwarf2/macro.c                           | 10 ++++++----
 gdb/testsuite/gdb.dwarf2/clang-cli-macro.exp |  6 ++++++
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/gdb/dwarf2/macro.c b/gdb/dwarf2/macro.c
index a511d0a3b44..66a40e88d3a 100644
--- a/gdb/dwarf2/macro.c
+++ b/gdb/dwarf2/macro.c
@@ -546,8 +546,9 @@ dwarf_decode_macro_bytes (dwarf2_per_objfile *per_objfile,
 			   line, body);
 		break;
 	      }
-	    if ((line == 0 && !at_commandline)
-		|| (line != 0 && at_commandline))
+	    if (((line == 0 && !at_commandline)
+		 || (line != 0 && at_commandline))
+		&& !producer_is_clang (cu))
 	      complaint (_("debug info gives %s macro %s with %s line %d: %s"),
 			 at_commandline ? _("command-line") : _("in-file"),
 			 is_define ? _("definition") : _("undefinition"),
@@ -648,8 +649,9 @@ dwarf_decode_macro_bytes (dwarf2_per_objfile *per_objfile,
 	    file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
 	    mac_ptr += bytes_read;
 
-	    if ((line == 0 && !at_commandline)
-		|| (line != 0 && at_commandline))
+	    if (((line == 0 && !at_commandline)
+		 || (line != 0 && at_commandline))
+		&& !producer_is_clang (cu))
 	      complaint (_("debug info gives source %d included "
 			   "from %s at %s line %d"),
 			 file, at_commandline ? _("command-line") : _("file"),
diff --git a/gdb/testsuite/gdb.dwarf2/clang-cli-macro.exp b/gdb/testsuite/gdb.dwarf2/clang-cli-macro.exp
index eafb75ad8d9..fdeaaff5afd 100644
--- a/gdb/testsuite/gdb.dwarf2/clang-cli-macro.exp
+++ b/gdb/testsuite/gdb.dwarf2/clang-cli-macro.exp
@@ -85,6 +85,12 @@ if {[prepare_for_testing "failed to prepare" $testfile [list $srcfile $asm_file]
     return
 }
 
+set re_result "[string_to_regexp $]$decimal = \[^\r\n\]+"
+
+with_complaints 5 {
+    gdb_test "print main" ^$re_result "no complaints"
+}
+
 if {![runto_main]} {
     return
 }

base-commit: 44fc9616c2e74396395f60c9a601317e4c4c4733
-- 
2.35.3


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH 2/2] [gdb/macro] Ignore malformed macro definition complaint for clang
  2024-05-17 10:47 [PATCH 1/2] [gdb/macro] Ignore in-file macro definition with 0 line complaint for clang Tom de Vries
@ 2024-05-17 10:47 ` Tom de Vries
  0 siblings, 0 replies; 2+ messages in thread
From: Tom de Vries @ 2024-05-17 10:47 UTC (permalink / raw)
  To: gdb-patches

With test-case test.c:
...
 #define NOARG
 int main (void) { return 0; }
...
compiled with clang:
...
$ clang test.c -g3 -fdebug-macro
...
we get a complaint:
...
$ gdb -q -batch -iex "set complaints 5" a.out -ex "p main"
During symbol reading: macro debug info contains a malformed macro definition:
`NOARG'
...
and four others from pre-defined defs.

This is because the macro body is defined as "NOARG" instead of "NOARG ".

There's a comment in parse_macro_definition that describes why the space
should be there, quoting the standard.

My guess is that this is just a matter of interpretation.  Either we have
- a name and empty definition, separated by a space, or
- a name and no definition, and therefore no space.

Anyway, this is long-standing and AFAICT intentional behaviour from clang, and
I think it's entirely reasonable, so drop the complaint for clang.

Likewise for '#define ARG(V)'.

Tested on x86_64-linux.
---
 gdb/dwarf2/macro.c                           | 12 +++++++-----
 gdb/testsuite/gdb.dwarf2/clang-cli-macro.exp |  4 ++++
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/gdb/dwarf2/macro.c b/gdb/dwarf2/macro.c
index 66a40e88d3a..deaad155bef 100644
--- a/gdb/dwarf2/macro.c
+++ b/gdb/dwarf2/macro.c
@@ -103,7 +103,7 @@ consume_improper_spaces (const char *p, const char *body)
 
 static void
 parse_macro_definition (struct macro_source_file *file, int line,
-			const char *body)
+			const char *body, struct dwarf2_cu *cu)
 {
   const char *p;
 
@@ -149,7 +149,8 @@ parse_macro_definition (struct macro_source_file *file, int line,
 	replacement = body + name_len + 1;
       else
 	{
-	  dwarf2_macro_malformed_definition_complaint (body);
+	  if (!producer_is_clang (cu))
+	    dwarf2_macro_malformed_definition_complaint (body);
 	  replacement = body + name_len;
 	}
 
@@ -213,7 +214,8 @@ parse_macro_definition (struct macro_source_file *file, int line,
 	  else if (*p == '\0')
 	    {
 	      /* Complain, but do define it.  */
-	      dwarf2_macro_malformed_definition_complaint (body);
+	      if (!producer_is_clang (cu))
+		dwarf2_macro_malformed_definition_complaint (body);
 	      macro_define_function (file, line, name.c_str (),
 				     argc, (const char **) argv,
 				     p);
@@ -569,7 +571,7 @@ dwarf_decode_macro_bytes (dwarf2_per_objfile *per_objfile,
 			   line, current_file->filename);
 	      }
 	    else if (is_define)
-	      parse_macro_definition (current_file, line, body);
+	      parse_macro_definition (current_file, line, body, cu);
 	    else
 	      {
 		gdb_assert (macinfo_type == DW_MACRO_undef
@@ -633,7 +635,7 @@ dwarf_decode_macro_bytes (dwarf2_per_objfile *per_objfile,
 	      }
 
 	    if (macinfo_type == DW_MACRO_define_strx)
-	      parse_macro_definition (current_file, line, body);
+	      parse_macro_definition (current_file, line, body, cu);
 	    else
 	      macro_undef (current_file, line, body);
 	   }
diff --git a/gdb/testsuite/gdb.dwarf2/clang-cli-macro.exp b/gdb/testsuite/gdb.dwarf2/clang-cli-macro.exp
index fdeaaff5afd..536132f6aa2 100644
--- a/gdb/testsuite/gdb.dwarf2/clang-cli-macro.exp
+++ b/gdb/testsuite/gdb.dwarf2/clang-cli-macro.exp
@@ -75,6 +75,8 @@ Dwarf::assemble $asm_file {
 	    start_file 0 1
 		# A macro defined at line 1 of the main file.
 		define 1 "TWO 2"
+		define 2 "THREE"
+		define 3 "FOUR(ARG)"
 	    end_file
 	    define 0 "ONE 1"
 	}
@@ -97,3 +99,5 @@ if {![runto_main]} {
 
 gdb_test "print TWO" "= 2" "print simple macro"
 gdb_test "print ONE" "= 1" "print defined from CLI"
+gdb_test "info macro THREE" "\r\n#define THREE "
+gdb_test "info macro FOUR" "\r\n[string_to_regexp {#define FOUR(ARG) }]"
-- 
2.35.3


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-05-17 10:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-17 10:47 [PATCH 1/2] [gdb/macro] Ignore in-file macro definition with 0 line complaint for clang Tom de Vries
2024-05-17 10:47 ` [PATCH 2/2] [gdb/macro] Ignore malformed macro definition " Tom de Vries

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).