From 745b5d3d4fd1d5cc97c84d559aa3dc25cc253b7f Mon Sep 17 00:00:00 2001 From: Jade Lovelace Date: Thu, 23 May 2024 15:16:10 -0700 Subject: [PATCH] clang-tidy: work with angle brackets and external projects Also fix the readme Change-Id: I422dff5536bf01d43983621aa01035bd77ac0252 --- clang-tidy/FixIncludes.cc | 43 +++++++++++++++++----------- clang-tidy/README.md | 8 +++--- doc/manual/rl-next/rename-lixexpr.md | 4 +-- 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/clang-tidy/FixIncludes.cc b/clang-tidy/FixIncludes.cc index 8ba350243..602d3d355 100644 --- a/clang-tidy/FixIncludes.cc +++ b/clang-tidy/FixIncludes.cc @@ -44,32 +44,41 @@ void FixIncludesCallbacks::LexedFileChanged(FileID, LexedFileChangeReason, } void FixIncludesCallbacks::InclusionDirective( - SourceLocation, const Token &, StringRef, bool, + SourceLocation, const Token &, StringRef FileName, bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File, StringRef, StringRef, const Module *, SrcMgr::CharacteristicKind) { if (Ignore) return; // FIXME: this is kinda evil, but this is a one-time fixup - const std::string SourceDir = "src/"; + const std::vector SourceDirs = {"src/", "include/lix/"}; - if (File && File->getNameAsRequested().contains(SourceDir)) { - StringRef Name = File->getNameAsRequested(); - auto Idx = Name.find(SourceDir); - assert(Idx != std::string::npos); - StringRef Suffix = Name.drop_front(Idx + SourceDir.length()); + const auto Bracketize = [IsAngled](StringRef s) { + return IsAngled ? ("<" + s + ">").str() : ("\"" + s + "\"").str(); + }; - if (!Suffix.starts_with("lib")) { - llvm::dbgs() << "ignored: " << Suffix << "\n"; - return; + for (const auto &SourceDir : SourceDirs) { + const bool IsAlreadyFixed = FileName.starts_with("lix/lib"); + if (File && File->getNameAsRequested().contains(SourceDir) && + !IsAlreadyFixed) { + StringRef Name = File->getNameAsRequested(); + auto Idx = Name.find(SourceDir); + assert(Idx != std::string::npos); + std::string Suffix = Name.drop_front(Idx + SourceDir.length()).str(); + + if (!Suffix.starts_with("lib")) { + llvm::dbgs() << "ignored: " << Suffix << "\n"; + return; + } + + Suffix = "lix/" + Suffix; + + auto Diag = Check.diag(FilenameRange.getBegin(), + "include needs to specify the source subdir"); + + Diag << FilenameRange + << FixItHint::CreateReplacement(FilenameRange, Bracketize(Suffix)); } - - auto Diag = Check.diag(FilenameRange.getBegin(), - "include needs to specify the source subdir"); - - Diag << FilenameRange - << FixItHint::CreateReplacement(FilenameRange, - ("\"" + Suffix + "\"").str()); } } diff --git a/clang-tidy/README.md b/clang-tidy/README.md index cf46c71ec..c2d1cb258 100644 --- a/clang-tidy/README.md +++ b/clang-tidy/README.md @@ -1,6 +1,6 @@ -# Clang tidy lints for Nix +# Clang tidy lints for Lix -This is a skeleton of a clang-tidy lints library for Nix. +This is a skeleton of a clang-tidy lints library for Lix. Currently there is one check (which is already obsolete as it has served its goal and is there as an example), `HasPrefixSuffixCheck`. @@ -10,13 +10,13 @@ goal and is there as an example), `HasPrefixSuffixCheck`. One file: ``` -ninja -C build && clang-tidy --checks='-*,nix-*' --load=build/libnix-clang-tidy.so -p ../compile_commands.json --fix ../src/libcmd/installables.cc +ninja -C build && clang-tidy --checks='-*,lix-*' --load=build/liblix-clang-tidy.so -p ../compile_commands.json -header-filter '\.\./src/.*\.h' --fix ../src/libcmd/installables.cc ``` Several files, in parallel: ``` -ninja -C build && run-clang-tidy -checks='-*,nix-*' -load=build/libnix-clang-tidy.so -p .. -fix ../src | tee -a clang-tidy-result +ninja -C build && run-clang-tidy -checks='-*,lix-*' -load=build/liblix-clang-tidy.so -p .. -header-filter '\.\./src/.*\.h' -fix ../src | tee -a clang-tidy-result ``` ## Resources diff --git a/doc/manual/rl-next/rename-lixexpr.md b/doc/manual/rl-next/rename-lixexpr.md index 698553c7f..32e3cfd09 100644 --- a/doc/manual/rl-next/rename-lixexpr.md +++ b/doc/manual/rl-next/rename-lixexpr.md @@ -22,10 +22,10 @@ Migration path: To apply this migration automatically, remove all `` from includes, so `#include ` -> `#include `. Then, the correct paths will be resolved from the tangled mess, and the clang-tidy automated fix will work. -Then run the following for out of tree projects: +Then run the following for out of tree projects (header filter is set to only fix instances in headers in `../src` relative to the compiler's working directory, as would be the case in nix-eval-jobs or other things built with meson, e.g.): ```console lix_root=$HOME/lix (cd $lix_root/clang-tidy && nix develop -c 'meson setup build && ninja -C build') -run-clang-tidy -checks='-*,lix-fixincludes' -load=$lix_root/clang-tidy/build/liblix-clang-tidy.so -p build/ -fix src +run-clang-tidy -checks='-*,lix-fixincludes' -load=$lix_root/clang-tidy/build/liblix-clang-tidy.so -p build/ -header-filter '\.\./src/.*\.h' -fix src ```