[vtk-developers] possible bug in vtkErrorMacro and vtkWarningMacro

Moreland, Kenneth kmorel at sandia.gov
Fri May 25 09:47:05 EDT 2007


No, it shouldn't.  And if you add that, you will break a lot of code.
If you do a grep in VTK, you will find lots of lines that look like the
following:

vtkErrorMacro(<< "Something bad happened here.");

If you add in the proposed change, the hundreds of instances of this
format will break.  The ability to add << to the beginning of the macro
argument is nice so that you can line up the << on multi-line macros,
much like is customary for cout.  (Note that the following example is
meaningless unless your email viewer uses a monospace font).

vtkErrorMacro(<< "Something bad happened here because "
              << x << " is not " << y);

For short error messages, it is also common to leave out the << operator
altogether.

vtkErrorMacro("Something bad happened here.");

As Ken Martin just responded (as I was writing this), this works because
the C/C++ specification states that if two string constants are next to
each other, they get merged into a single string.  It's been an
important feature for useful macro expansions for decades.  The only
downside is that if your error message starts with something other than
a string constant, you have to add the << to the beginning.  But that's
fairly rare and easy to fix when it happens.

-Ken

> -----Original Message-----
> From: vtk-developers-bounces+kmorel=sandia.gov at vtk.org [mailto:vtk-
> developers-bounces+kmorel=sandia.gov at vtk.org] On Behalf Of Amy
Squillacote
> Sent: Friday, May 25, 2007 7:34 AM
> To: Vtk Developers
> Subject: [vtk-developers] possible bug in vtkErrorMacro and
> vtkWarningMacro
> 
> Hi All,
> 
> Earlier today, someone pointed out to me a possible bug in
vtkErrorMacro
> and vtkWarningMacro; see below.
> 
> #define vtkErrorMacro(x)                        \
>    vtkErrorWithObjectMacro(this,x)
> 
> //
> // This macro is used to print out errors
> // vtkErrorWithObjectMacro(self, << "Error message" << variable);
> //
> #define vtkErrorWithObjectMacro(self, x)                        \
>    {                                                            \
>    if (vtkObject::GetGlobalWarningDisplay())                    \
>      {                                                          \
>      vtkOStreamWrapper::EndlType endl;                          \
>      vtkOStreamWrapper::UseEndl(endl);                          \
>      vtkOStrStreamWrapper vtkmsg;                               \
>      vtkmsg << "ERROR: In " __FILE__ ", line " << __LINE__      \
>             << "\n" << self->GetClassName() << " (" << self     \
>             << "): " x << "\n\n";                               \
> <----- offending line
>     ...
>    }
> 
> Shouldn't the line indicated above actually be the following (adding
> "<<" before "x")?
> << "): " << x << "\n\n";                               \
> 
> Similar code can also be found in the vtkWarningMacro. It looks like a
> bug to me, but at the same time it's hard to believe that it's
actually
> a bug because of how much those two macros are successfully used.
> According to cvs annotate, that line has been that way since December
> 2002. Do you think it's a bug? If not, why not?
> 
> - Amy
> 
> --
> Amy Squillacote
> Kitware, Inc.
> 28 Corporate Drive
> Clifton Park, NY 12065
> Phone: (518) 371-3971 x106
> 
> _______________________________________________
> vtk-developers mailing list
> vtk-developers at vtk.org
> http://www.vtk.org/mailman/listinfo/vtk-developers





More information about the vtk-developers mailing list