c++ - 转储 C++ 对象的内存布局在 clang 中不起作用

标签 c++ macos clang llvm

我读过一篇关于 dumping a C++ object's memory layout with Clang 的文章现在我正在尝试使用此功能。我创建了两个类:

// simple.cpp
struct Base
{
    int value;
};

struct Derived : Base
{
    int count;
};

int main(int argc, char* argv[])
{
    return 0;
}

并运行以下命令:

$ clang -cc1 -fdump-record-layouts-simple simple.cpp

一无所获。 clang 版本是:

$ clang++ -dumpversion
4.2.1

如果我运行以下命令:

$ clang -cc1 --help

在很多信息中我可以找到这个:

...
  -fdump-record-layouts-simple
                          Dump record layout information in a simple form used for testing
  -fdump-record-layouts   Dump record layout information
  -fdump-vtable-layouts   Dump the layouts of all vtables that will be emitted in a translation unit
...

我做错了什么?

最佳答案

您需要进行两项更改:

  1. 添加-emit-llvm 编译器开关。没有这个,就不需要 LLVM 输出,因此记录布局永远不会被计算,因此不会被转储。
  2. 在您的代码中使用类。如果从未使用过这些类,则不会为它们生成代码,也不会转储它们的布局。

进行这些更改后,像这样的输出将打印在标准输出上:

*** Dumping AST Record Layout
Type: struct Base

Layout: <ASTRecordLayout
  Size:32
  DataSize:32
  Alignment:32
  FieldOffsets: [0]>

*** Dumping AST Record Layout
Type: struct Derived

Layout: <ASTRecordLayout
  Size:64
  DataSize:64
  Alignment:32
  FieldOffsets: [32]>

...

Layout: <CGRecordLayout
  LLVMType:%struct.Base = type { i32 }
  NonVirtualBaseLLVMType:%struct.Base = type { i32 }
  IsZeroInitializable:1
  BitFields:[
]>

Layout: <CGRecordLayout
  LLVMType:%struct.Derived = type { %struct.Base, i32 }
  NonVirtualBaseLLVMType:%struct.Derived = type { %struct.Base, i32 }
  IsZeroInitializable:1
  BitFields:[
]>

关于c++ - 转储 C++ 对象的内存布局在 clang 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51005713/

相关文章:

C++:使用io.post和bind命令从boost线程在主线程中执行函数

c++ - QSystemTrayIcon 并不总是出现

ruby-on-rails - Errno::EACCES:运行捆绑时权限被拒绝,它命中 json 1.8.1,pg 0.17

javascript - 为什么在主进程和渲染进程中调用的 window.maximize() 与 macos 上的动画不同?

android - 如何为 Android 编译 C++14 代码?

c++ - 在 C++ 中通过实例对象调用静态方法的警告

c++ - 在 MSBuild 命令行上设置 VCCLCompilerTool.TreatWChar_tAsBuiltInType 属性

c - 为什么我的 openCL 构建在 OS X 上失败了?

c - 为什么 clang -Wempty-body 没有检测到这种情况?

c++ - NotImplementedException C++