c++ - 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.
解决方案
托马斯和蒂姆是对的。更改的值对应于格式化前后的行号。我认为“logErr”只是一个流。原来它是一个使用宏的__LINE__
宏。所以行号在程序集中是硬编码的。
#define logErr theTracer().SetFuncName(__func__); theTracer().SetFile(__FILE__); theTracer().SetLine(__LINE__); theTracer().SetError(); theTracer()
谢谢您的帮助。
推荐阅读
- android - 如何在飞镖中使用正则表达式对字符串列表进行排序?
- android - 在 android:onClick 的父上下文或祖先上下文中找不到方法值(视图)
- r - R Shiny 调试 - 发现没有文件和行号参考的错误
- laravel - 从特定模型变形关系中选择特定列
- django - 我正在从 API 调用中提取字典,当我提取 URL 信息时,它会在它们周围加上引号和括号
- python - 如何打包我的 python 程序,以便用户可以使用 setup.py 安装它
- javascript - 如何在javascript中按单词序列匹配两个字符串?
- flutter - Flutter:如何在没有小部件的情况下跟踪应用程序生命周期
- javascript - 隐藏特定用户角色 Wordpress 的类 CSS
- python - Sublime Text 不能与 Anaconda 一起编译