[clang-tools-extra] 6a80e56 - [clang-tidy] Fix macros handling in cppcoreguidelines-prefer-member-initializer (#72037)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Jan 22 07:17:37 PST 2024
Author: Piotr Zegar
Date: 2024-01-22T16:17:33+01:00
New Revision: 6a80e56ad0c7ae0adb8c4fb3f88eab7643566f41
URL: https://github.com/llvm/llvm-project/commit/6a80e56ad0c7ae0adb8c4fb3f88eab7643566f41
DIFF: https://github.com/llvm/llvm-project/commit/6a80e56ad0c7ae0adb8c4fb3f88eab7643566f41.diff
LOG: [clang-tidy] Fix macros handling in cppcoreguidelines-prefer-member-initializer (#72037)
Produces now valid fixes for a member variables initialized with macros.
Correctly uses expansion location instead of location inside macro to
get init code.
Close #70189
Added:
Modified:
clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/prefer-member-initializer.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp
index 23d7303371529d..f79a67819bb858 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp
@@ -261,9 +261,9 @@ void PreferMemberInitializerCheck::check(
SmallString<128> Insertion(
{UseAssignment ? " = " : "{",
- Lexer::getSourceText(
- CharSourceRange(InitValue->getSourceRange(), true),
- *Result.SourceManager, getLangOpts()),
+ Lexer::getSourceText(Result.SourceManager->getExpansionRange(
+ InitValue->getSourceRange()),
+ *Result.SourceManager, getLangOpts()),
UseAssignment ? "" : "}"});
Diag << FixItHint::CreateInsertion(FieldEnd, Insertion)
@@ -346,7 +346,7 @@ void PreferMemberInitializerCheck::check(
if (InvalidFix)
continue;
StringRef NewInit = Lexer::getSourceText(
- CharSourceRange(InitValue->getSourceRange(), true),
+ Result.SourceManager->getExpansionRange(InitValue->getSourceRange()),
*Result.SourceManager, getLangOpts());
if (HasInitAlready) {
if (InsertPos.isValid())
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 2822b17b435143..7ab091e10231cf 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -337,7 +337,8 @@ Changes in existing checks
- Improved :doc:`cppcoreguidelines-prefer-member-initializer
<clang-tidy/checks/cppcoreguidelines/prefer-member-initializer>` check to
ignore delegate constructors and ignore re-assignment for reference or when
- initialization depend on field that is initialized before.
+ initialization depend on field that is initialized before. Additionally, it
+ now provides valid fixes for member variables initialized with macros.
- Improved :doc:`cppcoreguidelines-pro-bounds-array-to-pointer-decay
<clang-tidy/checks/cppcoreguidelines/pro-bounds-array-to-pointer-decay>` check
diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/prefer-member-initializer.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/prefer-member-initializer.cpp
index b5603dea316d59..8086caa2aa6f2c 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/prefer-member-initializer.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/prefer-member-initializer.cpp
@@ -586,3 +586,33 @@ struct ReassignmentAfterUnsafetyAssignment {
}
int m_i;
};
+
+namespace PR70189 {
+#define RGB(r,g,b) ((unsigned long)(((unsigned char)(r)|((unsigned short)((unsigned char)(g))<<8))|(((unsigned long)(unsigned char)(b))<<16)))
+#define INVALID_HANDLE_VALUE ((void*)(unsigned long long)-1)
+#define SIMPLE 12
+
+class Foo {
+public:
+ Foo() {
+// CHECK-FIXES: Foo() : m_color(RGB(255, 128, 0)), m_handle(INVALID_HANDLE_VALUE), m_myval(SIMPLE) {
+ m_color = RGB(255, 128, 0);
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'm_color' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer]
+// CHECK-FIXES: {{^\ *$}}
+ m_handle = INVALID_HANDLE_VALUE;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'm_handle' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer]
+// CHECK-FIXES: {{^\ *$}}
+ m_myval = SIMPLE;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'm_myval' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer]
+// CHECK-FIXES: {{^\ *$}}
+ }
+private:
+ unsigned long m_color;
+ void* m_handle;
+ int m_myval;
+};
+
+#undef SIMPLE
+#undef INVALID_HANDLE_VALUE
+#undef RGB
+}
More information about the cfe-commits
mailing list