[llvm-branch-commits] [clang] c8d8248 - [clang-format] Improve detection of parameter declarations in K&R C

Tom Stellard via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Sep 9 20:03:57 PDT 2021


Author: owenca
Date: 2021-09-09T20:02:55-07:00
New Revision: c8d8248488a387d8e948ddfcbef0f1efa8c30f65

URL: https://github.com/llvm/llvm-project/commit/c8d8248488a387d8e948ddfcbef0f1efa8c30f65
DIFF: https://github.com/llvm/llvm-project/commit/c8d8248488a387d8e948ddfcbef0f1efa8c30f65.diff

LOG: [clang-format] Improve detection of parameter declarations in K&R C

Clean up the detection of parameter declarations in K&R C function
definitions. Also make it more precise by requiring the second
token after the r_paren to be either a star or keyword/identifier.

Differential Revision: https://reviews.llvm.org/D108094

(cherry picked from commmit 643f2be7b6afd91d9f0d6df89cd3391835763112)

Added: 
    

Modified: 
    clang/lib/Format/UnwrappedLineParser.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 49a5b4b6dc3e8..673986d16af2f 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -1008,14 +1008,26 @@ static bool isC78Type(const FormatToken &Tok) {
 //   {
 //      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;
+
+  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;
   if (!Tok || Tok->isNot(tok::r_paren))
     return false;
@@ -1378,21 +1390,11 @@ void UnwrappedLineParser::parseStructuralElement(bool IsTopLevel) {
       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))
-        break;
-      const FormatToken *PrevPrev = Previous->Previous;
-      if (!PrevPrev || (!isC78Type(*PrevPrev) && PrevPrev->isNot(tok::star)))
+      if (!IsTopLevel || !Style.isCpp() || !Previous || FormatTok->is(tok::eof))
         break;
       const unsigned Position = Tokens->getPosition() + 1;
-      if (Position == AllTokens.size())
-        break;
       assert(Position < AllTokens.size());
-      const FormatToken *Next = AllTokens[Position];
-      if (Next && Next->isOneOf(tok::l_paren, tok::semi))
-        break;
-      if (isC78ParameterDecl(FormatTok)) {
+      if (isC78ParameterDecl(FormatTok, AllTokens[Position], Previous)) {
         addUnwrappedLine();
         return;
       }


        


More information about the llvm-branch-commits mailing list