Skip to content

[clang-tidy] Rename out-of-line function definitions #91954

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

Merged
merged 1 commit into from
May 21, 2024

Conversation

revane
Copy link
Contributor

@revane revane commented May 13, 2024

Member function templates defined out-of-line were resulting in conflicting naming failures with overlapping usage sets. With this change, out-of-line definitions are treated as a usage of the failure which is the inline declaration.

@llvmbot
Copy link
Member

llvmbot commented May 13, 2024

@llvm/pr-subscribers-clang-tools-extra

@llvm/pr-subscribers-clang-tidy

Author: Edwin Vane (revane)

Changes

Member function templates defined out-of-line were resulting in conflicting naming failures with overlapping usage sets. With this change, out-of-line definitions are treated as a usage of the failure which is the inline declaration.


Full diff: https://github.com/llvm/llvm-project/pull/91954.diff

3 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp (+3)
  • (modified) clang-tools-extra/docs/ReleaseNotes.rst (+2-1)
  • (added) clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-ool.cpp (+30)
diff --git a/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp b/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp
index e811f5519de2c..2959946f51b54 100644
--- a/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp
+++ b/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp
@@ -123,6 +123,9 @@ static const NamedDecl *getFailureForNamedDecl(const NamedDecl *ND) {
   if (const auto *Method = dyn_cast<CXXMethodDecl>(ND)) {
     if (const CXXMethodDecl *Overridden = getOverrideMethod(Method))
       Canonical = cast<NamedDecl>(Overridden->getCanonicalDecl());
+    else if (const FunctionTemplateDecl *Primary = Method->getPrimaryTemplate())
+      Canonical =
+          cast<NamedDecl>(Primary->getTemplatedDecl()->getCanonicalDecl());
 
     if (Canonical != ND)
       return Canonical;
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index fc976ce3a33d5..e86786cfb301a 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -343,7 +343,8 @@ Changes in existing checks
   <clang-tidy/checks/readability/identifier-naming>` check in `GetConfigPerFile`
   mode by resolving symbolic links to header files. Fixed handling of Hungarian
   Prefix when configured to `LowerCase`. Added support for renaming designated
-  initializers. Added support for renaming macro arguments.
+  initializers. Added support for renaming macro arguments. Fixed renaming
+  conflicts arising from out-of-line member function template definitions.
 
 - Improved :doc:`readability-implicit-bool-conversion
   <clang-tidy/checks/readability/implicit-bool-conversion>` check to provide
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-ool.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-ool.cpp
new file mode 100644
index 0000000000000..f807875e27698
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-ool.cpp
@@ -0,0 +1,30 @@
+// RUN: %check_clang_tidy %s readability-identifier-naming %t -std=c++20 \
+// RUN:   --config='{CheckOptions: { \
+// RUN:     readability-identifier-naming.MethodCase: CamelCase, \
+// RUN:  }}'
+
+namespace SomeNamespace {
+namespace Inner {
+
+class SomeClass {
+public:
+    template <typename T>
+    int someMethod();
+// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: invalid case style for method 'someMethod' [readability-identifier-naming]
+// CHECK-FIXES: {{^}}    int SomeMethod();
+};
+template <typename T>
+int SomeClass::someMethod() {
+// CHECK-FIXES: {{^}}int SomeClass::SomeMethod() {
+    return 5;
+}
+
+} // namespace Inner
+
+void someFunc() {
+    Inner::SomeClass S;
+    S.someMethod<int>();
+// CHECK-FIXES: {{^}}    S.SomeMethod<int>();
+}
+
+} // namespace SomeNamespace

@revane
Copy link
Contributor Author

revane commented May 13, 2024

@PiotrZSL Finally fixing renaming of out-of-line template member functions.

Copy link
Member

@PiotrZSL PiotrZSL left a comment

Choose a reason for hiding this comment

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

LGTM

@revane
Copy link
Contributor Author

revane commented May 16, 2024

@PiotrZSL If everything looks good could you merge this PR for me?

@carlosgalvezp
Copy link
Contributor

A bit late for the review but would have been nice to name the test "out-of-line" instead of "ool", so it's easier to understand what it's about.

Member function templates defined out-of-line were resulting in
conflicting naming failures with overlapping usage sets. With this
change, out-of-line definitions are treated as a usage of the failure
which is the inline declaration.
@revane
Copy link
Contributor Author

revane commented May 17, 2024

It's not too late until it's submitted. I renamed the file as requested.

@PiotrZSL PiotrZSL merged commit e67f2cc into llvm:main May 21, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants