This API is going to be used by LLDB to recreate owning module information for Decls deserializwed from DWARF. Because of the assertion I also need to make setFromASTFile public. Alternatively, we could remove the assertion.
Setting this after creating a Decl is not safe in general; declarations with this flag set have 8 bytes of additional storage allocated before their start.
An assert(FromASTFile || hasLocalOwningModuleStorage()); would at least catch the unsafe cases. This also needs a comment saying that it's not safe to use in general, and perhaps a scarier name to discourage people from calling it. (Unless you're creating declarations with ::CreateDeserialized -- in which case you'd need all the Decl subclasses to befriend the creator, making this function unnecessary -- this is currently only going to be safe if ModulesLocalVisibility is enabled.)
I took your suggestion to heart and changed the downstream LLDB patch to use CreateDeserialized() everywhere, which is frankly a better fit there anyway. To make this possible I had to make a few more setters public, see the updated patch.
I also discovered that DeclBase::operator new() now always allocates 2*4 bytes extra storage. I think it needs to do this for local submodule visibility. This makes my change to CreateDeserialized() not strictly necessary, but I think it is better anyway.
I'm surprised so few setters needed to be made public for this to work. If this required making lots more setters public, I'd be concerned, because we generally don't want large chunks of the AST to be mutable after creation. But this seems fine.
The storage isn't necessarily allocated if the decl is created with ::Create rather than ::CreateDeserialized. Just changing the comment to say the declaration must have been created with CreateDeserialized would be fine.