When are SourceLocation's actually required in the AST

I've recently been generating a lot of clang ASTs programmatically and
I am wondering when SourceLocation's are supposed to be required in
the AST for clang to compile it correctly and when they are not. From
my experience, most of the time it works fine to just make it an
invalid clang::SourceLocation, but some of the time (three or four
instances I know of), it does not (e.g. template instantiations throw
assertion failures when the source location is invalid). So I'm
wondering if there is any rational on when a valid (even if
nonsensical - that always works fine) source location is required and
when not, so I know which are bugs and need fixing (which I'm happy to
do) and which are expected and I should simply work around.

Thanks,
Keno

My understanding is that the asserts were added to template instantiation because previously it transformed types without transforming location information. You ended up with QualTypes instead of TypeSourceInfo*'s or TypeLocs, which lost a lot of information. I think those asserts were added to make sure that the rest of the compiler continue to forward proper slocs. I don’t think they are truly needed for correctness.

I think Doug was responsible for those assertions. Maybe he has time now to answer questions, but maybe not. :slight_smile:

My understanding is that the asserts were added to template instantiation because previously it transformed types without transforming location information. You ended up with QualTypes instead of TypeSourceInfo*'s or TypeLocs, which lost a lot of information. I think those asserts were added to make sure that the rest of the compiler continue to forward proper slocs. I don’t think they are truly needed for correctness.

I think Doug was responsible for those assertions. Maybe he has time now to answer questions, but maybe not. :slight_smile:

That’s essentially the reason. Earlier on, we had some trouble with a loss of source-location information through template instantiation (and tree transformation in general), which manifest in poor diagnostics later on. That’s generally the reason to conjure up source locations that point at something related, because a diagnostic given an invalid source location is useless.

  • Doug