Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit 2b602ad

Browse files
committed
Introduce a new token kind 'cxx_defaultarg_end' to mark the end of C++ default arguments that were part of
lexed method declarations. This avoid interference with tokens coming after the point where the default arg tokens were 'injected', e.g. for typedef struct Inst { void m(int x=0); } *InstPtr; when parsing '0' the next token would be '*' and things would be messed up. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@110436 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent fcecd3c commit 2b602ad

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

include/clang/Basic/TokenKinds.def

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ TOK(unknown) // Not a token.
9696
TOK(eof) // End of file.
9797
TOK(eom) // End of macro (end of line inside a macro).
9898
TOK(code_completion) // Code completion marker
99+
TOK(cxx_defaultarg_end) // C++ default argument end marker
99100

100101
// C99 6.4.9: Comments.
101102
TOK(comment) // Comment (only in -E -C[C] mode)

lib/Parse/ParseCXXInlineMethods.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,13 @@ void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
142142
OwningExprResult DefArgResult(ParseAssignmentExpression());
143143
if (DefArgResult.isInvalid())
144144
Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
145-
else
145+
else {
146+
assert(Tok.is(tok::cxx_defaultarg_end) &&
147+
"We didn't parse the whole default arg!");
148+
ConsumeToken(); // Consume tok::cxx_defaultarg_end.
146149
Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
147150
move(DefArgResult));
151+
}
148152

149153
assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
150154
Tok.getLocation()) &&

lib/Parse/ParseDecl.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -3085,9 +3085,17 @@ void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
30853085
delete DefArgToks;
30863086
DefArgToks = 0;
30873087
Actions.ActOnParamDefaultArgumentError(Param);
3088-
} else
3088+
} else {
3089+
// Mark the end of the default argument so that we know when to
3090+
// stop when we parse it later on.
3091+
Token DefArgEnd;
3092+
DefArgEnd.startToken();
3093+
DefArgEnd.setKind(tok::cxx_defaultarg_end);
3094+
DefArgEnd.setLocation(Tok.getLocation());
3095+
DefArgToks->push_back(DefArgEnd);
30893096
Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
30903097
(*DefArgToks)[1].getLocation());
3098+
}
30913099
} else {
30923100
// Consume the '='.
30933101
ConsumeToken();

test/Parser/cxx-default-args.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ class C {
77
void m(int x = undecl + 0); // expected-error {{use of undeclared identifier 'undecl'}}
88
};
99

10+
typedef struct Inst {
11+
void m(int x=0);
12+
} *InstPtr;

0 commit comments

Comments
 (0)