[PATCH] D128677: [clangd] Support #import insertions
dgoldman updated this revision to Diff 479343. dgoldman added a comment. Run clang-format Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D128677/new/ https://reviews.llvm.org/D128677 Files: clang-tools-extra/clangd/CodeComplete.cpp clang-tools-extra/clangd/Headers.cpp clang-tools-extra/clangd/Headers.h clang-tools-extra/clangd/IncludeFixer.cpp clang-tools-extra/clangd/IncludeFixer.h clang-tools-extra/clangd/unittests/HeadersTests.cpp clang/include/clang/Tooling/Inclusions/HeaderIncludes.h clang/lib/Tooling/Inclusions/HeaderIncludes.cpp Index: clang/lib/Tooling/Inclusions/HeaderIncludes.cpp === --- clang/lib/Tooling/Inclusions/HeaderIncludes.cpp +++ clang/lib/Tooling/Inclusions/HeaderIncludes.cpp @@ -342,7 +342,8 @@ } llvm::Optional -HeaderIncludes::insert(llvm::StringRef IncludeName, bool IsAngled) const { +HeaderIncludes::insert(llvm::StringRef IncludeName, bool IsAngled, + bool IsImport) const { assert(IncludeName == trimInclude(IncludeName)); // If a ("header") already exists in code, "header" () with // different quotation will still be inserted. @@ -372,7 +373,8 @@ } assert(InsertOffset <= Code.size()); std::string NewInclude = - std::string(llvm::formatv("#include {0}\n", QuotedName)); + IsImport ? std::string(llvm::formatv("#import {0}\n", QuotedName)) + : std::string(llvm::formatv("#include {0}\n", QuotedName)); // When inserting headers at end of the code, also append '\n' to the code // if it does not end with '\n'. // FIXME: when inserting multiple #includes at the end of code, only one Index: clang/include/clang/Tooling/Inclusions/HeaderIncludes.h === --- clang/include/clang/Tooling/Inclusions/HeaderIncludes.h +++ clang/include/clang/Tooling/Inclusions/HeaderIncludes.h @@ -51,9 +51,9 @@ HeaderIncludes(llvm::StringRef FileName, llvm::StringRef Code, const IncludeStyle ); - /// Inserts an #include directive of \p Header into the code. If \p IsAngled - /// is true, \p Header will be quoted with <> in the directive; otherwise, it - /// will be quoted with "". + /// Inserts an #include or #import directive of \p Header into the code. + /// If \p IsAngled is true, \p Header will be quoted with <> in the directive; + /// otherwise, it will be quoted with "". /// /// When searching for points to insert new header, this ignores #include's /// after the #include block(s) in the beginning of a file to avoid inserting @@ -70,13 +70,13 @@ /// same category in the code that should be sorted after \p IncludeName. If /// \p IncludeName already exists (with exactly the same spelling), this /// returns None. - llvm::Optional insert(llvm::StringRef Header, - bool IsAngled) const; + llvm::Optional + insert(llvm::StringRef Header, bool IsAngled, bool IsImport = false) const; /// Removes all existing #includes of \p Header quoted with <> if \p IsAngled /// is true or "" if \p IsAngled is false. - /// This doesn't resolve the header file path; it only deletes #includes with - /// exactly the same spelling. + /// This doesn't resolve the header file path; it only deletes #includes and + /// #imports with exactly the same spelling. tooling::Replacements remove(llvm::StringRef Header, bool IsAngled) const; // Matches a whole #include directive. Index: clang-tools-extra/clangd/unittests/HeadersTests.cpp === --- clang-tools-extra/clangd/unittests/HeadersTests.cpp +++ clang-tools-extra/clangd/unittests/HeadersTests.cpp @@ -117,7 +117,8 @@ return Path.value_or(""); } - llvm::Optional insert(llvm::StringRef VerbatimHeader) { + llvm::Optional insert(llvm::StringRef VerbatimHeader, + bool ViaImport) { Clang = setupClang(); PreprocessOnlyAction Action; EXPECT_TRUE( @@ -126,7 +127,7 @@ IncludeInserter Inserter(MainFile, /*Code=*/"", format::getLLVMStyle(), CDB.getCompileCommand(MainFile)->Directory, >getPreprocessor().getHeaderSearchInfo()); -auto Edit = Inserter.insert(VerbatimHeader); +auto Edit = Inserter.insert(VerbatimHeader, ViaImport); Action.EndSourceFile(); return Edit; } @@ -330,9 +331,13 @@ } TEST_F(HeadersTest, PreferInserted) { - auto Edit = insert(""); + auto Edit = insert("", /*ViaImport=*/false); EXPECT_TRUE(Edit); - EXPECT_TRUE(StringRef(Edit->newText).contains("")); + EXPECT_EQ(Edit->newText, "#include \n"); + + Edit = insert("\"header.h\"", /*ViaImport=*/true); + EXPECT_TRUE(Edit); + EXPECT_EQ(Edit->newText, "#import \"header.h\"\n"); } TEST(Headers, NoHeaderSearchInfo) { Index:
[PATCH] D128677: [clangd] Support #import insertions
dgoldman updated this revision to Diff 479342. dgoldman added a comment. Rebase Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D128677/new/ https://reviews.llvm.org/D128677 Files: clang-tools-extra/clangd/CodeComplete.cpp clang-tools-extra/clangd/Headers.cpp clang-tools-extra/clangd/Headers.h clang-tools-extra/clangd/IncludeFixer.cpp clang-tools-extra/clangd/IncludeFixer.h clang-tools-extra/clangd/unittests/HeadersTests.cpp clang/include/clang/Tooling/Inclusions/HeaderIncludes.h clang/lib/Tooling/Inclusions/HeaderIncludes.cpp Index: clang/lib/Tooling/Inclusions/HeaderIncludes.cpp === --- clang/lib/Tooling/Inclusions/HeaderIncludes.cpp +++ clang/lib/Tooling/Inclusions/HeaderIncludes.cpp @@ -342,7 +342,8 @@ } llvm::Optional -HeaderIncludes::insert(llvm::StringRef IncludeName, bool IsAngled) const { +HeaderIncludes::insert(llvm::StringRef IncludeName, bool IsAngled, + bool IsImport) const { assert(IncludeName == trimInclude(IncludeName)); // If a ("header") already exists in code, "header" () with // different quotation will still be inserted. @@ -372,7 +373,8 @@ } assert(InsertOffset <= Code.size()); std::string NewInclude = - std::string(llvm::formatv("#include {0}\n", QuotedName)); + IsImport ? std::string(llvm::formatv("#import {0}\n", QuotedName)) + : std::string(llvm::formatv("#include {0}\n", QuotedName)); // When inserting headers at end of the code, also append '\n' to the code // if it does not end with '\n'. // FIXME: when inserting multiple #includes at the end of code, only one Index: clang/include/clang/Tooling/Inclusions/HeaderIncludes.h === --- clang/include/clang/Tooling/Inclusions/HeaderIncludes.h +++ clang/include/clang/Tooling/Inclusions/HeaderIncludes.h @@ -51,9 +51,9 @@ HeaderIncludes(llvm::StringRef FileName, llvm::StringRef Code, const IncludeStyle ); - /// Inserts an #include directive of \p Header into the code. If \p IsAngled - /// is true, \p Header will be quoted with <> in the directive; otherwise, it - /// will be quoted with "". + /// Inserts an #include or #import directive of \p Header into the code. + /// If \p IsAngled is true, \p Header will be quoted with <> in the directive; + /// otherwise, it will be quoted with "". /// /// When searching for points to insert new header, this ignores #include's /// after the #include block(s) in the beginning of a file to avoid inserting @@ -70,13 +70,13 @@ /// same category in the code that should be sorted after \p IncludeName. If /// \p IncludeName already exists (with exactly the same spelling), this /// returns None. - llvm::Optional insert(llvm::StringRef Header, - bool IsAngled) const; + llvm::Optional + insert(llvm::StringRef Header, bool IsAngled, bool IsImport = false) const; /// Removes all existing #includes of \p Header quoted with <> if \p IsAngled /// is true or "" if \p IsAngled is false. - /// This doesn't resolve the header file path; it only deletes #includes with - /// exactly the same spelling. + /// This doesn't resolve the header file path; it only deletes #includes and + /// #imports with exactly the same spelling. tooling::Replacements remove(llvm::StringRef Header, bool IsAngled) const; // Matches a whole #include directive. Index: clang-tools-extra/clangd/unittests/HeadersTests.cpp === --- clang-tools-extra/clangd/unittests/HeadersTests.cpp +++ clang-tools-extra/clangd/unittests/HeadersTests.cpp @@ -117,7 +117,8 @@ return Path.value_or(""); } - llvm::Optional insert(llvm::StringRef VerbatimHeader) { + llvm::Optional insert(llvm::StringRef VerbatimHeader, + bool ViaImport) { Clang = setupClang(); PreprocessOnlyAction Action; EXPECT_TRUE( @@ -126,7 +127,7 @@ IncludeInserter Inserter(MainFile, /*Code=*/"", format::getLLVMStyle(), CDB.getCompileCommand(MainFile)->Directory, >getPreprocessor().getHeaderSearchInfo()); -auto Edit = Inserter.insert(VerbatimHeader); +auto Edit = Inserter.insert(VerbatimHeader, ViaImport); Action.EndSourceFile(); return Edit; } @@ -330,9 +331,13 @@ } TEST_F(HeadersTest, PreferInserted) { - auto Edit = insert(""); + auto Edit = insert("", /*ViaImport=*/false); EXPECT_TRUE(Edit); - EXPECT_TRUE(StringRef(Edit->newText).contains("")); + EXPECT_EQ(Edit->newText, "#include \n"); + + Edit = insert("\"header.h\"", /*ViaImport=*/true); + EXPECT_TRUE(Edit); + EXPECT_EQ(Edit->newText, "#import \"header.h\"\n"); } TEST(Headers, NoHeaderSearchInfo) { Index:
[PATCH] D128677: [clangd] Support #import insertions
dgoldman created this revision. dgoldman added a reviewer: kadircet. Herald added subscribers: usaxena95, arphaman. Herald added a project: All. dgoldman requested review of this revision. Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov. Herald added projects: clang, clang-tools-extra. While it currently still defaults to #include instead of #import, this support can be used in the future to provide #import insertions for headers with ObjC symbols once SymbolCollector is updated. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D128677 Files: clang-tools-extra/clangd/CodeComplete.cpp clang-tools-extra/clangd/Headers.cpp clang-tools-extra/clangd/Headers.h clang-tools-extra/clangd/IncludeFixer.cpp clang-tools-extra/clangd/IncludeFixer.h clang-tools-extra/clangd/unittests/HeadersTests.cpp clang/include/clang/Tooling/Inclusions/HeaderIncludes.h clang/lib/Tooling/Inclusions/HeaderIncludes.cpp Index: clang/lib/Tooling/Inclusions/HeaderIncludes.cpp === --- clang/lib/Tooling/Inclusions/HeaderIncludes.cpp +++ clang/lib/Tooling/Inclusions/HeaderIncludes.cpp @@ -341,7 +341,8 @@ } llvm::Optional -HeaderIncludes::insert(llvm::StringRef IncludeName, bool IsAngled) const { +HeaderIncludes::insert(llvm::StringRef IncludeName, bool IsAngled, + bool IsImport) const { assert(IncludeName == trimInclude(IncludeName)); // If a ("header") already exists in code, "header" () with // different quotation will still be inserted. @@ -371,7 +372,8 @@ } assert(InsertOffset <= Code.size()); std::string NewInclude = - std::string(llvm::formatv("#include {0}\n", QuotedName)); + IsImport ? std::string(llvm::formatv("#import {0}\n", QuotedName)) + : std::string(llvm::formatv("#include {0}\n", QuotedName)); // When inserting headers at end of the code, also append '\n' to the code // if it does not end with '\n'. // FIXME: when inserting multiple #includes at the end of code, only one Index: clang/include/clang/Tooling/Inclusions/HeaderIncludes.h === --- clang/include/clang/Tooling/Inclusions/HeaderIncludes.h +++ clang/include/clang/Tooling/Inclusions/HeaderIncludes.h @@ -51,9 +51,9 @@ HeaderIncludes(llvm::StringRef FileName, llvm::StringRef Code, const IncludeStyle ); - /// Inserts an #include directive of \p Header into the code. If \p IsAngled - /// is true, \p Header will be quoted with <> in the directive; otherwise, it - /// will be quoted with "". + /// Inserts an #include or #import directive of \p IncludeName into the code. + /// If \p IsAngled is true, \p IncludeName will be quoted with <> in the + /// directive; otherwise, it will be quoted with "". /// /// When searching for points to insert new header, this ignores #include's /// after the #include block(s) in the beginning of a file to avoid inserting @@ -70,13 +70,13 @@ /// same category in the code that should be sorted after \p IncludeName. If /// \p IncludeName already exists (with exactly the same spelling), this /// returns None. - llvm::Optional insert(llvm::StringRef Header, - bool IsAngled) const; + llvm::Optional + insert(llvm::StringRef Header, bool IsAngled, bool IsImport = false) const; /// Removes all existing #includes of \p Header quoted with <> if \p IsAngled /// is true or "" if \p IsAngled is false. - /// This doesn't resolve the header file path; it only deletes #includes with - /// exactly the same spelling. + /// This doesn't resolve the header file path; it only deletes #includes and + /// #imports with exactly the same spelling. tooling::Replacements remove(llvm::StringRef Header, bool IsAngled) const; private: Index: clang-tools-extra/clangd/unittests/HeadersTests.cpp === --- clang-tools-extra/clangd/unittests/HeadersTests.cpp +++ clang-tools-extra/clangd/unittests/HeadersTests.cpp @@ -115,7 +115,8 @@ return Path.value_or(""); } - llvm::Optional insert(llvm::StringRef VerbatimHeader) { + llvm::Optional insert(llvm::StringRef VerbatimHeader, + bool ViaImport) { Clang = setupClang(); PreprocessOnlyAction Action; EXPECT_TRUE( @@ -124,7 +125,7 @@ IncludeInserter Inserter(MainFile, /*Code=*/"", format::getLLVMStyle(), CDB.getCompileCommand(MainFile)->Directory, >getPreprocessor().getHeaderSearchInfo()); -auto Edit = Inserter.insert(VerbatimHeader); +auto Edit = Inserter.insert(VerbatimHeader, ViaImport); Action.EndSourceFile(); return Edit; } @@ -328,9 +329,13 @@ } TEST_F(HeadersTest, PreferInserted) { - auto Edit = insert(""); + auto Edit = insert("", /*ViaImport=*/false);