summaryrefslogtreecommitdiffstats
path: root/clang/lib/Parse/ParseCXXInlineMethods.cpp
diff options
context:
space:
mode:
authorOlivier Goffart <ogoffart@woboq.com>2016-11-03 07:36:17 +0000
committerOlivier Goffart <ogoffart@woboq.com>2016-11-03 07:36:17 +0000
commit3cd10137392dab3a5fbfdeab0465bd71996e034d (patch)
treea076e57f2403109caafd1f8a8a65f2ccdb608939 /clang/lib/Parse/ParseCXXInlineMethods.cpp
parent459d409358ac4552dca613431a05862c0dc4b5ca (diff)
downloadbcm5719-llvm-3cd10137392dab3a5fbfdeab0465bd71996e034d.tar.gz
bcm5719-llvm-3cd10137392dab3a5fbfdeab0465bd71996e034d.zip
Fix heuristics skipping invalid ctor-initializers with C++11
Use better heuristics to detect if a '{' might be the start of the constructor body or not. Especially when there is a completion token. Fix the test 'test/CodeCompletion/ctor-initializer.cpp ' when clang defaults to c++11 The problem was is how we recover invalid code in the ctor-init part as we skip the function body. In particular, we want to know if a '{' is the begining of the body. In C++03, we always consider it as the beginng of the body. The problem was that in C++11, it may be the start of an initializer, so we skip over it, causing further parse errors later. (It is important that we are able to parse correctly the rest of the class definition, to know what are the class member, for example) This commit is improving the heuristics to decide if the '{' is starting a function body. The rules are the following: If we are not in a template argument, and that the previous tokens are not an identifier, or a >, then it is much more likely to be the function body. We verify that further by checking the token after the matching '}' The commit also fix the behavior when there is a code_completion token in the ctor-initializers. Differential Revision: https://reviews.llvm.org/D21502 llvm-svn: 285883
Diffstat (limited to 'clang/lib/Parse/ParseCXXInlineMethods.cpp')
-rw-r--r--clang/lib/Parse/ParseCXXInlineMethods.cpp46
1 files changed, 37 insertions, 9 deletions
diff --git a/clang/lib/Parse/ParseCXXInlineMethods.cpp b/clang/lib/Parse/ParseCXXInlineMethods.cpp
index 39fcc827041..30fe374a728 100644
--- a/clang/lib/Parse/ParseCXXInlineMethods.cpp
+++ b/clang/lib/Parse/ParseCXXInlineMethods.cpp
@@ -832,22 +832,30 @@ bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
}
}
- if (Tok.isOneOf(tok::identifier, tok::kw_template)) {
+ if (Tok.is(tok::identifier)) {
Toks.push_back(Tok);
ConsumeToken();
- } else if (Tok.is(tok::code_completion)) {
- Toks.push_back(Tok);
- ConsumeCodeCompletionToken();
- // Consume the rest of the initializers permissively.
- // FIXME: We should be able to perform code-completion here even if
- // there isn't a subsequent '{' token.
- MightBeTemplateArgument = true;
- break;
} else {
break;
}
} while (Tok.is(tok::coloncolon));
+ if (Tok.is(tok::code_completion)) {
+ Toks.push_back(Tok);
+ ConsumeCodeCompletionToken();
+ if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_decltype)) {
+ // Could be the start of another member initializer (the ',' has not
+ // been written yet)
+ continue;
+ }
+ }
+
+ if (Tok.is(tok::comma)) {
+ // The initialization is missing, we'll diagnose it later.
+ Toks.push_back(Tok);
+ ConsumeToken();
+ continue;
+ }
if (Tok.is(tok::less))
MightBeTemplateArgument = true;
@@ -888,6 +896,26 @@ bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
// means the initializer is malformed; we'll diagnose it later.
if (!getLangOpts().CPlusPlus11)
return false;
+
+ const Token &PreviousToken = Toks[Toks.size() - 2];
+ if (!MightBeTemplateArgument &&
+ !PreviousToken.isOneOf(tok::identifier, tok::greater,
+ tok::greatergreater)) {
+ // If the opening brace is not preceded by one of these tokens, we are
+ // missing the mem-initializer-id. In order to recover better, we need
+ // to use heuristics to determine if this '{' is most likely the
+ // begining of a brace-init-list or the function body.
+ // Check the token after the corresponding '}'.
+ TentativeParsingAction PA(*this);
+ if (SkipUntil(tok::r_brace) &&
+ !Tok.isOneOf(tok::comma, tok::ellipsis, tok::l_brace)) {
+ // Consider there was a malformed initializer and this is the start
+ // of the function body. We'll diagnose it later.
+ PA.Revert();
+ return false;
+ }
+ PA.Revert();
+ }
}
// Grab the initializer (or the subexpression of the template argument).
OpenPOWER on IntegriCloud