[clang-format] Dont interpret variable named interface as keyword for C++
authorJorge Pinto Sousa <jorge.pinto.sousa@proton.me>
Sun, 16 Apr 2023 10:17:40 +0000 (03:17 -0700)
committerOwen Pan <owenpiano@gmail.com>
Sun, 16 Apr 2023 10:23:53 +0000 (03:23 -0700)
Fixes #53173.

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

clang/lib/Format/UnwrappedLineParser.cpp
clang/unittests/Format/FormatTest.cpp

index e47d634..9158333 100644 (file)
@@ -1881,7 +1881,7 @@ void UnwrappedLineParser::parseStructuralElement(
         }
       }
 
-      if (FormatTok->is(Keywords.kw_interface)) {
+      if (!Style.isCpp() && FormatTok->is(Keywords.kw_interface)) {
         if (parseStructLike())
           return;
         break;
index 4b7fddd..c5c1f21 100644 (file)
@@ -25429,6 +25429,13 @@ TEST_F(FormatTest, SpaceAfterUDL) {
   verifyFormat("auto x = 5s .count() == 5;");
 }
 
+TEST_F(FormatTest, InterfaceAsClassMemberName) {
+  verifyFormat("class Foo {\n"
+               "  int interface;\n"
+               "  Foo::Foo(int iface) : interface{iface} {}\n"
+               "}");
+}
+
 } // namespace
 } // namespace test
 } // namespace format