[llvm-branch-commits] [clang] release/19.x: [clang-format] Fix annotation of braces enclosing stringification (#102998) (PR #103403)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Aug 13 12:47:22 PDT 2024


https://github.com/llvmbot created https://github.com/llvm/llvm-project/pull/103403

Backport ee2359968fa307ef45254c816e14df33374168cd

Requested by: @owenca

>From 62e19b39c71e79653918681acb5495d7e2598010 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Tue, 13 Aug 2024 12:39:33 -0700
Subject: [PATCH] [clang-format] Fix annotation of braces enclosing
 stringification (#102998)

Fixes #102937.

(cherry picked from commit ee2359968fa307ef45254c816e14df33374168cd)
---
 clang/lib/Format/UnwrappedLineParser.cpp      |  3 +++
 clang/unittests/Format/TokenAnnotatorTest.cpp | 11 +++++++++++
 2 files changed, 14 insertions(+)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index d406a531a5c0c2..688c7c5b1e977f 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -507,6 +507,9 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
     if (!Line->InMacroBody && !Style.isTableGen()) {
       // Skip PPDirective lines and comments.
       while (NextTok->is(tok::hash)) {
+        NextTok = Tokens->getNextToken();
+        if (NextTok->is(tok::pp_not_keyword))
+          break;
         do {
           NextTok = Tokens->getNextToken();
         } while (NextTok->NewlinesBefore == 0 && NextTok->isNot(tok::eof));
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 0b5475ea95989c..c20b50d14b80b1 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -3178,6 +3178,17 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
   EXPECT_BRACE_KIND(Tokens[17], BK_Block);
   EXPECT_BRACE_KIND(Tokens[22], BK_Block);
   EXPECT_BRACE_KIND(Tokens[26], BK_Block);
+
+  Tokens = annotate("{\n"
+                    "#define M(x) \\\n"
+                    "  return {#x};\n"
+                    "}");
+  ASSERT_EQ(Tokens.size(), 15u) << Tokens;
+  EXPECT_TOKEN(Tokens[0], tok::l_brace, TT_BlockLBrace);
+  EXPECT_BRACE_KIND(Tokens[0], BK_Block);
+  EXPECT_BRACE_KIND(Tokens[8], BK_BracedInit);
+  EXPECT_BRACE_KIND(Tokens[11], BK_BracedInit);
+  EXPECT_BRACE_KIND(Tokens[13], BK_Block);
 }
 
 TEST_F(TokenAnnotatorTest, UnderstandsElaboratedTypeSpecifier) {



More information about the llvm-branch-commits mailing list