Skip to content

Commit c310782

Browse files
authored
[clang-tidy][readability-identifier-naming] Resolve symlinks for checking style for file (#81985)
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. Test Plan: check-clang-tools
1 parent ad78e21 commit c310782

File tree

5 files changed

+32
-2
lines changed

5 files changed

+32
-2
lines changed

clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,13 +1408,16 @@ const IdentifierNamingCheck::FileStyle &
14081408
IdentifierNamingCheck::getStyleForFile(StringRef FileName) const {
14091409
if (!GetConfigPerFile)
14101410
return *MainFileStyle;
1411-
StringRef Parent = llvm::sys::path::parent_path(FileName);
1411+
1412+
SmallString<128> RealFileName;
1413+
llvm::sys::fs::real_path(FileName, RealFileName);
1414+
StringRef Parent = llvm::sys::path::parent_path(RealFileName);
14121415
auto Iter = NamingStylesCache.find(Parent);
14131416
if (Iter != NamingStylesCache.end())
14141417
return Iter->getValue();
14151418

14161419
llvm::StringRef CheckName = getID();
1417-
ClangTidyOptions Options = Context->getOptionsForFile(FileName);
1420+
ClangTidyOptions Options = Context->getOptionsForFile(RealFileName);
14181421
if (Options.Checks && GlobList(*Options.Checks).contains(CheckName)) {
14191422
auto It = NamingStylesCache.try_emplace(
14201423
Parent,

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ Changes in existing checks
188188
<clang-tidy/checks/readability/redundant-inline-specifier>` check to properly
189189
emit warnings for static data member with an in-class initializer.
190190

191+
- Improved :doc:`readability-identifier-naming
192+
<clang-tidy/checks/readability/identifier-naming>` check in `GetConfigPerFile`
193+
mode by resolving symbolic links to header files.
194+
191195
Removed checks
192196
^^^^^^^^^^^^^^
193197

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Checks: readability-identifier-naming
2+
CheckOptions:
3+
readability-identifier-naming.GlobalConstantCase: CamelCase
4+
readability-identifier-naming.GlobalConstantPrefix: k
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const int global_const = 5;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Specify `-std=c++20` to run test only once becuase test expects changes
2+
// in the header file so it fails if runs multiple times with different
3+
// `-std` flags as check_clang_tidy doesn by default.
4+
//
5+
// RUN: rm -rf %T/symlink
6+
// RUN: cp -r %S/Inputs/identifier-naming/symlink %T/symlink
7+
// RUN: mkdir -p %T/symlink/build
8+
// RUN: ln -s %T/symlink/include/test.h %T/symlink/build/test.h
9+
// 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
10+
// UNSUPPORTED: system-windows
11+
12+
#include "test.h"
13+
14+
int foo() {
15+
return global_const;
16+
// CHECK-MESSAGES: warning: invalid case style for global constant 'global_const' [readability-identifier-naming]
17+
// CHECK-FIXES: return kGlobalConst;
18+
}

0 commit comments

Comments
 (0)