Skip to content

Commit

Permalink
[clang-repl] Consider the scope spec in template lookups for deductio…
Browse files Browse the repository at this point in the history
…n guides.

isDeductionGuideName looks up the underlying template and if the template name
is qualified we miss that qualification resulting in an error. This issue
resurfaced in clang-repl where we call isDeductionGuideName more often to
distinguish between if we had a statement or declaration.

This patch passes the CXXScopeSpec information down to LookupTemplateName to
make the lookup more precise.

Differential revision: https://reviews.llvm.org/D147319
  • Loading branch information
vgvassilev committed May 8, 2023
1 parent 25cc5a7 commit 2c4620c
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -8081,7 +8081,7 @@ class Sema final {
/// Determine whether a particular identifier might be the name in a C++1z
/// deduction-guide declaration.
bool isDeductionGuideName(Scope *S, const IdentifierInfo &Name,
SourceLocation NameLoc,
SourceLocation NameLoc, CXXScopeSpec &SS,
ParsedTemplateTy *Template = nullptr);

bool DiagnoseUnknownTemplateName(const IdentifierInfo &II,
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3696,11 +3696,12 @@ void Parser::ParseDeclarationSpecifiers(

// Likewise, if this is a context where the identifier could be a template
// name, check whether this is a deduction guide declaration.
CXXScopeSpec SS;
if (getLangOpts().CPlusPlus17 &&
(DSContext == DeclSpecContext::DSC_class ||
DSContext == DeclSpecContext::DSC_top_level) &&
Actions.isDeductionGuideName(getCurScope(), *Tok.getIdentifierInfo(),
Tok.getLocation()) &&
Tok.getLocation(), SS) &&
isConstructorDeclarator(/*Unqualified*/ true,
/*DeductionGuide*/ true))
goto DoneWithDeclSpec;
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/Parse/ParseExprCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2911,9 +2911,9 @@ bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, ParsedType ObjectType,
if (!Ty)
return true;
Result.setConstructorName(Ty, IdLoc, IdLoc);
} else if (getLangOpts().CPlusPlus17 &&
AllowDeductionGuide && SS.isEmpty() &&
Actions.isDeductionGuideName(getCurScope(), *Id, IdLoc,
} else if (getLangOpts().CPlusPlus17 && AllowDeductionGuide &&
SS.isEmpty() &&
Actions.isDeductionGuideName(getCurScope(), *Id, IdLoc, SS,
&TemplateName)) {
// We have parsed a template-name naming a deduction guide.
Result.setDeductionGuideName(TemplateName, IdLoc);
Expand Down
5 changes: 2 additions & 3 deletions clang/lib/Parse/ParseTentative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,8 @@ bool Parser::isCXXDeclarationStatement(
switch (Tok.getKind()) {
case tok::identifier: {
IdentifierInfo *II = Tok.getIdentifierInfo();
bool isDeductionGuide =
Actions.isDeductionGuideName(getCurScope(), *II, Tok.getLocation(),
/*Template=*/nullptr);
bool isDeductionGuide = Actions.isDeductionGuideName(
getCurScope(), *II, Tok.getLocation(), SS, /*Template=*/nullptr);
if (Actions.isCurrentClassName(*II, getCurScope(), &SS) ||
isDeductionGuide) {
if (isConstructorDeclarator(/*Unqualified=*/SS.isEmpty(),
Expand Down
5 changes: 2 additions & 3 deletions clang/lib/Sema/SemaTemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,8 @@ TemplateNameKind Sema::isTemplateName(Scope *S,
}

bool Sema::isDeductionGuideName(Scope *S, const IdentifierInfo &Name,
SourceLocation NameLoc,
ParsedTemplateTy *Template) {
CXXScopeSpec SS;
SourceLocation NameLoc, CXXScopeSpec &SS,
ParsedTemplateTy *Template /*=nullptr*/) {
bool MemberOfUnknownSpecialization = false;

// We could use redeclaration lookup here, but we don't need to: the
Expand Down
4 changes: 4 additions & 0 deletions clang/test/Interpreter/disambiguate-decl-stmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ extern "C" int printf(const char*,...);

// Decls which are hard to disambiguate

// Templates
namespace ns1 { template<typename T> void tmplt(T &) {}}
int arg_tmplt = 12; ns1::tmplt(arg_tmplt);

// ParseStatementOrDeclaration returns multiple statements.
#ifdef MS
int g_bFlag = 1;
Expand Down

0 comments on commit 2c4620c

Please sign in to comment.