Skip to content

Commit acf4e17

Browse files
authored
[clang] fix parsing of late parsed attributes with delayed template parsing (#163483)
This simply ignores 'delayed template parsing' when functions have late parsed attributes, since these are not MSVC compatible anyway. Besides ignoring the attribute, this would also cause a memory leak.
1 parent 2ed7baa commit acf4e17

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,8 @@ Bug Fixes to Attribute Support
426426
(#GH141504) and on types returned from indirect calls (#GH142453).
427427
- Fixes some late parsed attributes, when applied to function definitions, not being parsed
428428
in function try blocks, and some situations where parsing of the function body
429-
is skipped, such as error recovery and code completion. (#GH153551)
429+
is skipped, such as error recovery, code completion, and msvc-compatible delayed
430+
template parsing. (#GH153551)
430431
- Using ``[[gnu::cleanup(some_func)]]`` where some_func is annotated with
431432
``[[gnu::error("some error")]]`` now correctly triggers an error. (#GH146520)
432433
- Fix a crash when the function name is empty in the `swift_name` attribute. (#GH157075)

clang/lib/Parse/Parser.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,7 @@ Decl *Parser::ParseFunctionDefinition(ParsingDeclarator &D,
12721272
// tokens and store them for late parsing at the end of the translation unit.
12731273
if (getLangOpts().DelayedTemplateParsing && Tok.isNot(tok::equal) &&
12741274
TemplateInfo.Kind == ParsedTemplateKind::Template &&
1275-
Actions.canDelayFunctionBody(D)) {
1275+
LateParsedAttrs->empty() && Actions.canDelayFunctionBody(D)) {
12761276
MultiTemplateParamsArg TemplateParameterLists(*TemplateInfo.TemplateParams);
12771277

12781278
ParseScope BodyScope(this, Scope::FnScope | Scope::DeclScope |
@@ -1301,10 +1301,8 @@ Decl *Parser::ParseFunctionDefinition(ParsingDeclarator &D,
13011301
}
13021302
return DP;
13031303
}
1304-
else if (CurParsedObjCImpl &&
1305-
!TemplateInfo.TemplateParams &&
1306-
(Tok.is(tok::l_brace) || Tok.is(tok::kw_try) ||
1307-
Tok.is(tok::colon)) &&
1304+
if (CurParsedObjCImpl && !TemplateInfo.TemplateParams &&
1305+
(Tok.is(tok::l_brace) || Tok.is(tok::kw_try) || Tok.is(tok::colon)) &&
13081306
Actions.CurContext->isTranslationUnit()) {
13091307
ParseScope BodyScope(this, Scope::FnScope | Scope::DeclScope |
13101308
Scope::CompoundStmtScope);
@@ -1420,7 +1418,8 @@ Decl *Parser::ParseFunctionDefinition(ParsingDeclarator &D,
14201418

14211419
// Late attributes are parsed in the same scope as the function body.
14221420
if (LateParsedAttrs)
1423-
ParseLexedAttributeList(*LateParsedAttrs, Res, false, true);
1421+
ParseLexedAttributeList(*LateParsedAttrs, Res, /*EnterScope=*/false,
1422+
/*OnDefinition=*/true);
14241423

14251424
if (SkipFunctionBodies && (!Res || Actions.canSkipFunctionBody(Res)) &&
14261425
trySkippingFunctionBody()) {

clang/test/Parser/DelayedTemplateParsing.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ void undeclared()
4343

4444
}
4545

46-
template <class T> void foo5() {} //expected-note {{previous definition is here}}
46+
template <class T> void foo5() {} //expected-note {{previous definition is here}}
4747
template <class T> void foo5() {} // expected-error {{redefinition of 'foo5'}}
4848

49-
49+
5050

5151

5252
namespace PR11931 {
@@ -195,3 +195,12 @@ template <typename> struct PR38460_2 {
195195
}
196196
};
197197
template struct PR38460_2<int>;
198+
199+
namespace LateParsedAttrs {
200+
template <class>
201+
void f(int a) __attribute__((__diagnose_if__(a > 0, "foo", "error"))) {}
202+
// expected-note@-1 {{from 'diagnose_if' attribute on 'f<int>'}}
203+
void g() {
204+
f<int>(1); // expected-error {{foo}}
205+
}
206+
} // namespace LateParsedAttrs

0 commit comments

Comments
 (0)