Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,8 @@ void ProTypeMemberInitCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
}

// FIXME: Copied from clang/lib/Sema/SemaDeclCXX.cpp.
static bool isIncompleteOrZeroLengthArrayType(ASTContext &Context, QualType T) {
static bool isIncompleteOrZeroLengthArrayType(const ASTContext &Context,
QualType T) {
if (T->isIncompleteArrayType())
return true;

Expand All @@ -375,7 +376,7 @@ static bool isIncompleteOrZeroLengthArrayType(ASTContext &Context, QualType T) {
return false;
}

static bool isEmpty(ASTContext &Context, const QualType &Type) {
static bool isEmpty(const ASTContext &Context, const QualType &Type) {
if (const CXXRecordDecl *ClassDecl = Type->getAsCXXRecordDecl()) {
return ClassDecl->isEmpty();
}
Expand Down Expand Up @@ -582,6 +583,33 @@ void ProTypeMemberInitCheck::checkMissingBaseClassInitializer(

void ProTypeMemberInitCheck::checkUninitializedTrivialType(
const ASTContext &Context, const VarDecl *Var) {
// Verify that the record actually needs initialization
const CXXRecordDecl *Record = Var->getType()->getAsCXXRecordDecl();
if (!Record)
return;

SmallPtrSet<const FieldDecl *, 16> FieldsToInit;
bool AnyMemberHasInitPerUnion = false;
forEachFieldWithFilter(
*Record, Record->fields(), AnyMemberHasInitPerUnion,
[&](const FieldDecl *F) {
if (IgnoreArrays && F->getType()->isArrayType())
return;
if (F->hasInClassInitializer() && F->getParent()->isUnion()) {
AnyMemberHasInitPerUnion = true;
removeFieldInitialized(F, FieldsToInit);
}
if (!F->hasInClassInitializer() &&
utils::type_traits::isTriviallyDefaultConstructible(F->getType(),
Context) &&
!isEmpty(Context, F->getType()) && !F->isUnnamedBitField() &&
!AnyMemberHasInitPerUnion)
FieldsToInit.insert(F);
});

if (FieldsToInit.empty())
return;

const DiagnosticBuilder Diag =
diag(Var->getBeginLoc(), "uninitialized record type: %0") << Var;

Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,11 @@ Changes in existing checks
adding an option to allow pointer arithmetic via prefix/postfix increment or
decrement operators.

- Improved :doc:`cppcoreguidelines-pro-type-member-init
<clang-tidy/checks/cppcoreguidelines/pro-type-member-init>` check to
correctly ignore ``std::array`` and other containers when `IgnoreArrays`
option is set to `true`.

- Improved :doc:`google-readability-casting
<clang-tidy/checks/google/readability-casting>` check by adding fix-it
notes for downcasts and casts to void pointer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,39 @@ struct HasArrayMember {
int RawArray[4];
int Number;
};

namespace std {
template <typename T, int N>
struct array {
T _Elems[N];
void fill(const T &);
};
}

void test_local_std_array() {
std::array<int, 4> a;
}

struct OnlyArray {
int a[4];
};

void test_local_only_array() {
OnlyArray a;
}

struct Mixed {
int a[4];
int b;
};

void test_local_mixed() {
Mixed m;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: uninitialized record type: 'm'
}

void test_std_array_fill() {
std::array<char, 10> someArray;
// CHECK-MESSAGES-NOT: warning: uninitialized record type: 'someArray'
someArray.fill('n');
}