[clang] Change `ASTUnit::getASTContext() const` to return a non-const `ASTContext` (PR #130096)
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Thu Mar 6 08:57:24 PST 2025
AaronBallman wrote:
> Thanks for the feedback!
>
> Is there a way to call `ASTContext::createMangleContext()` when I have a `const ASTUnit`? I've tried to make `ASTContext::createMangleContext()` const but this failed as it seems to actually call methods that potentially modify the context.
>
> However, when I have a `const Decl`, I can just call `getAstContext().createMangleContext()`.
There's two answers. In a world where we have const correctness, you shouldn't be able to (this is a misfeature on `Decl` that it has a `const` overload that returns a `const ASTContext &`). However, Clang inherited a lack of const correctness from the mists of time, so we have these sort of misfeatures still lurking (and will for quite some time). So for now, the recommendation is `const_cast` so it's clear where we're breaking const correctness.
https://github.com/llvm/llvm-project/pull/130096
More information about the cfe-commits
mailing list