Skip to content

Commit

Permalink
[clang-tidy][readability-identifier-naming] Resolve symlinks for chec…
Browse files Browse the repository at this point in the history
…king 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
  • Loading branch information
dmpolukhin committed Feb 19, 2024
1 parent ad78e21 commit c310782
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ Changes in existing checks
<clang-tidy/checks/readability/redundant-inline-specifier>` check to properly
emit warnings for static data member with an in-class initializer.

- Improved :doc:`readability-identifier-naming
<clang-tidy/checks/readability/identifier-naming>` check in `GetConfigPerFile`
mode by resolving symbolic links to header files.

Removed checks
^^^^^^^^^^^^^^

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Checks: readability-identifier-naming
CheckOptions:
readability-identifier-naming.GlobalConstantCase: CamelCase
readability-identifier-naming.GlobalConstantPrefix: k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const int global_const = 5;
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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: 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"

int foo() {
return global_const;
// CHECK-MESSAGES: warning: invalid case style for global constant 'global_const' [readability-identifier-naming]
// CHECK-FIXES: return kGlobalConst;
}

0 comments on commit c310782

Please sign in to comment.