首页 > 解决方案 > Assembler and hex dump change when formatting c++ code

问题描述

A C++ project I work on does not have consistent indentation. The lead developer told me it might not be safe to reformat the code. I thought it might not matter to the compiled code. As a test I tried reformatting one file using a Formatter based on the Eclipse "GNU [built-in]" Profile. When I recompiled the file the md5sum changed. I did a hexdump of the file and it showed one byte changed. I disassembled the object file. I also compiled with debugging and I got the source code line. I used diff to get the assembly instruction that changed.

The source was this line

        logErr
        << xmlutils.GetErrorMessage() << endl;

Below is the diff output showing the changed assembly

     23be:  89 04 24                mov    %eax,(%esp)
     23c1:  e8 fc ff ff ff          call   23c2 <_ZN12RerouteAdapt11WriteToFileERKSs+0x64>
     23c6:  e8 fc ff ff ff          call   23c7 <_ZN12RerouteAdapt11WriteToFileERKSs+0x69>
-    23cb:  c7 44 24 04 79 01 00    movl   $0x179,0x4(%esp)
+    23cb:  c7 44 24 04 84 01 00    movl   $0x184,0x4(%esp)
     23d2:  00 
     23d3:  89 04 24                mov    %eax,(%esp)
     23d6:  e8 fc ff ff ff          call   23d7 <_ZN12RerouteAdapt11WriteToFileERKSs+0x79>

The ordering of the headers was not changed by the reformat.

I know some C/C++, but very little about assembly. I was wondering if there was a simple explanation for why the object file would change. I thought the C++ compiler (GCC 4.8.2 on RHEL 7) was indifferent to formatting and white space. There were no differences besides this in the assembly.

标签: c++assembly

解决方案


托马斯和蒂姆是对的。更改的值对应于格式化前后的行号。我认为“logErr”只是一个流。原来它是一个使用宏的__LINE__宏。所以行号在程序集中是硬编码的。

#define logErr theTracer().SetFuncName(__func__); theTracer().SetFile(__FILE__); theTracer().SetLine(__LINE__); theTracer().SetError(); theTracer()

谢谢您的帮助。


推荐阅读