PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > How to debug macro?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
How to debug macro?

Réponse
 
LinkBack Outils de la discussion
Vieux 29/06/2008, 05h23   #1
Peng Yu
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut How to debug macro?

Hi,

It is benifitical to use macro in certain cases.

http://www.boost.org/doc/libs/1_35_0...doc/index.html

However, I found that it is not easy to debug a macro. For example,
for the following program, I can not trace into the last macro in gdb.

In this sense, if I want to debug the code easily, should I avoid
using macros. Are there any better way to debug macros?

Thanks,
Peng

$ cat main.cc
#include <boost/typeof/typeof.hpp>
#include <iostream>

#define MACRO_DEF \
class A { \
public: \
A(int a) : _a(a) { } \
int the_a() const { return _a; } \
private: \
int _a; \
};

MACRO_DEF

#define MACRO \
A a(1);\
std::cout << a.the_a() << std::endl;

int main() {
MACRO
}


$gdb main-g.exe
GNU gdb 6.4.90-debian
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and
you are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for
details.
This GDB was configured as "x86_64-linux-gnu"...Using host
libthread_db library "/lib/libthread_db.so.1".

(gdb) start
Breakpoint 1 at 0x40081c: file main.cc, line 20.
main () at main.cc:20
20 MACRO
(gdb) s
A (this=0x7fffb8f0de80, a=1) at main.cc:13
13 MACRO_DEF
(gdb) s
A::the_a (this=0x7fffb8f0de80) at main.cc:13
13 MACRO_DEF
(gdb) s
1
main () at main.cc:21
21 }
(gdb) n
0x00002afcf215b4ca in __libc_start_main () from /lib/libc.so.6
(gdb) n
Single stepping until exit from function __libc_start_main,
which has no line number information.

Program exited normally.
(gdb)
  Réponse avec citation
Vieux 29/06/2008, 05h30   #2
Ian Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to debug macro?

Peng Yu wrote:
> Hi,
>
> It is benifitical to use macro in certain cases.
>
> http://www.boost.org/doc/libs/1_35_0...doc/index.html
>
> However, I found that it is not easy to debug a macro.
>

Which is why they are best avoided. With the exception of passing file
name and line number to debug logs, I don't think I've used a macro in
C++ in the past decade.

--
Ian Collins.
  Réponse avec citation
Vieux 29/06/2008, 05h43   #3
Kai-Uwe Bux
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to debug macro?

Peng Yu wrote:

> Hi,
>
> It is benifitical to use macro in certain cases.
>
> http://www.boost.org/doc/libs/1_35_0...doc/index.html
>
> However, I found that it is not easy to debug a macro. For example,
> for the following program, I can not trace into the last macro in gdb.
>
> In this sense, if I want to debug the code easily, should I avoid
> using macros. Are there any better way to debug macros?


Look at the output of the preprocessor. I think most compilers allow you to
do that. This way, you can see what the macro expands to.


Best

Kai-Uwe Bux
  Réponse avec citation
Vieux 29/06/2008, 15h19   #4
Phlip
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to debug macro?

> It is benifitical to use macro in certain cases.

Use macros for...

- conditional compilation
- token pasting
- stringerization

but, naturally, try to avoid _those_ things unless the alternatives are
worse.


  Réponse avec citation
Vieux 30/06/2008, 06h51   #5
Greg Herlihy
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to debug macro?

On Jun 28, 9:23pm, Peng Yu <PengYu...@gmail.com> wrote:

> It is benifitical to use macro in certain cases.


Perhaps, but the example below certainly isn't one of them.

>
> #define MACRO_DEF \
> class A { \
> public: \
> A(int a) : _a(a) { } \
> int the_a() const { return _a; } \
> private: \
> int _a; \
> };
>
> MACRO_DEF
>
> #define MACRO \
> A a(1);\
> std::cout << a.the_a() << std::endl;


> However, I found that it is not easy to debug a macro. For example,
> for the following program, I can not trace into the last macro in gdb.


If you (who wrote the macro) have trouble debugging it, imagine the
difficulties that the maintenance programmer will face - simply to
understand what the macro is supposed to do. After all, what would you
think if you had to debug a C++ program whose main() function looked
like the main() in your program:

> int main() {
> MACRO
>
> }


Having to deal with code like this, in my experience, would be enough
to convince most programmers that they really should be working -
somewhere else.

Greg

  Réponse avec citation
Vieux 30/06/2008, 18h24   #6
Peng Yu
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to debug macro?

On Jun 30, 12:51 am, Greg Herlihy <gre...@mac.com> wrote:
> On Jun 28, 9:23 pm, Peng Yu <PengYu...@gmail.com> wrote:
>
> > It is benifitical to use macro in certain cases.

>
> Perhaps, but the example below certainly isn't one of them.
>
>
>
>
>
> > #define MACRO_DEF \
> > class A { \
> > public: \
> > A(int a) : _a(a) { } \
> > int the_a() const { return _a; } \
> > private: \
> > int _a; \
> > };

>
> > MACRO_DEF

>
> > #define MACRO \
> > A a(1);\
> > std::cout << a.the_a() << std::endl;
> > However, I found that it is not easy to debug a macro. For example,
> > for the following program, I can not trace into the last macro in gdb.

>
> If you (who wrote the macro) have trouble debugging it, imagine the
> difficulties that the maintenance programmer will face - simply to
> understand what the macro is supposed to do. After all, what would you
> think if you had to debug a C++ program whose main() function looked
> like the main() in your program:
>
> > int main() {
> > MACRO

>
> > }

>
> Having to deal with code like this, in my experience, would be enough
> to convince most programmers that they really should be working -
> somewhere else.


Hi Greg,

I guest you misunderstood my OP. I have never said that my example is
the case that I should use macro.

I just raised that example to show the difficulty of debugging it. It
is not at all similar to the real code that I'm pondering whether I
should use macro or not.

I originally considered macro because there are somewhat redundancies
(not exactly the same) in that code that can not be refactored in with
extract method, etc. However, as macro is hard to debug, I end up with
writing a code generator to generated the somewhat redundant code.

Thanks,
Peng
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 19h22.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11577 seconds with 14 queries