Skip to content

Commit 897cc8a

Browse files
xguptacor3ntin
andauthored
[RecursiveASTVisitor] Fix RecursiveASTVisitor (RAV) fails to visit the initializer of a bitfield (#69557)
The problem was introduced in the commit 6b8e3c0 when the possibility of initialized bitfields was added, but the logic in RecursiveASTVisitor was not updated. This PR fixed that. This fixes #64916. Patch by Scott McPeak --------- Co-authored-by: cor3ntin <[email protected]>
1 parent c285b7f commit 897cc8a

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,9 @@ Bug Fixes to AST Handling
650650
`Issue 64170 <https://github.com/llvm/llvm-project/issues/64170>`_
651651
- Fixed ``hasAnyBase`` not binding nodes in its submatcher.
652652
(`#65421 <https://github.com/llvm/llvm-project/issues/65421>`_)
653+
- Fixed a bug where RecursiveASTVisitor fails to visit the
654+
initializer of a bitfield.
655+
`Issue 64916 <https://github.com/llvm/llvm-project/issues/64916>`_
653656

654657
Miscellaneous Bug Fixes
655658
^^^^^^^^^^^^^^^^^^^^^^^

clang/include/clang/AST/RecursiveASTVisitor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2103,7 +2103,7 @@ DEF_TRAVERSE_DECL(FieldDecl, {
21032103
TRY_TO(TraverseDeclaratorHelper(D));
21042104
if (D->isBitField())
21052105
TRY_TO(TraverseStmt(D->getBitWidth()));
2106-
else if (D->hasInClassInitializer())
2106+
if (D->hasInClassInitializer())
21072107
TRY_TO(TraverseStmt(D->getInClassInitializer()));
21082108
})
21092109

clang/unittests/Tooling/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ add_clang_unittest(ToolingTests
2525
QualTypeNamesTest.cpp
2626
RangeSelectorTest.cpp
2727
RecursiveASTVisitorTests/Attr.cpp
28+
RecursiveASTVisitorTests/BitfieldInitializer.cpp
2829
RecursiveASTVisitorTests/CallbacksLeaf.cpp
2930
RecursiveASTVisitorTests/CallbacksUnaryOperator.cpp
3031
RecursiveASTVisitorTests/CallbacksBinaryOperator.cpp
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===- unittest/Tooling/RecursiveASTVisitorTests/BitfieldInitializer.cpp -===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "TestVisitor.h"
10+
#include <string>
11+
12+
using namespace clang;
13+
14+
namespace {
15+
16+
// Check to ensure that bitfield initializers are visited.
17+
class BitfieldInitializerVisitor
18+
: public ExpectedLocationVisitor<BitfieldInitializerVisitor> {
19+
public:
20+
bool VisitIntegerLiteral(IntegerLiteral *IL) {
21+
Match(std::to_string(IL->getValue().getSExtValue()), IL->getLocation());
22+
return true;
23+
}
24+
};
25+
26+
TEST(RecursiveASTVisitor, BitfieldInitializerIsVisited) {
27+
BitfieldInitializerVisitor Visitor;
28+
Visitor.ExpectMatch("123", 2, 15);
29+
EXPECT_TRUE(Visitor.runOver("struct S {\n"
30+
" int x : 8 = 123;\n"
31+
"};\n"));
32+
}
33+
34+
} // end anonymous namespace

0 commit comments

Comments
 (0)