Author: Joe Turner Date: 2020-10-26T12:24:43-07:00 New Revision: 1e076a8d8099a6c179c8773f2e621f9408ee2402
URL: https://github.com/llvm/llvm-project/commit/1e076a8d8099a6c179c8773f2e621f9408ee2402 DIFF: https://github.com/llvm/llvm-project/commit/1e076a8d8099a6c179c8773f2e621f9408ee2402.diff LOG: Make sure Objective-C category support in IncludeSorter handles top-level imports Currently, this would not correctly associate a category with the related include if it was top-level (i.e. no slashes in the path). This ensures that we explicitly think about that case. Reviewed By: gribozavr2 Differential Revision: https://reviews.llvm.org/D89608 Added: Modified: clang-tools-extra/clang-tidy/utils/IncludeSorter.cpp clang-tools-extra/unittests/clang-tidy/IncludeInserterTest.cpp Removed: ################################################################################ diff --git a/clang-tools-extra/clang-tidy/utils/IncludeSorter.cpp b/clang-tools-extra/clang-tidy/utils/IncludeSorter.cpp index f0e56a0af448..56c5bcae64ba 100644 --- a/clang-tools-extra/clang-tidy/utils/IncludeSorter.cpp +++ b/clang-tools-extra/clang-tidy/utils/IncludeSorter.cpp @@ -45,8 +45,12 @@ StringRef MakeCanonicalName(StringRef Str, IncludeSorter::IncludeStyle Style) { // Objective-C categories have a `+suffix` format, but should be grouped // with the file they are a category of. + size_t StartIndex = Canonical.find_last_of('/'); + if (StartIndex == StringRef::npos) { + StartIndex = 0; + } return Canonical.substr( - 0, Canonical.find_first_of('+', Canonical.find_last_of('/'))); + 0, Canonical.find_first_of('+', StartIndex)); } return RemoveFirstSuffix( RemoveFirstSuffix(Str, {".cc", ".cpp", ".c", ".h", ".hpp"}), diff --git a/clang-tools-extra/unittests/clang-tidy/IncludeInserterTest.cpp b/clang-tools-extra/unittests/clang-tidy/IncludeInserterTest.cpp index f1c9ba743542..1499197e9486 100644 --- a/clang-tools-extra/unittests/clang-tidy/IncludeInserterTest.cpp +++ b/clang-tools-extra/unittests/clang-tidy/IncludeInserterTest.cpp @@ -133,7 +133,7 @@ class ObjCCategoryHeaderInserterCheck : public IncludeInserterCheckBase { utils::IncludeSorter::IS_Google_ObjC) {} std::vector<StringRef> headersToInclude() const override { - return {"clang_tidy/tests/insert_includes_test_header+foo.h"}; + return {"top_level_test_header+foo.h"}; } }; @@ -158,6 +158,10 @@ std::string runCheckOnCode(StringRef Code, StringRef Filename) { {"clang_tidy/tests/" "insert_includes_test_header.h", "\n"}, + // Top-level main file include + + // category. + {"top_level_test_header.h", "\n"}, + {"top_level_test_header+foo.h", "\n"}, // ObjC category. {"clang_tidy/tests/" "insert_includes_test_header+foo.h", @@ -708,23 +712,21 @@ void foo() { TEST(IncludeInserterTest, InsertCategoryHeaderObjectiveC) { const char *PreCode = R"( -#import "clang_tidy/tests/insert_includes_test_header.h" +#import "top_level_test_header.h" void foo() { int a = 0; })"; const char *PostCode = R"( -#import "clang_tidy/tests/insert_includes_test_header.h" -#import "clang_tidy/tests/insert_includes_test_header+foo.h" +#import "top_level_test_header.h" +#import "top_level_test_header+foo.h" void foo() { int a = 0; })"; - EXPECT_EQ( - PostCode, - runCheckOnCode<ObjCCategoryHeaderInserterCheck>( - PreCode, "repo/clang_tidy/tests/insert_includes_test_header.mm")); + EXPECT_EQ(PostCode, runCheckOnCode<ObjCCategoryHeaderInserterCheck>( + PreCode, "top_level_test_header.mm")); } TEST(IncludeInserterTest, InsertGeneratedHeaderObjectiveC) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits