[clang] [clang-format] Correctly annotate pointer/reference in _Generic (PR #133673)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Sun Mar 30 19:33:37 PDT 2025
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/133673
Fix #133663
>From 1ab37d1726be943206c9e1b576468a9d02594783 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Sun, 30 Mar 2025 19:29:39 -0700
Subject: [PATCH] [clang-format] Correctly annotate pointer/reference in
_Generic
Fix #133663
---
clang/lib/Format/TokenAnnotator.cpp | 3 +++
clang/unittests/Format/TokenAnnotatorTest.cpp | 4 ++++
2 files changed, 7 insertions(+)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index d87b3a6088bd8..7bdcc04901f00 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -1417,6 +1417,9 @@ class AnnotatingParser {
}
} else if (Contexts.back().ContextType == Context::C11GenericSelection) {
Tok->setType(TT_GenericSelectionColon);
+ auto *Prev = Tok->getPreviousNonComment();
+ if (Prev && Prev->isPointerOrReference())
+ Prev->setFinalizedType(TT_PointerOrReference);
} else if (CurrentToken && CurrentToken->is(tok::numeric_constant)) {
Tok->setType(TT_BitFieldColon);
} else if (Contexts.size() == 1 &&
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index ac5e979aea071..af9fd574b068c 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -363,6 +363,10 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) {
ASSERT_EQ(Tokens.size(), 20u) << Tokens;
EXPECT_TOKEN(Tokens[14], tok::star, TT_PointerOrReference);
+ Tokens = annotate("#define foo(x) _Generic(x, bar *: 1, default: 0)");
+ ASSERT_EQ(Tokens.size(), 20u) << Tokens;
+ EXPECT_TOKEN(Tokens[11], tok::star, TT_PointerOrReference);
+
Tokens = annotate("Thingy kConfig = {\n"
" 1,\n"
" (uint16_t)(kScale * height_pixels),\n"
More information about the cfe-commits
mailing list