Checking C++ codes for compatibility with operator overloading

Operator overloading allows the semantic extension of existing code without the need for sweeping code changes. For example, automatic differentiation tools in C++ commonly use this feature to enhance the code with additional derivative computation. To this end, a floating point data type is changed to a complex user-defined type. While conceptually straightforward, this type change often leads to compilation errors that can be tedious to decipher and resolve. This is due to the fact that the built-in floating point types in C++ are treated differently than user-defined types, and code constructs that are legal for floating point types can be a violation of the C++ standard for complex user-defined types. We identify and classify such problematic code constructs and suggest how the code can be changed to avoid these errors, while still allowing the use of operator overloading. To automatically flag such occurrences, we developed a Clang-based tool for the static analysis of C++ code based on our assessment of constructs problematic in operator overloading for numeric types. It automatically finds instances of problematic code locations and prints Lint-like warning messages. To showcase the relevance of this topic and the usefulness of our tool, we consider the basic routines of the OpenFOAM CFD software package, consisting of 1,476 C++ source and header files, for a total of over 150,000 lines of code. Altogether, we found 74 distinct occurrences of problematic code constructs in 21 files. As some of these files are included in over 400 different locations in the OpenFOAM base, errors in these files create a torrent of error messages that often are difficult to comprehend. In summary, the classification of problematic instances aids developers in writing numerical code that is fit for operator overloading and the tool helps programmers that augment legacy code in spotting problematic code constructs.

[1]  Guillaume Melquiond,et al.  The Boost Interval Arithmetic Library , 2003 .

[2]  Murray Hill,et al.  Lint, a C Program Checker , 1978 .

[3]  Jean Utke,et al.  Data Representation Alternatives in Semantically Augmented Numerical Models , 2006, 2006 Sixth IEEE International Workshop on Source Code Analysis and Manipulation.

[4]  Laurent Hascoët,et al.  The Tapenade automatic differentiation tool: Principles, model, and specification , 2013, TOMS.

[5]  Andreas Griewank,et al.  Evaluating derivatives - principles and techniques of algorithmic differentiation, Second Edition , 2000, Frontiers in applied mathematics.

[6]  Barry Smith,et al.  Making automatic differentiation truly automatic: coupling PETSc with ADIC , 2005, Future Gener. Comput. Syst..

[7]  C. Bendtsen FADBAD, a flexible C++ package for automatic differentiation - using the forward and backward method , 1996 .

[8]  Robin J. Hogan,et al.  Fast Reverse-Mode Automatic Differentiation using Expression Templates in C++ , 2014, ACM Trans. Math. Softw..

[9]  Christophe Denis,et al.  Numerical 'health check' for scientific codes: the CADNA approach , 2007, Comput. Phys. Commun..

[10]  Andreas Griewank,et al.  Algorithm 755: ADOL-C: a package for the automatic differentiation of algorithms written in C/C++ , 1996, TOMS.

[11]  Eric Rignot,et al.  Continental scale, high order, high spatial resolution, ice sheet modeling using the Ice Sheet System Model (ISSM) , 2012 .

[12]  Bjarne Stroustrup,et al.  C++ Programming Language , 1986, IEEE Softw..

[13]  Bjarne Stroustrup,et al.  The C++ Programming Language”, 3rd Edition, Pearson Education, 2007 , 2015 .

[14]  Uwe Naumann,et al.  A Discrete Adjoint Model for OpenFOAM , 2013, ICCS.

[15]  Todd L. Veldhuizen,et al.  Expression templates , 1996 .

[16]  Diane Kelly,et al.  Towards the profiling of scientific software for accuracy , 2011, CASCON.

[17]  Aleksandar Jemcov,et al.  OpenFOAM: A C++ Library for Complex Physics Simulations , 2007 .

[18]  Hyrum K. Wright,et al.  Large-Scale Automated Refactoring Using ClangMR , 2013, 2013 IEEE International Conference on Software Maintenance.

[19]  Vincent Lefèvre,et al.  MPFR: A multiple-precision binary floating-point library with correct rounding , 2007, TOMS.