[clang] [RecursiveASTVisitor] Fix RecursiveASTVisitor (RAV) fails to visit the initializer of a bitfield (PR #69557)

Shivam Gupta via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 25 20:20:36 PDT 2023


https://github.com/xgupta updated https://github.com/llvm/llvm-project/pull/69557

>From 2e363be5e79e2aeeb219628db0c917e530e04d99 Mon Sep 17 00:00:00 2001
From: Shivam Gupta <shivam98.tkg at gmail.com>
Date: Thu, 19 Oct 2023 09:06:43 +0530
Subject: [PATCH 1/3] [RecursiveASTVisitor] Fix RecursiveASTVisitor (RAV) fails
 to visit the initializer of a bitfield

Patch by Scott McPeak
---
 clang/include/clang/AST/RecursiveASTVisitor.h |  2 +-
 clang/unittests/Tooling/CMakeLists.txt        |  1 +
 .../BitfieldInitializer.cpp                   | 34 +++++++++++++++++++
 3 files changed, 36 insertions(+), 1 deletion(-)
 create mode 100644 clang/unittests/Tooling/RecursiveASTVisitorTests/BitfieldInitializer.cpp

diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h
index 3dd23eb38eeabfc..53bc15e1b19f668 100644
--- a/clang/include/clang/AST/RecursiveASTVisitor.h
+++ b/clang/include/clang/AST/RecursiveASTVisitor.h
@@ -2103,7 +2103,7 @@ DEF_TRAVERSE_DECL(FieldDecl, {
   TRY_TO(TraverseDeclaratorHelper(D));
   if (D->isBitField())
     TRY_TO(TraverseStmt(D->getBitWidth()));
-  else if (D->hasInClassInitializer())
+  if (D->hasInClassInitializer())
     TRY_TO(TraverseStmt(D->getInClassInitializer()));
 })
 
diff --git a/clang/unittests/Tooling/CMakeLists.txt b/clang/unittests/Tooling/CMakeLists.txt
index 2fbe78e3fab7528..5a10a6b285390e9 100644
--- a/clang/unittests/Tooling/CMakeLists.txt
+++ b/clang/unittests/Tooling/CMakeLists.txt
@@ -25,6 +25,7 @@ add_clang_unittest(ToolingTests
   QualTypeNamesTest.cpp
   RangeSelectorTest.cpp
   RecursiveASTVisitorTests/Attr.cpp
+  RecursiveASTVisitorTests/BitfieldInitializer.cpp
   RecursiveASTVisitorTests/CallbacksLeaf.cpp
   RecursiveASTVisitorTests/CallbacksUnaryOperator.cpp
   RecursiveASTVisitorTests/CallbacksBinaryOperator.cpp
diff --git a/clang/unittests/Tooling/RecursiveASTVisitorTests/BitfieldInitializer.cpp b/clang/unittests/Tooling/RecursiveASTVisitorTests/BitfieldInitializer.cpp
new file mode 100644
index 000000000000000..676a491a43040ea
--- /dev/null
+++ b/clang/unittests/Tooling/RecursiveASTVisitorTests/BitfieldInitializer.cpp
@@ -0,0 +1,34 @@
+//===- unittest/Tooling/RecursiveASTVisitorTests/BitfieldInitializer.cpp -===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "TestVisitor.h"
+#include <string>
+
+using namespace clang;
+
+namespace {
+
+// Check to ensure that bitfield initializers are visited.
+class BitfieldInitializerVisitor : public ExpectedLocationVisitor<BitfieldInitializerVisitor> {
+public:
+  bool VisitIntegerLiteral(IntegerLiteral *IL) {
+    Match(std::to_string(IL->getValue().getSExtValue()), IL->getLocation());
+    return true;
+  }
+};
+
+TEST(RecursiveASTVisitor, BitfieldInitializerIsVisited) {
+  BitfieldInitializerVisitor Visitor;
+  Visitor.ExpectMatch("123", 2, 15); 
+  EXPECT_TRUE(Visitor.runOver(
+    "struct S {\n"
+    "  int x : 8 = 123;\n"
+    "};\n"));
+}
+
+} // end anonymous namespace

>From a6fa113206562f373f3aba81ce81acd5d9dcf9d1 Mon Sep 17 00:00:00 2001
From: Shivam Gupta <shivam98.tkg at gmail.com>
Date: Thu, 19 Oct 2023 09:24:38 +0530
Subject: [PATCH 2/3] clang-format

---
 .../RecursiveASTVisitorTests/BitfieldInitializer.cpp | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/clang/unittests/Tooling/RecursiveASTVisitorTests/BitfieldInitializer.cpp b/clang/unittests/Tooling/RecursiveASTVisitorTests/BitfieldInitializer.cpp
index 676a491a43040ea..c11e726fe855284 100644
--- a/clang/unittests/Tooling/RecursiveASTVisitorTests/BitfieldInitializer.cpp
+++ b/clang/unittests/Tooling/RecursiveASTVisitorTests/BitfieldInitializer.cpp
@@ -14,7 +14,8 @@ using namespace clang;
 namespace {
 
 // Check to ensure that bitfield initializers are visited.
-class BitfieldInitializerVisitor : public ExpectedLocationVisitor<BitfieldInitializerVisitor> {
+class BitfieldInitializerVisitor
+    : public ExpectedLocationVisitor<BitfieldInitializerVisitor> {
 public:
   bool VisitIntegerLiteral(IntegerLiteral *IL) {
     Match(std::to_string(IL->getValue().getSExtValue()), IL->getLocation());
@@ -24,11 +25,10 @@ class BitfieldInitializerVisitor : public ExpectedLocationVisitor<BitfieldInitia
 
 TEST(RecursiveASTVisitor, BitfieldInitializerIsVisited) {
   BitfieldInitializerVisitor Visitor;
-  Visitor.ExpectMatch("123", 2, 15); 
-  EXPECT_TRUE(Visitor.runOver(
-    "struct S {\n"
-    "  int x : 8 = 123;\n"
-    "};\n"));
+  Visitor.ExpectMatch("123", 2, 15);
+  EXPECT_TRUE(Visitor.runOver("struct S {\n"
+                              "  int x : 8 = 123;\n"
+                              "};\n"));
 }
 
 } // end anonymous namespace

>From 1ce8c2ca7bf5110e4bfb11eeaaa891183e73f2e5 Mon Sep 17 00:00:00 2001
From: Shivam Gupta <shivam98.tkg at gmail.com>
Date: Wed, 25 Oct 2023 22:16:34 +0530
Subject: [PATCH 3/3] Added release note

---
 clang/docs/ReleaseNotes.rst | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 42f20b9a9bb0410..a1f373b167a33cb 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -650,6 +650,9 @@ Bug Fixes to AST Handling
   `Issue 64170 <https://github.com/llvm/llvm-project/issues/64170>`_
 - Fixed ``hasAnyBase`` not binding nodes in its submatcher.
   (`#65421 <https://github.com/llvm/llvm-project/issues/65421>`_)
+- Fixed a bug where RecursiveASTVisitor (RAV) fails to visit the
+  initializer of a bitfield.
+  `Issue 64916 <https://github.com/llvm/llvm-project/issues/64916>`_
 
 Miscellaneous Bug Fixes
 ^^^^^^^^^^^^^^^^^^^^^^^



More information about the cfe-commits mailing list