Skip to content

[Clang] Workaround dependent source location issues #106925

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ Bug Fixes to C++ Support
- Template parameter names are considered in the name lookup of out-of-line class template
specialization right before its declaration context. (#GH64082)
- Fixed a constraint comparison bug for friend declarations. (#GH78101)
- Fix an issue with dependent source location expressions (#GH106428), (#GH81155), (#GH80210), (#GH85373)

Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
21 changes: 17 additions & 4 deletions clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5443,11 +5443,24 @@ struct EnsureImmediateInvocationInDefaultArgs

// Rewrite to source location to refer to the context in which they are used.
ExprResult TransformSourceLocExpr(SourceLocExpr *E) {
if (E->getParentContext() == SemaRef.CurContext)
DeclContext *DC = E->getParentContext();
if (DC == SemaRef.CurContext)
return E;
return getDerived().RebuildSourceLocExpr(E->getIdentKind(), E->getType(),
E->getBeginLoc(), E->getEndLoc(),
SemaRef.CurContext);

// FIXME: During instantiation, because the rebuild of defaults arguments
// is not always done in the context of the template instantiator,
// we run the risk of producing a dependent source location
// that would never be rebuilt.
// This usually happen during overload resolution, or in contexts
// where the value of the source location does not matter.
// However, we should find a better way to deal with source location
// of function template.
if (!SemaRef.CurrentInstantiationScope ||
!SemaRef.CurContext->isDependentContext() || DC->isDependentContext())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure the DC is always not a nullptr?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, there is always a declaration context (the top one being that of the translation unit)

DC = SemaRef.CurContext;

return getDerived().RebuildSourceLocExpr(
E->getIdentKind(), E->getType(), E->getBeginLoc(), E->getEndLoc(), DC);
}
};

Expand Down
60 changes: 60 additions & 0 deletions clang/test/SemaCXX/source_location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -929,3 +929,63 @@ void test() {
}

}

namespace GH106428 {

struct add_fn {
template <typename T>
constexpr auto operator()(T lhs, T rhs,
const std::source_location loc = std::source_location::current())
const -> T
{
return lhs + rhs;
}
};


template <class _Fp, class... _Args>
decltype(_Fp{}(0, 0))
__invoke(_Fp&& __f);

template<typename T>
struct type_identity { using type = T; };

template<class Fn>
struct invoke_result : type_identity<decltype(__invoke(Fn{}))> {};

using i = invoke_result<add_fn>::type;
static_assert(__is_same(i, int));

}

#if __cplusplus >= 202002L

namespace GH81155 {
struct buff {
buff(buff &, const char * = __builtin_FUNCTION());
};

template <class Ty>
Ty declval();

template <class Fx>
auto Call(buff arg) -> decltype(Fx{}(arg));

template <typename>
struct F {};

template <class Fx>
struct InvocableR : F<decltype(Call<Fx>(declval<buff>()))> {
static constexpr bool value = false;
};

template <class Fx, bool = InvocableR<Fx>::value>
void Help(Fx) {}

void Test() {
Help([](buff) {});
}

}

#endif
Loading