[clang] 071f870 - [clang-format] Avoid parsing "requires" as a keyword in non-C++-like languages.

Marek Kurdej via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 22 07:55:43 PST 2022


Author: Marek Kurdej
Date: 2022-02-22T16:55:38+01:00
New Revision: 071f870e7ff0a3d04f0d93852ff7c29b59111f78

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

LOG: [clang-format] Avoid parsing "requires" as a keyword in non-C++-like languages.

Fixes the issue raised post-review in D113319 (cf. https://reviews.llvm.org/D113319#3337485).

Reviewed By: krasimir

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

Added: 
    

Modified: 
    clang/lib/Format/UnwrappedLineParser.cpp
    clang/unittests/Format/FormatTestJS.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 35465bf9a85b..e2cbcea14d7a 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -1563,9 +1563,13 @@ void UnwrappedLineParser::parseStructuralElement(IfStmtKind *IfKind,
       parseConcept();
       return;
     case tok::kw_requires: {
-      bool ParsedClause = parseRequires();
-      if (ParsedClause)
-        return;
+      if (Style.isCpp()) {
+        bool ParsedClause = parseRequires();
+        if (ParsedClause)
+          return;
+      } else {
+        nextToken();
+      }
       break;
     }
     case tok::kw_enum:

diff  --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp
index d84533e8a2b0..67df2d41731a 100644
--- a/clang/unittests/Format/FormatTestJS.cpp
+++ b/clang/unittests/Format/FormatTestJS.cpp
@@ -323,6 +323,7 @@ TEST_F(FormatTestJS, ReservedWords) {
   verifyFormat("var struct = 2;");
   verifyFormat("var union = 2;");
   verifyFormat("var interface = 2;");
+  verifyFormat("var requires = {};");
   verifyFormat("interface = 2;");
   verifyFormat("x = interface instanceof y;");
   verifyFormat("interface Test {\n"


        


More information about the cfe-commits mailing list