aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm-project/clang/lib/Format/UnwrappedLineParser.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/llvm-project/clang/lib/Format/UnwrappedLineParser.cpp')
-rw-r--r--contrib/llvm-project/clang/lib/Format/UnwrappedLineParser.cpp41
1 files changed, 29 insertions, 12 deletions
diff --git a/contrib/llvm-project/clang/lib/Format/UnwrappedLineParser.cpp b/contrib/llvm-project/clang/lib/Format/UnwrappedLineParser.cpp
index beb2df59cd53..424c6ce0ce8c 100644
--- a/contrib/llvm-project/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/contrib/llvm-project/clang/lib/Format/UnwrappedLineParser.cpp
@@ -995,6 +995,13 @@ static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords,
Keywords.kw_import, tok::kw_export);
}
+// Checks whether a token is a type in K&R C (aka C78).
+static bool isC78Type(const FormatToken &Tok) {
+ return Tok.isOneOf(tok::kw_char, tok::kw_short, tok::kw_int, tok::kw_long,
+ tok::kw_unsigned, tok::kw_float, tok::kw_double,
+ tok::identifier);
+}
+
// This function checks whether a token starts the first parameter declaration
// in a K&R C (aka C78) function definition, e.g.:
// int f(a, b)
@@ -1002,13 +1009,24 @@ static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords,
// {
// return a + b;
// }
-static bool isC78ParameterDecl(const FormatToken *Tok) {
- if (!Tok)
+static bool isC78ParameterDecl(const FormatToken *Tok, const FormatToken *Next,
+ const FormatToken *FuncName) {
+ assert(Tok);
+ assert(Next);
+ assert(FuncName);
+
+ if (FuncName->isNot(tok::identifier))
return false;
- if (!Tok->isOneOf(tok::kw_int, tok::kw_char, tok::kw_float, tok::kw_double,
- tok::kw_struct, tok::kw_union, tok::kw_long, tok::kw_short,
- tok::kw_unsigned, tok::kw_register, tok::identifier))
+ const FormatToken *Prev = FuncName->Previous;
+ if (!Prev || (Prev->isNot(tok::star) && !isC78Type(*Prev)))
+ return false;
+
+ if (!isC78Type(*Tok) &&
+ !Tok->isOneOf(tok::kw_register, tok::kw_struct, tok::kw_union))
+ return false;
+
+ if (Next->isNot(tok::star) && !Next->Tok.getIdentifierInfo())
return false;
Tok = Tok->Previous;
@@ -1369,21 +1387,20 @@ void UnwrappedLineParser::parseStructuralElement(bool IsTopLevel) {
case tok::r_brace:
addUnwrappedLine();
return;
- case tok::l_paren:
+ case tok::l_paren: {
parseParens();
// Break the unwrapped line if a K&R C function definition has a parameter
// declaration.
- if (!IsTopLevel || !Style.isCpp())
- break;
- if (!Previous || Previous->isNot(tok::identifier))
+ if (!IsTopLevel || !Style.isCpp() || !Previous || FormatTok->is(tok::eof))
break;
- if (Previous->Previous && Previous->Previous->is(tok::at))
- break;
- if (isC78ParameterDecl(FormatTok)) {
+ const unsigned Position = Tokens->getPosition() + 1;
+ assert(Position < AllTokens.size());
+ if (isC78ParameterDecl(FormatTok, AllTokens[Position], Previous)) {
addUnwrappedLine();
return;
}
break;
+ }
case tok::kw_operator:
nextToken();
if (FormatTok->isBinaryOperator())