Skip to content

Commit 6051fec

Browse files
committed
[clang][analyzer]: fix 'clang-analyzer-optin.cplusplus.UninitializedObject false positive with unnamed fields'
1 parent 5ecbf46 commit 6051fec

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

clang/lib/StaticAnalyzer/Checkers/UninitializedObject/UninitializedObjectChecker.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717
//
1818
//===----------------------------------------------------------------------===//
1919

20-
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
2120
#include "UninitializedObject.h"
2221
#include "clang/ASTMatchers/ASTMatchFinder.h"
2322
#include "clang/Driver/DriverDiagnostic.h"
23+
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
2424
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
2525
#include "clang/StaticAnalyzer/Core/Checker.h"
2626
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
2727
#include "clang/StaticAnalyzer/Core/PathSensitive/DynamicType.h"
28+
#include "clang/include/clang/AST/Decl.h"
2829

2930
using namespace clang;
3031
using namespace clang::ento;
@@ -291,7 +292,10 @@ bool FindUninitializedFields::isNonUnionUninit(const TypedValueRegion *R,
291292

292293
// Are all of this non-union's fields initialized?
293294
for (const FieldDecl *I : RD->fields()) {
294-
295+
// Skip checking for unnamed bitfield
296+
if (I->isUnnamedBitField()) {
297+
continue;
298+
}
295299
const auto FieldVal =
296300
State->getLValue(I, loc::MemRegionVal(R)).castAs<loc::MemRegionVal>();
297301
const auto *FR = FieldVal.getRegionAs<FieldRegion>();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %clang_analyze_cc1 -analyzer-checker=optin.cplusplus.UninitializedObject -verify %s
2+
// expected-no-diagnostics
3+
4+
struct S
5+
{
6+
S(bool b)
7+
: b(b)
8+
{}
9+
bool b{false};
10+
long long : 7; // padding
11+
};
12+
13+
void f()
14+
{
15+
S s(true);
16+
}

0 commit comments

Comments
 (0)