-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[clang-tidy] Fix cppcoreguidelines-pro-type-member-init check
#169832
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zeyi2
wants to merge
2
commits into
llvm:main
Choose a base branch
from
zeyi2:fix-cppcg-pro-type-member-init
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+71
−2
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Member
|
@llvm/pr-subscribers-clang-tools-extra @llvm/pr-subscribers-clang-tidy Author: mitchell (zeyi2) ChangesCloses #169677 Full diff: https://github.com/llvm/llvm-project/pull/169832.diff 3 Files Affected:
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
index 66508da89f0dd..f8c096b4b4c66 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
@@ -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;
@@ -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();
}
@@ -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;
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index a6f80e3721db1..3ad2ffc07da5c 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -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.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init.ignorearrays.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init.ignorearrays.cpp
index 01859b3ad98f4..e4cfe679cfce9 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init.ignorearrays.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-member-init.ignorearrays.cpp
@@ -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');
+}
|
Co-authored-by: EugeneZelenko <eugene.zelenko@gmail.com>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes #169677