[clang-tools-extra] [clang-tidy][readability-identifier-naming] Resolve symlinks for checking style for file (PR #81985)

Dmitry Polukhin via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 16 11:54:15 PST 2024


https://github.com/dmpolukhin updated https://github.com/llvm/llvm-project/pull/81985

>From ddafb4672e1a481d4a9556ebe31ca9a07e1f3569 Mon Sep 17 00:00:00 2001
From: Dmitry Polukhin <dmitry.polukhin at gmail.com>
Date: Fri, 16 Feb 2024 03:51:07 -0800
Subject: [PATCH 1/3] [clang-tidy][readability-identifier-naming] Resolve
 symlinks for checking style for file

Summary:
Some build systems create symlinks in a temporary build directory for headers in the source tree for isolation purposes. These symlinks prevent `readability-identifier-naming` detecting issues and applying fixes. Without this fix clang-tidy is checking .clang-tidy config file in a temporary directory instead of source source location.

If you think this change may have undesired results, I can add an option to activate it only when users enable symlink resolution explicitly.

Test Plan: check-clang-tools
---
 .../readability/IdentifierNamingCheck.cpp         |  7 +++++--
 .../Inputs/identifier-naming/symlink/build/test.h |  1 +
 .../identifier-naming/symlink/include/.clang-tidy |  4 ++++
 .../identifier-naming/symlink/include/test.h      |  1 +
 .../readability/identifier-naming-symlink.cpp     | 15 +++++++++++++++
 5 files changed, 26 insertions(+), 2 deletions(-)
 create mode 120000 clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/identifier-naming/symlink/build/test.h
 create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/identifier-naming/symlink/include/.clang-tidy
 create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/identifier-naming/symlink/include/test.h
 create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-symlink.cpp

diff --git a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
index 5db9e99ab23708..335c3de25b861b 100644
--- a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
@@ -1408,13 +1408,16 @@ const IdentifierNamingCheck::FileStyle &
 IdentifierNamingCheck::getStyleForFile(StringRef FileName) const {
   if (!GetConfigPerFile)
     return *MainFileStyle;
-  StringRef Parent = llvm::sys::path::parent_path(FileName);
+
+  SmallString<128> RealFileName;
+  llvm::sys::fs::real_path(FileName, RealFileName);
+  StringRef Parent = llvm::sys::path::parent_path(RealFileName);
   auto Iter = NamingStylesCache.find(Parent);
   if (Iter != NamingStylesCache.end())
     return Iter->getValue();
 
   llvm::StringRef CheckName = getID();
-  ClangTidyOptions Options = Context->getOptionsForFile(FileName);
+  ClangTidyOptions Options = Context->getOptionsForFile(RealFileName);
   if (Options.Checks && GlobList(*Options.Checks).contains(CheckName)) {
     auto It = NamingStylesCache.try_emplace(
         Parent,
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/identifier-naming/symlink/build/test.h b/clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/identifier-naming/symlink/build/test.h
new file mode 120000
index 00000000000000..de218fbfa4662a
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/identifier-naming/symlink/build/test.h
@@ -0,0 +1 @@
+../include/test.h
\ No newline at end of file
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/identifier-naming/symlink/include/.clang-tidy b/clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/identifier-naming/symlink/include/.clang-tidy
new file mode 100644
index 00000000000000..296550f3aab1eb
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/identifier-naming/symlink/include/.clang-tidy
@@ -0,0 +1,4 @@
+Checks: readability-identifier-naming
+CheckOptions:
+  readability-identifier-naming.GlobalConstantCase: CamelCase
+  readability-identifier-naming.GlobalConstantPrefix: k
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/identifier-naming/symlink/include/test.h b/clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/identifier-naming/symlink/include/test.h
new file mode 100644
index 00000000000000..f3560e4e50b9ed
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/identifier-naming/symlink/include/test.h
@@ -0,0 +1 @@
+const int global_const = 5;
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-symlink.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-symlink.cpp
new file mode 100644
index 00000000000000..72926bff908df8
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-symlink.cpp
@@ -0,0 +1,15 @@
+// Specify `-std=c++20` to run test only once becuase test expects changes
+// in the header file so it fails if runs multiple times with different
+// `-std` flags as check_clang_tidy doesn by default.
+//
+// RUN: rm -rf %T/symlink
+// RUN: cp -r %S/Inputs/identifier-naming/symlink %T/symlink
+// RUN: %check_clang_tidy -std=c++20 %s readability-identifier-naming %t -- --header-filter="test.h" --config-file=%S/Inputs/identifier-naming/symlink/include/.clang-tidy -- -I %T/symlink/build
+
+#include "test.h"
+
+int foo() {
+    return global_const;
+    // CHECK-MESSAGES: warning: invalid case style for global constant 'global_const' [readability-identifier-naming]
+    // CHECK-FIXES: return kGlobalConst;
+}

>From 96a172a4a9c6204cc6bb11fef472ac60d5fb64d1 Mon Sep 17 00:00:00 2001
From: Dmitry Polukhin <dmitry.polukhin at gmail.com>
Date: Fri, 16 Feb 2024 08:58:37 -0800
Subject: [PATCH 2/3] Create symlinks on the fly, added release note, disabled
 test on Windows.

---
 clang-tools-extra/docs/ReleaseNotes.rst                       | 4 ++++
 .../readability/Inputs/identifier-naming/symlink/build/test.h | 1 -
 .../checkers/readability/identifier-naming-symlink.cpp        | 3 +++
 3 files changed, 7 insertions(+), 1 deletion(-)
 delete mode 120000 clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/identifier-naming/symlink/build/test.h

diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index f2fba9aa1450d6..e956bfe6c44fe3 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -164,6 +164,10 @@ Changes in existing checks
   `AllowStringArrays` option, enabling the exclusion of array types with deduced
   length initialized from string literals.
 
+- Improved :doc:`readability-identifier-naming
+  <clang-tidy/checks/readability/identifier-naming>` check in ``GetConfigPerFile: true``
+  mode by resolving symbolic links to header files.
+
 Removed checks
 ^^^^^^^^^^^^^^
 
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/identifier-naming/symlink/build/test.h b/clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/identifier-naming/symlink/build/test.h
deleted file mode 120000
index de218fbfa4662a..00000000000000
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/identifier-naming/symlink/build/test.h
+++ /dev/null
@@ -1 +0,0 @@
-../include/test.h
\ No newline at end of file
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-symlink.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-symlink.cpp
index 72926bff908df8..34dc340178dccc 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-symlink.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-symlink.cpp
@@ -4,7 +4,10 @@
 //
 // RUN: rm -rf %T/symlink
 // RUN: cp -r %S/Inputs/identifier-naming/symlink %T/symlink
+// RUN: mkdir -p %T/symlink/build
+// RUN: ln -s %T/symlink/include/test.h %T/symlink/build/test.h
 // RUN: %check_clang_tidy -std=c++20 %s readability-identifier-naming %t -- --header-filter="test.h" --config-file=%S/Inputs/identifier-naming/symlink/include/.clang-tidy -- -I %T/symlink/build
+// UNSUPPORTED: system-windows
 
 #include "test.h"
 

>From 4b46158a770101f0e10bb8f4d2ac8749214ebc02 Mon Sep 17 00:00:00 2001
From: Dmitry Polukhin <dmitry.polukhin at gmail.com>
Date: Fri, 16 Feb 2024 11:52:13 -0800
Subject: [PATCH 3/3] Comments resolved

---
 clang-tools-extra/docs/ReleaseNotes.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index e956bfe6c44fe3..d80d0b854c7e8a 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -165,7 +165,7 @@ Changes in existing checks
   length initialized from string literals.
 
 - Improved :doc:`readability-identifier-naming
-  <clang-tidy/checks/readability/identifier-naming>` check in ``GetConfigPerFile: true``
+  <clang-tidy/checks/readability/identifier-naming>` check in `GetConfigPerFile`
   mode by resolving symbolic links to header files.
 
 Removed checks



More information about the cfe-commits mailing list