[cfe-users] Manipulating the DeclContext

Connor Kuehl via cfe-users cfe-users at lists.llvm.org
Sun Mar 10 21:07:20 PDT 2019


I'm looking for some help in understanding how a DeclContext is created within Clang. More specifically, I'm curious to know how and when a DeclContext's members are added so that I can safely manipulate their order. If I understand it correctly, this order is based on a linked list which is iterated over when it's time to construct the layout.
The first member of the linked list being FirstDecl: https://clang.llvm.org/doxygen/classclang_1_1DeclContext.html#a6c8abf07fec5ca0ea9473e4df302191f
The linked list being generated with BuildDeclChain: https://clang.llvm.org/doxygen/classclang_1_1DeclContext.html#aeed74fdf591aa2cbf15c4170499b64da
And of course, iterating the fields with the begin and end iterators: https://clang.llvm.org/doxygen/classclang_1_1RecordDecl.html#afc36412f0657ec2e9810e6f9d321b29a
I'm trying to rearrange the order of fields within a RecordDecl before the record layout is finalized in lib/AST/RecordLayoutBuilder.cpp in which the record builder implementations do all of the low-level offset calculations based on the traversal of that linked list. I'm avoiding providing an ExternalASTSource that overrides the structure layout on the virtue that the only thing here that's changing is the order of the fields and it seems wasteful to re-implement the RecordLayoutBuilders' battle-hardened code in the ExternalASTSource to calculate the offsets.
I've got a basic version of this "working" for simple projects where fields are reordered as I want them to. However, I know there's a lot of machinery in the compiler and _I also don't know what I don't know_.
Compiling more complicated projects, like the Linux kernel, results in a seg-faulting compiler when it comes to code generation on a call to "clang::TagType::getDecl()", and this is where my question stems from.
The current approach involves modifying BuildDeclChain to set a tail pointer: https://github.com/clang-randstruct/llvm-project/commit/3b49de55975539e72279c7c45a4679c57aceea2a
Followed by invoking it with a new order of the FieldDecls: https://github.com/clang-randstruct/llvm-project/commit/af8ec9eafa1379a89a73183b88a46fb047c97d99#diff-88e7d1cb846dd215c4ab0a4fc7b5b57eR55
(Though I believe the boolean argument in that method call is wrong)
I think I'm compromising the integrity of the DeclContext somehow and am hoping for some guidance on mending it.
Any advice or further questions are greatly appreciated. Thank you.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20190310/cb7ed165/attachment.html>

More information about the cfe-users mailing list