Skip to content

Commit

Permalink
[RecursiveASTVisitor] Fix RecursiveASTVisitor (RAV) fails to visit th…
Browse files Browse the repository at this point in the history
…e initializer of a bitfield (llvm#69557)

The problem was introduced in the commit
llvm@6b8e3c0
when the possibility of initialized bitfields was added, but the logic
in RecursiveASTVisitor was not updated. This PR fixed that.

This fixes llvm#64916.

Patch by Scott McPeak

---------

Co-authored-by: cor3ntin <corentinjabot@gmail.com>
  • Loading branch information
xgupta and cor3ntin committed Oct 26, 2023
1 parent c285b7f commit 897cc8a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
3 changes: 3 additions & 0 deletions clang/docs/ReleaseNotes.rst
Expand Up @@ -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 fails to visit the
initializer of a bitfield.
`Issue 64916 <https://github.com/llvm/llvm-project/issues/64916>`_

Miscellaneous Bug Fixes
^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/AST/RecursiveASTVisitor.h
Expand Up @@ -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()));
})

Expand Down
1 change: 1 addition & 0 deletions clang/unittests/Tooling/CMakeLists.txt
Expand Up @@ -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
Expand Down
@@ -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

0 comments on commit 897cc8a

Please sign in to comment.