The Artima Developer Community
Sponsored Link

Prescriptions, Proscriptions, and Prognostications
Watch those spaces!
by Matthew Wilson
September 21, 2008
Summary
Having the computer help you write and maintain your code is an essential facet of successful software development, and all good consultants will recommended that you follow suit. (Even if, sometimes, they forget to do so themselves ...)

Advertisement

I'm always banging on to clients about the importance of periodically running code cleanup scripts over their code bases. One of the ones I recommended is a trailing spaces remover, essential for C/C++ code because of the dangerous effects it has on macros, and the concomitant difficulty of finding the problem(s). And yesterday I got caught by just such a situation, having failed to follow my own advice.

I'm in the process of adding some final touches to Pantheios before its (highly overdue) final 1.0 non-beta release. One of these is adding some examples illustrating how log statements may be targeted at certain back-ends by using the facilities for "overloading" the severity levels, i.e. combining a bona-fide severity level with additional information that may be used by a multiplexing/composite back-end to route the statement to specific concrete back-end(s). The details of what this all mean are somewhat outside this discussion, so suffice to say that it's the difference between, say

  #include <pantheios/pan.hpp> // gives us the 'pan' namespace alias, for brevity

  std::string general("general");

  pantheios::log(pan::notice, "A statement of ", general, " importance");
which is a candidate to be output by all back-ends (such as file, debugger, console, Syslog, ...), and, say
  #include <pantheios/pan.hpp> // gives us the 'pan' namespace alias, for brevity

  int beidSpecial = . . . // The identifier of the specific back-end to which the statement will be targetted

  std::string specific("specific");

  pantheios::log(pan::notice(beidSpecial), "A statement of ", specific, " importance");

These facilities have been available for the library for a very long-time, at least a year or so. But in writing the tutorial it became clear to me that users who're employing Pantheios in their C programs are somewhat left out in this regard. So I added the PANTHEIOS_MAKE_EXTENDED_SEVERITY() macro"

#define PANTHEIOS_MAKE_EXTENDED_SEVERITY(sev, xi28)   \
                                                      \
    (((sev) & 0x0f) | (((xi28) << 4) & ~0x0f))
Looks ok, yes? Except that I was on the receiving end of a brace of totally weird compiler errors, such as the following (from VC++ 9 x64):
H:\freelibs\pantheios\current\include\pantheios/pantheios.h(557) : error C2143: syntax error : missing ')' before '&'
H:\freelibs\pantheios\current\include\pantheios/pantheios.h(557) : error C2059: syntax error : ')'

I'm embarassed to admit that I had to sleep on this one before I had the mental wherewithall to realise my mistake, and to cause the IDE to show spaces. In that case the macro definition actually looks like the following (with the symbol « used to represent a space):

#define«PANTHEIOS_MAKE_EXTENDED_SEVERITY(sev,«xi28)«««\«««
««««««««««««««««««««««««««««««««««««««««««««««««««««««\
««««(((sev)«&«0x0f)«|«(((xi28)«<<«4)«&«~0x0f))

So, take the advice, as I will myself try to do more thoroughly, and strip those trailing spaces. I use a simple Perl script which is executed either by find (on UNIX) or for on Windows.

I'd love to hear from anyone who's had similar experiences, especially where they involve a little humbling stupidity. ;-)

Talk Back!

Have an opinion? Be the first to post a comment about this weblog entry.

RSS Feed

If you'd like to be notified whenever Matthew Wilson adds a new entry to his weblog, subscribe to his RSS feed.

About the Blogger

Matthew Wilson is a software development consultant and creator of the FastFormat, Pantheios and STLSoft libraries. He is author of the books Imperfect C++ (Addison-Wesley, October 2004) and Extended STL, volume 1 (Addison-Wesley, 2007), and is currently working on his third, Breaking Up The Monolith: Advanced C++ Design Without Compromise. He has published over 60 articles on C++ and other topics, and has served as columnist and contributing editor for C/C++ Users Journal. Matthew believes that code should be discoverable and largely self-documenting, and lives up to that by being a hopeless documentor. He can be contacted via http://www.imperfectcplusplus.com/ or stlsoft@gmail.com.

This weblog entry is Copyright © 2008 Matthew Wilson. All rights reserved.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use