Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,8 @@ Fixed Point Support in Clang
AST Matchers
------------

- Ensure ``hasBitWidth`` doesn't crash on bit widths that are dependent on template
parameters.
- Ensure ``isDerivedFrom`` matches the correct base in case more than one alias exists.
- Extend ``templateArgumentCountIs`` to support function and variable template
specialization.
Expand Down
5 changes: 5 additions & 0 deletions clang/include/clang/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -3234,6 +3234,11 @@ class FieldDecl : public DeclaratorDecl, public Mergeable<FieldDecl> {
return hasInClassInitializer() ? InitAndBitWidth->BitWidth : BitWidth;
}

/// Determines whether the bit width of this field is a constant integer.
/// This may not always be the case, such as inside template-dependent
/// expressions.
bool hasConstantIntegerBitWidth() const;

/// Computes the bit width of this field, if this is a bit field.
/// May not be called on non-bitfields.
/// Note that in order to successfully use this function, the bitwidth
Expand Down
3 changes: 2 additions & 1 deletion clang/include/clang/ASTMatchers/ASTMatchers.h
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,8 @@ AST_MATCHER(FieldDecl, isBitField) {
/// fieldDecl(hasBitWidth(2))
/// matches 'int a;' and 'int c;' but not 'int b;'.
AST_MATCHER_P(FieldDecl, hasBitWidth, unsigned, Width) {
return Node.isBitField() && Node.getBitWidthValue() == Width;
return Node.isBitField() && Node.hasConstantIntegerBitWidth() &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you are only testing the Node.hasConstantIntegerBitWidth() == true case in the test below. If that is correct we should add a test that covers both cases and any other combinations of these conditions not covered by testing.

Node.getBitWidthValue() == Width;
}

/// Matches non-static data members that have an in-class initializer.
Expand Down
9 changes: 6 additions & 3 deletions clang/lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4677,11 +4677,14 @@ void FieldDecl::setLazyInClassInitializer(LazyDeclStmtPtr NewInit) {
Init = NewInit;
}

bool FieldDecl::hasConstantIntegerBitWidth() const {
const auto *CE = dyn_cast_if_present<ConstantExpr>(getBitWidth());
return CE && CE->getAPValueResult().isInt();
}

unsigned FieldDecl::getBitWidthValue() const {
assert(isBitField() && "not a bitfield");
assert(isa<ConstantExpr>(getBitWidth()));
assert(cast<ConstantExpr>(getBitWidth())->hasAPValueResult());
assert(cast<ConstantExpr>(getBitWidth())->getAPValueResult().isInt());
assert(hasConstantIntegerBitWidth());
return cast<ConstantExpr>(getBitWidth())
->getAPValueResult()
.getInt()
Expand Down
13 changes: 13 additions & 0 deletions clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2925,6 +2925,19 @@ TEST_P(ASTMatchersTest, IsBitField) {
fieldDecl(isBitField(), hasName("b"))));
EXPECT_TRUE(matches("struct C { int a : 2; int b : 4; };",
fieldDecl(isBitField(), hasBitWidth(2), hasName("a"))));
if (GetParam().isCXX()) {
// This test verifies 2 things:
// (1) That templates work correctly.
// (2) That the matcher does not crash on template-dependent bit widths.
EXPECT_TRUE(matches("template<int N> "
"struct C { "
"explicit C(bool x) : a(x) { } "
"int a : N; "
"int b : 4; "
"}; "
"template struct C<2>;",
fieldDecl(isBitField(), hasBitWidth(2), hasName("a"))));
}
}

TEST_P(ASTMatchersTest, HasInClassInitializer) {
Expand Down